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

/www/libs/Zend/Mime/Mime.php

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