34c92975f0cef9ee85d618b77e0b689dbdbd1abf
[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 /** tmp.bd or not */
16 var $bd;
17
18 function Filter($_uid) {
19 global $philter;
20 $this->uid = $_uid;
21 $this->rules = array();
22
23 $sql = mysql_query("SELECT rank,rid,flags,name FROM ".bd()."rules WHERE uid='$_uid' ORDER BY rank");
24 while(list($_rank,$_rid,$_flags,$_name) = mysql_fetch_row($sql)) {
25 $this->rules[$_rid] = new Rule($_rank,$_flags,$_name);
26 $this->rules[$_rid]->sql_get_matches($_uid,$_rid);
27 $this->rules[$_rid]->sql_get_actions($_uid,$_rid);
28 }
29 }
30
31 function get_global_data($_plug_id) {
32 if(isset($this->rules[0]))
33 foreach($this->rules[0]->matches as $match)
34 if($match[0] == $_plug_id)
35 return $match;
36 return(Array($_plug_id,0));
37 }
38
39 function set_global_data($_plug_id, $data) {
40 if(!isset($this->rules[0]))
41 $this->rules[0] = new Rule(0,'','GLOBAL');
42 foreach($this->rules[0]->matches as $id=>$match)
43 if($match[0] == $_plug_id) {
44 $this->rules[0]->matches[$id] = $data;
45 return;
46 }
47 $this->rules[0]->matches[] = $data;
48 }
49
50 function to_string() {
51 global $mail_pool,$philter;
52 $res = "# generated by philter\n"
53 . "# vim:set syntax=procmail:\n"
54 . "\n"
55 . ":0 f\n"
56 . "|formail -I'X-Philter-Or'\n"
57 . "\n";
58
59 foreach($this->rules as $id=>$rule)
60 if($id)
61 $res .= $rule->to_string();
62 else
63 foreach($rule->matches as $id=>$match)
64 $res .= $philter->config['global_plugins'][$match[0]]->to_string($match);
65
66 $res .= ":0\n"
67 . "!";
68 foreach($mail_pool->emails as $mail)
69 if($mail->is_active())
70 $res .= ' '.$mail->email;
71
72 return $res."\n";
73 }
74
75 function delete_rule($_rid) {
76 $rk = $this->rules[$_rid]->rank;
77 $this->rules[$_rid]->sql_clean($this->uid, $_rid);
78 unset($this->rules[$_rid]);
79
80 mysql_query("UPDATE ".bd()."rules SET rank=rank-1 WHERE uid='{$this->uid}' and rank>$rk");
81 }
82
83 function new_rule_id() {
84 $i=1;
85 while(array_key_exists($i,$this->rules))
86 $i++;
87 return $i;
88 }
89
90 function handle_form() {
91 global $philter;
92
93 $rule_id = ($_POST['rule']['id'] ? $_POST['rule']['id'] : $this->new_rule_id());
94 $rank = (isset($this->rules[$rule_id]) ? $this->rules[$rule_id]->rank : count($this->rules)+1);
95
96 $flags = Array();
97 if($_POST['rule']['all']) $flags[] = 'all';
98 if(isset($_POST['rule']['block'])) $flags[] = 'block';
99 $flags = implode(',', $flags);
100
101 $my_rule = new Rule($rank,$flags,$_POST['rule']['name']);
102
103 // we create the $matches real array
104 if(isset($_POST['rule']['matches']))
105 foreach($_POST['rule']['matches'] as $data)
106 if(array_key_exists(0, $data))
107 $my_rule->matches[] = $data;
108
109 // we create the $actions real array
110 if(isset($_POST['rule']['actions']))
111 foreach($_POST['rule']['actions'] as $data)
112 if(array_key_exists(0, $data))
113 $my_rule->actions[] = $data;
114
115 if(!count($my_rule->actions) && !count($my_rule->matches)) {
116 $philter->set_error(_i18n('filter_err_empty'));
117 return false;
118 }
119
120 $my_rule->sql_store(get_user_id(),$rule_id);
121
122 $_POST['rule']['id'] = $rule_id;
123 $this->rules[$rule_id] = $my_rule;
124 return true;
125 }
126
127 function to_js() {
128 $res = "filter[0] = { all: 1, block:1, name:'"._i18n('new_rule')."', matches: [], actions: [] };\n";
129
130 foreach($this->rules as $id=>$rule)
131 if($id)
132 $res .= "filter[$id] = ".$rule->to_js().";\n";
133
134 return $res;
135 }
136
137 }
138
139 /********************************************************************************
140 * $Id$
141 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
142 ********************************************************************************/
143 ?>