mirror of
https://github.com/PurpleI2P/regi2p.git
synced 2025-01-15 13:59:56 +00:00
111 lines
4.2 KiB
PHP
111 lines
4.2 KiB
PHP
|
<?php
|
||
|
require __DIR__ . '/vendor/autoload.php';
|
||
|
require __DIR__ . '/config.php';
|
||
|
|
||
|
$pdo = (new App\DB($options))->pdo;
|
||
|
$util = new App\Utils;
|
||
|
|
||
|
$error = "";
|
||
|
|
||
|
$aContext = array(
|
||
|
'http' => array(
|
||
|
'method' => 'GET',
|
||
|
'proxy' => $options['http_proxy'],
|
||
|
'user_agent' => 'MYOB/6.66 (AN/ON)',
|
||
|
'request_fulluri' => true,
|
||
|
'timeout' => 120.0,
|
||
|
),
|
||
|
);
|
||
|
|
||
|
|
||
|
$STH = $pdo->query ("SELECT `name`, `url`, `etag` FROM `subscriptions` WHERE `active` = 1");
|
||
|
$lists = $STH->fetchAll(PDO::FETCH_ASSOC);
|
||
|
|
||
|
foreach ($lists as $list) {
|
||
|
echo "Processing " . $list['name'] . " subscription..." . PHP_EOL;
|
||
|
|
||
|
if (!empty($list['etag']))
|
||
|
$aContext['http']['header'] = 'If-None-Match: ' . $list['etag'] . '\r\n';
|
||
|
|
||
|
$cxContext = stream_context_create($aContext);
|
||
|
$f = fopen($list['url'], "r", false, $cxContext);
|
||
|
|
||
|
if ($f) {
|
||
|
$f_meta = stream_get_meta_data($f);
|
||
|
|
||
|
if (strpos($f_meta['wrapper_data'][0], "200") === false) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$etagHeader = array_filter($f_meta['wrapper_data'], function($el) {
|
||
|
return (strpos($el, "ETag") !== false);
|
||
|
});
|
||
|
|
||
|
if ($etagHeader) {
|
||
|
$etag = substr($etagHeader[array_keys($etagHeader)[0]], 6);
|
||
|
var_dump($etag);
|
||
|
$pdo->exec("UPDATE `subscriptions` SET `etag` = '" . $etag . "' WHERE `name` = '" . $list['name'] . "'");
|
||
|
}
|
||
|
|
||
|
while (($buffer = fgets($f, 4096)) !== false) {
|
||
|
$domain = "";
|
||
|
$record = $util->parseHostRecord($buffer);
|
||
|
|
||
|
if (!$util->isValidAddress($record['host'], $error)) {
|
||
|
echo "Error while validating " . $record['host'] . ": " . $error . PHP_EOL;
|
||
|
continue;
|
||
|
|
||
|
} else {
|
||
|
if ($util->isPunycodeDomain($record['host'])) {
|
||
|
$domain = idn_to_utf8($record['host']);
|
||
|
|
||
|
} else {
|
||
|
$domain = $record['host'];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!$util->isValidBase64($record['b64'])) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if ((isset($record['commands']) && isset($record['commands']['action']) && $record['commands']['action'] == "adddest" && isset($record['commands']['olddest'])) &&
|
||
|
$pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "' AND `base64` = '" . $record['commands']['olddest'] . "' LIMIT 1")->fetchColumn()) {
|
||
|
|
||
|
$base32 = $util->b32from64($record['b64']);
|
||
|
$pdo->exec("UPDATE `hosts` SET `base64` = '" . $record["b64"] . "', `base32` = '" . $base32 . "' WHERE `host` = '" . $domain . "'");
|
||
|
echo "Processed " . $domain . " (adddest)" . PHP_EOL;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if ((isset($record['commands']) && isset($record['commands']['action']) && $record['commands']['action'] == "addsubdomain" && isset($record['commands']['oldname']) && isset($record['commands']['olddest'])) &&
|
||
|
($pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $record['commands']['oldname'] . "' AND `base64` = '" . $record['commands']['olddest'] . "' LIMIT 1")->fetchColumn() &&
|
||
|
!$pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "'")->fetchColumn())) {
|
||
|
|
||
|
$base32 = $util->b32from64($record['b64']);
|
||
|
$pdo->exec("INSERT INTO `hosts` (`host`, `base64`, `base32`, `approved`) VALUES ('" . $domain . "', '" . $record["b64"] . "', '" . $base32 . "', 1)");
|
||
|
echo "Processed " . $domain . " (addsubdomain)" . PHP_EOL;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if(isset($record['commands']) && !$pdo->query("SELECT COUNT(*) FROM `hosts` WHERE `host` = '" . $domain . "' LIMIT 1")->fetchColumn()) {
|
||
|
|
||
|
$base32 = $util->b32from64($record['b64']);
|
||
|
$pdo->exec("INSERT INTO `hosts` (`host`, `base64`, `base32`, `approved`) VALUES ('" . $domain . "', '" . $record["b64"] . "', '" . $base32 . "', 1)");
|
||
|
echo "Processed " . $domain . PHP_EOL;
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!feof($f)) {
|
||
|
echo "Error: fgets() ended earlier than needed." . PHP_EOL;
|
||
|
}
|
||
|
|
||
|
fclose($f);
|
||
|
} else {
|
||
|
echo "Empty response while fetching update from " . $list['name']. "." . PHP_EOL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$pdo = null;
|
||
|
|