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

ItemController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 126
c 1
b 0
f 0
dl 0
loc 225
rs 10
wmc 27

3 Methods

Rating   Name   Duplication   Size   Complexity  
B inFoldersAction() 0 82 9
B addAction() 0 46 6
C getAction() 0 67 12
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      ItemControler.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2022 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
class ItemController extends BaseController
26
{
27
28
29
    /**
30
     * Manage case inFolder
31
     *
32
     * @param array $userData
33
     * @return string
34
     */
35
    public function inFoldersAction(array $userData): void
36
    {
37
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
38
        $strErrorDesc = '';
39
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
40
41
        // get parameters
42
        $arrQueryStringParams = $this->getQueryStringParams();
43
44
        if (strtoupper($requestMethod) === 'GET') {
45
            // define WHERE clause
46
            $sqlExtra = '';
47
48
            if (empty($userData['folders_list']) === false) {
49
                $userData['folders_list'] = explode(',', $userData['folders_list']);
50
            } else {
51
                $userData['folders_list'] = [];
52
            }
53
54
            // SQL where clause with folders list
55
            if (isset($arrQueryStringParams['folders']) === true) {
56
                // convert the folders to an array
57
                $arrQueryStringParams['folders'] = explode(',', str_replace( array('[',']') , ''  , $arrQueryStringParams['folders']));
58
59
                // ensure to only use the intersection
60
                $foldersList = implode(',', array_intersect($arrQueryStringParams['folders'], $userData['folders_list']));
61
62
                if (!empty($foldersList)) {
63
                    // build sql where clause
64
                    $sqlExtra = ' WHERE id_tree IN ('.$foldersList.')';
65
                } else {
66
                    // Send error
67
                    $this->sendOutput(
68
                        json_encode(['error' => 'Folders are mandatory']),
69
                        ['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided']
70
                    );
71
                }
72
            } else {
73
                // Send error
74
                $this->sendOutput(
75
                    json_encode(['error' => 'Folders are mandatory']),
76
                    ['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided']
77
                );
78
            }
79
80
            // SQL LIMIT
81
            $intLimit = 0;
82
            if (isset($arrQueryStringParams['limit']) === true) {
83
                $intLimit = $arrQueryStringParams['limit'];
84
            }
85
86
            // send query
87
            try {
88
                $itemModel = new ItemModel();
89
90
                $arrItems = $itemModel->getItems($sqlExtra, $intLimit, $userData['private_key'], $userData['id']);
91
92
                if (!empty($arrItems)) {
93
                    $responseData = json_encode($arrItems);
94
                } else {
95
                    $strErrorDesc = 'No content for this label';
96
                    $strErrorHeader = 'HTTP/1.1 204 No Content';
97
                }
98
            } catch (Error $e) {
99
                $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
100
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
101
            }
102
        } else {
103
            $strErrorDesc = 'Method not supported';
104
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
105
        }
106
107
        // send output
108
        if (empty($strErrorDesc) === true) {
109
            $this->sendOutput(
110
                $responseData,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $responseData does not seem to be defined for all execution paths leading up to this point.
Loading history...
111
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
112
            );
113
        } else {
114
            $this->sendOutput(
115
                json_encode(['error' => $strErrorDesc]),
116
                ['Content-Type: application/json', $strErrorHeader]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $strErrorHeader does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
            );
118
        }
119
    }
120
    //end InFoldersAction()
121
122
    /**
123
     * Manage case Add
124
     *
125
     * @param array $userData
126
     */
127
    public function addAction(array $userData)
128
    {
129
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
130
        $strErrorDesc = '';
131
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
132
133
        if (strtoupper($requestMethod) === 'POST') {
134
            if (empty($userData['folders_list']) === false) {
135
                $userData['folders_list'] = explode(',', $userData['folders_list']);
136
            } else {
137
                $userData['folders_list'] = [];
138
            }
139
140
            $data = json_decode(file_get_contents("php://input"));
141
142
            if (in_array($data->folderId, $userData['folders_list'])) {
143
                // send query
144
                try {
145
                    $itemModel = new ItemModel();
146
147
                    $itemModel->addItem($data->folderId, $data->userName, $data->hostname, $data->password);
148
                } catch (Error $e) {
149
                    $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
150
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
151
                }
152
            } else {
153
                $strErrorDesc = 'Folders are mandatory';
154
                $strErrorHeader = 'HTTP/1.1 401 Expected parameters not provided';
155
            }
156
        } else {
157
            $strErrorDesc = 'Method not supported';
158
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
159
        }
160
161
        // send output
162
        if (empty($strErrorDesc) === true) {
163
            $this->sendOutput(
164
                "",
165
                ['Content-Type: application/json', 'HTTP/1.1 201 Created']
166
            );
167
168
            //$this->sendOutput(['HTTP/1.1 201 Created']);
169
        } else {
170
            $this->sendOutput(
171
                json_encode(['error' => $strErrorDesc]),
172
                ['Content-Type: application/json', $strErrorHeader]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $strErrorHeader does not seem to be defined for all execution paths leading up to this point.
Loading history...
173
            );
174
        }
175
    }
176
    //end addAction()
177
178
    /**
179
     * Manage case get item by label
180
     *
181
     * @param array $userData
182
     */
183
    public function getAction(array $userData)
184
    {
185
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
186
        $strErrorDesc = '';
187
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
188
189
        // get parameters
190
        $arrQueryStringParams = $this->getQueryStringParams();
191
192
        if (strtoupper($requestMethod) === 'GET') {
193
            if (empty($userData['folders_list']) === false) {
194
                $userData['folders_list'] = explode(',', $userData['folders_list']);
195
            } else {
196
                $userData['folders_list'] = [];
197
            }
198
199
            if (isset($arrQueryStringParams['id']) === true && empty($arrQueryStringParams['id']) === false ) {
200
                try {
201
                    $itemModel = new ItemModel();
202
203
                    $item = $itemModel->getItem($arrQueryStringParams['id'], $userData['private_key'], $userData['id'], $userData['folders_list']);
204
205
                    if (!empty($item)) {
206
                        $responseData = json_encode($item);
207
                    } else {
208
                        $strErrorDesc = 'No content for this label';
209
                        $strErrorHeader = 'HTTP/1.1 204 No Content';
210
                    }
211
                } catch (Error $e) {
212
                    $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
213
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
214
                }
215
            } else if (isset($arrQueryStringParams['label']) === true && empty($arrQueryStringParams['label']) === false ) {
216
                try {
217
                    $itemModel = new ItemModel();
218
219
                    $item = $itemModel->getItemByLabel($arrQueryStringParams['label'], $userData['private_key'], $userData['id'], $userData['folders_list']);
220
221
                    if (!empty($item)) {
222
                        $responseData = json_encode($item);
223
                    } else {
224
                        $strErrorDesc = 'No content for this label';
225
                        $strErrorHeader = 'HTTP/1.1 204 No Content';
226
                    }
227
                } catch (Error $e) {
228
                    $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
229
                    $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
230
                }
231
            } else {
232
                $strErrorDesc = 'Id are mandatory';
233
                $strErrorHeader = 'HTTP/1.1 204 No Content';
234
            }
235
        } else {
236
            $strErrorDesc = 'Method not supported';
237
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
238
        }
239
240
        // send output
241
        if (empty($strErrorDesc) === true) {
242
            $this->sendOutput(
243
                $responseData,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $responseData does not seem to be defined for all execution paths leading up to this point.
Loading history...
244
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
245
            );
246
        } else {
247
            $this->sendOutput(
248
                json_encode(['error' => $strErrorDesc]),
249
                ['Content-Type: application/json', $strErrorHeader]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $strErrorHeader does not seem to be defined for all execution paths leading up to this point.
Loading history...
250
            );
251
        }
252
    }
253
    //end addAction()
254
}
255