PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Mime.php

https://bitbucket.org/mercysam/zfs
PHP | 365 lines | 197 code | 32 blank | 136 comment | 23 complexity | 8934ffa85f1ba91dc72ceb028818e437 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Support class for MultiPart Mime Messages
  22. *
  23. * @category Zend
  24. * @package Zend_Mime
  25. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Mime
  29. {
  30. const TYPE_OCTETSTREAM = 'application/octet-stream';
  31. const TYPE_TEXT = 'text/plain';
  32. const TYPE_HTML = 'text/html';
  33. const ENCODING_7BIT = '7bit';
  34. const ENCODING_8BIT = '8bit';
  35. const ENCODING_QUOTEDPRINTABLE = 'quoted-printable';
  36. const ENCODING_BASE64 = 'base64';
  37. const DISPOSITION_ATTACHMENT = 'attachment';
  38. const DISPOSITION_INLINE = 'inline';
  39. const LINELENGTH = 72;
  40. const LINEEND = "\n";
  41. const MULTIPART_ALTERNATIVE = 'multipart/alternative';
  42. const MULTIPART_MIXED = 'multipart/mixed';
  43. const MULTIPART_RELATED = 'multipart/related';
  44. protected $_boundary;
  45. protected static $makeUnique = 0;
  46. // lookup-Tables for QuotedPrintable
  47. public static $qpKeys = array(
  48. "\x00","\x01","\x02","\x03","\x04","\x05","\x06","\x07",
  49. "\x08","\x09","\x0A","\x0B","\x0C","\x0D","\x0E","\x0F",
  50. "\x10","\x11","\x12","\x13","\x14","\x15","\x16","\x17",
  51. "\x18","\x19","\x1A","\x1B","\x1C","\x1D","\x1E","\x1F",
  52. "\x7F","\x80","\x81","\x82","\x83","\x84","\x85","\x86",
  53. "\x87","\x88","\x89","\x8A","\x8B","\x8C","\x8D","\x8E",
  54. "\x8F","\x90","\x91","\x92","\x93","\x94","\x95","\x96",
  55. "\x97","\x98","\x99","\x9A","\x9B","\x9C","\x9D","\x9E",
  56. "\x9F","\xA0","\xA1","\xA2","\xA3","\xA4","\xA5","\xA6",
  57. "\xA7","\xA8","\xA9","\xAA","\xAB","\xAC","\xAD","\xAE",
  58. "\xAF","\xB0","\xB1","\xB2","\xB3","\xB4","\xB5","\xB6",
  59. "\xB7","\xB8","\xB9","\xBA","\xBB","\xBC","\xBD","\xBE",
  60. "\xBF","\xC0","\xC1","\xC2","\xC3","\xC4","\xC5","\xC6",
  61. "\xC7","\xC8","\xC9","\xCA","\xCB","\xCC","\xCD","\xCE",
  62. "\xCF","\xD0","\xD1","\xD2","\xD3","\xD4","\xD5","\xD6",
  63. "\xD7","\xD8","\xD9","\xDA","\xDB","\xDC","\xDD","\xDE",
  64. "\xDF","\xE0","\xE1","\xE2","\xE3","\xE4","\xE5","\xE6",
  65. "\xE7","\xE8","\xE9","\xEA","\xEB","\xEC","\xED","\xEE",
  66. "\xEF","\xF0","\xF1","\xF2","\xF3","\xF4","\xF5","\xF6",
  67. "\xF7","\xF8","\xF9","\xFA","\xFB","\xFC","\xFD","\xFE",
  68. "\xFF"
  69. );
  70. public static $qpReplaceValues = array(
  71. "=00","=01","=02","=03","=04","=05","=06","=07",
  72. "=08","=09","=0A","=0B","=0C","=0D","=0E","=0F",
  73. "=10","=11","=12","=13","=14","=15","=16","=17",
  74. "=18","=19","=1A","=1B","=1C","=1D","=1E","=1F",
  75. "=7F","=80","=81","=82","=83","=84","=85","=86",
  76. "=87","=88","=89","=8A","=8B","=8C","=8D","=8E",
  77. "=8F","=90","=91","=92","=93","=94","=95","=96",
  78. "=97","=98","=99","=9A","=9B","=9C","=9D","=9E",
  79. "=9F","=A0","=A1","=A2","=A3","=A4","=A5","=A6",
  80. "=A7","=A8","=A9","=AA","=AB","=AC","=AD","=AE",
  81. "=AF","=B0","=B1","=B2","=B3","=B4","=B5","=B6",
  82. "=B7","=B8","=B9","=BA","=BB","=BC","=BD","=BE",
  83. "=BF","=C0","=C1","=C2","=C3","=C4","=C5","=C6",
  84. "=C7","=C8","=C9","=CA","=CB","=CC","=CD","=CE",
  85. "=CF","=D0","=D1","=D2","=D3","=D4","=D5","=D6",
  86. "=D7","=D8","=D9","=DA","=DB","=DC","=DD","=DE",
  87. "=DF","=E0","=E1","=E2","=E3","=E4","=E5","=E6",
  88. "=E7","=E8","=E9","=EA","=EB","=EC","=ED","=EE",
  89. "=EF","=F0","=F1","=F2","=F3","=F4","=F5","=F6",
  90. "=F7","=F8","=F9","=FA","=FB","=FC","=FD","=FE",
  91. "=FF"
  92. );
  93. public static $qpKeysString =
  94. "\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";
  95. /**
  96. * Check if the given string is "printable"
  97. *
  98. * Checks that a string contains no unprintable characters. If this returns
  99. * false, encode the string for secure delivery.
  100. *
  101. * @param string $str
  102. * @return boolean
  103. */
  104. public static function isPrintable($str)
  105. {
  106. return (strcspn($str, self::$qpKeysString) == strlen($str));
  107. }
  108. /**
  109. * Encode a given string with the QUOTED_PRINTABLE mechanism and wrap the lines.
  110. *
  111. * @param string $str
  112. * @param int $lineLength Defaults to {@link LINELENGTH}
  113. * @param int $lineEnd Defaults to {@link LINEEND}
  114. * @return string
  115. */
  116. public static function encodeQuotedPrintable($str,
  117. $lineLength = self::LINELENGTH,
  118. $lineEnd = self::LINEEND)
  119. {
  120. $out = '';
  121. $str = self::_encodeQuotedPrintable($str);
  122. // Split encoded text into separate lines
  123. while ($str) {
  124. $ptr = strlen($str);
  125. if ($ptr > $lineLength) {
  126. $ptr = $lineLength;
  127. }
  128. // Ensure we are not splitting across an encoded character
  129. $pos = strrpos(substr($str, 0, $ptr), '=');
  130. if ($pos !== false && $pos >= $ptr - 2) {
  131. $ptr = $pos;
  132. }
  133. // Check if there is a space at the end of the line and rewind
  134. if ($ptr > 0 && $str[$ptr - 1] == ' ') {
  135. --$ptr;
  136. }
  137. // Add string and continue
  138. $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
  139. $str = substr($str, $ptr);
  140. }
  141. $out = rtrim($out, $lineEnd);
  142. $out = rtrim($out, '=');
  143. return $out;
  144. }
  145. /**
  146. * Converts a string into quoted printable format.
  147. *
  148. * @param string $str
  149. * @return string
  150. */
  151. private static function _encodeQuotedPrintable($str)
  152. {
  153. $str = str_replace('=', '=3D', $str);
  154. $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
  155. $str = rtrim($str);
  156. return $str;
  157. }
  158. /**
  159. * Encode a given string with the QUOTED_PRINTABLE mechanism for Mail Headers.
  160. *
  161. * Mail headers depend on an extended quoted printable algorithm otherwise
  162. * a range of bugs can occur.
  163. *
  164. * @param string $str
  165. * @param string $charset
  166. * @param int $lineLength Defaults to {@link LINELENGTH}
  167. * @param int $lineEnd Defaults to {@link LINEEND}
  168. * @return string
  169. */
  170. public static function encodeQuotedPrintableHeader($str, $charset,
  171. $lineLength = self::LINELENGTH,
  172. $lineEnd = self::LINEEND)
  173. {
  174. // Reduce line-length by the length of the required delimiter, charsets and encoding
  175. $prefix = sprintf('=?%s?Q?', $charset);
  176. $lineLength = $lineLength-strlen($prefix)-3;
  177. $str = self::_encodeQuotedPrintable($str);
  178. // Mail-Header required chars have to be encoded also:
  179. $str = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $str);
  180. // initialize first line, we need it anyways
  181. $lines = array(0 => "");
  182. // Split encoded text into separate lines
  183. $tmp = "";
  184. while(strlen($str) > 0) {
  185. $currentLine = max(count($lines)-1, 0);
  186. $token = self::getNextQuotedPrintableToken($str);
  187. $str = substr($str, strlen($token));
  188. $tmp .= $token;
  189. if( (strlen($token) == 1 && strpbrk($token, '!%,.:;<>'))
  190. || in_array($token, array("=3F", "=20", "=5F")) ) {
  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. }