summaryrefslogtreecommitdiffstats
path: root/ru/kolo/api.php
blob: 64dd35c115abcc6bd4b254d68ba20be679dab883 (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
<?php

	$dbuser = "kolo";
	$dbhost = "localhost";
	$dbdb = "kolo";
	$dbpw = "kolo";
	$conn = new mysqli($dbhost, $dbuser, $dbpw, $dbdb);
	header("Content-Type: application/json");
	if ($conn->connect_error) {
		http_response_code(500);
		exit('{"napaka":true,"obvestilo":"Napaka strežnika pri povezavi v podatkovno zbirko."}');
	}
	$query = "CREATE TABLE IF NOT EXISTS kolesa (
		omogoceno INT,
		ime VARCHAR(420),
		admin	VARCHAR(420)
	);
	CREATE TABLE IF NOT EXISTS ljudje (
		ime VARCHAR(420) PRIMARY KEY,
		izbranec VARCHAR(420),
		kolo VARCHAR(420),
		krneki VARCHAR(420)
	)";
	$result = $conn->query($query);
	var_dump($result);
	var_dump($conn->error);
	if (empty($_REQUEST['k'])) {
		http_response_code(400);
		exit ('{"napaka":true,"obvestilo":"Ne vem, na katero kolo se sklicuješ, ker manjka parameter k."}');
	}
	switch ($_REQUEST['m']) {
		case "r": /* registracija */
			if (empty($_REQUEST['i'])) {
				http_response_code(400);
				exit ('{"napaka":true,"obvestilo":"Ne vem, kako ti je ime, ker manjka parameter i"}');
			}
			$stmt2 = $conn->prepare("SELECT omogoceno FROM `kolesa` WHERE `ime`=?");
			$stmt2->bind_param("s", $_REQUEST["k"]);
			$stmt2->execute();
			$stmt2->bind_result($omogoceno);
			$stmt2->fetch();
			if ($omogoceno != 0) {
				http_response_code(400);
				exit ('{"napaka":true,"obvestilo":"Ne morem registrirati novih ljudi, ker se je vrtenje koles že začelo.}');
			}
			$stmt2->close();
			$stmt = $conn->prepare("INSERT INTO ljudje (ime, kolo) VALUES (?, ?) ON DUPLICATE KEY UPDATE `krneki`='123'");
			$stmt->bind_param("ss", $_REQUEST["i"], $_REQUEST["k"]);
			$stmt->execute();
			$stmt->close();
			http_response_code(201);
			exit ('{"napaka":false,"obvestilo":"Uporabnik registriran!"}');
			break;
		case "z": /* zakleni kolo, omogoči vrtenje, naključno razporedi osebe */
			$stmt = $conn->prepare("SELECT admin FROM kolesa WHERE `ime`='?'");
			$stmt->bind_param("s", $_REQUEST["k"]);
			$stmt->execute();
			$stmt->bind_result($admin_pass);
			$stmt->fetch();
			if ($_REQUEST['g'] !== $admin_pass) {
				http_response_code(400);
				exit ('{"napaka":true,"obvestilo":"Ne bom zaklenil tega kolesa, ker
					niste administrator."}');
			}
			$stmt->close();
			$stmt = $conn->prepare("UPDATE kolesa SET `omogoceno`=1 WHERE `ime` = '?'");
			$stmt->bind_param("s", $_REQUEST["k"]);
			$stmt->execute();
			$stmt->close();
			$stmt2 = $conn->prepare("SELECT ime FROM ljudje WHERE `kolo`=?");
			$stmt2->bind_param("s", $_REQUEST["k"]);
			$stmt2->execute();
			$result = $stmt->get_result();
			$ljudje = $result->fetch_all(MYSQLI_ASSOC);
			$ljudje2 = $ljudje;
			shuffle($ljudje);
			for ($i = 0; $i < sizeof($ljudje);) {
				if ($ljudje[$i] == $ljudje2[$i]) {
					shuffle($ljudje);
					$i = 0;
					continue;
				}
				$i++;
			}
			$stmt3 = $conn->prepare("UPDATE ljudje SET `izbranec`=? WHERE `ime`=?");
			for ($i = 0; $i < sizeof($ljudje); $i++) {
				$stmt3->bind_param("ss", $ljudje[$i], $ljudje2[$i]);
				$stmt3->execute();
			}
			$stmt3->close();
			$stmt2->close();
			http_response_code(200);
			exit ('{"napaka":false,"obvestilo":"uspešno zaklenil kolo."}');
			break;
		case "u": /* srčni utrip */
			$r = array();
			$stmt2 = $conn->prepare("SELECT ime FROM ljudje WHERE `kolo`=?");
			$stmt2->bind_param("s", $_REQUEST["k"]);
			$stmt2->execute();
			$result = $stmt->get_result();
			$ljudje = $result->fetch_all(MYSQLI_ASSOC);
			$r["moznosti_na_kolesu"] = $ljudje;
			$stmt = $conn->prepare("SELECT admin FROM kolesa WHERE `ime`=?");
			$stmt->bind_param("s", $_REQUEST["k"]);
			$stmt->execute();
			$stmt->bind_result($admin_pass);
			$stmt->fetch();
			if ($admin_pass === $_REQUEST["g"]) {
				$r["si_administrator"] = true;
			} else {
				$r["si_administrator"] = false;
				$r["si_administrator"] = false;
			}
			$stmt3 = $conn->prepare("SELECT omogoceno FROM `kolesa` WHERE `ime` = ?");
			$stmt3->bind_param("s", $_REQUEST["k"]);
			$stmt3->execute();
			$stmt3->bind_result($omogoceno);
			$stmt3->fetch();
			if ($omogoceno !== 1) {
				$r["dovoli_vrtenje"] = false;
			} else {
				$r["dovoli_vrtenje"] = true;
			}
			$r["napaka"] = false;
			$stmt2->close();
			$stmt->close();
			$stmt3->close();
			http_response_code(200);
			exit (json_encode($r));
			break;
		case "v": /* vrti */
			$stmt3 = $conn->prepare("SELECT omogoceno FROM `kolesa` WHERE `ime` = ?");
			$stmt3->bind_param("s", $_REQUEST["k"]);
			$stmt3->execute();
			$stmt3->bind_result($omogoceno);
			$stmt3->fetch();
			if ($omogoceno !== 1 /* a je vrtenje nedovoljeno */) {
				http_response_code(400);
				exit ('{"napaka":true,"obvestilo":"Ne morem vrteti, ker vrtenja še ni odobril administrator"}');
			}
			if (empty($_REQUEST["i"])) {
				http_response_code(400);
				exit ('{"napaka":true,"obvestilo":"Ne morem vrteti, ker nimam vašega imena."}');
			}
			$stmt2 = $conn->prepare("SELECT izbranec FROM `ljudje` WHERE `ime` = ?");
			$stmt2->bind_param("s", $_REQUEST["i"]);
			$stmt2->execute();
			$stmt2->bind_result($izbranec);
			$stmt2->fetch();
			$r["izbranec"] = $izbranec;
			$r["napaka"] = false;
			$stmt3->close();
			$stmt2->close();
			http_response_code(200);
			exit(json_encode($r));
			break;
		default:
			http_response_code(400);
			exit ('{"napaka":true,"obvestilo":"Ne vem, kaj hočeš,. ker je bila zahtevana napačna metoda, parameter m."}');
			break;
	}

?>