A terrible pile of things done to the code. Did a little bit of SM_PATH
[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 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
124 sqimap_mailbox_select($imapConnection, $mailbox);
125
126 $location = set_url_var($location,'composenew');
127 $location = set_url_var($location,'composesession');
128 $location = set_url_var($location,'session');
129
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 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
143 header("Location: $location");
144 } elseif(isset($undeleteButton)) {
145 // undelete messages if user isn't using move_to_trash or auto_expunge
146
147 if (is_array($msg) == 1) {
148 // Removes \Deleted flag from selected messages
149 $j = 0;
150 $i = 0;
151 // If they have selected nothing msg is size one still, but will be an infinite
152 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
153 while ($j < count($msg)) {
154 if ($msg[$i]) {
155 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
156 $j++;
157 }
158 $i++;
159 }
160 header ("Location: $location");
161 } else {
162 displayPageHeader($color, $mailbox);
163 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
164 }
165 } elseif (!isset($moveButton)) {
166 // If the delete button was pressed, the moveButton variable will not be set.
167 if (is_array($msg) == 1) {
168 // Marks the selected messages as 'Deleted'
169 $j = 0;
170 $i = 0;
171 // If they have selected nothing msg is size one still, but will be an infinite
172 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
173 while ($j < count($msg)) {
174 if (isset($msg[$i])) {
175 if (isset($markRead)) {
176 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
177 } else if (isset($markUnread)) {
178 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
179 } else if (isset($attache)) {
180 break;
181 } else {
182 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
183 }
184 $j++;
185 }
186 $i++;
187 }
188 if ($auto_expunge) {
189 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
190 }
191 if (isset($attache)) {
192 $composesession = attachSelectedMessages($msg, $imapConnection);
193 if ($compose_new_win) {
194 header ("Location: $location&composenew=1&session=$composesession");
195 } else {
196 $location = str_replace('search.php','compose.php',$location);
197 $location = str_replace('right_main.php','compose.php',$location);
198 header ("Location: $location&session=$composesession");
199 }
200 } else {
201 header ("Location: $location");
202 }
203 } else {
204 displayPageHeader($color, $mailbox);
205 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
206 }
207 } else { // Move messages
208 // lets check to see if they selected any messages
209 if (is_array($msg) == 1) {
210 $j = 0;
211 $i = 0;
212 // If they have selected nothing msg is size one still, but will be an infinite
213 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
214 while ($j < count($msg)) {
215 if (isset($msg[$i])) {
216 /** check if they would like to move it to the trash folder or not */
217 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
218 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
219 $j++;
220 }
221 $i++;
222 }
223 if ($auto_expunge) {
224 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
225 }
226 header ("Location: $location");
227 } else {
228 displayPageHeader($color, $mailbox);
229 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
230 }
231 }
232 // Log out this session
233 sqimap_logout($imapConnection);
234
235 ?>
236 </BODY></HTML>