summaryrefslogblamecommitdiffstats
path: root/www/insert.php
blob: e8ea85fcbd86e17b8e20f8dfcc1762f827fbc820 (plain) (tree)
1
2

              






























                                                             






                                                                                                                                                       





                                                                           

                                                            
                                                
     
                                                                        
                                                  
                                              


                            
                                                                              



                                                       
                                                     






                                                                                                                                                                                                                                                                                    
                                        
                                                        
                                          



                                                           
                                                                 




                                                                                  
                     






                                  


                          








                                   


                           









                                                                

                                          



                                                                  
                                        

                                                             
                                      








                                                                            
                                            

                                                                     

                                                                                                             
                      

                                                          











                                                                        

                                                      

                                                                         

                                      










                                                                                   
#!/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;
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);
}
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 = sha1($i, true);
	$s->execute();
} catch (PDOException $e) {}
if ($s->fetchColumn()) {
	echo "torrent $argv[4] je že shranjen v podatkovni zbirki" . PHP_EOL;
	exit(0);
}
try {
	$c->exec("CREATE TABLE IF NOT EXISTS torrenti (
		sha1 BINARY(20) NOT NULL PRIMARY KEY,
		pridobljeno TIMESTAMP NOT NULL,
		velikost_metainfo INT UNSIGNED NOT NULL COMMENT 'sem se štejejo tudi informacije, ki jih doda travnik, recimo created by, datum izdelave, vir in odjemalec. to ni le velikost info slovarja, vendar je striktno večja, saj je velikost celotne torrent datoteke.',
		velikost_koščka INT UNSIGNED NOT NULL,
		izvor VARBINARY(4096),
		ime VARBINARY(4096),
		ip BINARY(16) NOT NULL,
		vrata SMALLINT UNSIGNED NOT NULL,
		odjemalec VARCHAR(4096),
		tip ENUM('v1', 'v2', 'hybrid') NOT NULL,
		sha256 BINARY(32) NOT NULL
	)");
	$c->exec('CREATE TABLE IF NOT EXISTS datoteke (
		id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
		torrent BINARY(20) NOT NULL,
		FOREIGN KEY (torrent) REFERENCES torrenti (sha1),
		pot VARBINARY(4096) NOT NULL,
		velikost BIGINT NOT NULL
	)');
	$c->beginTransaction(); // glej php.net/manual/en/pdo.begintransaction.php
	$s = $c->prepare("INSERT INTO torrenti (
		sha1,
		pridobljeno,
		velikost_metainfo,
		velikost_koščka,
		izvor,
		ime,
		ip,
		vrata,
		odjemalec,
		tip,
		sha256
	) VALUES (
		:z,
		:pridobljeno,
		:velikost_metainfo,
		:v,
		:izvor,
		:ime,
		INET6_ATON(:ip),
		:vrata,
		:odjemalec,
		:tip,
		:sha256
	)");
	$s->bindParam(":z", $zgoščena_vrednost);
	$s->bindParam(":pridobljeno", $pridobljeno);
	$s->bindParam(":velikost_metainfo", $velikost_metainfo);
	$s->bindParam(":v", $velikost_koščka);
	$s->bindParam(":izvor", $izvor);
	$s->bindParam(":ime", $ime);
	$s->bindParam(":ip", $ip);
	$s->bindParam(":vrata", $vrata);
	$s->bindParam(":odjemalec", $odjemalec);
	$s->bindParam(":tip", $tip);
	$s->bindParam(":sha256", $sha256);
	$pridobljeno = date("Y-m-d H:i:s", $t->getCreationDate());
	$velikost_metainfo = filesize($argv[4]);
	$velikost_koščka = $t->getPieceLength();
	$izvor = $t->getSource();
	if (strlen($izvor ?: "") > 4096)
		echo "$argv[4] izvor $izvor daljši od 4096";
	$ime = $t->getName();
	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;
		exit(4);
	}
	$ip = explode("/", $t->getRootData()["source"]["ip"])[0];
	$vrata = intval(explode("/", $t->getRootData()["source"]["ip"])[1]);
	if (isset($t->getRootData()["source"]["v"]))
		$odjemalec = $t->getRootData()["source"]["v"];
	if (strlen($odjemalec ?: "") > 4096)
		echo "$argv[4] odjemalec $odjemalec daljši od 4096";
	$tip = $t->getProtocol();
	$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) {
			$s = $c->prepare("INSERT INTO datoteke (
				torrent,
				pot,
				velikost
			) VALUES (
				:torrent,
				:pot,
				:velikost
			)");
			$s->bindParam(":torrent", $zgoščena_vrednost);
			$s->bindParam(":pot", $pot);
			$s->bindParam(":velikost", $velikost);
			$pot = $datoteka["path"];
			$velikost = $datoteka["size"];
			if (strlen($pot) > 4096)
				echo "$argv[4] pot $pot daljša od 4096";
			$s->execute();
		}
	} catch (ParseException $e) {
		echo "neveljaven metainfo $argv[4]: " . $e->getMessage() . PHP_EOL;
		exit(5);
	}
	$c->commit();
} catch (PDOException $e) {
	echo "sql napaka pri $argv[4]: " . $e->getMessage() . PHP_EOL;
	$c->rollback();
	exit(2);
}
$c = null;