PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/shop/includes/classes/mime.php

https://github.com/severnaya99/Sg-2010
PHP | 260 lines | 125 code | 29 blank | 106 comment | 28 complexity | ce91918455f5fca0966f69eea559bd15 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // |zen-cart Open Source E-commerce |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2003 The zen-cart developers |
  7. // | |
  8. // | http://www.zen-cart.com/index.php |
  9. // | |
  10. // | The original class was made by Richard Heyes <richard@phpguru.org> |
  11. // | and can be found here: http://www.phpguru.org |
  12. // | Renamed and Modified by Jan Wildeboer for osCommerce |
  13. // | |
  14. // | Portions Copyright (c) 2003 osCommerce |
  15. // +----------------------------------------------------------------------+
  16. // | This source file is subject to version 2.0 of the GPL license, |
  17. // | that is bundled with this package in the file LICENSE, and is |
  18. // | available through the world-wide-web at the following url: |
  19. // | http://www.zen-cart.com/license/2_0.txt. |
  20. // | If you did not receive a copy of the zen-cart license and are unable |
  21. // | to obtain it through the world-wide-web, please send a note to |
  22. // | license@zen-cart.com so we can mail you a copy immediately. |
  23. // +----------------------------------------------------------------------+
  24. // $Id: mime.php 290 2004-09-15 19:48:26Z wilt $
  25. //
  26. class mime {
  27. var $_encoding;
  28. var $_subparts;
  29. var $_encoded;
  30. var $_headers;
  31. var $_body;
  32. /**
  33. * Constructor.
  34. *
  35. * Sets up the object.
  36. *
  37. * @param $body - The body of the mime part if any.
  38. * @param $params - An associative array of parameters:
  39. * content_type - The content type for this part eg multipart/mixed
  40. * encoding - The encoding to use, 7bit, base64, or quoted-printable
  41. * cid - Content ID to apply
  42. * disposition - Content disposition, inline or attachment
  43. * dfilename - Optional filename parameter for content disposition
  44. * description - Content description
  45. * @access public
  46. */
  47. function mime($body, $params = '') {
  48. if ($params == '') $params = array();
  49. // Make sure we use the correct linfeed sequence
  50. if (EMAIL_LINEFEED == 'CRLF') {
  51. $this->lf = "\r\n";
  52. } else {
  53. $this->lf = "\n";
  54. }
  55. reset($params);
  56. while (list($key, $value) = each($params)) {
  57. switch ($key) {
  58. case 'content_type':
  59. $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
  60. break;
  61. case 'encoding':
  62. $this->_encoding = $value;
  63. $headers['Content-Transfer-Encoding'] = $value;
  64. break;
  65. case 'cid':
  66. $headers['Content-ID'] = '<' . $value . '>';
  67. break;
  68. case 'disposition':
  69. $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
  70. break;
  71. case 'dfilename':
  72. if (isset($headers['Content-Disposition'])) {
  73. $headers['Content-Disposition'] .= '; filename="' . $value . '"';
  74. } else {
  75. $dfilename = $value;
  76. }
  77. break;
  78. case 'description':
  79. $headers['Content-Description'] = $value;
  80. break;
  81. case 'charset':
  82. if (isset($headers['Content-Type'])) {
  83. $headers['Content-Type'] .= '; charset="' . $value . '"';
  84. } else {
  85. $charset = $value;
  86. }
  87. break;
  88. }
  89. }
  90. // Default content-type
  91. if (!isset($_headers['Content-Type'])) {
  92. $_headers['Content-Type'] = 'text/plain';
  93. }
  94. // Assign stuff to member variables
  95. $this->_encoded = array();
  96. /* HPDL PHP3 */
  97. // $this->_headers =& $headers;
  98. $this->_headers = $headers;
  99. $this->_body = $body;
  100. }
  101. /**
  102. * encode()
  103. *
  104. * Encodes and returns the email. Also stores
  105. * it in the encoded member variable
  106. *
  107. * @return An associative array containing two elements,
  108. * body and headers. The headers element is itself
  109. * an indexed array.
  110. * @access public
  111. */
  112. function encode() {
  113. /* HPDL PHP3 */
  114. // $encoded =& $this->_encoded;
  115. $encoded = $this->_encoded;
  116. if (zen_not_null($this->_subparts)) {
  117. $boundary = '=_' . md5(uniqid(zen_rand()) . microtime());
  118. $this->_headers['Content-Type'] .= ';' . $this->lf . chr(9) . 'boundary="' . $boundary . '"';
  119. // Add body parts to $subparts
  120. for ($i=0; $i<count($this->_subparts); $i++) {
  121. $headers = array();
  122. /* HPDL PHP3 */
  123. // $tmp = $this->_subparts[$i]->encode();
  124. $_subparts = $this->_subparts[$i];
  125. $tmp = $_subparts->encode();
  126. reset($tmp['headers']);
  127. while (list($key, $value) = each($tmp['headers'])) {
  128. $headers[] = $key . ': ' . $value;
  129. }
  130. $subparts[] = implode($this->lf, $headers) . $this->lf . $this->lf . $tmp['body'];
  131. }
  132. $encoded['body'] = '--' . $boundary . $this->lf . implode('--' . $boundary . $this->lf, $subparts) . '--' . $boundary.'--' . $this->lf;
  133. } else {
  134. $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . $this->lf;
  135. }
  136. // Add headers to $encoded
  137. /* HPDL PHP3 */
  138. // $encoded['headers'] =& $this->_headers;
  139. $encoded['headers'] = $this->_headers;
  140. return $encoded;
  141. }
  142. /**
  143. * &addSubPart()
  144. *
  145. * Adds a subpart to current mime part and returns
  146. * a reference to it
  147. *
  148. * @param $body The body of the subpart, if any.
  149. * @param $params The parameters for the subpart, same
  150. * as the $params argument for constructor.
  151. * @return A reference to the part you just added. It is
  152. * crucial if using multipart/* in your subparts that
  153. * you use =& in your script when calling this function,
  154. * otherwise you will not be able to add further subparts.
  155. * @access public
  156. */
  157. /* HPDL PHP3 */
  158. // function &addSubPart($body, $params) {
  159. function addSubPart($body, $params) {
  160. $this->_subparts[] = new mime($body, $params);
  161. return $this->_subparts[count($this->_subparts) - 1];
  162. }
  163. /**
  164. * _getEncodedData()
  165. *
  166. * Returns encoded data based upon encoding passed to it
  167. *
  168. * @param $data The data to encode.
  169. * @param $encoding The encoding type to use, 7bit, base64,
  170. * or quoted-printable.
  171. * @access private
  172. */
  173. function _getEncodedData($data, $encoding) {
  174. switch ($encoding) {
  175. case '7bit':
  176. return $data;
  177. break;
  178. case 'quoted-printable':
  179. return $this->_quotedPrintableEncode($data);
  180. break;
  181. case 'base64':
  182. return rtrim(chunk_split(base64_encode($data), 76, $this->lf));
  183. break;
  184. }
  185. }
  186. /**
  187. * quoteadPrintableEncode()
  188. *
  189. * Encodes data to quoted-printable standard.
  190. *
  191. * @param $input The data to encode
  192. * @param $line_max Optional max line length. Should
  193. * not be more than 76 chars
  194. *
  195. * @access private
  196. */
  197. function _quotedPrintableEncode($input , $line_max = 76) {
  198. $lines = preg_split("/\r\n|\r|\n/", $input);
  199. $eol = $this->lf;
  200. $escape = '=';
  201. $output = '';
  202. while (list(, $line) = each($lines)) {
  203. $linlen = strlen($line);
  204. $newline = '';
  205. for ($i = 0; $i < $linlen; $i++) {
  206. $char = substr($line, $i, 1);
  207. $dec = ord($char);
  208. // convert space at eol only
  209. if ( ($dec == 32) && ($i == ($linlen - 1)) ) {
  210. $char = '=20';
  211. } elseif ($dec == 9) {
  212. // Do nothing if a tab.
  213. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) {
  214. $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
  215. }
  216. // $this->lf is not counted
  217. if ((strlen($newline) + strlen($char)) >= $line_max) {
  218. // soft line break; " =\r\n" is okay
  219. $output .= $newline . $escape . $eol;
  220. $newline = '';
  221. }
  222. $newline .= $char;
  223. }
  224. $output .= $newline . $eol;
  225. }
  226. // Don't want last crlf
  227. $output = substr($output, 0, -1 * strlen($eol));
  228. return $output;
  229. }
  230. }
  231. ?>