1572dd708SAndreas Gohr<?php 2572dd708SAndreas Gohr 3572dd708SAndreas Gohr/** 4572dd708SAndreas Gohr * MBOXCreator is a FeedCreator that implements the mbox format 5572dd708SAndreas Gohr * as described in http://www.qmail.org/man/man5/mbox.html 6572dd708SAndreas Gohr * 7572dd708SAndreas Gohr * @since 1.3 8572dd708SAndreas Gohr * @author Kai Blankenhorn <kaib@bitfolge.de> 9572dd708SAndreas Gohr */ 10572dd708SAndreas Gohrclass MBOXCreator extends FeedCreator 11572dd708SAndreas Gohr{ 12572dd708SAndreas Gohr 13572dd708SAndreas Gohr /** 14572dd708SAndreas Gohr * MBOXCreator constructor. 15572dd708SAndreas Gohr */ 16572dd708SAndreas Gohr public function __construct() 17572dd708SAndreas Gohr { 18572dd708SAndreas Gohr $this->contentType = "text/plain"; 19572dd708SAndreas Gohr $this->encoding = "ISO-8859-15"; 20572dd708SAndreas Gohr } 21572dd708SAndreas Gohr 22572dd708SAndreas Gohr /** 23572dd708SAndreas Gohr * Quoted Printable encoding 24572dd708SAndreas Gohr * 25572dd708SAndreas Gohr * @param string $input 26572dd708SAndreas Gohr * @param int $line_max wrap lines after these number of characters 27572dd708SAndreas Gohr * @return string 28572dd708SAndreas Gohr */ 29572dd708SAndreas Gohr protected function qp_enc($input = "", $line_max = 76) 30572dd708SAndreas Gohr { 31572dd708SAndreas Gohr $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); 32572dd708SAndreas Gohr $lines = preg_split("/(?:\r\n|\r|\n)/", $input); 33572dd708SAndreas Gohr $eol = "\r\n"; 34572dd708SAndreas Gohr $escape = "="; 35572dd708SAndreas Gohr $output = ""; 36572dd708SAndreas Gohr foreach ($lines as $line) { 37572dd708SAndreas Gohr $linlen = strlen($line); 38572dd708SAndreas Gohr $newline = ""; 39572dd708SAndreas Gohr for ($i = 0; $i < $linlen; $i++) { 40572dd708SAndreas Gohr $c = substr($line, $i, 1); 41572dd708SAndreas Gohr $dec = ord($c); 42572dd708SAndreas Gohr if (($dec == 32) && ($i == ($linlen - 1))) { // convert space at eol only 43572dd708SAndreas Gohr $c = "=20"; 44572dd708SAndreas Gohr } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { // always encode "\t", which is *not* required 45572dd708SAndreas Gohr $h2 = floor($dec / 16); 46572dd708SAndreas Gohr $h1 = floor($dec % 16); 47572dd708SAndreas Gohr $c = $escape.$hex["$h2"].$hex["$h1"]; 48572dd708SAndreas Gohr } 49572dd708SAndreas Gohr if ((strlen($newline) + strlen($c)) >= $line_max) { // CRLF is not counted 50572dd708SAndreas Gohr $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay 51572dd708SAndreas Gohr $newline = ""; 52572dd708SAndreas Gohr } 53572dd708SAndreas Gohr $newline .= $c; 54572dd708SAndreas Gohr } // end of for 55572dd708SAndreas Gohr $output .= $newline.$eol; 56572dd708SAndreas Gohr } 57572dd708SAndreas Gohr 58572dd708SAndreas Gohr return trim($output); 59572dd708SAndreas Gohr } 60572dd708SAndreas Gohr 61572dd708SAndreas Gohr /** 62572dd708SAndreas Gohr * Builds the MBOX contents. 63572dd708SAndreas Gohr * 64572dd708SAndreas Gohr * @inheritdoc 65572dd708SAndreas Gohr */ 66572dd708SAndreas Gohr public function createFeed() 67572dd708SAndreas Gohr { 68572dd708SAndreas Gohr $feed = ''; 69572dd708SAndreas Gohr for ($i = 0; $i < count($this->items); $i++) { 70572dd708SAndreas Gohr if ($this->items[$i]->author != "") { 71572dd708SAndreas Gohr $from = $this->items[$i]->author; 72572dd708SAndreas Gohr } else { 73572dd708SAndreas Gohr $from = $this->title; 74572dd708SAndreas Gohr } 75572dd708SAndreas Gohr $itemDate = new FeedDate($this->items[$i]->date); 76572dd708SAndreas Gohr $feed .= "From ".strtr(MBOXCreator::qp_enc($from), " ", "_")." ".date( 77572dd708SAndreas Gohr "D M d H:i:s Y", 78572dd708SAndreas Gohr $itemDate->unix() 79572dd708SAndreas Gohr )."\n"; 80572dd708SAndreas Gohr $feed .= "Content-Type: text/plain;\n"; 81572dd708SAndreas Gohr $feed .= " charset=\"".$this->encoding."\"\n"; 82572dd708SAndreas Gohr $feed .= "Content-Transfer-Encoding: quoted-printable\n"; 83572dd708SAndreas Gohr $feed .= "Content-Type: text/plain\n"; 84572dd708SAndreas Gohr $feed .= "From: \"".MBOXCreator::qp_enc($from)."\"\n"; 85572dd708SAndreas Gohr $feed .= "Date: ".$itemDate->rfc822()."\n"; 86572dd708SAndreas Gohr $feed .= "Subject: ".MBOXCreator::qp_enc(FeedCreator::iTrunc($this->items[$i]->title, 100))."\n"; 87572dd708SAndreas Gohr $feed .= "\n"; 88572dd708SAndreas Gohr $body = chunk_split(MBOXCreator::qp_enc($this->items[$i]->description)); 89572dd708SAndreas Gohr $feed .= preg_replace("~\nFrom ([^\n]*)(\n?)~", "\n>From $1$2\n", $body); 90572dd708SAndreas Gohr $feed .= "\n"; 91572dd708SAndreas Gohr $feed .= "\n"; 92572dd708SAndreas Gohr } 93572dd708SAndreas Gohr 94572dd708SAndreas Gohr return $feed; 95572dd708SAndreas Gohr } 96572dd708SAndreas Gohr 97572dd708SAndreas Gohr /** 98572dd708SAndreas Gohr * Generate a filename for the feed cache file. Overridden from FeedCreator to prevent XML data types. 99572dd708SAndreas Gohr * 100572dd708SAndreas Gohr * @return string the feed cache filename 101572dd708SAndreas Gohr * @since 1.4 102572dd708SAndreas Gohr * @access private 103572dd708SAndreas Gohr */ 104572dd708SAndreas Gohr protected function _generateFilename() 105572dd708SAndreas Gohr { 106*d3233986SAndreas Gohr $fileInfo = pathinfo($_SERVER["SCRIPT_NAME"]); 107572dd708SAndreas Gohr 108572dd708SAndreas Gohr return substr($fileInfo["basename"], 0, -(strlen($fileInfo["extension"]) + 1)).".mbox"; 109572dd708SAndreas Gohr } 110572dd708SAndreas Gohr} 111