Fix notice when image is missing
[squirrelmail.git] / functions / imap_mailbox.php
index d02f20e804c02314f6e5fa18100afa2e3f9a158f..684b34b0fdd18987982b714528b62c584600fd7c 100755 (executable)
@@ -5,7 +5,7 @@
  *
  * This implements all functions that manipulate mailboxes
  *
- * @copyright © 1999-2005 The SquirrelMail Project Team
+ * @copyright © 1999-2006 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -39,7 +39,7 @@ class mailboxes {
     var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
         $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
         $is_trash = false, $is_draft = false,  $mbxs = array(),
-        $unseen = false, $total = false;
+        $unseen = false, $total = false, $recent = false;
 
     function addMbx($mbx, $delimiter, $start, $specialfirst) {
         $ary = explode($delimiter, $mbx->mailboxname_full);
@@ -317,7 +317,7 @@ function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true,
  *
  * @param stream $imap_stream imap connection resource
  * @param string $mailbox mailbox name
- * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from 
+ * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from
  *  sqimap_get_mailboxes() (to avoid having to talk to imap server)
  * @return boolean
  * @since 1.0 or older
@@ -350,10 +350,12 @@ function sqimap_mailbox_exists ($imap_stream, $mailbox, $mailboxlist=null) {
  * @since 1.0 or older
  */
 function sqimap_mailbox_select ($imap_stream, $mailbox) {
+    // FIX ME: WHAAAA DO NOT USE "None" for something that does not exist. Use false or NULL instead
     if ($mailbox == 'None') {
         return;
     }
-
+    // cleanup $mailbox in order to prevent IMAP injection attacks
+    $mailbox = str_replace(array("\r","\n"), array("",""),$mailbox);
     $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
                                true, $response, $message);
     $result = array();
@@ -557,7 +559,7 @@ function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  * Before 1.2.0 used third argument for delimiter.
  *
  * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use
- * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags 
+ * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags
  * array (for example, "\NoSelect" in addition to "noselect")
  * @param array $line
  * @return array
@@ -771,7 +773,7 @@ function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=tr
      * There are three main listing commands we can use in IMAP:
      * LSUB        shows just the list of subscribed folders
      *            may include flags, but these are not necessarily accurate or authoratative
-     *            \NoSelect has special meaning: the folder does not exist -OR- it means this 
+     *            \NoSelect has special meaning: the folder does not exist -OR- it means this
      *            folder is not subscribed but children may be
      *            [RFC-2060]
      * LIST        this shows every mailbox on the system
@@ -816,6 +818,7 @@ function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=tr
         sqgetGlobalVar($sub_cache_name,$lsub_cache,SQ_SESSION);
     }
 
+    $lsub_assoc_ary=array();
     if (!empty($lsub_cache)) {
         $lsub_assoc_ary=$lsub_cache;
     } else {
@@ -840,7 +843,7 @@ function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=tr
         // and NOT a LSUB, so no need to do it again
         $list_assoc_ary  = $lsub_assoc_ary;
     } else {
-        // we did a LSUB so now we need to do a LIST            
+        // we did a LSUB so now we need to do a LIST
         // first see if it is in cache
         $list_cache_name='list_cache';
         if (!$force) {
@@ -944,7 +947,7 @@ function sqimap_get_mailboxes($imap_stream,$force=false,$show_only_subscribed=tr
             break;
         }
     }
-    
+
     if ($has_inbox == false) {
         // do a list request for inbox because we should always show
         // inbox even if the user isn't subscribed to it.
@@ -1036,14 +1039,14 @@ function sqimap_mailbox_tree($imap_stream,$lsub_ary) {
         if (in_array('\HasNoChildren',$flags)) { $noinferiors=1; }
 
         $noselect=0;
-        if (in_array('\NoSelect',$flags)) { $noselect=1; } 
+        if (in_array('\NoSelect',$flags)) { $noselect=1; }
         /**
          * LIST (SUBSCRIBED) has two new flags, \NonExistent which means the mailbox is subscribed to
          * but doesn't exist, and \PlaceHolder which is similar (but not the same) as \NoSelect
          * For right now, we'll treat these the same as \NoSelect and this behavior can be changed
          * later if needed
          */
-        if (in_array('\NonExistent',$flags)) { $noselect=1; } 
+        if (in_array('\NonExistent',$flags)) { $noselect=1; }
         if (in_array('\PlaceHolder',$flags)) { $noselect=1; }
         $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
     }
@@ -1089,7 +1092,6 @@ function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
     $trail_del = false;
     $start = 0;
 
-
     if (isset($folder_prefix) && ($folder_prefix != '')) {
         $start = substr_count($folder_prefix,$delimiter);
         if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
@@ -1202,13 +1204,13 @@ function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
                 $mbx = $oMbx->mailboxname_full;
                 if ($unseen_type == 2 ||
                    ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
-                   $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
+                   $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN RECENT)';
                 } else {
-                   $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
+                   $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN RECENT)';
                 }
                 sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
             } else {
-                $oMbx->unseen = $oMbx->total = false;
+                $oMbx->unseen = $oMbx->total = $oMbx->recent = false;
                 $tag = false;
             }
             $oMbx->tag = $tag;
@@ -1228,6 +1230,10 @@ function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
                 if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
                     $oMbx->total = $regs[1];
                 }
+                if (preg_match('/RECENT\s+([0-9]+)/i', $sResponse, $regs)) {
+                    $oMbx->recent = $regs[1];
+                }
+
            }
            unset($oMbx->tag);
         }
@@ -1242,6 +1248,7 @@ function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
                     $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
                     $oMbx->unseen = $aStatus['UNSEEN'];
                     $oMbx->total  = $aStatus['MESSAGES'];
+                    $oMbx->recent = $aStatus['RECENT'];
                 } else {
                     $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
                 }
@@ -1257,6 +1264,21 @@ function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
             }
         }
     }
+
+    $cnt = count($aMbxs);
+    for($i=0;$i<$cnt;++$i) {
+         $oMbx =& $aMbxs[$i];
+         unset($hook_status);
+         if (!empty($oMbx->unseen)) { $hook_status['UNSEEN']=$oMbx->unseen; }
+         if (!empty($oMbx->total)) { $hook_status['MESSAGES']=$oMbx->total; }
+         if (!empty($oMbx->recent)) { $hook_status['RECENT']=$oMbx->recent; }
+         if (!empty($hook_status))
+         {
+              $hook_status['MAILBOX']=$oMbx->mailboxname_full;
+              $hook_status['CALLER']='sqimap_get_status_mbx_tree'; // helps w/ debugging
+              do_hook_function('folder_status',$hook_status);
+         }
+    }
 }
 
 /**
@@ -1301,4 +1323,4 @@ function sqimap_mailbox_is_noinferiors($oImapStream,$sImapFolder,&$oBoxes) {
     return false;
 }
 
-?>
\ No newline at end of file
+?>