procmail generation TODO: implement each plugin. plugins : forward, header,
[old-projects.git] / philter / philter / include / filter.inc.php
CommitLineData
dd8de1ec
PH
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
10class Filter {
11 /** Rules */
12 var $rules;
13 /** user id */
14 var $uid;
772509f3
PH
15 /** tmp.bd or not */
16 var $bd;
dd8de1ec
PH
17
18 function Filter($_uid) {
772509f3 19 global $philter;
dd8de1ec
PH
20 $this->uid = $_uid;
21 $this->rules = array();
22
772509f3 23 $sql = mysql_query("SELECT rank,rid,flags,name FROM ".bd()."rules WHERE uid='$_uid' ORDER BY rank");
dd8de1ec 24 while(list($_rank,$_rid,$_flags,$_name) = mysql_fetch_row($sql)) {
de47756d 25 $this->rules[$_rid] = new Rule($_rank,$_flags,$_name);
dd8de1ec
PH
26 $this->rules[$_rid]->sql_get_matches($_uid,$_rid);
27 $this->rules[$_rid]->sql_get_actions($_uid,$_rid);
28 }
29 }
de47756d
PH
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 }
dd8de1ec
PH
49
50 function to_string() {
583e7202 51 global $mail_pool,$philter;
ca052371
PH
52 $res = ":0 f\n"
53 . "|formail -I'X-Philter-Or'\n"
54 . "\n";
55
56 foreach($this->rules as $id=>$rule)
57 if($id)
58 $res .= $rule->to_string();
583e7202
PH
59 else
60 foreach($rule->matches as $id=>$match)
61 $res .= $philter->config['global_plugins'][$match[0]]->to_string($match);
ca052371 62
583e7202
PH
63 $res .= ":0\n"
64 . "!";
65 foreach($mail_pool->emails as $mail)
66 if($mail->is_active())
67 $res .= ' '.$mail->email;
68
69 return $res."\n";
dd8de1ec
PH
70 }
71
72 function delete_rule($_rid) {
73 $rk = $this->rules[$_rid]->rank;
74 $this->rules[$_rid]->sql_clean($this->uid, $_rid);
75 unset($this->rules[$_rid]);
76
772509f3 77 mysql_query("UPDATE ".bd()."rules SET rank=rank-1 WHERE uid='{$this->uid}' and rank>$rk");
dd8de1ec
PH
78 }
79
80 function new_rule_id() {
81 $i=1;
82 while(array_key_exists($i,$this->rules))
83 $i++;
84 return $i;
85 }
86
87 function handle_form() {
88 global $philter;
89
90 $rule_id = ($_POST['rule']['id'] ? $_POST['rule']['id'] : $this->new_rule_id());
91 $rank = (isset($this->rules[$rule_id]) ? $this->rules[$rule_id]->rank : count($this->rules)+1);
92
93 $flags = Array();
94 if($_POST['rule']['all']) $flags[] = 'all';
95 if(isset($_POST['rule']['block'])) $flags[] = 'block';
96 $flags = implode(',', $flags);
97
98 $my_rule = new Rule($rank,$flags,$_POST['rule']['name']);
99
100 // we create the $matches real array
101 if(isset($_POST['rule']['matches']))
102 foreach($_POST['rule']['matches'] as $data)
103 if(array_key_exists(0, $data))
104 $my_rule->matches[] = $data;
105
106 // we create the $actions real array
107 if(isset($_POST['rule']['actions']))
108 foreach($_POST['rule']['actions'] as $data)
109 if(array_key_exists(0, $data))
110 $my_rule->actions[] = $data;
111
112 if(!count($my_rule->actions) && !count($my_rule->matches)) {
f0a83324 113 $philter->set_error(_i18n('filter_err_empty'));
dd8de1ec
PH
114 return false;
115 }
116
117 $my_rule->sql_store(get_user_id(),$rule_id);
118
119 $_POST['rule']['id'] = $rule_id;
120 $this->rules[$rule_id] = $my_rule;
121 return true;
122 }
123
124 function to_js() {
e4f6db07 125 $res = "filter[0] = { all: 1, block:1, name:'"._i18n('new_rule')."', matches: [], actions: [] };\n";
dd8de1ec
PH
126
127 foreach($this->rules as $id=>$rule)
de47756d
PH
128 if($id)
129 $res .= "filter[$id] = ".$rule->to_js().";\n";
dd8de1ec
PH
130
131 return $res;
132 }
133
134}
135
136/********************************************************************************
137* $Id$
138* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
139********************************************************************************/
140?>