Passed
Push — master ( d98489...d9bccd )
by Nils
04:45
created

errorHdl()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 2
dl 0
loc 12
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 API
11
 *
12
 * @file      bootstrap.php
13
 * ---
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 *
17
 * @copyright 2009-2022 Teampass.net
18
 *
19
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
20
 * ---
21
 *
22
 * @see       https://www.teampass.net
23
 */
24
define("PROJECT_ROOT_PATH", __DIR__ . "/..");
25
// include main configuration file
26
require __DIR__ . '/../../includes/config/settings.php';
27
require __DIR__ . '/../../includes/config/tp.config.php';
28
require __DIR__ . '/../../sources/main.functions.php';
29
30
// Load superglobal
31
require PROJECT_ROOT_PATH. '/../includes/libraries/protect/SuperGlobal/SuperGlobal.php';
32
$superGlobal = new protect\SuperGlobal\SuperGlobal();
33
34
// include the base controller file
35
require PROJECT_ROOT_PATH . "/Controller/Api/BaseController.php";
36
37
// include the use model file
38
require PROJECT_ROOT_PATH . "/Model/UserModel.php";
39
require PROJECT_ROOT_PATH . "/Model/ItemModel.php";
40
41
42
/**
43
 * Launch expected action for ITEM
44
 *
45
 * @param array $actions
46
 * @param array $userData
47
 * @return void
48
 */
49
function itemAction(array $actions, array $userData)
50
{
51
    require PROJECT_ROOT_PATH . "/Controller/Api/ItemController.php";
52
    
53
    $objFeedController = new ItemController();
54
    $strMethodName = $actions[0] . 'Action';
55
    $objFeedController->{$strMethodName}($userData);
56
}
57
58
59
/**
60
 * Check if API usage is allowed in Teampass settings
61
 *
62
 * @return string
63
 */
64
function apiIsEnabled(): string
65
{
66
    require PROJECT_ROOT_PATH . '/../includes/config/tp.config.php';
67
68
    if ((int) $SETTINGS['api'] === 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $SETTINGS seems to be never defined.
Loading history...
69
        return json_encode(
70
            [
71
                'error' => false,
72
                'error_message' => '',
73
                'error_header' => '',
74
            ]
75
        );
76
    } else {
77
        return json_encode(
78
            [
79
                'error' => true,
80
                'error_message' => 'API usage is not allowed',
81
                'error_header' => 'HTTP/1.1 404 Not Found',
82
            ]
83
        );
84
    }
85
}
86
87
88
/**
89
 * Check if connection is authorized
90
 *
91
 * @return string
92
 */
93
function verifyAuth(): string
94
{
95
    include_once PROJECT_ROOT_PATH . '/inc/jwt_utils.php';
96
    $bearer_token = get_bearer_token();
97
98
    if (empty($bearer_token) === false && is_jwt_valid($bearer_token) === true) {
99
        return json_encode(
100
            [
101
                'error' => false,
102
                'error_message' => '',
103
                'error_header' => '',
104
            ]
105
        );
106
    } else {
107
        return json_encode(
108
            [
109
                'error' => true,
110
                'error_message' => 'Access denied',
111
                'error_header' => 'HTTP/1.1 404 Not Found',
112
            ]
113
        );
114
    }
115
}
116
117
118
/**
119
 * Get the payload from bearer
120
 *
121
 * @return void
122
 */
123
function getDataFromToken(): string
124
{
125
    include_once PROJECT_ROOT_PATH . '/inc/jwt_utils.php';
126
    $bearer_token = get_bearer_token();
127
128
    if (empty($bearer_token) === false) {
129
        return json_encode(
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array... 'error_header' => '')) returns the type string which is incompatible with the documented return type void.
Loading history...
130
            [
131
                'data' => get_bearer_data($bearer_token),
132
                'error' => false,
133
                'error_message' => '',
134
                'error_header' => '',
135
            ]
136
        );
137
    } else {
138
        return json_encode(
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array...TP/1.1 404 Not Found')) returns the type string which is incompatible with the documented return type void.
Loading history...
139
            [
140
                'error' => true,
141
                'error_message' => 'Access denied',
142
                'error_header' => 'HTTP/1.1 404 Not Found',
143
            ]
144
        );
145
    }
146
}
147
148
149
/**
150
 * Send error output
151
 *
152
 * @param string $errorHeader
153
 * @param string $errorValues
154
 * @return void
155
 */
156
function errorHdl(string $errorHeader, string $errorValues)
157
{
158
    header_remove('Set-Cookie');
159
160
    if (is_array($errorHeader) && count($errorHeader)) {
0 ignored issues
show
introduced by
The condition is_array($errorHeader) is always false.
Loading history...
161
        foreach ($errorHeader as $httpHeader) {
162
            header($httpHeader);
163
        }
164
    }
165
166
    echo $errorValues;
167
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
168
}