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