IMP_Compose::_addAttachment PHP Method

_addAttachment() protected method

Adds an attachment to the outgoing compose message.
protected _addAttachment ( string $atc_file, integer $bytes, string $filename, string $type ) : IMP_Compose_Attachment
$atc_file string Temporary file containing attachment contents.
$bytes integer Size of data, in bytes.
$filename string Filename of data.
$type string MIME type of data.
return IMP_Compose_Attachment Attachment object.
    protected function _addAttachment($atc_file, $bytes, $filename, $type)
    {
        global $conf, $injector;
        $atc = new Horde_Mime_Part();
        $atc->setBytes($bytes);
        /* Try to determine the MIME type from 1) the extension and
         * then 2) analysis of the file (if available). */
        if (strlen($filename)) {
            $atc->setName($filename);
            if ($type == 'application/octet-stream') {
                $type = Horde_Mime_Magic::filenameToMIME($filename, false);
            }
        }
        $atc->setType($type);
        if ($atc->getType() == 'application/octet-stream' || $atc->getPrimaryType() == 'text') {
            $analyze = Horde_Mime_Magic::analyzeFile($atc_file, empty($conf['mime']['magic_db']) ? null : $conf['mime']['magic_db'], array('nostrip' => true));
            $atc->setCharset('UTF-8');
            if ($analyze) {
                $ctype = new Horde_Mime_Headers_ContentParam('Content-Type', $analyze);
                $atc->setType($ctype->value);
                if (isset($ctype->params['charset'])) {
                    $atc->setCharset($ctype->params['charset']);
                }
            }
        } else {
            $atc->setHeaderCharset('UTF-8');
        }
        $atc_ob = new IMP_Compose_Attachment($this, $atc, $atc_file);
        /* Check for attachment size limitations. */
        $size_limit = null;
        if ($atc_ob->linked) {
            if (!empty($conf['compose']['link_attach_size_limit'])) {
                $linked = true;
                $size_limit = 'link_attach_size_limit';
            }
        } elseif (!empty($conf['compose']['attach_size_limit'])) {
            $linked = false;
            $size_limit = 'attach_size_limit';
        }
        if (!is_null($size_limit)) {
            $total_size = $conf['compose'][$size_limit] - $bytes;
            foreach ($this as $val) {
                if ($val->linked == $linked) {
                    $total_size -= $val->getPart()->getBytes();
                }
            }
            if ($total_size < 0) {
                throw new IMP_Compose_Exception(strlen($filename) ? sprintf(_("Attached file \"%s\" exceeds the attachment size limits. File NOT attached."), $filename) : _("Attached file exceeds the attachment size limits. File NOT attached."));
            }
        }
        try {
            $injector->getInstance('Horde_Core_Hooks')->callHook('compose_attachment', 'imp', array($atc_ob));
        } catch (Horde_Exception_HookNotSet $e) {
        }
        $this->_atc[$atc_ob->id] = $atc_ob;
        $this->changed = 'changed';
        return $atc_ob;
    }