BaseController::sanitizeUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
        
63
        // Priority 1: JSON body
64
        if ($request->getContentTypeFormat() === 'json') {
65
            return $request->toArray();
66
        }
67
        
68
        // Priority 2: POST form data
69
        if ($request->getMethod() === 'POST' && $request->request->count() > 0) {
70
            return $this->sanitizeUrl($request->request->all());
71
        }
72
        
73
        // Priority 3: Query string
74
        $queryString = $request->getQueryString();
75
        if (!empty($queryString)) {
76
            parse_str(html_entity_decode($queryString), $query);
77
            return $this->sanitizeUrl($query);
78
        }
79
        
80
        return [];
81
    }
82
83
    /**
84
     * Sanitize URL elements.
85
     *
86
     * @param array $array
87
     * @return array|string
88
     */
89
    public function sanitizeUrl(array $array)
90
    {
91
        $filters = [];
92
        $array_size = count($array);
93
        for ($i=0; $i < $array_size; $i++) {
94
            array_push($filters, 'trim|escape');
95
        }
96
        
97
        return dataSanitizer(
98
            $array,
99
            $filters
100
        );
101
    }
102
103
104
    /**
105
     * Send API output.
106
     *
107
     * @param mixed  $data
108
     * @param string $httpHeader
109
     */
110
    protected function sendOutput($data, $httpHeaders=array()): void
111
    {
112
        header_remove('Set-Cookie');
113
114
        if (is_array($httpHeaders) && count($httpHeaders)) {
115
            foreach ($httpHeaders as $httpHeader) {
116
                header($httpHeader);
117
            }
118
        }
119
120
        echo $data;
121
    }
122
}
123