Rewrite of internal message caching
[squirrelmail.git] / src / move_messages.php
CommitLineData
59177427 1<?php
324ac3c5 2exit;
35586184 3/**
4 * move_messages.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Enables message moving between folders on the IMAP server.
10 *
30967a1e 11 * @version $Id$
8f6f9ba5 12 * @package squirrelmail
35586184 13 */
ef870322 14
30967a1e 15/**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
f7bc1576 19
20
21/**
22 * FIX ME REMOVE ME FIX ME REMOVE ME I DON'T DESERVE TO EXIST
23 *
24 * Integrate this is a clean manner in right_main.php and rename right_main to
25 * messageslist or whatever
26 **/
27
28
86725763 29define('SM_PATH','../');
30
31/* SquirrelMail required files. */
08185f2a 32require_once(SM_PATH . 'include/validate.php');
8650e9e1 33require_once(SM_PATH . 'functions/global.php');
86725763 34require_once(SM_PATH . 'functions/display_messages.php');
35require_once(SM_PATH . 'functions/imap.php');
36require_once(SM_PATH . 'functions/html.php');
37
acc28bac 38global $compose_new_win;
39
1e12d1ff 40if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
2cb178e3 41 $composesession = 0;
1e12d1ff 42}
43
acc28bac 44function attachSelectedMessages($msg, $imapConnection) {
133b9a0e 45 global $username, $attachment_dir, $startMessage,
eeb6e74a 46 $data_dir, $composesession,
2cb178e3 47 $msgs, $show_num, $compose_messages;
a2a30ee7 48
a2a30ee7 49 if (!isset($compose_messages)) {
11fd4a7b 50 $compose_messages = array();
2cb178e3 51 sqsession_register($compose_messages,'compose_messages');
acc28bac 52 }
53
07abb0f3 54 if (!$composesession) {
11fd4a7b 55 $composesession = 1;
2cb178e3 56 sqsession_register($composesession,'composesession');
acc28bac 57 } else {
11fd4a7b 58 $composesession++;
59 sqsession_register($composesession,'composesession');
acc28bac 60 }
61
21f3c131 62 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a2a30ee7 63
64 $composeMessage = new Message();
65 $rfc822_header = new Rfc822Header();
66 $composeMessage->rfc822_header = $rfc822_header;
67 $composeMessage->reply_rfc822_header = '';
68
2cb178e3 69 foreach($msg as $id) {
6201339c 70 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
11fd4a7b 71
2cb178e3 72 if ($response == 'OK') {
2cb178e3 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'];
78 break;
a2388eb7 79 }
2cb178e3 80 }
a2388eb7 81
2cb178e3 82 array_shift($body_a);
c2517a3b 83 array_pop($body_a);
2cb178e3 84 $body = implode('', $body_a);
85 $body .= "\r\n";
a2388eb7 86
2cb178e3 87 $localfilename = GenerateRandomString(32, 'FILE', 7);
88 $full_localfilename = "$hashed_attachment_dir/$localfilename";
a2388eb7 89
2cb178e3 90 $fp = fopen( $full_localfilename, 'wb');
91 fwrite ($fp, $body);
92 fclose($fp);
745fca27 93 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
2cb178e3 94 $full_localfilename);
a2388eb7 95 }
acc28bac 96 }
2cb178e3 97
a2a30ee7 98 $compose_messages[$composesession] = $composeMessage;
87d5f199 99 sqsession_register($compose_messages,'compose_messages');
100 session_write_close();
acc28bac 101 return $composesession;
102}
103
9837b0a5 104/* get globals */
1e12d1ff 105sqgetGlobalVar('key', $key, SQ_COOKIE);
106sqgetGlobalVar('username', $username, SQ_SESSION);
107sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
108sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
109sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
110
111sqgetGlobalVar('mailbox', $mailbox);
112sqgetGlobalVar('startMessage', $startMessage);
113sqgetGlobalVar('msg', $msg);
114
115sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
116sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
117sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
118
119sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
120sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
121sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
122sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
123sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
124sqgetGlobalVar('markRead', $markRead, SQ_POST);
125sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
057dbe18 126sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST);
127sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST);
1e12d1ff 128sqgetGlobalVar('attache', $attache, SQ_POST);
129sqgetGlobalVar('location', $location, SQ_POST);
6c540963 130sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST);
11026a30 131sqgetGlobalVar('dmn', $is_dmn, SQ_POST);
9837b0a5 132
f7bc1576 133
134
9837b0a5 135/* end of get globals */
136
65c3ec94 137$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 138$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 139
f7bc1576 140
141global $allow_thread_sort, $auto_expunge;
142
143if ($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';
147} else {
148 $aMailbox['SORT_METHOD'] = 'SQUIRREL';
149}
150sqgetGlobalVar('aLastSelectedMailbox',$aMailbox,SQ_SESSION);
151sqgetGlobalVar('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;
158
87d5f199 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);
8c33aaf7 162
cd885078 163/* remember changes to mailbox setting */
164if (!isset($lastTargetMailbox)) {
165 $lastTargetMailbox = 'INBOX';
166}
167if ($targetMailbox != $lastTargetMailbox) {
168 $lastTargetMailbox = $targetMailbox;
9837b0a5 169 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 170}
87d5f199 171$exception = false;
eeb6e74a 172$change = false;
00f9181e 173
174do_hook('move_before_move');
175
11fd4a7b 176/*
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
179*/
180$id = array();
181if (isset($msg) && is_array($msg)) {
182 foreach( $msg as $key=>$uid ) {
183 // using foreach removes the risk of infinite loops that was there //
184 $id[] = $uid;
185 }
186}
eeb6e74a 187$num_ids = count($id);
11fd4a7b 188
65c3ec94 189// expunge-on-demand if user isn't using move_to_trash or auto_expunge
190if(isset($expungeButton)) {
eeb6e74a 191 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
192 $change = true;
65c3ec94 193} elseif(isset($undeleteButton)) {
194 // undelete messages if user isn't using move_to_trash or auto_expunge
11fd4a7b 195 // Removes \Deleted flag from selected messages
eeb6e74a 196 if ($num_ids) {
11fd4a7b 197 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
65c3ec94 198 } else {
11fd4a7b 199 $exception = true;
65c3ec94 200 }
201} elseif (!isset($moveButton)) {
eeb6e74a 202 if ($num_ids) {
11fd4a7b 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);
fd181f53 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);
eeb6e74a 212 } else { // Delete messages
fd181f53 213 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
6c540963 214 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
d3714d3a 215 if ($auto_expunge) {
eeb6e74a 216 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
d3714d3a 217 }
eeb6e74a 218 $change = true;
11fd4a7b 219 }
2d7d3c2a 220 }
eeb6e74a 221 } else {
11fd4a7b 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);
226 } else {
227 $location = str_replace('search.php','compose.php',$location);
228 $location = str_replace('right_main.php','compose.php',$location);
229 }
11fd4a7b 230 }
65c3ec94 231 } else {
11fd4a7b 232 $exception = true;
65c3ec94 233 }
234} else { // Move messages
11026a30 235 if ( $num_ids > 0 ) {
eeb6e74a 236 if ( $is_dmn && $num_ids == 1 ) {
11026a30 237 sqimap_msgs_list_move($imapConnection,$id[0],$targetMailbox);
f7bc1576 238 $num_ids = sqimap_mailbox_expunge_dmn($imapConnection,$aMailbox, $id[0]);
11fd4a7b 239 } else {
11026a30 240 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
241 if ($auto_expunge) {
eeb6e74a 242 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
11fd4a7b 243 }
244 }
eeb6e74a 245 $change = true;
65c3ec94 246 } else {
11fd4a7b 247 $exception = true;
65c3ec94 248 }
249}
eeb6e74a 250if($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);
254 } else {
255 $location = set_url_var($location,'startMessage',1,false);
256 }
257 }
258}
65c3ec94 259// Log out this session
260sqimap_logout($imapConnection);
87d5f199 261if ($exception) {
262 displayPageHeader($color, $mailbox);
263 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
264} else {
265 header("Location: $location");
266 exit;
267}
324ac3c5 268
269function 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);
284
285 $sError = '';
286 /* retrieve the check boxes */
287 $aUid = array();
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 //
291 $aUid[] = $iUid;
292 }
293 }
294 $num_ids = count($id);
295
296 if (count($num_ids) && !isset($expungeButton)) {
297 /* handle submit buttons */
298 $sButton = '';
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;
309
310 $aUpdatedMsgs = false;
311 $bExpunge = false;
312 switch ($sButton) {
313 case 'setDeleted':
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);
317 $bExpunge = true;
318 }
319 break;
320 case 'unsetDeleted':
321 case 'setSeen':
322 case 'unsetSeen':
323 case 'setFlagged':
324 case 'unsetFlagged':
325 // get flag
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);
329 break;
330 case 'move':
331 $aUpdatedMsgs = sqimap_msgs_list_move($imapConnection,$aId,$targetMailbox);
332 $bExpunge = true;
333 break;
334 case 'attache':
335 $composesession = attachSelectedMessages($id, $imapConnection);
336 // dirty hack, add info to $aMailbox
337 $aMailbox['FORWARD_SESSION'] = $composesession;
338 break;
339 }
340
341 if ($aUpdatedMsgs) {
342 foreach ($aUpdatedMsgs as $iUid => $aMsg) {
343 if (isset($aMsg['FLAGS'])) {
344 $aMailbox['MSG_HEADERS'][$iUid]['FLAGS'] = $aMsg['FLAGS'];
345 }
346 }
347 if ($bExpunge && $aMailbox['AUTO_EXPUNGE'] &&
348 $iExpungedMessages = sqimap_mailbox_expunge($imapConnection, $aMailbox['NAME'], true))
349 {
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;
355 } else {
356 // remove expunged messages from cache
357 $aUidSet = $aMailbox['UIDSET'];
358 $aDeleted = array();
359 foreach ($aUpdatedMsgs as $iUid => $aValue) {
360 if (isset($aValue['FLAGS']['\\deleted']) && $aValue['FLAGS']['\\deleted']) {
361 $aDeleted[] = $iUid;
362 }
363 }
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]);
369 }
370 $aUidSet = array_keys($aUidSetDummy);
371 $aMailbox['UIDSET'] = $aUidSet;
372 // update EXISTS info
373 $aMailbox['EXISTS'] -= $iExpungedMessages;
374 }
375 }
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;
380 }
381 }
382 }
383 } else {
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
387
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;
394 } else {
395 $sError = _("No messages were selected.");
396 }
397 }
398}
dcc1cc82 399?>
400</BODY></HTML>