-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
141 lines (124 loc) · 3.54 KB
/
api.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
<?php if (!defined('DIRECTSCRIPT')) exit('No direct script access allowed');
class Api {
private static $instance;
function Api(){
self::$instance =& $this;
$this->method = 'get';
$this->allowed_methods = array('get', 'post', 'put', 'delete');
$this->params = array();
$this->request_uri_raw = '';
$this->request_uri = array();
$this->security = new Security();
$this->output = new Output();
$this->defaults = array(
'limit' => null,
'offset' => null,
'order' => null,
'joins' => null,
'include' => null
);
}
public static function &get_instance(){
return self::$instance;
}
function process_request(){
$this->method = strtolower($_SERVER['REQUEST_METHOD']);
if (!in_array($this->method, $this->allowed_methods)) {
$this->output->error('error', 'Method not supported: '.$this->method);
}
$this->request_uri_raw = strtolower($_SERVER['REQUEST_URI']);
$this->request_uri = explode("/", $this->request_uri_raw);
foreach ($this->request_uri as $k => &$v){
if ($v == "" || in_array($v, $GLOBALS['request_uri_ignore'])) {
unset($this->request_uri[$k]);
} else {
if (strpos($v, "?") > -1) {
$i = strpos($v, "?");
$v = substr($v, 0, $i);
if ($v == "") { unset($this->request_uri[$k]); }
}
}
}
unset($v);
$tmp = $this->request_uri;
$model = array_shift($tmp);
$this->load_first_model($model);
$id = array_shift($tmp)+0;
if ($id > 0){
$this->params['id'] = $id;
}
$this->get_params();
$this->$model->where_ary = $this->params;
$this->$model->defaults = $this->defaults;
// run the request method
$results = $this->$model->process_request($this->method);
$this->$model->push_results($results);
}
function load_first_model($model){
$modelName = ucfirst($model);
if (!file_exists('models/'.$model.'.php')){
$this->output->error('error', 'Unable to locate the model you have specified: '.$modelName);
}
require_once('models/'.$model.'.php');
$this->$model = new $modelName();
}
function get_params(){
$params = array();
if ($this->method == 'get'){
$params = $_GET;
} elseif ($this->method == 'post'){
$params = $_POST;
} elseif ($this->method == 'put' || $this->method == 'delete'){
$params = $this->parse_query(file_get_contents('php://input'));
}
$params = array_merge($params, $this->params); // has to be in this order, that way the internal params overwrite the posted params
$largest_array = 1;
if (!empty($this->params)){
$largest_array = count(max($this->params));
}
$this->params = array_fill(0, $largest_array, array());
foreach ($params as $key => $val){
if (array_key_exists($key, $this->defaults)){
$this->defaults[$key] = $val;
} else {
if (is_array($val)){
foreach ($val as $k => $v){
$this->params[$k][$key] = $v;
}
} else {
foreach ($this->params as $k => $v){
$this->params[$k][$key] = $val;
}
}
}
}
$this->clean_params($this->params);
}
function clean_params(&$params){
foreach ($params as &$v) {
stripslashes_deep($v);
}
}
function parse_query($str){
$pairs = explode('&', $str);
foreach($pairs as $pair) {
list($name, $value) = explode('=', urldecode($pair), 2);
$name = preg_replace('/\[|\]/', '', $name);
if (isset($params[$name])) {
if (!is_array($query[$name])) {
$tmp = $query[$name];
$params[$name] = array();
$params[$name][] = $tmp;
}
$params[$name][] = $value;
} else {
$params[$name] = $value;
}
}
return $params;
}
}
function &get_instance(){
return Api::get_instance();
}
?>