*/
function ip_to_uint($ip)
{
- return ip2long($ip);
+ $part = explode('.', $ip);
+ $v = 0;
+ $fact = 0x1000000;
+ for ($i = 0 ; $i < 4 ; ++$i) {
+ $v += $fact * $part[$i];
+ $fact >>= 8;
+ }
+ return $v;
}
/** Convert uint to ip (to build a human understandable ip)
}
$ips[] = $_SERVER['REMOTE_ADDR'];
foreach ($ips as &$ip) {
- $ip = "ip = " . ip_to_uint($ip);
+ $ip = '(ip & mask) = (' . ip_to_uint($ip) . '& mask)';
}
- $res = XDB::query('SELECT state
+ $res = XDB::query('SELECT state, description
FROM ip_watch
WHERE ' . implode(' OR ', $ips) . '
ORDER BY state DESC');
if ($res->numRows()) {
- $_SESSION['check_ip'] = $res->fetchOneCell();
+ $state = $res->fetchOneAssoc();
+ $_SESSION['check_ip'] = $state['state'];
+ $_SESSION['check_ip_desc'] = $state['description'];
} else {
$_SESSION['check_ip'] = 'safe';
}
switch (Post::v('action')) {
case 'create':
if (trim(Post::v('ipN')) != '') {
- Xdb::execute('INSERT IGNORE INTO ip_watch (ip, state, detection, last, uid, description)
- VALUES ({?}, {?}, CURDATE(), NOW(), {?}, {?})',
- ip_to_uint(trim(Post::v('ipN'))), Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
+ Xdb::execute('INSERT IGNORE INTO ip_watch (ip, mask, state, detection, last, uid, description)
+ VALUES ({?}, {?}, {?}, CURDATE(), NOW(), {?}, {?})',
+ ip_to_uint(trim(Post::v('ipN'))), ip_to_uint(trim(Post::v('maskN'))),
+ Post::v('stateN'), S::i('uid'), Post::v('descriptionN'));
};
break;
case 'edit':
Xdb::execute('UPDATE ip_watch
- SET state = {?}, last = NOW(), uid = {?}, description = {?}
+ SET state = {?}, last = NOW(), uid = {?}, description = {?}, mask = {?}
WHERE ip = {?}', Post::v('stateN'), S::i('uid'), Post::v('descriptionN'),
- ip_to_uint(Post::v('ipN')));
+ ip_to_uint(Post::v('maskN')), ip_to_uint(Post::v('ipN')));
break;
default:
$sql = "SELECT w.ip, IF(s.ip IS NULL,
IF(w.ip = s2.ip, s2.host, s2.forward_host),
IF(w.ip = s.ip, s.host, s.forward_host)),
- w.detection, w.state, a.alias AS forlife
+ w.mask, w.detection, w.state, a.alias AS forlife
FROM ip_watch AS w
LEFT JOIN logger.sessions AS s ON (s.ip = w.ip)
LEFT JOIN logger.sessions AS s2 ON (s2.forward_ip = w.ip)
$table = array();
$props = array();
- while (list($ip, $host, $date, $state, $forlife) = $it->next()) {
+ while (list($ip, $host, $mask, $date, $state, $forlife) = $it->next()) {
$ip = uint_to_ip($ip);
+ $mask = uint_to_ip($mask);
if (count($props) == 0 || $props['ip'] != $ip) {
if (count($props) > 0) {
$table[] = $props;
}
$props = array('ip' => $ip,
+ 'mask' => $mask,
'host' => $host,
'detection' => $date,
'state' => $state,
}
$page->assign('table', $table);
} elseif ($action == 'edit') {
- $sql = "SELECT w.detection, w.state, w.last, w.description,
+ $sql = "SELECT w.detection, w.state, w.last, w.description, w.mask,
a1.alias AS edit, a2.alias AS forlife, s.host
FROM ip_watch AS w
LEFT JOIN aliases AS a1 ON (a1.id = w.uid AND a1.type = 'a_vie')
$it = Xdb::iterRow($sql, ip_to_uint($ip));
$props = array();
- while (list($detection, $state, $last, $description, $edit, $forlife, $host) = $it->next()) {
+ while (list($detection, $state, $last, $description, $mask, $edit, $forlife, $host) = $it->next()) {
if (count($props) == 0) {
$props = array('ip' => $ip,
+ 'mask' => uint_to_ip($mask),
'host' => $host,
'detection' => $detection,
'state' => $state,