Call me anal
[squirrelmail.git] / plugins / filters / filters.php
index fdc7fd8021015af11ab9fe6e5bf8c54c4e98869a..dc53c9abfb0d534ecb9c89c49fa73632233df4e0 100644 (file)
@@ -1,32 +1,94 @@
 <?php
+
 /**
  * Message and Spam Filter Plugin - Filtering Functions
  *
- * This plugin filters your inbox into different folders based upon given
- * criteria.  It is most useful for people who are subscibed to mailing lists
- * to help organize their messages.  The argument stands that filtering is
- * not the place of the client, which is why this has been made a plugin for
- * SquirrelMail.  You may be better off using products such as Sieve or
- * Procmail to do your filtering so it happens even when SquirrelMail isn't
- * running.
- *
- * If you need help with this, or see improvements that can be made, please
- * email me directly at the address above.  I definately welcome suggestions
- * and comments.  This plugin, as is the case with all SquirrelMail plugins,
- * is not directly supported by the developers.  Please come to me off the
- * mailing list if you have trouble with it.
- *
- * Also view plugins/README.plugins for more information.
- *
- * @version $Id$
- * @copyright (c) 1999-2004 The SquirrelMail Project Team
+ * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
  * @package plugins
  * @subpackage filters
  */
 
 /**
- * FIXME: Undocumented function
+ * do not allow to call this file directly
+ */
+if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) {
+    header("Location: ../../src/login.php");
+    die();
+}
+
+/** load globals */
+global $UseSeparateImapConnection,
+    $AllowSpamFilters, $SpamFilters_YourHop, $SpamFilters_ShowCommercial,
+    $SpamFilters_DNScache, $SpamFilters_BulkQuery, $SpamFilters_SharedCache,
+    $SpamFilters_CacheTTL;
+
+/**
+ * load required functions. Plugin depends on IMAP functions and they are not
+ * loaded in src/webmail.php
+ */
+include_once (SM_PATH . 'functions/imap.php');
+
+/** load default config */
+if (file_exists(SM_PATH . 'plugins/filters/config_default.php')) {
+    include_once (SM_PATH . 'plugins/filters/config_default.php');
+} else {
+    // default config was removed.
+    $UseSeparateImapConnection = false;
+    $AllowSpamFilters = true;
+    $SpamFilters_YourHop = ' ';
+    $SpamFilters_ShowCommercial = false;
+    $SpamFilters_DNScache = array();
+    $SpamFilters_BulkQuery = '';
+    $SpamFilters_SharedCache = true;
+    $SpamFilters_CacheTTL = 7200;
+}
+
+if (file_exists(SM_PATH . 'config/filters_config.php')) {
+    include_once (SM_PATH . 'config/filters_config.php');
+} elseif (file_exists(SM_PATH . 'plugins/filters/config.php')) {
+    include_once (SM_PATH . 'plugins/filters/config.php');
+}
+
+/**
+ * Register option blocks
+ * @access private
+ */
+function filters_optpage_register_block() {
+    global $optpage_blocks, $AllowSpamFilters;
+
+    $optpage_blocks[] = array(
+        'name' => _("Message Filters"),
+        'url'  => SM_PATH . 'plugins/filters/options.php',
+        'desc' => _("Filtering enables messages with different criteria to be automatically filtered into different folders for easier organization."),
+        'js'   => false
+    );
+
+    if ($AllowSpamFilters) {
+        $optpage_blocks[] = array(
+            'name' => _("SPAM Filters"),
+            'url'  => SM_PATH . 'plugins/filters/spamoptions.php',
+            'desc' => _("SPAM filters allow you to select from various DNS based blacklists to detect junk email in your INBOX and move it to another folder (like Trash)."),
+            'js'   => false
+        );
+    }
+}
+
+/* Receive the status of the folder and do something with it */
+function filters_folder_status($statusarr) {
+
+    global $filter_inbox_count;
+    if (empty($filter_inbox_count)) $filter_inbox_count=0;
+
+    if ($statusarr['MAILBOX'] == 'INBOX')
+    {
+     if (!empty($statusarr['MESSAGES'])) $filter_inbox_count=$statusarr['MESSAGES'];
+    }
+}
+
+/**
+ * Saves the DNS Cache to disk
  * @access private
  */
 function filters_SaveCache () {
@@ -45,7 +107,7 @@ function filters_SaveCache () {
        $fp = fopen($data_dir . '/dnscache', 'r');
        flock($fp,LOCK_EX);
     }
-    $fp1=fopen($data_dir . '/dnscache', 'w+');
+    $fp1 = fopen($data_dir . '/dnscache', 'w+');
 
     foreach ($SpamFilters_DNScache as $Key=> $Value) {
        $tstr = $Key . ',' . $Value['L'] . ',' . $Value['T'] . "\n";
@@ -57,7 +119,7 @@ function filters_SaveCache () {
 }
 
 /**
- * FIXME: Undocumented function
+ * Loads the DNS Cache from disk
  * @access private
  */
 function filters_LoadCache () {
@@ -67,20 +129,21 @@ function filters_LoadCache () {
         $SpamFilters_DNScache = array();
         if ($fp = fopen ($data_dir . '/dnscache', 'r')) {
             flock($fp,LOCK_SH);
-            while ($data=fgetcsv($fp,1024)) {
+            while ($data = fgetcsv($fp,1024)) {
                if ($data[2] > time()) {
                   $SpamFilters_DNScache[$data[0]]['L'] = $data[1];
                   $SpamFilters_DNScache[$data[0]]['T'] = $data[2];
                }
             }
-
             flock($fp,LOCK_UN);
         }
     }
 }
 
 /**
- * FIXME: Undocumented function
+ * Uses the BulkQuery executable to query all the RBLs at once
+ * @param array $filters Array of SPAM Fitlers
+ * @param array $IPs Array of IP Addresses
  * @access private
  */
 function filters_bulkquery($filters, $IPs) {
@@ -121,21 +184,50 @@ function filters_bulkquery($filters, $IPs) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Starts the filtering process
+ * @param array $hook_args (since 1.5.2) do hook arguments. Is used to check
+ * hook name, array key = 0.
  * @access private
  */
-function start_filters() {
+function start_filters($hook_args) {
     global $imapServerAddress, $imapPort, $imap_stream, $imapConnection,
-           $UseSeparateImapConnection, $AllowSpamFilters;
+           $UseSeparateImapConnection, $AllowSpamFilters, $filter_inbox_count,
+           $username;
+
+    /**
+     * check hook that calls filtering. If filters are called by right_main_after_header,
+     * do filtering only when we are in INBOX folder.
+     */
+    if ($hook_args[0]=='right_main_after_header' &&
+        (sqgetGlobalVar('mailbox',$mailbox,SQ_FORM) && $mailbox!='INBOX')) {
+        return;
+    }
+
+    $filters = load_filters();
+
+    // No point running spam filters if there aren't any to run //
+    if ($AllowSpamFilters) {
+        $spamfilters = load_spam_filters();
+
+        $AllowSpamFilters = false;
+        foreach($spamfilters as $value) {
+            if ($value['enabled'] == SMPREF_ON) {
+                $AllowSpamFilters = true;
+                break;
+            }
+        }
+    }
+
+    if (!$AllowSpamFilters && empty($filters)) {
+        return;
+    }
 
-    sqgetGlobalVar('username', $username, SQ_SESSION);
-    sqgetGlobalVar('key',      $key,      SQ_COOKIE);
 
     // Detect if we have already connected to IMAP or not.
     // Also check if we are forced to use a separate IMAP connection
     if ((!isset($imap_stream) && !isset($imapConnection)) ||
         $UseSeparateImapConnection ) {
-            $stream = sqimap_login($username, $key, $imapServerAddress,
+            $stream = sqimap_login($username, false, $imapServerAddress,
                                 $imapPort, 10);
             $previously_connected = false;
     } else if (isset($imapConnection)) {
@@ -145,9 +237,17 @@ function start_filters() {
         $previously_connected = true;
         $stream = $imap_stream;
     }
-    $aStatus = sqimap_status_messages ($stream, 'INBOX', array('MESSAGES'));
 
-    if ($aStatus['MESSAGES']) {
+    if (!isset($filter_inbox_count)) {
+        $aStatus = sqimap_status_messages ($stream, 'INBOX', array('MESSAGES'));
+        if (!empty($aStatus['MESSAGES'])) {
+            $filter_inbox_count=$aStatus['MESSAGES'];
+        } else {
+            $filter_inbox_count=0;
+        }
+    }
+
+    if ($filter_inbox_count > 0) {
         sqimap_mailbox_select($stream, 'INBOX');
         // Filter spam from inbox before we sort them into folders
         if ($AllowSpamFilters) {
@@ -164,7 +264,8 @@ function start_filters() {
 }
 
 /**
- * FIXME: Undocumented function
+ * Does the loop through each filter
+ * @param stream imap_stream the stream to read from
  * @access private
  */
 function user_filters($imap_stream) {
@@ -186,6 +287,12 @@ function user_filters($imap_stream) {
                   $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
             $expunge = filter_search_and_delete($imap_stream, 'CC',
                   $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
+        } else if ($filters[$i]['where'] == 'Header and Body') {
+            $expunge = filter_search_and_delete($imap_stream, 'TEXT',
+                  $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
+        } else if ($filters[$i]['where'] == 'Message Body') {
+            $expunge = filter_search_and_delete($imap_stream, 'BODY',
+                  $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
         } else {
             /*
             *  If it's a normal TO, CC, SUBJECT, or FROM, then handle it
@@ -203,18 +310,31 @@ function user_filters($imap_stream) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Creates and runs the IMAP command to filter messages
+ * @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,
                                   $should_expunge) {
     global $languages, $squirrelmail_language, $allow_charset_search, $imap_server_type;
 
+    //TODO: make use of new mailbox cache. See mailbox_display.phpinfo
+
+    if (strtolower($where_to) == 'inbox') {
+        return array();
+    }
+
     if ($user_scan == 'new') {
         $category = 'UNSEEN';
     } else {
         $category = 'ALL';
     }
+    $category .= ' UNDELETED';
 
     if ($allow_charset_search &&
         isset($languages[$squirrelmail_language]['CHARSET']) &&
@@ -227,31 +347,59 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
     }
     if ($where == 'Header') {
         $what  = explode(':', $what);
+        $where = strtoupper($where);
         $where = trim($where . ' ' . $what[0]);
         $what  = addslashes(trim($what[1]));
     }
 
-    if ($imap_server_type == 'macosx') {
-        $search_str .= ' ' . $where . ' ' . $what;
+    // see comments in squirrelmail sqimap_search function
+    if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
+         $search_str .= ' ' . $where . ' ' . $what;
+        /* read data back from IMAP */
+        $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
     } else {
-        $search_str .= ' ' . $where . ' {' . strlen($what) . "}\r\n"
-                    . $what . "\r\n";
+        $search_str .= ' ' . $where . ' {' . strlen($what) . "}";
+        $sid = sqimap_session_id(true);
+        fputs ($imap_stream, $sid . ' ' . $search_str . "\r\n");
+        $read2 = sqimap_fgets($imap_stream);
+        # server should respond with Ready for argument, then we will send search text
+        #echo "RR2 $read2<br>";
+        fputs ($imap_stream, "$what\r\n");
+        #echo "SS $what<br>";
+        $read2 = sqimap_fgets($imap_stream);
+        #echo "RR2 $read2<br>";
+        $read[]=$read2;
+        $read3 = sqimap_fgets($imap_stream);
+        #echo "RR3 $read3<br>";
+        list($rtag,$response,$message)=explode(' ',$read3,3);
+##        $read2 = sqimap_retrieve_imap_response($imap_stream, $sid, true,
+##              $response, $message, $search_str, false, true, false);
+        #echo "RR2 $read2 / RESPONSE $response<br>";
     }
 
-    /* read data back from IMAP */
-    $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
     if (isset($read[0])) {
         $ids = array();
-        for ($i=0,$iCnt=count($read);$i<$iCnt;++$i) {
+        for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
             if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
-                $ids = preg_split("/ /", trim($regs[1]));
-            break;
+                $ids += explode(' ', trim($regs[1]));
             }
         }
         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);
+                 sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false);
+            }
+        } elseif ($response != 'OK') {
+            $query = $search_str . "\r\n".$what ."\r\n";
+            if ($response == 'NO') {
+                if (strpos($message,'BADCHARSET') !== false ||
+                    strpos($message,'character') !== false) {
+                    sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
+                } else {
+                    sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
+                }
+            } else {
+                sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
             }
         }
     }
@@ -259,7 +407,8 @@ function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_
 }
 
 /**
- * FIXME: Undocumented function
+ * Loops through all the Received Headers to find IP Addresses
+ * @param stream imap_stream the stream to read from
  * @access private
  */
 function spam_filters($imap_stream) {
@@ -280,7 +429,7 @@ function spam_filters($imap_stream) {
 
     $run = false;
 
-    foreach ($filters as $Key => $Value) {
+    foreach ($filters as $Value) {
         if ($Value['enabled']) {
             $run = true;
             break;
@@ -299,9 +448,9 @@ function spam_filters($imap_stream) {
         $search_array = array();
         $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, TRUE);
         if (isset($read[0])) {
-            for ($i=0,$iCnt=count($read);$i<$iCnt;++$i) {
+            for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
                 if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
-                    $search_array = preg_split("/ /", trim($regs[1]));
+                    $search_array = explode(' ', trim($regs[1]));
                 break;
                 }
             }
@@ -383,9 +532,14 @@ function spam_filters($imap_stream) {
 }
 
 /**
- * FIXME: Undocumented function
  * Does the loop through each enabled filter for the specified IP address.
  * IP format:  $a.$b.$c.$d
+ * @param int $a First subset of IP
+ * @param int $b Second subset of IP
+ * @param int $c Third subset of IP
+ * @param int $d Forth subset of IP
+ * @param array $filters The Spam Filters
+ * @return boolean Whether the IP is Spam
  * @access private
  */
 function filters_spam_check_site($a, $b, $c, $d, &$filters) {
@@ -393,8 +547,12 @@ 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
+                 */
                 $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
-                                $filters[$key]['dns'];
+                                $filters[$key]['dns'] . '.';
 
                 if(!isset($SpamFilters_DNScache[$filter_revip]['L']))
                         $SpamFilters_DNScache[$filter_revip]['L'] = '';
@@ -408,8 +566,12 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
                     $SpamFilters_DNScache[$filter_revip]['T'] =
                                        time() + $SpamFilters_CacheTTL;
                 }
-                if ($SpamFilters_DNScache[$filter_revip]['L'] ==
-                    $filters[$key]['result']) {
+
+                /**
+                 * gethostbyname returns ip if resolved, or returns original
+                 * host query if no resolution
+                 */
+                if ($SpamFilters_DNScache[$filter_revip]['L'] != $filter_revip) {
                     return 1;
                 }
             }
@@ -419,14 +581,15 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Loads the filters from the user preferences
+ * @return array All the user filters
  * @access private
  */
 function load_filters() {
     global $data_dir, $username;
 
     $filters = array();
-    for ($i=0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
+    for ($i = 0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
         $ary = explode(',', $fltr);
         $filters[$i]['where'] = $ary[0];
         $filters[$i]['what'] = $ary[1];
@@ -436,7 +599,8 @@ function load_filters() {
 }
 
 /**
- * FIXME: Undocumented function
+ * Loads the Spam Filters and checks the preferences for the enabled status
+ * @return array All the spam filters
  * @access private
  */
 function load_spam_filters() {
@@ -492,14 +656,6 @@ function load_spam_filters() {
             _("COMMERCIAL - RBL+ Dial-up entries.");
     }
 
-    $filters['ORDB']['prefname'] = 'filters_spam_ordb';
-    $filters['ORDB']['name'] = 'Open Relay Database List';
-    $filters['ORDB']['link'] = 'http://www.ordb.org/';
-    $filters['ORDB']['dns'] = 'relays.ordb.org';
-    $filters['ORDB']['result'] = '127.0.0.2';
-    $filters['ORDB']['comment'] =
-        _("FREE - ORDB was born when ORBS went off the air. It seems to have fewer false positives than ORBS did though.");
-
     $filters['FiveTen Direct']['prefname'] = 'filters_spam_fiveten_src';
     $filters['FiveTen Direct']['name'] = 'Five-Ten-sg.com Direct SPAM Sources';
     $filters['FiveTen Direct']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
@@ -568,7 +724,7 @@ function load_spam_filters() {
     $filters['SPAMhaus']['name'] = 'SPAMhaus Lists';
     $filters['SPAMhaus']['link'] = 'http://www.spamhaus.org';
     $filters['SPAMhaus']['dns'] = 'sbl.spamhaus.org';
-    $filters['SPAMhaus']['result'] = '127.0.0.6';
+    $filters['SPAMhaus']['result'] = '127.0.0.2';
     $filters['SPAMhaus']['comment'] =
         _("FREE - SPAMhaus - A list of well-known SPAM sources.");
 
@@ -709,22 +865,21 @@ function load_spam_filters() {
         _("FREE - Distributed Sender Boycott List - UN-Confirmed Relays");
 
     foreach ($filters as $Key => $Value) {
-        $filters[$Key]['enabled'] = getPref($data_dir, $username,
-            $filters[$Key]['prefname']);
+        $filters[$Key]['enabled'] = (bool)getPref($data_dir, $username, $filters[$Key]['prefname']);
     }
 
     return $filters;
 }
 
 /**
- * FIXME: Undocumented function
+ * Removes a User filter
+ * @param int $id ID of the filter to remove
  * @access private
  */
 function remove_filter ($id) {
     global $data_dir, $username;
 
-    while ($nextFilter = getPref($data_dir, $username, 'filter' .
-        ($id + 1))) {
+    while ($nextFilter = getPref($data_dir, $username, 'filter' . ($id + 1))) {
         setPref($data_dir, $username, 'filter' . $id, $nextFilter);
         $id ++;
     }
@@ -733,7 +888,9 @@ function remove_filter ($id) {
 }
 
 /**
- * FIXME: Undocumented function
+ * Swaps two filters
+ * @param int $id1 ID of first filter to swap
+ * @param int $id2 ID of second filter to swap
  * @access private
  */
 function filter_swap($id1, $id2) {
@@ -749,20 +906,21 @@ function filter_swap($id1, $id2) {
 }
 
 /**
- * This update the filter rules when renaming or deleting folders
+ * This updates the filter rules when renaming or deleting folders
  * @param array $args
  * @access private
  */
 function update_for_folder ($args) {
+
     $old_folder = $args[0];
-        $new_folder = $args[2];
-        $action = $args[1];
+    $new_folder = $args[2];
+    $action = $args[1];
     global $data_dir, $username;
     $filters = array();
     $filters = load_filters();
     $filter_count = count($filters);
     $p = 0;
-    for ($i=0;$i<$filter_count;$i++) {
+    for ($i = 0; $i < $filter_count; $i++) {
         if (!empty($filters)) {
             if ($old_folder == $filters[$i]['folder']) {
                 if ($action == 'rename') {
@@ -792,4 +950,3 @@ function do_error($string) {
     echo $string;
     echo "</font></p>\n";
 }
-?>
\ No newline at end of file