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