remove unnessecarry \r\n
[squirrelmail.git] / class / deliver / Deliver.class.php
CommitLineData
4960ec8e 1<?php
2
69298c28 3/**
4 * Deliver.class.php
5 *
6 * Copyright (c) 1999-2002 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
4960ec8e 15class Deliver {
16
59387d1c 17 function mail($message, $stream=false) {
3b53d87f 18 $rfc822_header = $message->rfc822_header;
19 if (count($message->entities)) {
59387d1c 20 $boundary = $this->mimeBoundary();
1274f430 21 $rfc822_header->content_type->properties['boundary']='"'.$boundary.'"';
3b53d87f 22 } else {
23 $boundary='';
24 }
75b14a70 25 $raw_length = 0;
1274f430 26 $reply_rfc822_header = (isset($message->reply_rfc822_header)
27 ? $message->reply_rfc822_header : '');
75b14a70 28 $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
29
3b53d87f 30 if ($stream) {
59387d1c 31 $this->preWriteToStream($header);
32 $this->writeToStream($stream, $header);
3b53d87f 33 }
59387d1c 34 $this->writeBody($message, $stream, $raw_length, $boundary);
3b53d87f 35 return $raw_length;
36 }
37
38 function writeBody($message, $stream, &$length_raw, $boundary='') {
1274f430 39 if ($boundary && !$message->rfc822_header) {
3b53d87f 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 }
59387d1c 48 $this->writeBodyPart($message, $stream, $length_raw);
3b53d87f 49 $boundary_depth = substr_count($message->entity_id,'.');
50 if ($boundary_depth) {
51 $boundary .= '_part'.$boundary_depth;
52 }
1274f430 53 $last = false;
3b53d87f 54 for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) {
59387d1c 55 $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary);
1274f430 56 if ($i == $entCount-1) $last = true;
3b53d87f 57 }
1274f430 58 if ($boundary && $last) {
252737e1 59 $s = "--".$boundary."--\r\n\r\n";
3b53d87f 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) {
1274f430 69 if ($message->mime_header) {
70 $type0 = $message->mime_header->type0;
71 } else {
72 $type0 = $message->rfc822_header->content_type->type0;
73 }
252737e1 74
75 $body_part_trailing = $last = '';
1274f430 76 switch ($type0) {
3b53d87f 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);
59387d1c 84 $this->writeToStream($stream, $body_part);
3b53d87f 85 }
252737e1 86 $last = $body_part;
3b53d87f 87 } elseif ($message->att_local_name) {
88 $filename = $message->att_local_name;
89 $file = fopen ($filename, 'rb');
252737e1 90 while ($body_part = fgets($file, 4096)) {
91 $length += $this->clean_crlf($body_part);
3b53d87f 92 if ($stream) {
252737e1 93 $this->preWriteToStream($body_part);
94 $this->writeToStream($stream, $body_part);
59387d1c 95 }
252737e1 96 $last = $body_part;
3b53d87f 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) {
59387d1c 106 $this->writeToStream($stream, $body_part);
59387d1c 107 }
3b53d87f 108 } elseif ($message->att_local_name) {
109 $filename = $message->att_local_name;
110 $file = fopen ($filename, 'rb');
252737e1 111 $encoded = '';
1274f430 112 while ($tmp = fread($file, 570)) {
252737e1 113 $body_part = chunk_split(base64_encode($tmp));
114 $length += $this->clean_crlf($body_part);
3b53d87f 115 if ($stream) {
252737e1 116 $this->writeToStream($stream, $body_part);
3b53d87f 117 }
118 }
119 fclose($file);
120 }
121 break;
122 }
252737e1 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 }
1274f430 133 }
3b53d87f 134 }
135
69298c28 136 function clean_crlf(&$s) {
3b53d87f 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
69298c28 143 function preWriteToStream(&$s) {
3b53d87f 144 }
145
146 function writeToStream($stream, $data) {
1274f430 147 fputs($stream, $data);
3b53d87f 148 }
149
150 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
151 return $stream;
152 }
59387d1c 153
0dab3bfb 154 function getBcc() {
59387d1c 155 return false;
156 }
3b53d87f 157
158 function prepareMIME_Header($message, $boundary) {
59387d1c 159 $mime_header = $message->mime_header;
160 $rn="\r\n";
4960ec8e 161 $header = array();
3b53d87f 162
59387d1c 163 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
164 $mime_header->type1;
3b53d87f 165 if (count($message->entities)) {
166 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
167 }
4960ec8e 168 if (isset($mime_header->parameters['name'])) {
1274f430 169 $contenttype .= '; name="'.
4960ec8e 170 encodeHeader($mime_header->parameters['name']). '"';
171 }
1274f430 172 if (isset($mime_header->parameters['charset'])) {
173 $charset = $mime_header->parameters['charset'];
174 $contenttype .= '; charset="'.
175 encodeHeader($charset). '"';
176 }
177
178
4960ec8e 179 $header[] = $contenttype . $rn;
180 if ($mime_header->description) {
181 $header[] .= 'Content-Description: ' . $mime_header->description . $rn;
182 }
183 if ($mime_header->encoding) {
1274f430 184 $encoding = $mime_header->encoding;
4960ec8e 185 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
1274f430 186 } else {
187 if ($mime_header->type0 == 'text' || $mime_header->type0 == 'message') {
188 $header[] .= 'Content-Transfer-Encoding: 8bit' . $rn;
189 } else {
190 $header[] .= 'Content-Transfer-Encoding: base64' . $rn;
191 }
4960ec8e 192 }
193 if ($mime_header->id) {
194 $header[] .= 'Content-ID: ' . $mime_header->id . $rn;
195 }
196 if ($mime_header->disposition) {
1274f430 197 $disposition = $mime_header->disposition;
198 $contentdisp = 'Content-Disposition: ' . $disposition->name;
199 if ($disposition->getProperty('filename')) {
200 $contentdisp .= '; filename="'.
201 encodeHeader($disposition->getProperty('filename')). '"';
4960ec8e 202 }
203 $header[] = $contentdisp . $rn;
204 }
205 if ($mime_header->md5) {
206 $header[] .= 'Content-MD5: ' . $mime_header->md5 . $rn;
207 }
208 if ($mime_header->language) {
209 $header[] .= 'Content-Language: ' . $mime_header->language . $rn;
210 }
211
212 $cnt = count($header);
213 $hdr_s = '';
214 for ($i = 0 ; $i < $cnt ; $i++) {
75b14a70 215 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
4960ec8e 216 }
217 $header = $hdr_s;
3b53d87f 218 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
219 return $header;
4960ec8e 220 }
221
75b14a70 222 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
4960ec8e 223 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
224 global $version, $useSendmail, $username;
225 global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
226 global $REMOTE_HOST;
59387d1c 227 $rn = "\r\n";
4960ec8e 228 /* This creates an RFC 822 date */
59387d1c 229 $date = date("D, j M Y H:i:s ", mktime()) . $this->timezone();
4960ec8e 230 /* Create a message-id */
231 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
232 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
233 /* Make an RFC822 Received: line */
59387d1c 234 if (isset($REMOTE_HOST)) {
235 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
236 } else {
237 $received_from = $REMOTE_ADDR;
238 }
239 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
240 if ($HTTP_X_FORWARDED_FOR == '') {
241 $HTTP_X_FORWARDED_FOR = 'unknown';
242 }
243 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
244 }
245 $header = array();
246 $header[] = "Received: from $received_from" . $rn;
247 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
248 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
249 $header[] = " $date" . $rn;
250 /* Insert the rest of the header fields */
251 $header[] = 'Message-ID: '. $message_id . $rn;
1274f430 252 if ($reply_rfc822_header->message_id) {
253 $header[] = 'In-Reply-To: '.$reply_rfc822_header->message_id . $rn;
254 $references = $this->calculate_references($reply_rfc822_header);
59387d1c 255 $header[] = 'References: '.$references . $rn;
256 }
257 $header[] = "Date: $date" . $rn;
258 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
259 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
260 /* RFC2822 if from contains more then 1 address */
261 if (count($rfc822_header->from) > 1) {
262 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
263 }
75b14a70 264 if (count($rfc822_header->to)) {
265 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn;
266 }
59387d1c 267 if (count($rfc822_header->cc)) {
268 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
269 }
270 if (count($rfc822_header->reply_to)) {
271 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
272 }
273 /* Sendmail should return true. Default = false */
1274f430 274 $bcc = $this->getBcc();
75b14a70 275 if (count($rfc822_header->bcc)) {
276 $s = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
277 if (!$bcc) {
278 $s = $this->foldLine($s, 78, str_pad('',4));
279 $raw_length += strlen($s);
280 } else {
281 $header[] = $s;
282 }
59387d1c 283 }
284 /* Identify SquirrelMail */
285 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
286 /* Do the MIME-stuff */
287 $header[] = "MIME-Version: 1.0" . $rn;
288 $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
289 $rfc822_header->content_type->type1;
290 if (count($rfc822_header->content_type->properties)) {
1274f430 291 foreach ($rfc822_header->content_type->properties as $k => $v) {
292 if ($k && $v) {
293 $contenttype .= ';' .$k.'='.$v;
294 }
59387d1c 295 }
296 }
297 $header[] = $contenttype . $rn;
298 if ($rfc822_header->dnt) {
299 $dnt = $rfc822_header->getAddr_s('dnt');
300 /* Pegasus Mail */
301 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
302 /* RFC 2298 */
303 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
304 }
305 if ($rfc822_header->priority) {
306 $prio = $rfc822_header->priority;
307 $header[] = 'X-Priority: '.$prio. $rn;
308 switch($prio) {
309 case 1: $header[] = 'Importance: High'. $rn; break;
310 case 3: $header[] = 'Importance: Normal'. $rn; break;
311 case 5: $header[] = 'Importance: Low'. $rn; break;
312 default: break;
313 }
314 }
315 /* Insert headers from the $more_headers array */
316 if(count($rfc822_header->more_headers)) {
317 reset($rfc822_header->more_headers);
318 foreach ($rfc822_header->more_headers as $k => $v) {
319 $header[] = $k.': '.$v .$rn;
320 }
321 }
322 $cnt = count($header);
323 $hdr_s = '';
324 for ($i = 0 ; $i < $cnt ; $i++) {
75b14a70 325 $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
59387d1c 326 }
327 $header = $hdr_s;
328 $header .= $rn; /* One blank line to separate header and body */
75b14a70 329 $raw_length += strlen($header);
59387d1c 330 return $header;
4960ec8e 331 }
332
333 /*
334 * function for cleanly folding of headerlines
335 */
1274f430 336 function foldLine($line, $length, $pre='') {
337 $cnt = strlen($line);
338 $res = '';
339 if ($cnt > $length) {
340 $fold_string = "\r\n " . $pre;
341 $length -=strlen($fold_string);
342 for ($i=0;$i<($cnt-$length);$i++) {
343 $fold_pos = 0;
344 /* first try to fold at delimiters */
345 for ($j=($i+$length); $j>$i; --$j) {
346 switch ($line{$j}) {
347 case (','):
348 case (';'): $fold_pos = $i = $j; break;
349 default: break;
350 }
4960ec8e 351 }
1274f430 352 if (!$fold_pos) { /* not succeed yet so we try at spaces & = */
353 for ($j=($i+$length); $j>$i; $j--) {
354 switch ($line{$j}) {
355 case (' '):
356 case ('='): $fold_pos = $i = $j; break;
357 default: break;
358 }
359 }
360 }
361 if (!$fold_pos) { /* clean folding didn't work */
362 $i = $j = $fold_pos = $i+$length;
363 }
364 $line = substr_replace($line,$line{$fold_pos}.$fold_string,
365 $fold_pos,1);
366 $cnt += strlen($fold_string);
367 $i = $j + strlen($fold_string);
368 }
369 }
370 return $line;
4960ec8e 371 }
3b53d87f 372
3b53d87f 373
59387d1c 374 function mimeBoundary () {
375 static $mimeBoundaryString;
3b53d87f 376
59387d1c 377 if ( !isset( $mimeBoundaryString ) ||
378 $mimeBoundaryString == '') {
379 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
380 mt_rand( 10000, 99999 );
381 }
382 return $mimeBoundaryString;
3b53d87f 383 }
384
59387d1c 385 /* Time offset for correct timezone */
386 function timezone () {
387 global $invert_time;
3b53d87f 388
59387d1c 389 $diff_second = date('Z');
390 if ($invert_time) {
391 $diff_second = - $diff_second;
392 }
393 if ($diff_second > 0) {
394 $sign = '+';
395 } else {
396 $sign = '-';
397 }
398 $diff_second = abs($diff_second);
399 $diff_hour = floor ($diff_second / 3600);
400 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
401 $zonename = '('.strftime('%Z').')';
402 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
3b53d87f 403 $zonename);
59387d1c 404 return ($result);
405 }
3b53d87f 406
1274f430 407 function calculate_references($hdr) {
408 $refer = $hdr->references;
59387d1c 409 if (strlen($refer) > 2) {
1274f430 410 $refer .= ' ' . $hdr->message_id;
59387d1c 411 } else {
1274f430 412 if ($hdr->in_reply_to) {
413 $refer .= $hdr->in_reply_to . ' ' . $hdr->message_id;
59387d1c 414 } else {
1274f430 415 $refer .= $hdr->message_id;
59387d1c 416 }
417 }
418 trim($refer);
419 return $refer;
420 }
421}
4960ec8e 422?>