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