-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
217 lines (186 loc) · 6.17 KB
/
index.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
require_once 'include/DbHandler.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$user_id = NULL;
/**
* JSON-ify response
*/
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response, JSON_UNESCAPED_SLASHES);
}
/**
* Verifying required params posted or not
*/
function verifyRequiredParams($required_fields) {
$error = false;
$error_fields = "";
$app = \Slim\Slim::getInstance();
$request_params = json_decode($app->request()->getBody());
if(!$request_params) {
$response = array();
$response["error"] = true;
$response["message"] = 'Malformed JSON input';
echoResponse(400, $response);
$app->stop();
}
foreach ($required_fields as $field) {
if (!array_key_exists($field, $request_params)) {
$error = true;
$error_fields .= $field . ', ';
}
}
if ($error) {
// Required field(s) are missing or empty
// echo error json and stop the app
$response = array();
$response["error"] = true;
$response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' are missing or empty';
echoResponse(400, $response);
$app->stop();
}
}
function validateEmail($email) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$app = \Slim\Slim::getInstance();
$response["error"] = true;
$response["message"] = 'Email address is not valid';
echoResponse(400, $response);
$app->stop();
}
}
function authenticate(\Slim\Route $route) {
// Getting request headers
$headers = apache_request_headers();
$response = array();
$app = \Slim\Slim::getInstance();
// Verifying Authorization Header
if (isset($headers['Authorization'])) {
$db = new DbHandler();
// get the api key
$api_key = $headers['Authorization'];
// validating api key
if (!$db->isValidApiKey($api_key)) {
// api key is not present in users table
$response["error"] = true;
$response["message"] = "Access Denied. Invalid API key";
echoResponse(401, $response);
$app->stop();
} else {
global $user_id;
// get user primary key id
$user = $db->getUserId($api_key);
if ($user != NULL)
$user_id = $user["id"];
}
} else {
// api key is missing in header
$response["error"] = true;
$response["message"] = "API Key is required";
echoResponse(400, $response);
$app->stop();
}
}
/**
* Slim application routes
*/
$app->get('/', function(){
echoResponse(200, true);
});
$app->get('/cuisine', function(){
$db = new DbHandler();
$response = $db->getAllCuisines();
echoResponse(200, $response);
});
$app->get('/bancarelle', function(){
$db = new DbHandler();
$response = $db->getAllBancarelle();
echoResponse(200, $response);
});
$app->post('/bancarelle', 'authenticate', function() use ($app){
verifyRequiredParams(array("name", "primary_cuisine", "secondary_cuisine"));
$db = new DbHandler();
$body = json_decode($app->request->getBody(), true);
$response = $db->insertNewBancarella($body["name"], $body["primary_cuisine"], $body["secondary_cuisine"]);
//Log creation of venue
global $user_id;
error_log("User " . $user_id . " added venue " . $body["name"]);
echoResponse(200, $response);
});
$app->post('/bancarelle/:id/rating', 'authenticate', function($bancarella_id) use ($app){
verifyRequiredParams(array("rating"));
global $user_id;
$db = new DbHandler();
$body = json_decode($app->request->getBody(), true);
$result = $db->rateBancarella($user_id, $bancarella_id, $body['rating']);
echoResponse(200, $result);
});
$app->post('/register', function() use ($app) {
verifyRequiredParams(array('name', 'email', 'password'));
$response = array();
$body = json_decode($app->request->getBody(), true);
$name = $body['name'];
$email = $body['email'];
$password = $body['password'];;
// validate email address - 400 if invalid
validateEmail($email);
$db = new DbHandler();
$res = $db->createUser($name, $email, $password);
switch($res){
case USER_ALREADY_EXISTED:
$response["error"] = true;
$response["message"] = "Sorry, this email already existed";
echoResponse(200, $response);
break;
case USER_CREATED_SUCCESSFULLY:
$response["error"] = false;
$response["message"] = "You are successfully registered";
echoResponse(201, $response);
break;
default:
$response["error"] = true;
$response["message"] = "An error occurred while registering";
echoResponse(200, $response);
break;
}
});
$app->post('/login', function() use ($app) {
// check for required params
verifyRequiredParams(array('email', 'password'));
// reading post params
$body = json_decode($app->request->getBody(), true);
$email = $body['email'];
$password = $body['password'];
$response = array();
$db = new DbHandler();
// check for correct email and password
if ($db->checkLogin($email, $password)) {
// get the user by email
$response = $db->getUserByEmail($email);
if ($response != NULL) {
$response["error"] = false;
} else {
// unknown error occurred
$response['error'] = true;
$response['message'] = "An error occurred. Please try again";
}
} else {
// user credentials are wrong
$response['error'] = true;
$response['message'] = 'Login failed. Incorrect credentials';
}
echoResponse(200, $response);
});
/**
* Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();