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