<commit_before><?php

/**
 * This file is part of the OpenPNE package.
 * (c) OpenPNE Project (http://www.openpne.jp/)
 *
 * For the full copyright and license information, please view the LICENSE
 * file and the NOTICE file that were distributed with this source code.
 */

/**
 * opMailMessage
 *
 * @package    OpenPNE
 * @subpackage util
 * @author     <NAME> <<EMAIL>>
 */
class opMailMessage extends Zend_Mail_Message
{
  protected $firstTextPos = 0;

  public function getContent()
  {
    // If this message is multi-part-mail,
    // opMailMessage retrives first text/plain part as a content
    if ($this->isMultiPart())
    {
      if ($this->firstTextPos)
      {
        return $this->getPart($this->firstTextPos)->getContent();
      }

      $content = '';
      $current = $this->key();
      $this->rewind();

      foreach ($this as $part)
      {
        $tok = strtok($part->contentType, ';');
        if ('text/plain' === $tok)
        {
          $this->firstTextPos = $this->key();
          $content = mb_convert_encoding($this->current()->getContent(), 'UTF-8', 'JIS');

          break;
        }
      }

      $this->_iterationPos = $current;

      return $content;
    }

    return mb_convert_encoding(parent::getContent(), 'UTF-8', 'JIS');
  }

  public function getImages()
  {
    if (!$this->isMultiPart())
    {
      return array();
    }

    $images = array();
    $allowTypes = array('image/jpeg', 'image/png', 'image/gif');

    $current = $this->key();
    $this->rewind();

    foreach ($this as $part)
    {
      $tok = strtok($part->contentType, ';');
      if (in_array($tok, $allowTypes))
      {
        $tmppath = tempnam(sys_get_temp_dir(), 'IMG');

        $fh = fopen($tmppath, 'w');
        fwrite($fh, base64_decode($part->getContent(), true));
        fclose($fh);

        $images[] = array(
          'tmp_name' => $tmppath,
          'type'    => $tok,
        );
      }
    }<commit_msg>Removes the last line feed in the base64-encoded data before passing it to base64_decode($data, true). (fixes #2477, BP from #2476 and #2261)<commit_after><?php

/**
 * This file is part of the OpenPNE package.
 * (c) OpenPNE Project (http://www.openpne.jp/)
 *
 * For the full copyright and license information, please view the LICENSE
 * file and the NOTICE file that were distributed with this source code.
 */

/**
 * opMailMessage
 *
 * @package    OpenPNE
 * @subpackage util
 * @author     <NAME> <<EMAIL>>
 */
class opMailMessage extends Zend_Mail_Message
{
  protected $firstTextPos = 0;

  public function getContent()
  {
    // If this message is multi-part-mail,
    // opMailMessage retrives first text/plain part as a content
    if ($this->isMultiPart())
    {
      if ($this->firstTextPos)
      {
        return $this->getPart($this->firstTextPos)->getContent();
      }

      $content = '';
      $current = $this->key();
      $this->rewind();

      foreach ($this as $part)
      {
        $tok = strtok($part->contentType, ';');
        if ('text/plain' === $tok)
        {
          $this->firstTextPos = $this->key();
          $content = mb_convert_encoding($this->current()->getContent(), 'UTF-8', 'JIS');

          break;
        }
      }

      $this->_iterationPos = $current;

      return $content;
    }

    return mb_convert_encoding(parent::getContent(), 'UTF-8', 'JIS');
  }

  public function getImages()
  {
    if (!$this->isMultiPart())
    {
      return array();
    }

    $images = array();
    $allowTypes = array('image/jpeg', 'image/png', 'image/gif');

    $current = $this->key();
    $this->rewind();

    foreach ($this as $part)
    {
      $tok = strtok($part->contentType, ';');
      if (in_array($tok, $allowTypes))
      {
        $tmppath = tempnam(sys_get_temp_dir(), 'IMG');

        $fh = fopen($tmppath, 'w');
        fwrite($fh, base64_decode(rtrim($part->getContent()), true));
        fclose($fh);

        $images[] = array(
          'tmp_name' => $tmppath,
          'type'    => $tok,
        );
      }
    }
{'fraction_non_alphanumeric': 0.16571570329942942, 'fraction_numerical': 0.0064500124038700075, 'mean_word_length': 2.3768844221105527, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, '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': '2468791', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1279, 'n_words': 305}