6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Enables message moving between folders on the IMAP server.
14 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
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');
24 global $compose_new_win;
26 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION
) ) {
30 function attachSelectedMessages($msg, $imapConnection) {
31 global $username, $attachment_dir, $startMessage,
32 $data_dir, $composesession, $uid_support,
33 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
36 if (!isset($compose_messages)) {
37 $compose_messages = array();
38 sqsession_register($compose_messages,'compose_messages');
41 if (!$composesession) {
43 sqsession_register($composesession,'composesession');
46 sqsession_register($composesession,'composesession');
49 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
51 if ($thread_sort_messages ||
$allow_server_sort) {
54 $start_index = ($startMessage-1) * $show_num;
59 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
61 $composeMessage = new Message();
62 $rfc822_header = new Rfc822Header();
63 $composeMessage->rfc822_header
= $rfc822_header;
64 $composeMessage->reply_rfc822_header
= '';
66 while ($j < count($msg)) {
67 if (isset($msg[$i])) {
69 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
71 if ($response == 'OK') {
73 // fetch the subject for the message with $id from msgs.
74 // is there a more efficient way to do this?
75 foreach($msgs as $k => $vals) {
76 if($vals['ID'] == $id) {
77 $subject = $msgs[$k]['SUBJECT'];
83 $body = implode('', $body_a);
86 $localfilename = GenerateRandomString(32, 'FILE', 7);
87 $full_localfilename = "$hashed_attachment_dir/$localfilename";
89 $fp = fopen( $full_localfilename, 'wb');
92 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
99 $compose_messages[$composesession] = $composeMessage;
100 sqsession_register($compose_messages,'compose_messages');
101 session_write_close();
102 return $composesession;
108 sqgetGlobalVar('key', $key, SQ_COOKIE
);
109 sqgetGlobalVar('username', $username, SQ_SESSION
);
110 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
111 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
112 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
114 sqgetGlobalVar('mailbox', $mailbox);
115 sqgetGlobalVar('startMessage', $startMessage);
116 sqgetGlobalVar('msg', $msg);
118 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
119 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
120 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
122 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
123 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
124 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
125 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
126 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
127 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
128 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
129 sqgetGlobalVar('attache', $attache, SQ_POST
);
130 sqgetGlobalVar('location', $location, SQ_POST
);
132 /* end of get globals */
134 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
135 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
137 $location = set_url_var($location,'composenew',0,false);
138 $location = set_url_var($location,'composesession',0,false);
139 $location = set_url_var($location,'session',0,false);
141 /* remember changes to mailbox setting */
142 if (!isset($lastTargetMailbox)) {
143 $lastTargetMailbox = 'INBOX';
145 if ($targetMailbox != $lastTargetMailbox) {
146 $lastTargetMailbox = $targetMailbox;
147 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
151 do_hook('move_before_move');
155 Move msg list sorting up here, as it is used several times,
156 makes it more efficient to do it in one place for the code
159 if (isset($msg) && is_array($msg)) {
160 foreach( $msg as $key=>$uid ) {
161 // using foreach removes the risk of infinite loops that was there //
166 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
167 if(isset($expungeButton)) {
168 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
169 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
170 if ($startMessage > $show_num) {
171 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
173 $location = set_url_var($location,'startMessage',1,false);
176 } elseif(isset($undeleteButton)) {
177 // undelete messages if user isn't using move_to_trash or auto_expunge
178 // Removes \Deleted flag from selected messages
180 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
184 } elseif (!isset($moveButton)) {
187 if (!isset($attache)) {
188 $button_action = concat_hook_function('move_messages_button_action');
189 if (isset($markRead)) {
190 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
191 } else if (isset($markUnread)) {
192 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
194 if (!$button_action) {
195 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
197 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
202 if (isset($attache)) {
203 $composesession = attachSelectedMessages($id, $imapConnection);
204 $location = set_url_var($location, 'session', $composesession, false);
205 if ($compose_new_win) {
206 $location = set_url_var($location, 'composenew', 1, false);
208 $location = str_replace('search.php','compose.php',$location);
209 $location = str_replace('right_main.php','compose.php',$location);
212 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
213 if ($startMessage > $show_num) {
214 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
216 $location = set_url_var($location,'startMessage',1, false);
223 } else { // Move messages
226 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
228 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
233 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
234 if ($startMessage > $show_num) {
235 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
237 $location = set_url_var($location,'startMessage',1, false);
244 // Log out this session
245 sqimap_logout($imapConnection);
247 displayPageHeader($color, $mailbox);
248 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
250 header("Location: $location");