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-2020 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: move_messages.php 14840 2020-01-07 07:42:38Z 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.  
  38.     if (!$composesession{
  39.         $composesession 1;
  40.             sqsession_register($composesession,'composesession');
  41.     else {
  42.         $composesession++;
  43.         sqsession_register($composesession,'composesession');
  44.     }
  45.  
  46.     $hashed_attachment_dir getHashedDir($username$attachment_dir$composesession);
  47.  
  48.     if ($thread_sort_messages || $allow_server_sort{
  49.        $start_index=0;
  50.     else {
  51.        $start_index ($startMessage-1$show_num;
  52.     }
  53.  
  54.     $i 0;
  55.     $j 0;
  56.     $hashed_attachment_dir getHashedDir($username$attachment_dir);
  57.  
  58.     $composeMessage new Message();
  59.     $rfc822_header new Rfc822Header();
  60.     $composeMessage->rfc822_header $rfc822_header;
  61.     $composeMessage->reply_rfc822_header '';
  62.  
  63.     while ($j count($msg)) {
  64.         if (isset($msg[$i])) {
  65.             $id $msg[$i];
  66.             
  67.             $body_a sqimap_run_command($imapConnection"FETCH $id RFC822",true$response$readmessage$uid_support);
  68.             
  69.             if ($response == 'OK'{
  70.                 $message sqimap_get_message($imapConnection$id$mailbox);
  71.  
  72.                 // fetch the subject for the message from the object
  73.                 //
  74.                 $subject $message->rfc822_header->subject;
  75.  
  76.                 // use subject for file name
  77.                 //
  78.                 if empty($subject) )
  79.                     $filename "untitled-".$message->entity_id;
  80.                 else
  81.                     $filename $subject;
  82.                 $filename .= '.eml';
  83.  
  84.                 // figure out a subject for new message
  85.                 //
  86.                 $subject decodeHeader($subjectfalsefalsetrue);
  87.                 $subject trim($subject);
  88.                 if (substr(strtolower($subject)04!= 'fwd:'{
  89.                     $subject 'Fwd: ' $subject;
  90.                 }
  91.  
  92.                 array_shift($body_a);
  93.                 array_pop($body_a);
  94.                 $body implode(''$body_a);
  95.                 $body .= "\r\n";
  96.  
  97.                 $localfilename GenerateRandomString(32'FILE'7);
  98.                 $full_localfilename "$hashed_attachment_dir/$localfilename";
  99.                 while (file_exists($full_localfilename)) {
  100.                     $localfilename GenerateRandomString(32'FILE'7);
  101.                     $full_localfilename "$hashed_attachment_dir/$localfilename";
  102.                 }
  103.  
  104.                 $fp fopen$full_localfilename'wb');
  105.                 fwrite ($fp$body);
  106.                 fclose($fp);
  107.                 $composeMessage->initAttachment('message/rfc822',$filename,
  108.                      $localfilename);
  109.                 $composeMessage->rfc822_header->subject $subject;
  110.             }
  111.             $j++;
  112.         }
  113.         $i++;
  114.     }
  115.  
  116.     sqgetGlobalVar('compose_messages'$compose_messagesSQ_SESSION);
  117.     if (!isset($compose_messages)
  118.      || !is_array($compose_messages)) {
  119.         $compose_messages array();
  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_FORM)) {
  154.     $submitted_token '';
  155. }
  156. /* end of get globals */
  157.  
  158. // security check
  159. sm_validate_security_token($submitted_token-1TRUE);
  160.  
  161. global $imap_stream_options// in case not defined in config
  162. $imapConnection sqimap_login($username$key$imapServerAddress$imapPort0$imap_stream_options);
  163. $mbx_response=sqimap_mailbox_select($imapConnection$mailbox);
  164.  
  165. $location set_url_var($location,'composenew',0,false);
  166. $location set_url_var($location,'composesession',0,false);
  167. $location set_url_var($location,'session',0,false);
  168. $location set_url_var($location,'preselected',NULL,false,true);
  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)) {
  200.     $cnt sqimap_mailbox_expunge($imapConnection$mailboxtrue);
  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  {
  225.                 sqimap_msgs_list_delete($imapConnection$mailbox$id);
  226.                 if ($auto_expunge{
  227.                     $cnt sqimap_mailbox_expunge($imapConnection$mailboxtrue);
  228.                 }
  229.                 if (($startMessage+$cnt-1>= $mbx_response['EXISTS']{
  230.                     if ($startMessage $show_num{
  231.                         $location set_url_var($location,'startMessage',$startMessage-$show_numfalse);
  232.                     else {
  233.                         $location set_url_var($location,'startMessage',1false);
  234.                     }
  235.                 }
  236.             }
  237.         else {
  238.             $composesession attachSelectedMessages($id$imapConnection);
  239.             $location set_url_var($location'session'$composesessionfalse);
  240.             $location set_url_var($location'forward_as_attachment_init'1false);
  241.             if ($compose_new_win{
  242.                 $location set_url_var($location'composenew'1false);
  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{
  257.                 $cnt sqimap_mailbox_expunge($imapConnection$mailboxtrue);
  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_numfalse);
  265.                 else {
  266.                     $location set_url_var($location,'startMessage',1false);
  267.                 }
  268.             }
  269.         }
  270.     else {
  271.         $exception true;
  272.     }
  273. }
  274. // Log out this session
  275. sqimap_logout($imapConnection);
  276. if ($exception{
  277.     displayPageHeader($color$mailbox);
  278.     error_message(_("No messages were selected.")$mailbox$sort$startMessage$color);
  279. else {
  280.     header("Location: $location");
  281.     exit;
  282. }
  283. ?>
  284. </body></html>

Documentation generated on Mon, 13 Jan 2020 04:25:01 +0100 by phpDocumentor 1.4.3