Mime.php 13 KB

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