Fixed wrong sqimap_run_command call. The $sid is added inside
[squirrelmail.git] / plugins / filters / filters.php
index 2f3ac1a59fa5eeb467f27ca025e6a7a626cda228..1300394e305c6cd54874544bad82ceb15eaf2251 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * Message and Spam Filter Plugin
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2003 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This plugin filters your inbox into different folders based upon given
  * $Id$
  */
 
-
 function filters_SaveCache () {
     global $data_dir, $SpamFilters_DNScache;
 
-    if (file_exists($data_dir . "/dnscache")) {
-        $fp = fopen($data_dir . "/dnscache", "r");
+    if (file_exists($data_dir . '/dnscache')) {
+        $fp = fopen($data_dir . '/dnscache', 'r');
     } else {
         $fp = false;
     }
     if ($fp) {
         flock($fp,LOCK_EX);
     } else {
-       $fp = fopen($data_dir . "/dnscache", "w+");
+       $fp = fopen($data_dir . '/dnscache', 'w+');
        fclose($fp);
-       $fp = fopen($data_dir . "/dnscache", "r");
+       $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";
@@ -53,13 +52,12 @@ function filters_SaveCache () {
     fclose($fp);
 }
 
-
 function filters_LoadCache () {
     global $data_dir, $SpamFilters_DNScache;
 
-    if (file_exists($data_dir . "/dnscache")) {
+    if (file_exists($data_dir . '/dnscache')) {
         $SpamFilters_DNScache = array();
-        if ($fp = fopen ($data_dir . "/dnscache", "r")) {
+        if ($fp = fopen ($data_dir . '/dnscache', 'r')) {
             flock($fp,LOCK_SH);
             while ($data=fgetcsv($fp,1024)) {
                if ($data[2] > time()) {
@@ -141,11 +139,11 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
             }
         }
 
-        $bqfil = $attachment_dir . $username . "-bq.in";
-        $fp = fopen($bqfil, "w");
+        $bqfil = $attachment_dir . $username . '-bq.in';
+        $fp = fopen($bqfil, 'w');
         fputs ($fp, $SpamFilters_CacheTTL . "\n");
         foreach ($rbls as $key => $value) {
-            fputs ($fp, "." . $key . "\n");
+            fputs ($fp, '.' . $key . "\n");
         }
         fputs ($fp, "----------\n");
         foreach ($IPs as $key => $value) {
@@ -153,7 +151,7 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
         }
         fclose ($fp);
         $bqout = array();
-        exec ($SpamFilters_BulkQuery . " < " . $bqfil, $bqout);
+        exec ($SpamFilters_BulkQuery . ' < ' . $bqfil, $bqout);
         foreach ($bqout as $value) {
             $Chunks = explode(',', $value);
             $SpamFilters_DNScache[$Chunks[0]]['L'] = $Chunks[1];
@@ -164,9 +162,12 @@ function filters_bulkquery($filters_spam_scan, $filters, $read) {
 }
 
 function start_filters() {
-    global $mailbox, $username, $key, $imapServerAddress, $imapPort, $imap,
-        $imap_general, $filters, $imap_stream, $imapConnection,
-    $UseSeparateImapConnection, $AllowSpamFilters;
+    global $mailbox, $imapServerAddress, $imapPort, $imap,
+           $imap_general, $filters, $imap_stream, $imapConnection,
+           $UseSeparateImapConnection, $AllowSpamFilters;
+
+    sqgetGlobalVar('username', $username, SQ_SESSION);
+    sqgetGlobalVar('key',      $key,      SQ_COOKIE);
 
 #    if ($mailbox == 'INBOX') {
         // Detect if we have already connected to IMAP or not.
@@ -200,7 +201,6 @@ function start_filters() {
 #    }
 }
 
-
 function user_filters($imap_stream) {
     global $data_dir, $username;
     $filters = load_filters();
@@ -210,7 +210,7 @@ function user_filters($imap_stream) {
     sqimap_mailbox_select($imap_stream, 'INBOX');
     $id = array();
     // For every rule
-    for ($i=0; $i < count($filters); $i++) {
+    for ($i=0, $num = count($filters); $i < $num; $i++) {
         // If it is the "combo" rule
         if ($filters[$i]['where'] == 'To or Cc') {
             /*
@@ -233,12 +233,14 @@ function user_filters($imap_stream) {
     // Clean out the mailbox whether or not auto_expunge is on
     // That way it looks like it was redirected properly
     if (count($id)) {
-       sqimap_mailbox_expunge($imap_stream, 'INBOX');
+        sqimap_mailbox_expunge($imap_stream, 'INBOX');
     }
 }
 
-function filter_search_and_delete($imap, $where, $what, $where_to, $user_scan, $del_id) {
-    global $languages, $squirrelmail_language, $allow_charset_search, $uid_support;
+function filter_search_and_delete($imap, $where, $what, $where_to, $user_scan, 
+                                  $del_id) {
+    global $languages, $squirrelmail_language, $allow_charset_search, 
+           $uid_support, $imap_server_type;
     if ($user_scan == 'new') {
         $category = 'UNSEEN';
     } else {
@@ -254,23 +256,30 @@ function filter_search_and_delete($imap, $where, $what, $where_to, $user_scan, $
     } else {
         $search_str = 'SEARCH CHARSET US-ASCII ' . $category;
     }
-    if ($where == "Header") {
+    if ($where == 'Header') {
         $what  = explode(':', $what);
         $where = trim($where . ' ' . $what[0]);
         $what  = addslashes(trim($what[1]));
     }
 
+    if ($imap_server_type == 'macosx') {    
+       $search_str .= ' ' . $where . ' ' . $what;
+    } else {
+       $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
 
-    for ($r=0; $r < count($read) &&
+    for ($r=0, $num = count($read); $r < $num &&
                 substr($read[$r], 0, 8) != '* SEARCH'; $r++) {}
     if ($response == 'OK') {
         $ids = explode(' ', $read[$r]);
         if (sqimap_mailbox_exists($imap, $where_to)) {
-            for ($j=2; $j < count($ids); $j++) {
+            for ($j=2, $num = count($ids); $j < $num; $j++) {
                 $id = trim($ids[$j]);
                 $del_id[] = $id;
                 sqimap_messages_copy ($imap, $id, $id, $where_to);
@@ -316,24 +325,23 @@ function spam_filters($imap_stream) {
     // flags for each message.  Kinda big.
     if ($filters_spam_scan != 'new') {
         $read = sqimap_run_command($imap_stream, 'FETCH 1:* (FLAGS BODY.PEEK[HEADER.FIELDS ' .
-            '(RECEIVED)])', true, $reponse, $message, $uid_support);
+            '(RECEIVED)])', true, $response, $message, $uid_support);
     } else {
-        $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $reponse, $message, $uid_support);
+        $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $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);
+            '(RECEIVED)])', true, $response, $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].',';
+            if (isset($read[0])) {
+                if (preg_match("/^\* SEARCH (.+)$/", $read[0], $regs)) {
+                    $search_array = preg_split("/ /", trim($regs[1]));
+                }
             }
-            $imap_query .= $Chunks[count($Chunks)-1];
+           $msgs_str = sqimap_message_list_squisher($search_array);
+            $imap_query = 'FETCH '.$msgs_str;
             $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);
+            $imap_query .= '(RECEIVED)])';
+           $read = sqimap_run_command($imap_stream,$imap_query, true, $response, $message, $uid_support);
         }
     }
     
@@ -412,12 +420,11 @@ function spam_filters($imap_stream) {
     if ($SpamFilters_SharedCache) {
        filters_SaveCache();
     } else {
-       session_register('SpamFilters_DNScache');
+       sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
     }
 
 }
 
-
 // Does the loop through each enabled filter for the specified IP address.
 // IP format:  $a.$b.$c.$d
 function filters_spam_check_site($a, $b, $c, $d, &$filters) {
@@ -427,6 +434,13 @@ function filters_spam_check_site($a, $b, $c, $d, &$filters) {
             if ($filters[$key]['dns']) {
                 $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
                                 $filters[$key]['dns'];
+
+                if(!isset($SpamFilters_DNScache[$filter_revip]['L']))
+                        $SpamFilters_DNScache[$filter_revip]['L'] = '';
+
+                if(!isset($SpamFilters_DNScache[$filter_revip]['T']))
+                        $SpamFilters_DNScache[$filter_revip]['T'] = '';
+
                 if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) {
                     $SpamFilters_DNScache[$filter_revip]['L'] =
                                     gethostbyname($filter_revip);