1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Vermeulen Maxime <[email protected]> |
4
|
|
|
* @version 2.0 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace BfwApi; |
8
|
|
|
|
9
|
|
|
use \Exception; |
10
|
|
|
use \BFW\Helpers\Secure; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Abstract class for Rest API user class |
14
|
|
|
* @package bfw-api |
15
|
|
|
*/ |
16
|
|
|
abstract class Rest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var mixed $datas Datas receive by request |
20
|
|
|
*/ |
21
|
|
|
protected $datas; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string $responseFormat (json|xml) The response format to use |
25
|
|
|
*/ |
26
|
|
|
protected $responseFormat; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
* Get datas from the request |
31
|
|
|
*/ |
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->obtainsDatasFromRequest(); |
35
|
|
|
$this->obtainResponseFormat(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Method called for GET request |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function getRequest() |
44
|
|
|
{ |
45
|
|
|
//Not Implemented |
46
|
|
|
http_response_code(501); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Method called for POST request |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function postRequest() |
55
|
|
|
{ |
56
|
|
|
//Not Implemented |
57
|
|
|
http_response_code(501); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Method called for PUT request |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function putRequest() |
66
|
|
|
{ |
67
|
|
|
//Not Implemented |
68
|
|
|
http_response_code(501); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Method called for DELETE request |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function deleteRequest() |
77
|
|
|
{ |
78
|
|
|
//Not Implemented |
79
|
|
|
http_response_code(501); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get datas receive from the request |
84
|
|
|
* |
85
|
|
|
* @link http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function obtainsDatasFromRequest() |
90
|
|
|
{ |
91
|
|
|
if (\BFW\Request::getServerVar('CONTENT-TYPE') === 'application/json') |
92
|
|
|
{ |
93
|
|
|
$requestDatas = file_get_contents('php://input'); |
94
|
|
|
$this->datas = Secure::securise( |
95
|
|
|
json_decode($requestDatas, true), |
96
|
|
|
'string', |
97
|
|
|
true |
98
|
|
|
); |
99
|
|
|
} else { |
100
|
|
|
$this->datas = Secure::securise($_POST, 'string', true); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the response format to use from header "Accept" or |
106
|
|
|
* the get parameter "format" |
107
|
|
|
* |
108
|
|
|
* The get parameter have the priority on the "Accept" header |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
protected function obtainResponseFormat() |
113
|
|
|
{ |
114
|
|
|
$formatFromHeader = $this->obtainResponseFormatFromAcceptHeader(); |
115
|
|
|
$formatFromGet = $this->obtainResponseFormatFromGetParameter(); |
116
|
|
|
|
117
|
|
|
if ($formatFromGet !== null) { |
118
|
|
|
$this->responseFormat = $formatFromGet; |
119
|
|
|
} elseif ($formatFromHeader !== null) { |
120
|
|
|
$this->responseFormat = $formatFromHeader; |
121
|
|
|
} else { |
122
|
|
|
http_response_code(406); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the format to use from the "Accept" http header |
128
|
|
|
* |
129
|
|
|
* Header format : text/html,application/xhtml+xml,application/xml;q=0.9 |
130
|
|
|
* |
131
|
|
|
* @return null|string |
132
|
|
|
*/ |
133
|
|
|
protected function obtainResponseFormatFromAcceptHeader() |
134
|
|
|
{ |
135
|
|
|
$acceptHeader = \BFW\Request::getServerVar('HTTP_ACCEPT'); |
136
|
|
|
|
137
|
|
|
if ($acceptHeader === '') { |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$availableHeader = [ |
142
|
|
|
'application/xml' => 'xml', |
143
|
|
|
'application/json' => 'json' |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
$allAcceptedFormat = explode(',', $acceptHeader); |
147
|
|
|
foreach ($allAcceptedFormat as $mimeTypeWithPreference) { |
148
|
|
|
$cutMimeAndPreference = explode(';', $mimeTypeWithPreference); |
149
|
|
|
$mimeType = $cutMimeAndPreference[0]; |
150
|
|
|
|
151
|
|
|
if (isset($availableHeader[$mimeType])) { |
152
|
|
|
return $availableHeader[$mimeType]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the response format to use from the get paramter "format" |
161
|
|
|
* |
162
|
|
|
* @return string|null |
163
|
|
|
*/ |
164
|
|
|
protected function obtainResponseFormatFromGetParameter() |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
|
|
$format = Secure::getSecurisedGetKey('format', 'text'); |
168
|
|
|
|
169
|
|
|
if ($format !== 'xml' && $format !== 'json') { |
170
|
|
|
return null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $format; |
174
|
|
|
} catch (Exception $ex) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Send a response for the api request with the correct format |
181
|
|
|
* |
182
|
|
|
* @param mixed $response Datas to send |
183
|
|
|
* |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
protected function sendResponse(&$response) |
187
|
|
|
{ |
188
|
|
|
if ($this->responseFormat === 'json') { |
189
|
|
|
$this->sendJsonResponse($response); |
190
|
|
|
} elseif ($this->responseFormat === 'xml') { |
191
|
|
|
$this->sendXmlResponse($response); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Send the response for json format |
197
|
|
|
* |
198
|
|
|
* @param mixed $response Datas to send |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function sendJsonResponse(&$response) |
203
|
|
|
{ |
204
|
|
|
header('Content-Type: application/json'); |
205
|
|
|
echo json_encode($response); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Send the response for xml format |
210
|
|
|
* |
211
|
|
|
* @param mixed $response Datas to send |
212
|
|
|
* |
213
|
|
|
* @return void |
214
|
|
|
*/ |
215
|
|
|
protected function sendXmlResponse(&$response) |
216
|
|
|
{ |
217
|
|
|
header('Content-Type: application/xml'); |
218
|
|
|
|
219
|
|
|
$phpToXml = new \bultonFr\PhpToXml\PhpToXml; |
220
|
|
|
echo $phpToXml->convert($response); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|