-
Notifications
You must be signed in to change notification settings - Fork 1
/
book.class.php
437 lines (371 loc) · 16.7 KB
/
book.class.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/**
* @Desc BookStoreApi Class for all CURD operations
* @Date 25-06-2020
* @Author Sapan Mohanty
* @Skype sapan.mohannty
* @github https://github.com/travelxml
*/
ini_set('display_errors', false);
//error_reporting(E_ALL);
require 'config/Database.php';
require 'vendor/autoload.php';
use \Firebase\JWT\JWT;
class BookStoreApi
{
function __construct()
{
$headers = $_SERVER; //apache_request_headers();
$this->token = isset($headers['HTTP_AUTH']) ? $headers['HTTP_AUTH'] : '';
$this->key = "BookStoreAPI64#$%%^&*@@HS256";
$this->actionArray = array(
'create_book',
'read_book',
'update_book',
'delete_book'
);
$this->database = new Database();
$this->db = $this->database->connect();
$this->response = "";
}
function ResponseJson($jsonString = NULL)
{
$this->cors();
echo json_encode($jsonString);
exit;
}
/* Regular json_encode would return NULL due to memory issues.
* @param $arr
* @return string
*/
private function jsonEncode($arr)
{
$str = '{';
$count = count($arr);
$current = 0;
foreach ($arr as $key => $value)
{
$str .= sprintf('"%s":', $this->sanitizeForJSON($key));
if (is_array($value))
{
$str .= '[';
foreach ($value as & $val)
{
$val = $this->sanitizeForJSON($val);
}
$str .= '"' . implode('","', $value) . '"';
$str .= ']';
}
else
{
$str .= sprintf('"%s"', $this->sanitizeForJSON($value));
}
$current++;
if ($current < $count)
{
$str .= ',';
}
}
$str .= '}';
echo $str;
exit;
}
/**
* @param string $str
* @return string
*/
private function sanitizeForJSON($str)
{
// Strip all slashes:
$str = stripslashes($str);
// Only escape backslashes:
$str = str_replace('"', '\"', $str);
return $str;
}
/**
* @Desc Validate Authentication
*/
function validAuth()
{
if ($this->token != "") // Auth Token
{
//echo "token id".$this->token.", token key: ".$this->key;
$decodedv = JWT::decode($this->token, $this->key, array(
'HS256'
));
//print_r($decodedv);
$existUserId = $decodedv->id;
$existUsserEmail = isset($decodedv->email) ? $decodedv->email : '';
$existUserName = isset($decodedv->user_name) ? $decodedv->user_name : '';
$query = 'SELECT user_id FROM users WHERE user_id = :user_id AND email = :email AND user_name = :user_name LIMIT 0,1';
$getBooks = $this->db->prepare( $query );
$getBooks->bindParam( ':user_id', $existUserId );
$getBooks->bindParam( ':email', $existUsserEmail );
$getBooks->bindParam( ':user_name', $existUserName );
$getBooks->execute();
$noOfRecords = $getBooks->fetch();
if (!$noOfRecords) {
return 0;
} else {
return $existUserId;
}
}
else
{
return 0;
}
}
/**
* @Desc Validate API Request parameters
* @Return Json Error
*/
function validateApiP($apiRequests = NULL)
{
$apiP = explode(",", $apiRequests);
$cnt = 1;
foreach ($apiP as $key => $value)
{
$explodeIndividual = explode("@|@", $value);
$requestValue = isset($_POST[trim($explodeIndividual[0]) ]) ? $_POST[trim($explodeIndividual[0]) ] : $this->{trim($explodeIndividual[0]) };
$typeOfVariable = $explodeIndividual[1];
switch ($typeOfVariable)
{
case 'not_blank':
if (trim($requestValue) == '') $this->displayError($explodeIndividual[0] . ' can\'t be blank', $this->WhatToDo . ':' . $cnt, __LINE__);
break;
case 'int':
if (isset($requestValue) && is_numeric($requestValue)){}
else
{
$this->displayError($explodeIndividual[0]. ' should be numeric', $this->WhatToDo . ':' . $cnt, __LINE__);
}
break;
case 'date':
if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $requestValue)) {
} else {
$this->displayError($explodeIndividual[0] . ' date should be YYYY-MM-DD format', $this->WhatToDo . ':' . $requestValue, __LINE__);
}
break;
case 'isbn_10':
$check = 0;
for ($i = 0; $i < 10; $i++) {
if ('x' === strtolower($requestValue[$i])) {
$check += 10 * (10 - $i);
} elseif (is_numeric($requestValue[$i])) {
$check += (int)$requestValue[$i] * (10 - $i);
} else {
return false;
}
}
if( (0 === ($check % 11)) ){ }else {
$this->displayError($explodeIndividual[0] . ' should be valid isbn10 no.' , $this->WhatToDo . ':' . $cnt, __LINE__);
}
break;
}
$cnt++;
}
}
/**
* @Desc Format JSON Error
*/
function displayError($error = NULL, $error_no = NULL, $line_no = NULL)
{
$error = ($error != "") ? $error : 'Wrong Parameters';
$error_no = ($error_no > 0) ? $error_no : 103;
$result['error'] = $error;
$result['error_no'] = intval($error_no);
$this->cors();
echo json_encode($result);
exit;
}
function jwtEncode(){
$userArray =['id' => 1, 'user_name'=> 'sapan', 'email' => 'ctoattraveltech@gmail.com']; // just hardcoded for now .. it can be pull from users table as well
return JWT::encode($userArray, $this->key);
}
/**
* @Desc it takes request & build JSON response
*/
function storeApi($APIRequest)
{
$this->requestDetails = json_decode(trim($APIRequest), true);
if (json_last_error() > 0)
{
$this->displayError('Invalid JSON input', '999', __LINE__);
}
$this->WhatToDo = $this->requestDetails['action'];
$this->userId = $this->validAuth();
if ( $this->userId < 1) {
$this->displayError('Invalid Auth Token', '112', __LINE__);
}
foreach ($this->requestDetails as $key => $value) {
$this->requestDetails[trim($key) ] = $value;
$specialChar = array("%0D%0A", "%C2%A0");
$specialCharWith = array("", "");
$$key = isset($$key) ? str_replace($specialChar, $specialCharWith, $$key) : '';
if (get_magic_quotes_gpc()) { //echo "MQ enabled.<br />";
if (!is_array($$key)) { //echo "Not an array, going to stripslashes.<br />";
$$key = stripslashes(trim(urldecode($value)));
} else { //echo "Is an array, NOT going to stripslashes.<br />";
$$key = trim(urldecode($value));
}
} else {
$$key = trim(urldecode($value));
}
$$key = utf8_decode($$key); //necessary for $$key =str_replace("%C2%A0","",$$key); condition does not work and many other characters
//echo "\$$key = ".$$key."<br />";
}
$results = array( );
switch ($this->WhatToDo)
{
case 'create_book': // create a book
$this->author = isset( $this->requestDetails['author_name'] ) ?$this->requestDetails['author_name']:'';
$this->title = isset( $this->requestDetails['title'] ) ?$this->requestDetails['title']:'';
$this->isbn = isset( $this->requestDetails['isbn'] ) ?$this->requestDetails['isbn']:'';
$this->releaseDate = isset( $this->requestDetails['release_date'] ) ?$this->requestDetails['release_date']:'';
$this->validateApiP('author@|@not_blank,title@|@not_blank,isbn@|@isbn_10');
$query = 'INSERT INTO books SET author_name = :author_name, title = :title, isbn = :isbn, user_id = :user_id';
if($this->releaseDate != "") {
$this->validateApiP('release_date@|@date');
$query .= ', release_date = :release_date';
}
$stmt = $this->db->prepare($query);
$stmt->bindParam(':author_name', $this->author);
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':isbn', $this->isbn);
$stmt->bindParam(':user_id', $this->userId);
if($this->releaseDate != "") {
$stmt->bindParam(':release_date', $this->release_date);
}
try {
$stmt->execute();
$success = 1;
$displayMessage = ['message' => 'book created successfully'];
} catch (PDOException $e) {
$success = 0;
if ($e->errorInfo[1] == 1062) {
$displayMessage = ['message' => 'duplicate entry'];
} else {
$displayMessage = ['message' => 'db error'];
}
}
$results['success'] = $success;
$results['data'] = $displayMessage;
$this->ResponseJson( $results );
break;
case 'update_book': // update a book
$this->author = isset( $this->requestDetails['author_name'] ) ?$this->requestDetails['author_name']:'';
$this->title = isset( $this->requestDetails['title'] ) ?$this->requestDetails['title']:'';
$this->isbn = isset( $this->requestDetails['isbn'] ) ?$this->requestDetails['isbn']:'';
$this->releaseDate = isset( $this->requestDetails['release_date'] ) ?$this->requestDetails['release_date']:'';
$this->id = isset( $this->requestDetails['id'] ) ?$this->requestDetails['id']:'';
$this->validateApiP('id@|@int,author@|@not_blank,title@|@not_blank,isbn@|@isbn_10');
$query = 'UPDATE books SET author_name = :author_name, title = :title, isbn = :isbn, user_id = :user_id';
if($this->releaseDate != "") {
$this->validateApiP('release_date@|@date');
$query .= ', release_date = :release_date';
}
$query .= ' WHERE book_id =:id ';
$stmt = $this->db->prepare($query);
$stmt->bindParam(':author_name', $this->author);
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':isbn', $this->isbn);
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':user_id', $this->userId);
if($this->releaseDate != "") {
$stmt->bindParam(':release_date', $this->release_date);
}
try {
$stmt->execute();
$success = 1;
$displayMessage = ['message' => 'book details are updated successfully'];
} catch (PDOException $e) {
$success = 0;
if ($e->errorInfo[1] == 1062) {
$displayMessage = ['message' => 'duplicate entry'];
} else {
$displayMessage = ['message' => 'db error'];
}
}
$results['success'] = $success;
$results['data'] = $displayMessage;
$this->ResponseJson( $results );
break;
case 'read_book': // read books // or a book
$this->id = isset( $this->requestDetails['id'] ) ?$this->requestDetails['id']:'';
$query = 'SELECT book_id, title, author_name, isbn, release_date,last_update FROM books WHERE user_id = :user_id';
if($this->id != ''){
$this->validateApiP('id@|@int');
$query .=" AND book_id = :book_id LIMIT 0,1";
$getBooks = $this->db->prepare($query);
$getBooks->bindParam(':book_id', $this->id);
} else {
$getBooks = $this->db->prepare($query);
}
$getBooks->bindParam(':user_id', $this->userId);
$booksArr = array();
$getBooks->execute();
$success = 1;
while($row = $getBooks->fetch(PDO::FETCH_ASSOC)){
extract($row);
$bookItem = array(
'id' => $book_id,
'title' => $title,
'author' => $author_name,
'isbn' => $isbn,
'release_date' => $release_date,
'last_update' => $last_update
);
array_push($booksArr, $bookItem);
}
$results['success'] = $success;
$results['data'] = (count($booksArr)>0)?$booksArr:['message' => 'invalid id'];
$this->ResponseJson( $results );
break;
case 'delete_book': // delete a book
$this->id = isset( $this->requestDetails['id'] ) ?$this->requestDetails['id']:'';
$this->validateApiP('id@|@int');
$query = 'DELETE FROM books WHERE book_id = :id AND user_id = :user_id';
$stmt = $this->db->prepare($query);
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':user_id', $this->userId);
try {
$stmt->execute();
if ($stmt->rowCount()) {
$success = 1;
$displayMessage = ['message' => 'book deleted successfully'];
} else{
$success = 0;
$displayMessage = ['message' => 'invalid id'];
}
} catch (PDOException $e) {
//var_dump($e);
$success = 0;
$displayMessage = ['message' => 'invalid id'];
}
$results['success'] = $success;
$results['data'] = $displayMessage;
$this->ResponseJson( $results );
break;
}
}
function cors() {
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
}
header('Content-Type: application/json; charset=utf-8');
}
} // class end
?>