little hack for auto hl in vim
[old-projects.git] / philter / philter / include / filter.inc.php
index 7b4c38f..34c9297 100644 (file)
@@ -12,21 +12,64 @@ class Filter {
     var $rules;
     /** user id */
     var $uid;
+    /** tmp.bd or not */
+    var $bd;
     
     function Filter($_uid) {
+        global $philter;
         $this->uid = $_uid;
         $this->rules = array();
 
-        $sql = mysql_query("SELECT rank,rid,flags,name FROM rules WHERE uid='$_uid' ORDER BY rank");
+        $sql = mysql_query("SELECT rank,rid,flags,name FROM ".bd()."rules WHERE uid='$_uid' ORDER BY rank");
         while(list($_rank,$_rid,$_flags,$_name) = mysql_fetch_row($sql)) {
-            $this->rules[$_rid] = new Rule($_rank,$_flags,$_name,true);
+            $this->rules[$_rid] = new Rule($_rank,$_flags,$_name);
             $this->rules[$_rid]->sql_get_matches($_uid,$_rid);
             $this->rules[$_rid]->sql_get_actions($_uid,$_rid);
         }
     }
+
+    function get_global_data($_plug_id) {
+        if(isset($this->rules[0]))
+            foreach($this->rules[0]->matches as $match)
+                if($match[0] == $_plug_id)
+                    return $match;
+        return(Array($_plug_id,0));
+    }
+
+    function set_global_data($_plug_id, $data) {
+        if(!isset($this->rules[0]))
+            $this->rules[0] = new Rule(0,'','GLOBAL');
+        foreach($this->rules[0]->matches as $id=>$match)
+            if($match[0] == $_plug_id) {
+                $this->rules[0]->matches[$id] = $data;
+                return;
+            }
+        $this->rules[0]->matches[] = $data;
+    }
     
     function to_string() {
-        return "";
+        global $mail_pool,$philter;
+        $res = "# generated by philter\n"
+            .  "# vim:set syntax=procmail:\n"
+            .  "\n"
+            .  ":0 f\n"
+            .  "|formail -I'X-Philter-Or'\n"
+            .  "\n";
+
+        foreach($this->rules as $id=>$rule)
+            if($id)
+                $res .= $rule->to_string();
+            else
+                foreach($rule->matches as $id=>$match)
+                    $res .= $philter->config['global_plugins'][$match[0]]->to_string($match);
+
+        $res .= ":0\n"
+             .  "!";
+        foreach($mail_pool->emails as $mail)
+            if($mail->is_active())
+                $res .= ' '.$mail->email;
+        
+        return $res."\n";
     }
 
     function delete_rule($_rid) {
@@ -34,7 +77,7 @@ class Filter {
         $this->rules[$_rid]->sql_clean($this->uid, $_rid);
         unset($this->rules[$_rid]);
 
-        mysql_query("UPDATE rules SET rank=rank-1 WHERE uid='{$this->uid}' and rank>$rk");
+        mysql_query("UPDATE ".bd()."rules SET rank=rank-1 WHERE uid='{$this->uid}' and rank>$rk");
     }
 
     function new_rule_id() {
@@ -70,7 +113,7 @@ class Filter {
                     $my_rule->actions[] = $data;
 
         if(!count($my_rule->actions) && !count($my_rule->matches)) {
-            $philter->set_error("You cannot add an empty rule");
+            $philter->set_error(_i18n('filter_err_empty'));
             return false;
         }
         
@@ -82,10 +125,11 @@ class Filter {
     }
 
     function to_js() {
-        $res = "filter[0] = { all: 1, block:1, name:'[ new rule ]', matches: [], actions: [] };\n";
+        $res = "filter[0] = { all: 1, block:1, name:'"._i18n('new_rule')."', matches: [], actions: [] };\n";
         
         foreach($this->rules as $id=>$rule)
-            $res .= "filter[$id] = ".$rule->to_js().";\n";
+            if($id)
+                $res .= "filter[$id] = ".$rule->to_js().";\n";
 
         return $res;
     }