6e73b175777b41d09277ef4050926c4d6380798b
[squirrelmail.git] / src / move_messages.php
1 <?php
2
3 /**
4 * move_messages.php
5 *
6 * Copyright (c) 1999-2004 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 * @package squirrelmail
13 */
14
15 /** Path for SquirrelMail required files. */
16 define('SM_PATH','../');
17
18 /* SquirrelMail required files. */
19 require_once(SM_PATH . 'include/validate.php');
20 require_once(SM_PATH . 'functions/global.php');
21 require_once(SM_PATH . 'functions/display_messages.php');
22 require_once(SM_PATH . 'functions/imap.php');
23 require_once(SM_PATH . 'functions/html.php');
24
25 global $compose_new_win;
26
27 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
28 $composesession = 0;
29 }
30
31 function attachSelectedMessages($msg, $imapConnection) {
32 global $username, $attachment_dir, $startMessage,
33 $data_dir, $composesession,
34 $msgs, $show_num, $compose_messages;
35
36 if (!isset($compose_messages)) {
37 $compose_messages = array();
38 sqsession_register($compose_messages,'compose_messages');
39 }
40
41 if (!$composesession) {
42 $composesession = 1;
43 sqsession_register($composesession,'composesession');
44 } else {
45 $composesession++;
46 sqsession_register($composesession,'composesession');
47 }
48
49 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
50
51 $composeMessage = new Message();
52 $rfc822_header = new Rfc822Header();
53 $composeMessage->rfc822_header = $rfc822_header;
54 $composeMessage->reply_rfc822_header = '';
55
56 foreach($msg as $id) {
57 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
58
59 if ($response == 'OK') {
60 // fetch the subject for the message with $id from msgs.
61 // is there a more efficient way to do this?
62 foreach($msgs as $k => $vals) {
63 if($vals['ID'] == $id) {
64 $subject = $msgs[$k]['SUBJECT'];
65 break;
66 }
67 }
68
69 array_shift($body_a);
70 array_pop($body_a);
71 $body = implode('', $body_a);
72 $body .= "\r\n";
73
74 $localfilename = GenerateRandomString(32, 'FILE', 7);
75 $full_localfilename = "$hashed_attachment_dir/$localfilename";
76
77 $fp = fopen( $full_localfilename, 'wb');
78 fwrite ($fp, $body);
79 fclose($fp);
80 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
81 $full_localfilename);
82 }
83 }
84
85 $compose_messages[$composesession] = $composeMessage;
86 sqsession_register($compose_messages,'compose_messages');
87 session_write_close();
88 return $composesession;
89 }
90
91
92
93 /* get globals */
94 sqgetGlobalVar('key', $key, SQ_COOKIE);
95 sqgetGlobalVar('username', $username, SQ_SESSION);
96 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
97 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
98 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
99
100 sqgetGlobalVar('mailbox', $mailbox);
101 sqgetGlobalVar('startMessage', $startMessage);
102 sqgetGlobalVar('msg', $msg);
103
104 sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
105 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
106 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
107
108 sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
109 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
110 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
111 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
112 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
113 sqgetGlobalVar('markRead', $markRead, SQ_POST);
114 sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
115 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST);
116 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST);
117 sqgetGlobalVar('attache', $attache, SQ_POST);
118 sqgetGlobalVar('location', $location, SQ_POST);
119 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST);
120
121 /* end of get globals */
122
123 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
124 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
125
126 $location = set_url_var($location,'composenew',0,false);
127 $location = set_url_var($location,'composesession',0,false);
128 $location = set_url_var($location,'session',0,false);
129
130 /* remember changes to mailbox setting */
131 if (!isset($lastTargetMailbox)) {
132 $lastTargetMailbox = 'INBOX';
133 }
134 if ($targetMailbox != $lastTargetMailbox) {
135 $lastTargetMailbox = $targetMailbox;
136 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
137 }
138 $exception = false;
139
140 do_hook('move_before_move');
141
142
143 /*
144 Move msg list sorting up here, as it is used several times,
145 makes it more efficient to do it in one place for the code
146 */
147 $id = array();
148 if (isset($msg) && is_array($msg)) {
149 foreach( $msg as $key=>$uid ) {
150 // using foreach removes the risk of infinite loops that was there //
151 $id[] = $uid;
152 }
153 }
154
155 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
156 if(isset($expungeButton)) {
157 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
158 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
159 if ($startMessage > $show_num) {
160 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
161 } else {
162 $location = set_url_var($location,'startMessage',1,false);
163 }
164 }
165 } elseif(isset($undeleteButton)) {
166 // undelete messages if user isn't using move_to_trash or auto_expunge
167 // Removes \Deleted flag from selected messages
168 if (count($id)) {
169 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
170 } else {
171 $exception = true;
172 }
173 } elseif (!isset($moveButton)) {
174 if (count($id)) {
175 $cnt = count($id);
176 if (!isset($attache)) {
177 if (isset($markRead)) {
178 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
179 } else if (isset($markUnread)) {
180 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
181 } else if (isset($markFlagged)) {
182 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', true, true);
183 } else if (isset($markUnflagged)) {
184 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', false, true);
185 } else {
186 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
187 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
188 if ($auto_expunge) {
189 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
190 }
191 }
192 }
193 }
194 if (isset($attache)) {
195 $composesession = attachSelectedMessages($id, $imapConnection);
196 $location = set_url_var($location, 'session', $composesession, false);
197 if ($compose_new_win) {
198 $location = set_url_var($location, 'composenew', 1, false);
199 } else {
200 $location = str_replace('search.php','compose.php',$location);
201 $location = str_replace('right_main.php','compose.php',$location);
202 }
203 } else {
204 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
205 if ($startMessage > $show_num) {
206 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
207 } else {
208 $location = set_url_var($location,'startMessage',1, false);
209 }
210 }
211 }
212 } else {
213 $exception = true;
214 }
215 } else { // Move messages
216
217 if (count($id)) {
218 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
219 if ($auto_expunge) {
220 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
221 } else {
222 $cnt = 0;
223 }
224
225 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
226 if ($startMessage > $show_num) {
227 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
228 } else {
229 $location = set_url_var($location,'startMessage',1, false);
230 }
231 }
232 } else {
233 $exception = true;
234 }
235 }
236 // Log out this session
237 sqimap_logout($imapConnection);
238 if ($exception) {
239 displayPageHeader($color, $mailbox);
240 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
241 } else {
242 header("Location: $location");
243 exit;
244 }
245 ?>
246 </BODY></HTML>