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