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-2014 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: move_messages.php 14436 2014-01-21 21:30:59Z 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_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 Sat, 02 Aug 2014 04:21:33 +0200 by phpDocumentor 1.4.3