2f632896820505cb28569d9833aac09fde66c0dd
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Enables message moving between folders on the IMAP server.
14 require_once('../src/validate.php');
15 require_once('../functions/display_messages.php');
16 require_once('../functions/imap.php');
18 global $compose_new_win;
20 function putSelectedMessagesIntoString($msg) {
25 // If they have selected nothing msg is size one still, but will
26 // be an infinite loop because we never increment j. so check to
27 // see if msg[0] is set or not to fix this.
28 while (($j < count($msg)) && ($msg[0])) {
30 if ($firstLoop != true) {
31 $selectedMessages .= "&";
36 $selectedMessages .= "selMsg[$j]=$msg[$i]";
44 function attachSelectedMessages($msg, $imapConnection) {
46 global $mailbox, $username, $attachment_dir, $attachments, $identity,
47 $data_dir, $composesession, $lastTargetMailbox, $uid_support;
49 if (!isset($attachments)) {
50 $attachments = array();
51 session_register('attachments');
54 if (!isset($composesession) ) {
56 session_register('composesession');
61 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
63 $rem_attachments = array();
64 foreach ($attachments as $info) {
65 if ($info['session'] == $composesession) {
66 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
67 if (file_exists($attached_file)) {
68 unlink($attached_file);
71 $rem_attachments[] = $info;
75 $attachments = $rem_attachments;
80 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
81 while ($j < count($msg)) {
82 if (isset($msg[$i])) {
84 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
85 if ($response = 'OK') {
86 // get subject so we can set the remotefilename
87 $read = sqimap_run_command ($imapConnection, "FETCH $id BODY.PEEK[HEADER.FIELDS (Subject)]", true, $response, $readmessage, $uid_support);
88 $subject = substr($read[1], strpos($read[1], ' '));
89 $subject = trim($subject);
91 if (isset($subject) && $subject != '') {
92 $subject = htmlentities($subject);
94 $subject = _("<No subject>");
95 $subject = htmlentities($subject);
99 $body = implode('', $body_a);
102 $localfilename = GenerateRandomString(32, 'FILE', 7);
103 $full_localfilename = "$hashed_attachment_dir/$localfilename";
105 $fp = fopen( $full_localfilename, 'w');
109 $newAttachment = array();
110 $newAttachment['localfilename'] = $localfilename;
111 $newAttachment['type'] = "message/rfc822";
112 $newAttachment['remotefilename'] = "$subject".".eml";
113 $newAttachment['session'] = $composesession;
114 $attachments[] = $newAttachment;
122 setPref($data_dir, $username, 'attachments', serialize($attachments));
123 return $composesession;
126 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
127 sqimap_mailbox_select($imapConnection, $mailbox);
129 /* remember changes to mailbox setting */
130 if (!isset($lastTargetMailbox)) {
131 $lastTargetMailbox = 'INBOX';
133 if ($targetMailbox != $lastTargetMailbox) {
134 $lastTargetMailbox = $targetMailbox;
135 session_register('lastTargetMailbox');
138 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
139 if(isset($expungeButton)) {
140 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
141 $location = get_location();
142 if (isset($where) && isset($what)) {
143 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
145 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
148 } elseif(isset($undeleteButton)) {
149 // undelete messages if user isn't using move_to_trash or auto_expunge
151 if (is_array($msg) == 1) {
152 // Removes \Deleted flag from selected messages
156 // If they have selected nothing msg is size one still, but will be an infinite
157 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
158 while ($j < count($msg)) {
160 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted");
165 $location = get_location();
168 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
170 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
172 displayPageHeader($color, $mailbox);
173 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
175 } elseif (!isset($moveButton)) {
176 // If the delete button was pressed, the moveButton variable will not be set.
177 if (is_array($msg) == 1) {
178 // Marks the selected messages as 'Deleted'
182 // If they have selected nothing msg is size one still, but will be an infinite
183 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
184 while ($j < count($msg)) {
185 if (isset($msg[$i])) {
186 if (isset($markRead)) {
187 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen");
188 } else if (isset($markUnread)) {
189 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen");
190 } else if (isset($attache)) {
193 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
200 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
202 $location = get_location();
203 if (isset($where) && isset($what)) {
204 if (isset($attache)) {
205 $composesession = attachSelectedMessages($msg, $imapConnection);
206 if ($compose_new_win == '1') {
207 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where)."&composenew=1&session=$composesession&attachedmessages=true");
209 header ("Location: $location/compose.php?startMessage=$startMessage&mailbox=". urlencode($mailbox)."&session=$composesession&attachedmessages=true".$wherewhat);
212 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
214 } elseif(isset($attache)) {
215 $composesession = attachSelectedMessages($msg, $imapConnection);
216 if ($compose_new_win == '1') {
217 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)."&composenew=1&session=$composesession&attachedmessages=true");
219 header ("Location: $location/compose.php?startMessage=$startMessage&mailbox=". urlencode($mailbox)."&session=$composesession&attachedmessages=true");
222 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
225 displayPageHeader($color, $mailbox);
226 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
228 } else { // Move messages
229 // lets check to see if they selected any messages
230 if (is_array($msg) == 1) {
234 // If they have selected nothing msg is size one still, but will be an infinite
235 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
236 while ($j < count($msg)) {
237 if (isset($msg[$i])) {
238 /** check if they would like to move it to the trash folder or not */
239 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
240 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted");
245 if ($auto_expunge == true)
246 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
248 $location = get_location();
249 if (isset($where) && isset($what))
250 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
252 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
254 displayPageHeader($color, $mailbox);
255 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
259 // Log out this session
260 sqimap_logout($imapConnection);