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