This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
175 lines (160 loc) · 4.59 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
<?php
/**
* @file
* Holdes basic web-services logic and returns JSONP encode reponses based on
* the input parmateres.
*
* It exposes 2 methods:
* getFact -> index.php?method=getFact&callback=parseJson&guid=2
* getFacts -> index.php?method=getFacts&offset=25&count=25&callback=parseJson
* getGuid -> index.php?method=getGuid&callback=parseJson
*
*
* If an error is detected the following JSONP will be returned, so make sure to have
* an error callback function.
*
* error({"msg":"The GUID provided is not a number!","code":-1})
*/
include_once dirname(__FILE__).'/utils/utils.inc';
include_once dirname(__FILE__).'/database/fact.inc';
include_once dirname(__FILE__).'/utils/syslogger.inc';
/**
* Defines the methods avaliable.
*/
define('FFF_ERROR_CALLBACK', 'error');
define('FFF_GET_FACT', 'getfact');
define('FFF_GET_FACTS', 'getfacts');
define('FFF_GET_GUID', 'getguid');
define('FFF_GET_COUNT', 'getcount');
/**
* Helper function that parses the GET parameters into an options array.
*
* @return array $options
* The array contains as minimum the options 'method' and 'callback'.
*/
function parseOptions() {
$options = array();
// Try to get method and callback parameters.
try {
$options['method'] = Utils::getMethod();
$options['callback'] = Utils::getCallback();
} catch (Exception $e) {
// The was an error in getting the parameters, so return an error message.
returnJsonError(array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
));
}
// If getFact is called, try to get the guid form the URL.
if ($options['method'] == FFF_GET_FACT) {
try {
$options['guid'] = Utils::getGUID();
} catch (Exception $e) {
// The was an error in getting the GUID, so return an error message.
returnJsonError(array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
));
}
}
if ($options['method'] == FFF_GET_FACTS) {
try {
$options['offset'] = Utils::getOffset();
$options['count'] = Utils::getCount();
} catch (Exception $e) {
// The was an error in getting the offset, so return an error message.
returnJsonError(array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
));
}
}
return $options;
}
/**
* Helper function to return an error message in JSONP with the error callback.
*
* @param array $result
* The array should contain the keys 'msg' and 'code'.
*/
function returnJsonError($result) {
header('Content-type: application/json');
echo FFF_ERROR_CALLBACK . '(' . json_encode($result) . ')';
exit(isset($result['code']) ? $result['code'] : -1);
}
/**
* Encodes the $result parameter into JSON and wraps it in the callback, it then
* prints the result, thereby returning it to the user.
*
* @param array $result
* @param string $callback
*/
function returnJson($result, $callback) {
header('Content-type: application/json');
if ($callback) {
echo $callback . '(' . json_encode($result) . ')';
}
else {
echo json_encode($result);
}
}
// Check the API key given and log the request.
try {
$api_key = Utils::getAPIKey();
Utils::isAPIKeyValid();
SysLogger::getLogger()->log($api_key, 'Accesed', LOG_INFO);
} catch (Exception $e) {
if (!isset($api_key)) {
$api_key = 'unknown';
}
SysLogger::getLogger()->log($api_key, 'Invalid API-KEY', LOG_ERR);
returnJsonError(array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
));
exit(-1);
}
// Main part of index, which takes action based on the options parsed by the
// helper functions above.
$options = parseOptions();
switch ($options['method']) {
case FFF_GET_FACT:
try {
$fact = new Fact(array('guid' => $options['guid']));
returnJson($fact->getFact(), $options['callback']);
} catch (Exception $e) {
returnJsonError(array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
));
}
exit(1);
break;
case FFF_GET_FACTS:
try {
$facts = Fact::getFacts($options['offset'], $options['count']);
returnJson($facts, $options['callback']);
} catch (Exception $e) {
returnJsonError(array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
));
}
exit(1);
break;
case FFF_GET_GUID:
returnJson(array('guid' => Fact::getGUID()), $options['callback']);
exit(1);
break;
case FFF_GET_COUNT:
returnJson(array('count' => Fact::getCount()), $options['callback']);
exit(1);
break;
default:
returnJsonError(array(
'msg' => 'Method is not available',
'code' => -1,
));
exit(-1);
break;
}