<commit_before><?php

namespace eXorus\PhpMimeMailParser;
use eXorus\PhpMimeMailParser\Attachment;

/**
 * Fast Mime Mail parser Class using PHP's MailParse Extension
 * @author <EMAIL>
 * @url http://www.fijiwebdesign.com/
 * @license http://creativecommons.org/licenses/by-sa/3.0/us/
 * @version r27
 *
 * This fork found on: https://github.com/eXorus/php-mime-mail-parser/
 * with contributions by eXorus 
 */

/**
 * Remove following require_once if you have PSR-0 class loader
 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
 */
require_once __DIR__ . '/Attachment.php';

class Parser
{

    /**
     * PHP MimeParser Resource ID
     */
    public $resource;

    /**
     * A file pointer to email
     */
    public $stream;

    /**
     * A text of an email
     */
    public $data;

    /**
     * Stream Resources for Attachments
     */
    public $attachmentStreams;

    /**
     * Array of Content-Id
     */
    public $attachmentContentId;

    /**
     * Array of New attribut src for Content-Id
     */
    public $attachmentNewSrc;

    /**
     * Inialize some stuff
     * @return
     */
    public function __construct()
    {
        $this->attachmentStreams = array();
        $this->attachmentContentId = array();
        $this->attachmentNewSrc = array();
    }

    /**
     * Free the held resouces
     * @return void
     */
    public function __destruct()
    {
        // clear the email file resource
        if (is_resource($this->stream)) {
            fclose($this->stream);
        }
        // clear the MailParse resource
        if (is_resource($this->resource)) {
            mailparse_msg_free($this->resource);
        }
        // remove attachment resources
        foreach ($this->attachmentStreams as $stream) {
            fclose($stream);
        }
    }

    /**
     * Set the file path we use to get the email text
     * @return Object MimeMailParser Instance
     * @param $mail_path Object
     */
    public function setPath($path)
    {
        // should parse message incrementally from file
        $this->resource = mailparse_msg_parse_file($path);
        $this->stream = fopen($path, 'r');
        $this->parse();
        return $this;
    }

    /**
     * Set the Stream resource we use to get the email text
     * @return Object MimeMailParser Instance
     * @param $stream Resource
     */
    public function setStream($stream)
    {
        // streams have to be cached to file first
        if (get_resource_type($stream) == 'stream') {
            $tmp_fp = tmpfile();
            if ($tmp_fp) {
                while (!feof($stream)) {
                    fwrite($tmp_fp, fread($stream, 2028));
                }
                fseek($tmp_fp, 0);
                $this->stream =& $tmp_fp;
            } else {
                throw new Exception('Could not create temporary files for attachments. Your tmp directory may be unwritable by PHP.');
                return false;
            }
            fclose($stream);
        } else {
            $this->stream = $stream;
        }

        $this->resource = mailparse_msg_create();
        // parses the message incrementally (low memory usage but slower)
        while (!feof($this->stream)) {
            mailparse_msg_parse($this->resource, fread($this->stream, 2082));
        }
        $this->parse();
        return $this;
    }

    /**
     * Set the email text
     * @return Object MimeMailParser Instance
     * @param $data String
     */
    public function setText($data)
    {
        $this->resource = \mailparse_msg_create();
        // does not parse incrementally, fast memory hog might explode
        mailparse_msg_parse($this->resource, $data);
        $this->data = $data;
        $this->parse();
        return $this;
    }

    /**
     * Parse the Message into parts
     * @return void
     * @private
     */
    private function parse()
    {
        $structure = mailparse_msg_get_structure($this->resource);
        $this->parts = array();
        foreach ($structure as $part_id) {
            $part = mailparse_msg_get_part($this->resource, $part_id);
            $this->parts[$part_id] = mailparse_msg_get_part_data($part);
        }
    }

    /**
     * Retrieve the Email Decoded Headers
     * @return Array
     */
    public function getHeaders()
    {
        if (isset($this->parts[1])) {
            $headers = $this->getPartHeaders($this->parts[1]);
            foreach ($headers as &$header) {
                $header = $this->_decodeHeader($header);
            }
        } else {
            throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email headers.');
            return false;
        }
    }
    /**
     * Retrieve the Email Raw Headers
     * @return string
     */
    public function getHeadersRaw()
    {
        if (isset($this->parts[1])) {
            return $this->getPartHeaderRaw($this->parts[1]);
        } else {
            throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email headers.');
            return false;
        }
    }<commit_msg>forgot to return the $headers<commit_after><?php

namespace eXorus\PhpMimeMailParser;
use eXorus\PhpMimeMailParser\Attachment;

/**
 * Fast Mime Mail parser Class using PHP's MailParse Extension
 * @author <EMAIL>
 * @url http://www.fijiwebdesign.com/
 * @license http://creativecommons.org/licenses/by-sa/3.0/us/
 * @version r27
 *
 * This fork found on: https://github.com/eXorus/php-mime-mail-parser/
 * with contributions by eXorus 
 */

/**
 * Remove following require_once if you have PSR-0 class loader
 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
 */
require_once __DIR__ . '/Attachment.php';

class Parser
{

    /**
     * PHP MimeParser Resource ID
     */
    public $resource;

    /**
     * A file pointer to email
     */
    public $stream;

    /**
     * A text of an email
     */
    public $data;

    /**
     * Stream Resources for Attachments
     */
    public $attachmentStreams;

    /**
     * Array of Content-Id
     */
    public $attachmentContentId;

    /**
     * Array of New attribut src for Content-Id
     */
    public $attachmentNewSrc;

    /**
     * Inialize some stuff
     * @return
     */
    public function __construct()
    {
        $this->attachmentStreams = array();
        $this->attachmentContentId = array();
        $this->attachmentNewSrc = array();
    }

    /**
     * Free the held resouces
     * @return void
     */
    public function __destruct()
    {
        // clear the email file resource
        if (is_resource($this->stream)) {
            fclose($this->stream);
        }
        // clear the MailParse resource
        if (is_resource($this->resource)) {
            mailparse_msg_free($this->resource);
        }
        // remove attachment resources
        foreach ($this->attachmentStreams as $stream) {
            fclose($stream);
        }
    }

    /**
     * Set the file path we use to get the email text
     * @return Object MimeMailParser Instance
     * @param $mail_path Object
     */
    public function setPath($path)
    {
        // should parse message incrementally from file
        $this->resource = mailparse_msg_parse_file($path);
        $this->stream = fopen($path, 'r');
        $this->parse();
        return $this;
    }

    /**
     * Set the Stream resource we use to get the email text
     * @return Object MimeMailParser Instance
     * @param $stream Resource
     */
    public function setStream($stream)
    {
        // streams have to be cached to file first
        if (get_resource_type($stream) == 'stream') {
            $tmp_fp = tmpfile();
            if ($tmp_fp) {
                while (!feof($stream)) {
                    fwrite($tmp_fp, fread($stream, 2028));
                }
                fseek($tmp_fp, 0);
                $this->stream =& $tmp_fp;
            } else {
                throw new Exception('Could not create temporary files for attachments. Your tmp directory may be unwritable by PHP.');
                return false;
            }
            fclose($stream);
        } else {
            $this->stream = $stream;
        }

        $this->resource = mailparse_msg_create();
        // parses the message incrementally (low memory usage but slower)
        while (!feof($this->stream)) {
            mailparse_msg_parse($this->resource, fread($this->stream, 2082));
        }
        $this->parse();
        return $this;
    }

    /**
     * Set the email text
     * @return Object MimeMailParser Instance
     * @param $data String
     */
    public function setText($data)
    {
        $this->resource = \mailparse_msg_create();
        // does not parse incrementally, fast memory hog might explode
        mailparse_msg_parse($this->resource, $data);
        $this->data = $data;
        $this->parse();
        return $this;
    }

    /**
     * Parse the Message into parts
     * @return void
     * @private
     */
    private function parse()
    {
        $structure = mailparse_msg_get_structure($this->resource);
        $this->parts = array();
        foreach ($structure as $part_id) {
            $part = mailparse_msg_get_part($this->resource, $part_id);
            $this->parts[$part_id] = mailparse_msg_get_part_data($part);
        }
    }

    /**
     * Retrieve the Email Decoded Headers
     * @return Array
     */
    public function getHeaders()
    {
        if (isset($this->parts[1])) {
            $headers = $this->getPartHeaders($this->parts[1]);
            foreach ($headers as &$header) {
                $header = $this->_decodeHeader($header);
            }
            return $headers;
        } else {
            throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email headers.');
            return false;
        }
    }
    /**
     * Retrieve the Email Raw Headers
     * @return string
     */
    public function getHeadersRaw()
    {
        if (isset($this->parts[1])) {
            return $this->getPartHeaderRaw($this->parts[1]);
        } else {
            throw new Exception('MimeMailParser::setPath() or MimeMailParser::setText() must be called before retrieving email headers.');
            return false;
        }
    }
{'fraction_non_alphanumeric': 0.1216099664590321, 'fraction_numerical': 0.0036415908001916626, 'mean_word_length': 1.9355836849507735, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 71, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5512388', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2919, 'n_words': 756}