init deliver class as replacement of smtp.php
[squirrelmail.git] / class / deliver / Deliver.class.php
1 <?php
2
3 class Deliver {
4
5 function prepareMIME_Header($message, $rn) {
6 $mime_header = $message->header;
7 $header = array();
8
9 $contenttype = 'Content-Type: '. $mime_header->contenttype->type0 .'/'.
10 $mime_header->contenttype->type1;
11 if (isset($mime_header->parameters['name'])) {
12 $contenttype .= ";\r\n " . 'name="'.
13 encodeHeader($mime_header->parameters['name']). '"';
14 }
15 $header[] = $contenttype . $rn;
16 if ($mime_header->description) {
17 $header[] .= 'Content-Description: ' . $mime_header->description . $rn;
18 }
19 if ($mime_header->encoding) {
20 $header[] .= 'Content-Transfer-Encoding: ' . $mime_header->encoding . $rn;
21 }
22 if ($mime_header->id) {
23 $header[] .= 'Content-ID: ' . $mime_header->id . $rn;
24 }
25 if ($mime_header->disposition) {
26 $contentdisp .= 'Content-Disposition: ' . $mime_header->disposition;
27 if (isset($mime_header->parameters['filename'])) {
28 $contentdisp .= ";\r\n " . 'filename="'.
29 encodeHeader($mime_header->parameters['filename']). '"';
30 }
31 $header[] = $contentdisp . $rn;
32 }
33 if ($mime_header->md5) {
34 $header[] .= 'Content-MD5: ' . $mime_header->md5 . $rn;
35 }
36 if ($mime_header->language) {
37 $header[] .= 'Content-Language: ' . $mime_header->language . $rn;
38 }
39
40 $cnt = count($header);
41 $hdr_s = '';
42 for ($i = 0 ; $i < $cnt ; $i++) {
43 $hdr_s .= foldLine($header[$i], 78, ' ');
44 }
45 $header = $hdr_s;
46 $header .= $rn; /* One blank line to separate header and body */
47 }
48
49 function prepareRFC822_Header($rfc822_header) {
50 global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
51 global $version, $useSendmail, $username;
52 global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
53 global $REMOTE_HOST;
54
55 /* This creates an RFC 822 date */
56 $date = date("D, j M Y H:i:s ", mktime()) . timezone();
57 /* Create a message-id */
58 $message_id = '<' . $REMOTE_PORT . '.' . $REMOTE_ADDR . '.';
59 $message_id .= time() . '.squirrel@' . $SERVER_NAME .'>';
60 /* Make an RFC822 Received: line */
61 if (isset($REMOTE_HOST))
62 {
63 $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
64 }
65 else
66 {
67 $received_from = $REMOTE_ADDR;
68 }
69 if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
70 if ($HTTP_X_FORWARDED_FOR == '') {
71 $HTTP_X_FORWARDED_FOR = 'unknown';
72 }
73 $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
74 }
75 $header = array();
76 $header[] = "Received: from $received_from" . $rn;
77 $header[] = " (SquirrelMail authenticated user $username)" . $rn;
78 $header[] = " by $SERVER_NAME with HTTP;" . $rn;
79 $header[] = " $date" . $rn;
80 /* Insert the rest of the header fields */
81 $header[] = "Message-ID: $message_id" . $rn;
82 $header[] = "Date: $date" . $rn;
83 $header[] = 'Subject: '.encodeHeader($rfc822_header->subject) . $rn;
84 $header[] = 'From: '. encodeHeader($rfc822_header->getAddr_s('from')) . $rn;
85 if (count($rfc822_header->from) > 1) /* RFC2822 if from contains
86 more then 1 address */
87 {
88 $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
89 }
90 $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn; // Who it's TO
91 if (count($rfc_header->cc))
92 {
93 $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
94 }
95 if (count($rfc822_header->$reply_to))
96 {
97 $header[] = 'Reply-To: '. encodeHeader($rfc822_header->getAddr_s('reply_to')) . $rn;
98 }
99 if (count($rfc_header->bcc) && $useSendmail)
100 {
101 $header[] = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
102 }
103 /* Identify SquirrelMail */
104 $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn;
105 /* Do the MIME-stuff */
106 $header[] = "MIME-Version: 1.0" . $rn;
107 $contenttype = 'Content-Type: '. $rfc822_header->contenttype->type0 .'/'.
108 $rfc822_header->contenttype->type1;
109 if (count($rfc822_header->contenttype->properties))
110 {
111 foreach ($rfc822_header->contenttype->properties as $k => $v)
112 {
113 $contenttype .= ';'. "\r\n " .$k.'='.$v; /* FOLDING */
114 }
115 }
116 $header[] = $contenttype . $rn;
117 if ($rfc822_header->dnt)
118 {
119 $dnt = $rfc822_header->getAddr_s('dnt');
120 /* Pegasus Mail */
121 $header[] = 'X-Confirm-Reading-To: '.$dnt;
122 /* RFC 2298 */
123 $header[] = 'Disposition-Notification-To: '.$dnt;
124 }
125 if ($rfc822_header->priority)
126 {
127 $prio = $rfc822_header->priority;
128 $header[] = 'X-Priority: '.$prio;
129 switch($prio)
130 {
131 case 1:
132 $header[] = 'Importance: High';
133 $header[] = 'X-MSMail-Priority: High';
134 break;
135 case 3:
136 $header[] = 'Importance: Normal';
137 $header[] = 'X-MSMail-Priority: Normal';
138 break;
139 case 5:
140 $header[] = 'Importance: Low';
141 $header[] = 'X-MSMail-Priority: Low';
142 break;
143 default:
144 break;
145 }
146 }
147 /* Insert headers from the $more_headers array */
148 if(count($more_headers))
149 {
150 reset($more_headers);
151 foreach ($more_headers as $k => $v)
152 {
153 $header[] = $k.': '.$v;
154 }
155 }
156 $cnt = count($header);
157 $hdr_s = '';
158 for ($i = 0 ; $i < $cnt ; $i++)
159 {
160 $hdr_s .= foldLine($header[$i], 78, ' ');
161 }
162 $header = $hdr_s;
163 $header .= $rn; /* One blank line to separate header and body */
164 $headerlength = strlen($header);
165
166 /* Write the header */
167 if ($fp) fputs ($fp, $header);
168
169 return $headerlength;
170 }
171
172 /*
173 * function for cleanly folding of headerlines
174 */
175 function foldLine($line, $length, $pre) {
176 $cnt = strlen($line);
177 $res = '';
178 if ($cnt > $lenght)
179 {
180 $fold_string = $pre.' '."\r\n";
181 for ($i=0;$i<($cnt-$length);$i++)
182 {
183 $fold_pos = 0;
184 /* first try to fold at delimiters */
185 for ($j=($i+$length); $j>$i; $j--)
186 {
187 switch ($line{$j})
188 {
189 case (','):
190 case (';'):
191 $fold_pos = $j;
192 break;
193 default:
194 break;
195 }
196 if ($fold_pos)
197 {
198 $j=$i;
199 }
200 }
201 if (!$fold_pos)
202 {
203 /* not succeed yet so we try at spaces and = */
204 for ($j=($i+$length); $j>$i; $j--)
205 {
206 switch ($line{$j})
207 {
208 case (' '):
209 case ('='):
210 $fold_pos = $j;
211 break;
212 default:
213 break;
214 }
215 if ($fold_pos)
216 {
217 $j=$i;
218 }
219 }
220 }
221 if (!$fold_pos)
222 {
223 /* clean folding didn't work */
224 $fold_pos = $i+$length;
225 }
226 $line = substr_replace($line,$line{$fold_pos}.$fold_string,$fold_pos,1);
227 $cnt += strlen($fold_string);
228 $i = $j + strlen($fold_string);
229 }
230 }
231 return $line;
232 }
233 }
234 ?>
235