-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropagezServer.php
230 lines (166 loc) · 5.38 KB
/
PropagezServer.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
<?php
/* ---------------------------------------------------------
*
*
* Vous ne devez pas modifier le code ci-dessous
*
*
* ---------------------------------------------------------
*/
/**
*
* Interface du web service
*
* Votre classe d'api doit implémenter cette interface
*
*/
interface Propagez_Server_Interface {
public function add($data);
public function update($id,$data);
public function get($id);
public function delete($id);
public function isDuplicate($data);
}
/**
*
* Classe pour le serveur
*
* Cette classe défini la logique de base du web service
*
*
*/
class Propagez_Server {
const VERSION = 0.1;
public static $api;
public static $response;
protected static $_secret;
public static $config = array(
'input_namespace' => 'propagez_api'
);
public static function run($api,$request = null) {
ini_set('display_errors', 0);
error_reporting(E_NONE);
try {
self::init($api);
self::handleRequest(isset($request) ? $request:$_REQUEST);
self::sendResponse();
} catch(Exception $e) {
self::error($e);
}
}
public static function init($api) {
if(!$api instanceof Propagez_Server_Interface) {
throw new Exception('Vous devez utiliser l\'interface');
}
if(!isset($api->secret) || empty($api->secret)) {
throw new Exception('Vous devez fournir une clé secrète');
}
self::$api = $api;
self::$_secret = $api->secret;
}
public static function handleRequest($url = null) {
if(is_array($url)) {
$inputs = $url;
} else {
if(strpos($url, '?') !== false) {
$query = substr($url,strpos($url, '?')+1);
} elseif(strpos($url, '&') !== false) {
$query = $url;
} else {
throw new Exception('Aucune requête');
}
$parts = explode('&',$query);
$inputs = array();
foreach($parts as $part) {
$part = explode('=',$part);
$inputs[$part[0]] = isset($part[1]) ? $part[1]:null;
}
}
$methodName = self::$config['input_namespace'].'_method';
$signatureName = self::$config['input_namespace'].'_signature';
$idName = self::$config['input_namespace'].'_id';
$dataName = self::$config['input_namespace'].'_data';
if(!isset($inputs[$methodName])) throw new Exception('Requête incomplète');
$method = $inputs[$methodName];
if(!method_exists(self::$api,$method)) throw new Exception('Méthode inconnue');
if(isset(self::$api->debug) && !self::$api->debug) {
if(!isset($inputs[$signatureName])) throw new Exception('Vous devez fournir une signature');
if(!self::verifySignature($inputs[$signatureName],$inputs)) throw new Exception('Signature invalide');
}
switch($method) {
case 'add':
if(!isset($inputs[$dataName])) throw new Exception('Aucune donnée');
$response = self::$api->$method(self::decodeData($inputs[$dataName]));
break;
case 'update':
if(!isset($inputs[$idName])) throw new Exception('Aucun ID');
if(!isset($inputs[$dataName])) throw new Exception('Aucune donnée');
$response = self::$api->$method($inputs[$idName], self::decodeData($inputs[$dataName]));
break;
case 'get':
case 'delete':
if(!isset($inputs[$idName])) throw new Exception('Aucun ID');
$response = self::$api->$method($inputs[$idName]);
break;
case 'isDuplicate':
if(!isset($inputs[$dataName])) throw new Exception('Aucune donnée');
$response = self::$api->$method(self::decodeData($inputs[$dataName]));
break;
case 'version':
$response = Propagez_Server::VERSION;
break;
default:
throw new Exception('Méthode inconnue');
break;
}
self::validateResponse($method,$response);
self::$response = array(
'success' => true,
'response' => $response
);
}
public static function validateResponse($method,$response) {
if(!isset($response)) throw new Exception('Aucune réponse');
switch($method) {
case 'add':
case 'update':
case 'get':
if(!is_array($response)) throw new Exception('Vous devez retourner un événement');
if(!isset($response['id'])) throw new Exception('L\'événement doit contenir un champ id');
break;
case 'delete':
case 'isDuplicate':
if($response !== true && $response !== false) throw new Exception('Vous devez retourner true ou false');
break;
}
return true;
}
public static function verifySignature($signature,$inputs) {
$methodName = self::$config['input_namespace'].'_method';
$idName = self::$config['input_namespace'].'_id';
$dataName = self::$config['input_namespace'].'_data';
$parts = array();
if(isset($inputs[$methodName])) $parts[] = $methodName.'='.rawurlencode($inputs[$methodName]);
if(isset($inputs[$idName])) $parts[] = $idName.'='.rawurlencode($inputs[$idName]);
if(isset($inputs[$dataName])) $parts[] = $dataName.'='.rawurlencode($inputs[$dataName]);
if($signature != md5(self::$_secret.'&'.implode('&',$parts))) return false;
return true;
}
public static function sendResponse() {
self::response(self::$response);
}
public static function response($data) {
header('Content-type: text/plain; charset="utf-8"');
echo json_encode($data);
exit();
}
public static function error($e) {
self::response(array(
'success' => false,
'error' => is_a($e,'Exception') ? $e->getMessage():$e
));
}
public static function decodeData($data) {
return is_array($data) ? $data:json_decode($data,true);
}
}