Lots of changes for variable initialization - clean up, really,
[squirrelmail.git] / src / move_messages.php
1 <?php
2
3 /**
4 * move_messages.php
5 *
6 * Copyright (c) 1999-2003 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 * $Id$
12 */
13
14 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
16
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');
22
23 global $compose_new_win;
24
25 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
26 $composesession = 0;
27 }
28
29 /* obsolete ?? */
30 function putSelectedMessagesIntoString($msg) {
31 $j = 0;
32 $i = 0;
33 $firstLoop = true;
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]) {
39 if ($firstLoop != true) {
40 $selectedMessages .= "&amp;";
41 } else {
42 $firstLoop = false;
43 }
44 $selectedMessages .= "selMsg[$j]=$msg[$i]";
45 $j++;
46 }
47 $i++;
48 }
49 }
50
51 function attachSelectedMessages($msg, $imapConnection) {
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
57 if (!isset($compose_messages)) {
58 $compose_messages = array();
59 sqsession_register($compose_messages,'compose_messages');
60 }
61
62 if (!$composesession) {
63 $composesession = 1;
64 sqsession_register($composesession,'composesession');
65 } else {
66 $composesession++;
67 sqsession_register($composesession,'composesession');
68 }
69
70 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
71
72 if ($thread_sort_messages || $allow_server_sort) {
73 $start_index=0;
74 } else {
75 $start_index = ($startMessage-1) * $show_num;
76 }
77
78 $i = 0;
79 $j = 0;
80 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
81
82 $composeMessage = new Message();
83 $rfc822_header = new Rfc822Header();
84 $composeMessage->rfc822_header = $rfc822_header;
85 $composeMessage->reply_rfc822_header = '';
86
87 while ($j < count($msg)) {
88 if (isset($msg[$i])) {
89 $id = $msg[$i];
90 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
91 if ($response = 'OK') {
92 $k = $i + $start_index;
93 $subject = $msgs[$k]['SUBJECT'];
94
95 array_shift($body_a);
96 $body = implode('', $body_a);
97 $body .= "\r\n";
98
99 $localfilename = GenerateRandomString(32, 'FILE', 7);
100 $full_localfilename = "$hashed_attachment_dir/$localfilename";
101
102 $fp = fopen( $full_localfilename, 'wb');
103 fwrite ($fp, $body);
104 fclose($fp);
105 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
106 $full_localfilename);
107 }
108 $j++;
109 }
110 $i++;
111 }
112 $compose_messages[$composesession] = $composeMessage;
113 sqsession_register($compose_messages,'compose_messages');
114 session_write_close();
115 return $composesession;
116 }
117
118
119
120 /* get globals */
121 sqgetGlobalVar('key', $key, SQ_COOKIE);
122 sqgetGlobalVar('username', $username, SQ_SESSION);
123 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
124 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
125 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
126
127 sqgetGlobalVar('mailbox', $mailbox);
128 sqgetGlobalVar('startMessage', $startMessage);
129 sqgetGlobalVar('msg', $msg);
130
131 sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
132 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
133 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
134
135 sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
136 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
137 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
138 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
139 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
140 sqgetGlobalVar('markRead', $markRead, SQ_POST);
141 sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
142 sqgetGlobalVar('attache', $attache, SQ_POST);
143 sqgetGlobalVar('location', $location, SQ_POST);
144
145 /* end of get globals */
146
147 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
148 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
149
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);
153
154 /* remember changes to mailbox setting */
155 if (!isset($lastTargetMailbox)) {
156 $lastTargetMailbox = 'INBOX';
157 }
158 if ($targetMailbox != $lastTargetMailbox) {
159 $lastTargetMailbox = $targetMailbox;
160 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
161 }
162 $exception = false;
163 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
164 if(isset($expungeButton)) {
165 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
166 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
167 if ($startMessage > $show_num) {
168 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
169 } else {
170 $location = set_url_var($location,'startMessage',1,false);
171 }
172 }
173 } elseif(isset($undeleteButton)) {
174 // undelete messages if user isn't using move_to_trash or auto_expunge
175 if (is_array($msg) == 1) {
176 // Removes \Deleted flag from selected messages
177 $j = $i = 0;
178 $id = array();
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)) {
182 if ($msg[$i]) {
183 $id[] = $msg[$i];
184 $j++;
185 }
186 $i++;
187 }
188 if (count($id)) {
189 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
190 }
191 } else {
192 $exception = true;
193 }
194 } elseif (!isset($moveButton)) {
195 // If the delete button was pressed, the moveButton variable will not be set.
196 if (is_array($msg)) {
197 // Marks the selected messages as 'Deleted'
198 $j = $i = $cnt = 0;
199 $id = array();
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)) {
203 if (isset($msg[$i])) {
204 $id[] = $msg[$i];
205 $j++;
206 }
207 $i++;
208 }
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 }
221 if (isset($attache)) {
222 $composesession = attachSelectedMessages($msg, $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 } else {
231 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
232 if ($startMessage > $show_num) {
233 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
234 } else {
235 $location = set_url_var($location,'startMessage',1, false);
236 }
237 }
238 }
239 } else {
240 $exception = true;
241 }
242 } else { // Move messages
243 // lets check to see if they selected any messages
244 if (is_array($msg)) {
245 $j = $i = 0;
246 $id = array();
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.
249 $cnt = count($msg);
250 while ($j < $cnt) {
251 if (isset($msg[$i])) {
252 $id[] = $msg[$i];
253 $j++;
254 }
255 $i++;
256 }
257 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
258 if ($auto_expunge) {
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) {
266 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
267 } else {
268 $location = set_url_var($location,'startMessage',1, false);
269 }
270 }
271 } else {
272 $exception = true;
273 }
274 }
275 // Log out this session
276 sqimap_logout($imapConnection);
277 if ($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 }
284 ?>
285 </BODY></HTML>