6 * Copyright (c) 1999-2004 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.
12 * @package squirrelmail
16 * Path for SquirrelMail required files.
22 * FIX ME REMOVE ME FIX ME REMOVE ME I DON'T DESERVE TO EXIST
24 * Integrate this is a clean manner in right_main.php and rename right_main to
25 * messageslist or whatever
29 define('SM_PATH','../');
31 /* SquirrelMail required files. */
32 require_once(SM_PATH
. 'include/validate.php');
33 require_once(SM_PATH
. 'functions/global.php');
34 require_once(SM_PATH
. 'functions/display_messages.php');
35 require_once(SM_PATH
. 'functions/imap.php');
36 require_once(SM_PATH
. 'functions/html.php');
38 global $compose_new_win;
40 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION
) ) {
44 function attachSelectedMessages($msg, $imapConnection) {
45 global $username, $attachment_dir, $startMessage,
46 $data_dir, $composesession,
47 $msgs, $show_num, $compose_messages;
49 if (!isset($compose_messages)) {
50 $compose_messages = array();
51 sqsession_register($compose_messages,'compose_messages');
54 if (!$composesession) {
56 sqsession_register($composesession,'composesession');
59 sqsession_register($composesession,'composesession');
62 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
64 $composeMessage = new Message();
65 $rfc822_header = new Rfc822Header();
66 $composeMessage->rfc822_header
= $rfc822_header;
67 $composeMessage->reply_rfc822_header
= '';
69 foreach($msg as $id) {
70 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
72 if ($response == 'OK') {
73 // fetch the subject for the message with $id from msgs.
74 // is there a more efficient way to do this?
75 foreach($msgs as $k => $vals) {
76 if($vals['ID'] == $id) {
77 $subject = $msgs[$k]['SUBJECT'];
84 $body = implode('', $body_a);
87 $localfilename = GenerateRandomString(32, 'FILE', 7);
88 $full_localfilename = "$hashed_attachment_dir/$localfilename";
90 $fp = fopen( $full_localfilename, 'wb');
93 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
98 $compose_messages[$composesession] = $composeMessage;
99 sqsession_register($compose_messages,'compose_messages');
100 session_write_close();
101 return $composesession;
105 sqgetGlobalVar('key', $key, SQ_COOKIE
);
106 sqgetGlobalVar('username', $username, SQ_SESSION
);
107 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
108 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
109 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
111 sqgetGlobalVar('mailbox', $mailbox);
112 sqgetGlobalVar('startMessage', $startMessage);
113 sqgetGlobalVar('msg', $msg);
115 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
116 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
117 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
119 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
120 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
121 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
122 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
123 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
124 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
125 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
126 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST
);
127 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST
);
128 sqgetGlobalVar('attache', $attache, SQ_POST
);
129 sqgetGlobalVar('location', $location, SQ_POST
);
130 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
131 sqgetGlobalVar('dmn', $is_dmn, SQ_POST
);
135 /* end of get globals */
137 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
138 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
141 global $allow_thread_sort, $auto_expunge;
143 if ($allow_thread_sort && getPref($data_dir, $username, "thread_$mailbox",0)) {
144 $aMailbox['SORT_METHOD'] = 'THREAD';
145 } else if ($allow_server_sort) {
146 $aMailbox['SORT_METHOD'] = 'SERVER';
148 $aMailbox['SORT_METHOD'] = 'SQUIRREL';
150 sqgetGlobalVar('aLastSelectedMailbox',$aMailbox,SQ_SESSION
);
151 sqgetGlobalVar('server_sort_array', $server_sort_array, SQ_SESSION
);
152 $aMailbox['UIDSET'] = $server_sort_array;
153 $aMailbox['SORT'] = $sort;
154 $aMailbox['NAME'] = $mailbox;
155 $aMailbox['EXISTS'] = $mbx_response['EXISTS'];
156 $aMailbox['AUTO_EXPUNGE'] = $auto_expunge;
157 $aMailbox['MSG_HEADERS'] = $msgs;
159 $location = set_url_var($location,'composenew',0,false);
160 $location = set_url_var($location,'composesession',0,false);
161 $location = set_url_var($location,'session',0,false);
163 /* remember changes to mailbox setting */
164 if (!isset($lastTargetMailbox)) {
165 $lastTargetMailbox = 'INBOX';
167 if ($targetMailbox != $lastTargetMailbox) {
168 $lastTargetMailbox = $targetMailbox;
169 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
174 do_hook('move_before_move');
177 Move msg list sorting up here, as it is used several times,
178 makes it more efficient to do it in one place for the code
181 if (isset($msg) && is_array($msg)) {
182 foreach( $msg as $key=>$uid ) {
183 // using foreach removes the risk of infinite loops that was there //
187 $num_ids = count($id);
189 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
190 if(isset($expungeButton)) {
191 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
193 } elseif(isset($undeleteButton)) {
194 // undelete messages if user isn't using move_to_trash or auto_expunge
195 // Removes \Deleted flag from selected messages
197 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
201 } elseif (!isset($moveButton)) {
203 if (!isset($attache)) {
204 if (isset($markRead)) {
205 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
206 } else if (isset($markUnread)) {
207 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
208 } else if (isset($markFlagged)) {
209 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', true, true);
210 } else if (isset($markUnflagged)) {
211 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', false, true);
212 } else { // Delete messages
213 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
214 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
216 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
222 $composesession = attachSelectedMessages($id, $imapConnection);
223 $location = set_url_var($location, 'session', $composesession, false);
224 if ($compose_new_win) {
225 $location = set_url_var($location, 'composenew', 1, false);
227 $location = str_replace('search.php','compose.php',$location);
228 $location = str_replace('right_main.php','compose.php',$location);
234 } else { // Move messages
235 if ( $num_ids > 0 ) {
236 if ( $is_dmn && $num_ids == 1 ) {
237 sqimap_msgs_list_move($imapConnection,$id[0],$targetMailbox);
238 $num_ids = sqimap_mailbox_expunge_dmn($imapConnection,$aMailbox, $id[0]);
240 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
242 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
250 if($change) { // Change the startMessage number if the mailbox was changed
251 if (($startMessage+
$num_ids-1) >= $mbx_response['EXISTS']) {
252 if ($startMessage > $show_num) {
253 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
255 $location = set_url_var($location,'startMessage',1,false);
259 // Log out this session
260 sqimap_logout($imapConnection);
262 displayPageHeader($color, $mailbox);
263 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
265 header("Location: $location");
269 function handleMessageListForm($imapConnection,&$aMailbox) {
270 /* incoming formdata */
271 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
272 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
273 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
274 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
275 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
276 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
277 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
278 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST
);
279 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST
);
280 sqgetGlobalVar('attache', $attache, SQ_POST
);
281 sqgetGlobalVar('location', $location, SQ_POST
);
282 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
283 sqgetGlobalVar('msg', $msg, SQ_POST
);
286 /* retrieve the check boxes */
288 if (isset($msg) && is_array($msg)) {
289 foreach( $msg as $key=>$iUid ) {
290 // using foreach removes the risk of infinite loops that was there //
294 $num_ids = count($id);
296 if (count($num_ids) && !isset($expungeButton)) {
297 /* handle submit buttons */
299 $sButton = (isset($expungeButton)) ?
'expunge' : $sButton;
300 $sButton = (isset($attache)) ?
'attache' : $sButton;
301 $sButton = (isset($moveButton)) ?
'move' : $sButton;
302 $sButton = (isset($copyButton)) ?
'copy' : $sButton;
303 $sButton = (isset($markDelete)) ?
'setDeleted' : $sButton;
304 $sButton = (isset($markUndelete)) ?
'unsetDeleted' : $sButton;
305 $sButton = (isset($markSeen)) ?
'setSeen' : $sButton;
306 $sButton = (isset($markUnseen)) ?
'unsetSeen' : $sButton;
307 $sButton = (isset($markFlagged)) ?
'setFlagged' : $sButton;
308 $sButton = (isset($markUnflagged)) ?
'unsetFlagged' : $sButton;
310 $aUpdatedMsgs = false;
314 // What kind of hook is this, can it be removed?
315 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
316 $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
326 $sFlag = (substr($sButton,0,3) == 'set') ?
'\\'.substr($sButton,3) : '\\'.substr($sButton,5);
327 $bSet = (substr($sButton,0,3) == 'set') ?
true : false;
328 $aUpdatedMsgs = sqimap_toggle_flag($imapConnection, $aUid, $sFlag, $bSet, true);
331 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aId,$targetMailbox);
335 $composesession = attachSelectedMessages($id, $imapConnection);
336 // dirty hack, add info to $aMailbox
337 $aMailbox['FORWARD_SESSION'] = $composesession;
342 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
343 if (isset($aMsg['FLAGS'])) {
344 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
347 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
348 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
350 if (count($aUpdateMsgs != $iExpungedMessages)) {
351 // there are more messages deleted permanently then we expected
352 // invalidate the cache
353 $aMailbox['UIDSET'] = false;
354 $aMailbox['MSG_HEADERS'] = false;
356 // remove expunged messages from cache
357 $aUidSet = $aMailbox['UIDSET'];
359 foreach ($aUpdatedMsgs as $iUid => $aValue) {
360 if (isset($aValue['FLAGS']['\\deleted']) && $aValue['FLAGS']['\\deleted']) {
364 if (count($aDeleted)) {
365 // create a UID => array index temp array
366 $aUidSetDummy = array_flip($aUidSet);
367 foreach ($aDeleted as $iUid) {
368 unset($aUidSetDummy[$iUid]);
370 $aUidSet = array_keys($aUidSetDummy);
371 $aMailbox['UIDSET'] = $aUidSet;
372 // update EXISTS info
373 $aMailbox['EXISTS'] -= $iExpungedMessages;
376 // Change the startMessage number if the mailbox was changed
377 if (($aMailbox['PAGEOFFSET']+
$iExpungedMessages-1) >= $aMailbox['EXISTS']) {
378 $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
379 $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
384 if (isset($expungeButton)) {
385 // on expunge we do not know which messages will be deleted
386 // so it's useless to try to sync the cache
388 // Close the mailbox so we do not need to parse the untagged expunge responses
389 sqimap_run_command($imapConnection,'CLOSE',false,$result,$message);
390 $aMbxResponse = sqimap_select($imapConnection,$aMailbox['NAME'];
391 // update the $aMailbox array
392 $aMailbox['EXISTS'] = $aMbxResponse['EXISTS'];
393 $aMailbox['UIDSET'] = false;
395 $sError = _("No messages were selected.");