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 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
17 /* SquirrelMail required files. */
18 require_once(SM_PATH
. 'include/validate.php');
19 require_once(SM_PATH
. 'functions/display_messages.php');
20 require_once(SM_PATH
. 'functions/imap.php');
21 require_once(SM_PATH
. 'functions/html.php');
23 global $compose_new_win;
25 function putSelectedMessagesIntoString($msg) {
29 // If they have selected nothing msg is size one still, but will
30 // be an infinite loop because we never increment j. so check to
31 // see if msg[0] is set or not to fix this.
32 while (($j < count($msg)) && ($msg[0])) {
34 if ($firstLoop != true) {
35 $selectedMessages .= "&";
39 $selectedMessages .= "selMsg[$j]=$msg[$i]";
46 function attachSelectedMessages($msg, $imapConnection) {
47 global $mailbox, $username, $attachment_dir, $attachments, $identity,
48 $data_dir, $composesession, $lastTargetMailbox, $uid_support,
49 $msgs, $startMessage, $show_num, $thread_sort_messages,
52 if (!isset($attachments)) {
53 $attachments = array();
54 session_register('attachments');
57 if (!isset($composesession) ) {
59 session_register('composesession');
64 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
66 $rem_attachments = array();
67 foreach ($attachments as $info) {
68 if ($info['session'] == $composesession) {
69 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
70 if (file_exists($attached_file)) {
71 unlink($attached_file);
74 $rem_attachments[] = $info;
78 $attachments = $rem_attachments;
80 if ($thread_sort_messages ||
$allow_server_sort) {
83 $start_index = ($startMessage-1) * $show_num;
88 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
89 while ($j < count($msg)) {
90 if (isset($msg[$i])) {
92 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
93 if ($response = 'OK') {
94 $k = $i +
$start_index;
95 $subject = $msgs[$k]['SUBJECT'];
98 $body = implode('', $body_a);
101 $localfilename = GenerateRandomString(32, 'FILE', 7);
102 $full_localfilename = "$hashed_attachment_dir/$localfilename";
104 $fp = fopen( $full_localfilename, 'wb');
107 $newAttachment = array();
108 $newAttachment['localfilename'] = $localfilename;
109 $newAttachment['type'] = "message/rfc822";
110 $newAttachment['remotefilename'] = $subject.'.eml';
111 $newAttachment['session'] = $composesession;
112 $attachments[] = $newAttachment;
119 setPref($data_dir, $username, 'attachments', serialize($attachments));
120 return $composesession;
124 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
125 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
127 $location = set_url_var($location,'composenew');
128 $location = set_url_var($location,'composesession');
129 $location = set_url_var($location,'session');
131 /* remember changes to mailbox setting */
132 if (!isset($lastTargetMailbox)) {
133 $lastTargetMailbox = 'INBOX';
135 if ($targetMailbox != $lastTargetMailbox) {
136 $lastTargetMailbox = $targetMailbox;
137 session_register('lastTargetMailbox');
140 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
141 if(isset($expungeButton)) {
142 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
143 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
144 if ($startMessage > $show_num) {
145 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
147 $location = set_url_var($location,'startMessage',1);
150 header("Location: $location");
151 } elseif(isset($undeleteButton)) {
152 // undelete messages if user isn't using move_to_trash or auto_expunge
153 if (is_array($msg) == 1) {
154 // Removes \Deleted flag from selected messages
157 // If they have selected nothing msg is size one still, but will be an infinite
158 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
159 while ($j < count($msg)) {
161 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
166 header ("Location: $location");
168 displayPageHeader($color, $mailbox);
169 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
171 } elseif (!isset($moveButton)) {
172 // If the delete button was pressed, the moveButton variable will not be set.
173 if (is_array($msg) == 1) {
174 // Marks the selected messages as 'Deleted'
177 // If they have selected nothing msg is size one still, but will be an infinite
178 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
179 while ($j < count($msg)) {
180 if (isset($msg[$i])) {
181 if (isset($markRead)) {
182 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
183 } else if (isset($markUnread)) {
184 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
185 } else if (isset($attache)) {
188 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
195 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
199 if (isset($attache)) {
200 $composesession = attachSelectedMessages($msg, $imapConnection);
201 if ($compose_new_win) {
202 header ("Location: $location&composenew=1&session=$composesession");
204 $location = str_replace('search.php','compose.php',$location);
205 $location = str_replace('right_main.php','compose.php',$location);
206 header ("Location: $location&session=$composesession");
209 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
210 if ($startMessage > $show_num) {
211 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
213 $location = set_url_var($location,'startMessage',1);
216 echo $location . ' ' .$show_num. ' '.$cnt;
217 header ("Location: $location");
220 displayPageHeader($color, $mailbox);
221 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
223 } else { // Move messages
224 // lets check to see if they selected any messages
225 if (is_array($msg) == 1) {
228 // If they have selected nothing msg is size one still, but will be an infinite
229 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
232 if (isset($msg[$i])) {
233 /** check if they would like to move it to the trash folder or not */
234 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
235 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
241 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
246 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
247 if ($startMessage > $show_num) {
248 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
250 $location = set_url_var($location,'startMessage',1);
253 header ("Location: $location");
255 displayPageHeader($color, $mailbox);
256 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
259 // Log out this session
260 sqimap_logout($imapConnection);