Rewrite of internal message caching
[squirrelmail.git] / src / move_messages.php
1 <?php
2 exit;
3 /**
4 * move_messages.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
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 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * Path for SquirrelMail required files.
17 * @ignore
18 */
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
29 define('SM_PATH','../');
30
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');
37
38 global $compose_new_win;
39
40 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
41 $composesession = 0;
42 }
43
44 function attachSelectedMessages($msg, $imapConnection) {
45 global $username, $attachment_dir, $startMessage,
46 $data_dir, $composesession,
47 $msgs, $show_num, $compose_messages;
48
49 if (!isset($compose_messages)) {
50 $compose_messages = array();
51 sqsession_register($compose_messages,'compose_messages');
52 }
53
54 if (!$composesession) {
55 $composesession = 1;
56 sqsession_register($composesession,'composesession');
57 } else {
58 $composesession++;
59 sqsession_register($composesession,'composesession');
60 }
61
62 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
63
64 $composeMessage = new Message();
65 $rfc822_header = new Rfc822Header();
66 $composeMessage->rfc822_header = $rfc822_header;
67 $composeMessage->reply_rfc822_header = '';
68
69 foreach($msg as $id) {
70 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
71
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'];
78 break;
79 }
80 }
81
82 array_shift($body_a);
83 array_pop($body_a);
84 $body = implode('', $body_a);
85 $body .= "\r\n";
86
87 $localfilename = GenerateRandomString(32, 'FILE', 7);
88 $full_localfilename = "$hashed_attachment_dir/$localfilename";
89
90 $fp = fopen( $full_localfilename, 'wb');
91 fwrite ($fp, $body);
92 fclose($fp);
93 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
94 $full_localfilename);
95 }
96 }
97
98 $compose_messages[$composesession] = $composeMessage;
99 sqsession_register($compose_messages,'compose_messages');
100 session_write_close();
101 return $composesession;
102 }
103
104 /* get globals */
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);
110
111 sqgetGlobalVar('mailbox', $mailbox);
112 sqgetGlobalVar('startMessage', $startMessage);
113 sqgetGlobalVar('msg', $msg);
114
115 sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
116 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
117 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
118
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);
132
133
134
135 /* end of get globals */
136
137 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
138 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
139
140
141 global $allow_thread_sort, $auto_expunge;
142
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';
147 } else {
148 $aMailbox['SORT_METHOD'] = 'SQUIRREL';
149 }
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;
158
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);
162
163 /* remember changes to mailbox setting */
164 if (!isset($lastTargetMailbox)) {
165 $lastTargetMailbox = 'INBOX';
166 }
167 if ($targetMailbox != $lastTargetMailbox) {
168 $lastTargetMailbox = $targetMailbox;
169 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
170 }
171 $exception = false;
172 $change = false;
173
174 do_hook('move_before_move');
175
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();
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 //
184 $id[] = $uid;
185 }
186 }
187 $num_ids = count($id);
188
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);
192 $change = 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
196 if ($num_ids) {
197 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
198 } else {
199 $exception = true;
200 }
201 } elseif (!isset($moveButton)) {
202 if ($num_ids) {
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);
215 if ($auto_expunge) {
216 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
217 }
218 $change = true;
219 }
220 }
221 } else {
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 }
230 }
231 } else {
232 $exception = true;
233 }
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]);
239 } else {
240 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
241 if ($auto_expunge) {
242 $num_ids = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
243 }
244 }
245 $change = true;
246 } else {
247 $exception = true;
248 }
249 }
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);
254 } else {
255 $location = set_url_var($location,'startMessage',1,false);
256 }
257 }
258 }
259 // Log out this session
260 sqimap_logout($imapConnection);
261 if ($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 }
268
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);
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 }
399 ?>
400 </BODY></HTML>