From 528a2dbf9192638a7866e202f61902f54569acf3 Mon Sep 17 00:00:00 2001 From: Brian Huisman Date: Fri, 17 May 2024 15:12:09 -0400 Subject: [PATCH] Store IP as text instead of INT (database change) This change will require you to edit your database or reinstall the Orcinus Site Search from scratch after deleting all associated database tables. Accounts for IPv4 and IPv6. --- orcinus/admin.php | 18 +++++++++--------- orcinus/config.php | 2 +- orcinus/js/admin.js | 2 +- orcinus/search.php | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/orcinus/admin.php b/orcinus/admin.php index c9a3fa0..9eb9382 100644 --- a/orcinus/admin.php +++ b/orcinus/admin.php @@ -286,7 +286,7 @@ if (!$_SESSION['admin_username']) { // Download a csv of the complete query log case 'query_log': $queryLog = $_DDATA['pdo']->query( - 'SELECT `query`, `results`, `stamp`, INET_NTOA(`ip`) AS `ipaddr` + 'SELECT `query`, `results`, `stamp`, `ip` FROM `'.$_DDATA['tbprefix'].'query` ORDER BY `stamp` DESC;' ); $err = $queryLog->errorInfo(); @@ -315,7 +315,7 @@ if (!$_SESSION['admin_username']) { if ($_GEOIP2) { $line['country'] = ''; try { - $geo = $_GEOIP2->country($line['ipaddr']); + $geo = $_GEOIP2->country($line['ip']); } catch(Exception $e) { $geo = false; } if (!empty($geo->raw['country']['names']['en'])) $line['country'] = $geo->raw['country']['names']['en']; @@ -1297,12 +1297,12 @@ ORCINUS; } } else $_SESSION['error'][] = 'Could not read result counts from query log.'; - $select = $_DDATA['pdo']->query('SELECT INET_NTOA(`ip`) AS `ipaddr` FROM `'.$_DDATA['tbprefix'].'query`;')->fetchAll(); + $select = $_DDATA['pdo']->query('SELECT `ip` FROM `'.$_DDATA['tbprefix'].'query`;')->fetchAll(); $locCount = array(); $locData = array('unk' => 'Unknown'); foreach ($select as $row) { try { - $geo = $_GEOIP2->country($row['ipaddr']); + $geo = $_GEOIP2->country($row['ip']); if (!empty($geo->raw['country']['iso_code'])) { if (empty($locCount[$geo->raw['country']['iso_code']])) { $locCount[$geo->raw['country']['iso_code']] = 1; @@ -1338,7 +1338,7 @@ ORCINUS; $_RDATA['query_log_found_rows'] = false; $queries = $_DDATA['pdo']->query( - 'SELECT `t`.`query`, `t`.`results`, INET_NTOA(`t`.`ip`) AS `ipaddr`, + 'SELECT `t`.`query`, `t`.`results`, `t`.`ip`, REGEXP_REPLACE(`t`.`query`, \'^[[:punct:]]+\', \'\') AS `alpha`, `s`.`hits`, `s`.`ipuni`, `s`.`last_hit` FROM `'.$_DDATA['tbprefix'].'query` AS `t` @@ -2927,11 +2927,11 @@ ORCINUS; country($query['ipaddr']); + $query['geo'] = $_GEOIP2->country($query['ip']); } catch(Exception $e) { $query['geo'] = false; } } ?> - - "> + raw['country']['iso_code'])) { if (file_exists(__DIR__.'/img/flags/'.strtolower($query['geo']->raw['country']['iso_code']).'.png')) { $flag = 'img/flags/'.strtolower($query['geo']->raw['country']['iso_code']).'.png'; @@ -3004,7 +3004,7 @@ ORCINUS;
  • diff --git a/orcinus/config.php b/orcinus/config.php index 1e1ce8c..ec136cc 100644 --- a/orcinus/config.php +++ b/orcinus/config.php @@ -256,7 +256,7 @@ if (!in_array($_DDATA['tbprefix'].'query', $_DDATA['tables'], true)) { `query` TINYTEXT NOT NULL, `results` TINYINT UNSIGNED NOT NULL, `stamp` INT UNSIGNED NOT NULL, - `ip` INT UNSIGNED NOT NULL, + `ip` VARCHAR(40) NOT NULL, `cache` MEDIUMBLOB NOT NULL ) ENGINE = MyISAM, CHARACTER SET = utf8mb4, diff --git a/orcinus/js/admin.js b/orcinus/js/admin.js index f51689a..1a07f6c 100644 --- a/orcinus/js/admin.js +++ b/orcinus/js/admin.js @@ -498,7 +498,7 @@ if (queriesModal) { hits_unique: values[2].getAttribute('data-value'), results: values[3].getAttribute('data-value'), stamp: new Date(parseInt(values[4].getAttribute('data-value')) * 1000).toString(), - ipaddr: values[5].innerHTML + ip: values[5].innerHTML }; Object.keys(values).forEach(keys => { diff --git a/orcinus/search.php b/orcinus/search.php index b34644e..e9679a9 100644 --- a/orcinus/search.php +++ b/orcinus/search.php @@ -220,7 +220,7 @@ if ($_RDATA['s_searchable_pages']) { // Check if this search is already cached $_SDATA['formatted'] = implode(' ', $_SDATA['formatted']); $checkCache = $_DDATA['pdo']->prepare( - 'SELECT `stamp`, INET_NTOA(`ip`) AS `ip`, `cache` + 'SELECT `stamp`, `ip`, `cache` FROM `'.$_DDATA['tbprefix'].'query` WHERE `query`=:query AND `cache`<>\'\' ORDER BY `stamp` DESC LIMIT 1;' @@ -616,14 +616,14 @@ if ($_RDATA['s_searchable_pages']) { `query`=:query, `results`=:results, `stamp`=UNIX_TIMESTAMP(), - `ip`=INET_ATON(:ipaddr), + `ip`=:ip, `cache`=:cache ;' ); $insertQuery->execute(array( 'query' => $_SDATA['formatted'], 'results' => count($_SDATA['results']), - 'ipaddr' => $_SERVER['REMOTE_ADDR'], + 'ip' => $_SERVER['REMOTE_ADDR'], 'cache' => $searchCache )); if (!$insertQuery->rowCount()) {