| 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,true); |
| 23 | $this->rules[$_rid]->sql_get_matches($_uid,$_rid); |
| 24 | $this->rules[$_rid]->sql_get_actions($_uid,$_rid); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | function to_string() { |
| 29 | return ""; |
| 30 | } |
| 31 | |
| 32 | function delete_rule($_rid) { |
| 33 | $rk = $this->rules[$_rid]->rank; |
| 34 | $this->rules[$_rid]->sql_clean($this->uid, $_rid); |
| 35 | unset($this->rules[$_rid]); |
| 36 | |
| 37 | mysql_query("UPDATE rules SET rank=rank-1 WHERE uid='{$this->uid}' and rank>$rk"); |
| 38 | } |
| 39 | |
| 40 | function new_rule_id() { |
| 41 | $i=1; |
| 42 | while(array_key_exists($i,$this->rules)) |
| 43 | $i++; |
| 44 | return $i; |
| 45 | } |
| 46 | |
| 47 | function handle_form() { |
| 48 | global $philter; |
| 49 | |
| 50 | $rule_id = ($_POST['rule']['id'] ? $_POST['rule']['id'] : $this->new_rule_id()); |
| 51 | $rank = (isset($this->rules[$rule_id]) ? $this->rules[$rule_id]->rank : count($this->rules)+1); |
| 52 | |
| 53 | $flags = Array(); |
| 54 | if($_POST['rule']['all']) $flags[] = 'all'; |
| 55 | if(isset($_POST['rule']['block'])) $flags[] = 'block'; |
| 56 | $flags = implode(',', $flags); |
| 57 | |
| 58 | $my_rule = new Rule($rank,$flags,$_POST['rule']['name']); |
| 59 | |
| 60 | // we create the $matches real array |
| 61 | if(isset($_POST['rule']['matches'])) |
| 62 | foreach($_POST['rule']['matches'] as $data) |
| 63 | if(array_key_exists(0, $data)) |
| 64 | $my_rule->matches[] = $data; |
| 65 | |
| 66 | // we create the $actions real array |
| 67 | if(isset($_POST['rule']['actions'])) |
| 68 | foreach($_POST['rule']['actions'] as $data) |
| 69 | if(array_key_exists(0, $data)) |
| 70 | $my_rule->actions[] = $data; |
| 71 | |
| 72 | if(!count($my_rule->actions) && !count($my_rule->matches)) { |
| 73 | $philter->set_error("You cannot add an empty rule"); |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | $my_rule->sql_store(get_user_id(),$rule_id); |
| 78 | |
| 79 | $_POST['rule']['id'] = $rule_id; |
| 80 | $this->rules[$rule_id] = $my_rule; |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | function to_js() { |
| 85 | $res = "filter[0] = { all: 1, block:1, name:'"._i18n('new_rule')."', matches: [], actions: [] };\n"; |
| 86 | |
| 87 | foreach($this->rules as $id=>$rule) |
| 88 | $res .= "filter[$id] = ".$rule->to_js().";\n"; |
| 89 | |
| 90 | return $res; |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | /******************************************************************************** |
| 96 | * $Id$ |
| 97 | * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100: |
| 98 | ********************************************************************************/ |
| 99 | ?> |