trying maildrop
[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;
19c62a84 50 /*
dd8de1ec
PH
51 $left_joins = "";
52 foreach($philter->config['action_plugins'] as $plug)
53 $left_joins .= $plug->sql_get();
19c62a84 54 */
772509f3 55 $bd = bd()."actions";
19c62a84 56 $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid'");
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;
19c62a84
PH
64
65 /*
dd8de1ec
PH
66 $left_joins = "";
67 foreach($philter->config['match_plugins'] as $plug)
68 $left_joins .= $plug->sql_get();
19c62a84 69 */
dd8de1ec 70
772509f3 71 $bd = bd()."matches";
19c62a84 72 $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid'");
fe9450a2
PH
73
74 if($_rid)
75 while($t = mysql_fetch_assoc($sql))
76 $this->matches[] = $philter->config['match_plugins'][$t['pid']]->sql_to_data($t);
77 else
78 while($t = mysql_fetch_assoc($sql))
79 $this->matches[] = $philter->config['global_plugins'][$t['pid']]->sql_to_data($t);
dd8de1ec
PH
80 }
81
82 function move_to($_new_rank, $_uid, $_rid) {
83 if($this->rank == $_new_rank)
84 return;
85 $this->rank = $_new_rank;
772509f3 86 mysql_query("UPDATE ".bd()."rules SET rank='$_new_rank' WHERE uid='$_uid' AND rid='$_rid'");
dd8de1ec
PH
87 }
88
89 function to_string() {
34989a23 90 global $philter;
5534b9a9
PH
91 $tmp = array();
92
93 foreach($this->matches as $match)
94 $tmp[] = $philter->config['match_plugins'][$match[0]]->to_string($match);
95
96 $connector = ($this->all ? ' && ' : ' || ');
97 $res = "if( ".implode($connector, $tmp)." )\n"
98 . "{\n";
e7e5e67c 99
fd96b13f 100 foreach($this->actions as $action)
5534b9a9 101 $res .= $philter->config['action_plugins'][$action[0]]->to_string($action);
ca052371 102
5534b9a9
PH
103 if($this->block)
104 $res .= " to /dev/null\n"
105 . " exit\n";
ca052371 106
5534b9a9 107 return $res."}\n\n";
dd8de1ec
PH
108 }
109
110 function sql_clean($_uid, $_rid) {
111 global $philter;
112
772509f3
PH
113 mysql_query("DELETE FROM ".bd()."rules WHERE uid='$_uid' AND rid='$_rid'");
114 mysql_query("DELETE FROM ".bd()."actions WHERE uid='$_uid' AND rid='$_rid'");
115 mysql_query("DELETE FROM ".bd()."matches WHERE uid='$_uid' AND rid='$_rid'");
208a7308 116 /*
dd8de1ec
PH
117 foreach($philter->config['action_plugins'] as $plug)
118 $plug->sql_clean($_uid, $_rid);
119 foreach($philter->config['match_plugins'] as $plug)
120 $plug->sql_clean($_uid, $_rid);
208a7308 121 */
dd8de1ec
PH
122 }
123
124 function sql_store($_uid, $_rid) {
125 global $philter;
126
127 $flags = Array();
128 if($this->all) $flags[] = 'all';
129 if($this->block) $flags[] = 'block';
130 $flags = implode(',',$flags);
131
132 $this->sql_clean($_uid,$_rid);
133
772509f3 134 mysql_query("INSERT INTO ".bd()."rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'");
dd8de1ec 135
de47756d
PH
136 if($_rid) {
137 foreach($this->matches as $match)
138 $philter->config['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
139
140 foreach($this->actions as $action)
141 $philter->config['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action);
142 } else
143 foreach($this->matches as $match)
144 $philter->config['global_plugins'][$match[0]]->sql_store($_uid, $_rid, $match);
dd8de1ec
PH
145 }
146
147 function to_js() {
148 $matches = array();
149 foreach($this->matches as $data) {
edb68514 150 ksort($data);
dd8de1ec
PH
151 $data = array_map('to_js_str', $data);
152 $matches[] = '[' . implode(',', $data) . ']';
153 }
154 $matches = '['.implode(',',$matches).']';
155
156 $actions = array();
157 foreach($this->actions as $data) {
edb68514 158 ksort($data);
dd8de1ec
PH
159 $data = array_map('to_js_str', $data);
160 $actions[] = '[' . implode(',', $data) . ']';
161 }
162 $actions = '['.implode(',',$actions).']';
163
164 return "{ all: ".($this->all ? "1" : "0")
165 .", block: ".($this->block ? "1" : "0")
166 .", name: '{$this->name}', matches: ".$matches
167 .", actions: ".$actions." }";
168 }
169}
170
171/********************************************************************************
172* $Id$
173* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
174********************************************************************************/
175?>