squirrelmail-webmail  1.4.22
About: SquirrelMail is a standards-based webmail package with strong MIME support, address books, and folder manipulation (written in PHP4).
  Fossies Dox: squirrelmail-webmail-1.4.22.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

move_messages.php
Go to the documentation of this file.
1 <?php
2 
15 define('PAGE_NAME', 'move_messages');
16 
17 /* Path for SquirrelMail required files. */
18 define('SM_PATH','../');
19 
20 /* SquirrelMail required files. */
21 require_once(SM_PATH . 'include/validate.php');
22 require_once(SM_PATH . 'functions/global.php');
23 require_once(SM_PATH . 'functions/display_messages.php');
24 require_once(SM_PATH . 'functions/imap.php');
25 require_once(SM_PATH . 'functions/html.php');
26 
27 global $compose_new_win;
28 
29 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
30  $composesession = 0;
31 }
32 
34  global $username, $attachment_dir, $startMessage,
35  $data_dir, $composesession, $uid_support, $mailbox,
36  $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
37  $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, $composesession);
53 
54  if ($thread_sort_messages || $allow_server_sort) {
55  $start_index=0;
56  } else {
57  $start_index = ($startMessage-1) * $show_num;
58  }
59 
60  $i = 0;
61  $j = 0;
62  $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
63 
64  $composeMessage = new Message();
66  $composeMessage->rfc822_header = $rfc822_header;
67  $composeMessage->reply_rfc822_header = '';
68 
69  while ($j < count($msg)) {
70  if (isset($msg[$i])) {
71  $id = $msg[$i];
72 
73  $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
74 
75  if ($response == 'OK') {
77 
78  // fetch the subject for the message from the object
79  //
80  $subject = $message->rfc822_header->subject;
81 
82  // use subject for file name
83  //
84  if ( empty($subject) )
85  $filename = "untitled-".$message->entity_id;
86  else
88  $filename .= '.msg';
89  $filename = decodeHeader($filename, false, false);
90 
91  // figure out a subject for new message
92  //
93  $subject = decodeHeader($subject, false, false, true);
94  $subject = trim($subject);
95  if (substr(strtolower($subject), 0, 4) != 'fwd:') {
96  $subject = 'Fwd: ' . $subject;
97  }
98 
99  array_shift($body_a);
100  array_pop($body_a);
101  $body = implode('', $body_a);
102  $body .= "\r\n";
103 
104  $localfilename = GenerateRandomString(32, 'FILE', 7);
105  $full_localfilename = "$hashed_attachment_dir/$localfilename";
106  while (file_exists($full_localfilename)) {
107  $localfilename = GenerateRandomString(32, 'FILE', 7);
108  $full_localfilename = "$hashed_attachment_dir/$localfilename";
109  }
110 
111  $fp = fopen( $full_localfilename, 'wb');
112  fwrite ($fp, $body);
113  fclose($fp);
114  $composeMessage->initAttachment('message/rfc822',$filename,
115  $localfilename);
116  $composeMessage->rfc822_header->subject = $subject;
117  }
118  $j++;
119  }
120  $i++;
121  }
122  $compose_messages[$composesession] = $composeMessage;
123  sqsession_register($compose_messages,'compose_messages');
124  session_write_close();
125  return $composesession;
126 }
127 
128 
129 
130 /* get globals */
131 sqgetGlobalVar('key', $key, SQ_COOKIE);
132 sqgetGlobalVar('username', $username, SQ_SESSION);
133 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
134 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
135 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
136 
137 sqgetGlobalVar('mailbox', $mailbox);
138 sqgetGlobalVar('startMessage', $startMessage);
139 sqgetGlobalVar('msg', $msg);
140 
142 sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
143 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
144 
145 sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
146 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
147 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
148 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
149 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
150 sqgetGlobalVar('markRead', $markRead, SQ_POST);
151 sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
152 sqgetGlobalVar('attache', $attache, SQ_POST);
153 sqgetGlobalVar('location', $location, SQ_POST);
154 
155 if (!sqgetGlobalVar('smtoken',$submitted_token, SQ_POST)) {
156  $submitted_token = '';
157 }
158 /* end of get globals */
159 
160 // security check
161 sm_validate_security_token($submitted_token, 3600, TRUE);
162 
165 
166 $location = set_url_var($location,'composenew',0,false);
167 $location = set_url_var($location,'composesession',0,false);
168 $location = set_url_var($location,'session',0,false);
169 
170 // make sure that cache is not used
171 $location = set_url_var($location,'use_mailbox_cache',0,false);
172 
173 /* remember changes to mailbox setting */
174 if (!isset($lastTargetMailbox)) {
175  $lastTargetMailbox = 'INBOX';
176 }
177 if ($targetMailbox != $lastTargetMailbox) {
178  $lastTargetMailbox = $targetMailbox;
179  sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
180 }
181 $exception = false;
182 
183 do_hook('move_before_move');
184 
185 
186 /*
187  Move msg list sorting up here, as it is used several times,
188  makes it more efficient to do it in one place for the code
189 */
190 $id = array();
191 if (isset($msg) && is_array($msg)) {
192  foreach( $msg as $key=>$uid ) {
193  // using foreach removes the risk of infinite loops that was there //
194  $id[] = $uid;
195  }
196 }
197 
198 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
199 if(isset($expungeButton)) {
201  if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
202  if ($startMessage > $show_num) {
203  $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
204  } else {
205  $location = set_url_var($location,'startMessage',1,false);
206  }
207  }
208 } elseif(isset($undeleteButton)) {
209  // undelete messages if user isn't using move_to_trash or auto_expunge
210  // Removes \Deleted flag from selected messages
211  if (count($id)) {
212  sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
213  } else {
214  $exception = true;
215  }
216 } elseif (!isset($moveButton)) {
217  if (count($id)) {
218  $cnt = count($id);
219  if (!isset($attache)) {
220  if (isset($markRead)) {
221  sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
222  } else if (isset($markUnread)) {
223  sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
224  } else {
226  if ($auto_expunge) {
228  }
229  if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
230  if ($startMessage > $show_num) {
231  $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
232  } else {
233  $location = set_url_var($location,'startMessage',1, false);
234  }
235  }
236  }
237  } else {
238  $composesession = attachSelectedMessages($id, $imapConnection);
239  $location = set_url_var($location, 'session', $composesession, false);
240  $location = set_url_var($location, 'forward_as_attachment_init', 1, false);
241  if ($compose_new_win) {
242  $location = set_url_var($location, 'composenew', 1, false);
243  } else {
244  $location = str_replace('search.php','compose.php',$location);
245  $location = str_replace('right_main.php','compose.php',$location);
246  }
247  }
248  } else {
249  $exception = true;
250  }
251 } else { // Move messages
252  if (count($id)) {
253  // move messages only when target mailbox is not the same as source mailbox
254  if ($mailbox!=$targetMailbox) {
255  sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
256  if ($auto_expunge) {
258  } else {
259  $cnt = 0;
260  }
261 
262  if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
263  if ($startMessage > $show_num) {
264  $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
265  } else {
266  $location = set_url_var($location,'startMessage',1, false);
267  }
268  }
269  }
270  } else {
271  $exception = true;
272  }
273 }
274 // Log out this session
276 if ($exception) {
278  error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
279 } else {
280  header("Location: $location");
282 }
283 ?>
284 </body></html>
$body
if(isset($ldap_server) && $ldap_server[0] &&! extension_loaded('ldap')) $body
Definition: bug_report.php:106
elseif
if(! sqgetGlobalVar('sound', $sound, SQ_GET)) elseif($sound=='(none)')
Definition: testsound.php:25
sqgetGlobalVar
sqgetGlobalVar($name, &$value, $search=SQ_INORDER)
Definition: global.php:344
sqimap_mailbox_expunge
sqimap_mailbox_expunge($imap_stream, $mailbox, $handle_errors=true, $id='')
Definition: imap_mailbox.php:198
$cnt
$cnt
Definition: options_identities.php:86
exit
exit
Definition: move_messages.php:281
$imapServerAddress
$imapServerAddress
Definition: config_default.php:202
sqimap_login
sqimap_login($username, $password, $imap_server_address, $imap_port, $hide)
Definition: imap_general.php:481
$rfc822_header
$rfc822_header
Definition: compose.php:362
$data_dir
$data_dir
Definition: config_default.php:499
SQ_SESSION
const SQ_SESSION
Definition: global.php:18
$id
$id
Definition: move_messages.php:190
sqimap_toggle_flag
sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors)
Definition: imap_messages.php:1173
displayPageHeader
displayPageHeader($color, $mailbox, $xtra='', $session=false)
Definition: page_header.php:105
$mailbox
$mailbox
Definition: options.php:28
SM_PATH
const SM_PATH
Definition: move_messages.php:18
$compose_new_win
global $compose_new_win
Definition: move_messages.php:21
set_url_var
set_url_var($url, $var, $val=null, $link=true)
Definition: html.php:109
html
Definition: html.class.php:18
$username
global $username
Definition: validate.php:55
$exception
if(!isset($lastTargetMailbox)) if($targetMailbox !=$lastTargetMailbox) $exception
Definition: move_messages.php:181
do_hook
do_hook($name)
Definition: plugin.php:42
$location
$location
Definition: move_messages.php:166
sqimap_mailbox_select
sqimap_mailbox_select($imap_stream, $mailbox)
Definition: imap_mailbox.php:238
$imapConnection
$imapConnection
Definition: move_messages.php:163
$sort
$sort
Definition: load_prefs.php:170
$show_num
$show_num
Definition: load_prefs.php:130
$uid_support
$uid_support
Definition: config_default.php:643
$message
$message
Definition: download.php:54
sm_validate_security_token
sm_validate_security_token($token, $validity_period=0, $show_error=FALSE)
Definition: strings.php:1405
getHashedDir
getHashedDir($username, $dir, $hash_dirs='')
Definition: prefs.php:109
Rfc822Header
Definition: Rfc822Header.class.php:26
sqimap_msgs_list_move
sqimap_msgs_list_move($imap_stream, $id, $mailbox, $handle_errors=true)
Definition: imap_messages.php:56
$attachment_dir
$attachment_dir
Definition: config_default.php:517
sqimap_msgs_list_delete
sqimap_msgs_list_delete($imap_stream, $mailbox, $id)
Definition: imap_messages.php:73
sqimap_logout
sqimap_logout($imap_stream)
Definition: imap_general.php:639
$j
for($i=0; $i< $numboxes; $i++) $j
Definition: empty_trash.php:72
SQ_POST
const SQ_POST
Definition: global.php:17
decodeHeader
decodeHeader($string, $utfencode=true, $htmlsave=true, $decide=false)
Definition: mime.php:633
$filename
if(isset($override_type0)) if(isset($override_type1)) $filename
Definition: download.php:97
sqsession_register
sqsession_register($var, $name)
Definition: global.php:289
_
_($str)
Definition: gettext.php:160
$base_uri
if(!(bool) ini_get('session.use_cookies')||ini_get('session.use_cookies')=='off') $base_uri
Definition: global.php:173
$subject
if(isset($messages[$mbx_response['UIDVALIDITY']]["$passed_id"])) if(!is_object($message)) $subject
Definition: download.php:64
sqimap_get_message
sqimap_get_message($imap_stream, $id, $mailbox)
Definition: imap_messages.php:1050
$allow_server_sort
$allow_server_sort
Definition: config_default.php:625
sqimap_run_command
sqimap_run_command($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id=false, $filter=false, $outputstream=false, $no_return=false)
Definition: imap_general.php:58
$imapPort
$imapPort
Definition: config_default.php:210
Message
Definition: Message.class.php:27
error_message
error_message($message, $mailbox, $sort, $startMessage, $color)
Definition: display_messages.php:20
$color
$color
Definition: load_prefs.php:28
SQ_COOKIE
const SQ_COOKIE
Definition: global.php:19
GenerateRandomString
GenerateRandomString($size, $chars, $flags=0)
Definition: strings.php:614
attachSelectedMessages
if(!sqgetGlobalVar('composesession', $composesession, SQ_SESSION)) attachSelectedMessages($msg, $imapConnection)
Definition: move_messages.php:33
$msgs
if(isset($sendreceipt)) $msgs[$passed_id]['FLAG_SEEN']
Definition: read_body.php:852
$mbx_response
$mbx_response
Definition: move_messages.php:164
$auto_expunge
$auto_expunge
Definition: config_default.php:397