| 1 | <?php | 
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- | 
|---|
| 3 | # | 
|---|
| 4 | # This file is part of Antispam, a plugin for Dotclear 2. | 
|---|
| 5 | # | 
|---|
| 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear | 
|---|
| 7 | # Licensed under the GPL version 2.0 license. | 
|---|
| 8 | # See LICENSE file or | 
|---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | 
|---|
| 10 | # | 
|---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- | 
|---|
| 12 | if (!defined('DC_RC_PATH')) { return; } | 
|---|
| 13 |  | 
|---|
| 14 | class dcFilterIP extends dcSpamFilter | 
|---|
| 15 | { | 
|---|
| 16 | public $name = 'IP Filter'; | 
|---|
| 17 | public $has_gui = true; | 
|---|
| 18 |  | 
|---|
| 19 | private $con; | 
|---|
| 20 | private $table; | 
|---|
| 21 |  | 
|---|
| 22 | public function __construct($core) | 
|---|
| 23 | { | 
|---|
| 24 | parent::__construct($core); | 
|---|
| 25 | $this->con =& $core->con; | 
|---|
| 26 | $this->table = $core->prefix.'spamrule'; | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | protected function setInfo() | 
|---|
| 30 | { | 
|---|
| 31 | $this->description = __('IP Blacklist / Whitelist Filter'); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | public function getStatusMessage($status,$comment_id) | 
|---|
| 35 | { | 
|---|
| 36 | return sprintf(__('Filtered by %1$s with rule %2$s.'),$this->guiLink(),$status); | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | public function isSpam($type,$author,$email,$site,$ip,$content,$post_id,&$status) | 
|---|
| 40 | { | 
|---|
| 41 | if (!$ip) { | 
|---|
| 42 | return; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | # White list check | 
|---|
| 46 | if ($this->checkIP($ip,'white') !== false) { | 
|---|
| 47 | return false; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | # Black list check | 
|---|
| 51 | if (($s = $this->checkIP($ip,'black')) !== false) { | 
|---|
| 52 | $status = $s; | 
|---|
| 53 | return true; | 
|---|
| 54 | } | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | public function gui($url) | 
|---|
| 58 | { | 
|---|
| 59 | global $default_tab; | 
|---|
| 60 | $core =& $this->core; | 
|---|
| 61 |  | 
|---|
| 62 | # Set current type and tab | 
|---|
| 63 | $ip_type = 'black'; | 
|---|
| 64 | if (!empty($_REQUEST['ip_type']) && $_REQUEST['ip_type'] == 'white') { | 
|---|
| 65 | $ip_type = 'white'; | 
|---|
| 66 | } | 
|---|
| 67 | $default_tab = 'tab_'.$ip_type; | 
|---|
| 68 |  | 
|---|
| 69 | # Add IP to list | 
|---|
| 70 | if (!empty($_POST['addip'])) | 
|---|
| 71 | { | 
|---|
| 72 | try | 
|---|
| 73 | { | 
|---|
| 74 | $global = !empty($_POST['globalip']) && $core->auth->isSuperAdmin(); | 
|---|
| 75 |  | 
|---|
| 76 | $this->addIP($ip_type,$_POST['addip'],$global); | 
|---|
| 77 | http::redirect($url.'&added=1&ip_type='.$ip_type); | 
|---|
| 78 | } | 
|---|
| 79 | catch (Exception $e) | 
|---|
| 80 | { | 
|---|
| 81 | $core->error->add($e->getMessage()); | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | # Remove IP from list | 
|---|
| 86 | if (!empty($_POST['delip']) && is_array($_POST['delip'])) | 
|---|
| 87 | { | 
|---|
| 88 | try { | 
|---|
| 89 | $this->removeRule($_POST['delip']); | 
|---|
| 90 | http::redirect($url.'&removed=1&ip_type='.$ip_type); | 
|---|
| 91 | } catch (Exception $e) { | 
|---|
| 92 | $core->error->add($e->getMessage()); | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | /* DISPLAY | 
|---|
| 97 | ---------------------------------------------- */ | 
|---|
| 98 | $res = ''; | 
|---|
| 99 |  | 
|---|
| 100 | if (!empty($_GET['added'])) { | 
|---|
| 101 | $res .= dcPage::success(__('IP address has been successfully added.'),true,false,false); | 
|---|
| 102 | } | 
|---|
| 103 | if (!empty($_GET['removed'])) { | 
|---|
| 104 | $res .= dcPage::success(__('IP addresses have been successfully removed.'),true,false,false); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | $res .= | 
|---|
| 108 | $this->displayForms($url,'black',__('Blacklist')). | 
|---|
| 109 | $this->displayForms($url,'white',__('Whitelist')); | 
|---|
| 110 |  | 
|---|
| 111 | return $res; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | private function displayForms($url,$type,$title) | 
|---|
| 115 | { | 
|---|
| 116 | $core =& $this->core; | 
|---|
| 117 |  | 
|---|
| 118 | $res = | 
|---|
| 119 | '<div class="multi-part" id="tab_'.$type.'" title="'.$title.'">'. | 
|---|
| 120 |  | 
|---|
| 121 | '<form action="'.html::escapeURL($url).'" method="post" class="fieldset">'. | 
|---|
| 122 |  | 
|---|
| 123 | '<p>'. | 
|---|
| 124 | form::hidden(array('ip_type'),$type). | 
|---|
| 125 | '<label class="classic" for="addip_'.$type.'">'.__('Add an IP address: ').'</label> '. | 
|---|
| 126 | form::field(array('addip', 'addip_'.$type),18,255); | 
|---|
| 127 | if ($core->auth->isSuperAdmin()) { | 
|---|
| 128 | $res .= '<label class="classic" for="globalip_'.$type.'">'.form::checkbox(array('globalip', 'globalip_'.$type),1).' '. | 
|---|
| 129 | __('Global IP (used for all blogs)').'</label> '; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | $res .= | 
|---|
| 133 | $core->formNonce(). | 
|---|
| 134 | '</p>'. | 
|---|
| 135 | '<p><input type="submit" value="'.__('Add').'"/></p>'. | 
|---|
| 136 | '</form>'; | 
|---|
| 137 |  | 
|---|
| 138 | $rs = $this->getRules($type); | 
|---|
| 139 |  | 
|---|
| 140 | if ($rs->isEmpty()) | 
|---|
| 141 | { | 
|---|
| 142 | $res .= '<p><strong>'.__('No IP address in list.').'</strong></p>'; | 
|---|
| 143 | } | 
|---|
| 144 | else | 
|---|
| 145 | { | 
|---|
| 146 | $res .= | 
|---|
| 147 | '<form action="'.html::escapeURL($url).'" method="post">'. | 
|---|
| 148 | '<h3>' . __('IP list') . '</h3>'. | 
|---|
| 149 | '<div class="antispam">'; | 
|---|
| 150 |  | 
|---|
| 151 | $res_global = ''; | 
|---|
| 152 | $res_local = ''; | 
|---|
| 153 | while ($rs->fetch()) | 
|---|
| 154 | { | 
|---|
| 155 | $bits = explode(':',$rs->rule_content); | 
|---|
| 156 | $pattern = $bits[0]; | 
|---|
| 157 | $ip = $bits[1]; | 
|---|
| 158 | $bitmask = $bits[2]; | 
|---|
| 159 |  | 
|---|
| 160 | $disabled_ip = false; | 
|---|
| 161 | $p_style = ''; | 
|---|
| 162 | if (!$rs->blog_id) { | 
|---|
| 163 | $disabled_ip = !$core->auth->isSuperAdmin(); | 
|---|
| 164 | $p_style .= ' global'; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | $item = | 
|---|
| 168 | '<p class="'.$p_style.'"><label class="classic" for="'.$type.'-ip-'.$rs->rule_id.'">'. | 
|---|
| 169 | form::checkbox(array('delip[]',$type.'-ip-'.$rs->rule_id),$rs->rule_id,false,'','',$disabled_ip).' '. | 
|---|
| 170 | html::escapeHTML($pattern). | 
|---|
| 171 | '</label></p>'; | 
|---|
| 172 |  | 
|---|
| 173 | if ($rs->blog_id) { | 
|---|
| 174 | // local list | 
|---|
| 175 | if ($res_local == '') { | 
|---|
| 176 | $res_local = '<h4>'.__('Local IPs (used only for this blog)').'</h4>'; | 
|---|
| 177 | } | 
|---|
| 178 | $res_local .= $item; | 
|---|
| 179 | } else { | 
|---|
| 180 | // global list | 
|---|
| 181 | if ($res_global == '') { | 
|---|
| 182 | $res_global = '<h4>'.__('Global IPs (used for all blogs)').'</h4>'; | 
|---|
| 183 | } | 
|---|
| 184 | $res_global .= $item; | 
|---|
| 185 | } | 
|---|
| 186 | } | 
|---|
| 187 | $res .= $res_local.$res_global; | 
|---|
| 188 |  | 
|---|
| 189 | $res .= | 
|---|
| 190 | '</div>'. | 
|---|
| 191 | '<p><input class="submit delete" type="submit" value="'.__('Delete').'"/>'. | 
|---|
| 192 | $core->formNonce(). | 
|---|
| 193 | form::hidden(array('ip_type'),$type). | 
|---|
| 194 | '</p>'. | 
|---|
| 195 | '</form>'; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | $res .= '</div>'; | 
|---|
| 199 |  | 
|---|
| 200 | return $res; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | private function ipmask($pattern,&$ip,&$mask) | 
|---|
| 204 | { | 
|---|
| 205 | $bits = explode('/',$pattern); | 
|---|
| 206 |  | 
|---|
| 207 | # Set IP | 
|---|
| 208 | $bits[0] .= str_repeat(".0", 3 - substr_count($bits[0], ".")); | 
|---|
| 209 | $ip = ip2long($bits[0]); | 
|---|
| 210 |  | 
|---|
| 211 | if (!$ip || $ip == -1) { | 
|---|
| 212 | throw new Exception('Invalid IP address'); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | # Set mask | 
|---|
| 216 | if (!isset($bits[1])) { | 
|---|
| 217 | $mask = -1; | 
|---|
| 218 | } elseif (strpos($bits[1],'.')) { | 
|---|
| 219 | $mask = ip2long($bits[1]); | 
|---|
| 220 | if (!$mask) { | 
|---|
| 221 | $mask = -1; | 
|---|
| 222 | } | 
|---|
| 223 | } else { | 
|---|
| 224 | $mask = ~((1 << (32 - $bits[1])) - 1); | 
|---|
| 225 | } | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | private function addIP($type,$pattern,$global) | 
|---|
| 229 | { | 
|---|
| 230 | $this->ipmask($pattern,$ip,$mask); | 
|---|
| 231 | $pattern = long2ip($ip).($mask != -1 ? '/'.long2ip($mask) : ''); | 
|---|
| 232 | $content = $pattern.':'.$ip.':'.$mask; | 
|---|
| 233 |  | 
|---|
| 234 | $old = $this->getRuleCIDR($type,$global,$ip,$mask); | 
|---|
| 235 | $cur = $this->con->openCursor($this->table); | 
|---|
| 236 |  | 
|---|
| 237 | if ($old->isEmpty()) | 
|---|
| 238 | { | 
|---|
| 239 | $id = $this->con->select('SELECT MAX(rule_id) FROM '.$this->table)->f(0) + 1; | 
|---|
| 240 |  | 
|---|
| 241 | $cur->rule_id = $id; | 
|---|
| 242 | $cur->rule_type = (string) $type; | 
|---|
| 243 | $cur->rule_content = (string) $content; | 
|---|
| 244 |  | 
|---|
| 245 | if ($global && $this->core->auth->isSuperAdmin()) { | 
|---|
| 246 | $cur->blog_id = null; | 
|---|
| 247 | } else { | 
|---|
| 248 | $cur->blog_id = $this->core->blog->id; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | $cur->insert(); | 
|---|
| 252 | } | 
|---|
| 253 | else | 
|---|
| 254 | { | 
|---|
| 255 | $cur->rule_type = (string) $type; | 
|---|
| 256 | $cur->rule_content = (string) $content; | 
|---|
| 257 | $cur->update('WHERE rule_id = '.(integer) $old->rule_id); | 
|---|
| 258 | } | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | private function getRules($type='all') | 
|---|
| 262 | { | 
|---|
| 263 | $strReq = | 
|---|
| 264 | 'SELECT rule_id, rule_type, blog_id, rule_content '. | 
|---|
| 265 | 'FROM '.$this->table.' '. | 
|---|
| 266 | "WHERE rule_type = '".$this->con->escape($type)."' ". | 
|---|
| 267 | "AND (blog_id = '".$this->core->blog->id."' OR blog_id IS NULL) ". | 
|---|
| 268 | 'ORDER BY blog_id ASC, rule_content ASC '; | 
|---|
| 269 |  | 
|---|
| 270 | return $this->con->select($strReq); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | private function getRuleCIDR($type,$global,$ip,$mask) | 
|---|
| 274 | { | 
|---|
| 275 | $strReq = | 
|---|
| 276 | 'SELECT * FROM '.$this->table.' '. | 
|---|
| 277 | "WHERE rule_type = '".$this->con->escape($type)."' ". | 
|---|
| 278 | "AND rule_content LIKE '%:".(integer) $ip.":".(integer) $mask."' ". | 
|---|
| 279 | 'AND blog_id '.($global ? 'IS NULL ' : "= '".$this->core->blog->id."' "); | 
|---|
| 280 |  | 
|---|
| 281 | return $this->con->select($strReq); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | private function checkIP($cip,$type) | 
|---|
| 285 | { | 
|---|
| 286 | $core =& $this->core; | 
|---|
| 287 |  | 
|---|
| 288 | $strReq = | 
|---|
| 289 | 'SELECT DISTINCT(rule_content) '. | 
|---|
| 290 | 'FROM '.$this->table.' '. | 
|---|
| 291 | "WHERE rule_type = '".$this->con->escape($type)."' ". | 
|---|
| 292 | "AND (blog_id = '".$this->core->blog->id."' OR blog_id IS NULL) ". | 
|---|
| 293 | 'ORDER BY rule_content ASC '; | 
|---|
| 294 |  | 
|---|
| 295 | $rs = $this->con->select($strReq); | 
|---|
| 296 | while ($rs->fetch()) | 
|---|
| 297 | { | 
|---|
| 298 | list($pattern,$ip,$mask) = explode(':',$rs->rule_content); | 
|---|
| 299 | if ((ip2long($cip) & (integer) $mask) == ((integer) $ip & (integer) $mask)) { | 
|---|
| 300 | return $pattern; | 
|---|
| 301 | } | 
|---|
| 302 | } | 
|---|
| 303 | return false; | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | private function removeRule($ids) | 
|---|
| 307 | { | 
|---|
| 308 | $strReq = 'DELETE FROM '.$this->table.' '; | 
|---|
| 309 |  | 
|---|
| 310 | if (is_array($ids)) { | 
|---|
| 311 | foreach ($ids as $i => $v) { | 
|---|
| 312 | $ids[$i] = (integer) $v; | 
|---|
| 313 | } | 
|---|
| 314 | $strReq .= 'WHERE rule_id IN ('.implode(',',$ids).') '; | 
|---|
| 315 | } else { | 
|---|
| 316 | $ids = (integer) $ids; | 
|---|
| 317 | $strReq .= 'WHERE rule_id = '.$ids.' '; | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | if (!$this->core->auth->isSuperAdmin()) { | 
|---|
| 321 | $strReq .= "AND blog_id = '".$this->core->blog->id."' "; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 | $this->con->execute($strReq); | 
|---|
| 325 | } | 
|---|
| 326 | } | 
|---|
| 327 | ?> | 
|---|