globals plugins now work, bogoplugin for an example
authorPierre Habouzit <madcoder@debian.org>
Wed, 10 Sep 2003 22:34:45 +0000 (22:34 +0000)
committerPierre Habouzit <madcoder@debian.org>
Wed, 10 Sep 2003 22:34:45 +0000 (22:34 +0000)
philter/philter/include/filter.inc.php
philter/philter/include/philter.inc.php
philter/philter/include/plugin.inc.php
philter/philter/include/plugin_bogofilter.inc.php
philter/philter/include/rule.inc.php
philter/philter/index.php
philter/philter/locales/plugins/bogofilter.en.inc.php [new file with mode: 0644]
philter/philter/locales/plugins/bogofilter.fr.inc.php [new file with mode: 0644]

index 827d252..e5b2188 100644 (file)
@@ -19,11 +19,30 @@ class Filter {
 
         $sql = mysql_query("SELECT rank,rid,flags,name FROM 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 "";
@@ -85,7 +104,8 @@ class Filter {
         $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;
     }
index 0196f66..d6ae075 100644 (file)
@@ -30,7 +30,7 @@ class Philter {
             'path'    => array('procmail', 'spool'),
             'match_plugins' => array(),
             'action_plugins' => array(),
-            'global_plugin' => array(),
+            'global_plugins' => array(),
             'i18n' => array()
         );
         $this->err = "";
@@ -133,12 +133,12 @@ class Philter {
                 return false;
             }
             
-            if(isset($this->config['global_plugin'][$rtti])) {
+            if(isset($this->config['global_plugins'][$rtti])) {
                 $this->err = "Philter::register_global_plugin : a plugin is already registerd";
                 return false;
             }
             
-            $this->config['global_plugin'][$rtti] = $_plugin;
+            $this->config['global_plugins'][$rtti] = $_plugin;
             return true;
         }
 
index 0141d8a..066b8e8 100644 (file)
@@ -80,12 +80,19 @@ class MatchPlugin extends Plugin {
         $this->global=$_glob;
     }
 
+    function sql_store($_uid, $_rid, $_data) {
+        if($this->global)
+            mysql_query("INSERT INTO actions SET uid='$_uid',rid='0',pid='{$_data[0]}',data='{$_data[1]}'");
+        else
+            mysql_query("INSERT INTO actions SET uid='$_uid',rid='$_rid',pid='{$_data[0]}',data='{$_data[1]}'");
+    }
+
     /** true if the plugin is used as global rule
      * overload with (return false) if the plugin can not be used as a global rule
      * overload with (return true) if the plugin can not be used as a normal rule
      * @return  true if usable as a global rule, false else
      */
-    function is_global() { return $global; }
+    function is_global() { return $this->global; }
 }
 
 /********************************************************************************
index cb0243a..54b02d4 100644 (file)
 class BogoPlugin extends MatchPlugin {
     function BogoPlugin($_glob=false) { $this->MatchPlugin($_glob); }
     function rtti() { return 1; }
-    function name() { return "AntiSpam"; }
+    function name() { return _i18n('1_bogo'); }
 
     /* the plugin is only global */
     function is_global() { return true; }
 
-    function sql_store($_uid, $_rid, $_data) {
-        return "INSERT INTO matches SET uid='$_uid',rid='$_rid',pid='".$this->rtti()."',data='{$_data['data']}';\n";
-    }
-
     function to_string($_data) {
         if($_data['data']=='off')
             return "";
@@ -41,11 +37,18 @@ class BogoPlugin extends MatchPlugin {
         return $res;
     }
 
-    function to_form() {
+    function to_form($_data) {
         $res =  "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"1\">\n"
             .   "<tr><th width=\"25%\">BogoFilter</th>\n";
 
-        $res .= "<td>TODO</td>\n";
+        $res .= "<td><input type='hidden' name='global[1][0]' value='1' />\n"
+            .   "    <input type='radio' name='global[1][1]' value='0' "
+            .   ($_data[1]==0 ? 'checked="checked "' : '')."/>"._i18n('1_bogo_off')."<br />\n"
+            .   "    <input type='radio' name='global[1][1]' value='1' "
+            .   ($_data[1]==1 ? 'checked="checked "' : '')."/>"._i18n('1_bogo_on')."<br />\n"
+            .   "    <input type='radio' name='global[1][1]' value='2' "
+            .   ($_data[1]==2 ? 'checked="checked "' : '')."/>"._i18n('1_bogo_drop')."<br />\n"
+            .   "</td>\n";
 
         $res .= "</tr>\n"
             .   "</table>\n";
index 6ab2116..726b070 100644 (file)
@@ -106,11 +106,15 @@ class Rule {
 
         mysql_query("INSERT INTO rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'");
 
-        foreach($this->matches as $match)
-            $philter->config['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $action);
-
-        foreach($this->actions as $action)
-            $philter->config['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
+        if($_rid) {
+            foreach($this->matches as $match)
+                $philter->config['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
+
+            foreach($this->actions as $action)
+                $philter->config['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
+        } else
+            foreach($this->matches as $match)
+                $philter->config['global_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
     }
 
     function to_js() {
index 5b31d0c..447a12f 100644 (file)
@@ -15,6 +15,12 @@ $mail_pool = new EmailPool(get_user_id());
 $filter = new Filter(get_user_id());
 
 if(count($_POST)) {                             // a FORM has been submitted
+    if(isset($_POST['global'])) {               //      FORM global
+        foreach($philter->config['global_plugins'] as $id=>$g_plugin)
+            if(isset($_POST['global'][$id]))
+                $filter->set_global_data($id,$_POST['global'][$id]);
+        $filter->rules[0]->sql_store(get_user_id(),0);
+    }
     if(isset($_POST['emails']))                 //      FORM emails
         if(!$mail_pool->handle_form())
             echo "<p style=\"color:red;\">".$philter->error()."</p>\n";
@@ -42,18 +48,18 @@ require("include/js_factory.inc.php");
 <div class="rubrique"><?php i18n('global_title'); ?></div>
 <?php
 i18n('global_help');
-if(count($philter->config['global_plugin'])) {
+if(count($philter->config['global_plugins'])) {
     echo "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
         ."<table width=\"95%\" class=\"bicol\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">\n";
 
-    foreach($philter->config['global_plugin'] as $g_plugin) {
+    foreach($philter->config['global_plugins'] as $id=>$g_plugin) {
         echo "<tr><td>\n";
-        echo $g_plugin->to_form(/* data */);
+        echo $g_plugin->to_form($filter->get_global_data($id));
         echo "</td></tr>\n";
         
     }
-
-    echo "</table>\n"
+    echo "<tr><td align='center'><input type='submit' name='global[submit]' value='"._i18n('apply_changes')."'/></td></tr>\n"
+        ."</table>\n"
         ."</form>\n";
 }
 
@@ -81,7 +87,8 @@ i18n('filter_help');
         <tr><td>
 <?php
             foreach($filter->rules as $id=>$rule)
-                echo "<input type=\"hidden\" id=\"order$id\" name=\"order[$id]\" value=\"{$rule->rank}\" />\n";
+                if($id)
+                    echo "<input type=\"hidden\" id=\"order$id\" name=\"order[$id]\" value=\"{$rule->rank}\" />\n";
            
             $presel = (isset($_POST['rule']['id']) ? $_POST['rule']['id']
                         : (isset($_POST['order']['select']) ? $_POST['order']['select'] : 0));
@@ -92,9 +99,10 @@ i18n('filter_help');
             echo "</option>\n";
             
             foreach($filter->rules as $id=>$rule)
-                echo "<option value=\"$id\""
-                    .($presel==$id ? " selected=\"selected\"":"")
-                    .">{$rule->name}</option>\n";
+                if($id)
+                    echo "<option value=\"$id\""
+                        .($presel==$id ? " selected=\"selected\"":"")
+                        .">{$rule->name}</option>\n";
 ?>
             </select>
         </td></tr><tr><td>
diff --git a/philter/philter/locales/plugins/bogofilter.en.inc.php b/philter/philter/locales/plugins/bogofilter.en.inc.php
new file mode 100644 (file)
index 0000000..67fdc94
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+/********************************************************************************
+* i18n file
+*
+* This file is part of the philter distribution
+* Copyright: See COPYING files that comes with this distribution
+********************************************************************************/
+
+$philter->config['i18n']['1_bogo'] = 'AntiSpam';
+$philter->config['i18n']['1_bogo_off'] = 'AntiSpam is off';
+$philter->config['i18n']['1_bogo_on'] = 'AntiSpam is on, and only marks emails';
+$philter->config['i18n']['1_bogo_drop'] = 'AntiSpam is on, and drops the emails considered as spams';
+
+/********************************************************************************
+* $Id$
+* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
+********************************************************************************/
+?>
diff --git a/philter/philter/locales/plugins/bogofilter.fr.inc.php b/philter/philter/locales/plugins/bogofilter.fr.inc.php
new file mode 100644 (file)
index 0000000..a7a1ffb
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+/********************************************************************************
+* i18n file
+*
+* This file is part of the philter distribution
+* Copyright: See COPYING files that comes with this distribution
+********************************************************************************/
+
+$philter->config['i18n']['1_bogo'] = 'AntiSpam';
+$philter->config['i18n']['1_bogo_off'] = 'le filtre anti-spam est coupé';
+$philter->config['i18n']['1_bogo_on'] = 'le filtre anti-spam est activé, et marque les mails';
+$philter->config['i18n']['1_bogo_drop'] = 'le filtre anti-spam est activé, et jette les mails détectés comme spams';
+
+/********************************************************************************
+* $Id$
+* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
+********************************************************************************/
+?>