Some code cleanups to read_body.php
[squirrelmail.git] / plugins / filters / filters.php
index 887d2c87d1995b98dc2bdff2d9d9863c5c1a786c..4d88d6f7510d979be53537ca79b785daa57f06f6 100644 (file)
@@ -52,11 +52,11 @@ function filters_SaveCache () {
     fclose($fp);
 }
 
-
 function filters_LoadCache () {
     global $data_dir, $SpamFilters_DNScache;
 
     if (file_exists($data_dir . "/dnscache")) {
+        $SpamFilters_DNScache = array();
         if ($fp = fopen ($data_dir . "/dnscache", "r")) {
             flock($fp,LOCK_SH);
             while ($data=fgetcsv($fp,1024)) {
@@ -73,7 +73,8 @@ function filters_LoadCache () {
 
 function filters_bulkquery($filters_spam_scan, $filters, $read) {
     global $SpamFilters_YourHop, $attachment_dir, $username,
-           $SpamFilters_DNScache, $SpamFilters_BulkQuery;
+           $SpamFilters_DNScache, $SpamFilters_BulkQuery,
+           $SpamFilters_CacheTTL;
 
     $IPs = array();
     $i = 0;
@@ -87,14 +88,6 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
         $MsgNum = $Chunks[1];
 
         $i ++;
-        $Scan = 1;
-
-        // Check for normal IMAP servers
-        if ($filters_spam_scan == 'new') {
-            if (is_int(strpos($Chunks[4], '\Seen'))) {
-                $Scan = 0;
-            }
-        }
 
         // Look through all of the Received headers for IP addresses
         // Stop when I get ")" on a line
@@ -106,32 +99,28 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
             // Check to see if this line is the right "Received from" line
             // to check
             if (is_int(strpos($read[$i], $SpamFilters_YourHop))) {
-
-                // short-circuit and skip work if we don't scan this one
-                if ($Scan) {
-                    $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
-                    $elements = explode(' ', $read[$i]);
-                    foreach ($elements as $value) {
-                        if ($value != '' &&
-                            ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
-                                $value, $regs)) {
-                            $Chunks = explode('.', $value);
-                            $IP = $Chunks[3] . '.' . $Chunks[2] . '.' .
-                                  $Chunks[1] . '.' . $Chunks[0];
-                            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;
-                                    }
+                $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
+                $elements = explode(' ', $read[$i]);
+                foreach ($elements as $value) {
+                    if ($value != '' &&
+                        ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
+                            $value, $regs)) {
+                        $Chunks = explode('.', $value);
+                        $IP = $Chunks[3] . '.' . $Chunks[2] . '.' .
+                              $Chunks[1] . '.' . $Chunks[0];
+                        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;
                                 }
                             }
-                            // If we've checked one IP and YourHop is
-                            // just a space
-                            if ($SpamFilters_YourHop == ' ') {
-                                break;  // don't check any more
-                            }
+                        }
+                        // If we've checked one IP and YourHop is
+                        // just a space
+                        if ($SpamFilters_YourHop == ' ') {
+                            break;  // don't check any more
                         }
                     }
                 }
@@ -152,6 +141,7 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
 
         $bqfil = $attachment_dir . $username . "-bq.in";
         $fp = fopen($bqfil, "w");
+        fputs ($fp, $SpamFilters_CacheTTL . "\n");
         foreach ($rbls as $key => $value) {
             fputs ($fp, "." . $key . "\n");
         }
@@ -171,92 +161,10 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
     }
 }
 
-
-function filters_sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
-    global $color, $squirrelmail_language, $imap_general_debug;
-
-    $data = array();
-    $size = 0;
-
-    do {
-        $read = fgets($imap_stream, 9096);
-        if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) {
-            break;  // found end of reply
-        }
-
-        // Continue if needed for this single line
-        while (strpos($read, "\n") === false) {
-            $read .= fgets($imap_stream, 9096);
-        }
-
-        $data[] = $read;
-
-        if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
-            $size = $regs[1];
-            if ($imap_general_debug) {
-                echo "<small><tt><font color=\"#CC0000\">Size is $size</font></tt></small><br>\n";
-            }
-
-            $total_size = 0;
-            do {
-                $read = fgets($imap_stream, 9096);
-                if ($imap_general_debug) {
-                    echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
-                    flush();
-                }
-                $data[] = $read;
-                $total_size += strlen($read);
-            } while ($total_size < $size);
-
-            $size = 0;
-        }
-        // For debugging purposes
-        if ($imap_general_debug) {
-            echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n";
-            flush();
-        }
-    } while (true);
-
-    $response = $regs[1];
-    $message = trim($regs[2]);
-
-    if ($imap_general_debug) {
-        echo '--<br>';
-    }
-
-    if (!$handle_errors) {
-        return $data;
-    }
-
-    if ($response == 'NO') {
-        // ignore this error from m$ exchange, it is not fatal (aka bug)
-        if (strstr($message, 'command resulted in') === false) {
-            set_up_language($squirrelmail_language);
-            echo "<br><b><font color=$color[2]>\n" .
-                 _("ERROR : Could not complete request.") .
-                 "</b><br>\n" .
-                 _("Reason Given: ") .
-                 $message . "</font><br>\n";
-            exit;
-        }
-    } else if ($response == 'BAD') {
-        set_up_language($squirrelmail_language);
-        echo "<br><b><font color=$color[2]>\n" .
-             _("ERROR : Bad or malformed request.") .
-             "</b><br>\n" .
-             _("Server responded: ") .
-             $message . "</font><br>\n";
-        exit;
-    }
-
-    return $data;
-}
-
-
 function start_filters() {
     global $mailbox, $username, $key, $imapServerAddress, $imapPort, $imap,
-        $imap_general, $filters, $imap_stream, $imapConnection,
-    $UseSeparateImapConnection, $AllowSpamFilters;
+           $imap_general, $filters, $imap_stream, $imapConnection,
+           $UseSeparateImapConnection, $AllowSpamFilters;
 
 #    if ($mailbox == 'INBOX') {
         // Detect if we have already connected to IMAP or not.
@@ -290,13 +198,14 @@ function start_filters() {
 #    }
 }
 
-
 function user_filters($imap_stream) {
+    global $data_dir, $username;
     $filters = load_filters();
     if (! $filters) return;
+    $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
 
     sqimap_mailbox_select($imap_stream, 'INBOX');
-
+    $id = array();
     // For every rule
     for ($i=0; $i < count($filters); $i++) {
         // If it is the "combo" rule
@@ -305,38 +214,53 @@ function user_filters($imap_stream) {
             *  If it's "TO OR CC", we have to do two searches, one for TO
             *  and the other for CC.
             */
-            filter_search_and_delete($imap_stream, 'TO',
-            $filters[$i]['what'], $filters[$i]['folder']);
-            filter_search_and_delete($imap_stream, 'CC',
-            $filters[$i]['what'], $filters[$i]['folder']);
+            $id = filter_search_and_delete($imap_stream, 'TO',
+                  $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $id);
+            $id = filter_search_and_delete($imap_stream, 'CC',
+                  $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $id);
         } else {
             /*
             *  If it's a normal TO, CC, SUBJECT, or FROM, then handle it
             *  normally.
             */
-            filter_search_and_delete($imap_stream, $filters[$i]['where'],
-            $filters[$i]['what'], $filters[$i]['folder']);
+            $id = filter_search_and_delete($imap_stream, $filters[$i]['where'],
+                 $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $id);
         }
     }
     // Clean out the mailbox whether or not auto_expunge is on
     // That way it looks like it was redirected properly
-    sqimap_mailbox_expunge($imap_stream, 'INBOX');
+    if (count($id)) {
+        sqimap_mailbox_expunge($imap_stream, 'INBOX');
+    }
 }
 
-function filter_search_and_delete($imap, $where, $what, $where_to) {
-    global $languages, $squirrelmail_language;
-    if (isset($languages[$squirrelmail_language]['CHARSET']) &&
+function filter_search_and_delete($imap, $where, $what, $where_to, $user_scan, $del_id) {
+    global $languages, $squirrelmail_language, $allow_charset_search, $uid_support;
+    if ($user_scan == 'new') {
+        $category = 'UNSEEN';
+    } else {
+        $category = 'ALL';
+    }
+
+    if ($allow_charset_search &&
+        isset($languages[$squirrelmail_language]['CHARSET']) &&
         $languages[$squirrelmail_language]['CHARSET']) {
-        $search_str = "SEARCH CHARSET "
-            . strtoupper($languages[$squirrelmail_language]['CHARSET']) 
-            . " ALL ";
+        $search_str = 'SEARCH CHARSET '
+                    . strtoupper($languages[$squirrelmail_language]['CHARSET'])
+                    . ' ' . $category;
     } else {
-        $search_str = "SEARCH CHARSET US-ASCII ALL ";
+        $search_str = 'SEARCH CHARSET US-ASCII ' . $category;
     }
-    $search_str .= $where . ' {' . strlen($what) . "}\r\n" . $what . "\r\n";
-    
-    fputs ($imap, "a001 $search_str");
-    $read = filters_sqimap_read_data ($imap, 'a001', true, $response, $message);
+    if ($where == "Header") {
+        $what  = explode(':', $what);
+        $where = trim($where . ' ' . $what[0]);
+        $what  = addslashes(trim($what[1]));
+    }
+    $search_str .= ' ' . $where . ' {' . strlen($what) . "}\r\n"
+                 . $what . "\r\n";
+
+    /* read data back from IMAP */
+    $read = sqimap_run_command($imap, $search_str, true, $response, $message, $uid_support);
 
     // This may have problems with EIMS due to it being goofy
 
@@ -347,16 +271,18 @@ function filter_search_and_delete($imap, $where, $what, $where_to) {
         if (sqimap_mailbox_exists($imap, $where_to)) {
             for ($j=2; $j < count($ids); $j++) {
                 $id = trim($ids[$j]);
+                $del_id[] = $id;
                 sqimap_messages_copy ($imap, $id, $id, $where_to);
-                sqimap_messages_flag ($imap, $id, $id, 'Deleted');
+                sqimap_messages_flag ($imap, $id, $id, 'Deleted',false);
             }
         }
     }
+    return $del_id;
 }
 
 // These are the spam filters
 function spam_filters($imap_stream) {
-    global $data_dir, $username;
+    global $data_dir, $username, $uid_support;
     global $SpamFilters_YourHop;
     global $SpamFilters_DNScache;
     global $SpamFilters_SharedCache;
@@ -387,13 +313,30 @@ function spam_filters($imap_stream) {
 
     // Ask for a big list of all "Received" headers in the inbox with
     // flags for each message.  Kinda big.
-    fputs($imap_stream, 'A3999 FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
-        "(RECEIVED)])\r\n");
-
-    $read = filters_sqimap_read_data ($imap_stream, 'A3999', true,
-                                    $response, $message);
-
-    if ($response != 'OK') {
+    if ($filters_spam_scan != 'new') {
+        $read = sqimap_run_command($imap_stream, 'FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
+            '(RECEIVED)])', true, $reponse, $message, $uid_support);
+    } else {
+        $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $reponse, $message, $uid_support);
+        if ($response != 'OK' || trim($read[0]) == '* SEARCH') {
+           $read = sqimap_run_command($imap_stream, 'FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
+            '(RECEIVED)])', true, $reponse, $message, $uid_support);
+        } else {
+           $read[0] = trim($read[0]);
+            $i = 0;
+            $imap_query = $sid.' FETCH ';
+            $Chunks = explode(' ', $read[0]);
+            for ($i=2; $i < (count($Chunks)-1) ; $i++) {
+                $imap_query .= $Chunks[$i].',';
+            }
+            $imap_query .= $Chunks[count($Chunks)-1];
+            $imap_query .= ' (FLAGS BODY.PEEK[HEADER.FIELDS ';
+            $imap_query .= '(RECEIVED)])';//\r\n";
+           $read = sqimap_run_command($imap_stream,$imap_query, true, $reponse, $message, $uid_support);
+        }
+    }
+    
+    if (isset($response) && $response != 'OK') {
         return;
     }
 
@@ -414,14 +357,6 @@ function spam_filters($imap_stream) {
         $IPs = array();
         $i ++;
         $IsSpam = 0;
-        $Scan = 1;
-
-        // Check for normal IMAP servers
-        if ($filters_spam_scan == 'new') {
-            if (is_int(strpos($Chunks[4], '\Seen'))) {
-                $Scan = 0;
-            }
-        }
 
         // Look through all of the Received headers for IP addresses
         // Stop when I get ")" on a line
@@ -435,25 +370,23 @@ function spam_filters($imap_stream) {
             if (is_int(strpos($read[$i], $SpamFilters_YourHop))) {
 
                 // short-circuit and skip work if we don't scan this one
-                if ($Scan) {
-                    $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
-                    $elements = explode(' ', $read[$i]);
-                    foreach ($elements as $value) {
-                        if ($value != '' &&
-                            ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
-                                $value, $regs)) {
-                            $Chunks = explode('.', $value);
-                            if (filters_spam_check_site($Chunks[0],
-                                    $Chunks[1], $Chunks[2], $Chunks[3],
-                                    $filters)) {
-                                $IsSpam ++;
-                                break;  // no sense in checking more IPs
-                            }
-                            // If we've checked one IP and YourHop is
-                            // just a space
-                            if ($SpamFilters_YourHop == ' ') {
-                                break;  // don't check any more
-                            }
+                $read[$i] = ereg_replace('[^0-9\.]', ' ', $read[$i]);
+                $elements = explode(' ', $read[$i]);
+                foreach ($elements as $value) {
+                    if ($value != '' &&
+                        ereg('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',
+                            $value, $regs)) {
+                        $Chunks = explode('.', $value);
+                        if (filters_spam_check_site($Chunks[0],
+                                $Chunks[1], $Chunks[2], $Chunks[3],
+                                $filters)) {
+                            $IsSpam ++;
+                            break;  // no sense in checking more IPs
+                        }
+                        // If we've checked one IP and YourHop is
+                        // just a space
+                        if ($SpamFilters_YourHop == ' ') {
+                            break;  // don't check any more
                         }
                     }
                 }
@@ -467,7 +400,7 @@ function spam_filters($imap_stream) {
                 sqimap_messages_copy ($imap_stream, $MsgNum, $MsgNum,
                                     $filters_spam_folder);
                 sqimap_messages_flag ($imap_stream, $MsgNum, $MsgNum,
-                                    'Deleted');
+                                    'Deleted', false);
             }
         } else {
         }
@@ -480,8 +413,8 @@ function spam_filters($imap_stream) {
     } else {
        session_register('SpamFilters_DNScache');
     }
-}
 
+}
 
 // Does the loop through each enabled filter for the specified IP address.
 // IP format:  $a.$b.$c.$d
@@ -494,9 +427,9 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
                                 $filters[$key]['dns'];
                 if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) {
                     $SpamFilters_DNScache[$filter_revip]['L'] =
-                                        gethostbyname($filter_revip);
+                                    gethostbyname($filter_revip);
                     $SpamFilters_DNScache[$filter_revip]['T'] =
-                                           time() + $SpamFilters_CacheTTL;
+                                       time() + $SpamFilters_CacheTTL;
                 }
                 if ($SpamFilters_DNScache[$filter_revip]['L'] ==
                     $filters[$key]['result']) {
@@ -510,6 +443,7 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
 
 function load_filters() {
     global $data_dir, $username;
+
     $filters = array();
     for ($i=0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
         $ary = explode(',', $fltr);
@@ -829,6 +763,30 @@ function load_spam_filters() {
     $filters['NJABL DUL']['comment'] =
         _("FREE, for now - Not Just Another Blacklist - Dial-up IPs.");
 
+    $filters['Conf DSBL.ORG Relay']['prefname'] = 'filters_spam_dsbl_conf_ss';
+    $filters['Conf DSBL.ORG Relay']['name'] = 'DSBL.org Confirmed Relay List';
+    $filters['Conf DSBL.ORG Relay']['link'] = 'http://www.dsbl.org/';
+    $filters['Conf DSBL.ORG Relay']['dns'] = 'list.dsbl.org';
+    $filters['Conf DSBL.ORG Relay']['result'] = '127.0.0.2';
+    $filters['Conf DSBL.ORG Relay']['comment'] =
+        _("FREE - Distributed Sender Boycott List - Confirmed Relays");
+
+    $filters['Conf DSBL.ORG Multi-Stage']['prefname'] = 'filters_spam_dsbl_conf_ms';
+    $filters['Conf DSBL.ORG Multi-Stage']['name'] = 'DSBL.org Confirmed Multi-Stage Relay List';
+    $filters['Conf DSBL.ORG Multi-Stage']['link'] = 'http://www.dsbl.org/';
+    $filters['Conf DSBL.ORG Multi-Stage']['dns'] = 'multihop.dsbl.org';
+    $filters['Conf DSBL.ORG Multi-Stage']['result'] = '127.0.0.2';
+    $filters['Conf DSBL.ORG Multi-Stage']['comment'] =
+        _("FREE - Distributed Sender Boycott List - Confirmed Multi-stage Relays");
+
+    $filters['UN-Conf DSBL.ORG']['prefname'] = 'filters_spam_dsbl_unc';
+    $filters['UN-Conf DSBL.ORG']['name'] = 'DSBL.org UN-Confirmed Relay List';
+    $filters['UN-Conf DSBL.ORG']['link'] = 'http://www.dsbl.org/';
+    $filters['UN-Conf DSBL.ORG']['dns'] = 'unconfirmed.dsbl.org';
+    $filters['UN-Conf DSBL.ORG']['result'] = '127.0.0.2';
+    $filters['UN-Conf DSBL.ORG']['comment'] =
+        _("FREE - Distributed Sender Boycott List - UN-Confirmed Relays");
+
     foreach ($filters as $Key => $Value) {
         $filters[$Key]['enabled'] = getPref($data_dir, $username,
             $filters[$Key]['prefname']);