summaryrefslogblamecommitdiffstats
path: root/ru/kolo/api.php
blob: 64dd35c115abcc6bd4b254d68ba20be679dab883 (plain) (tree)
1
2
3
4
5
6
7
8





                              
                                                           
                                                 

















                                                                                                          





                                                                                                                     









                                                                                                                      
                                                        
                                                                                                                                              
                         






                                                                                                                                      

                                                                                          




                                                                                           




                                                                                                   




























                                                                                                    


                                            






























                                                                                                  



                                                





                                                                                                  


                                                                                                                                 
                                                    
                                                        
                                                                                                                 
                         








                                                                                                 
                                                
                                              







                                                                                                                                         
<?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;
	}

?>