summaryrefslogtreecommitdiffstats
path: root/www
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2023-02-12 02:58:34 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2023-02-12 02:58:34 +0100
commit245b7dca53d4991f0c830203b84e45710f8030fa (patch)
tree0a61be1b8bd8b1549ee293bb1b4f2f672ab108e4 /www
parentinsert.php fixes for collisions (diff)
downloadtravnik-245b7dca53d4991f0c830203b84e45710f8030fa.tar
travnik-245b7dca53d4991f0c830203b84e45710f8030fa.tar.gz
travnik-245b7dca53d4991f0c830203b84e45710f8030fa.tar.bz2
travnik-245b7dca53d4991f0c830203b84e45710f8030fa.tar.lz
travnik-245b7dca53d4991f0c830203b84e45710f8030fa.tar.xz
travnik-245b7dca53d4991f0c830203b84e45710f8030fa.tar.zst
travnik-245b7dca53d4991f0c830203b84e45710f8030fa.zip
Diffstat (limited to 'www')
-rwxr-xr-xwww/insert.php54
1 files changed, 46 insertions, 8 deletions
diff --git a/www/insert.php b/www/insert.php
index f6da24b..e8ea85f 100755
--- a/www/insert.php
+++ b/www/insert.php
@@ -1,5 +1,36 @@
#!/usr/bin/php
<?php
+function handler (
+ int $errno,
+ string $errstr,
+ string $errfile = null,
+ int $errline = null,
+ array $errcontext = null
+) : bool {
+ global $argv;
+ echo "napaka v $argv[4]: ";
+ return false;
+}
+set_error_handler("handler", E_ALL);
+error_reporting(E_ALL);
+function getInfoString ($f) {
+ global $argv;
+ $p = strpos($f, "4:info");
+ if ($p === false)
+ echo "$argv[4] strpos 4:info false";
+ $p += strlen("4:info");
+ $f = substr($f, $p);
+ $temp = $f;
+ while (true) {
+ $p = strpos($temp, "6:sourced2:ip");
+ $temp[$p] = "x";
+ if (strpos($temp, "6:sourced2:ip") === false)
+ break;
+ }
+ if ($p === false)
+ echo "$argv[4] strpos 6:sourced2:ip false";
+ return substr($f, 0, $p);
+}
require_once "vendor/autoload.php";
use Rhilip\Bencode\TorrentFile;
use Rhilip\Bencode\ParseException;
@@ -7,17 +38,23 @@ if ($argc != 5) {
echo "uporaba: $argv[0] 'mysql:host=tranzistor:3306;dbname=travnik;charset=utf8' travnik " . '$TRAVNIK_DB_PASSWORD datoteka.torrent' . PHP_EOL;
exit(1);
}
-$t = TorrentFile::load($argv[4]);
+try {
+ $t = TorrentFile::load($argv[4]);
+} catch (ParseException $e) {
+ echo "nalaganje $argv[4] ni uspelo: " . $e->getMessage() . PHP_EOL;
+ exit(6);
+}
$c = new PDO($argv[1], $argv[2], $argv[3]);
$c->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$i = getInfoString(file_get_contents($argv[4]));
try {
$s = $c->prepare("SELECT COUNT(1) FROM torrenti WHERE sha1=:z");
$s->bindParam(":z", $zgoščena_vrednost);
- $zgoščena_vrednost = $t->getInfoHashV1(true);
+ $zgoščena_vrednost = sha1($i, true);
$s->execute();
} catch (PDOException $e) {}
if ($s->fetchColumn()) {
- echo "torrent je že shranjen v podatkovni zbirki" . PHP_EOL;
+ echo "torrent $argv[4] je že shranjen v podatkovni zbirki" . PHP_EOL;
exit(0);
}
try {
@@ -31,7 +68,7 @@ try {
ip BINARY(16) NOT NULL,
vrata SMALLINT UNSIGNED NOT NULL,
odjemalec VARCHAR(4096),
- tip ENUM('v1', 'v2, 'hybrid') NOT NULL,
+ tip ENUM('v1', 'v2', 'hybrid') NOT NULL,
sha256 BINARY(32) NOT NULL
)");
$c->exec('CREATE TABLE IF NOT EXISTS datoteke (
@@ -82,10 +119,10 @@ try {
$velikost_metainfo = filesize($argv[4]);
$velikost_koščka = $t->getPieceLength();
$izvor = $t->getSource();
- if (strlen($izvor) > 4096)
+ if (strlen($izvor ?: "") > 4096)
echo "$argv[4] izvor $izvor daljši od 4096";
$ime = $t->getName();
- if (strlen($ime) > 4096)
+ if (strlen($ime ?: "") > 4096)
echo "$argv[4] ime $ime daljše od 4096";
if (!isset($t->getRootData()["source"])) {
echo "$argv[4] nima source ključa" . PHP_EOL;
@@ -95,10 +132,11 @@ try {
$vrata = intval(explode("/", $t->getRootData()["source"]["ip"])[1]);
if (isset($t->getRootData()["source"]["v"]))
$odjemalec = $t->getRootData()["source"]["v"];
- if (strlen($odjemalec) > 4096)
+ if (strlen($odjemalec ?: "") > 4096)
echo "$argv[4] odjemalec $odjemalec daljši od 4096";
$tip = $t->getProtocol();
- $sha256 = $t->getInfoHashV2();
+ $sha256 = hash("sha256", $i, true);
+ // echo "sha1: " . bin2hex($zgoščena_vrednost) . " sha256: " . bin2hex($sha256) . PHP_EOL; // debug
$s->execute();
try {
foreach ($t->getFileList() as $datoteka) {