e5b21880533362e41d0c792154d22dbd07e11f96
[old-projects.git] / philter / philter / include / filter.inc.php
1 <?php
2 /********************************************************************************
3 * include/filter.inc.php : The complete Filter Object
4 * ----------------------
5 *
6 * This file is part of the philter distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
9
10 class Filter {
11 /** Rules */
12 var $rules;
13 /** user id */
14 var $uid;
15
16 function Filter($_uid) {
17 $this->uid = $_uid;
18 $this->rules = array();
19
20 $sql = mysql_query("SELECT rank,rid,flags,name FROM rules WHERE uid='$_uid' ORDER BY rank");
21 while(list($_rank,$_rid,$_flags,$_name) = mysql_fetch_row($sql)) {
22 $this->rules[$_rid] = new Rule($_rank,$_flags,$_name);
23 $this->rules[$_rid]->sql_get_matches($_uid,$_rid);
24 $this->rules[$_rid]->sql_get_actions($_uid,$_rid);
25 }
26 }
27
28 function get_global_data($_plug_id) {
29 if(isset($this->rules[0]))
30 foreach($this->rules[0]->matches as $match)
31 if($match[0] == $_plug_id)
32 return $match;
33 return(Array($_plug_id,0));
34 }
35
36 function set_global_data($_plug_id, $data) {
37 if(!isset($this->rules[0]))
38 $this->rules[0] = new Rule(0,'','GLOBAL');
39 foreach($this->rules[0]->matches as $id=>$match)
40 if($match[0] == $_plug_id) {
41 $this->rules[0]->matches[$id] = $data;
42 return;
43 }
44 $this->rules[0]->matches[] = $data;
45 }
46
47 function to_string() {
48 return "";
49 }
50
51 function delete_rule($_rid) {
52 $rk = $this->rules[$_rid]->rank;
53 $this->rules[$_rid]->sql_clean($this->uid, $_rid);
54 unset($this->rules[$_rid]);
55
56 mysql_query("UPDATE rules SET rank=rank-1 WHERE uid='{$this->uid}' and rank>$rk");
57 }
58
59 function new_rule_id() {
60 $i=1;
61 while(array_key_exists($i,$this->rules))
62 $i++;
63 return $i;
64 }
65
66 function handle_form() {
67 global $philter;
68
69 $rule_id = ($_POST['rule']['id'] ? $_POST['rule']['id'] : $this->new_rule_id());
70 $rank = (isset($this->rules[$rule_id]) ? $this->rules[$rule_id]->rank : count($this->rules)+1);
71
72 $flags = Array();
73 if($_POST['rule']['all']) $flags[] = 'all';
74 if(isset($_POST['rule']['block'])) $flags[] = 'block';
75 $flags = implode(',', $flags);
76
77 $my_rule = new Rule($rank,$flags,$_POST['rule']['name']);
78
79 // we create the $matches real array
80 if(isset($_POST['rule']['matches']))
81 foreach($_POST['rule']['matches'] as $data)
82 if(array_key_exists(0, $data))
83 $my_rule->matches[] = $data;
84
85 // we create the $actions real array
86 if(isset($_POST['rule']['actions']))
87 foreach($_POST['rule']['actions'] as $data)
88 if(array_key_exists(0, $data))
89 $my_rule->actions[] = $data;
90
91 if(!count($my_rule->actions) && !count($my_rule->matches)) {
92 $philter->set_error(_i18n('filter_err_empty'));
93 return false;
94 }
95
96 $my_rule->sql_store(get_user_id(),$rule_id);
97
98 $_POST['rule']['id'] = $rule_id;
99 $this->rules[$rule_id] = $my_rule;
100 return true;
101 }
102
103 function to_js() {
104 $res = "filter[0] = { all: 1, block:1, name:'"._i18n('new_rule')."', matches: [], actions: [] };\n";
105
106 foreach($this->rules as $id=>$rule)
107 if($id)
108 $res .= "filter[$id] = ".$rule->to_js().";\n";
109
110 return $res;
111 }
112
113 }
114
115 /********************************************************************************
116 * $Id$
117 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
118 ********************************************************************************/
119 ?>