4960ec8e |
1 | <?php |
4b4abf93 |
2 | |
69298c28 |
3 | /** |
4 | * Deliver.class.php |
5 | * |
69298c28 |
6 | * This contains all the functions needed to send messages through |
7 | * a delivery backend. |
8 | * |
4b4abf93 |
9 | * @author Marc Groot Koerkamp |
4b5049de |
10 | * @copyright © 1999-2007 The SquirrelMail Project Team |
4b4abf93 |
11 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
883d9cd3 |
12 | * @version $Id$ |
72b004e6 |
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 | |
b67d61ee |
31 | /** |
32 | * Most recently calculated Message-ID |
33 | * External code should NEVER access this directly! |
34 | * @var string |
35 | */ |
36 | var $message_id; |
37 | |
72b004e6 |
38 | /** |
39 | * function mail - send the message parts to the SMTP stream |
40 | * |
0fdb0aa1 |
41 | * @param Message $message Message object to send |
42 | * @param resource $stream Handle to the SMTP stream |
e2ccf284 |
43 | * (when FALSE, nothing will be |
44 | * written to the stream; this can |
45 | * be used to determine the actual |
46 | * number of bytes that will be |
47 | * written to the stream) |
0fdb0aa1 |
48 | * @param string $reply_id Identifies message being replied to |
49 | * (OPTIONAL; caller should ONLY specify |
50 | * a value for this when the message |
51 | * being sent is a reply) |
52 | * @param string $reply_ent_id Identifies message being replied to |
53 | * in the case it was an embedded/attached |
54 | * message inside another (OPTIONAL; caller |
55 | * should ONLY specify a value for this |
56 | * when the message being sent is a reply) |
e2ccf284 |
57 | * @param mixed $extra Any implementation-specific variables |
58 | * can be passed in here and used in |
59 | * an overloaded version of this method |
60 | * if needed. |
72b004e6 |
61 | * |
b67d61ee |
62 | * @return array An array containing at least these elements in this order: |
63 | * - The number of bytes written (or that would have been |
64 | * written) to the output stream |
65 | * - The message ID (WARNING: if $stream is FALSE, this |
66 | * may not be supplied, or may not be accurate) |
67 | * |
72b004e6 |
68 | */ |
e2ccf284 |
69 | function mail($message, $stream=false, $reply_id=0, $reply_ent_id=0, |
70 | $extra=NULL) { |
71 | |
78f39e78 |
72 | $rfc822_header = $message->rfc822_header; |
73 | if (count($message->entities)) { |
74 | $boundary = $this->mimeBoundary(); |
75 | $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"'; |
76 | } else { |
77 | $boundary=''; |
78 | } |
79 | $raw_length = 0; |
0fdb0aa1 |
80 | |
81 | |
82 | // calculate reply header if needed |
83 | // |
84 | if ($reply_id) { |
85 | global $imapConnection, $username, $imapServerAddress, |
86 | $imapPort, $mailbox; |
e2ccf284 |
87 | |
88 | if (!is_resource($imapConnection)) |
0fdb0aa1 |
89 | $imapConnection = sqimap_login($username, FALSE, |
90 | $imapServerAddress, $imapPort, 0); |
91 | |
92 | sqimap_mailbox_select($imapConnection, $mailbox); |
93 | $reply_message = sqimap_get_message($imapConnection, $reply_id, $mailbox); |
94 | |
95 | if ($reply_ent_id) { |
96 | /* redefine the messsage in case of message/rfc822 */ |
97 | $reply_message = $message->getEntity($reply_ent_id); |
98 | /* message is an entity which contains the envelope and type0=message |
99 | * and type1=rfc822. The actual entities are childs from |
100 | * $reply_message->entities[0]. That's where the encoding and is located |
101 | */ |
102 | |
103 | $orig_header = $reply_message->rfc822_header; /* here is the envelope located */ |
104 | |
105 | } else { |
106 | $orig_header = $reply_message->rfc822_header; |
107 | } |
08e33475 |
108 | $message->reply_rfc822_header = $orig_header; |
0fdb0aa1 |
109 | } |
0fdb0aa1 |
110 | |
111 | |
72b004e6 |
112 | $reply_rfc822_header = (isset($message->reply_rfc822_header) |
78f39e78 |
113 | ? $message->reply_rfc822_header : ''); |
114 | $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length); |
75b14a70 |
115 | |
e2ccf284 |
116 | $this->send_mail($message, $header, $boundary, $stream, $raw_length, $extra); |
117 | |
b67d61ee |
118 | return array($raw_length, $this->message_id); |
e2ccf284 |
119 | } |
120 | |
121 | /** |
122 | * function send_mail - send the message parts to the IMAP stream |
123 | * |
124 | * @param Message $message Message object to send |
125 | * @param string $header Headers ready to send |
126 | * @param string $boundary Message parts boundary |
127 | * @param resource $stream Handle to the SMTP stream |
128 | * (when FALSE, nothing will be |
129 | * written to the stream; this can |
130 | * be used to determine the actual |
131 | * number of bytes that will be |
132 | * written to the stream) |
133 | * @param int &$raw_length The number of bytes written (or that |
134 | * would have been written) to the |
135 | * output stream - NOTE that this is |
136 | * passed by reference |
137 | * @param mixed $extra Any implementation-specific variables |
138 | * can be passed in here and used in |
139 | * an overloaded version of this method |
140 | * if needed. |
141 | * |
142 | * @return void |
143 | * |
144 | */ |
145 | function send_mail($message, $header, $boundary, $stream=false, |
146 | &$raw_length, $extra=NULL) { |
147 | |
148 | |
78f39e78 |
149 | if ($stream) { |
59387d1c |
150 | $this->preWriteToStream($header); |
151 | $this->writeToStream($stream, $header); |
78f39e78 |
152 | } |
153 | $this->writeBody($message, $stream, $raw_length, $boundary); |
3b53d87f |
154 | } |
78f39e78 |
155 | |
72b004e6 |
156 | /** |
157 | * function writeBody - generate and write the mime boundaries around each part to the stream |
158 | * |
159 | * Recursively formats and writes the MIME boundaries of the $message |
160 | * to the output stream. |
161 | * |
162 | * @param Message $message Message object to transform |
163 | * @param resource $stream SMTP output stream |
e2ccf284 |
164 | * (when FALSE, nothing will be |
165 | * written to the stream; this can |
166 | * be used to determine the actual |
167 | * number of bytes that will be |
168 | * written to the stream) |
72b004e6 |
169 | * @param integer &$length_raw raw length of the message (part) |
170 | * as returned by mail fn |
171 | * @param string $boundary custom boundary to call, usually for subparts |
172 | * |
3a70ee56 |
173 | * @return void |
72b004e6 |
174 | */ |
3b53d87f |
175 | function writeBody($message, $stream, &$length_raw, $boundary='') { |
caf6a9a4 |
176 | // calculate boundary in case of multidimensional mime structures |
177 | if ($boundary && $message->entity_id && count($message->entities)) { |
178 | if (strpos($boundary,'_part_')) { |
179 | $boundary = substr($boundary,0,strpos($boundary,'_part_')); |
ba86230e |
180 | |
593d083d |
181 | // the next four lines use strrev to reverse any nested boundaries |
182 | // because RFC 2046 (5.1.1) says that if a line starts with the outer |
183 | // boundary string (doesn't matter what the line ends with), that |
184 | // can be considered a match for the outer boundary; thus the nested |
185 | // boundary needs to be unique from the outer one |
ba86230e |
186 | // |
187 | } else if (strpos($boundary,'_trap_')) { |
188 | $boundary = substr(strrev($boundary),0,strpos(strrev($boundary),'_part_')); |
caf6a9a4 |
189 | } |
ba86230e |
190 | $boundary_new = strrev($boundary . '_part_'.$message->entity_id); |
caf6a9a4 |
191 | } else { |
192 | $boundary_new = $boundary; |
193 | } |
1274f430 |
194 | if ($boundary && !$message->rfc822_header) { |
78f39e78 |
195 | $s = '--'.$boundary."\r\n"; |
caf6a9a4 |
196 | $s .= $this->prepareMIME_Header($message, $boundary_new); |
78f39e78 |
197 | $length_raw += strlen($s); |
198 | if ($stream) { |
3b53d87f |
199 | $this->preWriteToStream($s); |
78f39e78 |
200 | $this->writeToStream($stream, $s); |
201 | } |
202 | } |
203 | $this->writeBodyPart($message, $stream, $length_raw); |
72b004e6 |
204 | |
78f39e78 |
205 | $last = false; |
206 | for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) { |
caf6a9a4 |
207 | $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary_new); |
78f39e78 |
208 | if ($i == $entCount-1) $last = true; |
209 | } |
1274f430 |
210 | if ($boundary && $last) { |
dcc5c913 |
211 | $s = "--".$boundary_new."--\r\n\r\n"; |
78f39e78 |
212 | $length_raw += strlen($s); |
213 | if ($stream) { |
3b53d87f |
214 | $this->preWriteToStream($s); |
78f39e78 |
215 | $this->writeToStream($stream, $s); |
216 | } |
217 | } |
3b53d87f |
218 | } |
219 | |
72b004e6 |
220 | /** |
221 | * function writeBodyPart - write each individual mimepart |
222 | * |
223 | * Recursively called by WriteBody to write each mime part to the SMTP stream |
224 | * |
225 | * @param Message $message Message object to transform |
226 | * @param resource $stream SMTP output stream |
e2ccf284 |
227 | * (when FALSE, nothing will be |
228 | * written to the stream; this can |
229 | * be used to determine the actual |
230 | * number of bytes that will be |
231 | * written to the stream) |
72b004e6 |
232 | * @param integer &$length length of the message part |
233 | * as returned by mail fn |
234 | * |
3a70ee56 |
235 | * @return void |
72b004e6 |
236 | */ |
3b53d87f |
237 | function writeBodyPart($message, $stream, &$length) { |
1274f430 |
238 | if ($message->mime_header) { |
78f39e78 |
239 | $type0 = $message->mime_header->type0; |
240 | } else { |
241 | $type0 = $message->rfc822_header->content_type->type0; |
242 | } |
243 | |
244 | $body_part_trailing = $last = ''; |
245 | switch ($type0) |
246 | { |
247 | case 'text': |
248 | case 'message': |
249 | if ($message->body_part) { |
250 | $body_part = $message->body_part; |
4cf85ddd |
251 | // remove NUL characters |
252 | $body_part = str_replace("\0",'',$body_part); |
78f39e78 |
253 | $length += $this->clean_crlf($body_part); |
254 | if ($stream) { |
72b004e6 |
255 | $this->preWriteToStream($body_part); |
78f39e78 |
256 | $this->writeToStream($stream, $body_part); |
257 | } |
258 | $last = $body_part; |
259 | } elseif ($message->att_local_name) { |
1f270d3c |
260 | global $username, $attachment_dir; |
261 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
78f39e78 |
262 | $filename = $message->att_local_name; |
1f270d3c |
263 | $file = fopen ($hashed_attachment_dir . '/' . $filename, 'rb'); |
78f39e78 |
264 | while ($body_part = fgets($file, 4096)) { |
257dea3f |
265 | // remove NUL characters |
266 | $body_part = str_replace("\0",'',$body_part); |
78f39e78 |
267 | $length += $this->clean_crlf($body_part); |
268 | if ($stream) { |
269 | $this->preWriteToStream($body_part); |
270 | $this->writeToStream($stream, $body_part); |
271 | } |
272 | $last = $body_part; |
273 | } |
274 | fclose($file); |
275 | } |
276 | break; |
277 | default: |
278 | if ($message->body_part) { |
279 | $body_part = $message->body_part; |
257dea3f |
280 | // remove NUL characters |
281 | $body_part = str_replace("\0",'',$body_part); |
78f39e78 |
282 | $length += $this->clean_crlf($body_part); |
283 | if ($stream) { |
284 | $this->writeToStream($stream, $body_part); |
285 | } |
286 | } elseif ($message->att_local_name) { |
1f270d3c |
287 | global $username, $attachment_dir; |
288 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
78f39e78 |
289 | $filename = $message->att_local_name; |
1f270d3c |
290 | $file = fopen ($hashed_attachment_dir . '/' . $filename, 'rb'); |
78f39e78 |
291 | while ($tmp = fread($file, 570)) { |
27ff4efb |
292 | $body_part = chunk_split(base64_encode($tmp)); |
293 | // Up to 4.3.10 chunk_split always appends a newline, |
294 | // while in 4.3.11 it doesn't if the string to split |
295 | // is shorter than the chunk length. |
296 | if( substr($body_part, -1 , 1 ) != "\n" ) |
297 | $body_part .= "\n"; |
78f39e78 |
298 | $length += $this->clean_crlf($body_part); |
299 | if ($stream) { |
300 | $this->writeToStream($stream, $body_part); |
301 | } |
302 | } |
303 | fclose($file); |
3b53d87f |
304 | } |
78f39e78 |
305 | break; |
306 | } |
307 | $body_part_trailing = ''; |
308 | if ($last && substr($last,-1) != "\n") { |
309 | $body_part_trailing = "\r\n"; |
310 | } |
311 | if ($body_part_trailing) { |
312 | $length += strlen($body_part_trailing); |
313 | if ($stream) { |
314 | $this->preWriteToStream($body_part_trailing); |
315 | $this->writeToStream($stream, $body_part_trailing); |
316 | } |
317 | } |
3b53d87f |
318 | } |
78f39e78 |
319 | |
72b004e6 |
320 | /** |
321 | * function clean_crlf - change linefeeds and newlines to legal characters |
322 | * |
323 | * The SMTP format only allows CRLF as line terminators. |
324 | * This function replaces illegal teminators with the correct terminator. |
325 | * |
326 | * @param string &$s string to clean linefeeds on |
327 | * |
3a70ee56 |
328 | * @return void |
72b004e6 |
329 | */ |
69298c28 |
330 | function clean_crlf(&$s) { |
3b53d87f |
331 | $s = str_replace("\r\n", "\n", $s); |
332 | $s = str_replace("\r", "\n", $s); |
333 | $s = str_replace("\n", "\r\n", $s); |
78f39e78 |
334 | return strlen($s); |
3b53d87f |
335 | } |
78f39e78 |
336 | |
72b004e6 |
337 | /** |
338 | * function strip_crlf - strip linefeeds and newlines from a string |
339 | * |
340 | * The SMTP format only allows CRLF as line terminators. |
341 | * This function strips all line terminators from the string. |
342 | * |
343 | * @param string &$s string to clean linefeeds on |
344 | * |
3a70ee56 |
345 | * @return void |
72b004e6 |
346 | */ |
e4f9307a |
347 | function strip_crlf(&$s) { |
348 | $s = str_replace("\r\n ", '', $s); |
78f39e78 |
349 | $s = str_replace("\r", '', $s); |
350 | $s = str_replace("\n", '', $s); |
e4f9307a |
351 | } |
3b53d87f |
352 | |
72b004e6 |
353 | /** |
354 | * function preWriteToStream - reserved for extended functionality |
355 | * |
356 | * This function is not yet implemented. |
357 | * Reserved for extended functionality. |
358 | * |
359 | * @param string &$s string to operate on |
360 | * |
3a70ee56 |
361 | * @return void |
72b004e6 |
362 | */ |
69298c28 |
363 | function preWriteToStream(&$s) { |
3b53d87f |
364 | } |
78f39e78 |
365 | |
72b004e6 |
366 | /** |
367 | * function writeToStream - write data to the SMTP stream |
368 | * |
369 | * @param resource $stream SMTP output stream |
370 | * @param string $data string with data to send to the SMTP stream |
371 | * |
3a70ee56 |
372 | * @return void |
72b004e6 |
373 | */ |
3b53d87f |
374 | function writeToStream($stream, $data) { |
78f39e78 |
375 | fputs($stream, $data); |
3b53d87f |
376 | } |
78f39e78 |
377 | |
72b004e6 |
378 | /** |
379 | * function initStream - reserved for extended functionality |
380 | * |
381 | * This function is not yet implemented. |
382 | * Reserved for extended functionality. |
383 | * |
384 | * @param Message $message Message object |
385 | * @param string $host host name or IP to connect to |
386 | * @param string $user username to log into the SMTP server with |
387 | * @param string $pass password to log into the SMTP server with |
388 | * @param integer $length |
389 | * |
3a70ee56 |
390 | * @return handle $stream file handle resource to SMTP stream |
72b004e6 |
391 | */ |
3b53d87f |
392 | function initStream($message, $length=0, $host='', $port='', $user='', $pass='') { |
78f39e78 |
393 | return $stream; |
3b53d87f |
394 | } |
72b004e6 |
395 | |
396 | /** |
397 | * function getBCC - reserved for extended functionality |
398 | * |
399 | * This function is not yet implemented. |
400 | * Reserved for extended functionality. |
401 | * |
402 | */ |
403 | function getBCC() { |
78f39e78 |
404 | return false; |
59387d1c |
405 | } |
3b53d87f |
406 | |
72b004e6 |
407 | /** |
408 | * function prepareMIME_Header - creates the mime header |
409 | * |
410 | * @param Message $message Message object to act on |
411 | * @param string $boundary mime boundary from fn MimeBoundary |
412 | * |
3a70ee56 |
413 | * @return string $header properly formatted mime header |
72b004e6 |
414 | */ |
3b53d87f |
415 | function prepareMIME_Header($message, $boundary) { |
78f39e78 |
416 | $mime_header = $message->mime_header; |
417 | $rn="\r\n"; |
418 | $header = array(); |
1274f430 |
419 | |
78f39e78 |
420 | $contenttype = 'Content-Type: '. $mime_header->type0 .'/'. |
421 | $mime_header->type1; |
422 | if (count($message->entities)) { |
fcce1b3c |
423 | $contenttype .= ';' . 'boundary="'.$boundary.'"'; |
78f39e78 |
424 | } |
425 | if (isset($mime_header->parameters['name'])) { |
426 | $contenttype .= '; name="'. |
427 | encodeHeader($mime_header->parameters['name']). '"'; |
428 | } |
429 | if (isset($mime_header->parameters['charset'])) { |
430 | $charset = $mime_header->parameters['charset']; |
431 | $contenttype .= '; charset="'. |
432 | encodeHeader($charset). '"'; |
433 | } |
1274f430 |
434 | |
78f39e78 |
435 | $header[] = $contenttype . $rn; |
436 | if ($mime_header->description) { |
eb72e151 |
437 | $header[] = 'Content-Description: ' . $mime_header->description . $rn; |
78f39e78 |
438 | } |
439 | if ($mime_header->encoding) { |
eb72e151 |
440 | $header[] = 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn; |
78f39e78 |
441 | } else { |
442 | if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') { |
eb72e151 |
443 | $header[] = 'Content-Transfer-Encoding: 8bit' . $rn; |
78f39e78 |
444 | } else { |
eb72e151 |
445 | $header[] = 'Content-Transfer-Encoding: base64' . $rn; |
78f39e78 |
446 | } |
447 | } |
448 | if ($mime_header->id) { |
eb72e151 |
449 | $header[] = 'Content-ID: ' . $mime_header->id . $rn; |
78f39e78 |
450 | } |
451 | if ($mime_header->disposition) { |
452 | $disposition = $mime_header->disposition; |
453 | $contentdisp = 'Content-Disposition: ' . $disposition->name; |
454 | if ($disposition->getProperty('filename')) { |
455 | $contentdisp .= '; filename="'. |
456 | encodeHeader($disposition->getProperty('filename')). '"'; |
457 | } |
72b004e6 |
458 | $header[] = $contentdisp . $rn; |
78f39e78 |
459 | } |
460 | if ($mime_header->md5) { |
eb72e151 |
461 | $header[] = 'Content-MD5: ' . $mime_header->md5 . $rn; |
78f39e78 |
462 | } |
463 | if ($mime_header->language) { |
eb72e151 |
464 | $header[] = 'Content-Language: ' . $mime_header->language . $rn; |
78f39e78 |
465 | } |
4960ec8e |
466 | |
78f39e78 |
467 | $cnt = count($header); |
468 | $hdr_s = ''; |
469 | for ($i = 0 ; $i < $cnt ; $i++) { |
470 | $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4)); |
471 | } |
472 | $header = $hdr_s; |
473 | $header .= $rn; /* One blank line to separate mimeheader and body-entity */ |
474 | return $header; |
475 | } |
4960ec8e |
476 | |
72b004e6 |
477 | /** |
478 | * function prepareRFC822_Header - prepares the RFC822 header string from Rfc822Header object(s) |
479 | * |
480 | * This function takes the Rfc822Header object(s) and formats them |
481 | * into the RFC822Header string to send to the SMTP server as part |
482 | * of the SMTP message. |
483 | * |
484 | * @param Rfc822Header $rfc822_header |
485 | * @param Rfc822Header $reply_rfc822_header |
486 | * @param integer &$raw_length length of the message |
487 | * |
3a70ee56 |
488 | * @return string $header |
72b004e6 |
489 | */ |
75b14a70 |
490 | function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) { |
b37e457f |
491 | global $domain, $username, $encode_header_key, |
e930c9fe |
492 | $edit_identity, $hide_auth_header; |
60a46e65 |
493 | |
49c7f411 |
494 | /* if server var SERVER_NAME not available, or contains |
495 | ":" (e.g. IPv6) which is illegal in a Message-ID, use $domain */ |
496 | if(!sqGetGlobalVar('SERVER_NAME', $SERVER_NAME, SQ_SERVER) || |
497 | strpos($SERVER_NAME,':') !== FALSE) { |
60a46e65 |
498 | $SERVER_NAME = $domain; |
78f39e78 |
499 | } |
60a46e65 |
500 | |
78f39e78 |
501 | sqGetGlobalVar('REMOTE_ADDR', $REMOTE_ADDR, SQ_SERVER); |
502 | sqGetGlobalVar('REMOTE_PORT', $REMOTE_PORT, SQ_SERVER); |
503 | sqGetGlobalVar('REMOTE_HOST', $REMOTE_HOST, SQ_SERVER); |
504 | sqGetGlobalVar('HTTP_VIA', $HTTP_VIA, SQ_SERVER); |
505 | sqGetGlobalVar('HTTP_X_FORWARDED_FOR', $HTTP_X_FORWARDED_FOR, SQ_SERVER); |
60a46e65 |
506 | |
78f39e78 |
507 | $rn = "\r\n"; |
223872cb |
508 | |
78f39e78 |
509 | /* This creates an RFC 822 date */ |
867fed37 |
510 | $date = date('D, j M Y H:i:s ', time()) . $this->timezone(); |
49c7f411 |
511 | |
78f39e78 |
512 | /* Create a message-id */ |
49c7f411 |
513 | $message_id = '<'; |
514 | /* user-specifc data to decrease collision chance */ |
515 | $seed_data = $username . '.'; |
516 | $seed_data .= (!empty($REMOTE_PORT) ? $REMOTE_PORT . '.' : ''); |
517 | $seed_data .= (!empty($REMOTE_ADDR) ? $REMOTE_ADDR . '.' : ''); |
518 | /* add the current time in milliseconds and randomness */ |
519 | $seed_data .= uniqid(mt_rand(),true); |
520 | /* put it through one-way hash and add it to the ID */ |
521 | $message_id .= md5($seed_data) . '.squirrel@' . $SERVER_NAME .'>'; |
b67d61ee |
522 | $this->message_id = $message_id; |
523 | |
78f39e78 |
524 | /* Make an RFC822 Received: line */ |
525 | if (isset($REMOTE_HOST)) { |
526 | $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])"; |
527 | } else { |
528 | $received_from = $REMOTE_ADDR; |
529 | } |
530 | if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) { |
531 | if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') { |
532 | $HTTP_X_FORWARDED_FOR = 'unknown'; |
533 | } |
534 | $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)"; |
535 | } |
536 | $header = array(); |
432db2fc |
537 | |
538 | /** |
539 | * SquirrelMail header |
540 | * |
541 | * This Received: header provides information that allows to track |
542 | * user and machine that was used to send email. Don't remove it |
543 | * unless you understand all possible forging issues or your |
544 | * webmail installation does not prevent changes in user's email address. |
545 | * See SquirrelMail bug tracker #847107 for more details about it. |
bb08da84 |
546 | * |
547 | * Add $hide_squirrelmail_header as a candidate for config_local.php |
548 | * to allow completely hiding SquirrelMail participation in message |
539a323f |
549 | * processing; This is dangerous, especially if users can modify their |
e930c9fe |
550 | * account information, as it makes mapping a sent message back to the |
551 | * original sender almost impossible. |
432db2fc |
552 | */ |
e930c9fe |
553 | $show_sm_header = ( defined('hide_squirrelmail_header') ? ! hide_squirrelmail_header : 1 ); |
bb08da84 |
554 | |
555 | if ( $show_sm_header ) { |
556 | if (isset($encode_header_key) && |
432db2fc |
557 | trim($encode_header_key)!='') { |
558 | // use encoded headers, if encryption key is set and not empty |
b81efcb4 |
559 | $header[] = 'X-Squirrel-UserHash: '.OneTimePadEncrypt($username,base64_encode($encode_header_key)).$rn; |
560 | $header[] = 'X-Squirrel-FromHash: '.OneTimePadEncrypt($this->ip2hex($REMOTE_ADDR),base64_encode($encode_header_key)).$rn; |
432db2fc |
561 | if (isset($HTTP_X_FORWARDED_FOR)) |
b81efcb4 |
562 | $header[] = 'X-Squirrel-ProxyHash:'.OneTimePadEncrypt($this->ip2hex($HTTP_X_FORWARDED_FOR),base64_encode($encode_header_key)).$rn; |
bb08da84 |
563 | } else { |
432db2fc |
564 | // use default received headers |
565 | $header[] = "Received: from $received_from" . $rn; |
566 | if ($edit_identity || ! isset($hide_auth_header) || ! $hide_auth_header) |
567 | $header[] = " (SquirrelMail authenticated user $username)" . $rn; |
568 | $header[] = " by $SERVER_NAME with HTTP;" . $rn; |
569 | $header[] = " $date" . $rn; |
bb08da84 |
570 | } |
35aaf666 |
571 | } |
432db2fc |
572 | |
59387d1c |
573 | /* Insert the rest of the header fields */ |
574 | $header[] = 'Message-ID: '. $message_id . $rn; |
ea87de81 |
575 | if (is_object($reply_rfc822_header) && |
b4316b34 |
576 | isset($reply_rfc822_header->message_id) && |
577 | $reply_rfc822_header->message_id) { |
78f39e78 |
578 | $rep_message_id = $reply_rfc822_header->message_id; |
579 | // $this->strip_crlf($message_id); |
580 | $header[] = 'In-Reply-To: '.$rep_message_id . $rn; |
581 | $references = $this->calculate_references($reply_rfc822_header); |
582 | $header[] = 'References: '.$references . $rn; |
583 | } |
584 | $header[] = "Date: $date" . $rn; |
59387d1c |
585 | $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn; |
ac47827c |
586 | $header[] = 'From: '. $rfc822_header->getAddr_s('from',",$rn ",true) . $rn; |
587 | |
ea87de81 |
588 | // folding address list [From|To|Cc|Bcc] happens by using ",$rn<space>" |
589 | // as delimiter |
ac47827c |
590 | // Do not use foldLine for that. |
591 | |
592 | // RFC2822 if from contains more then 1 address |
59387d1c |
593 | if (count($rfc822_header->from) > 1) { |
215de78c |
594 | $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn; |
78f39e78 |
595 | } |
596 | if (count($rfc822_header->to)) { |
ac47827c |
597 | $header[] = 'To: '. $rfc822_header->getAddr_s('to',",$rn ",true) . $rn; |
78f39e78 |
598 | } |
599 | if (count($rfc822_header->cc)) { |
ac47827c |
600 | $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',",$rn ",true) . $rn; |
78f39e78 |
601 | } |
602 | if (count($rfc822_header->reply_to)) { |
215de78c |
603 | $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn; |
78f39e78 |
604 | } |
605 | /* Sendmail should return true. Default = false */ |
606 | $bcc = $this->getBcc(); |
607 | if (count($rfc822_header->bcc)) { |
ac47827c |
608 | $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',",$rn ",true) . $rn; |
78f39e78 |
609 | if (!$bcc) { |
78f39e78 |
610 | $raw_length += strlen($s); |
611 | } else { |
612 | $header[] = $s; |
613 | } |
614 | } |
72b004e6 |
615 | /* Identify SquirrelMail */ |
b37e457f |
616 | $header[] = 'User-Agent: SquirrelMail/' . SM_VERSION . $rn; |
78f39e78 |
617 | /* Do the MIME-stuff */ |
618 | $header[] = 'MIME-Version: 1.0' . $rn; |
619 | $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'. |
59387d1c |
620 | $rfc822_header->content_type->type1; |
78f39e78 |
621 | if (count($rfc822_header->content_type->properties)) { |
622 | foreach ($rfc822_header->content_type->properties as $k => $v) { |
623 | if ($k && $v) { |
72b004e6 |
624 | $contenttype .= ';' .$k.'='.$v; |
78f39e78 |
625 | } |
626 | } |
627 | } |
59387d1c |
628 | $header[] = $contenttype . $rn; |
78f39e78 |
629 | if ($encoding = $rfc822_header->encoding) { |
23301b33 |
630 | $header[] = 'Content-Transfer-Encoding: ' . $encoding . $rn; |
72b004e6 |
631 | } |
539a323f |
632 | if (isset($rfc822_header->dnt) && $rfc822_header->dnt) { |
72b004e6 |
633 | $dnt = $rfc822_header->getAddr_s('dnt'); |
78f39e78 |
634 | /* Pegasus Mail */ |
635 | $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn; |
636 | /* RFC 2298 */ |
637 | $header[] = 'Disposition-Notification-To: '.$dnt. $rn; |
638 | } |
639 | if ($rfc822_header->priority) { |
d1db3699 |
640 | switch($rfc822_header->priority) |
78f39e78 |
641 | { |
d1db3699 |
642 | case 1: |
91e0dccc |
643 | $header[] = 'X-Priority: 1 (Highest)'.$rn; |
644 | $header[] = 'Importance: High'. $rn; break; |
d1db3699 |
645 | case 5: |
91e0dccc |
646 | $header[] = 'X-Priority: 5 (Lowest)'.$rn; |
647 | $header[] = 'Importance: Low'. $rn; break; |
78f39e78 |
648 | default: break; |
649 | } |
650 | } |
72b004e6 |
651 | /* Insert headers from the $more_headers array */ |
78f39e78 |
652 | if(count($rfc822_header->more_headers)) { |
653 | reset($rfc822_header->more_headers); |
654 | foreach ($rfc822_header->more_headers as $k => $v) { |
655 | $header[] = $k.': '.$v .$rn; |
656 | } |
657 | } |
658 | $cnt = count($header); |
659 | $hdr_s = ''; |
5d2a7b15 |
660 | |
78f39e78 |
661 | for ($i = 0 ; $i < $cnt ; $i++) { |
5d2a7b15 |
662 | $sKey = substr($header[$i],0,strpos($header[$i],':')); |
663 | switch ($sKey) |
664 | { |
665 | case 'Message-ID': |
666 | case 'In-Reply_To': |
667 | $hdr_s .= $header[$i]; |
668 | break; |
669 | case 'References': |
670 | $sRefs = substr($header[$i],12); |
671 | $aRefs = explode(' ',$sRefs); |
672 | $sLine = 'References:'; |
673 | foreach ($aRefs as $sReference) { |
cc201b46 |
674 | if ( trim($sReference) == '' ) { |
675 | /* Don't add spaces. */ |
676 | } elseif (strlen($sLine)+strlen($sReference) >76) { |
5d2a7b15 |
677 | $hdr_s .= $sLine; |
678 | $sLine = $rn . ' ' . $sReference; |
679 | } else { |
680 | $sLine .= ' '. $sReference; |
681 | } |
682 | } |
683 | $hdr_s .= $sLine; |
684 | break; |
ac47827c |
685 | case 'To': |
686 | case 'Cc': |
687 | case 'Bcc': |
688 | case 'From': |
689 | $hdr_s .= $header[$i]; |
690 | break; |
5d2a7b15 |
691 | default: $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4)); break; |
692 | } |
78f39e78 |
693 | } |
78f39e78 |
694 | $header = $hdr_s; |
695 | $header .= $rn; /* One blank line to separate header and body */ |
696 | $raw_length += strlen($header); |
697 | return $header; |
4960ec8e |
698 | } |
699 | |
72b004e6 |
700 | /** |
701 | * function foldLine - for cleanly folding of headerlines |
702 | * |
703 | * @param string $line |
704 | * @param integer $length length to fold the line at |
705 | * @param string $pre prefix the line with... |
706 | * |
3a70ee56 |
707 | * @return string $line folded line with trailing CRLF |
72b004e6 |
708 | */ |
1274f430 |
709 | function foldLine($line, $length, $pre='') { |
e4f9307a |
710 | $line = substr($line,0, -2); |
fb1d6cb6 |
711 | $length -= 2; /* do not fold between \r and \n */ |
712 | $cnt = strlen($line); |
713 | if ($cnt > $length) { /* try folding */ |
714 | $fold_string = "\r\n " . $pre; |
715 | $bFirstFold = false; |
716 | $aFoldLine = array(); |
717 | while (strlen($line) > $length) { |
718 | $fold = false; |
719 | /* handle encoded parts */ |
7060c7f0 |
720 | if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(\s+|.*)/Ui',$line,$regs)) { |
fb1d6cb6 |
721 | $fold_tmp = $regs[1]; |
7060c7f0 |
722 | if (!trim($regs[5])) { |
723 | $fold_tmp .= $regs[5]; |
724 | } |
fb1d6cb6 |
725 | $iPosEnc = strpos($line,$fold_tmp); |
726 | $iLengthEnc = strlen($fold_tmp); |
c422e824 |
727 | $iPosEncEnd = $iPosEnc+$iLengthEnc; |
728 | if ($iPosEnc < $length && (($iPosEncEnd) > $length)) { |
fb1d6cb6 |
729 | $fold = true; |
730 | /* fold just before the start of the encoded string */ |
7060c7f0 |
731 | if ($iPosEnc) { |
732 | $aFoldLine[] = substr($line,0,$iPosEnc); |
733 | } |
fb1d6cb6 |
734 | $line = substr($line,$iPosEnc); |
735 | if (!$bFirstFold) { |
736 | $bFirstFold = true; |
737 | $length -= strlen($fold_string); |
738 | } |
739 | if ($iLengthEnc > $length) { /* place the encoded |
740 | string on a separate line and do not fold inside it*/ |
223872cb |
741 | /* minimize foldstring */ |
742 | $fold_string = "\r\n "; |
743 | $aFoldLine[] = substr($line,0,$iLengthEnc); |
744 | $line = substr($line,$iLengthEnc); |
72b004e6 |
745 | } |
c422e824 |
746 | } else if ($iPosEnc < $length) { /* the encoded string fits into the foldlength */ |
fb1d6cb6 |
747 | /*remainder */ |
fb1d6cb6 |
748 | $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd); |
c422e824 |
749 | if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',$sLineRem) || !preg_match('/[=,;\s]/',$sLineRem)) { |
fb1d6cb6 |
750 | /*impossible to fold clean in the next part -> fold after the enc string */ |
7060c7f0 |
751 | $aFoldLine[] = substr($line,0,$iPosEncEnd); |
752 | $line = substr($line,$iPosEncEnd); |
fb1d6cb6 |
753 | $fold = true; |
754 | if (!$bFirstFold) { |
755 | $bFirstFold = true; |
756 | $length -= strlen($fold_string); |
757 | } |
72b004e6 |
758 | } |
fb1d6cb6 |
759 | } |
760 | } |
761 | if (!$fold) { |
762 | $line_tmp = substr($line,0,$length); |
763 | $iFoldPos = false; |
764 | /* try to fold at logical places */ |
765 | switch (true) |
766 | { |
767 | case ($iFoldPos = strrpos($line_tmp,',')): break; |
768 | case ($iFoldPos = strrpos($line_tmp,';')): break; |
769 | case ($iFoldPos = strrpos($line_tmp,' ')): break; |
770 | case ($iFoldPos = strrpos($line_tmp,'=')): break; |
771 | default: break; |
772 | } |
72b004e6 |
773 | |
fb1d6cb6 |
774 | if (!$iFoldPos) { /* clean folding didn't work */ |
775 | $iFoldPos = $length; |
776 | } |
777 | $aFoldLine[] = substr($line,0,$iFoldPos+1); |
778 | $line = substr($line,$iFoldPos+1); |
779 | if (!$bFirstFold) { |
780 | $bFirstFold = true; |
781 | $length -= strlen($fold_string); |
782 | } |
783 | } |
784 | } |
785 | /*$reconstruct the line */ |
786 | if ($line) { |
787 | $aFoldLine[] = $line; |
788 | } |
789 | $line = implode($fold_string,$aFoldLine); |
790 | } |
791 | return $line."\r\n"; |
792 | } |
3b53d87f |
793 | |
72b004e6 |
794 | /** |
795 | * function mimeBoundary - calculates the mime boundary to use |
796 | * |
797 | * This function will generate a random mime boundary base part |
798 | * for the message if the boundary has not already been set. |
799 | * |
3a70ee56 |
800 | * @return string $mimeBoundaryString random mime boundary string |
72b004e6 |
801 | */ |
59387d1c |
802 | function mimeBoundary () { |
78f39e78 |
803 | static $mimeBoundaryString; |
3b53d87f |
804 | |
78f39e78 |
805 | if ( !isset( $mimeBoundaryString ) || |
806 | $mimeBoundaryString == '') { |
807 | $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' . |
808 | mt_rand( 10000, 99999 ); |
809 | } |
810 | return $mimeBoundaryString; |
3b53d87f |
811 | } |
812 | |
72b004e6 |
813 | /** |
814 | * function timezone - Time offset for correct timezone |
815 | * |
3a70ee56 |
816 | * @return string $result with timezone and offset |
72b004e6 |
817 | */ |
59387d1c |
818 | function timezone () { |
78f39e78 |
819 | global $invert_time; |
820 | |
821 | $diff_second = date('Z'); |
822 | if ($invert_time) { |
823 | $diff_second = - $diff_second; |
824 | } |
825 | if ($diff_second > 0) { |
826 | $sign = '+'; |
827 | } else { |
828 | $sign = '-'; |
829 | } |
830 | $diff_second = abs($diff_second); |
831 | $diff_hour = floor ($diff_second / 3600); |
832 | $diff_minute = floor (($diff_second-3600*$diff_hour) / 60); |
833 | $zonename = '('.strftime('%Z').')'; |
72b004e6 |
834 | $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute, |
3b53d87f |
835 | $zonename); |
78f39e78 |
836 | return ($result); |
59387d1c |
837 | } |
3b53d87f |
838 | |
72b004e6 |
839 | /** |
f2ac3325 |
840 | * function calculate_references - calculate correct References string |
841 | * Adds the current message ID, and makes sure it doesn't grow forever, |
842 | * to that extent it drops message-ID's in a smart way until the string |
843 | * length is under the recommended value of 1000 ("References: <986>\r\n"). |
844 | * It always keeps the first and the last three ID's. |
72b004e6 |
845 | * |
846 | * @param Rfc822Header $hdr message header to calculate from |
847 | * |
f2ac3325 |
848 | * @return string $refer concatenated and trimmed References string |
72b004e6 |
849 | */ |
1274f430 |
850 | function calculate_references($hdr) { |
f2ac3325 |
851 | $aReferences = preg_split('/\s+/', $hdr->references); |
78f39e78 |
852 | $message_id = $hdr->message_id; |
853 | $in_reply_to = $hdr->in_reply_to; |
539a323f |
854 | |
f2ac3325 |
855 | // if References already exists, add the current message ID at the end. |
856 | // no References exists; if we know a IRT, add that aswell |
857 | if (count($aReferences) == 0 && $in_reply_to) { |
858 | $aReferences[] = $in_reply_to; |
859 | } |
860 | $aReferences[] = $message_id; |
861 | |
862 | // sanitize the array: trim whitespace, remove dupes |
86e6a9eb |
863 | array_walk($aReferences, 'sq_trim_value'); |
f2ac3325 |
864 | $aReferences = array_unique($aReferences); |
865 | |
866 | while ( count($aReferences) > 4 && strlen(implode(' ', $aReferences)) >= 986 ) { |
867 | $aReferences = array_merge(array_slice($aReferences,0,1),array_slice($aReferences,2)); |
78f39e78 |
868 | } |
f2ac3325 |
869 | return implode(' ', $aReferences); |
870 | } |
871 | |
432db2fc |
872 | /** |
873 | * Converts ip address to hexadecimal string |
874 | * |
875 | * Function is used to convert ipv4 and ipv6 addresses to hex strings. |
876 | * It removes all delimiter symbols from ip addresses, converts decimal |
877 | * ipv4 numbers to hex and pads strings in order to present full length |
ea87de81 |
878 | * address. ipv4 addresses are represented as 8 byte strings, ipv6 addresses |
432db2fc |
879 | * are represented as 32 byte string. |
880 | * |
881 | * If function fails to detect address format, it returns unprocessed string. |
882 | * @param string $string ip address string |
883 | * @return string processed ip address string |
94511d23 |
884 | * @since 1.5.1 and 1.4.5 |
432db2fc |
885 | */ |
886 | function ip2hex($string) { |
887 | if (preg_match("/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/",$string,$match)) { |
888 | // ipv4 address |
889 | $ret = str_pad(dechex($match[1]),2,'0',STR_PAD_LEFT) |
890 | . str_pad(dechex($match[2]),2,'0',STR_PAD_LEFT) |
891 | . str_pad(dechex($match[3]),2,'0',STR_PAD_LEFT) |
892 | . str_pad(dechex($match[4]),2,'0',STR_PAD_LEFT); |
893 | } elseif (preg_match("/^([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)\:([0-9a-h]+)$/i",$string,$match)) { |
894 | // full ipv6 address |
895 | $ret = str_pad($match[1],4,'0',STR_PAD_LEFT) |
896 | . str_pad($match[2],4,'0',STR_PAD_LEFT) |
897 | . str_pad($match[3],4,'0',STR_PAD_LEFT) |
898 | . str_pad($match[4],4,'0',STR_PAD_LEFT) |
899 | . str_pad($match[5],4,'0',STR_PAD_LEFT) |
900 | . str_pad($match[6],4,'0',STR_PAD_LEFT) |
901 | . str_pad($match[7],4,'0',STR_PAD_LEFT) |
902 | . str_pad($match[8],4,'0',STR_PAD_LEFT); |
903 | } elseif (preg_match("/^\:\:([0-9a-h\:]+)$/i",$string,$match)) { |
904 | // short ipv6 with all starting symbols nulled |
905 | $aAddr=explode(':',$match[1]); |
906 | $ret=''; |
907 | foreach ($aAddr as $addr) { |
908 | $ret.=str_pad($addr,4,'0',STR_PAD_LEFT); |
909 | } |
910 | $ret=str_pad($ret,32,'0',STR_PAD_LEFT); |
911 | } elseif (preg_match("/^([0-9a-h\:]+)::([0-9a-h\:]+)$/i",$string,$match)) { |
912 | // short ipv6 with middle part nulled |
913 | $aStart=explode(':',$match[1]); |
914 | $sStart=''; |
915 | foreach($aStart as $addr) { |
916 | $sStart.=str_pad($addr,4,'0',STR_PAD_LEFT); |
917 | } |
918 | $aEnd = explode(':',$match[2]); |
919 | $sEnd=''; |
920 | foreach($aEnd as $addr) { |
921 | $sEnd.=str_pad($addr,4,'0',STR_PAD_LEFT); |
922 | } |
923 | $ret = $sStart |
924 | . str_pad('',(32 - strlen($sStart . $sEnd)),'0',STR_PAD_LEFT) |
925 | . $sEnd; |
926 | } else { |
927 | // unknown addressing |
928 | $ret = $string; |
929 | } |
930 | return $ret; |
931 | } |
59387d1c |
932 | } |