These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | require_once('class.FlipSession.php'); |
||
3 | require_once('libs/Slim/Slim/Slim.php'); |
||
4 | require_once('Autoload.php'); |
||
5 | \Slim\Slim::registerAutoloader(); |
||
6 | |||
7 | const SUCCESS = 0; |
||
8 | const UNRECOGNIZED_METHOD = 1; |
||
9 | const INVALID_PARAM = 2; |
||
10 | const ALREADY_LOGGED_IN = 3; |
||
11 | const INVALID_LOGIN = 4; |
||
12 | const ACCESS_DENIED = 5; |
||
13 | const INTERNAL_ERROR = 6; |
||
14 | |||
15 | const UNKNOWN_ERROR = 255; |
||
16 | |||
17 | class OAuth2Auth extends \Slim\Middleware |
||
18 | { |
||
19 | protected $headers = array(); |
||
20 | |||
21 | public function __construct($headers) |
||
22 | { |
||
23 | $this->headers = $headers; |
||
24 | } |
||
25 | |||
26 | private function getUserFromSession() |
||
27 | { |
||
28 | if(FlipSession::isLoggedIn()) |
||
29 | { |
||
30 | return FlipSession::getUser(); |
||
31 | } |
||
32 | return false; |
||
33 | } |
||
34 | |||
35 | /* |
||
36 | * @SuppressWarnings("Superglobals") |
||
37 | * @SuppressWarnings("StaticAccess") |
||
38 | */ |
||
39 | private function getUserFromBasicAuth($header) |
||
40 | { |
||
41 | $auth = \AuthProvider::getInstance(); |
||
42 | $auth->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
||
43 | $user = FlipSession::getUser(); |
||
44 | if($user === false) |
||
45 | { |
||
46 | $data = substr($header['Authorization'], 6); |
||
47 | $userpass = explode(':', base64_decode($data)); |
||
48 | $user = $auth->getUserByLogin($userpass[0], $userpass[1]); |
||
49 | } |
||
50 | return $user; |
||
51 | } |
||
52 | |||
53 | /* |
||
54 | * @SuppressWarnings("StaticAccess") |
||
55 | */ |
||
56 | private function getUserFromToken($header) |
||
57 | { |
||
58 | $auth = \AuthProvider::getInstance(); |
||
59 | $key = substr($header, 7); |
||
60 | return $auth->getUserByAccessCode($key); |
||
61 | } |
||
62 | |||
63 | private function getUserFromHeader($header) |
||
64 | { |
||
65 | if(strncmp($header, 'Basic', 5) == 0) |
||
66 | { |
||
67 | return $this->getUserFromBasicAuth($header); |
||
68 | } |
||
69 | return $this->getUserFromToken($header); |
||
70 | } |
||
71 | |||
72 | public function call() |
||
73 | { |
||
74 | // no auth header |
||
75 | if(!isset($this->headers['Authorization'])) |
||
76 | { |
||
77 | $this->app->user = $this->getUserFromSession(); |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | $header = $this->headers['Authorization']; |
||
82 | $this->app->user = $this->getUserFromHeader($header); |
||
83 | } |
||
84 | |||
85 | if($this->app->user === false) |
||
86 | { |
||
87 | $this->app->getLog()->error("No user found for call"); |
||
88 | } |
||
89 | |||
90 | // this line is required for the application to proceed |
||
91 | $this->next->call(); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | class FlipRESTFormat extends \Slim\Middleware |
||
96 | { |
||
97 | private function fix_encoded_element($key, $value, &$array, $prefix = '') |
||
98 | { |
||
99 | if(is_array($value)) |
||
100 | { |
||
101 | $array[$key] = implode(';', $value); |
||
102 | } |
||
103 | else if($key === '_id' && is_object($value)) |
||
104 | { |
||
105 | $array[$key] = $value->{'$id'}; |
||
106 | } |
||
107 | else if(is_object($value)) |
||
108 | { |
||
109 | $array[$key] = $this->app->request->getUrl().$this->app->request->getPath().$prefix.'/'.$key; |
||
110 | } |
||
111 | else if(strncmp($value, 'data:', 5) === 0) |
||
112 | { |
||
113 | $array[$key] = $this->app->request->getUrl().$this->app->request->getPath().$prefix.'/'.$key; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | private function createCSV(&$array) |
||
118 | { |
||
119 | if (count($array) == 0) |
||
120 | { |
||
121 | return null; |
||
122 | } |
||
123 | ob_start(); |
||
124 | $df = fopen("php://output", 'w'); |
||
125 | if(is_array($array)) |
||
126 | { |
||
127 | $first = reset($array); |
||
128 | $keys = FALSE; |
||
129 | if(is_array($first)) |
||
130 | { |
||
131 | $keys = array_keys($first); |
||
132 | } |
||
133 | else if(is_object($first)) |
||
134 | { |
||
135 | $keys = array_keys(get_object_vars($first)); |
||
136 | } |
||
137 | fputcsv($df, $keys); |
||
138 | foreach ($array as $row) |
||
139 | { |
||
140 | if(is_array($row)) |
||
141 | { |
||
142 | $id = $row[$keys[0]]; |
||
143 | foreach($row as $key=>$value) |
||
144 | { |
||
145 | $this->fix_encoded_element($key, $value, $row, '/'.$id); |
||
146 | } |
||
147 | fputcsv($df, $row); |
||
148 | } |
||
149 | else if(is_object($row)) |
||
150 | { |
||
151 | $keyName = $keys[0]; |
||
152 | $id = $row->$keyName; |
||
153 | if(is_object($id)) |
||
154 | { |
||
155 | $id = $id->{'$id'}; |
||
156 | } |
||
157 | $values = get_object_vars($row); |
||
158 | foreach($values as $key=>$value) |
||
159 | { |
||
160 | $this->fix_encoded_element($key, $value, $values, '/'.$id); |
||
161 | } |
||
162 | fputcsv($df, $values); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | else |
||
167 | { |
||
168 | $array = get_object_vars($array); |
||
169 | fputcsv($df, array_keys($array)); |
||
170 | foreach($array as $key=>$value) |
||
171 | { |
||
172 | $this->fix_encoded_element($key, $value, $array); |
||
173 | } |
||
174 | fputcsv($df, $array); |
||
175 | } |
||
176 | fclose($df); |
||
177 | return ob_get_clean(); |
||
178 | } |
||
179 | |||
180 | private function createXML(&$array) |
||
181 | { |
||
182 | $obj = new SerializableObject($array); |
||
183 | return $obj->xmlSerialize(); |
||
184 | } |
||
185 | |||
186 | public function call() |
||
187 | { |
||
188 | if($this->app->request->isOptions()) |
||
189 | { |
||
190 | return; |
||
191 | } |
||
192 | $params = $this->app->request->params(); |
||
193 | $fmt = null; |
||
194 | if(isset($params['fmt'])) |
||
195 | { |
||
196 | $fmt = $params['fmt']; |
||
197 | } |
||
198 | if($fmt === null && isset($params['$format'])) |
||
199 | { |
||
200 | $fmt = $params['$format']; |
||
201 | if(strstr($fmt, 'odata.streaming=true')) |
||
202 | { |
||
203 | $this->app->response->setStatus(406); |
||
204 | return; |
||
205 | } |
||
206 | } |
||
207 | if($fmt === null) |
||
208 | { |
||
209 | $mimeType = $this->app->request->headers->get('Accept'); |
||
210 | if(strstr($mimeType, 'odata.streaming=true')) |
||
211 | { |
||
212 | $this->app->response->setStatus(406); |
||
213 | return; |
||
214 | } |
||
215 | switch($mimeType) |
||
216 | { |
||
217 | case 'text/csv': |
||
218 | $fmt = 'csv'; |
||
219 | break; |
||
220 | case 'text/x-vCard': |
||
221 | $fmt = 'vcard'; |
||
222 | break; |
||
223 | default: |
||
224 | $fmt = 'json'; |
||
225 | break; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | $this->app->fmt = $fmt; |
||
230 | $this->app->odata = new ODataParams($params); |
||
231 | |||
232 | |||
233 | $this->next->call(); |
||
234 | |||
235 | if($this->app->response->getStatus() == 200 && $this->app->fmt !== 'json') |
||
236 | { |
||
237 | $data = json_decode($this->app->response->getBody()); |
||
238 | $text = false; |
||
0 ignored issues
–
show
|
|||
239 | switch($this->app->fmt) |
||
240 | { |
||
241 | case 'data-table': |
||
242 | $this->app->response->headers->set('Content-Type', 'application/json'); |
||
243 | $text = json_encode(array('data'=>$data)); |
||
244 | break; |
||
245 | case 'csv': |
||
246 | $this->app->response->headers->set('Content-Type', 'text/csv'); |
||
247 | $path = $this->app->request->getPathInfo(); |
||
248 | $path = strrchr($path, '/'); |
||
249 | $path = substr($path, 1); |
||
250 | $this->app->response->headers->set('Content-Disposition', 'attachment; filename='.$path.'.csv'); |
||
251 | $text = $this->createCSV($data); |
||
252 | break; |
||
253 | case 'xml': |
||
254 | $this->app->response->headers->set('Content-Type', 'application/xml'); |
||
255 | $text = $this->createXML($data); |
||
256 | break; |
||
257 | case 'passthru': |
||
258 | $text = $this->app->response->getBody(); |
||
259 | break; |
||
260 | default: |
||
261 | $text = 'Unknown fmt '.$fmt; |
||
262 | break; |
||
263 | } |
||
264 | $this->app->response->setBody($text); |
||
265 | } |
||
266 | else if($this->app->response->getStatus() == 200) |
||
267 | { |
||
268 | $this->app->response->headers->set('Content-Type', 'application/json;odata.metadata=none'); |
||
269 | } |
||
270 | } |
||
271 | } |
||
272 | |||
273 | class FlipREST extends \Slim\Slim |
||
274 | { |
||
275 | function __construct() |
||
0 ignored issues
–
show
|
|||
276 | { |
||
277 | parent::__construct(); |
||
278 | $this->config('debug', false); |
||
279 | $headers = array(); |
||
280 | if(php_sapi_name() !== "cli") |
||
281 | { |
||
282 | $headers = apache_request_headers(); |
||
283 | } |
||
284 | $this->add(new OAuth2Auth($headers)); |
||
285 | $this->add(new FlipRESTFormat()); |
||
286 | $errorHandler = array($this, 'errorHandler'); |
||
287 | $this->error($errorHandler); |
||
288 | } |
||
289 | |||
290 | function get_json_body($array=false) |
||
0 ignored issues
–
show
|
|||
291 | { |
||
292 | return $this->getJsonBody($array); |
||
293 | } |
||
294 | |||
295 | function getJsonBody($array=false) |
||
0 ignored issues
–
show
|
|||
296 | { |
||
297 | $body = $this->request->getBody(); |
||
298 | return json_decode($body, $array); |
||
299 | } |
||
300 | |||
301 | function errorHandler($exception) |
||
0 ignored issues
–
show
|
|||
302 | { |
||
303 | $error = array( |
||
304 | 'code' => $exception->getCode(), |
||
305 | 'message' => $exception->getMessage(), |
||
306 | 'file' => $exception->getFile(), |
||
307 | 'line' => $exception->getLine(), |
||
308 | ); |
||
309 | $this->response->headers->set('Content-Type', 'application/json'); |
||
310 | error_log(print_r($error, true)); |
||
311 | echo json_encode($error); |
||
312 | } |
||
313 | } |
||
314 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
315 | ?> |
||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
|||
316 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.