PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Mime.php

https://bitbucket.org/skuda/rsslounge
PHP | 365 lines | 196 code | 32 blank | 137 comment | 21 complexity | 59b07d53b02a489f6f86df75d3b3f560 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mime
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Mime.php 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * Support class for MultiPart Mime Messages
  23. *
  24. * @category Zend
  25. * @package Zend_Mime
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Mime
  30. {
  31. const TYPE_OCTETSTREAM = 'application/octet-stream';
  32. const TYPE_TEXT = 'text/plain';
  33. const TYPE_HTML = 'text/html';
  34. const ENCODING_7BIT = '7bit';
  35. const ENCODING_8BIT = '8bit';
  36. const ENCODING_QUOTEDPRINTABLE = 'quoted-printable';
  37. const ENCODING_BASE64 = 'base64';
  38. const DISPOSITION_ATTACHMENT = 'attachment';
  39. const DISPOSITION_INLINE = 'inline';
  40. const LINELENGTH = 72;
  41. const LINEEND = "\n";
  42. const MULTIPART_ALTERNATIVE = 'multipart/alternative';
  43. const MULTIPART_MIXED = 'multipart/mixed';
  44. const MULTIPART_RELATED = 'multipart/related';
  45. protected $_boundary;
  46. protected static $makeUnique = 0;
  47. // lookup-Tables for QuotedPrintable
  48. public static $qpKeys = array(
  49. "\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
  50. "\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F",
  51. "\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
  52. "\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F",
  53. "\x7F","\x80","\x81","\x82","\x83","\x84","\x85","\x86",
  54. "\x87","\x88","\x89","\x8A","\x8B","\x8C","\x8D","\x8E",
  55. "\x8F","\x90","\x91","\x92","\x93","\x94","\x95","\x96",
  56. "\x97","\x98","\x99","\x9A","\x9B","\x9C","\x9D","\x9E",
  57. "\x9F","\xA0","\xA1","\xA2","\xA3","\xA4","\xA5","\xA6",
  58. "\xA7","\xA8","\xA9","\xAA","\xAB","\xAC","\xAD","\xAE",
  59. "\xAF","\xB0","\xB1","\xB2","\xB3","\xB4","\xB5","\xB6",
  60. "\xB7","\xB8","\xB9","\xBA","\xBB","\xBC","\xBD","\xBE",
  61. "\xBF","\xC0","\xC1","\xC2","\xC3","\xC4","\xC5","\xC6",
  62. "\xC7","\xC8","\xC9","\xCA","\xCB","\xCC","\xCD","\xCE",
  63. "\xCF","\xD0","\xD1","\xD2","\xD3","\xD4","\xD5","\xD6",
  64. "\xD7","\xD8","\xD9","\xDA","\xDB","\xDC","\xDD","\xDE",
  65. "\xDF","\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6",
  66. "\xE7","\xE8","\xE9","\xEA","\xEB","\xEC","\xED","\xEE",
  67. "\xEF","\xF0","\xF1","\xF2","\xF3","\xF4","\xF5","\xF6",
  68. "\xF7","\xF8","\xF9","\xFA","\xFB","\xFC","\xFD","\xFE",
  69. "\xFF"
  70. );
  71. public static $qpReplaceValues = array(
  72. "=00","=01","=02","=03","=04","=05","=06","=07",
  73. "=08","=09","=0A","=0B","=0C","=0D","=0E","=0F",
  74. "=10","=11","=12","=13","=14","=15","=16","=17",
  75. "=18","=19","=1A","=1B","=1C","=1D","=1E","=1F",
  76. "=7F","=80","=81","=82","=83","=84","=85","=86",
  77. "=87","=88","=89","=8A","=8B","=8C","=8D","=8E",
  78. "=8F","=90","=91","=92","=93","=94","=95","=96",
  79. "=97","=98","=99","=9A","=9B","=9C","=9D","=9E",
  80. "=9F","=A0","=A1","=A2","=A3","=A4","=A5","=A6",
  81. "=A7","=A8","=A9","=AA","=AB","=AC","=AD","=AE",
  82. "=AF","=B0","=B1","=B2","=B3","=B4","=B5","=B6",
  83. "=B7","=B8","=B9","=BA","=BB","=BC","=BD","=BE",
  84. "=BF","=C0","=C1","=C2","=C3","=C4","=C5","=C6",
  85. "=C7","=C8","=C9","=CA","=CB","=CC","=CD","=CE",
  86. "=CF","=D0","=D1","=D2","=D3","=D4","=D5","=D6",
  87. "=D7","=D8","=D9","=DA","=DB","=DC","=DD","=DE",
  88. "=DF","=E0","=E1","=E2","=E3","=E4","=E5","=E6",
  89. "=E7","=E8","=E9","=EA","=EB","=EC","=ED","=EE",
  90. "=EF","=F0","=F1","=F2","=F3","=F4","=F5","=F6",
  91. "=F7","=F8","=F9","=FA","=FB","=FC","=FD","=FE",
  92. "=FF"
  93. );
  94. public static $qpKeysString =
  95. "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
  96. /**
  97. * Check if the given string is "printable"
  98. *
  99. * Checks that a string contains no unprintable characters. If this returns
  100. * false, encode the string for secure delivery.
  101. *
  102. * @param string $str
  103. * @return boolean
  104. */
  105. public static function isPrintable($str)
  106. {
  107. return (strcspn($str, self::$qpKeysString) == strlen($str));
  108. }
  109. /**
  110. * Encode a given string with the QUOTED_PRINTABLE mechanism and wrap the lines.
  111. *
  112. * @param string $str
  113. * @param int $lineLength Defaults to {@link LINELENGTH}
  114. * @param int $lineEnd Defaults to {@link LINEEND}
  115. * @return string
  116. */
  117. public static function encodeQuotedPrintable($str,
  118. $lineLength = self::LINELENGTH,
  119. $lineEnd = self::LINEEND)
  120. {
  121. $out = '';
  122. $str = self::_encodeQuotedPrintable($str);
  123. // Split encoded text into separate lines
  124. while ($str) {
  125. $ptr = strlen($str);
  126. if ($ptr > $lineLength) {
  127. $ptr = $lineLength;
  128. }
  129. // Ensure we are not splitting across an encoded character
  130. $pos = strrpos(substr($str, 0, $ptr), '=');
  131. if ($pos !== false && $pos >= $ptr - 2) {
  132. $ptr = $pos;
  133. }
  134. // Check if there is a space at the end of the line and rewind
  135. if ($ptr > 0 && $str[$ptr - 1] == ' ') {
  136. --$ptr;
  137. }
  138. // Add string and continue
  139. $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
  140. $str = substr($str, $ptr);
  141. }
  142. $out = rtrim($out, $lineEnd);
  143. $out = rtrim($out, '=');
  144. return $out;
  145. }
  146. /**
  147. * Converts a string into quoted printable format.
  148. *
  149. * @param string $str
  150. * @return string
  151. */
  152. private static function _encodeQuotedPrintable($str)
  153. {
  154. $str = str_replace('=', '=3D', $str);
  155. $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
  156. $str = rtrim($str);
  157. return $str;
  158. }
  159. /**
  160. * Encode a given string with the QUOTED_PRINTABLE mechanism for Mail Headers.
  161. *
  162. * Mail headers depend on an extended quoted printable algorithm otherwise
  163. * a range of bugs can occur.
  164. *
  165. * @param string $str
  166. * @param string $charset
  167. * @param int $lineLength Defaults to {@link LINELENGTH}
  168. * @param int $lineEnd Defaults to {@link LINEEND}
  169. * @return string
  170. */
  171. public static function encodeQuotedPrintableHeader($str, $charset,
  172. $lineLength = self::LINELENGTH,
  173. $lineEnd = self::LINEEND)
  174. {
  175. // Reduce line-length by the length of the required delimiter, charsets and encoding
  176. $prefix = sprintf('=?%s?Q?', $charset);
  177. $lineLength = $lineLength-strlen($prefix)-3;
  178. $str = self::_encodeQuotedPrintable($str);
  179. // Mail-Header required chars have to be encoded also:
  180. $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str);
  181. // initialize first line, we need it anyways
  182. $lines = array(0 => "");
  183. // Split encoded text into separate lines
  184. $tmp = "";
  185. while(strlen($str) > 0) {
  186. $currentLine = max(count($lines)-1, 0);
  187. $token = self::getNextQuotedPrintableToken($str);
  188. $str = substr($str, strlen($token));
  189. $tmp .= $token;
  190. if($token == '=20') {
  191. // only if we have a single char token or space, we can append the
  192. // tempstring it to the current line or start a new line if necessary.
  193. if(strlen($lines[$currentLine].$tmp) > $lineLength) {
  194. $lines[$currentLine+1] = $tmp;
  195. } else {
  196. $lines[$currentLine] .= $tmp;
  197. }
  198. $tmp = "";
  199. }
  200. // don't forget to append the rest to the last line
  201. if(strlen($str) == 0) {
  202. $lines[$currentLine] .= $tmp;
  203. }
  204. }
  205. // assemble the lines together by pre- and appending delimiters, charset, encoding.
  206. for($i = 0; $i < count($lines); $i++) {
  207. $lines[$i] = " ".$prefix.$lines[$i]."?=";
  208. }
  209. $str = trim(implode($lineEnd, $lines));
  210. return $str;
  211. }
  212. /**
  213. * Retrieves the first token from a quoted printable string.
  214. *
  215. * @param string $str
  216. * @return string
  217. */
  218. private static function getNextQuotedPrintableToken($str)
  219. {
  220. if(substr($str, 0, 1) == "=") {
  221. $token = substr($str, 0, 3);
  222. } else {
  223. $token = substr($str, 0, 1);
  224. }
  225. return $token;
  226. }
  227. /**
  228. * Encode a given string in mail header compatible base64 encoding.
  229. *
  230. * @param string $str
  231. * @param string $charset
  232. * @param int $lineLength Defaults to {@link LINELENGTH}
  233. * @param int $lineEnd Defaults to {@link LINEEND}
  234. * @return string
  235. */
  236. public static function encodeBase64Header($str,
  237. $charset,
  238. $lineLength = self::LINELENGTH,
  239. $lineEnd = self::LINEEND)
  240. {
  241. $prefix = '=?' . $charset . '?B?';
  242. $suffix = '?=';
  243. $remainingLength = $lineLength - strlen($prefix) - strlen($suffix);
  244. $encodedValue = self::encodeBase64($str, $remainingLength, $lineEnd);
  245. $encodedValue = str_replace($lineEnd, $suffix . $lineEnd . ' ' . $prefix, $encodedValue);
  246. $encodedValue = $prefix . $encodedValue . $suffix;
  247. return $encodedValue;
  248. }
  249. /**
  250. * Encode a given string in base64 encoding and break lines
  251. * according to the maximum linelength.
  252. *
  253. * @param string $str
  254. * @param int $lineLength Defaults to {@link LINELENGTH}
  255. * @param int $lineEnd Defaults to {@link LINEEND}
  256. * @return string
  257. */
  258. public static function encodeBase64($str,
  259. $lineLength = self::LINELENGTH,
  260. $lineEnd = self::LINEEND)
  261. {
  262. return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
  263. }
  264. /**
  265. * Constructor
  266. *
  267. * @param null|string $boundary
  268. * @access public
  269. * @return void
  270. */
  271. public function __construct($boundary = null)
  272. {
  273. // This string needs to be somewhat unique
  274. if ($boundary === null) {
  275. $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++);
  276. } else {
  277. $this->_boundary = $boundary;
  278. }
  279. }
  280. /**
  281. * Encode the given string with the given encoding.
  282. *
  283. * @param string $str
  284. * @param string $encoding
  285. * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  286. * @return string
  287. */
  288. public static function encode($str, $encoding, $EOL = self::LINEEND)
  289. {
  290. switch ($encoding) {
  291. case self::ENCODING_BASE64:
  292. return self::encodeBase64($str, self::LINELENGTH, $EOL);
  293. case self::ENCODING_QUOTEDPRINTABLE:
  294. return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL);
  295. default:
  296. /**
  297. * @todo 7Bit and 8Bit is currently handled the same way.
  298. */
  299. return $str;
  300. }
  301. }
  302. /**
  303. * Return a MIME boundary
  304. *
  305. * @access public
  306. * @return string
  307. */
  308. public function boundary()
  309. {
  310. return $this->_boundary;
  311. }
  312. /**
  313. * Return a MIME boundary line
  314. *
  315. * @param mixed $EOL Defaults to {@link LINEEND}
  316. * @access public
  317. * @return string
  318. */
  319. public function boundaryLine($EOL = self::LINEEND)
  320. {
  321. return $EOL . '--' . $this->_boundary . $EOL;
  322. }
  323. /**
  324. * Return MIME ending
  325. *
  326. * @access public
  327. * @return string
  328. */
  329. public function mimeEnd($EOL = self::LINEEND)
  330. {
  331. return $EOL . '--' . $this->_boundary . '--' . $EOL;
  332. }
  333. }