Lots of changes for variable initialization - clean up, really,
[squirrelmail.git] / src / move_messages.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * move_messages.php
5 *
76911253 6 * Copyright (c) 1999-2003 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 *
11 * $Id$
12 */
ef870322 13
86725763 14/* Path for SquirrelMail required files. */
15define('SM_PATH','../');
16
17/* SquirrelMail required files. */
08185f2a 18require_once(SM_PATH . 'include/validate.php');
86725763 19require_once(SM_PATH . 'functions/display_messages.php');
20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/html.php');
22
acc28bac 23global $compose_new_win;
24
1e12d1ff 25if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
26 $composesession = 0;
27}
28
ce2c797b 29/* obsolete ?? */
65c3ec94 30function putSelectedMessagesIntoString($msg) {
31 $j = 0;
32 $i = 0;
33 $firstLoop = true;
65c3ec94 34 // If they have selected nothing msg is size one still, but will
35 // be an infinite loop because we never increment j. so check to
36 // see if msg[0] is set or not to fix this.
37 while (($j < count($msg)) && ($msg[0])) {
38 if ($msg[$i]) {
5e9e90fd 39 if ($firstLoop != true) {
40 $selectedMessages .= "&amp;";
41 } else {
42 $firstLoop = false;
43 }
dd88d31f 44 $selectedMessages .= "selMsg[$j]=$msg[$i]";
05207a68 45 $j++;
65c3ec94 46 }
47 $i++;
48 }
49}
50
acc28bac 51function attachSelectedMessages($msg, $imapConnection) {
a2a30ee7 52 global $username, $attachment_dir,
53 $data_dir, $composesession, $uid_support,
54 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
55 $compose_messages;
56
a2a30ee7 57 if (!isset($compose_messages)) {
58 $compose_messages = array();
59 sqsession_register($compose_messages,'compose_messages');
acc28bac 60 }
61
07abb0f3 62 if (!$composesession) {
21f3c131 63 $composesession = 1;
a2a30ee7 64 sqsession_register($composesession,'composesession');
acc28bac 65 } else {
21f3c131 66 $composesession++;
07abb0f3 67 sqsession_register($composesession,'composesession');
acc28bac 68 }
69
21f3c131 70 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
acc28bac 71
8c33aaf7 72 if ($thread_sort_messages || $allow_server_sort) {
73 $start_index=0;
74 } else {
75 $start_index = ($startMessage-1) * $show_num;
76 }
acc28bac 77
78 $i = 0;
79 $j = 0;
21f3c131 80 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a2a30ee7 81
82 $composeMessage = new Message();
83 $rfc822_header = new Rfc822Header();
84 $composeMessage->rfc822_header = $rfc822_header;
85 $composeMessage->reply_rfc822_header = '';
86
acc28bac 87 while ($j < count($msg)) {
88 if (isset($msg[$i])) {
21f3c131 89 $id = $msg[$i];
5b8d68ca 90 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
21f3c131 91 if ($response = 'OK') {
8c33aaf7 92 $k = $i + $start_index;
93 $subject = $msgs[$k]['SUBJECT'];
21f3c131 94
8c33aaf7 95 array_shift($body_a);
96 $body = implode('', $body_a);
97 $body .= "\r\n";
21f3c131 98
8c33aaf7 99 $localfilename = GenerateRandomString(32, 'FILE', 7);
100 $full_localfilename = "$hashed_attachment_dir/$localfilename";
101
81d79073 102 $fp = fopen( $full_localfilename, 'wb');
8c33aaf7 103 fwrite ($fp, $body);
104 fclose($fp);
a2a30ee7 105 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
106 $full_localfilename);
acc28bac 107 }
8c33aaf7 108 $j++;
109 }
110 $i++;
acc28bac 111 }
a2a30ee7 112 $compose_messages[$composesession] = $composeMessage;
87d5f199 113 sqsession_register($compose_messages,'compose_messages');
114 session_write_close();
acc28bac 115 return $composesession;
116}
117
9c22eb94 118
9837b0a5 119
120/* get globals */
1e12d1ff 121sqgetGlobalVar('key', $key, SQ_COOKIE);
122sqgetGlobalVar('username', $username, SQ_SESSION);
123sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
124sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
125sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
126
127sqgetGlobalVar('mailbox', $mailbox);
128sqgetGlobalVar('startMessage', $startMessage);
129sqgetGlobalVar('msg', $msg);
130
131sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
132sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
133sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
134
135sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
136sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
137sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
138sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
139sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
140sqgetGlobalVar('markRead', $markRead, SQ_POST);
141sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
142sqgetGlobalVar('attache', $attache, SQ_POST);
143sqgetGlobalVar('location', $location, SQ_POST);
9837b0a5 144
9837b0a5 145/* end of get globals */
146
65c3ec94 147$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 148$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 149
87d5f199 150$location = set_url_var($location,'composenew',0,false);
151$location = set_url_var($location,'composesession',0,false);
152$location = set_url_var($location,'session',0,false);
8c33aaf7 153
cd885078 154/* remember changes to mailbox setting */
155if (!isset($lastTargetMailbox)) {
156 $lastTargetMailbox = 'INBOX';
157}
158if ($targetMailbox != $lastTargetMailbox) {
159 $lastTargetMailbox = $targetMailbox;
9837b0a5 160 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 161}
87d5f199 162$exception = false;
65c3ec94 163// expunge-on-demand if user isn't using move_to_trash or auto_expunge
164if(isset($expungeButton)) {
9c22eb94 165 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
166 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
167 if ($startMessage > $show_num) {
87d5f199 168 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
9c22eb94 169 } else {
87d5f199 170 $location = set_url_var($location,'startMessage',1,false);
9c22eb94 171 }
172 }
65c3ec94 173} elseif(isset($undeleteButton)) {
174 // undelete messages if user isn't using move_to_trash or auto_expunge
65c3ec94 175 if (is_array($msg) == 1) {
176 // Removes \Deleted flag from selected messages
ce2c797b 177 $j = $i = 0;
178 $id = array();
65c3ec94 179 // If they have selected nothing msg is size one still, but will be an infinite
180 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
181 while ($j < count($msg)) {
f9b3e5d9 182 if ($msg[$i]) {
ce2c797b 183 $id[] = $msg[$i];
8c33aaf7 184 $j++;
f9b3e5d9 185 }
186 $i++;
65c3ec94 187 }
ce2c797b 188 if (count($id)) {
189 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
190 }
65c3ec94 191 } else {
87d5f199 192 $exception = true;
65c3ec94 193 }
194} elseif (!isset($moveButton)) {
195 // If the delete button was pressed, the moveButton variable will not be set.
ce2c797b 196 if (is_array($msg)) {
65c3ec94 197 // Marks the selected messages as 'Deleted'
ce2c797b 198 $j = $i = $cnt = 0;
199 $id = array();
65c3ec94 200 // If they have selected nothing msg is size one still, but will be an infinite
201 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
202 while ($j < count($msg)) {
23fd3c8e 203 if (isset($msg[$i])) {
ce2c797b 204 $id[] = $msg[$i];
65c3ec94 205 $j++;
2d7d3c2a 206 }
dd88d31f 207 $i++;
65c3ec94 208 }
ce2c797b 209 if (count($id) && !isset($attache)) {
210 if (isset($markRead)) {
211 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
212 } else if (isset($markUnread)) {
213 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
214 } else {
215 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
216 if ($auto_expunge) {
217 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
218 }
219 }
220 }
8c33aaf7 221 if (isset($attache)) {
acc28bac 222 $composesession = attachSelectedMessages($msg, $imapConnection);
87d5f199 223 $location = set_url_var($location, 'session', $composesession, false);
8c33aaf7 224 if ($compose_new_win) {
87d5f199 225 $location = set_url_var($location, 'composenew', 1, false);
acc28bac 226 } else {
8c33aaf7 227 $location = str_replace('search.php','compose.php',$location);
228 $location = str_replace('right_main.php','compose.php',$location);
acc28bac 229 }
8c33aaf7 230 } else {
9c22eb94 231 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
232 if ($startMessage > $show_num) {
87d5f199 233 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 234 } else {
87d5f199 235 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 236 }
237 }
8c33aaf7 238 }
65c3ec94 239 } else {
87d5f199 240 $exception = true;
65c3ec94 241 }
242} else { // Move messages
243 // lets check to see if they selected any messages
ce2c797b 244 if (is_array($msg)) {
245 $j = $i = 0;
246 $id = array();
65c3ec94 247 // If they have selected nothing msg is size one still, but will be an infinite
248 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
9c22eb94 249 $cnt = count($msg);
250 while ($j < $cnt) {
23fd3c8e 251 if (isset($msg[$i])) {
ce2c797b 252 $id[] = $msg[$i];
65c3ec94 253 $j++;
dd88d31f 254 }
255 $i++;
65c3ec94 256 }
ce2c797b 257 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
8c33aaf7 258 if ($auto_expunge) {
9c22eb94 259 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
260 } else {
261 $cnt = 0;
262 }
263
264 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
265 if ($startMessage > $show_num) {
87d5f199 266 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 267 } else {
87d5f199 268 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 269 }
8c33aaf7 270 }
65c3ec94 271 } else {
87d5f199 272 $exception = true;
65c3ec94 273 }
274}
65c3ec94 275// Log out this session
276sqimap_logout($imapConnection);
87d5f199 277if ($exception) {
278 displayPageHeader($color, $mailbox);
279 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
280} else {
281 header("Location: $location");
282 exit;
283}
2aa12d5e 284?>
6332704d 285</BODY></HTML>