Passed
Push — master ( 29ea3c...1f091d )
by Nils
10:51
created

BaseController   A

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
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
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL on line 63 at column 0
Loading history...
64
        if ($request->getContentTypeFormat() !== 'json') {
65
=======
66
        if ($request->getContentTypeFormat() != 'application/json') {
67
>>>>>>> 7bd3d6ef1 (Fixes in patch:)
68
            parse_str(html_entity_decode($queryString), $query);
69
            return $this->sanitizeUrl($query);
70
        }
71
72
        return $request->toArray();
73
    }
74
75
    /**
76
     * Undocumented function
77
     *
78
     * @param array $array
79
     * @return array|string
80
     */
81
    public function sanitizeUrl(array $array)
82
    {
83
        $filters = [];
84
        $array_size = count($array);
85
        for ($i=0; $i < $array_size; $i++) {
86
            array_push($filters, 'trim|escape');
87
        }
88
        
89
        return dataSanitizer(
90
            $array,
91
            $filters
92
        );
93
    }
94
95
96
    /**
97
     * Send API output.
98
     *
99
     * @param mixed  $data
100
     * @param string $httpHeader
101
     */
102
    protected function sendOutput($data, $httpHeaders=array()): void
103
    {
104
        header_remove('Set-Cookie');
105
106
        if (is_array($httpHeaders) && count($httpHeaders)) {
107
            foreach ($httpHeaders as $httpHeader) {
108
                header($httpHeader);
109
            }
110
        }
111
112
        echo $data;
113
    }
114
}
115