-
Notifications
You must be signed in to change notification settings - Fork 0
/
cinemas.php
128 lines (102 loc) · 3.67 KB
/
cinemas.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
124
125
126
127
128
<?php
// error_reporting(-1); // reports all errors
// ini_set("display_errors", "1"); // shows all errors
// ini_set("log_errors", 1);
// ini_set("error_log", "/tmp/php-error.log");
header('Content-type:application/json;');
require_once 'assets/classes/cinema.php';
require_once 'assets/classes/apikey.php';
$apikey = new apikey();
$apikey->checkkey();
$cinemas = new cinema();
$json = array();
if ($_SERVER['REQUEST_METHOD'] === 'GET' && $apikey->Level >= 1) {
// check if acces is granted.
// check if user has the right level.
if($apikey->getstatus() == 1){
if(!isset($_GET['id'])){
// get all items.
$cinemas = $cinemas->find_all();
// put all items in json array.
foreach($cinemas as $cinema):
$json[]= $cinema;
endforeach;
// show jwt key if client uses api for the first time.
if($apikey->getused() == 0) {
$json['jwtkey'] = $apikey->getjwtkey();
}
// update used column to 1;
$apikey->Used = 1;
$apikey->update($apikey->Id);
// display all items in json
echo json_encode($json);
} else if (isset($_GET['id']) && is_numeric($_GET['id'])){
// echo $_GET['id'];
$cinemas = $cinemas->find_by_id($_GET['id']);
// show jwt key if client uses api for the first time.
if($apikey->getused() == 0) {
$cinemas['jwtkey'] = $apikey->getjwtkey();
}
// update used column to 1;
$apikey->Used = 1;
$apikey->update($apikey->Id);
// display all items in json
echo json_encode($cinemas);
}
} else {
$json['access'] = "invalid keys";
echo json_encode($json);
}
} else if ($_SERVER['REQUEST_METHOD'] === 'POST' && $apikey->Level >= 2) {
if($apikey->getstatus() == 1){
$json['access'] = "level 2 access";
// show jwt key if client uses api for the first time.
if($apikey->getused() == 0) {
$json['jwtkey'] = $apikey->getjwtkey();
}
// update used column to 1;
$apikey->Used = 1;
$apikey->update($apikey->Id);
// display all items in json
echo json_encode($json);
} else {
$json['access'] = "invalid keys for level 2";
echo json_encode($json);
}
} else if ($_SERVER['REQUEST_METHOD'] === 'PUT' && $apikey->Level >= 3) {
if($apikey->getstatus() == 1){
$json['access'] = "level 3 access";
// show jwt key if client uses api for the first time.
if($apikey->getused() == 0) {
$json['jwtkey'] = $apikey->getjwtkey();
}
// update used column to 1;
$apikey->Used = 1;
$apikey->update($apikey->Id);
// display all items in json
echo json_encode($json);
} else {
$json['access'] = "invalid keys for level 3";
echo json_encode($json);
}
} else if ($_SERVER['REQUEST_METHOD'] === 'DELETE' && $apikey->Level >= 4) {
if($apikey->getstatus() == 1){
$json['access'] = "level 4 access";
// show jwt key if client uses api for the first time.
if($apikey->getused() == 0) {
$json['jwtkey'] = $apikey->getjwtkey();
}
// update used column to 1;
$apikey->Used = 1;
$apikey->update($apikey->Id);
// display all items in json
echo json_encode($json);
} else {
$json['access'] = "invalid keys for level 4";
echo json_encode($json);
}
} else {
$json['access'] = "no access";
echo json_encode($json);
}
?>