Happy New Year
[squirrelmail.git] / plugins / filters / filters.php
index 5e150b0ef5f74585010610dff9507d5680bc8bf9..6092b80b0b9c0c89daabfb5866f883498840d48e 100644 (file)
@@ -3,13 +3,14 @@
 /**
  * Message and Spam Filter Plugin - Filtering Functions
  *
- * @copyright © 1999-2009 The SquirrelMail Project Team
+ * @copyright 1999-2020 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package plugins
  * @subpackage filters
  */
 
+// TODO: This plugin has an issue that seems to corrupt folder cache for subfolders of INBOX
 /**
  * do not allow to call this file directly
  */
@@ -142,7 +143,7 @@ function filters_LoadCache () {
 
 /**
  * Uses the BulkQuery executable to query all the RBLs at once
- * @param array $filters Array of SPAM Fitlers
+ * @param array $filters Array of SPAM Filters
  * @param array $IPs Array of IP Addresses
  * @access private
  */
@@ -190,9 +191,26 @@ function filters_bulkquery($filters, $IPs) {
  * @access private
  */
 function start_filters($hook_args) {
-    global $imapServerAddress, $imapPort, $imap_stream, $imapConnection,
-           $UseSeparateImapConnection, $AllowSpamFilters, $filter_inbox_count,
-           $username;
+    global $imapServerAddress, $imapPort, $imap_stream_options, $imap_stream,
+           $imapConnection, $UseSeparateImapConnection, $AllowSpamFilters,
+           $filter_inbox_count, $username;
+
+    // if there were filtering errors previously during
+    // this login session, we won't try again
+    //
+    // (errors that this plugin was able to catch or a "NO"
+    // response/failure from IMAP found in the current session,
+    // which could have resulted from an attempted filter copy
+    // (over quota), in which case execution halts before this
+    // plugin can catch the problem -- note, however, that any
+    // other IMAP "NO" failure (caused by unrelated actions) at
+    // any time during the current session will cause this plugin
+    // to effectively shut down)
+    //
+    sqgetGlobalVar('filters_error', $filters_error, SQ_SESSION, FALSE);
+    sqgetGlobalVar('IMAP_FATAL_ERROR_TYPE', $imap_fatal_error, SQ_SESSION, '');
+    if ($filters_error || $imap_fatal_error == 'NO')
+        return;
 
     /**
      * check hook that calls filtering. If filters are called by right_main_after_header,
@@ -218,6 +236,7 @@ function start_filters($hook_args) {
         }
     }
 
+    // No user filters, and no spam filters, no need to continue //
     if (!$AllowSpamFilters && empty($filters)) {
         return;
     }
@@ -228,7 +247,7 @@ function start_filters($hook_args) {
     if ((!isset($imap_stream) && !isset($imapConnection)) ||
         $UseSeparateImapConnection ) {
             $stream = sqimap_login($username, false, $imapServerAddress,
-                                $imapPort, 10);
+                                $imapPort, 10, $imap_stream_options);
             $previously_connected = false;
     } else if (isset($imapConnection)) {
         $stream = $imapConnection;
@@ -311,12 +330,12 @@ function user_filters($imap_stream) {
 
 /**
  * Creates and runs the IMAP command to filter messages
+ * @param string $imap_stream TODO: Document this parameter
  * @param string $where Which part of the message to search (TO, CC, SUBJECT, etc...)
  * @param string $what String to search for
  * @param string $where_to Folder it will move to
  * @param string $user_scan Whether to search all or just unseen
  * @param string $should_expunge
- * @param boolean $where Which part of location to search
  * @access private
  */
 function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_scan,
@@ -354,7 +373,7 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
 
     // see comments in squirrelmail sqimap_search function
     if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
-         $search_str .= ' ' . $where . ' ' . $what;
+        $search_str .= ' ' . $where . ' ' . $what;
         /* read data back from IMAP */
         $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
     } else {
@@ -386,8 +405,16 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
         }
         if ($response == 'OK' && count($ids)) {
             if (sqimap_mailbox_exists($imap_stream, $where_to)) {
-                 $should_expunge = true;
-                 sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false);
+                if (!sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false)) {
+                    // if errors occurred, don't try to filter again during this session
+                    sqsession_register(TRUE, 'filters_error');
+                    global $color;
+                    error_box(_("A problem occurred filtering messages. Check filter settings and account quota if applicable. Filtering is disabled for the remainder of this login session."), $color);
+                }
+
+                // expunge even in the case of errors, in case some
+                // messages were filtered before the error happened
+                $should_expunge = true;
             }
         } elseif ($response != 'OK') {
             $query = $search_str . "\r\n".$what ."\r\n";
@@ -451,7 +478,7 @@ function spam_filters($imap_stream) {
             for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
                 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
                     $search_array = explode(' ', trim($regs[1]));
-                break;
+                    break;
                 }
             }
         }
@@ -492,14 +519,15 @@ function spam_filters($imap_stream) {
                             $aSpamIds[] = $MsgNum;
                             $isspam = true;
                         }
+
                         if ($bulkquery) {
                             array_shift($aMatch);
-                            $IP = explode('.',$aMatch);
+                            $IP = explode('.', $aMatch);
                             foreach ($filters as $key => $value) {
                                 if ($filters[$key]['enabled'] && $filters[$key]['dns']) {
                                     if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) {
-                                       $IPs[$IP] = true;
-                                       break;
+                                        $IPs[$IP] = true;
+                                        break;
                                     }
                                 }
                             }
@@ -516,7 +544,15 @@ function spam_filters($imap_stream) {
     }
     // Lookie!  It's spam!  Yum!
     if (count($aSpamIds) && sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
-        sqimap_msgs_list_move ($imap_stream, $aSpamIds, $filters_spam_folder);
+        if (!sqimap_msgs_list_move($imap_stream, $aSpamIds, $filters_spam_folder)) {
+           // if errors occurred, don't try to filter again during this session
+           sqsession_register(TRUE, 'filters_error');
+           global $color;
+           error_box(_("A problem occurred filtering messages. Check filter settings and account quota if applicable. Filtering is disabled for the remainder of this login session."), $color);
+        }
+
+        // expunge even in the case of errors, in case some
+        // messages were filtered before the error happened
         sqimap_mailbox_expunge($imap_stream, 'INBOX');
     }
 
@@ -547,9 +583,11 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
     foreach ($filters as $key => $value) {
         if ($filters[$key]['enabled']) {
             if ($filters[$key]['dns']) {
+
                 /**
-                 * RFC allows . on end of hostname to force domain lookup
-                 * to not use search domain from resolv.conf
+                 * RFC allows . on end of hostname to force domain lookup to
+                 * not use search domain from resolv.conf, i.e. to ensure
+                 * search domain isn't used if no hostname is found
                  */
                 $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
                                 $filters[$key]['dns'] . '.';
@@ -569,7 +607,7 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
 
                 /**
                  * gethostbyname returns ip if resolved, or returns original
-                 * host query if no resolution
+                 * host supplied to function if there is no resolution
                  */
                 if ($SpamFilters_DNScache[$filter_revip]['L'] != $filter_revip) {
                     return 1;
@@ -887,7 +925,6 @@ function filter_swap($id1, $id2) {
  * @access private
  */
 function update_for_folder ($args) {
-
     $old_folder = $args[0];
     $new_folder = $args[2];
     $action = $args[1];