css default modifications
[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();
772509f3
PH
54
55 $bd = bd()."actions";
56 $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid' ".$left_joins);
dd8de1ec
PH
57
58 while($t = mysql_fetch_assoc($sql))
59 $this->actions[] = $philter->config['action_plugins'][$t['pid']]->sql_to_data($t);
60 }
61
62 function sql_get_matches($_uid, $_rid) {
63 global $philter;
64
65 $left_joins = "";
66 foreach($philter->config['match_plugins'] as $plug)
67 $left_joins .= $plug->sql_get();
68
772509f3
PH
69 $bd = bd()."matches";
70 $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid' ".$left_joins);
fe9450a2
PH
71
72 if($_rid)
73 while($t = mysql_fetch_assoc($sql))
74 $this->matches[] = $philter->config['match_plugins'][$t['pid']]->sql_to_data($t);
75 else
76 while($t = mysql_fetch_assoc($sql))
77 $this->matches[] = $philter->config['global_plugins'][$t['pid']]->sql_to_data($t);
dd8de1ec
PH
78 }
79
80 function move_to($_new_rank, $_uid, $_rid) {
81 if($this->rank == $_new_rank)
82 return;
83 $this->rank = $_new_rank;
772509f3 84 mysql_query("UPDATE ".bd()."rules SET rank='$_new_rank' WHERE uid='$_uid' AND rid='$_rid'");
dd8de1ec
PH
85 }
86
87 function to_string() {
88 return "";
89 }
90
91 function sql_clean($_uid, $_rid) {
92 global $philter;
93
772509f3
PH
94 mysql_query("DELETE FROM ".bd()."rules WHERE uid='$_uid' AND rid='$_rid'");
95 mysql_query("DELETE FROM ".bd()."actions WHERE uid='$_uid' AND rid='$_rid'");
96 mysql_query("DELETE FROM ".bd()."matches WHERE uid='$_uid' AND rid='$_rid'");
dd8de1ec
PH
97 foreach($philter->config['action_plugins'] as $plug)
98 $plug->sql_clean($_uid, $_rid);
99 foreach($philter->config['match_plugins'] as $plug)
100 $plug->sql_clean($_uid, $_rid);
101 }
102
103 function sql_store($_uid, $_rid) {
104 global $philter;
105
106 $flags = Array();
107 if($this->all) $flags[] = 'all';
108 if($this->block) $flags[] = 'block';
109 $flags = implode(',',$flags);
110
111 $this->sql_clean($_uid,$_rid);
112
772509f3 113 mysql_query("INSERT INTO ".bd()."rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'");
dd8de1ec 114
de47756d
PH
115 if($_rid) {
116 foreach($this->matches as $match)
117 $philter->config['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
118
119 foreach($this->actions as $action)
120 $philter->config['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
121 } else
122 foreach($this->matches as $match)
123 $philter->config['global_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
dd8de1ec
PH
124 }
125
126 function to_js() {
127 $matches = array();
eca2e192 128 ksort($this->matches);
dd8de1ec
PH
129 foreach($this->matches as $data) {
130 $data = array_map('to_js_str', $data);
131 $matches[] = '[' . implode(',', $data) . ']';
132 }
133 $matches = '['.implode(',',$matches).']';
134
135 $actions = array();
136 foreach($this->actions as $data) {
137 $data = array_map('to_js_str', $data);
138 $actions[] = '[' . implode(',', $data) . ']';
139 }
140 $actions = '['.implode(',',$actions).']';
141
142 return "{ all: ".($this->all ? "1" : "0")
143 .", block: ".($this->block ? "1" : "0")
144 .", name: '{$this->name}', matches: ".$matches
145 .", actions: ".$actions." }";
146 }
147}
148
149/********************************************************************************
150* $Id$
151* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
152********************************************************************************/
153?>