-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.class.php
More file actions
63 lines (54 loc) · 1.57 KB
/
Controller.class.php
File metadata and controls
63 lines (54 loc) · 1.57 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
<?php
/**
* An abstract Controller class for endpoint controllers to extend.
* Plus some static helper functions.
* Written by Bradley Gill
* altered effect (http://alteredeffect.com)
* bradgill@gmail.com
*/
abstract class Controller {
// Mandatory HTTP verbs to implement
abstract protected function get( $chunks );
abstract protected function put( $chunks );
abstract protected function post( $chunks );
abstract protected function delete( $chunks );
/**
* Something has gone wrong! Terminate the script.
* @param HTTP error $code.
* @param Text to write in the body.
*/
static function error( $code, $desc ) {
$codes = array();
$codes[400] = 'Bad Request';
$codes[401] = 'Unauthorized';
$codes[403] = 'Forbidden';
$codes[404] = 'Not Found';
$codes[405] = 'Method Not Allowed';
$codes[406] = 'Not Acceptable';
$codes[501] = 'Not Implemented';
$codes[503] = 'Service unavailable';
header('Content-Type: text/html');
header( 'HTTP/1.1 '. $code . ' '. @$codes[$code] );
die( $desc );
}
/**
* Send out some JSON! Terminate the script.
* @param $obj object to JSON encode.
*/
static function json_out( $obj ) {
header('Content-Type: application/json');
die( json_encode( $obj ) );
}
/**
* Retrieve the data passed in.
* @param HTTP $verb. ex: one of 'get', 'put', 'post', or 'delete'.
* @return an associative array of data.
*/
static function getData( $verb ) {
if ( in_array( $verb, array( 'put', 'delete' ) ) ) {
return json_decode(file_get_contents("php://input"), true );
} else
return $_REQUEST;
}
}
?>