summaryrefslogtreecommitdiffstats
path: root/www/insert.php
blob: f6da24bd4b32bfef1371bfef807e5084d0e9a58a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/php
<?php
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);
}
$t = TorrentFile::load($argv[4]);
$c = new PDO($argv[1], $argv[2], $argv[3]);
$c->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
	$s = $c->prepare("SELECT COUNT(1) FROM torrenti WHERE sha1=:z");
	$s->bindParam(":z", $zgoščena_vrednost);
	$zgoščena_vrednost = $t->getInfoHashV1(true);
	$s->execute();
} catch (PDOException $e) {}
if ($s->fetchColumn()) {
	echo "torrent 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 = $t->getInfoHashV2();
	$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;