Passed
Branch wip_sessions (2e0cc8)
by Nils
04:59
created

BaseController::getQueryStringParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version    API
12
 *
13
 * @file      BaseController.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2023 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
26
27
use TeampassClasses\SuperGlobal\SuperGlobal;
28
29
class BaseController
30
{
31
    /**
32
     * __call magic method.
33
     */
34
    public function __call($name, $arguments)
35
    {
36
        $this->sendOutput('', array('HTTP/1.1 404 Not Found'));
37
    }
38
39
    /**
40
     * Get URI elements.
41
     * 
42
     * @return array|string
43
     */
44
    public function getUriSegments()
45
    {
46
        $superGlobal = new SuperGlobal();
47
        $uri = parse_url($superGlobal->get('REQUEST_URI', 'SERVER'), PHP_URL_PATH);
48
        $uri = explode( '/', $uri );
49
        return $this->sanitizeUrl(array_slice($uri, ((int) array_search('index.php', $uri) + 1)));
50
    }
51
52
    /**
53
     * Get querystring params.
54
     * 
55
     * @return array|string
56
     */
57
    public function getQueryStringParams()
58
    {
59
        $superGlobal = new SuperGlobal();
60
        parse_str(html_entity_decode($superGlobal->get('QUERY_STRING', 'SERVER')), $query);
61
        return $this->sanitizeUrl($query);
62
    }
63
64
    /**
65
     * Undocumented function
66
     *
67
     * @param array $array
68
     * @return array|string
69
     */
70
    public function sanitizeUrl(array $array)
71
    {
72
        $filters = [];
73
        $array_size = count($array);
74
        for ($i=0; $i < $array_size; $i++) {
75
            array_push($filters, 'trim|escape');
76
        }
77
        
78
        return dataSanitizer(
79
            $array,
80
            $filters,
81
            __DIR__.'/../../..'
0 ignored issues
show
Unused Code introduced by
The call to dataSanitizer() has too many arguments starting with __DIR__ . '/../../..'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
        return /** @scrutinizer ignore-call */ dataSanitizer(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
82
        );
83
    }
84
85
86
    /**
87
     * Send API output.
88
     *
89
     * @param mixed  $data
90
     * @param string $httpHeader
91
     */
92
    protected function sendOutput($data, $httpHeaders=array()): void
93
    {
94
        header_remove('Set-Cookie');
95
96
        if (is_array($httpHeaders) && count($httpHeaders)) {
97
            foreach ($httpHeaders as $httpHeader) {
98
                header($httpHeader);
99
            }
100
        }
101
102
        echo $data;
103
    }
104
}