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