dbb7b269b99ca28a8ae5f17de5cb9ec965505020
2 /********************************************************************************
3 * include/rule.inc.php : The class representing One rule
6 * This file is part of the philter distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 function rule_cmp($r1, $r2) {
11 return ($r1->rank
- $r2->rank
);
14 function to_js_str($s) {
15 if((string)((int)($s))==$s)
17 $s = addslashes(stripslashes($s));
18 $s = str_replace("\r\n", '\n', $s);
19 $s = str_replace("\n", '\n', $s);
23 /** this class represents One rule of filtering
26 /** rank of the rule */
28 /** all the matches that the Rule use */
30 /** all the actions that the Rule use */
32 /** name of the rule */
34 /** true if we must stop if the rule matches */
36 /** true if all matches must match, false if only one is enough */
39 function Rule($_rank,$_flags,$_name) {
40 $this->matches
= array();
41 $this->actions
= array();
44 $this->all
= (stristr($_flags,'all')!==false
);
45 $this->block
= (stristr($_flags,'block')!==false
);
48 function sql_get_actions($_uid, $_rid) {
52 foreach($philter->config['action_plugins'] as $plug)
53 $left_joins .= $plug->sql_get();
56 $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid'");
58 while($t = mysql_fetch_assoc($sql))
59 $this->actions
[] = $philter->config
['action_plugins'][$t['pid']]->sql_to_data($t);
62 function sql_get_matches($_uid, $_rid) {
67 foreach($philter->config['match_plugins'] as $plug)
68 $left_joins .= $plug->sql_get();
72 $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid'");
75 while($t = mysql_fetch_assoc($sql))
76 $this->matches
[] = $philter->config
['match_plugins'][$t['pid']]->sql_to_data($t);
78 while($t = mysql_fetch_assoc($sql))
79 $this->matches
[] = $philter->config
['global_plugins'][$t['pid']]->sql_to_data($t);
82 function move_to($_new_rank, $_uid, $_rid) {
83 if($this->rank
== $_new_rank)
85 $this->rank
= $_new_rank;
86 mysql_query("UPDATE ".bd()."rules SET rank='$_new_rank' WHERE uid='$_uid' AND rid='$_rid'");
89 function to_string() {
92 $res = $this->block ?
":0\n" : ":0 c\n";
95 foreach($this->matches
as $match)
96 $res .= $philter->config
['match_plugins'][$match[0]]->to_string($match);
99 foreach($this->matches
as $match) {
100 $res .= (empty($i) ?
":0 f\n" : ":0 fE");
101 $res .= $philter->config
['match_plugins'][$match[0]]->to_string($match);
102 $res .= "| formail -I'X-Philter-Or: yes'\n";
108 . "* ^X-Philter-Or: yes\n"
109 . "| formail -I'X-Philter-Or'\n"
111 . ($this->block ?
":0\n" : ":0 c\n");
115 foreach($this->actions
as $action)
116 $res .= $philter->config
['action_plugins'][$action[0]]->to_string($action);
124 function sql_clean($_uid, $_rid) {
127 mysql_query("DELETE FROM ".bd()."rules WHERE uid='$_uid' AND rid='$_rid'");
128 mysql_query("DELETE FROM ".bd()."actions WHERE uid='$_uid' AND rid='$_rid'");
129 mysql_query("DELETE FROM ".bd()."matches WHERE uid='$_uid' AND rid='$_rid'");
131 foreach($philter->config['action_plugins'] as $plug)
132 $plug->sql_clean($_uid, $_rid);
133 foreach($philter->config['match_plugins'] as $plug)
134 $plug->sql_clean($_uid, $_rid);
138 function sql_store($_uid, $_rid) {
142 if($this->all
) $flags[] = 'all';
143 if($this->block
) $flags[] = 'block';
144 $flags = implode(',',$flags);
146 $this->sql_clean($_uid,$_rid);
148 mysql_query("INSERT INTO ".bd()."rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'");
151 foreach($this->matches
as $match)
152 $philter->config
['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
154 foreach($this->actions
as $action)
155 $philter->config
['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
157 foreach($this->matches
as $match)
158 $philter->config
['global_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
163 foreach($this->matches
as $data) {
165 $data = array_map('to_js_str', $data);
166 $matches[] = '[' . implode(',', $data) . ']';
168 $matches = '['.implode(',',$matches).']';
171 foreach($this->actions
as $data) {
173 $data = array_map('to_js_str', $data);
174 $actions[] = '[' . implode(',', $data) . ']';
176 $actions = '['.implode(',',$actions).']';
178 return "{ all: ".($this->all ?
"1" : "0")
179 .", block: ".($this->block ?
"1" : "0")
180 .", name: '{$this->name}', matches: ".$matches
181 .", actions: ".$actions." }";
185 /********************************************************************************
187 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
188 ********************************************************************************/