Passed
Push — master ( fd5210...56a3aa )
by Nils
04:41
created

FolderModel::getFoldersInfo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 3
nop 1
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
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      folderModel.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2024 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 FolderModel extends Database
28
{
29
    public function getFoldersInfo(array $foldersId): array
30
    {
31
        $rows = $this->select( "SELECT id, title FROM " . prefixTable('nested_tree') . " WHERE nlevel=1" );
32
33
        $ret = [];
34
35
        foreach ($rows as $row) {
36
			$isVisible = in_array((int) $row['id'], $foldersId);
37
            $childrens = $this->getFoldersChildren($row['id'], $foldersId);
38
39
            if ($isVisible || count($childrens) > 0) {
40
                array_push(
41
                    $ret,
42
                    [
43
                        'id' => (int) $row['id'],
44
                        'title' => $row['title'],
45
						'isVisible' => $isVisible,
46
                        'childrens' => $childrens
47
                    ]
48
                );
49
            }
50
        }
51
52
        return $ret;
53
    }
54
55
    private function getFoldersChildren(int $parentId, array $foldersId): array
56
    {
57
        $ret = [];
58
        $childrens = $this->select('SELECT id, title FROM ' . prefixTable('nested_tree') . ' WHERE parent_id=' . $parentId);
59
60
        if ( count($childrens) > 0) {
61
            foreach ($childrens as $children) {
62
				$isVisible = in_array((int) $children['id'], $foldersId);
63
                $childs = $this->getFoldersChildren($children['id'], $foldersId);
64
65
                if (in_array((int) $children['id'], $foldersId) || count($childs) > 0) {
66
                    array_push(
67
                        $ret,
68
                        [
69
                            'id' => (int) $children['id'],
70
                            'title' => $children['title'],
71
							'isVisible' => $isVisible,
72
                            'childrens' => $childs
73
                        ]
74
                    );
75
                }
76
            }
77
        }
78
79
        return $ret;
80
    }
81
82
    public function createFolder(
83
        string $title,
84
        int $parent_id,
85
        int $complexity,
86
        int $duration,
87
        int $create_auth_without,
88
        int $edit_auth_without,
89
        string $icon,
90
        string $icon_selected,
91
        string $access_rights,
92
        int $is_admin,
93
        array $foldersId,
94
        int $is_manager,
95
        int $user_can_create_root_folder,
96
        int $user_can_manage_all_users,
97
        int $user_id,
98
        string $user_roles
99
    ): array
100
    {
101
        require_once TEAMPASS_ROOT_PATH.'/sources/folders.functions.php';
102
103
        // Validate inputs
104
        $title = filter_var($title, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
105
        $parent_id = filter_var($parent_id, FILTER_SANITIZE_NUMBER_INT);
106
        $complexity = filter_var($complexity, FILTER_SANITIZE_NUMBER_INT);
107
        $duration = isset($duration) === true ? filter_var($duration, FILTER_SANITIZE_NUMBER_INT) : 0;
108
        $create_auth_without = isset($create_auth_without) === true ? filter_var($create_auth_without, FILTER_SANITIZE_NUMBER_INT) : 0;
109
        $edit_auth_without = isset($edit_auth_without) === true ? filter_var($edit_auth_without, FILTER_SANITIZE_NUMBER_INT) : 0;
110
        $icon = filter_var($icon, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
111
        $icon_selected = filter_var($icon_selected, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
112
        $access_rights = isset($access_rights) === true ? filter_var($access_rights, FILTER_SANITIZE_FULL_SPECIAL_CHARS) : 'W';
113
114
        // Do checks
115
        if (
116
            in_array($complexity, [TP_PW_STRENGTH_1, TP_PW_STRENGTH_2, TP_PW_STRENGTH_3, TP_PW_STRENGTH_4, TP_PW_STRENGTH_5]) === false ||
117
            in_array($access_rights, ['R', 'W', 'NE', 'ND', 'NDNE']) === false
118
        ) {
119
            return [
120
                'error' => true,
121
                'error_header' => 'HTTP/1.1 422 Unprocessable Entity',
122
                'error_message' => 'Invalid parameters'
123
            ];}
124
125
        // Create folder
126
        $creationStatus = createNewFolder(
127
            (string) $title,
128
            (int) $parent_id,
129
            (int) $complexity,
130
            (int) $duration,
131
            (int) $create_auth_without,
132
            (int) $edit_auth_without,
133
            (string) $icon,
134
            (string) $icon_selected,
135
            (string) $access_rights,
136
            (int) $is_admin,
137
            (array) $foldersId,
138
            (int) $is_manager,
139
            (int) $user_can_create_root_folder,
140
            (int) $user_can_manage_all_users,
141
            (int) $user_id,
142
            (string) $user_roles
143
        );
144
145
        return $creationStatus;
146
    }
147
}