Returns a mime part converted attachment

@return Zend_Mime_Part
334683
public function toMimePart()
    {
        /** @var Zend_Mime_Part $attachmentMimePart */
        $attachmentMimePart = new Zend_Mime_Part($this->getBinary());

        $attachmentMimePart->type = $this->getMimeType();
        $attachmentMimePart->disposition = $this->getDisposition();
        $attachmentMimePart->encoding = $this->getEncoding();
        $attachmentMimePart->filename = $this->getFileName();

        return $attachmentMimePart;
    }
147351
protected function getAttachmentStream(&$part)
    {
        /** @var resource $temp_fp */
        $temp_fp = tmpfile();

        $headers = $this->getPart('headers', $part);
        $encodingType = array_key_exists('content-transfer-encoding', $headers) ?
            $headers['content-transfer-encoding'] : '';

        if ($temp_fp) {
            if ($this->stream) {
                $start = $part['starting-pos-body'];
                $end = $part['ending-pos-body'];
                fseek($this->stream, $start, SEEK_SET);
                $len = $end - $start;
                $written = 0;
                while ($written < $len) {
                    $write = $len;
                    $data = fread($this->stream, $write);
                    fwrite($temp_fp, $this->decodeContentTransfer($data, $encodingType));
                    $written += $write;
                }
            } elseif ($this->data) {
                $attachment = $this->decodeContentTransfer($this->getPartBodyFromText($part), $encodingType);
                fwrite($temp_fp, $attachment, strlen($attachment));
            }
            fseek($temp_fp, 0, SEEK_SET);
        } else {
            throw new Exception(
                'Could not create temporary files for attachments. Your tmp directory may be unwritable by PHP.'
            );
        }

        return $temp_fp;
    }
739757
public function mime()
    {
        if (!$this->exists()) {
            return false;
        }
        if (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $finfo = finfo_file($finfo, $this->pwd());
            if (!$finfo) {
                return false;
            }
            list($type) = explode(';', $finfo);

            return $type;
        }
        if (function_exists('mime_content_type')) {
            return mime_content_type($this->pwd());
        }

        return false;
    }
910556
public function getRawContent($id, $part = null)
    {
        if ($part !== null) {
            // TODO: implement
            /**
             * @see Zend_Mail_Storage_Exception
             */
            // require_once 'Zend/Mail/Storage/Exception.php';
            throw new Zend_Mail_Storage_Exception('not implemented');
        }

        $fh = fopen($this->_getFileData($id, 'filename'), 'r');

        while (!feof($fh)) {
            $line = fgets($fh);
            if (!trim($line)) {
                break;
            }
        }

        $content = stream_get_contents($fh);
        fclose($fh);
        return $content;
    }
1026742
public function getPart($num)
    {
        if (isset($this->_parts[$num])) {
            return $this->_parts[$num];
        }

        if (!$this->_mail && $this->_content === null) {
            /**
             * @see Zend_Mail_Exception
             */
            // require_once 'Zend/Mail/Exception.php';
            throw new Zend_Mail_Exception('part not found');
        }

        if ($this->_mail && $this->_mail->hasFetchPart) {
            // TODO: fetch part
            // return
        }

        $this->_cacheContent();

        if (!isset($this->_parts[$num])) {
            /**
             * @see Zend_Mail_Exception
             */
            // require_once 'Zend/Mail/Exception.php';
            throw new Zend_Mail_Exception('part not found');
        }

        return $this->_parts[$num];
    }
1270046
public function parse()
    {
        // Validate user based regex
        try {
            preg_match(
                config("{$this->configBase}.parser.file_regex"),
                '',
                $matches
            );

        } catch (\Exception $e) {
            $this->warningCount++;

            return $this->failed('Configuration error in the regular expression');
        }

        foreach ($this->parsedMail->getAttachments() as $attachment) {
            if (strpos($attachment->filename, '.zip') !== false
                && ($attachment->contentType == 'application/octet-stream'
                        || $attachment->contentType == 'application/zip'
                )
            ) {
                $zip = new Zipper;

                if (!$this->createWorkingDir()) {
                    return $this->failed(
                        "Unable to create working directory"
                    );
                }

                file_put_contents($this->tempPath . $attachment->filename, $attachment->getContent());

                $zip->zip($this->tempPath . $attachment->filename);
                $zip->extractTo($this->tempPath);

                foreach ($zip->listFiles() as $index => $compressedFile) {
                    if (strpos($compressedFile, '.csv') !== false) {
                        // For each CSV file we find, we are going to do magic (however they usually only send 1 zip)
                        if (preg_match(
                            config("{$this->configBase}.parser.file_regex"),
                            $compressedFile,
                            $matches
                        )) {
                            $this->feedName = $matches[1];

                            // If feed is known and enabled, validate data and save report
                            if ($this->isKnownFeed() && $this->isEnabledFeed()) {
                                $csvReports = new Reader\CsvReader(
                                    new SplFileObject($this->tempPath . $compressedFile)
                                );
                                $csvReports->setHeaderRowNumber(0);

                                foreach ($csvReports as $report) {

                                    // Handle field mappings first
                                    $aliasses = config("{$this->configBase}.feeds.{$this->feedName}.aliasses");
                                    if (is_array($aliasses)) {
                                        foreach ($aliasses as
                                                 $alias => $real) {
                                            if (array_key_exists($alias, $report)) {
                                                $report[$real] = $report[$alias];
                                                unset($report[$alias]);
                                            }
                                        }
                                    }
                                    /*
                                     * Emergency fix for major false positives from shadowserver feed
                                     * This only happens under very specific conditions and will be filtered out
                                     * as specific as possible.
                                     */
                                    if ($this->feedName == 'spam_url') {
                                        $urlFilter = '/([a-zA-Z]{3,5}.[a-zA-Z]{3,5}.com)/';
                                        if( $report['src_geo'] == 'RU' &&
                                            $report['url'] == "http://{$report['host']}" &&
                                            $report['src_asn'] == "8402" &&
                                            preg_match($urlFilter, $report['host'], $filterMatches)
                                        ) {
                                            continue;
                                        }
                                    }

                                    /*
                                     * Legacy 3.x fix for migrations.
                                     *
                                     * This resolves shadowserver errors where the CSV was send in duplicate resulting
                                     * in the header fields being used as data. If the header is detected the row can
                                     * be skipped safely
                                     */
                                    if ($report['ip'] === 'ip') {
                                        continue;
                                    }

                                    // Sanity check
                                    if ($this->hasRequiredFields($report) === true) {
                                        // incident has all requirements met, filter and add!
                                        $report = $this->applyFilters($report);

                                        $incident = new Incident();
                                        $incident->source      = config("{$this->configBase}.parser.name");
                                        $incident->source_id   = false;
                                        $incident->ip          = $report['ip'];
                                        $incident->domain      = false;
                                        $incident->class       =
                                            config("{$this->configBase}.feeds.{$this->feedName}.class");
                                        $incident->type        =
                                            config("{$this->configBase}.feeds.{$this->feedName}.type");
                                        $incident->timestamp   = strtotime($report['timestamp']);
                                        $incident->information = json_encode($report);

                                        // some rows have a domain, which is an optional column we want to register
                                        switch ($this->feedName) {
                                            case "spam_url":
                                                if (isset($report['url'])) {
                                                    $incident->domain = getDomain($report['url']);
                                                }
                                                break;
                                            case "ssl_scan":
                                                if (isset($report['subject_common_name'])) {

                                                    /*
                                                     * Common name does not add http://, but that is required for
                                                     * the domain helper check so lets add it manually
                                                     */
                                                    $testurl = "http://{$report['subject_common_name']}";

                                                    $incident->domain = getDomain($testurl);
                                                }
                                                break;
                                            case "compromised_website":
                                                if (isset($report['http_host'])) {
                                                    $incident->domain = getDomain($report['http_host']);
                                                }
                                                break;
                                        }

                                        $this->incidents[] = $incident;

                                    } //End hasRequired fields
                                } // End foreach report loop
                            } // End isKnown & isEnabled
                        } else { // Pregmatch failed to get feedName from attachment
                            $this->warningCount++;
                        }
                    } else { // Attached file is not a CSV within a ZIP file
                        $this->warningCount++;
                    }
                } // End each file in ZIP attachment loop
            } // End if not a ZIP attachment
        } // End foreach attachment loop

        return $this->success();
    }
1257636
public function getMimeBoundary()
    {
        if ($this->mimeBoundary === false) {
            $this->mimeBoundary = null;
            $contentType = $this->getContentType();
            if ($contentType !== null) {
                $this->mimeBoundary = $contentType->getValueFor('boundary');
            }
        }
        return $this->mimeBoundary;
    }
1227834
private function encodePart($part)
    {
        if (strpos($part, '?') !== false) {
            if (strpos($part, '?') === 0) {
                $output = '?';
                $part = substr($part, 1);
            } else {
                $pe = explode('?', $part);
                $output = $pe[0].'/?';
                $part = $pe[1];
                unset($pe);
            }

            if (strpos($part, '&')) {
                $parts = explode('&', $part);
            } else {
                $parts = [$part];
            }
            foreach ($parts as &$p) {
                if (strpos($p, '=') !== false) {
                    $pe = explode('=', $p);
                    $p = $pe[0].'='.urlencode($pe[1]);
                }
            }
            unset($p, $pe);

            $output .= implode('&', $parts);

            return $output;
        }

        if (strpos($part, '#') !== false) {
            if (strpos($part, '#') === 0) {
                if (substr($part, 1, 1) === '!') {
                    return '#!'.rawurlencode(substr($part, 2));
                }

                return '#'.rawurlencode(substr($part, 1));
            }

            $pe = explode('#', $part);
            if (substr($pe[1], 0, 1) === '!') {
                return $pe[0].'/#!'.rawurlencode(substr($pe[1], 1));
            }

            return $pe[0].'/#'.rawurlencode($pe[1]);
        }

        return rawurlencode($part);
    }
73991
protected function getAllNonFilteredParts()
    {
        $parts = [ $this ];
        foreach ($this->children as $part) {
            if ($part instanceof MimePart) {
                $parts = array_merge(
                    $parts,
                    $part->getAllNonFilteredParts()
                );
            } else {
                array_push($parts, $part);
            }
        }
        return $parts;
    }
366467
public function getMimeType()
	{
		$finfo = finfo_open(FILEINFO_MIME_TYPE);
		$mime = finfo_file($finfo, $this->path);
		finfo_close($finfo);

		return $mime;
	}
1341095
public function getMime()
    {
        if ($this->metadata->isFile()) {
            $mimes = $this->package->config('mime.types');
            foreach ($mimes as $mime => $mimeValues) {
                $key = collect($mimeValues)->search(function ($mimeValue) {
                    return preg_match($mimeValue, $this->metadata->getMimeType());
                });

                if ($key !== false) {
                    return $mime;
                }
            }

            return 'file';
        }

        return 'dir';
    }
703287
public function getMimeLiteralPartFactory()
    {
        if ($this->mimeLiteralPartFactory === null) {
            $this->mimeLiteralPartFactory = new MimeLiteralPartFactory($this->getCharsetConverter());
        }
        return $this->mimeLiteralPartFactory;
    }
1321893
protected function buildTextPart()
    {
        if ($this->body === false) {
            $result = null;
        } else {
            $result = new MimePart($this->body);
            $result->setType(Mime::TYPE_TEXT);
            $result->setCharset(APP_CHARSET);
        }

        return $result;
    }
231720
public function getMimeType()
    {
        // return mime type ala mimetype extension
        $finfo = finfo_open(FILEINFO_MIME);

        $mime = finfo_file($finfo, $this->getFullPath());

        return $mime;
    }
1314850
public function getMailMIME()
    {
        $result = '';
        $ismultipart = true;
        switch ($this->message_type) {
            case 'inline':
                $result .= $this->headerLine('Content-Type', 'multipart/related;');
                $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
                break;
            case 'attach':
            case 'inline_attach':
            case 'alt_attach':
            case 'alt_inline_attach':
                $result .= $this->headerLine('Content-Type', 'multipart/mixed;');
                $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
                break;
            case 'alt':
            case 'alt_inline':
                $result .= $this->headerLine('Content-Type', 'multipart/alternative;');
                $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
                break;
            default:
                // Catches case 'plain': and case '':
                $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet);
                $ismultipart = false;
                break;
        }
        // RFC1341 part 5 says 7bit is assumed if not specified
        if ($this->Encoding != '7bit') {
            // RFC 2045 section 6.4 says multipart MIME parts may only use 7bit, 8bit or binary CTE
            if ($ismultipart) {
                if ($this->Encoding == '8bit') {
                    $result .= $this->headerLine('Content-Transfer-Encoding', '8bit');
                }
                // The only remaining alternatives are quoted-printable and base64, which are both 7bit compatible
            } else {
                $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding);
            }
        }

        if ($this->Mailer != 'mail') {
            $result .= $this->LE;
        }

        return $result;
    }
598398
private function createMimeFromString($body)
    {
        $bodyPart = new Part($body);
        $bodyPart->setEncoding(Mime::ENCODING_QUOTEDPRINTABLE);
        $bodyPart->setCharset(SmtpTransportZend2::ENCODING);
        ($this->isHTML($body)) ? $bodyPart->setType(Mime::TYPE_HTML) : $bodyPart->setType(Mime::TYPE_TEXT);
        $mimeMessage = new \Zend\Mime\Message();
        $mimeMessage->addPart($bodyPart);
        return $mimeMessage;
    }
494636
public function getAttachmentMimeTemplate($attachment)
    {
        $file = $attachment['file'];
        $data = $attachment['data'];

        $head = array();
        $head[] = "Content-Type: application/octet-stream; name=\"{$file}\"";
        $head[] = "Content-Transfer-Encoding: base64";
        $head[] = "Content-Disposition: attachment; filename=\"{$file}\"";
        $head[] = "";
        $head[] = $data;
        $head[] = "";
        $head[] = "--{$this->_uid}";

        return implode(PHP_EOL, $head);
    }