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