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