PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Mime.php

https://github.com/ostric/e-learning
PHP | 252 lines | 136 code | 21 blank | 95 comment | 11 complexity | c6863305e022ca7748914d845d195505 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
  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 = str_replace('=', '=3D', $str);
  122. $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str);
  123. $str = rtrim($str);
  124. // Split encoded text into separate lines
  125. while ($str) {
  126. $ptr = strlen($str);
  127. if ($ptr > $lineLength) {
  128. $ptr = $lineLength;
  129. }
  130. // Ensure we are not splitting across an encoded character
  131. $pos = strrpos(substr($str, 0, $ptr), '=');
  132. if ($pos !== false && $pos >= $ptr - 2) {
  133. $ptr = $pos;
  134. }
  135. // Check if there is a space at the end of the line and rewind
  136. if ($ptr > 0 && $str[$ptr - 1] == ' ') {
  137. --$ptr;
  138. }
  139. // Add string and continue
  140. $out .= substr($str, 0, $ptr) . '=' . $lineEnd;
  141. $str = substr($str, $ptr);
  142. }
  143. $out = rtrim($out, $lineEnd);
  144. $out = rtrim($out, '=');
  145. return $out;
  146. }
  147. /**
  148. * Encode a given string in base64 encoding and break lines
  149. * according to the maximum linelength.
  150. *
  151. * @param string $str
  152. * @param int $lineLength Defaults to {@link LINELENGTH}
  153. * @param int $lineEnd Defaults to {@link LINEEND}
  154. * @return string
  155. */
  156. public static function encodeBase64($str,
  157. $lineLength = self::LINELENGTH,
  158. $lineEnd = self::LINEEND)
  159. {
  160. return rtrim(chunk_split(base64_encode($str), $lineLength, $lineEnd));
  161. }
  162. /**
  163. * Constructor
  164. *
  165. * @param null|string $boundary
  166. * @access public
  167. * @return void
  168. */
  169. public function __construct($boundary = null)
  170. {
  171. // This string needs to be somewhat unique
  172. if ($boundary === null) {
  173. $this->_boundary = '=_' . md5(microtime(1) . self::$makeUnique++);
  174. } else {
  175. $this->_boundary = $boundary;
  176. }
  177. }
  178. /**
  179. * Encode the given string with the given encoding.
  180. *
  181. * @param string $str
  182. * @param string $encoding
  183. * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  184. * @return string
  185. */
  186. public static function encode($str, $encoding, $EOL = self::LINEEND)
  187. {
  188. switch ($encoding) {
  189. case self::ENCODING_BASE64:
  190. return self::encodeBase64($str, self::LINELENGTH, $EOL);
  191. case self::ENCODING_QUOTEDPRINTABLE:
  192. return self::encodeQuotedPrintable($str, self::LINELENGTH, $EOL);
  193. default:
  194. /**
  195. * @todo 7Bit and 8Bit is currently handled the same way.
  196. */
  197. return $str;
  198. }
  199. }
  200. /**
  201. * Return a MIME boundary
  202. *
  203. * @access public
  204. * @return string
  205. */
  206. public function boundary()
  207. {
  208. return $this->_boundary;
  209. }
  210. /**
  211. * Return a MIME boundary line
  212. *
  213. * @param mixed $EOL Defaults to {@link LINEEND}
  214. * @access public
  215. * @return string
  216. */
  217. public function boundaryLine($EOL = self::LINEEND)
  218. {
  219. return $EOL . '--' . $this->_boundary . $EOL;
  220. }
  221. /**
  222. * Return MIME ending
  223. *
  224. * @access public
  225. * @return string
  226. */
  227. public function mimeEnd($EOL = self::LINEEND)
  228. {
  229. return $EOL . '--' . $this->_boundary . '--' . $EOL;
  230. }
  231. }