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