|
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] : '') |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|