Passed
Push — teampass_3.0 ( 66a471...ca4dee )
by Nils
04:34
created

prepareNodeData()   D

Complexity

Conditions 25
Paths 48

Size

Total Lines 135
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 84
c 0
b 0
f 0
nc 48
nop 14
dl 0
loc 135
rs 4.1666

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Teampass - a collaborative passwords manager.
7
 * ---
8
 * This library is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @project   Teampass
13
 * @file      tree.php
14
 *
15
 * @author    Nils Laumaillé ([email protected])
16
 * @copyright 2009-2021 Teampass.net
17
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
18
 *
19
 * @see       https://www.teampass.net
20
 */
21
22
23
require_once 'SecureHandler.php';
24
session_name('teampass_session');
25
session_start();
26
if (
27
    isset($_SESSION['CPM']) === false
28
    || $_SESSION['CPM'] !== 1
29
    || isset($_SESSION['key']) === false
30
    || empty($_SESSION['key']) === true
31
) {
32
    die('Hacking attempt...');
33
}
34
35
// Load config
36
if (file_exists('../includes/config/tp.config.php')) {
37
    include_once '../includes/config/tp.config.php';
38
} elseif (file_exists('./includes/config/tp.config.php')) {
39
    include_once './includes/config/tp.config.php';
40
} else {
41
    throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
42
}
43
44
// includes
45
require_once $SETTINGS['cpassman_dir'] . '/includes/config/include.php';
46
require_once $SETTINGS['cpassman_dir'] . '/sources/SplClassLoader.php';
47
require_once $SETTINGS['cpassman_dir'] . '/sources/main.functions.php';
48
require_once $SETTINGS['cpassman_dir'] . '/includes/language/' . $_SESSION['user_language'] . '.php';
49
require_once $SETTINGS['cpassman_dir'] . '/includes/config/settings.php';
50
51
// header
52
header('Content-type: text/html; charset=utf-8');
53
header('Cache-Control: no-cache, must-revalidate');
54
55
// Define Timezone
56
if (isset($SETTINGS['timezone'])) {
57
    date_default_timezone_set($SETTINGS['timezone']);
58
} else {
59
    date_default_timezone_set('UTC');
60
}
61
62
// Connect to mysql server
63
require_once $SETTINGS['cpassman_dir'] . '/includes/libraries/Database/Meekrodb/db.class.php';
64
if (defined('DB_PASSWD_CLEAR') === false) {
65
    define('DB_PASSWD_CLEAR', defuseReturnDecrypted(DB_PASSWD, $SETTINGS));
66
}
67
DB::$host = DB_HOST;
68
DB::$user = DB_USER;
69
DB::$password = DB_PASSWD_CLEAR;
70
DB::$dbName = DB_NAME;
71
DB::$port = DB_PORT;
72
DB::$encoding = DB_ENCODING;
73
74
// Superglobal load
75
require_once $SETTINGS['cpassman_dir'] . '/includes/libraries/protect/SuperGlobal/SuperGlobal.php';
76
$superGlobal = new protect\SuperGlobal\SuperGlobal();
77
$get = [];
78
$get['user_tree_structure'] = $superGlobal->get('user_tree_structure', 'GET');
79
$get['user_tree_last_refresh_timestamp'] = $superGlobal->get('user_tree_last_refresh_timestamp', 'GET');
80
$get['force_refresh'] = $superGlobal->get('force_refresh', 'GET');
81
$get['id'] = $superGlobal->get('id', 'GET');
82
$session = [];
83
$session['forbiden_pfs'] = $superGlobal->get('forbiden_pfs', 'SESSION');
84
$session['groupes_visibles'] = $superGlobal->get('groupes_visibles', 'SESSION');
85
$session['list_restricted_folders_for_items'] = $superGlobal->get('list_restricted_folders_for_items', 'SESSION');
86
$session['user_id'] = $superGlobal->get('user_id', 'SESSION');
87
$session['login'] = $superGlobal->get('login', 'SESSION');
88
$session['user_read_only'] = $superGlobal->get('user_read_only', 'SESSION');
89
$session['personal_folder'] = $superGlobal->get('personal_folder', 'SESSION');
90
$session['list_folders_limited'] = $superGlobal->get('list_folders_limited', 'SESSION');
91
$session['read_only_folders'] = $superGlobal->get('read_only_folders', 'SESSION');
92
$session['personal_visible_groups'] = $superGlobal->get('personal_visible_groups', 'SESSION');
93
94
95
$lastFolderChange = DB::query(
96
    'SELECT * FROM ' . prefixTable('misc') . '
97
    WHERE type = %s AND intitule = %s',
98
    'timestamp',
99
    'last_folder_change'
100
);
101
if (
102
    empty($get['user_tree_structure']) === true
103
    || ($get['user_tree_last_refresh_timestamp'] !== null && strtotime($lastFolderChange) > strtotime($get['user_tree_last_refresh_timestamp']))
104
    || (isset($get['force_refresh']) === true && (int) $get['force_refresh'] === 1)
105
) {
106
    // Build tree
107
    $tree = new SplClassLoader('Tree\NestedTree', $SETTINGS['cpassman_dir'] . '/includes/libraries');
108
    $tree->register();
109
    $tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
110
111
    if (
112
        isset($session['list_folders_limited']) === true
113
        && count($session['list_folders_limited']) > 0
0 ignored issues
show
Bug introduced by
It seems like $session['list_folders_limited'] can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

113
        && count(/** @scrutinizer ignore-type */ $session['list_folders_limited']) > 0
Loading history...
114
    ) {
115
        $listFoldersLimitedKeys = array_keys($session['list_folders_limited']);
0 ignored issues
show
Bug introduced by
It seems like $session['list_folders_limited'] can also be of type null; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

115
        $listFoldersLimitedKeys = array_keys(/** @scrutinizer ignore-type */ $session['list_folders_limited']);
Loading history...
116
    } else {
117
        $listFoldersLimitedKeys = array();
118
    }
119
    // list of items accessible but not in an allowed folder
120
    if (
121
        isset($session['list_restricted_folders_for_items']) === true
122
        && count($session['list_restricted_folders_for_items']) > 0
123
    ) {
124
        $listRestrictedFoldersForItemsKeys = @array_keys($session['list_restricted_folders_for_items']);
125
    } else {
126
        $listRestrictedFoldersForItemsKeys = array();
127
    }
128
129
    $ret_json = array();
130
    $last_visible_parent = -1;
131
    $last_visible_parent_level = 1;
132
133
    // build the tree to be displayed
134
    if (
135
        isset($get['id']) === true
136
        && is_numeric(intval($get['id'])) === true
137
        && isset($_SESSION['user']['treeloadstrategy']) === true
138
        && $_SESSION['user']['treeloadstrategy'] === 'sequential'
139
    ) {
140
        $ret_json = buildNodeTree(
141
            $get['id'],
142
            $listFoldersLimitedKeys,
143
            $listRestrictedFoldersForItemsKeys,
144
            /** @scrutinizer ignore-type */ $tree,
145
            $SETTINGS
146
        );
147
    } elseif (
148
        isset($_SESSION['user']['treeloadstrategy']) === true
149
        && $_SESSION['user']['treeloadstrategy'] === 'sequential'
150
    ) {
151
        $ret_json = buildNodeTree(
152
            0,
153
            $listFoldersLimitedKeys,
154
            $listRestrictedFoldersForItemsKeys,
155
            /** @scrutinizer ignore-type */ $tree,
156
            $SETTINGS
157
        );
158
    } else {
159
        $completTree = $tree->getTreeWithChildren();
160
        foreach ($completTree[0]->children as $child) {
161
            recursiveTree(
162
                $child,
163
                $completTree,
164
                /** @scrutinizer ignore-type */ $tree,
165
                $listFoldersLimitedKeys,
166
                $listRestrictedFoldersForItemsKeys,
167
                $last_visible_parent,
168
                $last_visible_parent_level,
169
                $SETTINGS,
170
                $ret_json,
171
                $session['forbiden_pfs'],
172
                $session['groupes_visibles'],
173
                $session['list_restricted_folders_for_items'],
174
                $session['user_id'],
175
                $session['login'],
176
                $session['user_read_only'],
177
                $session['personal_folder'],
178
                $session['list_folders_limited'],
179
                $session['read_only_folders'],
180
                $session['personal_visible_groups']
181
            );
182
        }
183
    }
184
185
    // Save in SESSION
186
    $superGlobal->put('user_tree_structure', $ret_json, 'SESSION');
187
    $superGlobal->put('user_tree_last_refresh_timestamp', time(), 'SESSION');
188
189
    // Send back
190
    echo json_encode($ret_json);
191
} else {
192
    //echo '['.$get['user_tree_structure'].']';
193
    echo $get['user_tree_structure'];
194
}
195
196
/**
197
 * Get through asked folders.
198
 *
199
 * @param int   $nodeId                            Id
200
 * @param array $listFoldersLimitedKeys            Limited
201
 * @param array $listRestrictedFoldersForItemsKeys Restricted
202
 * @param array $tree                              The tree
203
 * @param array $SETTINGS                          Teampass settings
204
 *
205
 * @return array
206
 */
207
function buildNodeTree(
208
    $nodeId,
209
    $listFoldersLimitedKeys,
210
    $listRestrictedFoldersForItemsKeys,
211
    $tree,
212
    $SETTINGS
213
) {
214
    // Load library
215
    include_once $SETTINGS['cpassman_dir'] . '/includes/libraries/protect/SuperGlobal/SuperGlobal.php';
216
    $superGlobal = new protect\SuperGlobal\SuperGlobal();
217
218
    // Prepare superGlobal variables
219
    $session_forbiden_pfs = $superGlobal->get('forbiden_pfs', 'SESSION');
220
    $session_groupes_visibles = $superGlobal->get('groupes_visibles', 'SESSION');
221
    $session_list_restricted_folders_for_items = $superGlobal->get('list_restricted_folders_for_items', 'SESSION');
222
    $session_user_id = $superGlobal->get('user_id', 'SESSION');
223
    $session_login = $superGlobal->get('login', 'SESSION');
224
    $session_no_access_folders = $superGlobal->get('no_access_folders', 'SESSION');
225
    $session_list_folders_limited = $superGlobal->get('list_folders_limited', 'SESSION');
226
    $session_read_only_folders = $superGlobal->get('read_only_folders', 'SESSION');
227
    $session_personal_folders = $superGlobal->get('personal_folders', 'SESSION');
228
    $session_personal_visible_groups = $superGlobal->get('personal_visible_groups', 'SESSION');
229
    $session_user_read_only = $superGlobal->get('user_read_only', 'SESSION');
230
231
    $ret_json = array();
232
233
    // Be sure that user can only see folders he/she is allowed to
234
    if (
235
        in_array($nodeId, $session_forbiden_pfs) === false
236
        || in_array($nodeId, $session_groupes_visibles) === true
237
        || in_array($nodeId, $listFoldersLimitedKeys) === true
238
        || in_array($nodeId, $listRestrictedFoldersForItemsKeys) === true
239
    ) {
240
        $nbChildrenItems = 0;
241
242
        // Check if any allowed folder is part of the descendants of this node
243
        $nodeDescendants = $tree->getDescendants($nodeId, false, true, false);
244
        foreach ($nodeDescendants as $node) {
245
            $displayThisNode = false;
246
            if ((in_array($node->id, $session_forbiden_pfs) === false
247
                    || in_array($node->id, $session_groupes_visibles) === true
248
                    || in_array($node->id, $listFoldersLimitedKeys) === true
249
                    || in_array($node->id, $listRestrictedFoldersForItemsKeys)) === true
250
                && (in_array(
251
                    $node->id,
252
                    array_merge($session_groupes_visibles, $session_list_restricted_folders_for_items)
253
                ) === true
254
                    || (is_array($listFoldersLimitedKeys) === true && in_array($node->id, $listFoldersLimitedKeys) === true)
255
                    || (is_array($listRestrictedFoldersForItemsKeys) === true && in_array($node->id, $listRestrictedFoldersForItemsKeys) === true)
256
                    || in_array($node->id, $session_no_access_folders) === true)
257
            ) {
258
                $displayThisNode = true;
259
            }
260
261
            if ($displayThisNode === true) {
262
                $hide_node = $show_but_block = false;
263
                $text = '';
264
                $title = '';
265
266
                // get count of Items in this folder
267
                DB::query(
268
                    'SELECT *
269
                    FROM ' . prefixTable('items') . '
270
                    WHERE inactif=%i AND id_tree = %i',
271
                    0,
272
                    $node->id
273
                );
274
                $itemsNb = DB::count();
275
276
                // get info about current folder
277
                DB::query(
278
                    'SELECT *
279
                    FROM ' . prefixTable('nested_tree') . '
280
                    WHERE parent_id = %i',
281
                    $node->id
282
                );
283
                $childrenNb = DB::count();
284
285
                // If personal Folder, convert id into user name
286
                $node->title = $node->title === $session_user_id && (int) $node->nlevel === 1 ?
287
                    $session_login :
288
                    ($node->title === null ? '' : htmlspecialchars_decode($node->title, ENT_QUOTES));
289
290
                // prepare json return for current node
291
                $parent = $node->parent_id === 0 ? '#' : 'li_' . $node->parent_id;
292
293
                // special case for READ-ONLY folder
294
                $title = $session_user_read_only === true && in_array($node->id, $session_personal_folders) === false ? langHdl('read_only_account') : $title;
295
                $text .= str_replace('&', '&amp;', $node->title);
296
                $restricted = '0';
297
                $folderClass = 'folder';
298
299
                if (in_array($node->id, $session_groupes_visibles)) {
300
                    if (in_array($node->id, $session_read_only_folders)) {
301
                        $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
302
                        $title = langHdl('read_only_account');
303
                        $restricted = 1;
304
                        $folderClass = 'folder_not_droppable';
305
                    } elseif ($session_user_read_only === true && !in_array($node->id, $session_personal_visible_groups)) {
306
                        $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
307
                    }
308
                    $text .=
309
                        ' <span class=\'badge badge-danger ml-2 items_count\' id=\'itcount_' . $node->id . '\'>' . $itemsNb . '</span>'
310
                        .(isset($SETTINGS['tree_counters']) && $SETTINGS['tree_counters'] === 1 ?
311
                            '/'.$nbChildrenItems .'/'.(count($nodeDescendants) - 1)  :
312
                            '')
313
                        .'</span>';
314
                } elseif (in_array($node->id, $listFoldersLimitedKeys)) {
315
                    $restricted = 1;
316
                    $text .= 
317
                        $session_user_read_only === true ?
318
                            "<i class='far fa-eye fa-xs mr-1'></i>" :
319
                            '<span class="badge badge-danger ml-2 items_count" id="itcount_' . $node->id . '">' . count($session_list_folders_limited[$node->id]) . '</span>';
320
                } elseif (in_array($node->id, $listRestrictedFoldersForItemsKeys)) {
321
                    $restricted = 1;
322
                    if ($session_user_read_only === true) {
323
                        $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
324
                    }
325
                    $text .= $session_user_read_only === true ? 
326
                        "<i class='far fa-eye fa-xs mr-1'></i>" :
327
                        '<span class="badge badge-danger ml-2 items_count" id="itcount_' . $node->id . '">' . count($session_list_restricted_folders_for_items[$node->id]) . '</span>';
328
                } else {
329
                    $restricted = 1;
330
                    $folderClass = 'folder_not_droppable';
331
                    if (isset($SETTINGS['show_only_accessible_folders']) && (int) $SETTINGS['show_only_accessible_folders'] === 1) {
332
                        // folder is not visible
333
                        $nodeDirectDescendants = $tree->getDescendants($nodeId, false, true, true);
334
                        if (count($nodeDirectDescendants) > 0) {
335
                            // show it but block it
336
                            $hide_node = false;
337
                            $show_but_block = true;
338
                        } else {
339
                            // hide it
340
                            $hide_node = true;
341
                        }
342
                    } else {
343
                        // folder is visible but not accessible by user
344
                        $show_but_block = true;
345
                    }
346
                }
347
348
                // json
349
                if ($hide_node === false && $show_but_block === false) {
350
                    array_push(
351
                        $ret_json,
352
                        array(
353
                            'id' => 'li_' . $node->id,
354
                            'parent' => $parent,
355
                            'text' => $text,
356
                            'children' => $childrenNb === 0 ? false : true,
357
                            'li_attr' => array(
358
                                'class' => 'jstreeopen',
359
                                'title' => 'ID [' . $node->id . '] ' . $title,
360
                            ),
361
                            'a_attr' => array(
362
                                'id' => 'fld_' . $node->id,
363
                                'class' => $folderClass,
364
                                'onclick' => 'ListerItems(' . $node->id . ', ' . $restricted . ', 0, 1)',
365
                                'data-title' => $node->title,
366
                            ),
367
                        )
368
                    );
369
                } elseif ($show_but_block === true) {
370
                    array_push(
371
                        $ret_json,
372
                        array(
373
                            'id' => 'li_' . $node->id,
374
                            'parent' =>  $parent,
375
                            'children' => $childrenNb === 0 ? false : true,
376
                            'text' => '<i class="fas fa-times fa-xs text-danger mr-1"></i>' . $text,
377
                            'li_attr' => array(
378
                                'class' => '',
379
                                'title' => 'ID [' . $node->id . '] ' . langHdl('no_access'),
380
                            ),
381
                        )
382
                    );
383
                }
384
            }
385
        }
386
    }
387
    return $ret_json;
388
}
389
390
391
/**
392
 * Get through complete tree
393
 *
394
 * @param int     $nodeId                            Id
395
 * @param array   $completTree                       Tree info
396
 * @param array   $tree                              The tree
397
 * @param array   $listFoldersLimitedKeys            Limited
398
 * @param array   $listRestrictedFoldersForItemsKeys Restricted
399
 * @param int     $last_visible_parent               Visible parent
400
 * @param int     $last_visible_parent_level         Parent level
401
 * @param array   $SETTINGS                          Teampass settings
402
 * @param array   $ret_json                          Array
403
 * @param string  $session_forbiden_pfs,
404
 * @param string  $session_groupes_visibles,
405
 * @param string  $session_list_restricted_folders_for_items,
406
 * @param int     $session_user_id,
407
 * @param string  $session_login,
408
 * @param string  $session_user_read_only,
409
 * @param int     $session_personal_folder,
410
 * @param string  $session_list_folders_limited,
411
 * @param string  $session_read_only_folders,
412
 * @param string  $session_personal_visible_groups
413
 *
414
 * @return array
415
 */
416
function recursiveTree(
417
    $nodeId,
418
    $completTree,
419
    $tree,
420
    $listFoldersLimitedKeys,
421
    $listRestrictedFoldersForItemsKeys,
422
    $last_visible_parent,
423
    $last_visible_parent_level,
424
    $SETTINGS,
425
    &$ret_json = array(),
426
    $session_forbiden_pfs,
427
    $session_groupes_visibles,
428
    $session_list_restricted_folders_for_items,
429
    $session_user_id,
430
    $session_login,
431
    $session_user_read_only,
432
    $session_personal_folder,
433
    $session_list_folders_limited,
434
    $session_read_only_folders,
435
    $session_personal_visible_groups
436
) {
437
    $text = '';
438
    $title = '';
439
    $show_but_block = false;
440
441
    // Load config
442
    if (file_exists('../includes/config/tp.config.php')) {
443
        include '../includes/config/tp.config.php';
444
    } elseif (file_exists('./includes/config/tp.config.php')) {
445
        include './includes/config/tp.config.php';
446
    } else {
447
        throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
448
    }
449
450
    // Be sure that user can only see folders he/she is allowed to
451
    if (
452
        in_array($completTree[$nodeId]->id, $session_forbiden_pfs) === false
453
        || in_array($completTree[$nodeId]->id, $session_groupes_visibles) === true
454
    ) {
455
        $displayThisNode = false;
456
        $hide_node = false;
457
        $nbChildrenItems = 0;
458
        $nodeDirectDescendants = $tree->getDescendants($completTree[$nodeId]->id, false, false, true);
459
460
        // Check if any allowed folder is part of the descendants of this node
461
        $nodeDescendants = $tree->getDescendants($completTree[$nodeId]->id, true, false, true);
462
        foreach ($nodeDescendants as $node) {
463
            // manage tree counters
464
            if (
465
                isset($SETTINGS['tree_counters']) === true
466
                && (int) $SETTINGS['tree_counters'] === 1
467
                && in_array(
468
                    $node,
469
                    array_merge($session_groupes_visibles, $session_list_restricted_folders_for_items)
470
                ) === true
471
            ) {
472
                DB::query(
473
                    'SELECT * FROM ' . prefixTable('items') . '
474
                    WHERE inactif=%i AND id_tree = %i',
475
                    0,
476
                    $node
477
                );
478
                $nbChildrenItems += DB::count();
479
            }
480
481
            if (
482
                in_array($node, $session_groupes_visibles) === true
483
            ) {
484
                // Final check - is PF allowed?
485
                $nodeDetails = $tree->getNode($node);
486
                if (
487
                    (int) $nodeDetails->personal_folder === 1
488
                    && (int) $SETTINGS['enable_pf_feature'] === 1
489
                    && (int) $session_personal_folder === 0
490
                ) {
491
                    $displayThisNode = false;
492
                } else {
493
                    $displayThisNode = true;
494
                    // not adding a break in order to permit a correct count of items
495
                }
496
                $hide_node = $show_but_block = false;
497
                $text = $title = '';
498
            }
499
        }
500
501
        if ($displayThisNode === true) {
502
            handleNode(
503
                $nodeId,
504
                $completTree,
505
                $tree,
506
                $listFoldersLimitedKeys,
507
                $listRestrictedFoldersForItemsKeys,
508
                $last_visible_parent,
509
                $last_visible_parent_level,
510
                $SETTINGS,
511
                $ret_json,
512
                $session_forbiden_pfs,
513
                $session_groupes_visibles,
514
                $session_list_restricted_folders_for_items,
515
                $session_user_id,
516
                $session_login,
517
                $session_user_read_only,
518
                $session_personal_folder,
519
                $session_list_folders_limited,
520
                $session_read_only_folders,
521
                $session_personal_visible_groups,
522
                $text,
523
                $title,
524
                $hide_node,
525
                $show_but_block,
526
                $nbChildrenItems,
527
                $nodeDescendants,
528
                $nodeDirectDescendants
529
            );
530
        }
531
    }
532
    return $ret_json;
533
}
534
535
536
function handleNode(
537
    $nodeId,
538
    $completTree,
539
    $tree,
540
    $listFoldersLimitedKeys,
541
    $listRestrictedFoldersForItemsKeys,
542
    $last_visible_parent,
543
    $last_visible_parent_level,
544
    $SETTINGS,
545
    &$ret_json = array(),
546
    $session_forbiden_pfs,
547
    $session_groupes_visibles,
548
    $session_list_restricted_folders_for_items,
549
    $session_user_id,
550
    $session_login,
551
    $session_user_read_only,
552
    $session_personal_folder,
553
    $session_list_folders_limited,
554
    $session_read_only_folders,
555
    $session_personal_visible_groups,
556
    $text,
557
    $title,
0 ignored issues
show
Unused Code introduced by
The parameter $title 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

557
    /** @scrutinizer ignore-unused */ $title,

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...
558
    $hide_node,
0 ignored issues
show
Unused Code introduced by
The parameter $hide_node 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

558
    /** @scrutinizer ignore-unused */ $hide_node,

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...
559
    $show_but_block,
0 ignored issues
show
Unused Code introduced by
The parameter $show_but_block 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

559
    /** @scrutinizer ignore-unused */ $show_but_block,

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...
560
    $nbChildrenItems,
561
    $nodeDescendants,
562
    $nodeDirectDescendants
563
)
564
{
565
    // get info about current folder
566
    DB::query(
567
        'SELECT * FROM ' . prefixTable('items') . '
568
        WHERE inactif=%i AND id_tree = %i',
569
        0,
570
        $completTree[$nodeId]->id
571
    );
572
    $itemsNb = DB::count();
573
574
    // If personal Folder, convert id into user name
575
    if ((int) $completTree[$nodeId]->title === (int) $session_user_id && (int) $completTree[$nodeId]->nlevel === 1) {
576
        $completTree[$nodeId]->title = $session_login;
577
    }
578
579
    // Decode if needed
580
    $completTree[$nodeId]->title = htmlspecialchars_decode($completTree[$nodeId]->title, ENT_QUOTES);
581
582
    $nodeData = prepareNodeData(
583
        (int) $completTree[$nodeId]->id,
584
        $session_groupes_visibles,
585
        $session_read_only_folders,
586
        $session_personal_visible_groups,
587
        (int) $nbChildrenItems,
588
        $nodeDescendants,
589
        (int) $itemsNb,
590
        $session_list_folders_limited,
591
        (int) $SETTINGS['show_only_accessible_folders'],
592
        $nodeDirectDescendants,
593
        (int) $SETTINGS['tree_counters'],
594
        (int) $session_user_read_only,
595
        $listFoldersLimitedKeys,
596
        $listRestrictedFoldersForItemsKeys
597
    );
598
    /*
599
    if (in_array($completTree[$nodeId]->id, $session_groupes_visibles) === true) {
600
        if (in_array($completTree[$nodeId]->id, $session_read_only_folders) === true) {
601
            $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
602
            $title = langHdl('read_only_account');
603
            $restricted = 1;
604
            $folderClass = 'folder_not_droppable';
605
        } elseif (
606
            $session_user_read_only === true
607
            && in_array($completTree[$nodeId]->id, $session_personal_visible_groups) === false
608
        ) {
609
            $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
610
        }
611
        $text .= 
612
            '<span class=\'badge badge-pill badge-light ml-2 items_count\' id=\'itcount_' . $completTree[$nodeId]->id . '\'>' . $itemsNb .
613
            ((isset($SETTINGS['tree_counters']) === true && (int) $SETTINGS['tree_counters'] === 1) ?
614
                '/'.$nbChildrenItems .'/'.(count($nodeDescendants) - 1)  :
615
                '')
616
            . '</span>';
617
    } elseif (in_array($completTree[$nodeId]->id, $listFoldersLimitedKeys) === true) {
618
        $restricted = 1;
619
        if ($session_user_read_only === true) {
620
            $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
621
        }
622
        $text .= '<span class=\'badge badge-pill badge-light ml-2 items_count\' id=\'itcount_' . $completTree[$nodeId]->id . '\'>' . count($session_list_folders_limited[$completTree[$nodeId]->id]);
623
    } elseif (in_array($completTree[$nodeId]->id, $listRestrictedFoldersForItemsKeys) === true) {
624
        $restricted = 1;
625
        if ($session_user_read_only === true) {
626
            $text = "<i class='far fa-eye fa-xs mr-1'></i>" . $text;
627
        }
628
        $text .= '<span class=\'badge badge-pill badge-light ml-2 items_count\' id=\'itcount_' . $completTree[$nodeId]->id . '\'>' . count($session_list_restricted_folders_for_items[$completTree[$nodeId]->id]) . '</span>';
629
    } else {
630
        $restricted = 1;
631
        $folderClass = 'folder_not_droppable';
632
        if (
633
            isset($SETTINGS['show_only_accessible_folders']) === true
634
            && (int) $SETTINGS['show_only_accessible_folders'] === 1
635
            && $nbChildrenItems === 0
636
        ) {
637
            // folder should not be visible
638
            // only if it has no descendants
639
            $nodeDirectDescendants = $tree->getDescendants($nodeId, false, false, true);
640
            if (
641
                count(
642
                    array_diff(
643
                        $nodeDirectDescendants,
644
                        array_merge(
645
                            $session_groupes_visibles,
646
                            array_keys($session_list_restricted_folders_for_items)
647
                        )
648
                    )
649
                ) !== count($nodeDirectDescendants)
650
            ) {
651
                // show it but block it
652
                $show_but_block = true;
653
                $hide_node = false;
654
            } else {
655
                // hide it
656
                $hide_node = true;
657
            }
658
        } else {
659
            // folder is visible but not accessible by user
660
            $show_but_block = true;
661
        }
662
    }
663
    */
664
665
    // prepare json return for current node
666
    $parent = $completTree[$nodeId]->parent_id === '0' ? '#' : 'li_' . $completTree[$nodeId]->parent_id;
667
668
    // handle displaying
669
    if (
670
        isset($SETTINGS['show_only_accessible_folders']) === true
671
        && (int) $SETTINGS['show_only_accessible_folders'] === 1
672
    ) {
673
        if ($nodeData['hide_node'] === true) {
674
            $last_visible_parent = (int) $parent;
675
            $last_visible_parent_level = $completTree[$nodeId]->nlevel--;
676
        } elseif ($completTree[$nodeId]->nlevel < $last_visible_parent_level) {
677
            $last_visible_parent = -1;
678
        }
679
    }
680
681
    // json
682
    if ($nodeData['hide_node'] === false && $nodeData['show_but_block'] === false) {
683
        array_push(
684
            $ret_json,
685
            array(
686
                'id' => 'li_' . $completTree[$nodeId]->id,
687
                'parent' => $last_visible_parent === -1 ? $parent : $last_visible_parent,
688
                'text' => $text.$completTree[$nodeId]->title.$nodeData['html'],
689
                'li_attr' => array(
690
                    'class' => 'jstreeopen',
691
                    'title' => 'ID [' . $completTree[$nodeId]->id . '] ' . $nodeData['title'],
692
                ),
693
                'a_attr' => array(
694
                    'id' => 'fld_' . $completTree[$nodeId]->id,
695
                    'class' => $nodeData['folderClass'],
696
                    'onclick' => 'ListerItems(' . $completTree[$nodeId]->id . ', ' . $nodeData['restricted'] . ', 0, 1)',
697
                    'data-title' => $completTree[$nodeId]->title,
698
                ),
699
            )
700
        );
701
    } elseif ($nodeData['show_but_block'] === true) {
702
        array_push(
703
            $ret_json,
704
            array(
705
                'id' => 'li_' . $completTree[$nodeId]->id,
706
                'parent' => $last_visible_parent === -1 ? $parent : $last_visible_parent,
707
                'text' => '<i class="fas fa-times fa-xs text-danger mr-1"></i>'.$text.$completTree[$nodeId]->title.$nodeData['html'],
708
                'li_attr' => array(
709
                    'class' => '',
710
                    'title' => 'ID [' . $completTree[$nodeId]->id . '] ' . langHdl('no_access'),
711
                ),
712
            )
713
        );
714
    }
715
    foreach ($completTree[$nodeId]->children as $child) {
716
        recursiveTree(
717
            $child,
718
            $completTree,
719
            /** @scrutinizer ignore-type */ $tree,
720
            $listFoldersLimitedKeys,
721
            $listRestrictedFoldersForItemsKeys,
722
            $last_visible_parent,
723
            $last_visible_parent_level,
724
            $SETTINGS,
725
            $ret_json,
726
            $session_forbiden_pfs,
727
            $session_groupes_visibles,
728
            $session_list_restricted_folders_for_items,
729
            $session_user_id,
730
            $session_login,
731
            $session_user_read_only,
732
            $session_personal_folder,
733
            $session_list_folders_limited,
734
            $session_read_only_folders,
735
            $session_personal_visible_groups
736
        );
737
    }
738
}
739
740
741
742
function prepareNodeData(
743
    $nodeId,
744
    $session_groupes_visibles,
745
    $session_read_only_folders,
746
    $session_personal_visible_groups,
747
    $nbChildrenItems,
748
    $nodeDescendants,
749
    $itemsNb,
750
    $session_list_folders_limited,
751
    $show_only_accessible_folders,
0 ignored issues
show
Unused Code introduced by
The parameter $show_only_accessible_folders 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

751
    /** @scrutinizer ignore-unused */ $show_only_accessible_folders,

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...
752
    $nodeDirectDescendants,
753
    $tree_counters,
754
    $session_user_read_only,
755
    $listFoldersLimitedKeys,
756
    $listRestrictedFoldersForItemsKeys
757
): array
758
{
759
    // special case for READ-ONLY folder
760
    if (
761
        $session_user_read_only === true
762
        && in_array($completTree[$nodeId]->id, $session_user_read_only) === false
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $completTree seems to be never defined.
Loading history...
763
    ) {
764
        $title = langHdl('read_only_account');
765
    }
766
767
    if (in_array($nodeId, $session_groupes_visibles) === true) {
768
        if (in_array($nodeId, $session_read_only_folders) === true) {
769
            return [
770
                'html' => '<i class="far fa-eye fa-xs mr-1"></i><span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . $itemsNb .
771
                    ($tree_counters === 1 ? '/'.$nbChildrenItems .'/'.(count($nodeDescendants) - 1)  : '') . '</span>',
772
                'title' => langHdl('read_only_account'),
773
                'restricted' => 1,
774
                'folderClass' => 'folder_not_droppable',
775
                'show_but_block' => false,
776
                'hide_node' => false,
777
            ];
778
        }
779
780
        if (
781
            $session_user_read_only === true
782
            && in_array($nodeId, $session_personal_visible_groups) === false
783
        ) {
784
            return [
785
                'html' => '<i class="far fa-eye fa-xs mr-1"></i><span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . $itemsNb .
786
                    ($tree_counters === 1 ? '/'.$nbChildrenItems .'/'.(count($nodeDescendants) - 1)  : '') . '</span>',
787
                    'title' => $isset($title) === true ? $title : '',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $isset seems to be never defined.
Loading history...
788
                    'restricted' => 0,
789
                    'folderClass' => 'folder',
790
                    'show_but_block' => false,
791
                    'hide_node' => false,
792
            ];
793
        }
794
        
795
        return [
796
            'html' => '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . $itemsNb .
797
                ($tree_counters === 1 ? '/'.$nbChildrenItems .'/'.(count($nodeDescendants) - 1)  : '') . '</span>',
798
                'title' => isset($title) === true ? $title : '',
799
                'restricted' => 0,
800
                'folderClass' => 'folder',
801
                'show_but_block' => false,
802
                'hide_node' => false,
803
        ];
804
    }
805
    
806
    if (in_array($nodeId, $listFoldersLimitedKeys) === true) {
807
        return [
808
            'html' => $session_user_read_only === true ? '<i class="far fa-eye fa-xs mr-1"></i>' : '' .
809
                '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . count($session_list_folders_limited[$nodeId]) . '</span>',
810
            'title' => $isset($title) === true ? $title : '',
811
            'restricted' => 1,
812
            'folderClass' => 'folder',
813
            'show_but_block' => false,
814
            'hide_node' => false,
815
        ];
816
    }
817
    
818
    if (in_array($nodeId, $listRestrictedFoldersForItemsKeys) === true) {
819
        return [
820
            'html' => $session_user_read_only === true ? '<i class="far fa-eye fa-xs mr-1"></i>' : '' .
821
                '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . count($session_list_restricted_folders_for_items[$nodeId]) . '</span>',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $session_list_restricted_folders_for_items seems to be never defined.
Loading history...
822
            'title' => $isset($title) === true ? $title : '',
823
            'restricted' => 1,
824
            'folderClass' => 'folder',
825
            'show_but_block' => false,
826
            'hide_node' => false,
827
        ];
828
    }
829
    
830
    if (
831
        isset($SETTINGS['show_only_accessible_folders']) === true
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $SETTINGS seems to never exist and therefore isset should always be false.
Loading history...
832
        && (int) $SETTINGS['show_only_accessible_folders'] === 1
833
        && $nbChildrenItems === 0
834
    ) {
835
        // folder should not be visible
836
        // only if it has no descendants
837
        if (
838
            count(
839
                array_diff(
840
                    $nodeDirectDescendants,
841
                    array_merge(
842
                        $session_groupes_visibles,
843
                        array_keys($session_list_restricted_folders_for_items)
844
                    )
845
                )
846
            ) !== count($nodeDirectDescendants)
847
        ) {
848
            // show it but block it
849
            return [
850
                'html' => '',
851
                'title' => $isset($title) === true ? $title : '',
852
                'restricted' => 1,
853
                'folderClass' => 'folder_not_droppable',
854
                'show_but_block' => true,
855
                'hide_node' => false,
856
            ];
857
        }
858
        
859
        // hide it
860
        return [
861
            'html' => '',
862
            'title' => $isset($title) === true ? $title : '',
863
            'restricted' => 1,
864
            'folderClass' => 'folder_not_droppable',
865
            'show_but_block' => false,
866
            'hide_node' => true,
867
        ];
868
    }
869
870
    return [
871
        'html' => '',
872
        'title' => isset($title) === true ? $title : '',
873
        'restricted' => 1,
874
        'folderClass' => 'folder_not_droppable',
875
        'show_but_block' => true,
876
        'hide_node' => false,
877
    ];
878
}