1d639574f6f12fa57d0db52014fc403d166c3105
[squirrelmail.git] / class / deliver / Deliver.class.php
1 <?php
2
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
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 preWriteToStream(&$s) {
144 }
145
146 function writeToStream($stream, $data) {
147 fputs($stream, $data);
148 }
149
150 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
151 return $stream;
152 }
153
154 function getBcc() {
155 return false;
156 }
157
158 function prepareMIME_Header($message, $boundary) {
159 $mime_header = $message->mime_header;
160 $rn="\r\n";
161 $header = array();
162
163 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
164 $mime_header->type1;
165 if (count($message->entities)) {
166 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
167 }
168 if (isset($mime_header->parameters['name'])) {
169 $contenttype .= '; name="'.
170 encodeHeader($mime_header->parameters['name']). '"';
171 }
172 if (isset($mime_header->parameters['charset'])) {
173 $charset = $mime_header->parameters['charset'];
174 $contenttype .= '; charset="'.
175 encodeHeader($charset). '"';
176 }
177
178
179 $header[] = $contenttype . $rn;
180 if ($mime_header->description) {
181 $header[] .= 'Content-Description: ' . $mime_header->description . $rn;
182 }
183 if ($mime_header->encoding) {
184 $encoding = $mime_header->encoding;
185 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
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 }
192 }
193 if ($mime_header->id) {
194 $header[] .= 'Content-ID: ' . $mime_header->id . $rn;
195 }
196 if ($mime_header->disposition) {
197 $disposition = $mime_header->disposition;
198 $contentdisp = 'Content-Disposition: ' . $disposition->name;
199 if ($disposition->getProperty('filename')) {
200 $contentdisp .= '; filename="'.
201 encodeHeader($disposition->getProperty('filename')). '"';
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++) {
215 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
216 }
217 $header = $hdr_s;
218 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
219 return $header;
220 }
221
222 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
223 $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
224 $SERVER_NAME = $_SERVER['SERVER_NAME'];
225 $REMOTE_PORT = $_SERVER['REMOTE_PORT'];
226 if(isset($_SERVER['REMOTE_HOST'])) {
227 $REMOTE_HOST = $_SERVER['REMOTE_HOST'];
228 }
229 if(isset($_SERVER['HTTP_VIA'])) {
230 $HTTP_VIA = $_SERVER['HTTP_VIA'];
231 }
232 if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
233 $HTTP_X_FORWARDED_FOR = $_SERVER['HTTP_X_FORWARDED_FOR'];
234 }
235 global $version, $username;
236 $rn = "\r\n";
237 /* This creates an RFC 822 date */
238 $date = date("D, j M Y H:i:s ", mktime()) . $this->timezone();
239 /* Create a message-id */
240 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
241 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
242 /* Make an RFC822 Received: line */
243 if (isset($REMOTE_HOST)) {
244 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
245 } else {
246 $received_from = $REMOTE_ADDR;
247 }
248 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
249 if ($HTTP_X_FORWARDED_FOR == '') {
250 $HTTP_X_FORWARDED_FOR = 'unknown';
251 }
252 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
253 }
254 $header = array();
255 $header[] = "Received: from $received_from" . $rn;
256 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
257 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
258 $header[] = " $date" . $rn;
259 /* Insert the rest of the header fields */
260 $header[] = 'Message-ID: '. $message_id . $rn;
261 if ($reply_rfc822_header->message_id) {
262 $header[] = 'In-Reply-To: '.$reply_rfc822_header->message_id . $rn;
263 $references = $this->calculate_references($reply_rfc822_header);
264 $header[] = 'References: '.$references . $rn;
265 }
266 $header[] = "Date: $date" . $rn;
267 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
268 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
269 /* RFC2822 if from contains more then 1 address */
270 if (count($rfc822_header->from) > 1) {
271 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
272 }
273 if (count($rfc822_header->to)) {
274 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn;
275 }
276 if (count($rfc822_header->cc)) {
277 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
278 }
279 if (count($rfc822_header->reply_to)) {
280 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
281 }
282 /* Sendmail should return true. Default = false */
283 $bcc = $this->getBcc();
284 if (count($rfc822_header->bcc)) {
285 $s = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
286 if (!$bcc) {
287 $s = $this->foldLine($s, 78, str_pad('',4));
288 $raw_length += strlen($s);
289 } else {
290 $header[] = $s;
291 }
292 }
293 /* Identify SquirrelMail */
294 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
295 /* Do the MIME-stuff */
296 $header[] = "MIME-Version: 1.0" . $rn;
297 $contenttype = 'Content-Type: '. $rfc822_header->content_type->type0 .'/'.
298 $rfc822_header->content_type->type1;
299 if (count($rfc822_header->content_type->properties)) {
300 foreach ($rfc822_header->content_type->properties as $k => $v) {
301 if ($k && $v) {
302 $contenttype .= ';' .$k.'='.$v;
303 }
304 }
305 }
306 $header[] = $contenttype . $rn;
307 if ($rfc822_header->dnt) {
308 $dnt = $rfc822_header->getAddr_s('dnt');
309 /* Pegasus Mail */
310 $header[] = 'X-Confirm-Reading-To: '.$dnt. $rn;
311 /* RFC 2298 */
312 $header[] = 'Disposition-Notification-To: '.$dnt. $rn;
313 }
314 if ($rfc822_header->priority) {
315 $prio = $rfc822_header->priority;
316 $header[] = 'X-Priority: '.$prio. $rn;
317 switch($prio) {
318 case 1: $header[] = 'Importance: High'. $rn; break;
319 case 3: $header[] = 'Importance: Normal'. $rn; break;
320 case 5: $header[] = 'Importance: Low'. $rn; break;
321 default: break;
322 }
323 }
324 /* Insert headers from the $more_headers array */
325 if(count($rfc822_header->more_headers)) {
326 reset($rfc822_header->more_headers);
327 foreach ($rfc822_header->more_headers as $k => $v) {
328 $header[] = $k.': '.$v .$rn;
329 }
330 }
331 $cnt = count($header);
332 $hdr_s = '';
333 for ($i = 0 ; $i < $cnt ; $i++) {
334 $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
335 }
336 $header = $hdr_s;
337 $header .= $rn; /* One blank line to separate header and body */
338 $raw_length += strlen($header);
339 return $header;
340 }
341
342 /*
343 * function for cleanly folding of headerlines
344 */
345 function foldLine($line, $length, $pre='') {
346 $cnt = strlen($line);
347 $res = '';
348 if ($cnt > $length) {
349 $fold_string = "\r\n " . $pre;
350 $length -=strlen($fold_string);
351 for ($i=0;$i<($cnt-$length);$i++) {
352 $fold_pos = 0;
353 /* first try to fold at delimiters */
354 for ($j=($i+$length); $j>$i; --$j) {
355 switch ($line{$j}) {
356 case (','):
357 case (';'): $fold_pos = $i = $j; break;
358 default: break;
359 }
360 }
361 if (!$fold_pos) { /* not succeed yet so we try at spaces & = */
362 for ($j=($i+$length); $j>$i; $j--) {
363 switch ($line{$j}) {
364 case (' '):
365 case ('='): $fold_pos = $i = $j; break;
366 default: break;
367 }
368 }
369 }
370 if (!$fold_pos) { /* clean folding didn't work */
371 $i = $j = $fold_pos = $i+$length;
372 }
373 $line = substr_replace($line,$line{$fold_pos}.$fold_string,
374 $fold_pos,1);
375 $cnt += strlen($fold_string);
376 $i = $j + strlen($fold_string);
377 }
378 }
379 return $line;
380 }
381
382
383 function mimeBoundary () {
384 static $mimeBoundaryString;
385
386 if ( !isset( $mimeBoundaryString ) ||
387 $mimeBoundaryString == '') {
388 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
389 mt_rand( 10000, 99999 );
390 }
391 return $mimeBoundaryString;
392 }
393
394 /* Time offset for correct timezone */
395 function timezone () {
396 global $invert_time;
397
398 $diff_second = date('Z');
399 if ($invert_time) {
400 $diff_second = - $diff_second;
401 }
402 if ($diff_second > 0) {
403 $sign = '+';
404 } else {
405 $sign = '-';
406 }
407 $diff_second = abs($diff_second);
408 $diff_hour = floor ($diff_second / 3600);
409 $diff_minute = floor (($diff_second-3600*$diff_hour) / 60);
410 $zonename = '('.strftime('%Z').')';
411 $result = sprintf ("%s%02d%02d %s", $sign, $diff_hour, $diff_minute,
412 $zonename);
413 return ($result);
414 }
415
416 function calculate_references($hdr) {
417 $refer = $hdr->references;
418 if (strlen($refer) > 2) {
419 $refer .= ' ' . $hdr->message_id;
420 } else {
421 if ($hdr->in_reply_to) {
422 $refer .= $hdr->in_reply_to . ' ' . $hdr->message_id;
423 } else {
424 $refer .= $hdr->message_id;
425 }
426 }
427 trim($refer);
428 return $refer;
429 }
430 }
431 ?>