PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/interfase/draft/mail.php

https://github.com/nopticon/hyd
PHP | 270 lines | 211 code | 42 blank | 17 comment | 43 complexity | eb4c973c1e915462f4555d1bf55c0fd3 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /*
  3. <Orion, a web development framework for RK.>
  4. Copyright (C) <2011> <Orion>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. if (!defined('IN_APP')) exit;
  17. if (class_exists('_mail')) {
  18. return;
  19. }
  20. class _mail {
  21. public $all;
  22. public $data = array();
  23. public function __construct() {
  24. return;
  25. }
  26. public function parse_header($header) {
  27. $last_header = '';
  28. $parsed_header = w();
  29. for ($j = 0, $end = count($header); $j < $end; $j++) {
  30. $hd = split(':', $header[$j], 2);
  31. if (preg_match_all("/\s/", $hd[0], $matches) || !isset($hd[1]) || !$hd[1]) {
  32. if ($last_header) {
  33. $parsed_header[$last_header] .= "\r\n" . trim($header[$j]);
  34. }
  35. } else {
  36. $last_header = strtolower($hd[0]);
  37. if (!isset($parsed_header[$last_header])) {
  38. $parsed_header[$last_header] = '';
  39. }
  40. $parsed_header[$last_header] .= (($parsed_header[$last_header]) ? "\r\n" : '') . trim($hd[1]);
  41. }
  42. }
  43. foreach ($parsed_header as $hd_name => $hd_content) {
  44. $start_enc_tag = $stop_enc_tag = 0;
  45. $pre_text = $enc_text = $post_text = "";
  46. while(1) {
  47. if (strstr($hd_content, '=?') && strstr($hd_content, '?=') && substr_count($hd_content,'?') > 3) {
  48. $start_enc_tag = strpos($hd_content, '=?');
  49. $pre_text = substr($hd_content, 0, $start_enc_tag);
  50. do {
  51. $stop_enc_tag = strpos($hd_content, '?=', $stop_enc_tag) + 2;
  52. $enc_text = substr($hd_content, $start_enc_tag, $stop_enc_tag);
  53. }
  54. while (!(substr_count($enc_text, '?') > 3));
  55. $enc_text = explode('?', $enc_text, 5);
  56. switch (strtoupper($enc_text[2])) {
  57. case "B":
  58. $dec_text = base64_decode($enc_text[3]);
  59. break;
  60. case "Q":
  61. default:
  62. $dec_text = quoted_printable_decode($enc_text[3]);
  63. $dec_text = str_replace('_', ' ', $dec_text);
  64. break;
  65. }
  66. $post_text = substr($hd_content, $stop_enc_tag);
  67. if (substr(ltrim($post_text), 0, 2) == '=?') {
  68. $post_text = ltrim($post_text);
  69. }
  70. $hd_content = $pre_text . $dec_text . $post_text;
  71. $parsed_header[$hd_name] = $hd_content;
  72. } else {
  73. break;
  74. }
  75. }
  76. }
  77. return $parsed_header;
  78. }
  79. public function parse_address($addr) {
  80. $atpos = strpos($addr, '@');
  81. $minpos = strpos($addr, '<');
  82. $majpos = strpos($addr, '>');
  83. $fromstart = 0;
  84. $fromend = strlen($addr);
  85. if ($minpos < $atpos && $majpos > $atpos) {
  86. $fromstart = $minpos + 1;
  87. $fromend = $majpos;
  88. }
  89. return substr($addr, $fromstart, $fromend - $fromstart);
  90. }
  91. public function parse_date($ddate) {
  92. $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  93. if (strpos($ddate, ',')) {
  94. $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
  95. }
  96. $date_arr = explode(' ', $ddate);
  97. $date_time = explode(':', $date_arr[3]);
  98. $ddate_H = $date_time[0];
  99. $ddate_i = $date_time[1];
  100. $ddate_s = $date_time[2];
  101. $ddate_m = $date_arr[1];
  102. $ddate_d = $date_arr[0];
  103. $ddate_Y = $date_arr[2];
  104. for ($j = 0; $j < 12; $j++) {
  105. if ($ddate_m == $dmonths[$j]) {
  106. $ddate_m = $j + 1;
  107. }
  108. }
  109. $time_zn = intval($date_arr[4]) * 36;
  110. $ddate_U = gmmktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
  111. return ($ddate_U - $time_zn);
  112. }
  113. public function parse_ip($value) {
  114. $result = '127.0.0.1';
  115. if ($count = preg_match_all('#from \[([0-9\.]+)\]#is', $value, $part)) {
  116. $result = $part[1][($count - 1)];
  117. }
  118. return $result;
  119. }
  120. public function body($header, $message) {
  121. if (!isset($header['content-transfer-encoding'])) {
  122. $header['content-transfer-encoding'] = '';
  123. }
  124. $content_transfer_encoding = strtolower(trim($header['content-transfer-encoding']));
  125. if (empty($content_transfer_encoding)) {
  126. $content_transfer_encoding = '8bit';
  127. }
  128. $message = $this->text_decode($content_transfer_encoding, $message);
  129. if (!isset($header['content-type'])) {
  130. $header['content-type'] = '';
  131. }
  132. $content_type = split(';', $header["content-type"]);
  133. for ($i = 0, $end = count($content_type); $i < $end; $i++) {
  134. $content_type[$i] = trim(strtolower($content_type[$i]));
  135. }
  136. if (empty($content_type[0])) {
  137. $content_type[0] = 'text/plain';
  138. }
  139. if (strstr($content_type[0], 'multipart/') || strstr($content_type[0], 'message/')) {
  140. $content_type[0] = 'multipart';
  141. }
  142. switch ($content_type[0]) {
  143. case 'text/plain':
  144. $this->data['text-plain'] = htmlentities(implode("\n", $message));
  145. break;
  146. case 'text/html':
  147. $this->data['text-html'] = implode("\n", $message);
  148. break;
  149. case 'multipart':
  150. $content_type[1] = split(';', $content_type[1]);
  151. $boundary = '';
  152. foreach($content_type[1] as $ct_pars) {
  153. $ct_pars = split('=', $ct_pars, 2);
  154. if (strtolower($ct_pars[0]) == 'boundary') {
  155. $boundary = str_replace('"', '', $ct_pars[1]);
  156. }
  157. }
  158. if ($boundary) {
  159. $parts = $this->split_multipart($boundary, $message);
  160. foreach ($parts as $part) {
  161. $this->parse_part($part);
  162. }
  163. }
  164. break;
  165. default:
  166. $this->data['attachments'][] = array('header' => $header, 'content' => @implode("\n", $message));
  167. break;
  168. }
  169. return $this->data;
  170. }
  171. public function text_decode($encoding, $text) {
  172. switch ($encoding)
  173. {
  174. case 'quoted-printable':
  175. $dec_text = explode("\n", quoted_printable_decode(implode("\n", $text)));
  176. break;
  177. case 'base64':
  178. for($i = 0, $end = count($text); $i < $end; $i++) {
  179. $text[$i] = trim($text[$i]);
  180. }
  181. $dec_text = explode("\n", base64_decode(@implode('', $text)));
  182. break;
  183. case '7bit':
  184. case '8bit':
  185. case 'binary':
  186. default:
  187. $dec_text = $text;
  188. break;
  189. }
  190. return $dec_text;
  191. }
  192. public function split_multipart($boundary, $text) {
  193. $parts = array();
  194. $tmp = array();
  195. foreach ($text as $i => $line) {
  196. $line = trim($line);
  197. if (strstr(strtolower($line), '--' . $boundary)) {
  198. $parts[] = $tmp;
  199. $tmp = array();
  200. } else {
  201. $tmp[] = $line;
  202. }
  203. }
  204. for ($i = 0, $end = count($parts); $i < $end; $i++) {
  205. $parts[$i] = explode("\n", trim(implode("\n", $parts[$i])));
  206. }
  207. return $parts;
  208. }
  209. public function parse_part($text) {
  210. $headerpart = array();
  211. $contentpart = array();
  212. $noheader = 0;
  213. foreach ($text as $riga) {
  214. if (!$riga) {
  215. $noheader++;
  216. }
  217. if ($noheader) {
  218. $contentpart[] = $riga;
  219. } else {
  220. $headerpart[] = $riga;
  221. }
  222. }
  223. $this->body($this->parse_header($headerpart), explode("\n", trim(implode("\n", $contentpart))));
  224. }
  225. }