[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/openpsa/universalfeedcreator/lib/Creator/ -> MBOXCreator.php (source)

   1  <?php
   2  
   3  /**
   4   * MBOXCreator is a FeedCreator that implements the mbox format
   5   * as described in http://www.qmail.org/man/man5/mbox.html
   6   *
   7   * @since   1.3
   8   * @author  Kai Blankenhorn <kaib@bitfolge.de>
   9   */
  10  class MBOXCreator extends FeedCreator
  11  {
  12  
  13      /**
  14       * MBOXCreator constructor.
  15       */
  16      public function __construct()
  17      {
  18          $this->contentType = "text/plain";
  19          $this->encoding = "ISO-8859-15";
  20      }
  21  
  22      /**
  23       * Quoted Printable encoding
  24       *
  25       * @param string $input
  26       * @param int $line_max wrap lines after these number of characters
  27       * @return string
  28       */
  29      protected function qp_enc($input = "", $line_max = 76)
  30      {
  31          $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
  32          $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
  33          $eol = "\r\n";
  34          $escape = "=";
  35          $output = "";
  36          foreach ($lines as $line) {
  37              $linlen = strlen($line);
  38              $newline = "";
  39              for ($i = 0; $i < $linlen; $i++) {
  40                  $c = substr($line, $i, 1);
  41                  $dec = ord($c);
  42                  if (($dec == 32) && ($i == ($linlen - 1))) { // convert space at eol only
  43                      $c = "=20";
  44                  } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { // always encode "\t", which is *not* required
  45                      $h2 = floor($dec / 16);
  46                      $h1 = floor($dec % 16);
  47                      $c = $escape.$hex["$h2"].$hex["$h1"];
  48                  }
  49                  if ((strlen($newline) + strlen($c)) >= $line_max) { // CRLF is not counted
  50                      $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  51                      $newline = "";
  52                  }
  53                  $newline .= $c;
  54              } // end of for
  55              $output .= $newline.$eol;
  56          }
  57  
  58          return trim($output);
  59      }
  60  
  61      /**
  62       * Builds the MBOX contents.
  63       *
  64       * @inheritdoc
  65       */
  66      public function createFeed()
  67      {
  68          $feed = '';
  69          for ($i = 0; $i < count($this->items); $i++) {
  70              if ($this->items[$i]->author != "") {
  71                  $from = $this->items[$i]->author;
  72              } else {
  73                  $from = $this->title;
  74              }
  75              $itemDate = new FeedDate($this->items[$i]->date);
  76              $feed .= "From ".strtr(MBOXCreator::qp_enc($from), " ", "_")." ".date(
  77                      "D M d H:i:s Y",
  78                      $itemDate->unix()
  79                  )."\n";
  80              $feed .= "Content-Type: text/plain;\n";
  81              $feed .= "    charset=\"".$this->encoding."\"\n";
  82              $feed .= "Content-Transfer-Encoding: quoted-printable\n";
  83              $feed .= "Content-Type: text/plain\n";
  84              $feed .= "From: \"".MBOXCreator::qp_enc($from)."\"\n";
  85              $feed .= "Date: ".$itemDate->rfc822()."\n";
  86              $feed .= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title, 100))."\n";
  87              $feed .= "\n";
  88              $body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description));
  89              $feed .= preg_replace("~\nFrom ([^\n]*)(\n?)~", "\n>From $1$2\n", $body);
  90              $feed .= "\n";
  91              $feed .= "\n";
  92          }
  93  
  94          return $feed;
  95      }
  96  
  97      /**
  98       * Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types.
  99       *
 100       * @return string the feed cache filename
 101       * @since  1.4
 102       * @access private
 103       */
 104      protected function _generateFilename()
 105      {
 106          $fileInfo = pathinfo($_SERVER["SCRIPT_NAME"]);
 107  
 108          return substr($fileInfo["basename"], 0, -(strlen($fileInfo["extension"]) + 1)).".mbox";
 109      }
 110  }