Deliver.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. /**
  3. * Deliver.class.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains all the functions needed to send messages through
  9. * a delivery backend.
  10. *
  11. * @version $Id$
  12. * @author Marc Groot Koerkamp
  13. * @package squirrelmail
  14. */
  15. /**
  16. * Deliver Class - called to actually deliver the message
  17. *
  18. * This class is called by compose.php and other code that needs
  19. * to send messages. All delivery functionality should be centralized
  20. * in this class.
  21. *
  22. * Do not place UI code in this class, as UI code should be placed in templates
  23. * going forward.
  24. *
  25. * @author Marc Groot Koerkamp
  26. * @package squirrelmail
  27. */
  28. class Deliver {
  29. /**
  30. * function mail - send the message parts to the SMTP stream
  31. *
  32. * @param Message $message Message class to send
  33. * @param resource $stream file handle to the SMTP stream
  34. *
  35. * @return integer $raw_length
  36. */
  37. function mail($message, $stream=false) {
  38. $rfc822_header = $message->rfc822_header;
  39. if (count($message->entities)) {
  40. $boundary = $this->mimeBoundary();
  41. $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"';
  42. } else {
  43. $boundary='';
  44. }
  45. $raw_length = 0;
  46. $reply_rfc822_header = (isset($message->reply_rfc822_header)
  47. ? $message->reply_rfc822_header : '');
  48. $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
  49. if ($stream) {
  50. $this->preWriteToStream($header);
  51. $this->writeToStream($stream, $header);
  52. }
  53. $this->writeBody($message, $stream, $raw_length, $boundary);
  54. return $raw_length;
  55. }
  56. /**
  57. * function writeBody - generate and write the mime boundaries around each part to the stream
  58. *
  59. * Recursively formats and writes the MIME boundaries of the $message
  60. * to the output stream.
  61. *
  62. * @param Message $message Message object to transform
  63. * @param resource $stream SMTP output stream
  64. * @param integer &$length_raw raw length of the message (part)
  65. * as returned by mail fn
  66. * @param string $boundary custom boundary to call, usually for subparts
  67. *
  68. * @return void
  69. */
  70. function writeBody($message, $stream, &$length_raw, $boundary='') {
  71. // calculate boundary in case of multidimensional mime structures
  72. if ($boundary && $message->entity_id && count($message->entities)) {
  73. if (strpos($boundary,'_part_')) {
  74. $boundary = substr($boundary,0,strpos($boundary,'_part_'));
  75. }
  76. $boundary_new = $boundary . '_part_'.$message->entity_id;
  77. } else {
  78. $boundary_new = $boundary;
  79. }
  80. if ($boundary && !$message->rfc822_header) {
  81. $s = '--'.$boundary."\r\n";
  82. $s .= $this->prepareMIME_Header($message, $boundary_new);
  83. $length_raw += strlen($s);
  84. if ($stream) {
  85. $this->preWriteToStream($s);
  86. $this->writeToStream($stream, $s);
  87. }
  88. }
  89. $this->writeBodyPart($message, $stream, $length_raw);
  90. $last = false;
  91. for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) {
  92. $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary_new);
  93. if ($i == $entCount-1) $last = true;
  94. }
  95. if ($boundary && $last) {
  96. $s = "--".$boundary_new."--\r\n\r\n";
  97. $length_raw += strlen($s);
  98. if ($stream) {
  99. $this->preWriteToStream($s);
  100. $this->writeToStream($stream, $s);
  101. }
  102. }
  103. }
  104. /**
  105. * function writeBodyPart - write each individual mimepart
  106. *
  107. * Recursively called by WriteBody to write each mime part to the SMTP stream
  108. *
  109. * @param Message $message Message object to transform
  110. * @param resource $stream SMTP output stream
  111. * @param integer &$length length of the message part
  112. * as returned by mail fn
  113. *
  114. * @return void
  115. */
  116. function writeBodyPart($message, $stream, &$length) {
  117. if ($message->mime_header) {
  118. $type0 = $message->mime_header->type0;
  119. } else {
  120. $type0 = $message->rfc822_header->content_type->type0;
  121. }
  122. $body_part_trailing = $last = '';
  123. switch ($type0)
  124. {
  125. case 'text':
  126. case 'message':
  127. if ($message->body_part) {
  128. $body_part = $message->body_part;
  129. $length += $this->clean_crlf($body_part);
  130. if ($stream) {
  131. $this->preWriteToStream($body_part);
  132. $this->writeToStream($stream, $body_part);
  133. }
  134. $last = $body_part;
  135. } elseif ($message->att_local_name) {
  136. $filename = $message->att_local_name;
  137. $file = fopen ($filename, 'rb');
  138. while ($body_part = fgets($file, 4096)) {
  139. $length += $this->clean_crlf($body_part);
  140. if ($stream) {
  141. $this->preWriteToStream($body_part);
  142. $this->writeToStream($stream, $body_part);
  143. }
  144. $last = $body_part;
  145. }
  146. fclose($file);
  147. }
  148. break;
  149. default:
  150. if ($message->body_part) {
  151. $body_part = $message->body_part;
  152. $length += $this->clean_crlf($body_part);
  153. if ($stream) {
  154. $this->writeToStream($stream, $body_part);
  155. }
  156. } elseif ($message->att_local_name) {
  157. $filename = $message->att_local_name;
  158. $file = fopen ($filename, 'rb');
  159. $encoded = '';
  160. while ($tmp = fread($file, 570)) {
  161. $body_part = chunk_split(base64_encode($tmp));
  162. $length += $this->clean_crlf($body_part);
  163. if ($stream) {
  164. $this->writeToStream($stream, $body_part);
  165. }
  166. }
  167. fclose($file);
  168. }
  169. break;
  170. }
  171. $body_part_trailing = '';
  172. if ($last && substr($last,-1) != "\n") {
  173. $body_part_trailing = "\r\n";
  174. }
  175. if ($body_part_trailing) {
  176. $length += strlen($body_part_trailing);
  177. if ($stream) {
  178. $this->preWriteToStream($body_part_trailing);
  179. $this->writeToStream($stream, $body_part_trailing);
  180. }
  181. }
  182. }
  183. /**
  184. * function clean_crlf - change linefeeds and newlines to legal characters
  185. *
  186. * The SMTP format only allows CRLF as line terminators.
  187. * This function replaces illegal teminators with the correct terminator.
  188. *
  189. * @param string &$s string to clean linefeeds on
  190. *
  191. * @return void
  192. */
  193. function clean_crlf(&$s) {
  194. $s = str_replace("\r\n", "\n", $s);
  195. $s = str_replace("\r", "\n", $s);
  196. $s = str_replace("\n", "\r\n", $s);
  197. return strlen($s);
  198. }
  199. /**
  200. * function strip_crlf - strip linefeeds and newlines from a string
  201. *
  202. * The SMTP format only allows CRLF as line terminators.
  203. * This function strips all line terminators from the string.
  204. *
  205. * @param string &$s string to clean linefeeds on
  206. *
  207. * @return void
  208. */
  209. function strip_crlf(&$s) {
  210. $s = str_replace("\r\n ", '', $s);
  211. $s = str_replace("\r", '', $s);
  212. $s = str_replace("\n", '', $s);
  213. }
  214. /**
  215. * function preWriteToStream - reserved for extended functionality
  216. *
  217. * This function is not yet implemented.
  218. * Reserved for extended functionality.
  219. *
  220. * @param string &$s string to operate on
  221. *
  222. * @return void
  223. */
  224. function preWriteToStream(&$s) {
  225. }
  226. /**
  227. * function writeToStream - write data to the SMTP stream
  228. *
  229. * @param resource $stream SMTP output stream
  230. * @param string $data string with data to send to the SMTP stream
  231. *
  232. * @return void
  233. */
  234. function writeToStream($stream, $data) {
  235. fputs($stream, $data);
  236. }
  237. /**
  238. * function initStream - reserved for extended functionality
  239. *
  240. * This function is not yet implemented.
  241. * Reserved for extended functionality.
  242. *
  243. * @param Message $message Message object
  244. * @param string $host host name or IP to connect to
  245. * @param string $user username to log into the SMTP server with
  246. * @param string $pass password to log into the SMTP server with
  247. * @param integer $length
  248. *
  249. * @return handle $stream file handle resource to SMTP stream
  250. */
  251. function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
  252. return $stream;
  253. }
  254. /**
  255. * function getBCC - reserved for extended functionality
  256. *
  257. * This function is not yet implemented.
  258. * Reserved for extended functionality.
  259. *
  260. */
  261. function getBCC() {
  262. return false;
  263. }
  264. /**
  265. * function prepareMIME_Header - creates the mime header
  266. *
  267. * @param Message $message Message object to act on
  268. * @param string $boundary mime boundary from fn MimeBoundary
  269. *
  270. * @return string $header properly formatted mime header
  271. */
  272. function prepareMIME_Header($message, $boundary) {
  273. $mime_header = $message->mime_header;
  274. $rn="\r\n";
  275. $header = array();
  276. $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
  277. $mime_header->type1;
  278. if (count($message->entities)) {
  279. $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
  280. }
  281. if (isset($mime_header->parameters['name'])) {
  282. $contenttype .= '; name="'.
  283. encodeHeader($mime_header->parameters['name']). '"';
  284. }
  285. if (isset($mime_header->parameters['charset'])) {
  286. $charset = $mime_header->parameters['charset'];
  287. $contenttype .= '; charset="'.
  288. encodeHeader($charset). '"';
  289. }
  290. $header[] = $contenttype . $rn;
  291. if ($mime_header->description) {
  292. $header[] .= 'Content-Description: ' . $mime_header->description . $rn;
  293. }
  294. if ($mime_header->encoding) {
  295. $encoding = $mime_header->encoding;
  296. $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
  297. } else {
  298. if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') {
  299. $header[] .= 'Content-Transfer-Encoding: 8bit' . $rn;
  300. } else {
  301. $header[] .= 'Content-Transfer-Encoding: base64' . $rn;
  302. }
  303. }
  304. if ($mime_header->id) {
  305. $header[] .= 'Content-ID: ' . $mime_header->id . $rn;
  306. }
  307. if ($mime_header->disposition) {
  308. $disposition = $mime_header->disposition;
  309. $contentdisp = 'Content-Disposition: ' . $disposition->name;
  310. if ($disposition->getProperty('filename')) {
  311. $contentdisp .= '; filename="'.
  312. encodeHeader($disposition->getProperty('filename')). '"';
  313. }
  314. $header[] = $contentdisp . $rn;
  315. }
  316. if ($mime_header->md5) {
  317. $header[] .= 'Content-MD5: ' . $mime_header->md5 . $rn;
  318. }
  319. if ($mime_header->language) {
  320. $header[] .= 'Content-Language: ' . $mime_header->language . $rn;
  321. }
  322. $cnt = count($header);
  323. $hdr_s = '';
  324. for ($i = 0 ; $i < $cnt ; $i++) {
  325. $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
  326. }
  327. $header = $hdr_s;
  328. $header .= $rn; /* One blank line to separate mimeheader and body-entity */
  329. return $header;
  330. }
  331. /**
  332. * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s)
  333. *
  334. * This function takes the Rfc822Header object(s) and formats them
  335. * into the RFC822Header string to send to the SMTP server as part
  336. * of the SMTP message.
  337. *
  338. * @param Rfc822Header $rfc822_header
  339. * @param Rfc822Header $reply_rfc822_header
  340. * @param integer &$raw_length length of the message
  341. *
  342. * @return string $header
  343. */
  344. function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
  345. global $domain, $version, $username, $skip_SM_header;
  346. /* if server var SERVER_NAME not available, use $domain */
  347. if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER)) {
  348. $SERVER_NAME = $domain;
  349. }
  350. sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER);
  351. sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER);
  352. sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER);
  353. sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER);
  354. sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER);
  355. $rn = "\r\n";
  356. /* This creates an RFC 822 date */
  357. $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone();
  358. /* Create a message-id */
  359. $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
  360. $message_id .= time() . '.squirrel@' . $REMOTE_ADDR .'>';
  361. /* Make an RFC822 Received: line */
  362. if (isset($REMOTE_HOST)) {
  363. $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
  364. } else {
  365. $received_from = $REMOTE_ADDR;
  366. }
  367. if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
  368. if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') {
  369. $HTTP_X_FORWARDED_FOR = 'unknown';
  370. }
  371. $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
  372. }
  373. $header = array();
  374. if ( !isset($skip_SM_header) || !$skip_SM_header )
  375. {
  376. $header[] = "Received: from $received_from" . $rn;
  377. $header[] = " (SquirrelMail authenticated user $username);" . $rn;
  378. $header[] = " by $SERVER_NAME with HTTP;" . $rn;
  379. $header[] = " $date" . $rn;
  380. }
  381. /* Insert the rest of the header fields */
  382. $header[] = 'Message-ID: '. $message_id . $rn;
  383. if ($reply_rfc822_header->message_id) {
  384. $rep_message_id = $reply_rfc822_header->message_id;
  385. // $this->strip_crlf($message_id);
  386. $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
  387. $references = $this->calculate_references($reply_rfc822_header);
  388. $header[] = 'References: '.$references . $rn;
  389. }
  390. $header[] = "Date: $date" . $rn;
  391. $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
  392. $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn;
  393. // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>" as delimiter
  394. // Do not use foldLine for that.
  395. // RFC2822 if from contains more then 1 address
  396. if (count($rfc822_header->from) > 1) {
  397. $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
  398. }
  399. if (count($rfc822_header->to)) {
  400. $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn;
  401. }
  402. if (count($rfc822_header->cc)) {
  403. $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn;
  404. }
  405. if (count($rfc822_header->reply_to)) {
  406. $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
  407. }
  408. /* Sendmail should return true. Default = false */
  409. $bcc = $this->getBcc();
  410. if (count($rfc822_header->bcc)) {
  411. $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn;
  412. if (!$bcc) {
  413. $raw_length += strlen($s);
  414. } else {
  415. $header[] = $s;
  416. }
  417. }
  418. /* Identify SquirrelMail */
  419. $header[] = 'User-Agent: SquirrelMail/' . $version . $rn;
  420. /* Do the MIME-stuff */
  421. $header[] = 'MIME-Version: 1.0' . $rn;
  422. $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
  423. $rfc822_header->content_type->type1;
  424. if (count($rfc822_header->content_type->properties)) {
  425. foreach ($rfc822_header->content_type->properties as $k => $v) {
  426. if ($k && $v) {
  427. $contenttype .= ';' .$k.'='.$v;
  428. }
  429. }
  430. }
  431. $header[] = $contenttype . $rn;
  432. if ($encoding = $rfc822_header->encoding) {
  433. $header[] .= 'Content-Transfer-Encoding: ' . $encoding . $rn;
  434. }
  435. if ($rfc822_header->dnt) {
  436. $dnt = $rfc822_header->getAddr_s('dnt');
  437. /* Pegasus Mail */
  438. $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
  439. /* RFC 2298 */
  440. $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
  441. }
  442. if ($rfc822_header->priority) {
  443. switch($rfc822_header->priority)
  444. {
  445. case 1:
  446. $header[] = 'X-Priority: 1 (Highest)'.$rn;
  447. $header[] = 'Importance: High'. $rn; break;
  448. case 3:
  449. $header[] = 'X-Priority: 3 (Normal)'.$rn;
  450. $header[] = 'Importance: Normal'. $rn; break;
  451. case 5:
  452. $header[] = 'X-Priority: 5 (Lowest)'.$rn;
  453. $header[] = 'Importance: Low'. $rn; break;
  454. default: break;
  455. }
  456. }
  457. /* Insert headers from the $more_headers array */
  458. if(count($rfc822_header->more_headers)) {
  459. reset($rfc822_header->more_headers);
  460. foreach ($rfc822_header->more_headers as $k => $v) {
  461. $header[] = $k.': '.$v .$rn;
  462. }
  463. }
  464. $cnt = count($header);
  465. $hdr_s = '';
  466. for ($i = 0 ; $i < $cnt ; $i++) {
  467. $sKey = substr($header[$i],0,strpos($header[$i],':'));
  468. switch ($sKey)
  469. {
  470. case 'Message-ID':
  471. case 'In-Reply_To':
  472. $hdr_s .= $header[$i];
  473. break;
  474. case 'References':
  475. $sRefs = substr($header[$i],12);
  476. $aRefs = explode(' ',$sRefs);
  477. $sLine = 'References:';
  478. foreach ($aRefs as $sReference) {
  479. if (strlen($sLine)+strlen($sReference) >76) {
  480. $hdr_s .= $sLine;
  481. $sLine = $rn . ' ' . $sReference;
  482. } else {
  483. $sLine .= ' '. $sReference;
  484. }
  485. }
  486. $hdr_s .= $sLine;
  487. break;
  488. case 'To':
  489. case 'Cc':
  490. case 'Bcc':
  491. case 'From':
  492. $hdr_s .= $header[$i];
  493. break;
  494. default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break;
  495. }
  496. }
  497. $header = $hdr_s;
  498. $header .= $rn; /* One blank line to separate header and body */
  499. $raw_length += strlen($header);
  500. return $header;
  501. }
  502. /**
  503. * function foldLine - for cleanly folding of headerlines
  504. *
  505. * @param string $line
  506. * @param integer $length length to fold the line at
  507. * @param string $pre prefix the line with...
  508. *
  509. * @return string $line folded line with trailing CRLF
  510. */
  511. function foldLine($line, $length, $pre='') {
  512. $line = substr($line,0, -2);
  513. $length -= 2; /* do not fold between \r and \n */
  514. $cnt = strlen($line);
  515. if ($cnt > $length) { /* try folding */
  516. $fold_string = "\r\n " . $pre;
  517. $bFirstFold = false;
  518. $aFoldLine = array();
  519. while (strlen($line) > $length) {
  520. $fold = false;
  521. /* handle encoded parts */
  522. if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) {
  523. $fold_tmp = $regs[1];
  524. if (!trim($regs[5])) {
  525. $fold_tmp .= $regs[5];
  526. }
  527. $iPosEnc = strpos($line,$fold_tmp);
  528. $iLengthEnc = strlen($fold_tmp);
  529. $iPosEncEnd = $iPosEnc+$iLengthEnc;
  530. if ($iPosEnc < $length && (($iPosEncEnd) > $length)) {
  531. $fold = true;
  532. /* fold just before the start of the encoded string */
  533. if ($iPosEnc) {
  534. $aFoldLine[] = substr($line,0,$iPosEnc);
  535. }
  536. $line = substr($line,$iPosEnc);
  537. if (!$bFirstFold) {
  538. $bFirstFold = true;
  539. $length -= strlen($fold_string);
  540. }
  541. if ($iLengthEnc > $length) { /* place the encoded
  542. string on a separate line and do not fold inside it*/
  543. /* minimize foldstring */
  544. $fold_string = "\r\n ";
  545. $aFoldLine[] = substr($line,0,$iLengthEnc);
  546. $line = substr($line,$iLengthEnc);
  547. }
  548. } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */
  549. /*remainder */
  550. $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
  551. if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) || !preg_match('/[=,;\s]/',$sLineRem)) {
  552. /*impossible to fold clean in the next part -> fold after the enc string */
  553. $aFoldLine[] = substr($line,0,$iPosEncEnd);
  554. $line = substr($line,$iPosEncEnd);
  555. $fold = true;
  556. if (!$bFirstFold) {
  557. $bFirstFold = true;
  558. $length -= strlen($fold_string);
  559. }
  560. }
  561. }
  562. }
  563. if (!$fold) {
  564. $line_tmp = substr($line,0,$length);
  565. $iFoldPos = false;
  566. /* try to fold at logical places */
  567. switch (true)
  568. {
  569. case ($iFoldPos = strrpos($line_tmp,',')): break;
  570. case ($iFoldPos = strrpos($line_tmp,';')): break;
  571. case ($iFoldPos = strrpos($line_tmp,' ')): break;
  572. case ($iFoldPos = strrpos($line_tmp,'=')): break;
  573. default: break;
  574. }
  575. if (!$iFoldPos) { /* clean folding didn't work */
  576. $iFoldPos = $length;
  577. }
  578. $aFoldLine[] = substr($line,0,$iFoldPos+1);
  579. $line = substr($line,$iFoldPos+1);
  580. if (!$bFirstFold) {
  581. $bFirstFold = true;
  582. $length -= strlen($fold_string);
  583. }
  584. }
  585. }
  586. /*$reconstruct the line */
  587. if ($line) {
  588. $aFoldLine[] = $line;
  589. }
  590. $line = implode($fold_string,$aFoldLine);
  591. }
  592. return $line."\r\n";
  593. }
  594. /**
  595. * function mimeBoundary - calculates the mime boundary to use
  596. *
  597. * This function will generate a random mime boundary base part
  598. * for the message if the boundary has not already been set.
  599. *
  600. * @return string $mimeBoundaryString random mime boundary string
  601. */
  602. function mimeBoundary () {
  603. static $mimeBoundaryString;
  604. if ( !isset( $mimeBoundaryString ) ||
  605. $mimeBoundaryString == '') {
  606. $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
  607. mt_rand( 10000, 99999 );
  608. }
  609. return $mimeBoundaryString;
  610. }
  611. /**
  612. * function timezone - Time offset for correct timezone
  613. *
  614. * @return string $result with timezone and offset
  615. */
  616. function timezone () {
  617. global $invert_time;
  618. $diff_second = date('Z');
  619. if ($invert_time) {
  620. $diff_second = - $diff_second;
  621. }
  622. if ($diff_second > 0) {
  623. $sign = '+';
  624. } else {
  625. $sign = '-';
  626. }
  627. $diff_second = abs($diff_second);
  628. $diff_hour = floor ($diff_second / 3600);
  629. $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
  630. $zonename = '('.strftime('%Z').')';
  631. $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
  632. $zonename);
  633. return ($result);
  634. }
  635. /**
  636. * function calculate_references - calculate correct Referer string
  637. *
  638. * @param Rfc822Header $hdr message header to calculate from
  639. *
  640. * @return string $refer concatenated and trimmed Referer string
  641. */
  642. function calculate_references($hdr) {
  643. $refer = $hdr->references;
  644. $message_id = $hdr->message_id;
  645. $in_reply_to = $hdr->in_reply_to;
  646. if (strlen($refer) > 2) {
  647. $refer .= ' ' . $message_id;
  648. } else {
  649. if ($in_reply_to) {
  650. $refer .= $in_reply_to . ' ' . $message_id;
  651. } else {
  652. $refer .= $message_id;
  653. }
  654. }
  655. trim($refer);
  656. return $refer;
  657. }
  658. }
  659. ?>