Passed
Pull Request — master (#3505)
by
unknown
05:44
created

folderAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 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      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
 * Check if connection is authorized
107
 *
108
 * @return string
109
 */
110
function verifyAuth(): string
111
{
112
    include_once API_ROOT_PATH . '/inc/jwt_utils.php';
113
    $bearer_token = get_bearer_token();
114
115
    if (empty($bearer_token) === false && is_jwt_valid($bearer_token) === true) {
116
        return json_encode(
117
            [
118
                'error' => false,
119
                'error_message' => '',
120
                'error_header' => '',
121
            ]
122
        );
123
    } else {
124
        return json_encode(
125
            [
126
                'error' => true,
127
                'error_message' => 'Access denied',
128
                'error_header' => 'HTTP/1.1 404 Not Found',
129
            ]
130
        );
131
    }
132
}
133
134
/**
135
 * Get the payload from bearer
136
 *
137
 * @return string
138
 */
139
function getDataFromToken(): string
140
{
141
    include_once API_ROOT_PATH . '/inc/jwt_utils.php';
142
    $bearer_token = get_bearer_token();
143
144
    if (empty($bearer_token) === false) {
145
        return json_encode(
146
            [
147
                'data' => get_bearer_data($bearer_token),
148
                'error' => false,
149
                'error_message' => '',
150
                'error_header' => '',
151
            ]
152
        );
153
    } else {
154
        return json_encode(
155
            [
156
                'error' => true,
157
                'error_message' => 'Access denied',
158
                'error_header' => 'HTTP/1.1 404 Not Found',
159
            ]
160
        );
161
    }
162
}
163
164
/**
165
 * Send error output
166
 *
167
 * @param string $errorHeader
168
 * @param string $errorValues
169
 * @return void
170
 */
171
function errorHdl(string $errorHeader, string $errorValues)
172
{
173
    header_remove('Set-Cookie');
174
175
    header($errorHeader);
176
177
    echo $errorValues;
178
}
179