Completed
Push — master ( cbcc0c...f3f5f8 )
by Patrick
03:55
created

class.FlipREST.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

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, 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
        $this->app->isLocal = false;
233
        if($_SERVER['SERVER_ADDR'] === $_SERVER['REMOTE_ADDR'])
234
        {
235
            $this->app->isLocal = true;
236
        }
237
238
239
        $this->next->call();
240
241
        if($this->app->response->getStatus() == 200 && $this->app->fmt !== 'json')
242
        {
243
            $data = json_decode($this->app->response->getBody());
244
            $text = false;
0 ignored issues
show
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
245
            switch($this->app->fmt)
246
            {
247
                case 'data-table':
248
                    $this->app->response->headers->set('Content-Type', 'application/json');
249
                    $text = json_encode(array('data'=>$data));
250
                    break;
251
                case 'csv':
252
                    $this->app->response->headers->set('Content-Type', 'text/csv');
253
                    $path = $this->app->request->getPathInfo();
254
                    $path = strrchr($path, '/');
255
                    $path = substr($path, 1);
256
                    $this->app->response->headers->set('Content-Disposition', 'attachment; filename='.$path.'.csv');
257
                    $text = $this->createCSV($data);
258
                    break;
259
                case 'xml':
260
                    $this->app->response->headers->set('Content-Type', 'application/xml');
261
                    $text = $this->createXML($data);
262
                    break;
263
                case 'passthru':
264
                    $text = $this->app->response->getBody();
265
                    break;
266
                default:
267
                    $text = 'Unknown fmt '.$fmt;
268
                    break;
269
            }
270
            $this->app->response->setBody($text);
271
        }
272
        else if($this->app->response->getStatus() == 200)
273
        {
274
            $this->app->response->headers->set('Content-Type', 'application/json;odata.metadata=none');
275
        }
276
    }
277
}
278
279
class FlipREST extends \Slim\Slim
280
{
281
    public function __construct()
282
    {
283
        parent::__construct();
284
        $this->config('debug', false);
285
        $headers = array();
286
        if(php_sapi_name() !== "cli")
287
        {
288
            $headers = apache_request_headers();
289
        }
290
        $this->add(new OAuth2Auth($headers));
291
        $this->add(new FlipRESTFormat());
292
        $errorHandler = array($this, 'errorHandler');
293
        $this->error($errorHandler);
294
    }
295
296
    public function get_json_body($array = false)
297
    {
298
        return $this->getJsonBody($array);
299
    }
300
301
    public function getJsonBody($array = false)
302
    {
303
        $body = $this->request->getBody();
304
        return json_decode($body, $array);
305
    }
306
307
    public function errorHandler($exception)
308
    {
309
        $error = array(
310
            'code' => $exception->getCode(),
311
            'message' => $exception->getMessage(),
312
            'file' => $exception->getFile(),
313
            'line' => $exception->getLine(),
314
        );
315
        $this->response->headers->set('Content-Type', 'application/json');
316
        error_log(print_r($error, true));
317
        echo json_encode($error);
318
    }
319
}
320
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
321