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