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

ItemModel::getItems()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 95
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 58
c 2
b 0
f 0
nc 9
nop 4
dl 0
loc 95
rs 7.6719

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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      ItemModel.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
require_once API_ROOT_PATH . "/Model/Database.php";
26
27
class ItemModel extends Database
28
{
29
30
31
    /**
32
     * Get the list of items to return
33
     *
34
     * @param string $sqlExtra
35
     * @param integer $limit
36
     * @param string $userPrivateKey
37
     * @param integer $userId
38
     *
39
     * @return array
40
     */
41
    public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, int $userId): array
42
    {
43
        $rows = $this->select(
44
            "SELECT id, label, description, pw, url, id_tree, login, email, viewed_no, fa_icon, inactif, perso
45
            FROM ".prefixTable('items')
46
47
            . $sqlExtra . " ORDER BY id ASC" .
48
            ($limit > 0 ? " LIMIT ?". ["i", $limit] : '')
0 ignored issues
show
Bug introduced by
Are you sure array('i', $limit) of type array<integer,integer|string> can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            ($limit > 0 ? " LIMIT ?". /** @scrutinizer ignore-type */ ["i", $limit] : '')
Loading history...
49
        );
50
51
        $ret = [];
52
53
        foreach ($rows as $row) {
54
            $userKey = $this->select(
55
                'SELECT share_key
56
                FROM ' . prefixTable('sharekeys_items') . '
57
                WHERE user_id = '.$userId.' AND object_id = '.$row['id']
58
            );
59
            if (count($userKey) === 0 || empty($row['pw']) === true) {
60
                // No share key found
61
                $pwd = '';
62
            } else {
63
                $pwd = base64_decode(doDataDecryption(
64
                    $row['pw'],
65
                    decryptUserObjectKey(
66
                        $userKey[0]['share_key'],
67
                        $userPrivateKey
68
                    )
69
                ));
70
            }
71
72
            $champs = $this->select(
73
                'SELECT c.title, ci.data
74
                FROM ' . prefixTable('categories_items') . ' AS ci
75
                INNER JOIN ' . prefixTable('categories') . ' AS c
76
                ON ci.field_id = c.id
77
                WHERE item_id = '. $row['id']
78
79
80
81
82
83
84
85
86
87
            );
88
89
            if ((int) $row['inactif'] === 0) {
90
                if (count($champs) === 0) {
91
                    array_push(
92
                        $ret,
93
                        [
94
                            'id' => (int) $row['id'],
95
                            'label' => $row['label'],
96
                            'description' => $row['description'],
97
                            'pwd' => $pwd,
98
                            'url' => $row['url'],
99
                            'login' => $row['login'],
100
                            'email' => $row['email'],
101
                            'viewed_no' => (int) $row['viewed_no'],
102
                            'fa_icon' => $row['fa_icon'],
103
                            'inactif' => (int) $row['inactif'],
104
                            'perso' => (int) $row['perso']
105
                        ]
106
                    );
107
                } else {
108
                    $retChamps = array();
109
110
                    foreach ($champs as $champ) {
111
                        $retChamps[$champ['title']] = $champ['data'];
112
                    }
113
114
                    array_push(
115
                        $ret,
116
                        [
117
                            'id' => (int) $row['id'],
118
                            'label' => $row['label'],
119
                            'description' => $row['description'],
120
                            'pwd' => $pwd,
121
                            'url' => $row['url'],
122
                            'login' => $row['login'],
123
                            'email' => $row['email'],
124
                            'viewed_no' => (int) $row['viewed_no'],
125
                            'fa_icon' => $row['fa_icon'],
126
                            'inactif' => (int) $row['inactif'],
127
                            'perso' => (int) $row['perso'],
128
                            'champs' => $retChamps
129
                        ]
130
                    );
131
                }
132
            }
133
        }
134
135
        return $ret;
136
137
138
139
140
    }
141
    //end getItems()
142
143
    /**
144
     * Add item
145
     *
146
     * @return bool
147
     */
148
    public function addItem(string $idTree, string $userName, string $hostname, string $password) : bool
0 ignored issues
show
Unused Code introduced by
The parameter $password is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

148
    public function addItem(string $idTree, string $userName, string $hostname, /** @scrutinizer ignore-unused */ string $password) : bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $idTree is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

148
    public function addItem(/** @scrutinizer ignore-unused */ string $idTree, string $userName, string $hostname, string $password) : bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $userName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

148
    public function addItem(string $idTree, /** @scrutinizer ignore-unused */ string $userName, string $hostname, string $password) : bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $hostname is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

148
    public function addItem(string $idTree, string $userName, /** @scrutinizer ignore-unused */ string $hostname, string $password) : bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
        // TODO ecrire
151
        return true;
152
    }
153
154
    public function getItem(string $itemId, string $userPrivateKey, int $userId, array $foldersList): array
155
    {
156
        $item = $this->select("SELECT id, label, pw, id_tree, login FROM " . prefixTable('items') . " WHERE id=" . $itemId )[0];
157
158
        if (in_array($item['id_tree'], $foldersList)) {
159
            $userKey = $this->select('SELECT share_key FROM ' . prefixTable('sharekeys_items') . ' WHERE user_id = '.$userId.' AND object_id = '.$item['id']);
160
161
            if (count($userKey) === 0 || empty($item['pw']) === true) {
162
                // No share key found
163
                $pwd = '';
164
            } else {
165
                $pwd = base64_decode(doDataDecryption(
166
                    $item['pw'],
167
                    decryptUserObjectKey(
168
                        $userKey[0]['share_key'],
169
                        $userPrivateKey
170
                    )
171
                ));
172
            }
173
174
            return [
175
                    'id' => $item['id'],
176
                    'label' => $item['label'],
177
                    'pwd' => $pwd,
178
                    'login' => $item['login']
179
            ];
180
        }
181
182
        return array();
183
    }
184
185
    public function getItemByLabel(string $itemLabel, string $userPrivateKey, int $userId, array $foldersList): array
186
    {
187
        $item = $this->select("SELECT id, label, pw, id_tree, login FROM " . prefixTable('items') . " WHERE label='" . $itemLabel . "'")[0];
188
189
        if (in_array($item['id_tree'], $foldersList)) {
190
            $userKey = $this->select('SELECT share_key FROM ' . prefixTable('sharekeys_items') . ' WHERE user_id = '.$userId.' AND object_id = '.$item['id'] );
191
192
            if (count($userKey) === 0 || empty($item['pw']) === true) {
193
                // No share key found
194
                $pwd = '';
195
            } else {
196
                $pwd = base64_decode(doDataDecryption(
197
                    $item['pw'],
198
                    decryptUserObjectKey(
199
                        $userKey[0]['share_key'],
200
                        $userPrivateKey
201
                    )
202
                ));
203
            }
204
205
            return [
206
                    'id' => $item['id'],
207
                    'label' => $item['label'],
208
                    'pwd' => $pwd,
209
                    'login' => $item['login']
210
            ];
211
        }
212
213
        return array();
214
    }
215
}
216