-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi.php
More file actions
114 lines (103 loc) · 3.49 KB
/
api.php
File metadata and controls
114 lines (103 loc) · 3.49 KB
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
<?php
include('autoload.inc.php');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
function doNotCacheThis() {
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
}
function parseGETArray($s) {
$s = str_replace('%20','', $s);
$s = str_replace(' ','', $s);
if(substr($s,0,1) != '[') {
$s = "[$s";
}
if(substr($s,-1,1) != ']') {
$s = "$s]";
}
$s = str_replace('[','["', $s);
$s = str_replace(']','"]', $s);
$s = str_replace(',','","', $s);
return json_decode($s);
}
// TODO move this to ChuckAPI and output as debugging info is ?debug is given
$timer = new Timer();
if(isset($_GET['callback'])) {
header('Content-Type: text/javascript');
$api = ChuckScriptCommunicationAPI::getInstance();
$api->setCallback($_GET['callback']);
} else {
header('Content-Type: application/json');
$api = ChuckAPI::getInstance();
}
if(isset($_GET['escape'])) {
$escape = $_GET['escape'];
if($escape == "javascript") {
$api->setEscaper(new JavaScriptEscaper());
}
}
if(!isset($_SERVER['PATH_INFO'])) {
exit();
}
$path = $_SERVER['PATH_INFO'];
// process the request
if($path == '/categories') {
$api->echoCategories();
} else {
$firstName = 'Chuck';
if(isset($_GET['firstName'])) {
$firstName = $_GET['firstName'];
}
$lastName = 'Norris';
if(isset($_GET['lastName'])) {
$lastName = $_GET['lastName'];
}
if(preg_match('/^\/jokes\/random\/([0-9]+)/',$path, $matches)) { // think of the possible trailing '/' !!!
doNotCacheThis();
// return multiple quotes
$number = $matches[1];
if(isset($_GET['limitTo'])) {
$limitTo = parseGETArray($_GET['limitTo']);
$api->echoRandomQuotesBelongingTo($firstName, $lastName, $number, $limitTo);
} elseif(isset($_GET['exclude'])) {
$exclude = parseGETArray($_GET['exclude']);
$api->echoRandomQuotesExcluding($firstName, $lastName, $number, $exclude);
} else {
$api->echoRandomQuotes($firstName, $lastName, $number);
}
} elseif(preg_match('/^\/jokes\/random/',$path)) { // think of the possible trailing '/' !!!
doNotCacheThis();
// return a single quote
if(isset($_GET['limitTo'])) {
$limitTo = parseGETArray($_GET['limitTo']);
$api->echoRandomQuoteBelongingTo($firstName, $lastName, $limitTo);
} elseif(isset($_GET['exclude'])) {
$exclude = parseGETArray($_GET['exclude']);
$api->echoRandomQuoteExcluding($firstName, $lastName, $exclude);
} else {
$api->echoRandomQuote($firstName, $lastName);
}
} elseif(preg_match('/^\/jokes\/([0-9]+)/',$path, $matches)) { // think of the possible trailing '/' !!!
$api->echoQuoteById($firstName, $lastName, $matches[1]);
} elseif(preg_match('/^\/jokes\/count/',$path, $matches)) { // think of the possible trailing '/' !!!
$api->echoQuoteCount();
} elseif(preg_match('/^\/jokes/',$path)) { // think of the possible trailing '/' !!!
$offset = -1;
$limit = -1;
// enabling paging only if both criteria are available
if(isset($_GET['offset']) && isset($_GET['limit'])) {
$offset = $_GET['offset'];
$limit = $_GET['limit'];
}
if(isset($_GET['limitTo'])) {
$limitTo = parseGETArray($_GET['limitTo']);
$api->echoAllQuotesBelongingTo($firstName, $lastName, $limitTo, $offset, $limit);
} elseif(isset($_GET['exclude'])) {
$exclude = parseGETArray($_GET['exclude']);
$api->echoAllQuotesExcluding($firstName, $lastName, $exclude, $offset, $limit);
} else {
$api->echoAllQuotes($firstName, $lastName, $offset, $limit);
}
}
}
// do not log the request