added use of PHP's session management
[squirrelmail.git] / src / compose.php
1 <?
2 /** This code sends a mail.
3 **
4 ** There are 3 modes of operation:
5 ** - Start new mail
6 ** - Add an attachment
7 ** - Send mail
8 **/
9
10 session_start();
11
12 if (!isset($config_php))
13 include("../config/config.php");
14 if (!isset($strings_php))
15 include("../functions/strings.php");
16 if (!isset($page_header_php))
17 include("../functions/page_header.php");
18 if (!isset($imap_php))
19 include("../functions/imap.php");
20 if (!isset($date_php))
21 include("../functions/date.php");
22 if (!isset($mime_php))
23 include("../functions/mime.php");
24 if (!isset($smtp_php))
25 include("../functions/smtp.php");
26 if (!isset($display_messages_php))
27 include("../functions/display_messages.php");
28
29 include("../src/load_prefs.php");
30
31 // This function is used when not sending or adding attachments
32 function newMail () {
33 global $forward_id, $imapConnection, $msg, $ent_num, $body_ary, $body,
34 $reply_id, $send_to, $send_to_cc, $mailbox;
35
36 $send_to = decodeHeader($send_to);
37 $send_to_cc = decodeHeader($send_to_cc);
38
39 if ($forward_id) {
40 sqimap_mailbox_select($imapConnection, $mailbox);
41 $msg = sqimap_get_message($imapConnection, $forward_id, $mailbox);
42
43 if (containsType($msg, "text", "html", $ent_num)) {
44 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
45 } else if (containsType($msg, "text", "plain", $ent_num)) {
46 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
47 }
48 // add other primary displaying msg types here
49 else {
50 // find any type that's displayable
51 if (containsType($msg, "text", "any_type", $ent_num)) {
52 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
53 } else if (containsType($msg, "msg", "any_type", $ent_num)) {
54 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"]);
55 } else {
56 $body = _("No Message");
57 }
58 }
59
60 $type1 = $msg["ENTITIES"][$ent_num]["TYPE1"];
61
62 $tmp = _("-------- Original Message ---------\n");
63 $body_ary = explode("\n", $body);
64 $body = "";
65 for ($i=0;$i < count($body_ary);$i++) {
66 if ($type1 == "html")
67 $tmp .= strip_tags($body_ary[$i]);
68 else
69 $tmp .= $body_ary[$i];
70 $body = "$body$tmp\n";
71 $tmp = "";
72 }
73 }
74
75 if ($reply_id) {
76 sqimap_mailbox_select($imapConnection, $mailbox);
77 $msg = sqimap_get_message($imapConnection, $reply_id, $mailbox);
78
79 if (containsType($msg, "text", "html", $ent_num)) {
80 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
81 } else if (containsType($msg, "text", "plain", $ent_num)) {
82 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
83 }
84 // add other primary displaying msg types here
85 else {
86 // find any type that's displayable
87 if (containsType($msg, "text", "any_type", $ent_num)) {
88 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
89 } else if (containsType($msg, "msg", "any_type", $ent_num)) {
90 $body = decodeBody($msg["ENTITIES"][$ent_num]["BODY"], $msg["ENTITIES"][$ent_num]["ENCODING"], false);
91 } else {
92 $body = _("No Message");
93 }
94 }
95
96 $type1 = $msg["ENTITIES"][$ent_num]["TYPE1"];
97
98 $body_ary = explode("\n", $body);
99 $body = "";
100 for ($i=0;$i < count($body_ary);$i++) {
101 if ($type1 == "html")
102 $tmp = strip_tags($body_ary[$i]);
103 else
104 $tmp = $body_ary[$i];
105 $body = "$body> $tmp\n";
106 }
107 }
108
109 $send_to = sqimap_find_email($send_to);
110
111 $send_to = ereg_replace("\"", "", $send_to);
112 $send_to = stripslashes($send_to);
113
114 /** This formats a CC string if they hit "reply all" **/
115 if ($send_to_cc != "") {
116 $send_to_cc = ereg_replace(";", ",", $send_to_cc);
117 $sendcc = explode(",", $send_to_cc);
118 $send_to_cc = "";
119
120 for ($i = 0; $i < count($sendcc); $i++) {
121 $sendcc[$i] = trim($sendcc[$i]);
122 if ($sendcc[$i] == "")
123 continue;
124
125 $sendcc[$i] = sqimap_find_email($sendcc[$i]);
126 $whofrom = sqimap_find_displayable_name($msg["HEADER"]["FROM"]);
127 $whoreplyto = sqimap_find_email($msg["HEADER"]["REPLYTO"]);
128
129 if ((strtolower(trim($sendcc[$i])) != strtolower(trim($whofrom))) &&
130 (strtolower(trim($sendcc[$i])) != strtolower(trim($whoreplyto))) &&
131 (trim($sendcc[$i]) != "")) {
132 $send_to_cc .= trim($sendcc[$i]) . ", ";
133 }
134 }
135 $send_to_cc = trim($send_to_cc);
136 if (substr($send_to_cc, -1) == ",") {
137 $send_to_cc = substr($send_to_cc, 0, strlen($send_to_cc) - 1);
138 }
139 }
140 } // function newMail()
141
142 function showInputForm () {
143 global $send_to, $send_to_cc, $reply_subj, $forward_subj, $body,
144 $passed_body, $color, $use_signature, $signature, $editor_size,
145 $attachments, $subject, $newmail, $PHPSESSID;
146
147 $subject = decodeHeader($subject);
148 $reply_subj = decodeHeader($reply_subj);
149 $forward_subj = decodeHeader($forward_subj);
150
151 echo "\n<SCRIPT LANGUAGE=JavaScript><!--\n";
152 echo "function open_abook() { \n";
153 echo " var nwin = window.open(\"addrbook_popup.php?PHPSESSID=$PHPSESSID\",\"abookpopup\",";
154 echo "\"width=670,height=300,resizable=yes,scrollbars=yes\");\n";
155 echo " if((!nwin.opener) && (document.windows != null))\n";
156 echo " nwin.opener = document.windows;\n";
157 echo "}\n";
158 echo "// --></SCRIPT>\n\n";
159
160 echo "\n<FORM name=compose action=\"compose.php?PHPSESSID=$PHPSESSID\" METHOD=POST\n";
161 echo "ENCTYPE=\"multipart/form-data\">\n";
162 echo "<TABLE COLS=2 WIDTH=50 ALIGN=center CELLSPACING=0 BORDER=0>\n";
163 echo " <TR>\n";
164 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
165 echo _("To:");
166 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
167 if ($send_to)
168 echo " <INPUT TYPE=TEXT NAME=send_to VALUE=\"$send_to\" SIZE=60><BR>";
169 else
170 echo " <INPUT TYPE=TEXT NAME=send_to SIZE=60><BR>";
171 echo " </TD>\n";
172 echo " </TR>\n";
173 echo " <TR>\n";
174 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
175 echo _("CC:");
176 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
177 if ($send_to_cc)
178 echo " <INPUT TYPE=TEXT NAME=send_to_cc SIZE=60 VALUE=\"$send_to_cc\"><BR>";
179 else
180 echo " <INPUT TYPE=TEXT NAME=send_to_cc SIZE=60><BR>";
181 echo " </TD>\n";
182 echo " </TR>\n";
183 echo " <TR>\n";
184 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
185 echo _("BCC:");
186 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
187 if ($send_to_bcc)
188 echo " <INPUT TYPE=TEXT NAME=send_to_bcc VALUE=\"$send_to_bcc\" SIZE=60><BR>";
189 else
190 echo " <INPUT TYPE=TEXT NAME=send_to_bcc SIZE=60><BR>";
191 echo " </TD>\n";
192 echo " </TR>\n";
193
194 echo "<SCRIPT LANGUAGE=JavaScript><!--\n document.write(\"";
195 echo "<TR><TD BGCOLOR=\\\"$color[4]\\\">&nbsp;</TD>";
196 echo "</TD><TD BGCOLOR=\\\"$color[4]\\\" ALIGN=LEFT>";
197 printf("<A HREF=\\\"javascript:open_abook();\\\">%s</A>",
198 _("Lookup recipients in addressbook.")."<BR>");
199 echo "</TD></TR>\");\n";
200 echo "// --></SCRIPT>\n";
201
202 echo " <TR>\n";
203 echo " <TD WIDTH=50 BGCOLOR=\"$color[4]\" ALIGN=RIGHT>\n";
204 echo _("Subject:");
205 echo " </TD><TD WIDTH=% BGCOLOR=\"$color[4]\" ALIGN=LEFT>\n";
206 if ($reply_subj) {
207 $reply_subj = str_replace("\"", "'", $reply_subj);
208 $reply_subj = stripslashes($reply_subj);
209 $reply_subj = trim($reply_subj);
210 if (substr(strtolower($reply_subj), 0, 3) != "re:")
211 $reply_subj = "Re: $reply_subj";
212 echo " <INPUT TYPE=TEXT NAME=subject SIZE=60 VALUE=\"$reply_subj\">";
213 } else if ($forward_subj) {
214 $forward_subj = str_replace("\"", "'", $forward_subj);
215 $forward_subj = stripslashes($forward_subj);
216 $forward_subj = trim($forward_subj);
217 if ((substr(strtolower($forward_subj), 0, 4) != "fwd:") &&
218 (substr(strtolower($forward_subj), 0, 5) != "[fwd:") &&
219 (substr(strtolower($forward_subj), 0, 6) != "[ fwd:"))
220 $forward_subj = "[Fwd: $forward_subj]";
221 echo " <INPUT TYPE=TEXT NAME=subject SIZE=50 VALUE=\"$forward_subj\">";
222 } else {
223 echo " <INPUT TYPE=TEXT NAME=subject VALUE=\"$subject\" SIZE=50>";
224 }
225 echo "&nbsp;&nbsp;<INPUT TYPE=SUBMIT NAME=send VALUE=\"". _("Send") . "\">";
226 echo " </TD>\n";
227 echo " </TR>\n";
228
229 echo " <TR>\n";
230 echo " <TD BGCOLOR=\"$color[4]\" COLSPAN=2>\n";
231 if ($use_signature == true && $newmail == true)
232 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>". $body . "\n\n-- \n".$signature."</TEXTAREA><BR>";
233 else
234 echo " &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS=\"$editor_size\" WRAP=HARD>".$body."</TEXTAREA><BR>\n";
235 echo " </TD>\n";
236 echo " </TR>\n";
237 echo " <TR><TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT NAME=send VALUE=\"";
238 echo _("Send");
239 echo "\"></TD></TR>\n";
240
241 // This code is for attachments
242 echo " <tr>\n";
243 echo " <TD WIDTH=50 BGCOLOR=\"$color[0]\" VALIGN=TOP ALIGN=RIGHT>\n";
244 echo " <SMALL><BR></SMALL>"._("Attach:");
245 echo " </td><td width=% ALIGN=left BGCOLOR=\"$color[0]\">\n";
246 // echo " <INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\"\n";
247 // echo " value=\"10000\">\n";
248 echo " <INPUT NAME=\"attachfile\" TYPE=\"file\">\n";
249 echo " &nbsp;&nbsp;<input type=\"submit\" name=\"attach\"\n";
250 echo " value=\"" . _("Add") ."\">\n";
251 echo " </td>\n";
252 echo " </font>\n";
253 echo " </tr>\n";
254 if (isset($attachments) && count($attachments)>0) {
255 echo "</tr><tr><td width=50 bgcolor=\"$color[0]\" align=right>\n";
256 echo "&nbsp;";
257 echo "</td><td width=% align=left bgcolor=\"$color[0]\">";
258 while (list($localname, $remotename) = each($attachments)) {
259 echo "<input type=\"checkbox\" name=\"delete[]\" value=\"$localname\">\n";
260 echo "$remotename <input type=\"hidden\" name=\"attachments[$localname]\" value=\"$remotename\"><br>\n";
261 }
262
263 echo "<input type=\"submit\" name=\"do_delete\" value=\""._("Delete selected attachments")."\">\n";
264 echo "</td></tr>";
265 }
266 // End of attachment code
267
268 echo "</TABLE>\n";
269 echo "</FORM>";
270 }
271
272 function showSentForm () {
273 global $PHPSESSID;
274
275 echo "<BR><BR><BR><CENTER><B>Message Sent!</B><BR><BR>";
276 echo "You will be automatically forwarded.<BR>If not, <A HREF=\"right_main.php?PHPSESSID=$PHPSESSID\">click here</A>";
277 echo "</CENTER>";
278 }
279
280 function checkInput ($show) {
281 /** I implemented the $show variable because the error messages
282 were getting sent before the page header. So, I check once
283 using $show=false, and then when i'm ready to display the
284 error message, show=true **/
285 global $body, $send_to, $subject, $color;
286
287 if ($body == "") {
288 if ($show)
289 plain_error_message(_("You have not entered a message body."), $color);
290 return false;
291 } else if ($send_to == "") {
292 if ($show)
293 plain_error_message(_("You have not filled in the \"To:\" field."), $color);
294 return false;
295 } else if ($subject == "") {
296 if ($show)
297 plain_error_message(_("You have not entered a subject."), $color);
298 return false;
299 }
300 return true;
301 } // function checkInput()
302
303 if(isset($send)) {
304 if (checkInput(false)) {
305 sendMessage($send_to, $send_to_cc, $send_to_bcc, $subject, $body);
306 header ("Location: right_main.php?PHPSESSID=$PHPSESSID");
307 } else {
308 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
309 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
310 displayPageHeader($color, "None");
311 checkInput(true);
312
313 showInputForm();
314 }
315 } else if (isset($attach)) {
316 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
317 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
318 displayPageHeader($color, "None");
319
320 $localfilename = md5("$attachfile, $attachfile_name, $REMOTE_IP, $REMOTE_PORT, $UNIQUE_ID, and everything else that may add entropy");
321 $localfilename = $localfilename;
322
323 // Put the file in a better place
324 error_reporting(0); // Rename will produce error output if it fails
325 if (!rename($attachfile, $attachment_dir.$localfilename)) {
326 if (!copy($attachfile, $attachment_dir.$localfilename)) {
327 plain_error_message(_("Could not move/copy file. File not attached"));
328 $failed = true;
329 }
330 }
331 // If it still exists, PHP will remove the original file
332
333 if (!$failed) {
334 // Write information about the file
335 $fp = fopen ($attachment_dir.$localfilename.".info", "w");
336 fputs ($fp, "$attachfile_type\n$attachfile_name\n");
337 fclose ($fp);
338
339 $attachments[$localfilename] = $attachfile_name;
340 }
341
342 showInputForm();
343 } else if (isset($do_delete)) {
344 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
345 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
346 displayPageHeader($color, "None");
347
348 while (list($key, $localname) = each($delete)) {
349 array_splice ($attachments, $key, 1);
350 unlink ($attachment_dir.$localname);
351 unlink ($attachment_dir.$localname.".info");
352 }
353
354 showInputForm();
355 } else {
356 echo "<HTML><BODY TEXT=\"$color[8]\" BGCOLOR=\"$color[4]\" LINK=\"$color[7]\" VLINK=\"$color[7]\" ALINK=\"$color[7]\">\n";
357 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
358 displayPageHeader($color, "None");
359
360 $newmail = true;
361 newMail();
362 showInputForm();
363 }
364 ?>