4c9ecd773c8e9349ee098171fd19211e637426e7
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();
55 $sql = mysql_query("SELECT * FROM actions WHERE actions.uid='$_uid' AND actions.rid='$_rid' ".$left_joins);
57 while($t = mysql_fetch_assoc($sql))
58 $this->actions
[] = $philter->config
['action_plugins'][$t['pid']]->sql_to_data($t);
61 function sql_get_matches($_uid, $_rid) {
65 foreach($philter->config
['match_plugins'] as $plug)
66 $left_joins .= $plug->sql_get();
68 $sql = mysql_query("SELECT * FROM matches WHERE matches.uid='$_uid' AND matches.rid='$_rid' ".$left_joins);
70 while($t = mysql_fetch_assoc($sql))
71 $this->matches
[] = $philter->config
['match_plugins'][$t['pid']]->sql_to_data($t);
74 function move_to($_new_rank, $_uid, $_rid) {
75 if($this->rank
== $_new_rank)
77 $this->rank
= $_new_rank;
78 mysql_query("UPDATE rules SET rank='$_new_rank' WHERE uid='$_uid' AND rid='$_rid'");
81 function to_string() {
85 function sql_clean($_uid, $_rid) {
88 mysql_query("DELETE FROM rules WHERE uid='$_uid' AND rid='$_rid'");
89 mysql_query("DELETE FROM actions WHERE uid='$_uid' AND rid='$_rid'");
90 mysql_query("DELETE FROM matches WHERE uid='$_uid' AND rid='$_rid'");
91 foreach($philter->config
['action_plugins'] as $plug)
92 $plug->sql_clean($_uid, $_rid);
93 foreach($philter->config
['match_plugins'] as $plug)
94 $plug->sql_clean($_uid, $_rid);
97 function sql_store($_uid, $_rid) {
101 if($this->all
) $flags[] = 'all';
102 if($this->block
) $flags[] = 'block';
103 $flags = implode(',',$flags);
105 $this->sql_clean($_uid,$_rid);
107 mysql_query("INSERT INTO rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'");
109 foreach($this->matches
as $match)
110 $philter->config
['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $action);
112 foreach($this->actions
as $action)
113 $philter->config
['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
118 foreach($this->matches
as $data) {
119 $data = array_map('to_js_str', $data);
120 $matches[] = '[' . implode(',', $data) . ']';
122 $matches = '['.implode(',',$matches).']';
125 foreach($this->actions
as $data) {
126 $data = array_map('to_js_str', $data);
127 $actions[] = '[' . implode(',', $data) . ']';
129 $actions = '['.implode(',',$actions).']';
131 return "{ all: ".($this->all ?
"1" : "0")
132 .", block: ".($this->block ?
"1" : "0")
133 .", name: '{$this->name}', matches: ".$matches
134 .", actions: ".$actions." }";
138 /********************************************************************************
140 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
141 ********************************************************************************/