1
0
mirror of https://github.com/PurpleI2P/regi2p.git synced 2025-01-15 13:59:56 +00:00
regi2p/import.php

59 lines
1.5 KiB
PHP
Raw Normal View History

<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/config.php';
$pdo = (new App\DB($options))->pdo;
$util = new App\Utils;
$error = "";
$f = @fopen("hosts.txt", "r");
if ($f) {
$pdo->beginTransaction();
$STH = $pdo->prepare("INSERT IGNORE INTO hosts (host, base64, base32) VALUES (:host, :base64, :base32)");
$STH->bindParam('host', $domain);
$STH->bindParam('base64', $base64);
$STH->bindParam('base32', $base32);
while (($buffer = fgets($f, 4096)) !== false) {
$domain = "";
$record = $util->parseHostRecord($buffer);
if (!$util->isValidDomain($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'])) {
echo "Error while validating " . $record['host'] . ": incorrect base64: " . $record['b64'] . PHP_EOL;
continue;
}
$base64 = $record['b64'];
$base32 = $util->b32from64($record['b64']);
$STH->execute();
}
if (!feof($f)) {
echo "Error: fgets() ended earlier than needed" . PHP_EOL;
}
if (!$pdo->commit()) {
echo "Error while saving records to database";
}
fclose($f);
}
$pdo = null;