removed encodeHeader calls for the complete addresses, Only the personal
[squirrelmail.git] / class / deliver / Deliver.class.php
1 <?php
2
3 /**
4 * Deliver.class.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains all the functions needed to send messages through
10 * a delivery backend.
11 *
12 * $Id$
13 */
14
15 class Deliver {
16
17 function mail($message, $stream=false) {
18 $rfc822_header = $message->rfc822_header;
19 if (count($message->entities)) {
20 $boundary = $this->mimeBoundary();
21 $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"';
22 } else {
23 $boundary='';
24 }
25 $raw_length = 0;
26 $reply_rfc822_header = (isset($message->reply_rfc822_header)
27 ? $message->reply_rfc822_header : '');
28 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
29
30 if ($stream) {
31 $this->preWriteToStream($header);
32 $this->writeToStream($stream, $header);
33 }
34 $this->writeBody($message, $stream, $raw_length, $boundary);
35 return $raw_length;
36 }
37
38 function writeBody($message, $stream, &$length_raw, $boundary='') {
39 if ($boundary && !$message->rfc822_header) {
40 $s = '--'.$boundary."\r\n";
41 $s .= $this->prepareMIME_Header($message, $boundary);
42 $length_raw += strlen($s);
43 if ($stream) {
44 $this->preWriteToStream($s);
45 $this->writeToStream($stream, $s);
46 }
47 }
48 $this->writeBodyPart($message, $stream, $length_raw);
49 $boundary_depth = substr_count($message->entity_id,'.');
50 if ($boundary_depth) {
51 $boundary .= '_part'.$boundary_depth;
52 }
53 $last = false;
54 for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) {
55 $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary);
56 if ($i == $entCount-1) $last = true;
57 }
58 if ($boundary && $last) {
59 $s = "--".$boundary."--\r\n\r\n";
60 $length_raw += strlen($s);
61 if ($stream) {
62 $this->preWriteToStream($s);
63 $this->writeToStream($stream, $s);
64 }
65 }
66 }
67
68 function writeBodyPart($message, $stream, &$length) {
69 if ($message->mime_header) {
70 $type0 = $message->mime_header->type0;
71 } else {
72 $type0 = $message->rfc822_header->content_type->type0;
73 }
74
75 $body_part_trailing = $last = '';
76 switch ($type0) {
77 case 'text':
78 case 'message':
79 if ($message->body_part) {
80 $body_part = $message->body_part;
81 $length += $this->clean_crlf($body_part);
82 if ($stream) {
83 $this->preWriteToStream($body_part);
84 $this->writeToStream($stream, $body_part);
85 }
86 $last = $body_part;
87 } elseif ($message->att_local_name) {
88 $filename = $message->att_local_name;
89 $file = fopen ($filename, 'rb');
90 while ($body_part = fgets($file, 4096)) {
91 $length += $this->clean_crlf($body_part);
92 if ($stream) {
93 $this->preWriteToStream($body_part);
94 $this->writeToStream($stream, $body_part);
95 }
96 $last = $body_part;
97 }
98 fclose($file);
99 }
100 break;
101 default:
102 if ($message->body_part) {
103 $body_part = $message->body_part;
104 $length += $this->clean_crlf($body_part);
105 if ($stream) {
106 $this->writeToStream($stream, $body_part);
107 }
108 } elseif ($message->att_local_name) {
109 $filename = $message->att_local_name;
110 $file = fopen ($filename, 'rb');
111 $encoded = '';
112 while ($tmp = fread($file, 570)) {
113 $body_part = chunk_split(base64_encode($tmp));
114 $length += $this->clean_crlf($body_part);
115 if ($stream) {
116 $this->writeToStream($stream, $body_part);
117 }
118 }
119 fclose($file);
120 }
121 break;
122 }
123 $body_part_trailing = '';
124 if ($last && substr($last,-1) != "\n") {
125 $body_part_trailing = "\r\n";
126 }
127 if ($body_part_trailing) {
128 $length += strlen($body_part_trailing);
129 if ($stream) {
130 $this->preWriteToStream($body_part_trailing);
131 $this->writeToStream($stream, $body_part_trailing);
132 }
133 }
134 }
135
136 function clean_crlf(&$s) {
137 $s = str_replace("\r\n", "\n", $s);
138 $s = str_replace("\r", "\n", $s);
139 $s = str_replace("\n", "\r\n", $s);
140 return strlen($s);
141 }
142
143 function strip_crlf(&$s) {
144 $s = str_replace("\r\n ", '', $s);
145 $s = str_replace("\r", '', $s);
146 $s = str_replace("\n", '', $s);
147 }
148
149 function preWriteToStream(&$s) {
150 }
151
152 function writeToStream($stream, $data) {
153 fputs($stream, $data);
154 }
155
156 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
157 return $stream;
158 }
159
160 function getBcc() {
161 return false;
162 }
163
164 function prepareMIME_Header($message, $boundary) {
165 $mime_header = $message->mime_header;
166 $rn="\r\n";
167 $header = array();
168
169 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
170 $mime_header->type1;
171 if (count($message->entities)) {
172 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
173 }
174 if (isset($mime_header->parameters['name'])) {
175 $contenttype .= '; name="'.
176 encodeHeader($mime_header->parameters['name']). '"';
177 }
178 if (isset($mime_header->parameters['charset'])) {
179 $charset = $mime_header->parameters['charset'];
180 $contenttype .= '; charset="'.
181 encodeHeader($charset). '"';
182 }
183
184
185 $header[] = $contenttype . $rn;
186 if ($mime_header->description) {
187 $header[] .= 'Content-Description: ' . $mime_header->description . $rn;
188 }
189 if ($mime_header->encoding) {
190 $encoding = $mime_header->encoding;
191 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
192 } else {
193 if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') {
194 $header[] .= 'Content-Transfer-Encoding: 8bit' . $rn;
195 } else {
196 $header[] .= 'Content-Transfer-Encoding: base64' . $rn;
197 }
198 }
199 if ($mime_header->id) {
200 $header[] .= 'Content-ID: ' . $mime_header->id . $rn;
201 }
202 if ($mime_header->disposition) {
203 $disposition = $mime_header->disposition;
204 $contentdisp = 'Content-Disposition: ' . $disposition->name;
205 if ($disposition->getProperty('filename')) {
206 $contentdisp .= '; filename="'.
207 encodeHeader($disposition->getProperty('filename')). '"';
208 }
209 $header[] = $contentdisp . $rn;
210 }
211 if ($mime_header->md5) {
212 $header[] .= 'Content-MD5: ' . $mime_header->md5 . $rn;
213 }
214 if ($mime_header->language) {
215 $header[] .= 'Content-Language: ' . $mime_header->language . $rn;
216 }
217
218 $cnt = count($header);
219 $hdr_s = '';
220 for ($i = 0 ; $i < $cnt ; $i++) {
221 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
222 }
223 $header = $hdr_s;
224 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
225 return $header;
226 }
227
228 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
229 $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
230 $SERVER_NAME = $_SERVER['SERVER_NAME'];
231 $REMOTE_PORT = $_SERVER['REMOTE_PORT'];
232 if(isset($_SERVER['REMOTE_HOST'])) {
233 $REMOTE_HOST = $_SERVER['REMOTE_HOST'];
234 }
235 if(isset($_SERVER['HTTP_VIA'])) {
236 $HTTP_VIA = $_SERVER['HTTP_VIA'];
237 }
238 if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
239 $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
240 }
241 global $version, $username;
242 $rn = "\r\n";
243 /* This creates an RFC 822 date */
244 $date = date('D, j M Y H:i:s ', mktime()) . $this->timezone();
245 /* Create a message-id */
246 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
247 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
248 /* Make an RFC822 Received: line */
249 if (isset($REMOTE_HOST)) {
250 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
251 } else {
252 $received_from = $REMOTE_ADDR;
253 }
254 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
255 if (!isset($HTTP_X_FORWARDED_FOR) || $HTTP_X_FORWARDED_FOR == '') {
256 $HTTP_X_FORWARDED_FOR = 'unknown';
257 }
258 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
259 }
260 $header = array();
261 $header[] = "Received: from $received_from" . $rn;
262 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
263 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
264 $header[] = " $date" . $rn;
265 /* Insert the rest of the header fields */
266 $header[] = 'Message-ID: '. $message_id . $rn;
267 if ($reply_rfc822_header->message_id) {
268 $rep_message_id = $reply_rfc822_header->message_id;
269 // $this->strip_crlf($message_id);
270 $header[] = 'In-Reply-To: '.$rep_message_id . $rn;
271 $references = $this->calculate_references($reply_rfc822_header);
272 $header[] = 'References: '.$references . $rn;
273 }
274 $header[] = "Date: $date" . $rn;
275 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
276 $header[] = 'From: '. $rfc822_header->getAddr_s('from',',',true) . $rn;
277 /* RFC2822 if from contains more then 1 address */
278 if (count($rfc822_header->from) > 1) {
279 $header[] = 'Sender: '. $rfc822_header->getAddr_s('sender',',',true) . $rn;
280 }
281 if (count($rfc822_header->to)) {
282 $header[] = 'To: '. $rfc822_header->getAddr_s('to',',',true) . $rn;
283 }
284 if (count($rfc822_header->cc)) {
285 $header[] = 'Cc: '. $rfc822_header->getAddr_s('cc',',',true) . $rn;
286 }
287 if (count($rfc822_header->reply_to)) {
288 $header[] = 'Reply-To: '. $rfc822_header->getAddr_s('reply_to',',',true) . $rn;
289 }
290 /* Sendmail should return true. Default = false */
291 $bcc = $this->getBcc();
292 if (count($rfc822_header->bcc)) {
293 $s = 'Bcc: '. $rfc822_header->getAddr_s('bcc',',',true) . $rn;
294 if (!$bcc) {
295 $s = $this->foldLine($s, 78, str_pad('',4));
296 $raw_length += strlen($s);
297 } else {
298 $header[] = $s;
299 }
300 }
301 /* Identify SquirrelMail */
302 $header[] = 'User-Agent: SquirrelMail/' . $version . $rn;
303 /* Do the MIME-stuff */
304 $header[] = 'MIME-Version: 1.0' . $rn;
305 $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
306 $rfc822_header->content_type->type1;
307 if (count($rfc822_header->content_type->properties)) {
308 foreach ($rfc822_header->content_type->properties as $k => $v) {
309 if ($k && $v) {
310 $contenttype .= ';' .$k.'='.$v;
311 }
312 }
313 }
314 $header[] = $contenttype . $rn;
315 if ($rfc822_header->dnt) {
316 $dnt = $rfc822_header->getAddr_s('dnt');
317 /* Pegasus Mail */
318 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
319 /* RFC 2298 */
320 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
321 }
322 if ($rfc822_header->priority) {
323 $prio = $rfc822_header->priority;
324 $header[] = 'X-Priority: '.$prio. $rn;
325 switch($prio) {
326 case 1: $header[] = 'Importance: High'. $rn; break;
327 case 3: $header[] = 'Importance: Normal'. $rn; break;
328 case 5: $header[] = 'Importance: Low'. $rn; break;
329 default: break;
330 }
331 }
332 /* Insert headers from the $more_headers array */
333 if(count($rfc822_header->more_headers)) {
334 reset($rfc822_header->more_headers);
335 foreach ($rfc822_header->more_headers as $k => $v) {
336 $header[] = $k.': '.$v .$rn;
337 }
338 }
339 $cnt = count($header);
340 $hdr_s = '';
341 for ($i = 0 ; $i < $cnt ; $i++) {
342 $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
343 }
344 // $debug = "Debug: <123456789012345678901234567890123456789012345678901234567890123456789>\r\n";
345 // $this->foldLine($debug, 78, str_pad('',4));
346 $header = $hdr_s;
347 $header .= $rn; /* One blank line to separate header and body */
348 $raw_length += strlen($header);
349 return $header;
350 }
351
352 /*
353 * function for cleanly folding of headerlines
354 */
355 function foldLine($line, $length, $pre='') {
356 $line = substr($line,0, -2);
357 $length -= 2; /* do not fold between \r and \n */
358 $cnt = strlen($line);
359 if ($cnt > $length) { /* try folding */
360 $fold_string = "\r\n " . $pre;
361 $bFirstFold = false;
362 $aFoldLine = array();
363 while (strlen($line) > $length) {
364 $fold = false;
365 /* handle encoded parts */
366 if (preg_match('/(=\?.+\?.+\?.+\?=)/',$line,$regs)) {
367 $fold_tmp = $regs[1];
368 $iPosEnc = strpos($line,$fold_tmp);
369 $iLengthEnc = strlen($fold_tmp);
370 if ($iPosEnc < $length && ($iPosEnc + $iLengthEnc > $length)) {
371 $fold = true;
372 /* fold just before the start of the encoded string */
373 $aFoldLine[] = substr($line,0,$iPosEnc);
374 $line = substr($line,$iPosEnc);
375 if (!$bFirstFold) {
376 $bFirstFold = true;
377 $length -= strlen($fold_string);
378 }
379 if ($iLengthEnc > $length) { /* place the encoded
380 string on a separate line and do not fold inside it*/
381 if ($iLengthEnc < (998 - strlen($fold_string))) {
382 $aFoldLine[] = substr($line,0,$iLengthEnc);
383 $line = substr($line,$iLengthEnc);
384 } else { /* line is too long, continue with normal folding */
385 $fold = false;
386 }
387 }
388 } else { /* the encoded string fits into the foldlength */
389 /*remainder */
390 $iPosEncEnd = $iPosEnc+$iLengthEnc;
391 $sLineRem = substr($line,$iPosEncEnd,$length - $iPosEncEnd);
392 if (!preg_match('/[=,;\s]/',$sLineRem)) {
393 /*impossible to fold clean in the next part -> fold after the enc string */
394 $aFoldLine[] = substr($line,0,$iPosEncEnd+1);
395 $line = substr($line,$iPosEncEnd+1);
396 $fold = true;
397 if (!$bFirstFold) {
398 $bFirstFold = true;
399 $length -= strlen($fold_string);
400 }
401 }
402 }
403 }
404 if (!$fold) {
405 $line_tmp = substr($line,0,$length);
406 $iFoldPos = false;
407 /* try to fold at logical places */
408 switch (true)
409 {
410 case ($iFoldPos = strrpos($line_tmp,',')): break;
411 case ($iFoldPos = strrpos($line_tmp,';')): break;
412 case ($iFoldPos = strrpos($line_tmp,' ')): break;
413 case ($iFoldPos = strrpos($line_tmp,'=')): break;
414 default: break;
415 }
416
417 if (!$iFoldPos) { /* clean folding didn't work */
418 $iFoldPos = $length;
419 }
420 $aFoldLine[] = substr($line,0,$iFoldPos+1);
421 $line = substr($line,$iFoldPos+1);
422 if (!$bFirstFold) {
423 $bFirstFold = true;
424 $length -= strlen($fold_string);
425 }
426 }
427 }
428 /*$reconstruct the line */
429 if ($line) {
430 $aFoldLine[] = $line;
431 }
432 $line = implode($fold_string,$aFoldLine);
433 }
434 return $line."\r\n";
435 }
436
437 function mimeBoundary () {
438 static $mimeBoundaryString;
439
440 if ( !isset( $mimeBoundaryString ) ||
441 $mimeBoundaryString == '') {
442 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
443 mt_rand( 10000, 99999 );
444 }
445 return $mimeBoundaryString;
446 }
447
448 /* Time offset for correct timezone */
449 function timezone () {
450 global $invert_time;
451
452 $diff_second = date('Z');
453 if ($invert_time) {
454 $diff_second = - $diff_second;
455 }
456 if ($diff_second > 0) {
457 $sign = '+';
458 } else {
459 $sign = '-';
460 }
461 $diff_second = abs($diff_second);
462 $diff_hour = floor ($diff_second / 3600);
463 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
464 $zonename = '('.strftime('%Z').')';
465 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
466 $zonename);
467 return ($result);
468 }
469
470 function calculate_references($hdr) {
471 $refer = $hdr->references;
472 $message_id = $hdr->message_id;
473 $in_reply_to = $hdr->in_reply_to;
474 if (strlen($refer) > 2) {
475 $refer .= ' ' . $message_id;
476 } else {
477 if ($in_reply_to) {
478 $refer .= $in_reply_to . ' ' . $message_id;
479 } else {
480 $refer .= $message_id;
481 }
482 }
483 trim($refer);
484 return $refer;
485 }
486 }
487 ?>