diff options
-rw-r--r-- | misc/analiza.txt | 2 | ||||
-rwxr-xr-x | www/insert.php | 54 |
2 files changed, 47 insertions, 9 deletions
diff --git a/misc/analiza.txt b/misc/analiza.txt index ac2ef30..bb8b5f8 100644 --- a/misc/analiza.txt +++ b/misc/analiza.txt @@ -1 +1 @@ -cd www; find .. -maxdepth 2 -name *.torrent | parallel ./insert.php 'mysql:host=tranzistor:3306\;dbname=travnik\;charset=utf8' travnik $TRAVNIK_DB_PASSWORD {} | tee ../tmp/insert.txt; ocd +cd www; find .. -maxdepth 1 -name *.torrent | parallel ./insert.php 'mysql:host=tranzistor:3306\;dbname=travnik\;charset=utf8' travnik $TRAVNIK_DB_PASSWORD {} | tee ../tmp/insert.txt; ocd 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) { |