BaseController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 3 1
A getUriSegments() 0 8 1
A getQueryStringParams() 0 10 2
A sendOutput() 0 11 4
A sanitizeUrl() 0 11 2
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-2025 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 Symfony\Component\HttpFoundation\Request AS symfonyRequest;
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
        $request = symfonyRequest::createFromGlobals();
47
        $requestUri = $request->getRequestUri();
48
49
        $uri = parse_url($requestUri, PHP_URL_PATH);
50
        $uri = explode( '/', $uri );
51
        return $this->sanitizeUrl(array_slice($uri, ((int) array_search('index.php', $uri) + 1)));
52
    }
53
54
    /**
55
     * Get querystring params.
56
     * 
57
     * @return array|string
58
     */
59
    public function getQueryStringParams()
60
    {
61
        $request = symfonyRequest::createFromGlobals();
62
        $queryString = $request->getQueryString();
63
        if ($request->getContentTypeFormat() !== 'json') {
64
            parse_str(html_entity_decode($queryString), $query);
65
            return $this->sanitizeUrl($query);
66
        }
67
68
        return $request->toArray();
69
    }
70
71
    /**
72
     * Undocumented function
73
     *
74
     * @param array $array
75
     * @return array|string
76
     */
77
    public function sanitizeUrl(array $array)
78
    {
79
        $filters = [];
80
        $array_size = count($array);
81
        for ($i=0; $i < $array_size; $i++) {
82
            array_push($filters, 'trim|escape');
83
        }
84
        
85
        return dataSanitizer(
86
            $array,
87
            $filters
88
        );
89
    }
90
91
92
    /**
93
     * Send API output.
94
     *
95
     * @param mixed  $data
96
     * @param string $httpHeader
97
     */
98
    protected function sendOutput($data, $httpHeaders=array()): void
99
    {
100
        header_remove('Set-Cookie');
101
102
        if (is_array($httpHeaders) && count($httpHeaders)) {
103
            foreach ($httpHeaders as $httpHeader) {
104
                header($httpHeader);
105
            }
106
        }
107
108
        echo $data;
109
    }
110
}
111