1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Teampass - a collaborative passwords manager. |
4
|
|
|
* --- |
5
|
|
|
* This file is part of the TeamPass project. |
6
|
|
|
* |
7
|
|
|
* TeamPass is free software: you can redistribute it and/or modify it |
8
|
|
|
* under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, version 3 of the License. |
10
|
|
|
* |
11
|
|
|
* TeamPass is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* Certain components of this file may be under different licenses. For |
20
|
|
|
* details, see the `licenses` directory or individual file headers. |
21
|
|
|
* --- |
22
|
|
|
* @version API |
23
|
|
|
* |
24
|
|
|
* @file FolderModel.php |
25
|
|
|
* @author Nils Laumaillé ([email protected]) |
26
|
|
|
* @copyright 2009-2025 Teampass.net |
27
|
|
|
* @license GPL-3.0 |
28
|
|
|
* @see https://www.teampass.net |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
use TeampassClasses\Language\Language; |
32
|
|
|
|
33
|
|
|
class FolderModel |
34
|
|
|
{ |
35
|
|
|
public function getFoldersInfo(array $foldersId): array |
36
|
|
|
{ |
37
|
|
|
// Get folders |
38
|
|
|
$rows = DB::query( |
39
|
|
|
'SELECT id, title |
40
|
|
|
FROM ' . prefixTable('nested_tree') . ' |
41
|
|
|
WHERE nlevel = %i', |
42
|
|
|
1 |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$ret = []; |
46
|
|
|
|
47
|
|
|
foreach ($rows as $row) { |
48
|
|
|
$isVisible = in_array((int) $row['id'], $foldersId); |
49
|
|
|
$childrens = $this->getFoldersChildren($row['id'], $foldersId); |
50
|
|
|
|
51
|
|
|
if ($isVisible || count($childrens) > 0) { |
52
|
|
|
array_push( |
53
|
|
|
$ret, |
54
|
|
|
[ |
55
|
|
|
'id' => (int) $row['id'], |
56
|
|
|
'title' => $row['title'], |
57
|
|
|
'isVisible' => $isVisible, |
58
|
|
|
'childrens' => $childrens |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $ret; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getFoldersChildren(int $parentId, array $foldersId): array |
68
|
|
|
{ |
69
|
|
|
$ret = []; |
70
|
|
|
$childrens = DB::query( |
71
|
|
|
'SELECT id, title |
72
|
|
|
FROM ' . prefixTable('nested_tree') . ' |
73
|
|
|
WHERE parent_id = %i', |
74
|
|
|
$parentId |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if ( count($childrens) > 0) { |
78
|
|
|
foreach ($childrens as $children) { |
79
|
|
|
$isVisible = in_array((int) $children['id'], $foldersId); |
80
|
|
|
$childs = $this->getFoldersChildren($children['id'], $foldersId); |
81
|
|
|
|
82
|
|
|
if (in_array((int) $children['id'], $foldersId) || count($childs) > 0) { |
83
|
|
|
array_push( |
84
|
|
|
$ret, |
85
|
|
|
[ |
86
|
|
|
'id' => (int) $children['id'], |
87
|
|
|
'title' => $children['title'], |
88
|
|
|
'isVisible' => $isVisible, |
89
|
|
|
'childrens' => $childs |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $ret; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function createFolder( |
100
|
|
|
string $title, |
101
|
|
|
int $parent_id, |
102
|
|
|
int $complexity, |
103
|
|
|
int $duration, |
104
|
|
|
int $create_auth_without, |
105
|
|
|
int $edit_auth_without, |
106
|
|
|
string $icon, |
107
|
|
|
string $icon_selected, |
108
|
|
|
string $access_rights, |
109
|
|
|
int $is_admin, |
110
|
|
|
array $foldersId, |
111
|
|
|
int $is_manager, |
112
|
|
|
int $user_can_create_root_folder, |
113
|
|
|
int $user_can_manage_all_users, |
114
|
|
|
int $user_id, |
115
|
|
|
string $user_roles |
116
|
|
|
): array |
117
|
|
|
{ |
118
|
|
|
// Validate inputs |
119
|
|
|
include_once API_ROOT_PATH . '/../sources/main.functions.php'; |
120
|
|
|
$data = [ |
121
|
|
|
'title' => $title, |
122
|
|
|
'parent_id' => $parent_id, |
123
|
|
|
'complexity' => $complexity, |
124
|
|
|
'duration' => $duration, |
125
|
|
|
'create_auth_without' => $create_auth_without, |
126
|
|
|
'edit_auth_without' => $edit_auth_without, |
127
|
|
|
'icon' => $icon, |
128
|
|
|
'icon_selected' => $icon_selected, |
129
|
|
|
'access_rights' => $access_rights, |
130
|
|
|
'is_admin' => $is_admin, |
131
|
|
|
'foldersId' => json_encode($foldersId), |
132
|
|
|
'is_manager' => $is_manager, |
133
|
|
|
'user_can_create_root_folder' => $user_can_create_root_folder, |
134
|
|
|
'user_can_manage_all_users' => $user_can_manage_all_users, |
135
|
|
|
'user_id' => $user_id, |
136
|
|
|
'user_roles' => $user_roles, |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$filters = [ |
140
|
|
|
'title' => 'trim|escape', |
141
|
|
|
'parent_id' => 'cast:integer', |
142
|
|
|
'complexity' => 'cast:integer', |
143
|
|
|
'duration' => 'cast:integer', |
144
|
|
|
'create_auth_without' => 'cast:integer', |
145
|
|
|
'edit_auth_without' => 'cast:integer', |
146
|
|
|
'icon' => 'trim|escape', |
147
|
|
|
'icon_selected' => 'trim|escape', |
148
|
|
|
'access_rights' => 'trim|escape', |
149
|
|
|
'is_admin' => 'cast:integer', |
150
|
|
|
'foldersId' => 'cast:array', |
151
|
|
|
'is_manager' => 'cast:integer', |
152
|
|
|
'user_can_create_root_folder' => 'cast:integer', |
153
|
|
|
'user_can_manage_all_users' => 'cast:integer', |
154
|
|
|
'user_id' => 'cast:integer', |
155
|
|
|
'user_roles' => 'trim|escape', |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$inputData = dataSanitizer( |
159
|
|
|
$data, |
160
|
|
|
$filters |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
// Extract inputs |
164
|
|
|
$title = $inputData['title']; |
165
|
|
|
$parent_id = $inputData['parent_id']; |
166
|
|
|
$complexity = $inputData['complexity']; |
167
|
|
|
$duration = isset($inputData['duration']) === true ? $inputData['duration'] : 0; |
168
|
|
|
$create_auth_without = isset($inputData['create_auth_without']) === true ? $inputData['create_auth_without'] : 0; |
169
|
|
|
$edit_auth_without = isset($inputData['edit_auth_without']) === true ? $inputData['edit_auth_without'] : 0; |
170
|
|
|
$icon = $inputData['icon']; |
171
|
|
|
$icon_selected = $inputData['icon_selected']; |
172
|
|
|
$access_rights = isset($inputData['access_rights']) === true ? $inputData['access_rights'] : 'W'; |
173
|
|
|
$foldersId = $inputData['foldersId']; |
174
|
|
|
|
175
|
|
|
// Do checks |
176
|
|
|
if ( |
177
|
|
|
in_array($complexity, [TP_PW_STRENGTH_1, TP_PW_STRENGTH_2, TP_PW_STRENGTH_3, TP_PW_STRENGTH_4, TP_PW_STRENGTH_5]) === false || |
178
|
|
|
in_array($access_rights, ['R', 'W', 'NE', 'ND', 'NDNE']) === false |
179
|
|
|
) { |
180
|
|
|
return [ |
181
|
|
|
'error' => true, |
182
|
|
|
'error_header' => 'HTTP/1.1 422 Unprocessable Entity', |
183
|
|
|
'error_message' => 'Invalid parameters' |
184
|
|
|
];} |
185
|
|
|
|
186
|
|
|
// Create folder |
187
|
|
|
require_once TEAMPASS_ROOT_PATH.'/sources/folders.class.php'; |
188
|
|
|
$lang = new Language(); |
189
|
|
|
$folderManager = new FolderManager($lang); |
190
|
|
|
$params = [ |
191
|
|
|
'title' => (string) $title, |
192
|
|
|
'parent_id' => (int) $parent_id, |
193
|
|
|
'complexity' => (int) $complexity, |
194
|
|
|
'duration' => (int) $duration, |
195
|
|
|
'create_auth_without' => (int) $create_auth_without, |
196
|
|
|
'edit_auth_without' => (int) $edit_auth_without, |
197
|
|
|
'icon' => (string) $icon, |
198
|
|
|
'icon_selected' => (string) $icon_selected, |
199
|
|
|
'access_rights' => (string) $access_rights, |
200
|
|
|
'user_is_admin' => (int) $is_admin, |
201
|
|
|
'user_accessible_folders' => (array) $foldersId, |
202
|
|
|
'user_is_manager' => (int) $is_manager, |
203
|
|
|
'user_can_create_root_folder' => (int) $user_can_create_root_folder, |
204
|
|
|
'user_can_manage_all_users' => (int) $user_can_manage_all_users, |
205
|
|
|
'user_id' => (int) $user_id, |
206
|
|
|
'user_roles' => (string) $user_roles |
207
|
|
|
]; |
208
|
|
|
$creationStatus = $folderManager->createNewFolder($params); |
209
|
|
|
|
210
|
|
|
return $creationStatus; |
211
|
|
|
} |
212
|
|
|
} |