Passed
Branch wip_sessions (2e0cc8)
by Nils
04:59
created

prepareNodeData()   D

Complexity

Conditions 23
Paths 30

Size

Total Lines 135
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 86
nc 30
nop 16
dl 0
loc 135
rs 4.1666
c 0
b 0
f 0

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-2023 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
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
24
use TeampassClasses\SessionManager\SessionManager;
25
use TeampassClasses\Language\Language;
26
use TeampassClasses\PerformChecks\PerformChecks;
27
use TeampassClasses\NestedTree\NestedTree;
28
29
// Load functions
30
require_once 'main.functions.php';
31
32
// init
33
loadClasses('DB');
34
$session = SessionManager::getSession();
35
$request = Request::createFromGlobals();
36
$lang = new Language();
37
38
// Load config if $SETTINGS not defined
39
try {
40
    include_once __DIR__.'/../includes/config/tp.config.php';
41
} catch (Exception $e) {
42
    throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
43
}
44
45
// Do checks
46
// Instantiate the class with posted data
47
$checkUserAccess = new PerformChecks(
48
    dataSanitizer(
49
        [
50
            'type' => null !== $request->request->get('type') ? htmlspecialchars($request->request->get('type')) : '',
51
        ],
52
        [
53
            'type' => 'trim|escape',
54
        ],
55
    ),
56
    [
57
        'user_id' => returnIfSet($session->get('user-id'), null),
58
        'user_key' => returnIfSet($session->get('key'), null),
59
    ]
60
);
61
// Handle the case
62
echo $checkUserAccess->caseHandler();
63
if (
64
    $checkUserAccess->userAccessPage('items') === false ||
65
    $checkUserAccess->checkSession() === false
66
) {
67
    // Not allowed page
68
    $session->set('system-error_code', ERR_NOT_ALLOWED);
69
    include $SETTINGS['cpassman_dir'] . '/error.php';
70
    exit;
71
}
72
73
// Define Timezone
74
date_default_timezone_set(isset($SETTINGS['timezone']) === true ? $SETTINGS['timezone'] : 'UTC');
75
76
// Set header properties
77
header('Content-type: text/html; charset=utf-8');
78
header('Cache-Control: no-cache, no-store, must-revalidate');
79
80
// --------------------------------- //
81
82
// Load tree
83
$tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
84
85
// Prepare sanitization
86
$data = [
87
    'forbidenPfs' => null !== $session->get('user-forbiden_personal_folders') ? json_encode($session->get('user-forbiden_personal_folders')) : '{}',
88
    'visibleFolders' => null !== $session->get('user-accessible_folders') ? json_encode($session->get('user-accessible_folders')) : '{}',
89
    'userId' => null !== $session->get('user-id') ? $session->get('user-id') : '',
90
    'userLogin' => null !== $session->get('user-login') ? $session->get('user-login') : '',
91
    'userReadOnly' => null !== $session->get('user-read_only') ? $session->get('user-read_only') : '',
92
    'limitedFolders' => null !== $session->get('user-list_folders_limited') ? json_encode($session->get('user-list_folders_limited')) : '{}',
93
    'readOnlyFolders' => null !== $session->get('user-read_only_folders') ? json_encode($session->get('user-read_only_folders')) : '{}',
94
    'personalVisibleFolders' => null !== $session->get('user-personal_visible_folders') ? json_encode($session->get('user-personal_visible_folders')) : '{}',
95
    'userTreeLastRefresh' => null !== $session->get('user-tree_last_refresh_timestamp') ? $session->get('user-tree_last_refresh_timestamp') : '',
96
    'forceRefresh' => null !== $request->query->get('force_refresh') ? $request->query->get('force_refresh') : '',
97
    'nodeId' => null !== $request->query->get('id') ? $request->query->get('id') : '',
98
    'restrictedFoldersForItems' => null !== $request->query->get('list_restricted_folders_for_items') ? json_encode($request->query->get('list_restricted_folders_for_items')) : '{}',
99
    'noAccessFolders' => null !== $session->get('user-no_access_folders') ? json_encode($session->get('user-no_access_folders')) : '{}',
100
    'personalFolders' => null !== $session->get('user-personal_folders') ? json_encode($session->get('user-personal_folders')) : '{}',
101
    'userCanCreateRootFolder' => null !== $session->get('user-can_create_root_folder') ? json_encode($session->get('user-can_create_root_folder')) : '{}',
102
    'userTreeLoadStrategy' => null !== $session->get('user-tree_load_strategy') ? $session->get('user-tree_load_strategy') : '',
103
];
104
105
$filters = [
106
    'forbidenPfs' => 'cast:array',
107
    'visibleFolders' => 'cast:array',
108
    'userId' => 'cast:integer',
109
    'userLogin' => 'trim|escape',
110
    'userReadOnly' => 'cast:boolean',
111
    'limitedFolders' => 'cast:array',
112
    'readOnlyFolders' => 'cast:array',
113
    'personalVisibleFolders' => 'cast:array',
114
    'userTreeLastRefresh' => 'cast:integer',
115
    'forceRefresh' => 'cast:integer',
116
    'nodeId' => 'cast:integer',
117
    'restrictedFoldersForItems' => 'cast:array',
118
    'noAccessFolders' => 'cast:array',
119
    'personalFolders' => 'cast:array',
120
    'userCanCreateRootFolder' => 'cast:array',
121
    'userTreeLoadStrategy' => 'trim|escape',
122
];
123
124
$inputData = dataSanitizer(
125
    $data,
126
    $filters,
127
    $SETTINGS['cpassman_dir']
0 ignored issues
show
Unused Code introduced by
The call to dataSanitizer() has too many arguments starting with $SETTINGS['cpassman_dir']. ( Ignorable by Annotation )

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

127
$inputData = /** @scrutinizer ignore-call */ dataSanitizer(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
128
);
129
130
$lastFolderChange = DB::queryfirstrow(
131
    'SELECT valeur FROM ' . prefixTable('misc') . '
132
    WHERE type = %s AND intitule = %s',
133
    'timestamp',
134
    'last_folder_change'
135
);
136
if (DB::count() === 0) {
137
    $lastFolderChange['valeur'] = 0;
138
}
139
140
// Should we use a cache or refresh the tree
141
$goTreeRefresh = loadTreeStrategy(
142
    (int) $lastFolderChange['valeur'],
143
    (int) $inputData['userTreeLastRefresh'],
144
    (null === $session->get('user-tree_structure') || empty($session->get('user-tree_structure')) === true) ? [] : $session->get('user-tree_structure'),
145
    (int) $inputData['userId'],
146
    (int) $inputData['forceRefresh']
147
);
148
149
// We don't use the cache if an ID of folder is provided
150
if ($goTreeRefresh['state'] === true || empty($inputData['nodeId']) === false || $inputData['userTreeLoadStrategy'] === 'sequential') {
151
    // Build tree
152
    $tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
153
154
    if (
155
        isset($inputData['limitedFolders']) === true
156
        && is_array($inputData['limitedFolders']) === true
157
        && count($inputData['limitedFolders']) > 0
158
    ) {
159
        $listFoldersLimitedKeys = array_keys($inputData['limitedFolders']);
160
    } else {
161
        $listFoldersLimitedKeys = array();
162
    }
163
    
164
    // list of items accessible but not in an allowed folder
165
    if (
166
        isset($inputData['restrictedFoldersForItems']) === true
167
        && count($inputData['restrictedFoldersForItems']) > 0
168
    ) {
169
        $listRestrictedFoldersForItemsKeys = @array_keys($inputData['restrictedFoldersForItems']);
170
    } else {
171
        $listRestrictedFoldersForItemsKeys = array();
172
    }
173
    
174
    $ret_json = array();
175
    $last_visible_parent = -1;
176
    $last_visible_parent_level = 1;
177
    $nodeId = isset($inputData['nodeId']) === true && is_int($inputData['nodeId']) === true ? $inputData['nodeId'] : 0;
178
179
    // build the tree to be displayed
180
    if (showFolderToUser(
181
        $nodeId,
182
        $inputData['forbidenPfs'],
183
        $inputData['visibleFolders'],
184
        $listFoldersLimitedKeys,
185
        $listRestrictedFoldersForItemsKeys
186
    ) === true)
187
    {
188
        if ($inputData['userTreeLoadStrategy'] === 'sequential') {
189
            // SEQUENTIAL MODE
190
            $completTree = $tree->getDescendants(empty($nodeId) === true ? 0 : (int) $nodeId, false, true, false);
191
            foreach ($completTree as $child) {
192
                recursiveTree(
193
                    (int) $child->id,
194
                    $child,
195
                    /** @scrutinizer ignore-type */ $tree,
196
                    $listFoldersLimitedKeys,
197
                    $listRestrictedFoldersForItemsKeys,
198
                    $last_visible_parent,
199
                    $last_visible_parent_level,
200
                    $SETTINGS,
201
                    $inputData,
202
                    $ret_json
203
                );
204
            }
205
206
        } else {
207
            // FULL MODE
208
            $completTree = $tree->getTreeWithChildren();
209
            foreach ($completTree[0]->children as $child) {
210
                recursiveTree(
211
                    (int) $child,
212
                    $completTree[$child],
213
                    /** @scrutinizer ignore-type */ $tree,
214
                    $listFoldersLimitedKeys,
215
                    $listRestrictedFoldersForItemsKeys,
216
                    $last_visible_parent,
217
                    $last_visible_parent_level,
218
                    $SETTINGS,
219
                    $inputData,
220
                    $ret_json
221
                );
222
            }
223
        }
224
    }
225
226
    // Save in SESSION
227
    $session->set('user-tree_structure', $ret_json);
228
    $session->set('user-tree_last_refresh_timestamp', time());
229
230
    $ret_json = json_encode($ret_json);
231
232
    // Save user folders tree
233
    cacheTreeUserHandler(
234
        (int) $inputData['userId'],
235
        $ret_json,
236
        $SETTINGS
237
    );
238
239
    // Add new process
240
    DB::insert(
241
        prefixTable('processes'),
242
        array(
243
            'created_at' => time(),
244
            'process_type' => 'user_build_cache_tree',
245
            'arguments' => json_encode([
246
                'user_id' => (int) $inputData['userId'],
247
            ], JSON_HEX_QUOT | JSON_HEX_TAG),
248
            'updated_at' => '',
249
            'finished_at' => '',
250
            'output' => '',
251
        )
252
    );
253
254
    // Send back
255
    echo $ret_json;
256
} else {
257
    echo $goTreeRefresh['data'];
258
}
259
260
261
/**
262
 * Check if user can see this folder based upon rights
263
 *
264
 * @param integer $nodeId
265
 * @param array $session_forbiden_pfs
266
 * @param array $session_groupes_visibles
267
 * @param array $listFoldersLimitedKeys
268
 * @param array $listRestrictedFoldersForItemsKeys
269
 * @return boolean
270
 */
271
function showFolderToUser(
272
    int $nodeId,
273
    array $session_forbiden_pfs,
274
    array $session_groupes_visibles,
275
    array $listFoldersLimitedKeys,
276
    array $listRestrictedFoldersForItemsKeys
277
): bool
278
{
279
    $big_array = array_diff(
280
        array_unique(
281
            array_merge(
282
                $session_groupes_visibles, 
283
                $listFoldersLimitedKeys, 
284
                $listRestrictedFoldersForItemsKeys
285
            ), 
286
            SORT_NUMERIC
287
        ), 
288
        $session_forbiden_pfs
289
    );
290
    if ($nodeId === 0 || in_array($nodeId, $big_array) === true) {
291
        return true;
292
    }
293
    return false;
294
}
295
296
297
298
/**
299
 * Get through complete tree
300
 *
301
 * @param int     $nodeId                            Id
302
 * @param stdClass   $currentNode                       Tree info
303
 * @param NestedTree   $tree                              The tree
304
 * @param array   $listFoldersLimitedKeys            Limited
305
 * @param array   $listRestrictedFoldersForItemsKeys Restricted
306
 * @param int     $last_visible_parent               Visible parent
307
 * @param int     $last_visible_parent_level         Parent level
308
 * @param array   $SETTINGS                          Teampass settings
309
 * @param array   $inputData,
310
 * @param array   $ret_json
311
 *
312
 * @return array
313
 */
314
function recursiveTree(
315
    int $nodeId,
316
    stdClass $currentNode,
317
    NestedTree $tree,
318
    array $listFoldersLimitedKeys,
319
    array $listRestrictedFoldersForItemsKeys,
320
    int $last_visible_parent,
321
    int $last_visible_parent_level,
322
    array $SETTINGS,
323
    array $inputData,
324
    array &$ret_json = array()
325
) {
326
    $text = '';
327
    
328
    // Load config
329
    include __DIR__.'/../includes/config/tp.config.php';
330
331
    $displayThisNode = false;
332
    $nbItemsInSubfolders = $nbSubfolders = $nbItemsInFolder = 0;
333
    $nodeDescendants = $tree->getDescendants($nodeId, true, false, false);
334
    
335
    foreach ($nodeDescendants as $node) {
336
        if (
337
            in_array($node->id, array_merge($inputData['personalFolders'], $inputData['visibleFolders'])) === true
338
        ) {
339
            // Final check - is PF allowed?
340
            if (
341
                (int) $node->personal_folder === 1
342
                && (int) $SETTINGS['enable_pf_feature'] === 1
343
                && in_array($node->id, $inputData['personalFolders']) === false
344
            ) {
345
                $displayThisNode = false;
346
            } else {
347
                $displayThisNode = true;
348
                $nbItemsInSubfolders = (int) $node->nb_items_in_subfolders;
349
                $nbItemsInFolder = (int) $node->nb_items_in_folder;
350
                $nbSubfolders = (int) $node->nb_subfolders;
351
                break;
352
            }
353
        }
354
    }
355
    
356
    if ($displayThisNode === true) {
0 ignored issues
show
introduced by
The condition $displayThisNode === true is always false.
Loading history...
357
        handleNode(
358
            (int) $nodeId,
359
            $currentNode,
360
            $tree,
361
            $listFoldersLimitedKeys,
362
            $listRestrictedFoldersForItemsKeys,
363
            $last_visible_parent,
364
            $last_visible_parent_level,
365
            $SETTINGS,
366
            $inputData,
367
            $text,
368
            $nbItemsInSubfolders,
369
            $nbSubfolders,
370
            $nbItemsInFolder,
371
            $ret_json
372
        );
373
    }
374
    
375
    return $ret_json;
376
}
377
378
/**
379
 * Permits to get the Node definition
380
 *
381
 * @param integer $nodeId
382
 * @param stdClass $currentNode
383
 * @param NestedTree $tree
384
 * @param array $listFoldersLimitedKeys
385
 * @param array $listRestrictedFoldersForItemsKeys
386
 * @param integer $last_visible_parent
387
 * @param integer $last_visible_parent_level
388
 * @param array $SETTINGS
389
 * @param array $inputData
390
 * @param string $text
391
 * @param integer $nbItemsInSubfolders
392
 * @param integer $nbSubfolders
393
 * @param integer $nbItemsInFolder
394
 * @param array $ret_json
395
 * @return void
396
 */
397
function handleNode(
398
    int $nodeId,
399
    stdClass $currentNode,
400
    NestedTree $tree,
401
    array $listFoldersLimitedKeys,
402
    array $listRestrictedFoldersForItemsKeys,
403
    int $last_visible_parent,
404
    int $last_visible_parent_level,
405
    array $SETTINGS,
406
    array $inputData,
407
    string $text,
408
    int $nbItemsInSubfolders,
409
    int $nbSubfolders,
410
    int $nbItemsInFolder,
411
    array &$ret_json = array()
412
)
413
{
414
    // If personal Folder, convert id into user name
415
    if ((int) $currentNode->title === (int) $inputData['userId'] && (int) $currentNode->nlevel === 1) {
416
        $currentNode->title = $inputData['userLogin'];
417
    }
418
419
    // Decode if needed
420
    $currentNode->title = htmlspecialchars_decode($currentNode->title, ENT_QUOTES);
421
422
    $nodeData = prepareNodeData(
423
        (int) $nodeId,
424
        $inputData['visibleFolders'],
425
        $inputData['readOnlyFolders'],
426
        $inputData['personalVisibleFolders'],
427
        (int) $nbItemsInFolder,
428
        (int) $nbItemsInSubfolders,
429
        (int) $nbSubfolders,
430
        $inputData['limitedFolders'],
431
        (int) $SETTINGS['show_only_accessible_folders'],
432
        isset($SETTINGS['tree_counters']) === true && isset($SETTINGS['enable_tasks_manager']) === true && (int) $SETTINGS['enable_tasks_manager'] === 1 && (int) $SETTINGS['tree_counters'] === 1 ? 1 : 0,
433
        (bool) $inputData['userReadOnly'],
434
        $listFoldersLimitedKeys,
435
        $listRestrictedFoldersForItemsKeys,
436
        $inputData['restrictedFoldersForItems'],
437
        $inputData['personalFolders'],
438
        $tree
439
    );
440
441
    // Prepare JSON 
442
    $tmpRetArray = prepareNodeJson(
443
        $currentNode,
444
        $nodeData,
445
        $inputData,
446
        $ret_json,
447
        $last_visible_parent,
448
        $last_visible_parent_level,
449
        $nodeId,
450
        $text,
451
        $nbSubfolders,
452
        $SETTINGS
453
    );    
454
    $last_visible_parent = $tmpRetArray['last_visible_parent'];
455
    $last_visible_parent_level = $tmpRetArray['last_visible_parent_level'];
456
    $ret_json = $tmpRetArray['ret_json'];
457
458
    // ensure we get the children of the folder
459
    if (isset($currentNode->children) === false) {
460
        $currentNode->children = $tree->getDescendants($nodeId, false, true, true);
461
    }
462
    if ($inputData['userTreeLoadStrategy'] === 'full' && isset($currentNode->children) === true) {
463
        foreach ($currentNode->children as $child) {
464
            recursiveTree(
465
                (int) $child,
466
                $tree->getNode($child),// get node info for this child
467
                /** @scrutinizer ignore-type */ $tree,
468
                $listFoldersLimitedKeys,
469
                $listRestrictedFoldersForItemsKeys,
470
                $last_visible_parent,
471
                $last_visible_parent_level,
472
                $SETTINGS,
473
                $inputData,
474
                $ret_json
475
            );
476
        }
477
    }
478
}
479
480
/**
481
 * Permits to prepare the node data
482
 *
483
 * @param stdClass $currentNode
484
 * @param array $nodeData
485
 * @param array $inputData
486
 * @param array $ret_json
487
 * @param integer $last_visible_parent
488
 * @param integer $last_visible_parent_level
489
 * @param integer $nodeId
490
 * @param string $text
491
 * @param integer $nbSubfolders
492
 * @param array $SETTINGS
493
 * @return array
494
 */
495
function prepareNodeJson(
496
    stdClass $currentNode,
497
    array $nodeData,
498
    array $inputData,
499
    array &$ret_json,
500
    int &$last_visible_parent,
501
    int &$last_visible_parent_level,
502
    int $nodeId,
503
    string $text,
504
    int $nbSubfolders,
505
    array $SETTINGS
506
): array
507
{
508
    // Load user's language
509
    $lang = new Language(); 
510
511
    // prepare json return for current node
512
    $parent = $currentNode->parent_id === '0' ? '#' : 'li_' . $currentNode->parent_id;
513
514
    // handle displaying
515
    if (isKeyExistingAndEqual('show_only_accessible_folders', 1, $SETTINGS) === true) {
516
        if ($nodeData['hide_node'] === true) {
517
            $last_visible_parent = (int) $parent;
518
            $last_visible_parent_level = $currentNode->nlevel--;
519
        } elseif ($currentNode->nlevel < $last_visible_parent_level) {
520
            $last_visible_parent = -1;
521
        }
522
    }
523
524
    // json
525
    if ($nodeData['hide_node'] === false && $nodeData['show_but_block'] === false) {
526
        array_push(
527
            $ret_json,
528
            array(
529
                'id' => 'li_' . $nodeId,
530
                'parent' => $last_visible_parent === -1 ? $parent : $last_visible_parent,
531
                'text' => '<i class="'.$currentNode->fa_icon.' tree-folder mr-2" data-folder="'.$currentNode->fa_icon.'"  data-folder-selected="'.$currentNode->fa_icon_selected.'"></i>'.$text.$currentNode->title.$nodeData['html'],
532
                'li_attr' => array(
533
                    'class' => 'jstreeopen',
534
                    'title' => 'ID [' . $nodeId . '] ' . $nodeData['title'],
535
                ),
536
                'a_attr' => array(
537
                    'id' => 'fld_' . $nodeId,
538
                    'class' => $nodeData['folderClass'],
539
                    'onclick' => 'ListerItems(' . $nodeId . ', ' . $nodeData['restricted'] . ', 0, 1)',
540
                    'data-title' => $currentNode->title,
541
                ),
542
                'is_pf' => in_array($nodeId, $inputData['personalFolders']) === true ? 1 : 0,
543
                'can_edit' => (int) $inputData['userCanCreateRootFolder'],
544
            )
545
        );
546
        
547
        if ($inputData['userTreeLoadStrategy'] === 'sequential') {
548
            $ret_json[count($ret_json) - 1]['children'] = $nbSubfolders > 0 ? true : false;
549
        }
550
551
    } elseif ($nodeData['show_but_block'] === true) {
552
        array_push(
553
            $ret_json,
554
            array(
555
                'id' => 'li_' . $nodeId,
556
                'parent' => $last_visible_parent === -1 ? $parent : $last_visible_parent,
557
                'text' => '<i class="'.$currentNode->fa_icon.' tree-folder mr-2" data-folder="'.$currentNode->fa_icon.'"  data-folder-selected="'.$currentNode->fa_icon_selected.'"></i>'.'<i class="fas fa-times fa-xs text-danger mr-1 ml-1"></i>'.$text.$currentNode->title.$nodeData['html'],
558
                'li_attr' => array(
559
                    'class' => '',
560
                    'title' => 'ID [' . $nodeId . '] ' . $lang->get('no_access'),
561
                ),
562
            )
563
        );
564
    }
565
566
    return [
567
        'last_visible_parent' => (int) $last_visible_parent,
568
        'last_visible_parent_level' => (int) $last_visible_parent_level,
569
        'ret_json' => $ret_json
570
    ];
571
}
572
573
/**
574
 * Get the context of the folder
575
 *
576
 * @param integer $nodeId
577
 * @param array $session_groupes_visibles
578
 * @param array $session_read_only_folders
579
 * @param array $session_personal_visible_groups
580
 * @param integer $nbItemsInFolder
581
 * @param integer $nbItemsInSubfolders
582
 * @param integer $nbSubfolders
583
 * @param array $session_list_folders_limited
584
 * @param integer $show_only_accessible_folders
585
 * @param integer $tree_counters
586
 * @param bool $session_user_read_only
587
 * @param array $listFoldersLimitedKeys
588
 * @param array $listRestrictedFoldersForItemsKeys
589
 * @param array $session_list_restricted_folders_for_items
590
 * @param array $session_personal_folder
591
 * @param NestedTree $tree
592
 * @return array
593
 */
594
function prepareNodeData(
595
    int $nodeId,
596
    array $session_groupes_visibles,
597
    array $session_read_only_folders,
598
    array $session_personal_visible_groups,
599
    int $nbItemsInFolder,
600
    int $nbItemsInSubfolders,
601
    int $nbSubfolders,
602
    array $session_list_folders_limited,
603
    int $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

603
    /** @scrutinizer ignore-unused */ int $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...
604
    int $tree_counters,
605
    bool $session_user_read_only,
606
    array $listFoldersLimitedKeys,
607
    array $listRestrictedFoldersForItemsKeys,
608
    array $session_list_restricted_folders_for_items,
609
    array $session_personal_folder,
610
    NestedTree $tree
611
): array
612
{
613
    // Load user's language
614
    $lang = new Language(); 
615
616
    if (in_array($nodeId, $session_groupes_visibles) === true) {
617
        // special case for READ-ONLY folder
618
        if (in_array($nodeId, $session_read_only_folders) === true) {
619
            return [
620
                'html' => '<i class="far fa-eye fa-xs mr-1 ml-1"></i>'.
621
                    ($tree_counters === 1 ? '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . $nbItemsInFolder .'/'.$nbItemsInSubfolders .'/'.$nbSubfolders. '</span>'  : ''),
622
                'title' => $lang->get('read_only_account'),
623
                'restricted' => 1,
624
                'folderClass' => 'folder_not_droppable',
625
                'show_but_block' => false,
626
                'hide_node' => false,
627
                'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
628
            ];
629
630
        } elseif (
631
            $session_user_read_only === true
632
            && in_array($nodeId, $session_personal_visible_groups) === false
633
        ) {
634
            return [
635
                'html' => '<i class="far fa-eye fa-xs mr-1"></i>'.
636
                    ($tree_counters === 1 ? '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . $nbItemsInFolder .'/'.$nbItemsInSubfolders .'/'.$nbSubfolders. '</span>'  : ''),
637
                'title' => $lang->get('read_only_account'),
638
                'restricted' => 0,
639
                'folderClass' => 'folder',
640
                'show_but_block' => false,
641
                'hide_node' => false,
642
                'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
643
            ];
644
        }
645
        
646
        return [
647
            'html' => ($tree_counters === 1 ? '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . $nbItemsInFolder .'/'.$nbItemsInSubfolders .'/'.$nbSubfolders. '</span>'  : ''),
648
            'title' => '',
649
            'restricted' => 0,
650
            'folderClass' => 'folder',
651
            'show_but_block' => false,
652
            'hide_node' => false,
653
            'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
654
        ];
655
656
    } elseif (in_array($nodeId, $listFoldersLimitedKeys) === true) {
657
        return [
658
            'html' => ($session_user_read_only === true ? '<i class="far fa-eye fa-xs mr-1"></i>' : '') .
659
                ($tree_counters === 1 ? '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . count($session_list_folders_limited[$nodeId]) . '</span>' : ''),
660
            'title' => '',
661
            'restricted' => 1,
662
            'folderClass' => 'folder',
663
            'show_but_block' => false,
664
            'hide_node' => false,
665
            'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
666
        ];
667
668
    } elseif (in_array($nodeId, $listRestrictedFoldersForItemsKeys) === true) {
669
        return [
670
            'html' => $session_user_read_only === true ? '<i class="far fa-eye fa-xs mr-1"></i>' : '' .
671
                '<span class="badge badge-pill badge-light ml-2 items_count" id="itcount_' . $nodeId . '">' . count($session_list_restricted_folders_for_items[$nodeId]) . '</span>',
672
            'title' => '',
673
            'restricted' => 1,
674
            'folderClass' => 'folder',
675
            'show_but_block' => false,
676
            'hide_node' => false,
677
            'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
678
        ];
679
680
    } elseif ((int) $nbSubfolders === 0
681
        //&& (int) $show_only_accessible_folders === 1
682
    ) {
683
        // folder should not be visible
684
        // only if it has no descendants
685
        $nodeDirectDescendants = $tree->getDescendants($nodeId, false, false, true);
686
        if (
687
            count(
688
                array_diff(
689
                    $nodeDirectDescendants,
690
                    array_merge(
691
                        $session_groupes_visibles,
692
                        array_keys($session_list_restricted_folders_for_items)
693
                    )
694
                )
695
            ) !== count($nodeDirectDescendants)
696
        ) {
697
            // show it but block it
698
            return [
699
                'html' => '',
700
                'title' => '',
701
                'restricted' => 1,
702
                'folderClass' => 'folder_not_droppable',
703
                'show_but_block' => true,
704
                'hide_node' => false,
705
                'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
706
            ];
707
        }
708
        
709
        // hide it
710
        return [
711
            'html' => '',
712
            'title' => '',
713
            'restricted' => 1,
714
            'folderClass' => 'folder_not_droppable',
715
            'show_but_block' => false,
716
            'hide_node' => true,
717
            'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
718
        ];
719
    }
720
721
    return [
722
        'html' => '',
723
        'title' => '',
724
        'restricted' => 1,
725
        'folderClass' => 'folder_not_droppable',
726
        'show_but_block' => true,
727
        'hide_node' => false,
728
        'is_pf' => in_array($nodeId, $session_personal_folder) === true ? 1 : 0,
729
    ];
730
}
731
732
733
/**
734
 * Permits to check if we can user a cache instead of loading from DB
735
 *
736
 * @param integer $lastTreeChange
737
 * @param integer $userTreeLastRefresh
738
 * @param array $userSessionTreeStructure
739
 * @param integer $userId
740
 * @param integer $forceRefresh
741
 * @param array $SETTINGS
742
 * @return array
743
 */
744
function loadTreeStrategy(
745
    int $lastTreeChange,
746
    int $userTreeLastRefresh,
747
    array $userSessionTreeStructure,
748
    int $userId,
749
    int $forceRefresh
750
): array
751
{
752
    // Case when refresh is EXPECTED / MANDATORY
753
    if ((int) $forceRefresh === 1) {
754
        return [
755
            'state' => true,
756
            'data' => [],
757
        ];
758
    }
759
760
    // Case when an update in the tree has been done
761
    // Refresh is then mandatory
762
    if ((int) $lastTreeChange > (int) $userTreeLastRefresh) {
763
        return [
764
            'state' => true,
765
            'data' => [],
766
        ];
767
    }
768
769
    // Does this user has the tree structure in session?
770
    // If yes then use it
771
    if (count($userSessionTreeStructure) > 0) {
772
        return [
773
            'state' => false,
774
            'data' => json_encode($userSessionTreeStructure),
775
        ];
776
    }
777
778
    // Does this user has a tree cache
779
    $userCacheTree = DB::queryfirstrow(
780
        'SELECT data
781
        FROM ' . prefixTable('cache_tree') . '
782
        WHERE user_id = %i',
783
        $userId
784
    );
785
    if (empty($userCacheTree['data']) === false && $userCacheTree['data'] !== '[]') {
786
        return [
787
            'state' => false,
788
            'data' => $userCacheTree['data'],
789
        ];
790
    }
791
792
    return [
793
        'state' => true,
794
        'data' => [],
795
    ];
796
}