| 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 | |
| 10 | function rule_cmp($r1, $r2) { |
| 11 | return ($r1->rank - $r2->rank); |
| 12 | } |
| 13 | |
| 14 | function 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 | */ |
| 25 | class 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; |
| 50 | /* |
| 51 | $left_joins = ""; |
| 52 | foreach($philter->config['action_plugins'] as $plug) |
| 53 | $left_joins .= $plug->sql_get(); |
| 54 | */ |
| 55 | $bd = bd()."actions"; |
| 56 | $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid'"); |
| 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; |
| 64 | |
| 65 | /* |
| 66 | $left_joins = ""; |
| 67 | foreach($philter->config['match_plugins'] as $plug) |
| 68 | $left_joins .= $plug->sql_get(); |
| 69 | */ |
| 70 | |
| 71 | $bd = bd()."matches"; |
| 72 | $sql = mysql_query("SELECT * FROM $bd WHERE $bd.uid='$_uid' AND $bd.rid='$_rid'"); |
| 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); |
| 80 | } |
| 81 | |
| 82 | function move_to($_new_rank, $_uid, $_rid) { |
| 83 | if($this->rank == $_new_rank) |
| 84 | return; |
| 85 | $this->rank = $_new_rank; |
| 86 | mysql_query("UPDATE ".bd()."rules SET rank='$_new_rank' WHERE uid='$_uid' AND rid='$_rid'"); |
| 87 | } |
| 88 | |
| 89 | function to_string() { |
| 90 | return ""; |
| 91 | } |
| 92 | |
| 93 | function sql_clean($_uid, $_rid) { |
| 94 | global $philter; |
| 95 | |
| 96 | mysql_query("DELETE FROM ".bd()."rules WHERE uid='$_uid' AND rid='$_rid'"); |
| 97 | mysql_query("DELETE FROM ".bd()."actions WHERE uid='$_uid' AND rid='$_rid'"); |
| 98 | mysql_query("DELETE FROM ".bd()."matches WHERE uid='$_uid' AND rid='$_rid'"); |
| 99 | /* |
| 100 | foreach($philter->config['action_plugins'] as $plug) |
| 101 | $plug->sql_clean($_uid, $_rid); |
| 102 | foreach($philter->config['match_plugins'] as $plug) |
| 103 | $plug->sql_clean($_uid, $_rid); |
| 104 | */ |
| 105 | } |
| 106 | |
| 107 | function sql_store($_uid, $_rid) { |
| 108 | global $philter; |
| 109 | |
| 110 | $flags = Array(); |
| 111 | if($this->all) $flags[] = 'all'; |
| 112 | if($this->block) $flags[] = 'block'; |
| 113 | $flags = implode(',',$flags); |
| 114 | |
| 115 | $this->sql_clean($_uid,$_rid); |
| 116 | |
| 117 | mysql_query("INSERT INTO ".bd()."rules SET uid='$_uid',rid='$_rid',rank='{$this->rank}',flags='$flags',name='{$this->name}'"); |
| 118 | |
| 119 | if($_rid) { |
| 120 | foreach($this->matches as $match) |
| 121 | $philter->config['match_plugins'][$match[0]]->sql_store($_uid, $_rid, $match); |
| 122 | |
| 123 | foreach($this->actions as $action) |
| 124 | $philter->config['action_plugins'][$action[0]]->sql_store($_uid, $_rid, $action); |
| 125 | } else |
| 126 | foreach($this->matches as $match) |
| 127 | $philter->config['global_plugins'][$match[0]]->sql_store($_uid, $_rid, $match); |
| 128 | } |
| 129 | |
| 130 | function to_js() { |
| 131 | $matches = array(); |
| 132 | foreach($this->matches as $data) { |
| 133 | ksort($data); |
| 134 | $data = array_map('to_js_str', $data); |
| 135 | $matches[] = '[' . implode(',', $data) . ']'; |
| 136 | } |
| 137 | $matches = '['.implode(',',$matches).']'; |
| 138 | |
| 139 | $actions = array(); |
| 140 | foreach($this->actions as $data) { |
| 141 | ksort($data); |
| 142 | $data = array_map('to_js_str', $data); |
| 143 | $actions[] = '[' . implode(',', $data) . ']'; |
| 144 | } |
| 145 | $actions = '['.implode(',',$actions).']'; |
| 146 | |
| 147 | return "{ all: ".($this->all ? "1" : "0") |
| 148 | .", block: ".($this->block ? "1" : "0") |
| 149 | .", name: '{$this->name}', matches: ".$matches |
| 150 | .", actions: ".$actions." }"; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /******************************************************************************** |
| 155 | * $Id$ |
| 156 | * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100: |
| 157 | ********************************************************************************/ |
| 158 | ?> |