802ddba8cf80572f7b36ddd3987fe37fb8c1c68c
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, $data_dir, $composesession, $lastTargetMailbox;
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;
81 while ($j < count($msg)) {
82 if (isset($msg[$i])) {
84 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage);
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);
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 return $composesession;
125 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
126 sqimap_mailbox_select($imapConnection, $mailbox);
128 /* remember changes to mailbox setting */
129 if (!isset($lastTargetMailbox)) {
130 $lastTargetMailbox = 'INBOX';
132 if ($targetMailbox != $lastTargetMailbox) {
133 $lastTargetMailbox = $targetMailbox;
134 session_register('lastTargetMailbox');
137 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
138 if(isset($expungeButton)) {
139 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
140 $location = get_location();
141 if (isset($where) && isset($what)) {
142 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
144 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
147 } elseif(isset($undeleteButton)) {
148 // undelete messages if user isn't using move_to_trash or auto_expunge
150 if (is_array($msg) == 1) {
151 // Removes \Deleted flag from selected messages
155 // If they have selected nothing msg is size one still, but will be an infinite
156 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
157 while ($j < count($msg)) {
159 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted");
164 $location = get_location();
167 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
169 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
171 displayPageHeader($color, $mailbox);
172 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
174 } elseif (!isset($moveButton)) {
175 // If the delete button was pressed, the moveButton variable will not be set.
176 if (is_array($msg) == 1) {
177 // Marks the selected messages as 'Deleted'
181 // If they have selected nothing msg is size one still, but will be an infinite
182 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
183 while ($j < count($msg)) {
184 if (isset($msg[$i])) {
185 if (isset($markRead)) {
186 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen");
187 } else if (isset($markUnread)) {
188 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen");
189 } else if (isset($attache)) {
192 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
199 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
201 $location = get_location();
202 if (isset($where) && isset($what)) {
203 if (isset($attache)) {
204 $composesession = attachSelectedMessages($msg, $imapConnection);
205 if ($compose_new_win == '1') {
206 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where)."&composenew=1&session=$composesession&attachedmessages=true");
208 header ("Location: $location/compose.php?startMessage=$startMessage&mailbox=". urlencode($mailbox)."&session=$composesession&attachedmessages=true".$wherewhat);
211 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
213 } elseif(isset($attache)) {
214 $composesession = attachSelectedMessages($msg, $imapConnection);
215 if ($compose_new_win == '1') {
216 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)."&composenew=1&session=$composesession&attachedmessages=true");
218 header ("Location: $location/compose.php?startMessage=$startMessage&mailbox=". urlencode($mailbox)."&session=$composesession&attachedmessages=true");
221 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
224 displayPageHeader($color, $mailbox);
225 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
227 } else { // Move messages
228 // lets check to see if they selected any messages
229 if (is_array($msg) == 1) {
233 // If they have selected nothing msg is size one still, but will be an infinite
234 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
235 while ($j < count($msg)) {
236 if (isset($msg[$i])) {
237 /** check if they would like to move it to the trash folder or not */
238 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
239 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted");
244 if ($auto_expunge == true)
245 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
247 $location = get_location();
248 if (isset($where) && isset($what))
249 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
251 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
253 displayPageHeader($color, $mailbox);
254 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
258 // Log out this session
259 sqimap_logout($imapConnection);