1497 |
jpm |
1 |
<?php
|
|
|
2 |
// In : utf8 url_encoded (get et post)
|
|
|
3 |
// Out : utf8
|
|
|
4 |
|
|
|
5 |
// TODO : gerer les retours : dans ce controleur : code retour et envoi ...
|
|
|
6 |
class JRest {
|
|
|
7 |
|
|
|
8 |
/** Parsed configuration file */
|
|
|
9 |
private $config;
|
|
|
10 |
|
|
|
11 |
/** The HTTP request method used. */
|
|
|
12 |
private $method = 'GET';
|
|
|
13 |
|
|
|
14 |
/** The HTTP request data sent (if any). */
|
|
|
15 |
private $requestData = NULL;
|
|
|
16 |
|
|
|
17 |
/** Array of strings to convert into the HTTP response. */
|
|
|
18 |
private $output = array();
|
|
|
19 |
|
|
|
20 |
/** Nom resource. */
|
|
|
21 |
private $resource = NULL;
|
|
|
22 |
|
|
|
23 |
/** Identifiant unique resource. */
|
|
|
24 |
private $uid = NULL;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Constructor. Parses the configuration file "JRest.ini", grabs any request data sent, records the HTTP
|
|
|
28 |
* request method used and parses the request URL to find out the requested resource
|
|
|
29 |
* @param str iniFile Configuration file to use
|
|
|
30 |
*/
|
|
|
31 |
public function JRest($iniFile = 'jrest.ini.php') {
|
|
|
32 |
$this->config = parse_ini_file($iniFile, TRUE);
|
|
|
33 |
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD']) && isset($_SERVER['QUERY_STRING'])) {
|
|
|
34 |
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] > 0) {
|
|
|
35 |
$this->requestData = '';
|
|
|
36 |
$httpContent = fopen('php://input', 'r');
|
|
|
37 |
while ($data = fread($httpContent, 1024)) {
|
|
|
38 |
$this->requestData .= $data;
|
|
|
39 |
}
|
|
|
40 |
fclose($httpContent);
|
|
|
41 |
}
|
|
|
42 |
if (strlen($_SERVER['QUERY_STRING']) == 0) {
|
|
|
43 |
$len = strlen($_SERVER['REQUEST_URI']);
|
|
|
44 |
} else {
|
|
|
45 |
$len = -(strlen($_SERVER['QUERY_STRING']) + 1);
|
|
|
46 |
}
|
|
|
47 |
$urlString = substr($_SERVER['REQUEST_URI'], strlen($this->config['settings']['baseURL']), $len);
|
|
|
48 |
|
|
|
49 |
$urlParts = explode('/', $urlString);
|
|
|
50 |
|
|
|
51 |
if (isset($urlParts[0])) $this->resource = $urlParts[0];
|
|
|
52 |
if (count($urlParts) > 1 && $urlParts[1] != '') {
|
|
|
53 |
array_shift($urlParts);
|
|
|
54 |
foreach ($urlParts as $uid) {
|
|
|
55 |
if ($uid != '') {
|
|
|
56 |
$this->uid[] = urldecode($uid);
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$this->method = $_SERVER['REQUEST_METHOD'];
|
|
|
62 |
} else {
|
|
|
63 |
trigger_error('I require the server variables REQUEST_URI, REQUEST_METHOD and QUERY_STRING to work.', E_USER_ERROR);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Execute the request.
|
|
|
69 |
*/
|
|
|
70 |
function exec() {
|
|
|
71 |
switch ($this->method) {
|
|
|
72 |
case 'GET':
|
|
|
73 |
$this->get();
|
|
|
74 |
break;
|
|
|
75 |
case 'POST':
|
|
|
76 |
$this->post();
|
|
|
77 |
break;
|
|
|
78 |
case 'DELETE':
|
|
|
79 |
$this->delete();
|
|
|
80 |
break;
|
|
|
81 |
case 'PUT':
|
|
|
82 |
$this->add();
|
|
|
83 |
break;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Execute a GET request. A GET request fetches a list of resource when no resource name is given, a list of element
|
|
|
89 |
* when a resource name is given, or a resource element when a resource and resource unique identifier are given. It does not change the
|
|
|
90 |
* database contents.
|
|
|
91 |
*/
|
|
|
92 |
private function get() {
|
|
|
93 |
if ($this->resource) {
|
|
|
94 |
$resource_file = 'services/'.ucfirst($this->resource).'.php';
|
|
|
95 |
$resource_class = ucfirst($this->resource);
|
|
|
96 |
if (file_exists($resource_file)) {
|
|
|
97 |
include_once $resource_file;
|
|
|
98 |
if (class_exists($resource_class)) {
|
|
|
99 |
$service = new $resource_class($this->config);
|
|
|
100 |
if ($this->uid) { // get a resource element
|
|
|
101 |
if (method_exists($service, 'getElement')) {
|
|
|
102 |
$service->getElement($this->uid);
|
|
|
103 |
}
|
|
|
104 |
} elseif (method_exists($service, 'getRessource')) { // get all elements of a ressource
|
|
|
105 |
$service->getRessource();
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
} else { // get resources
|
|
|
110 |
// include set.jrest.php, instanticiation et appel
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function post() {
|
|
|
115 |
$pairs = array();
|
|
|
116 |
// Récupération des paramètres passés dans le contenu de la requête HTTP (= POST)
|
|
|
117 |
if ($this->requestData) {
|
|
|
118 |
$pairs = $this->parseRequestData();
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Ajout des informations concernant l'upload de fichier passées dans la variable $_FILE
|
|
|
122 |
if(isset($_FILES)) {
|
|
|
123 |
foreach ($_FILES as $v) {
|
|
|
124 |
$pairs[$v['name']] = $v;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
// Ne pas effacer cette ligne ! Elle est indispensable pour les services du Carnet en ligne
|
|
|
128 |
// qui n'utilisent que le tableau pairs dans les posts
|
|
|
129 |
$pairs = array_merge($pairs, $_POST);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// gestion du contenu du post
|
|
|
133 |
if(isset($_POST))
|
|
|
134 |
{
|
|
|
135 |
// Safari ne sait pas envoyer des DELETE avec gwt...
|
|
|
136 |
// Nous utilisons le parametre "action" passé dans le POST qui doit contenir DELETE pour lancer la supression
|
1587 |
aurelien |
137 |
if (isset($pairs['action']) && $pairs['action'] == 'DELETE') {
|
1497 |
jpm |
138 |
$this->delete();
|
|
|
139 |
return;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
if (count($pairs) != 0) {
|
|
|
143 |
if ($this->uid) { // get a resource element
|
|
|
144 |
$resource_file = 'services/'.ucfirst($this->resource).'.php';
|
|
|
145 |
$resource_class = ucfirst($this->resource);
|
|
|
146 |
if (file_exists($resource_file)) {
|
|
|
147 |
include_once $resource_file;
|
|
|
148 |
if (class_exists($resource_class)) {
|
|
|
149 |
$service = new $resource_class($this->config);
|
|
|
150 |
if (method_exists($service,'updateElement')) { // Update element
|
|
|
151 |
// TODO : a voir le retour ...
|
|
|
152 |
if ($service->updateElement($this->uid, $pairs)) {
|
|
|
153 |
$this->created();
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
} else { // get all elements of a ressource
|
|
|
159 |
$this->add($pairs);
|
|
|
160 |
}
|
|
|
161 |
} else {
|
|
|
162 |
$this->lengthRequired();
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
private function delete() {
|
|
|
168 |
$resource_file = 'services/'.ucfirst($this->resource).'.php';
|
|
|
169 |
$resource_class = ucfirst($this->resource);
|
|
|
170 |
if (file_exists($resource_file)) {
|
|
|
171 |
include_once $resource_file;
|
|
|
172 |
if (class_exists($resource_class)) {
|
|
|
173 |
$service = new $resource_class($this->config);
|
|
|
174 |
if ($this->uid) { // get a resource element
|
|
|
175 |
if (method_exists($service, 'deleteElement')) { // Delete element
|
|
|
176 |
if ($service->deleteElement($this->uid)) {
|
|
|
177 |
$this->noContent();
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
private function add($pairs = null) {
|
|
|
186 |
if (is_null($pairs)) {
|
|
|
187 |
$pairs = array();
|
|
|
188 |
// Récupération des paramètres passés dans le contenu de la requête HTTP (= POST)
|
|
|
189 |
// FIXME : vérifier que l'on récupère bien les données passées par PUT
|
|
|
190 |
if ($this->requestData) {
|
|
|
191 |
$pairs = $this->parseRequestData();
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
if (count($pairs) != 0) {
|
|
|
196 |
$resource_file = 'services/'.ucfirst($this->resource).'.php';
|
|
|
197 |
$resource_class = ucfirst($this->resource);
|
|
|
198 |
if (file_exists($resource_file)) {
|
|
|
199 |
include_once $resource_file;
|
|
|
200 |
if (class_exists($resource_class)) {
|
|
|
201 |
$service = new $resource_class($this->config);
|
|
|
202 |
if (method_exists($service,'createElement')) { // Create a new element
|
|
|
203 |
if ($service->createElement($pairs)) {
|
|
|
204 |
$this->created();
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
} else {
|
|
|
210 |
$this->lengthRequired();
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Parse the HTTP request data.
|
|
|
216 |
* @return str[] Array of name value pairs
|
|
|
217 |
*/
|
|
|
218 |
private function parseRequestData() {
|
|
|
219 |
$values = array();
|
|
|
220 |
$pairs = explode('&', $this->requestData);
|
|
|
221 |
foreach ($pairs as $pair) {
|
|
|
222 |
$parts = explode('=', $pair);
|
|
|
223 |
if (isset($parts[0]) && isset($parts[1])) {
|
|
|
224 |
$parts[1] = rtrim(urldecode($parts[1]));
|
|
|
225 |
$values[$parts[0]] = $parts[1];
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
return $values;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Send a HTTP 201 response header.
|
|
|
233 |
*/
|
|
|
234 |
private function created($url = FALSE) {
|
|
|
235 |
header('HTTP/1.0 201 Created');
|
|
|
236 |
if ($url) {
|
|
|
237 |
header('Location: '.$url);
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Send a HTTP 204 response header.
|
|
|
243 |
*/
|
|
|
244 |
private function noContent() {
|
|
|
245 |
header('HTTP/1.0 204 No Content');
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Send a HTTP 400 response header.
|
|
|
250 |
*/
|
|
|
251 |
private function badRequest() {
|
|
|
252 |
header('HTTP/1.0 400 Bad Request');
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* Send a HTTP 401 response header.
|
|
|
257 |
*/
|
|
|
258 |
private function unauthorized($realm = 'JRest') {
|
|
|
259 |
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
|
|
|
260 |
header('WWW-Authenticate: Basic realm="'.$realm.'"');
|
|
|
261 |
}
|
|
|
262 |
header('HTTP/1.0 401 Unauthorized');
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Send a HTTP 404 response header.
|
|
|
267 |
*/
|
|
|
268 |
private function notFound() {
|
|
|
269 |
header('HTTP/1.0 404 Not Found');
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* Send a HTTP 405 response header.
|
|
|
274 |
*/
|
|
|
275 |
private function methodNotAllowed($allowed = 'GET, HEAD') {
|
|
|
276 |
header('HTTP/1.0 405 Method Not Allowed');
|
|
|
277 |
header('Allow: '.$allowed);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* Send a HTTP 406 response header.
|
|
|
282 |
*/
|
|
|
283 |
private function notAcceptable() {
|
|
|
284 |
header('HTTP/1.0 406 Not Acceptable');
|
|
|
285 |
echo join(', ', array_keys($this->config['renderers']));
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Send a HTTP 411 response header.
|
|
|
290 |
*/
|
|
|
291 |
private function lengthRequired() {
|
|
|
292 |
header('HTTP/1.0 411 Length Required');
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Send a HTTP 500 response header.
|
|
|
297 |
*/
|
|
|
298 |
private function internalServerError() {
|
|
|
299 |
header('HTTP/1.0 500 Internal Server Error');
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
?>
|