From de47756d26c6cd3fd18f650ce2137f3e19da4059 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 10 Sep 2003 22:34:45 +0000 Subject: [PATCH] globals plugins now work, bogoplugin for an example --- philter/philter/include/filter.inc.php | 24 ++++++++++++++++++-- philter/philter/include/philter.inc.php | 6 ++--- philter/philter/include/plugin.inc.php | 9 +++++++- philter/philter/include/plugin_bogofilter.inc.php | 17 ++++++++------ philter/philter/include/rule.inc.php | 14 +++++++----- philter/philter/index.php | 26 ++++++++++++++-------- .../philter/locales/plugins/bogofilter.en.inc.php | 18 +++++++++++++++ .../philter/locales/plugins/bogofilter.fr.inc.php | 18 +++++++++++++++ 8 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 philter/philter/locales/plugins/bogofilter.en.inc.php create mode 100644 philter/philter/locales/plugins/bogofilter.fr.inc.php diff --git a/philter/philter/include/filter.inc.php b/philter/philter/include/filter.inc.php index 827d252..e5b2188 100644 --- a/philter/philter/include/filter.inc.php +++ b/philter/philter/include/filter.inc.php @@ -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; } diff --git a/philter/philter/include/philter.inc.php b/philter/philter/include/philter.inc.php index 0196f66..d6ae075 100644 --- a/philter/philter/include/philter.inc.php +++ b/philter/philter/include/philter.inc.php @@ -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; } diff --git a/philter/philter/include/plugin.inc.php b/philter/philter/include/plugin.inc.php index 0141d8a..066b8e8 100644 --- a/philter/philter/include/plugin.inc.php +++ b/philter/philter/include/plugin.inc.php @@ -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; } } /******************************************************************************** diff --git a/philter/philter/include/plugin_bogofilter.inc.php b/philter/philter/include/plugin_bogofilter.inc.php index cb0243a..54b02d4 100644 --- a/philter/philter/include/plugin_bogofilter.inc.php +++ b/philter/philter/include/plugin_bogofilter.inc.php @@ -10,15 +10,11 @@ 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 = "\n" . "\n"; - $res .= "\n"; + $res .= "\n"; $res .= "\n" . "
BogoFilterTODO\n" + . " "._i18n('1_bogo_off')."
\n" + . " "._i18n('1_bogo_on')."
\n" + . " "._i18n('1_bogo_drop')."
\n" + . "
\n"; diff --git a/philter/philter/include/rule.inc.php b/philter/philter/include/rule.inc.php index 6ab2116..726b070 100644 --- a/philter/philter/include/rule.inc.php +++ b/philter/philter/include/rule.inc.php @@ -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() { diff --git a/philter/philter/index.php b/philter/philter/index.php index 5b31d0c..447a12f 100644 --- a/philter/philter/index.php +++ b/philter/philter/index.php @@ -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 "

".$philter->error()."

\n"; @@ -42,18 +48,18 @@ require("include/js_factory.inc.php");
config['global_plugin'])) { +if(count($philter->config['global_plugins'])) { echo "
\n" ."\n"; - foreach($philter->config['global_plugin'] as $g_plugin) { + foreach($philter->config['global_plugins'] as $id=>$g_plugin) { echo "\n"; } - - echo "
\n"; - echo $g_plugin->to_form(/* data */); + echo $g_plugin->to_form($filter->get_global_data($id)); echo "
\n" + echo "\n" + ."\n" ."
\n"; } @@ -81,7 +87,8 @@ i18n('filter_help'); rules as $id=>$rule) - echo "rank}\" />\n"; + if($id) + echo "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 "\n"; foreach($filter->rules as $id=>$rule) - echo "\n"; + if($id) + echo "\n"; ?> diff --git a/philter/philter/locales/plugins/bogofilter.en.inc.php b/philter/philter/locales/plugins/bogofilter.en.inc.php new file mode 100644 index 0000000..67fdc94 --- /dev/null +++ b/philter/philter/locales/plugins/bogofilter.en.inc.php @@ -0,0 +1,18 @@ +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 index 0000000..a7a1ffb --- /dev/null +++ b/philter/philter/locales/plugins/bogofilter.fr.inc.php @@ -0,0 +1,18 @@ +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: +********************************************************************************/ +?> -- 2.1.4