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 |
15 | class 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 | } |
1274f430 |
25 | $reply_rfc822_header = (isset($message->reply_rfc822_header) |
26 | ? $message->reply_rfc822_header : ''); |
27 | $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header); |
3b53d87f |
28 | $raw_length = strlen($header); |
29 | if ($stream) { |
59387d1c |
30 | $this->preWriteToStream($header); |
31 | $this->writeToStream($stream, $header); |
3b53d87f |
32 | } |
59387d1c |
33 | $this->writeBody($message, $stream, $raw_length, $boundary); |
3b53d87f |
34 | return $raw_length; |
35 | } |
36 | |
37 | function writeBody($message, $stream, &$length_raw, $boundary='') { |
1274f430 |
38 | if ($boundary && !$message->rfc822_header) { |
3b53d87f |
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 | } |
59387d1c |
47 | $this->writeBodyPart($message, $stream, $length_raw); |
3b53d87f |
48 | $boundary_depth = substr_count($message->entity_id,'.'); |
49 | if ($boundary_depth) { |
50 | $boundary .= '_part'.$boundary_depth; |
51 | } |
1274f430 |
52 | $last = false; |
3b53d87f |
53 | for ($i=0, $entCount=count($message->entities);$i<$entCount;$i++) { |
59387d1c |
54 | $msg = $this->writeBody($message->entities[$i], $stream, $length_raw, $boundary); |
1274f430 |
55 | if ($i == $entCount-1) $last = true; |
3b53d87f |
56 | } |
1274f430 |
57 | if ($boundary && $last) { |
58 | $s = "\r\n--".$boundary."--\r\n\r\n"; |
3b53d87f |
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) { |
1274f430 |
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) { |
3b53d87f |
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); |
59387d1c |
81 | $this->writeToStream($stream, $body_part); |
3b53d87f |
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); |
59387d1c |
91 | } |
3b53d87f |
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) { |
59387d1c |
101 | $this->writeToStream($stream, $body_part); |
59387d1c |
102 | } |
3b53d87f |
103 | } elseif ($message->att_local_name) { |
104 | $filename = $message->att_local_name; |
105 | $file = fopen ($filename, 'rb'); |
1274f430 |
106 | while ($tmp = fread($file, 570)) { |
3b53d87f |
107 | $encoded = chunk_split(base64_encode($tmp)); |
1274f430 |
108 | |
3b53d87f |
109 | if ($stream) { |
110 | $this->writeToStream($stream, $encoded); |
111 | } |
112 | } |
113 | fclose($file); |
114 | } |
115 | break; |
116 | } |
1274f430 |
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); |
1274f430 |
122 | } |
3b53d87f |
123 | } |
124 | |
69298c28 |
125 | function clean_crlf(&$s) { |
3b53d87f |
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 | |
69298c28 |
132 | function preWriteToStream(&$s) { |
3b53d87f |
133 | } |
134 | |
135 | function writeToStream($stream, $data) { |
1274f430 |
136 | fputs($stream, $data); |
3b53d87f |
137 | } |
138 | |
139 | function initStream($message, $length=0, $host='', $port='', $user='', $pass='') { |
140 | return $stream; |
141 | } |
59387d1c |
142 | |
0dab3bfb |
143 | function getBcc() { |
59387d1c |
144 | return false; |
145 | } |
3b53d87f |
146 | |
147 | function prepareMIME_Header($message, $boundary) { |
59387d1c |
148 | $mime_header = $message->mime_header; |
149 | $rn="\r\n"; |
4960ec8e |
150 | $header = array(); |
3b53d87f |
151 | |
59387d1c |
152 | $contenttype = 'Content-Type: '. $mime_header->type0 .'/'. |
153 | $mime_header->type1; |
3b53d87f |
154 | if (count($message->entities)) { |
155 | $contenttype .= ";\r\n " . 'boundary="'.$boundary.'"'; |
156 | } |
4960ec8e |
157 | if (isset($mime_header->parameters['name'])) { |
1274f430 |
158 | $contenttype .= '; name="'. |
4960ec8e |
159 | encodeHeader($mime_header->parameters['name']). '"'; |
160 | } |
1274f430 |
161 | if (isset($mime_header->parameters['charset'])) { |
162 | $charset = $mime_header->parameters['charset']; |
163 | $contenttype .= '; charset="'. |
164 | encodeHeader($charset). '"'; |
165 | } |
166 | |
167 | |
4960ec8e |
168 | $header[] = $contenttype . $rn; |
169 | if ($mime_header->description) { |
170 | $header[] .= 'Content-Description: ' . $mime_header->description . $rn; |
171 | } |
172 | if ($mime_header->encoding) { |
1274f430 |
173 | $encoding = $mime_header->encoding; |
4960ec8e |
174 | $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn; |
1274f430 |
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 | } |
4960ec8e |
181 | } |
182 | if ($mime_header->id) { |
183 | $header[] .= 'Content-ID: ' . $mime_header->id . $rn; |
184 | } |
185 | if ($mime_header->disposition) { |
1274f430 |
186 | $disposition = $mime_header->disposition; |
187 | $contentdisp = 'Content-Disposition: ' . $disposition->name; |
188 | if ($disposition->getProperty('filename')) { |
189 | $contentdisp .= '; filename="'. |
190 | encodeHeader($disposition->getProperty('filename')). '"'; |
4960ec8e |
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++) { |
59387d1c |
204 | $hdr_s .= $this->foldLine($header[$i], 78, ' '); |
4960ec8e |
205 | } |
206 | $header = $hdr_s; |
3b53d87f |
207 | $header .= $rn; /* One blank line to separate mimeheader and body-entity */ |
208 | return $header; |
4960ec8e |
209 | } |
210 | |
1274f430 |
211 | function prepareRFC822_Header($rfc822_header, $reply_rfc822_header) { |
4960ec8e |
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; |
59387d1c |
216 | $rn = "\r\n"; |
4960ec8e |
217 | /* This creates an RFC 822 date */ |
59387d1c |
218 | $date = date("D, j M Y H:i:s ", mktime()) . $this->timezone(); |
4960ec8e |
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 */ |
59387d1c |
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; |
1274f430 |
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); |
59387d1c |
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 */ |
1274f430 |
261 | $bcc = $this->getBcc(); |
59387d1c |
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)) { |
1274f430 |
272 | foreach ($rfc822_header->content_type->properties as $k => $v) { |
273 | if ($k && $v) { |
274 | $contenttype .= ';' .$k.'='.$v; |
275 | } |
59387d1c |
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; |
4960ec8e |
311 | } |
312 | |
313 | /* |
314 | * function for cleanly folding of headerlines |
315 | */ |
1274f430 |
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 | } |
4960ec8e |
331 | } |
1274f430 |
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; |
4960ec8e |
351 | } |
3b53d87f |
352 | |
3b53d87f |
353 | |
59387d1c |
354 | function mimeBoundary () { |
355 | static $mimeBoundaryString; |
3b53d87f |
356 | |
59387d1c |
357 | if ( !isset( $mimeBoundaryString ) || |
358 | $mimeBoundaryString == '') { |
359 | $mimeBoundaryString = '----=_' . date( 'YmdHis' ) . '_' . |
360 | mt_rand( 10000, 99999 ); |
361 | } |
362 | return $mimeBoundaryString; |
3b53d87f |
363 | } |
364 | |
59387d1c |
365 | /* Time offset for correct timezone */ |
366 | function timezone () { |
367 | global $invert_time; |
3b53d87f |
368 | |
59387d1c |
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, |
3b53d87f |
383 | $zonename); |
59387d1c |
384 | return ($result); |
385 | } |
3b53d87f |
386 | |
1274f430 |
387 | function calculate_references($hdr) { |
388 | $refer = $hdr->references; |
59387d1c |
389 | if (strlen($refer) > 2) { |
1274f430 |
390 | $refer .= ' ' . $hdr->message_id; |
59387d1c |
391 | } else { |
1274f430 |
392 | if ($hdr->in_reply_to) { |
393 | $refer .= $hdr->in_reply_to . ' ' . $hdr->message_id; |
59387d1c |
394 | } else { |
1274f430 |
395 | $refer .= $hdr->message_id; |
59387d1c |
396 | } |
397 | } |
398 | trim($refer); |
399 | return $refer; |
400 | } |
401 | } |
4960ec8e |
402 | ?> |