/
rest.class.php
426 lines (370 loc) · 11.4 KB
/
rest.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
<?php
class RestUtils {
public static function authentication() {
// print_r($_SERVER);
// exit();
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
$db = new DB();
$username = mysql_real_escape_string($_SERVER['PHP_AUTH_USER']);
$password = mysql_real_escape_string($_SERVER['PHP_AUTH_PW']);
$domain = 'nebula.com';
$hash = md5($username .':'. $domain .':'. $password);
$sql = "SELECT id from n_nebulauser WHERE username='$username' AND ha1='$hash'";
$q = $db->query($sql);
if( mysql_num_rows( $q ) ){
$fetch = mysql_fetch_assoc($q);
return $fetch['id'];
}
else
return null;
}
return false;
}
public static function processRequest() {
// get our verb
$request_method = strtolower($_SERVER['REQUEST_METHOD']);
$element = $_GET['REST_element'];
$id = null;
$operation = null;
if(isset($_GET['REST_id']))
$id = $_GET['REST_id'];
if(isset($_GET['REST_operation']))
$operation = $_GET['REST_operation'];
$return_obj = new RestRequest();
// we'll store our data here
$data = array();
//TO recode
switch ($request_method) {
// gets are easy...
//already got element and id
case 'get':
$data = $_GET;
unset($data['REST_element']);
unset($data['REST_id']);
unset($data['REST_operation']);
unset($data['REST_format']);
break;
// so are posts
/*case 'post':
$data = $_POST;
break;*/
// here's the tricky bit...
case 'put':
case 'post':
// basically, we read a string from PHP's special input location,
// and then parse it out into an array via parse_str... per the PHP docs:
// Parses str as if it were the query string passed via a URL and sets
// variables in the current scope.
$contents = file_get_contents('php://input');
//die('-'.$contents.'-');
$data = RestUtils::data_decode( $contents, $_SERVER['CONTENT_TYPE'] );
//$data = $contents;
break;
}
// store the data
$return_obj->setData($data);
// store the method
$return_obj->setMethod($request_method);
// store the method
$return_obj->setOperation($operation);
// store the element
$return_obj->setElement($element);
// store the id
$return_obj->setID($id);
// set the raw data, so we can access it if needed (there may be
// other pieces to your requests)
$return_obj->setRequestVars($data);
return $return_obj;
}
/**
* Simple recursive function to build an XML response.
*/
public static function xml_encode($k, $v) {
if (is_object ($v) && strtolower (get_class ($v)) == 'simplexmlelement') {
return preg_replace ('/<\?xml(.*?)\?>/', '', $v->asXML ());
}
$res = '';
$attrs = '';
if (! is_numeric ($k)) {
$res = '<' . $k . '{{attributes}}>';
}
if (is_array ($v)) {
foreach ($v as $key => $value) {
if (strpos ($key, '@') === 0) {
$attrs .= ' ' . substr ($key, 1) . '="' . $this->_xml_entities ($value) . '"';
continue;
}
$res .= RestUtils::xml_encode($key, $value);
$keys = array_keys ($v);
if (is_numeric ($key) && $key != array_pop ($keys)) {
$res .= '</' . $k . ">\n<" . $k . '>';
}
}
} else {
$res .= RestUtils::_xml_entities ($v);
}
if (! is_numeric ($k)) {
$res .= '</' . $k . ">\n";
}
$res = str_replace ('<' . $k . '{{attributes}}>', '<' . $k . $attrs . '>', $res);
return $res;
}
/**
* Converts entities to unicode entities (ie. < becomes <).
* From php.net/htmlentities comments, user "webwurst at web dot de"
*/
public static function _xml_entities ($string) {
$trans = get_html_translation_table (HTML_ENTITIES);
foreach ($trans as $key => $value) {
$trans[$key] = '&#' . ord ($key) . ';';
}
return strtr ($string, $trans);
}
public static function xml_decode($contents) {
$xml = new SimpleXMLElement ($contents);
//if ($xml->getName () == 'nebula') {
// multiple
/*$res = array ();
//$cls = get_class ($this);
//$cls = $xml->getName();
foreach ($xml->children () as $child) {
$obj = new stdClass();
foreach ((array) $child as $k => $v) {
$k = str_replace ('-', '_', $k);
if (isset ($v['nil']) && $v['nil'] == 'true') {
continue;
} else {
$obj->_data[$k] = $v;
}
}
$res[] = $obj;
}*/
$res = array ();
//$cls = get_class ($this);
//$cls = $xml->getName();
foreach ($xml->children () as $child) {
$obj = new stdClass();
foreach ((array) $child as $k => $v) {
$k = str_replace ('-', '_', $k);
if (isset ($v['nil']) && $v['nil'] == 'true') {
continue;
} else {
$obj->_data[$k] = $v;
}
}
$res[] = $obj;
}
echo $res;
return $res;
/*} elseif ($xml->getName () == 'errors') {
// parse error message
$this->error = $xml->error;
$this->errno = $this->response_code;
return false;
}*/
}
public static function data_encode($contents, $type='json') {
switch($type) {
case 'json':
return json_encode($contents, JSON_FORCE_OBJECT);
case 'xml':
return RestUtils::xml_encode('nebula', $contents);
}
}
public static function data_decode($contents, $type='json') {
if( strpos($type, 'json') )
return json_decode($contents, true);
else
//return RestUtils::xml_decode($contents);
$xml = simplexml_load_string($contents);
return RestUtils::objectsIntoArray($xml); }
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
public static function getStatusCodeMessage($status) {
// these could be stored in a .ini file and loaded
// via parse_ini_file()... however, this will suffice
// for an example
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
public static function sendResponse($status = 200, $body = '', $type = 'json') {
if($body == '')
$body = array();
elseif(is_string($body) && ($type=='json' || $type=='xml'))
$body = array('result' => $body);
$status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status);
// set the status
header($status_header);
// set the content type
if($type == 'json')
header('Content-type: application/json');
else if($type == 'xml')
header('Content-type: application/xml');
else
header('Content-type: text/html');
// pages with body are easy
if($body != '') {
// send the body
if($type == 'json')
$body = RestUtils::data_encode($body, 'json');
else if($type == 'xml')
$body = RestUtils::data_encode($body, 'xml');
}
// we need to create the body if none is passed
else {
// create some body messages
$body = '';
// this is purely optional, but makes the pages a little nicer to read
// for your users. Since you won't likely send a lot of different status codes,
// this also shouldn't be too ponderous to maintain
switch($status) {
case 401:
$body = 'You must be authorized to view this page.';
break;
case 404:
$body = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.';
break;
case 500:
$body = 'The server encountered an error processing your request.';
break;
case 501:
$body = 'The requested method is not implemented.';
break;
//TODO., more status? default?
/*default:
RestUtils::error();*/
}
}
echo $body;
exit();
}
public static function error($status='500', $body='') {
//DEBUG Mode
RestUtils::sendResponse($status, $body, $type='text/html');
//Production Mode
//RestUtils::sendResponse(500);
exit();
}
}
class RestRequest {
private $request_vars;
private $data;
private $http_accept;
private $method;
private $element;
private $id;
public function __construct() {
$this->request_vars = array();
$this->data = '';
$this->element = '';
$this->id = '';
$this->http_accept = (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'xml')) ? 'xml' : 'json';
$this->method = 'get';
}
//Setters
public function setData($data) { $this->data = $data; }
public function setElement($element) { $this->element = $element; }
public function setID($id) { $this->id = $id; }
public function setMethod($method) { $this->method = $method; }
public function setOperation($operation) { $this->operation = $operation; }
public function setHttpAccept($http_accept) { $this->http_accept = $http_accept; }
public function setRequestVars($request_vars) { $this->request_vars = $request_vars; }
//Getters
public function getData() { return $this->data; }
public function getElement() { return $this->element; }
public function getID() { return $this->id; }
public function getMethod() { return $this->method; }
public function getOperation() { return $this->operation; }
public function getHttpAccept() { return $this->http_accept; }
public function getRequestVars() { return $this->request_vars; }
public function __toString() {
$res = 'Method: '.$this->getMethod()."\n".
'Type: '.$this->getHttpAccept()."\n".
'Variables: '."\n";
foreach($this->getRequestVars() as $k=>$v)
$res .= $k.'='.$v."\n";
$res .= 'Data: '.$this->getData();
return $res;
}
}
class Response {
private $status;
private $body;
function __construct($status, $body='', $type='') {
$this->status = $status;
$this->body = $body;
$this->type = $type;
}
function getStatus() { return $this->status; }
function getBody() { return $this->body; }
function getType() { return $this->type; }
function setStatus($status) { $this->status = $status; }
function setBody($body) { $this->body = $body; }
function setType($type) { $this->type = $type; }
}
?>