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.
This commit is contained in:
Brian Huisman 2024-05-17 15:12:09 -04:00
parent 5af827a728
commit 528a2dbf91
4 changed files with 14 additions and 14 deletions

View file

@ -286,7 +286,7 @@ if (!$_SESSION['admin_username']) {
// Download a csv of the complete query log // Download a csv of the complete query log
case 'query_log': case 'query_log':
$queryLog = $_DDATA['pdo']->query( $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;' FROM `'.$_DDATA['tbprefix'].'query` ORDER BY `stamp` DESC;'
); );
$err = $queryLog->errorInfo(); $err = $queryLog->errorInfo();
@ -315,7 +315,7 @@ if (!$_SESSION['admin_username']) {
if ($_GEOIP2) { if ($_GEOIP2) {
$line['country'] = ''; $line['country'] = '';
try { try {
$geo = $_GEOIP2->country($line['ipaddr']); $geo = $_GEOIP2->country($line['ip']);
} catch(Exception $e) { $geo = false; } } catch(Exception $e) { $geo = false; }
if (!empty($geo->raw['country']['names']['en'])) if (!empty($geo->raw['country']['names']['en']))
$line['country'] = $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.'; } 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(); $locCount = array();
$locData = array('unk' => 'Unknown'); $locData = array('unk' => 'Unknown');
foreach ($select as $row) { foreach ($select as $row) {
try { try {
$geo = $_GEOIP2->country($row['ipaddr']); $geo = $_GEOIP2->country($row['ip']);
if (!empty($geo->raw['country']['iso_code'])) { if (!empty($geo->raw['country']['iso_code'])) {
if (empty($locCount[$geo->raw['country']['iso_code']])) { if (empty($locCount[$geo->raw['country']['iso_code']])) {
$locCount[$geo->raw['country']['iso_code']] = 1; $locCount[$geo->raw['country']['iso_code']] = 1;
@ -1338,7 +1338,7 @@ ORCINUS;
$_RDATA['query_log_found_rows'] = false; $_RDATA['query_log_found_rows'] = false;
$queries = $_DDATA['pdo']->query( $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`, REGEXP_REPLACE(`t`.`query`, \'^[[:punct:]]+\', \'\') AS `alpha`,
`s`.`hits`, `s`.`ipuni`, `s`.`last_hit` `s`.`hits`, `s`.`ipuni`, `s`.`last_hit`
FROM `'.$_DDATA['tbprefix'].'query` AS `t` FROM `'.$_DDATA['tbprefix'].'query` AS `t`
@ -2927,11 +2927,11 @@ ORCINUS;
</td><?php </td><?php
if ($_GEOIP2) { if ($_GEOIP2) {
try { try {
$query['geo'] = $_GEOIP2->country($query['ipaddr']); $query['geo'] = $_GEOIP2->country($query['ip']);
} catch(Exception $e) { $query['geo'] = false; } } catch(Exception $e) { $query['geo'] = false; }
} ?> } ?>
<td class="text-end d-none d-md-table-cell" data-value="<?php echo $query['ipaddr']; ?>"> <td class="text-end d-none d-md-table-cell" data-value="<?php echo $query['ip']; ?>">
<a href="https://bgp.he.net/ip/<?php echo $query['ipaddr']; ?>" target="_blank"><?php echo $query['ipaddr']; ?></a><?php <a href="https://bgp.he.net/ip/<?php echo $query['ip']; ?>" target="_blank"><?php echo $query['ip']; ?></a><?php
if (!empty($query['geo']->raw['country']['iso_code'])) { if (!empty($query['geo']->raw['country']['iso_code'])) {
if (file_exists(__DIR__.'/img/flags/'.strtolower($query['geo']->raw['country']['iso_code']).'.png')) { 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'; $flag = 'img/flags/'.strtolower($query['geo']->raw['country']['iso_code']).'.png';
@ -3004,7 +3004,7 @@ ORCINUS;
<li class="list-group-item"> <li class="list-group-item">
<label class="d-flex"> <label class="d-flex">
<strong class="pe-2">From IP Address</strong> <strong class="pe-2">From IP Address</strong>
<var class="flex-grow-1 text-end" id="os_queries_modal_ipaddr"></var> <var class="flex-grow-1 text-end" id="os_queries_modal_ip"></var>
</label> </label>
</li> </li>
</ul> </ul>

View file

@ -256,7 +256,7 @@ if (!in_array($_DDATA['tbprefix'].'query', $_DDATA['tables'], true)) {
`query` TINYTEXT NOT NULL, `query` TINYTEXT NOT NULL,
`results` TINYINT UNSIGNED NOT NULL, `results` TINYINT UNSIGNED NOT NULL,
`stamp` INT UNSIGNED NOT NULL, `stamp` INT UNSIGNED NOT NULL,
`ip` INT UNSIGNED NOT NULL, `ip` VARCHAR(40) NOT NULL,
`cache` MEDIUMBLOB NOT NULL `cache` MEDIUMBLOB NOT NULL
) ENGINE = MyISAM, ) ENGINE = MyISAM,
CHARACTER SET = utf8mb4, CHARACTER SET = utf8mb4,

View file

@ -498,7 +498,7 @@ if (queriesModal) {
hits_unique: values[2].getAttribute('data-value'), hits_unique: values[2].getAttribute('data-value'),
results: values[3].getAttribute('data-value'), results: values[3].getAttribute('data-value'),
stamp: new Date(parseInt(values[4].getAttribute('data-value')) * 1000).toString(), stamp: new Date(parseInt(values[4].getAttribute('data-value')) * 1000).toString(),
ipaddr: values[5].innerHTML ip: values[5].innerHTML
}; };
Object.keys(values).forEach(keys => { Object.keys(values).forEach(keys => {

View file

@ -220,7 +220,7 @@ if ($_RDATA['s_searchable_pages']) {
// Check if this search is already cached // Check if this search is already cached
$_SDATA['formatted'] = implode(' ', $_SDATA['formatted']); $_SDATA['formatted'] = implode(' ', $_SDATA['formatted']);
$checkCache = $_DDATA['pdo']->prepare( $checkCache = $_DDATA['pdo']->prepare(
'SELECT `stamp`, INET_NTOA(`ip`) AS `ip`, `cache` 'SELECT `stamp`, `ip`, `cache`
FROM `'.$_DDATA['tbprefix'].'query` FROM `'.$_DDATA['tbprefix'].'query`
WHERE `query`=:query AND `cache`<>\'\' WHERE `query`=:query AND `cache`<>\'\'
ORDER BY `stamp` DESC LIMIT 1;' ORDER BY `stamp` DESC LIMIT 1;'
@ -616,14 +616,14 @@ if ($_RDATA['s_searchable_pages']) {
`query`=:query, `query`=:query,
`results`=:results, `results`=:results,
`stamp`=UNIX_TIMESTAMP(), `stamp`=UNIX_TIMESTAMP(),
`ip`=INET_ATON(:ipaddr), `ip`=:ip,
`cache`=:cache `cache`=:cache
;' ;'
); );
$insertQuery->execute(array( $insertQuery->execute(array(
'query' => $_SDATA['formatted'], 'query' => $_SDATA['formatted'],
'results' => count($_SDATA['results']), 'results' => count($_SDATA['results']),
'ipaddr' => $_SERVER['REMOTE_ADDR'], 'ip' => $_SERVER['REMOTE_ADDR'],
'cache' => $searchCache 'cache' => $searchCache
)); ));
if (!$insertQuery->rowCount()) { if (!$insertQuery->rowCount()) {