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