fixed warnings
[squirrelmail.git] / src / move_messages.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * move_messages.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 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 */
ef870322 13
86725763 14/* Path for SquirrelMail required files. */
15define('SM_PATH','../');
16
17/* SquirrelMail required files. */
08185f2a 18require_once(SM_PATH . 'include/validate.php');
86725763 19require_once(SM_PATH . 'functions/display_messages.php');
20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/html.php');
22
acc28bac 23global $compose_new_win;
24
65c3ec94 25function putSelectedMessagesIntoString($msg) {
26 $j = 0;
27 $i = 0;
28 $firstLoop = true;
65c3ec94 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]) {
5e9e90fd 34 if ($firstLoop != true) {
35 $selectedMessages .= "&amp;";
36 } else {
37 $firstLoop = false;
38 }
dd88d31f 39 $selectedMessages .= "selMsg[$j]=$msg[$i]";
05207a68 40 $j++;
65c3ec94 41 }
42 $i++;
43 }
44}
45
acc28bac 46function attachSelectedMessages($msg, $imapConnection) {
5b8d68ca 47 global $mailbox, $username, $attachment_dir, $attachments, $identity,
8c33aaf7 48 $data_dir, $composesession, $lastTargetMailbox, $uid_support,
49 $msgs, $startMessage, $show_num, $thread_sort_messages,
50 $allow_server_sort;
acc28bac 51
acc28bac 52 if (!isset($attachments)) {
21f3c131 53 $attachments = array();
54 session_register('attachments');
acc28bac 55 }
56
fee4465d 57 if (!isset($composesession) ) {
21f3c131 58 $composesession = 1;
fee4465d 59 session_register('composesession');
acc28bac 60 } else {
21f3c131 61 $composesession++;
acc28bac 62 }
63
21f3c131 64 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
acc28bac 65
66 $rem_attachments = array();
67 foreach ($attachments as $info) {
21f3c131 68 if ($info['session'] == $composesession) {
acc28bac 69 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
70 if (file_exists($attached_file)) {
21f3c131 71 unlink($attached_file);
acc28bac 72 }
21f3c131 73 } else {
74 $rem_attachments[] = $info;
75 }
acc28bac 76 }
77
78 $attachments = $rem_attachments;
79
8c33aaf7 80 if ($thread_sort_messages || $allow_server_sort) {
81 $start_index=0;
82 } else {
83 $start_index = ($startMessage-1) * $show_num;
84 }
acc28bac 85
86 $i = 0;
87 $j = 0;
21f3c131 88 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
acc28bac 89 while ($j < count($msg)) {
90 if (isset($msg[$i])) {
21f3c131 91 $id = $msg[$i];
5b8d68ca 92 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
21f3c131 93 if ($response = 'OK') {
8c33aaf7 94 $k = $i + $start_index;
95 $subject = $msgs[$k]['SUBJECT'];
21f3c131 96
8c33aaf7 97 array_shift($body_a);
98 $body = implode('', $body_a);
99 $body .= "\r\n";
21f3c131 100
8c33aaf7 101 $localfilename = GenerateRandomString(32, 'FILE', 7);
102 $full_localfilename = "$hashed_attachment_dir/$localfilename";
103
81d79073 104 $fp = fopen( $full_localfilename, 'wb');
8c33aaf7 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();
acc28bac 114 }
8c33aaf7 115 $j++;
116 }
117 $i++;
acc28bac 118 }
6540ee5e 119 setPref($data_dir, $username, 'attachments', serialize($attachments));
acc28bac 120 return $composesession;
121}
122
65c3ec94 123$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
124sqimap_mailbox_select($imapConnection, $mailbox);
125
8c33aaf7 126$location = set_url_var($location,'composenew');
127$location = set_url_var($location,'composesession');
128$location = set_url_var($location,'session');
129
130
cd885078 131/* remember changes to mailbox setting */
132if (!isset($lastTargetMailbox)) {
133 $lastTargetMailbox = 'INBOX';
134}
135if ($targetMailbox != $lastTargetMailbox) {
136 $lastTargetMailbox = $targetMailbox;
137 session_register('lastTargetMailbox');
138}
139
65c3ec94 140// expunge-on-demand if user isn't using move_to_trash or auto_expunge
141if(isset($expungeButton)) {
142 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
8c33aaf7 143 header("Location: $location");
65c3ec94 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;
65c3ec94 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)) {
f9b3e5d9 154 if ($msg[$i]) {
d8a8203a 155 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
8c33aaf7 156 $j++;
f9b3e5d9 157 }
158 $i++;
65c3ec94 159 }
8c33aaf7 160 header ("Location: $location");
65c3ec94 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;
65c3ec94 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)) {
23fd3c8e 174 if (isset($msg[$i])) {
65c3ec94 175 if (isset($markRead)) {
d8a8203a 176 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
65c3ec94 177 } else if (isset($markUnread)) {
d8a8203a 178 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
acc28bac 179 } else if (isset($attache)) {
180 break;
181 } else {
65c3ec94 182 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
183 }
184 $j++;
2d7d3c2a 185 }
dd88d31f 186 $i++;
65c3ec94 187 }
188 if ($auto_expunge) {
8cf653ad 189 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
65c3ec94 190 }
8c33aaf7 191 if (isset($attache)) {
acc28bac 192 $composesession = attachSelectedMessages($msg, $imapConnection);
8c33aaf7 193 if ($compose_new_win) {
194 header ("Location: $location&composenew=1&session=$composesession");
acc28bac 195 } else {
8c33aaf7 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");
acc28bac 199 }
8c33aaf7 200 } else {
201 header ("Location: $location");
202 }
65c3ec94 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;
65c3ec94 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)) {
23fd3c8e 215 if (isset($msg[$i])) {
65c3ec94 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);
d8a8203a 218 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
65c3ec94 219 $j++;
dd88d31f 220 }
221 $i++;
65c3ec94 222 }
8c33aaf7 223 if ($auto_expunge) {
8cf653ad 224 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
8c33aaf7 225 }
226 header ("Location: $location");
65c3ec94 227 } else {
228 displayPageHeader($color, $mailbox);
229 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
230 }
231}
65c3ec94 232// Log out this session
233sqimap_logout($imapConnection);
b40316f9 234
2aa12d5e 235?>
6332704d 236</BODY></HTML>