summaryrefslogtreecommitdiffstats
path: root/www/insert.php
blob: e8ea85fcbd86e17b8e20f8dfcc1762f827fbc820 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/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;