some tweaks and bugfixes
[old-projects.git] / philter / philter / include / rule.inc.php
CommitLineData
dd8de1ec
PH
1<?php
2/********************************************************************************
3* include/rule.inc.php : The class representing One rule
4* --------------------
5*
6* This file is part of the philter distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
10function rule_cmp($r1, $r2) {
11 return ($r1->rank - $r2->rank);
12}
13
14function to_js_str($s) {
15 if((string)((int)($s))==$s)
16 return((int)($s));
17 $s = addslashes(stripslashes($s));
18 $s = str_replace("\r\n", '\n', $s);
19 $s = str_replace("\n", '\n', $s);
20 return "'$s'";
21}
22
23/** this class represents One rule of filtering
24 */
25class Rule {
26 /** rank of the rule */
27 var $rank;
28 /** all the matches that the Rule use */
29 var $matches;
30 /** all the actions that the Rule use */
31 var $actions;
32 /** name of the rule */
33 var $name;
34 /** true if we must stop if the rule matches */
35 var $block;
36 /** true if all matches must match, false if only one is enough */
37 var $all;
38
39 function Rule($_rank,$_flags,$_name) {
40 $this->matches = array();
41 $this->actions = array();
42 $this->rank = $_rank;
43 $this->name = $_name;
44 $this->all = (stristr($_flags,'all')!==false);
45 $this->block = (stristr($_flags,'block')!==false);
46 }
47
48 function sql_get_actions($_uid, $_rid) {
49 global $philter;
50
51 $left_joins = "";
52 foreach($philter->config['action_plugins'] as $plug)
53 $left_joins .= $plug->sql_get();
54
55 $sql = mysql_query("SELECT * FROM actions WHERE actions.uid='$_uid' AND actions.rid='$_rid' ".$left_joins);
56
57 while($t = mysql_fetch_assoc($sql))
58 $this->actions[] = $philter->config['action_plugins'][$t['pid']]->sql_to_data($t);
59 }
60
61 function sql_get_matches($_uid, $_rid) {
62 global $philter;
63
64 $left_joins = "";
65 foreach($philter->config['match_plugins'] as $plug)
66 $left_joins .= $plug->sql_get();
67
68 $sql = mysql_query("SELECT * FROM matches WHERE matches.uid='$_uid' AND matches.rid='$_rid' ".$left_joins);
fe9450a2
PH
69
70 if($_rid)
71 while($t = mysql_fetch_assoc($sql))
72 $this->matches[] = $philter->config['match_plugins'][$t['pid']]->sql_to_data($t);
73 else
74 while($t = mysql_fetch_assoc($sql))
75 $this->matches[] = $philter->config['global_plugins'][$t['pid']]->sql_to_data($t);
dd8de1ec
PH
76 }
77
78 function move_to($_new_rank, $_uid, $_rid) {
79 if($this->rank == $_new_rank)
80 return;
81 $this->rank = $_new_rank;
82 mysql_query("UPDATE rules SET rank='$_new_rank' WHERE uid='$_uid' AND rid='$_rid'");
83 }
84
85 function to_string() {
86 return "";
87 }
88
89 function sql_clean($_uid, $_rid) {
90 global $philter;
91
92 mysql_query("DELETE FROM rules WHERE uid='$_uid' AND rid='$_rid'");
93 mysql_query("DELETE FROM actions WHERE uid='$_uid' AND rid='$_rid'");
94 mysql_query("DELETE FROM matches WHERE uid='$_uid' AND rid='$_rid'");
95 foreach($philter->config['action_plugins'] as $plug)
96 $plug->sql_clean($_uid, $_rid);
97 foreach($philter->config['match_plugins'] as $plug)
98 $plug->sql_clean($_uid, $_rid);
99 }
100
101 function sql_store($_uid, $_rid) {
102 global $philter;
103
104 $flags = Array();
105 if($this->all) $flags[] = 'all';
106 if($this->block) $flags[] = 'block';
107 $flags = implode(',',$flags);
108
109 $this->sql_clean($_uid,$_rid);
110
111 mysql_query("INSERT INTO rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'");
112
de47756d
PH
113 if($_rid) {
114 foreach($this->matches as $match)
115 $philter->config['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
116
117 foreach($this->actions as $action)
118 $philter->config['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
119 } else
120 foreach($this->matches as $match)
121 $philter->config['global_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
dd8de1ec
PH
122 }
123
124 function to_js() {
125 $matches = array();
eca2e192 126 ksort($this->matches);
dd8de1ec
PH
127 foreach($this->matches as $data) {
128 $data = array_map('to_js_str', $data);
129 $matches[] = '[' . implode(',', $data) . ']';
130 }
131 $matches = '['.implode(',',$matches).']';
132
133 $actions = array();
134 foreach($this->actions as $data) {
135 $data = array_map('to_js_str', $data);
136 $actions[] = '[' . implode(',', $data) . ']';
137 }
138 $actions = '['.implode(',',$actions).']';
139
140 return "{ all: ".($this->all ? "1" : "0")
141 .", block: ".($this->block ? "1" : "0")
142 .", name: '{$this->name}', matches: ".$matches
143 .", actions: ".$actions." }";
144 }
145}
146
147/********************************************************************************
148* $Id$
149* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
150********************************************************************************/
151?>