Source for file move_messages.php

Documentation is available at move_messages.php

  1. <?php
  2.  
  3. /**
  4.  * move_messages.php
  5.  *
  6.  * Enables message moving between folders on the IMAP server.
  7.  *
  8.  * @copyright 1999-2012 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: move_messages.php 14319 2012-04-27 07:15:28Z pdontthink $
  11.  * @package squirrelmail
  12.  */
  13.  
  14. /** This is the move_messages page */
  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'$composesessionSQ_SESSION) ) {
  30.   $composesession 0;
  31. }
  32.  
  33. function attachSelectedMessages($msg$imapConnection{
  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();
  65.     $rfc822_header new Rfc822Header();
  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'{
  76.                 $message sqimap_get_message($imapConnection$id$mailbox);
  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
  87.                     $filename $subject;
  88.                 $filename .= '.eml';
  89.  
  90.                 // figure out a subject for new message
  91.                 //
  92.                 $subject decodeHeader($subjectfalsefalsetrue);
  93.                 $subject trim($subject);
  94.                 if (substr(strtolower($subject)04!= 'fwd:'{
  95.                     $subject 'Fwd: ' $subject;
  96.                 }
  97.  
  98.                 array_shift($body_a);
  99.                 array_pop($body_a);
  100.                 $body implode(''$body_a);
  101.                 $body .= "\r\n";
  102.  
  103.                 $localfilename GenerateRandomString(32'FILE'7);
  104.                 $full_localfilename "$hashed_attachment_dir/$localfilename";
  105.                 while (file_exists($full_localfilename)) {
  106.                     $localfilename GenerateRandomString(32'FILE'7);
  107.                     $full_localfilename "$hashed_attachment_dir/$localfilename";
  108.                 }
  109.  
  110.                 $fp fopen$full_localfilename'wb');
  111.                 fwrite ($fp$body);
  112.                 fclose($fp);
  113.                 $composeMessage->initAttachment('message/rfc822',$filename,
  114.                      $localfilename);
  115.                 $composeMessage->rfc822_header->subject $subject;
  116.             }
  117.             $j++;
  118.         }
  119.         $i++;
  120.     }
  121.     $compose_messages[$composesession$composeMessage;
  122.     sqsession_register($compose_messages,'compose_messages');
  123.     return $composesession;
  124. }
  125.  
  126.  
  127.  
  128. /* get globals */
  129. sqgetGlobalVar('key',       $key,           SQ_COOKIE);
  130. sqgetGlobalVar('username',  $username,      SQ_SESSION);
  131. sqgetGlobalVar('onetimepad',$onetimepad,    SQ_SESSION);
  132. sqgetGlobalVar('delimiter'$delimiter,     SQ_SESSION);
  133. sqgetGlobalVar('base_uri',  $base_uri,      SQ_SESSION);
  134.  
  135. sqgetGlobalVar('mailbox'$mailbox);
  136. sqgetGlobalVar('startMessage'$startMessage);
  137. sqgetGlobalVar('msg'$msg);
  138.  
  139. sqgetGlobalVar('msgs',              $msgs,              SQ_SESSION);
  140. sqgetGlobalVar('composesession',    $composesession,    SQ_SESSION);
  141. sqgetGlobalVar('lastTargetMailbox'$lastTargetMailboxSQ_SESSION);
  142.  
  143. sqgetGlobalVar('moveButton',      $moveButton,      SQ_POST);
  144. sqgetGlobalVar('expungeButton',   $expungeButton,   SQ_POST);
  145. sqgetGlobalVar('targetMailbox',   $targetMailbox,   SQ_POST);
  146. sqgetGlobalVar('expungeButton',   $expungeButton,   SQ_POST);
  147. sqgetGlobalVar('undeleteButton',  $undeleteButton,  SQ_POST);
  148. sqgetGlobalVar('markRead',        $markRead,        SQ_POST);
  149. sqgetGlobalVar('markUnread',      $markUnread,      SQ_POST);
  150. sqgetGlobalVar('attache',         $attache,         SQ_POST);
  151. sqgetGlobalVar('location',        $location,        SQ_POST);
  152.  
  153. if (!sqgetGlobalVar('smtoken',$submitted_tokenSQ_POST)) {
  154.     $submitted_token '';
  155. }
  156. /* end of get globals */
  157.  
  158. // security check
  159. sm_validate_security_token($submitted_token3600TRUE);
  160.  
  161. $imapConnection sqimap_login($username$key$imapServerAddress$imapPort0);
  162. $mbx_response=sqimap_mailbox_select($imapConnection$mailbox);
  163.  
  164. $location set_url_var($location,'composenew',0,false);
  165. $location set_url_var($location,'composesession',0,false);
  166. $location set_url_var($location,'session',0,false);
  167. $location set_url_var($location,'preselected',NULL,false,true);
  168.  
  169. // make sure that cache is not used
  170. $location set_url_var($location,'use_mailbox_cache',0,false);
  171.  
  172. /* remember changes to mailbox setting */
  173. if (!isset($lastTargetMailbox)) {
  174.     $lastTargetMailbox 'INBOX';
  175. }
  176. if ($targetMailbox != $lastTargetMailbox{
  177.     $lastTargetMailbox $targetMailbox;
  178.     sqsession_register($lastTargetMailbox'lastTargetMailbox');
  179. }
  180. $exception false;
  181.  
  182. do_hook('move_before_move');
  183.  
  184.  
  185. /*
  186.     Move msg list sorting up here, as it is used several times,
  187.     makes it more efficient to do it in one place for the code
  188. */
  189. $id array();
  190. if (isset($msg&& is_array($msg)) {
  191.     foreach$msg as $key=>$uid {
  192.         // using foreach removes the risk of infinite loops that was there //
  193.         $id[$uid;
  194.     }
  195. }
  196.  
  197. // expunge-on-demand if user isn't using move_to_trash or auto_expunge
  198. if(isset($expungeButton)) {
  199.     $cnt sqimap_mailbox_expunge($imapConnection$mailboxtrue);
  200.     if (($startMessage+$cnt-1>= $mbx_response['EXISTS']{
  201.         if ($startMessage $show_num{
  202.             $location set_url_var($location,'startMessage',$startMessage-$show_num,false);
  203.         else {
  204.             $location set_url_var($location,'startMessage',1,false);
  205.         }
  206.     }
  207. elseif(isset($undeleteButton)) {
  208.     // undelete messages if user isn't using move_to_trash or auto_expunge
  209.     // Removes \Deleted flag from selected messages
  210.     if (count($id)) {
  211.         sqimap_toggle_flag($imapConnection$id'\\Deleted',false,true);
  212.     else {
  213.         $exception true;
  214.     }
  215. elseif (!isset($moveButton)) {
  216.     if (count($id)) {
  217.         $cnt count($id);
  218.         if (!isset($attache)) {
  219.             if (isset($markRead)) {
  220.                 sqimap_toggle_flag($imapConnection$id'\\Seen',true,true);
  221.             else if (isset($markUnread)) {
  222.                 sqimap_toggle_flag($imapConnection$id'\\Seen',false,true);
  223.             else  {
  224.                 sqimap_msgs_list_delete($imapConnection$mailbox$id);
  225.                 if ($auto_expunge{
  226.                     $cnt sqimap_mailbox_expunge($imapConnection$mailboxtrue);
  227.                 }
  228.                 if (($startMessage+$cnt-1>= $mbx_response['EXISTS']{
  229.                     if ($startMessage $show_num{
  230.                         $location set_url_var($location,'startMessage',$startMessage-$show_numfalse);
  231.                     else {
  232.                         $location set_url_var($location,'startMessage',1false);
  233.                     }
  234.                 }
  235.             }
  236.         else {
  237.             $composesession attachSelectedMessages($id$imapConnection);
  238.             $location set_url_var($location'session'$composesessionfalse);
  239.             $location set_url_var($location'forward_as_attachment_init'1false);
  240.             if ($compose_new_win{
  241.                 $location set_url_var($location'composenew'1false);
  242.             else {
  243.                 $location str_replace('search.php','compose.php',$location);
  244.                 $location str_replace('right_main.php','compose.php',$location);
  245.             }
  246.         }
  247.     else {
  248.         $exception true;
  249.     }
  250. else {    // Move messages
  251.     if (count($id)) {
  252.         // move messages only when target mailbox is not the same as source mailbox
  253.         if ($mailbox!=$targetMailbox{
  254.             sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
  255.             if ($auto_expunge{
  256.                 $cnt sqimap_mailbox_expunge($imapConnection$mailboxtrue);
  257.             else {
  258.                 $cnt 0;
  259.             }
  260.  
  261.             if (($startMessage+$cnt-1>= $mbx_response['EXISTS']{
  262.                 if ($startMessage $show_num{
  263.                     $location set_url_var($location,'startMessage',$startMessage-$show_numfalse);
  264.                 else {
  265.                     $location set_url_var($location,'startMessage',1false);
  266.                 }
  267.             }
  268.         }
  269.     else {
  270.         $exception true;
  271.     }
  272. }
  273. // Log out this session
  274. sqimap_logout($imapConnection);
  275. if ($exception{
  276.     displayPageHeader($color$mailbox);
  277.     error_message(_("No messages were selected.")$mailbox$sort$startMessage$color);
  278. else {
  279.     header("Location: $location");
  280.     exit;
  281. }
  282. ?>
  283. </body></html>

Documentation generated on Mon, 20 May 2013 04:22:41 +0200 by phpDocumentor 1.4.3