Passed
Push — master ( 69fa1a...d5bf88 )
by Nils
04:26
created

ItemController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 69
c 4
b 0
f 0
dl 0
loc 134
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
B inFoldersAction() 0 67 7
A getAction() 0 47 5
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-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
class ItemController extends BaseController
26
{
27
28
29
    /**
30
     * Manage case inFolder - get items inside an array of folders
31
     *
32
     * @param array $userData
33
     */
34
    public function inFoldersAction(array $userData): void
35
    {
36
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
37
        $strErrorDesc = '';
38
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
39
40
        // get parameters
41
        $arrQueryStringParams = $this->getQueryStringParams();
42
43
        if (strtoupper($requestMethod) === 'GET') {
44
            // define WHERE clause
45
            $sqlExtra = '';
46
            if (empty($userData['folders_list']) === false) {
47
                $userData['folders_list'] = explode(',', $userData['folders_list']);
48
            } else {
49
                $userData['folders_list'] = [];
50
            }
51
52
            // SQL where clause with folders list
53
            if (isset($arrQueryStringParams['folders']) === true) {
54
                // convert the folders to an array
55
                $arrQueryStringParams['folders'] = explode(',', str_replace( array('[',']') , ''  , $arrQueryStringParams['folders']));
56
57
                // ensure to only use the intersection
58
                $foldersList = implode(',', array_intersect($arrQueryStringParams['folders'], $userData['folders_list']));
59
60
                // build sql where clause
61
                $sqlExtra = ' WHERE id_tree IN ('.$foldersList.')';
62
            } else {
63
                // Send error
64
                $this->sendOutput(
65
                    json_encode(['error' => 'Folders are mandatory']),
66
                    ['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided']
67
                );
68
            }
69
70
            // SQL LIMIT
71
            $intLimit = 0;
72
            if (isset($arrQueryStringParams['limit']) === true) {
73
                $intLimit = $arrQueryStringParams['limit'];
74
            }
75
76
            // send query
77
            try {
78
                $itemModel = new ItemModel();
79
80
                $arrItems = $itemModel->getItems($sqlExtra, $intLimit, $userData['private_key'], $userData['id']);
81
                $responseData = json_encode($arrItems);
82
            } catch (Error $e) {
83
                $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
84
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
85
            }
86
        } else {
87
            $strErrorDesc = 'Method not supported';
88
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
89
        }
90
91
        // send output
92
        if (empty($strErrorDesc) === true) {
93
            $this->sendOutput(
94
                $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...
95
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
96
            );
97
        } else {
98
            $this->sendOutput(
99
                json_encode(['error' => $strErrorDesc]), 
100
                ['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...
101
            );
102
        }
103
    }
104
    //end InFoldersAction() 
105
106
107
    /**
108
     * Manage case get - get an item
109
     *
110
     * @param array $userData
111
     */
112
    public function getAction(array $userData): void
113
    {
114
        $superGlobal = new protect\SuperGlobal\SuperGlobal();
115
        $strErrorDesc = '';
116
        $requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER');
117
118
        // get parameters
119
        $arrQueryStringParams = $this->getQueryStringParams();
120
121
        if (strtoupper($requestMethod) === 'GET') {
122
            // SQL where clause with item id
123
            if (isset($arrQueryStringParams['id']) === true) {
124
                // build sql where clause
125
                $sqlExtra = ' WHERE i.id = '.$arrQueryStringParams['id'];
126
            } else {
127
                // Send error
128
                $this->sendOutput(
129
                    json_encode(['error' => 'Item id is mandatory']),
130
                    ['Content-Type: application/json', 'HTTP/1.1 401 Expected parameters not provided']
131
                );
132
            }
133
134
            // send query
135
            try {
136
                $itemModel = new ItemModel();
137
138
                $arrItems = $itemModel->getItems($sqlExtra, 0, $userData['private_key'], $userData['id']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sqlExtra does not seem to be defined for all execution paths leading up to this point.
Loading history...
139
                $responseData = json_encode($arrItems);
140
            } catch (Error $e) {
141
                $strErrorDesc = $e->getMessage().'. Something went wrong! Please contact support.';
142
                $strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
143
            }
144
        } else {
145
            $strErrorDesc = 'Method not supported';
146
            $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
147
        }
148
149
        // send output
150
        if (empty($strErrorDesc) === true) {
151
            $this->sendOutput(
152
                $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...
153
                ['Content-Type: application/json', 'HTTP/1.1 200 OK']
154
            );
155
        } else {
156
            $this->sendOutput(
157
                json_encode(['error' => $strErrorDesc]), 
158
                ['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...
159
            );
160
        }
161
    }
162
    //end getAction() 
163
}