Passed
Push — master ( 1f91ec...0204e8 )
by Nils
04:11
created

folderAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
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      bootstrap.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
define("API_ROOT_PATH", __DIR__ . "/..");
27
28
// include main configuration file
29
require API_ROOT_PATH . '/../includes/config/settings.php';
30
require API_ROOT_PATH . '/../includes/config/tp.config.php';
31
require API_ROOT_PATH . '/../sources/main.functions.php';
32
33
// Load superglobal
34
require API_ROOT_PATH. '/../includes/libraries/protect/SuperGlobal/SuperGlobal.php';
35
$superGlobal = new protect\SuperGlobal\SuperGlobal();
36
37
// include the base controller file
38
require API_ROOT_PATH . "/Controller/BaseController.php";
39
40
// include the use model file
41
require API_ROOT_PATH . "/Model/UserModel.php";
42
require API_ROOT_PATH . "/Model/ItemModel.php";
43
require API_ROOT_PATH . "/Model/FolderModel.php";
44
45
/**
46
 * Launch expected action for ITEM
47
 *
48
 * @param array $actions
49
 * @param array $userData
50
 * @return void
51
 */
52
function itemAction(array $actions, array $userData)
53
{
54
    require API_ROOT_PATH . "/Controller/ItemController.php";
55
    
56
    $objFeedController = new ItemController();
57
    $strMethodName = $actions[0] . 'Action';
58
    $objFeedController->{$strMethodName}($userData);
59
}
60
61
/**
62
 * Launch expected action for FOLDER
63
 *
64
 * @param array $actions
65
 * @param array $userData
66
 * @return void
67
 */
68
function folderAction(array $actions, array $userData)
69
{
70
    require API_ROOT_PATH . "/Controller/FolderController.php";
71
72
    $objFeedController = new FolderController();
73
    $strMethodName = $actions[0] . 'Action';
74
    $objFeedController->{$strMethodName}($userData);
75
}
76
77
/**
78
 * Check if API usage is allowed in Teampass settings
79
 *
80
 * @return string
81
 */
82
function apiIsEnabled(): string
83
{
84
    require API_ROOT_PATH . '/../includes/config/tp.config.php';
85
86
    if ((int) $SETTINGS['api'] === 1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $SETTINGS seems to be never defined.
Loading history...
87
        return json_encode(
88
            [
89
                'error' => false,
90
                'error_message' => '',
91
                'error_header' => '',
92
            ]
93
        );
94
    } else {
95
        return json_encode(
96
            [
97
                'error' => true,
98
                'error_message' => 'API usage is not allowed',
99
                'error_header' => 'HTTP/1.1 404 Not Found',
100
            ]
101
        );
102
    }
103
}
104
105
106
/**
107
 * Check if connection is authorized
108
 *
109
 * @return string
110
 */
111
function verifyAuth(): string
112
{
113
    include_once API_ROOT_PATH . '/inc/jwt_utils.php';
114
    $bearer_token = get_bearer_token();
115
116
    if (empty($bearer_token) === false && is_jwt_valid($bearer_token) === true) {
117
        return json_encode(
118
            [
119
                'error' => false,
120
                'error_message' => '',
121
                'error_header' => '',
122
            ]
123
        );
124
    } else {
125
        return json_encode(
126
            [
127
                'error' => true,
128
                'error_message' => 'Access denied',
129
                'error_header' => 'HTTP/1.1 404 Not Found',
130
            ]
131
        );
132
    }
133
}
134
135
136
/**
137
 * Get the payload from bearer
138
 *
139
 * @return string
140
 */
141
function getDataFromToken(): string
142
{
143
    include_once API_ROOT_PATH . '/inc/jwt_utils.php';
144
    $bearer_token = get_bearer_token();
145
146
    if (empty($bearer_token) === false) {
147
        return json_encode(
148
            [
149
                'data' => get_bearer_data($bearer_token),
150
                'error' => false,
151
                'error_message' => '',
152
                'error_header' => '',
153
            ]
154
        );
155
    } else {
156
        return json_encode(
157
            [
158
                'error' => true,
159
                'error_message' => 'Access denied',
160
                'error_header' => 'HTTP/1.1 404 Not Found',
161
            ]
162
        );
163
    }
164
}
165
166
167
/**
168
 * Send error output
169
 *
170
 * @param string $errorHeader
171
 * @param string $errorValues
172
 * @return void
173
 */
174
function errorHdl(string $errorHeader, string $errorValues)
175
{
176
    header_remove('Set-Cookie');
177
178
    header($errorHeader);
179
180
    echo $errorValues;
181
}