xtra check for To addresses, if not available don't show the headerline.
[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 = "\r\n--".$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 switch ($type0) {
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);
82 $this->writeToStream($stream, $body_part);
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);
92 }
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) {
102 $this->writeToStream($stream, $body_part);
103 }
104 } elseif ($message->att_local_name) {
105 $filename = $message->att_local_name;
106 $file = fopen ($filename, 'rb');
107 while ($tmp = fread($file, 570)) {
108 $encoded = chunk_split(base64_encode($tmp));
109
110 if ($stream) {
111 $this->writeToStream($stream, $encoded);
112 }
113 }
114 fclose($file);
115 }
116 break;
117 }
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);
123 }
124 }
125
126 function clean_crlf(&$s) {
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
133 function preWriteToStream(&$s) {
134 }
135
136 function writeToStream($stream, $data) {
137 fputs($stream, $data);
138 }
139
140 function initStream($message, $length=0, $host='', $port='', $user='', $pass='') {
141 return $stream;
142 }
143
144 function getBcc() {
145 return false;
146 }
147
148 function prepareMIME_Header($message, $boundary) {
149 $mime_header = $message->mime_header;
150 $rn="\r\n";
151 $header = array();
152
153 $contenttype = 'Content-Type: '. $mime_header->type0 .'/'.
154 $mime_header->type1;
155 if (count($message->entities)) {
156 $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"';
157 }
158 if (isset($mime_header->parameters['name'])) {
159 $contenttype .= '; name="'.
160 encodeHeader($mime_header->parameters['name']). '"';
161 }
162 if (isset($mime_header->parameters['charset'])) {
163 $charset = $mime_header->parameters['charset'];
164 $contenttype .= '; charset="'.
165 encodeHeader($charset). '"';
166 }
167
168
169 $header[] = $contenttype . $rn;
170 if ($mime_header->description) {
171 $header[] .= 'Content-Description: ' . $mime_header->description . $rn;
172 }
173 if ($mime_header->encoding) {
174 $encoding = $mime_header->encoding;
175 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
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 }
182 }
183 if ($mime_header->id) {
184 $header[] .= 'Content-ID: ' . $mime_header->id . $rn;
185 }
186 if ($mime_header->disposition) {
187 $disposition = $mime_header->disposition;
188 $contentdisp = 'Content-Disposition: ' . $disposition->name;
189 if ($disposition->getProperty('filename')) {
190 $contentdisp .= '; filename="'.
191 encodeHeader($disposition->getProperty('filename')). '"';
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++) {
205 $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
206 }
207 $header = $hdr_s;
208 $header .= $rn; /* One blank line to separate mimeheader and body-entity */
209 return $header;
210 }
211
212 function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
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;
217 $rn = "\r\n";
218 /* This creates an RFC 822 date */
219 $date = date("D, j M Y H:i:s ", mktime()) . $this->timezone();
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 */
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;
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);
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 }
254 if (count($rfc822_header->to)) {
255 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn;
256 }
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 */
264 $bcc = $this->getBcc();
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 }
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)) {
281 foreach ($rfc822_header->content_type->properties as $k => $v) {
282 if ($k && $v) {
283 $contenttype .= ';' .$k.'='.$v;
284 }
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++) {
315 $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
316 }
317 $header = $hdr_s;
318 $header .= $rn; /* One blank line to separate header and body */
319 $raw_length += strlen($header);
320 return $header;
321 }
322
323 /*
324 * function for cleanly folding of headerlines
325 */
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 }
341 }
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;
361 }
362
363
364 function mimeBoundary () {
365 static $mimeBoundaryString;
366
367 if ( !isset( $mimeBoundaryString ) ||
368 $mimeBoundaryString == '') {
369 $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' .
370 mt_rand( 10000, 99999 );
371 }
372 return $mimeBoundaryString;
373 }
374
375 /* Time offset for correct timezone */
376 function timezone () {
377 global $invert_time;
378
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,
393 $zonename);
394 return ($result);
395 }
396
397 function calculate_references($hdr) {
398 $refer = $hdr->references;
399 if (strlen($refer) > 2) {
400 $refer .= ' ' . $hdr->message_id;
401 } else {
402 if ($hdr->in_reply_to) {
403 $refer .= $hdr->in_reply_to . ' ' . $hdr->message_id;
404 } else {
405 $refer .= $hdr->message_id;
406 }
407 }
408 trim($refer);
409 return $refer;
410 }
411 }
412 ?>