-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.php
123 lines (115 loc) · 3.68 KB
/
rest.php
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
<?php
//Klase
require_once 'Klase/KorisnikDao.php';
require_once 'Klase/Korisnik.php';
//Funkcija zaglavlje (Postavke headera)
function zag() {
header("{$_SERVER['SERVER_PROTOCOL']} 200 OK");
header('Content-Type: text/html');
header('Access-Control-Allow-Origin: *');
}
//REST funkcije za manipulaciju podacima
function rest_get($request, $data) {
$id = explode("/", $request);
$id = array_pop($id);
if(is_int($id)){
$tip = array_pop($id);
switch($tip){
case 'fotografije':
$rezultat = array();
break;
}
print "{ \"$tip\": " . json_encode($rezultat) . "}";
} else{
$tip = $id;
switch($tip){
case 'fotografije':
$rezultat = array();
break;
}
print "{ \"".$tip."\": " . json_encode($rezultat) . "}";
}
}
function rest_post($request, $data) {
$uri = explode("/", $request);
$tip = array_pop($uri);
switch($tip){
case 'login':
$dao = new \Dao\KorisnikDao();
$email = htmlentities($data['email']);
$pass = htmlentities($data['password']);
$hash = md5($pass);
$logged =$dao->getLogin($email, $hash);
if($logged){
session_start();
$usr = $dao->getByExample('email', $email);
$usr = $usr[0];
$username = $usr->getIme();
$_SESSION['username'] = $username;
$id= $usr->getId();
$_SESSION['korisnikId'] = $id;
}
if(!$logged)
rest_error("Pogrešni podaci.");
return;
break;
case 'logout':
session_start();
if(isset($_SESSION['username']) && $_SESSION['username'] == $data['username']) {
unset($_SESSION['username']);
session_destroy();
}else
rest_error("Niste prijavljeni.");
return;
break;
case 'register':
session_start();
try {
$korisnik = new Korisnik();
$ime = htmlentities($data['ime']);
$prezime = htmlentities($data['prezime']);
$korisnik->setIme($ime . " " . $prezime);
$korisnik->setEmail(htmlentities($data['email']));
$password = htmlentities($data['password']);
$korisnik->setPassword(md5($password));
$kdao = new \Dao\KorisnikDao();
$kdao->create($korisnik);
$username = $ime . " " . $prezime;
$_SESSION['username'] = $username;
$id=$korisnik->getId();
$_SESSION['korisnikId']=$id;
}catch (Exception $e){
rest_error($e->getMessage());
}
break;
}
}
function rest_delete($request) {
}
function rest_put($request, $data) {
}
function rest_error($error) {
$json = "{ \"Greška\": ".json_encode($error)."}";
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
print $json;
}
$method = $_SERVER['REQUEST_METHOD'];
$request = $_SERVER['REQUEST_URI'];
switch($method) {
case 'PUT':
parse_str(file_get_contents('php://input'), $put_vars);
zag(); $data = $put_vars;
rest_put($request, $data); break;
case 'POST':
zag(); $data = $_POST;
rest_post($request, $data); break;
case 'GET':
zag(); $data = $_GET;
rest_get($request, $data); break;
case 'DELETE':
zag(); rest_delete($request); break;
default:
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
rest_error($request); break;
}
?>