Passed
Push — master ( 854912...958461 )
by Nils
05:03
created

buildItemDefinition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Teampass - a collaborative passwords manager.
7
 * ---
8
 * This file is part of the TeamPass project.
9
 * 
10
 * TeamPass is free software: you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, version 3 of the License.
13
 * 
14
 * TeamPass is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 * 
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 * 
22
 * Certain components of this file may be under different licenses. For
23
 * details, see the `licenses` directory or individual file headers.
24
 * ---
25
 * @file      import.queries.php
26
 * @author    Nils Laumaillé ([email protected])
27
 * @copyright 2009-2024 Teampass.net
28
 * @license   GPL-3.0
29
 * @see       https://www.teampass.net
30
 */
31
32
33
use Goodby\CSV\Import\Standard\Lexer;
34
use Goodby\CSV\Import\Standard\Interpreter;
35
use Goodby\CSV\Import\Standard\LexerConfig;
36
use voku\helper\AntiXSS;
37
use TeampassClasses\NestedTree\NestedTree;
38
use TeampassClasses\SessionManager\SessionManager;
39
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
40
use TeampassClasses\Language\Language;
41
use TeampassClasses\PerformChecks\PerformChecks;
42
use TeampassClasses\ConfigManager\ConfigManager;
43
44
// Load functions
45
require_once 'main.functions.php';
46
47
// init
48
loadClasses('DB');
49
$session = SessionManager::getSession();
50
$request = SymfonyRequest::createFromGlobals();
51
$lang = new Language($session->get('user-language') ?? 'english');
52
53
// Load config if $SETTINGS not defined
54
$configManager = new ConfigManager();
55
$SETTINGS = $configManager->getAllSettings();
56
57
// Do checks
58
// Instantiate the class with posted data
59
$checkUserAccess = new PerformChecks(
60
    dataSanitizer(
61
        [
62
            'type' => $request->request->get('type', '') !== '' ? htmlspecialchars($request->request->get('type')) : '',
63
        ],
64
        [
65
            'type' => 'trim|escape',
66
        ],
67
    ),
68
    [
69
        'user_id' => returnIfSet($session->get('user-id'), null),
70
        'user_key' => returnIfSet($session->get('key'), null),
71
    ]
72
);
73
// Handle the case
74
echo $checkUserAccess->caseHandler();
75
if (
76
    $checkUserAccess->userAccessPage('import') === false ||
77
    $checkUserAccess->checkSession() === false
78
) {
79
    // Not allowed page
80
    $session->set('system-error_code', ERR_NOT_ALLOWED);
81
    include $SETTINGS['cpassman_dir'] . '/error.php';
82
    exit;
83
}
84
85
// Define Timezone
86
date_default_timezone_set(isset($SETTINGS['timezone']) === true ? $SETTINGS['timezone'] : 'UTC');
87
88
// Set header properties
89
header('Content-type: text/html; charset=utf-8');
90
header('Cache-Control: no-cache, no-store, must-revalidate');
91
error_reporting(E_ERROR);
92
set_time_limit(0);
93
94
// --------------------------------- //
95
96
// Set some constants for program readability
97
define('KP_PATH', 0);
98
define('KP_GROUP', 1);
99
define('KP_TITLE', 2);
100
define('KP_PASSWORD', 3);
101
define('KP_USERNAME', 4);
102
define('KP_URL', 5);
103
define('KP_UUID', 6);
104
define('KP_NOTES', 7);
105
106
107
$tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
108
109
// POST Varaibles
110
$post_key = filter_input(INPUT_POST, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
111
$post_data = filter_input(INPUT_POST, 'data', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES);
112
113
// Build query
114
switch (filter_input(INPUT_POST, 'type', FILTER_SANITIZE_FULL_SPECIAL_CHARS)) {
115
    //Check if import CSV file format is what expected
116
    case 'import_file_format_csv':
117
        // Check KEY and rights
118
        if ($post_key !== $session->get('key')) {
119
            echo prepareExchangedData(
120
                array(
121
                    'error' => true,
122
                    'message' => $lang->get('key_is_not_correct'),
123
                ),
124
                'encode'
125
            );
126
            break;
127
        }
128
129
        //load full tree
130
        $tree->rebuild();
131
        $tree = $tree->getDescendants();
132
133
        // Init post variable
134
        $post_operation_id = filter_input(INPUT_POST, 'file', FILTER_SANITIZE_NUMBER_INT);
135
136
        // Get filename from database
137
        $data = DB::queryFirstRow(
138
            'SELECT valeur
139
            FROM '.prefixTable('misc').'
140
            WHERE increment_id = %i AND type = "temp_file"',
141
            $post_operation_id
142
        );
143
        
144
        // Delete operation id
145
        DB::delete(
146
            prefixTable('misc'),
147
            "increment_id = %i AND type = 'temp_file'",
148
            $post_operation_id
149
        );
150
151
        // do some initializations
152
        $file = $SETTINGS['path_to_files_folder'].'/'.$data['valeur'];
153
        $importation_possible = true;
154
        $itemsArray = array();
155
        $line_number = 0;
156
        $account = $text = '';
157
        $continue_on_next_line = false;
158
159
        // Open file
160
        if ($fp = fopen($file, 'r')) {
161
            // data from CSV
162
            $valuesToImport = array();
163
164
            // Lexer configuration
165
            $config = new LexerConfig();
166
            $lexer = new Lexer($config);
167
            $config->setIgnoreHeaderLine('true');
168
            $interpreter = new Interpreter();
169
            $interpreter->addObserver(function (array $row) use (&$valuesToImport) {
170
                $valuesToImport[] = array(
171
                    'Label' => $row[0],
172
                    'Login' => $row[1],
173
                    'Password' => $row[2],
174
                    'url' => $row[3],
175
                    'Comments' => $row[4],
176
                );
177
            });
178
            $lexer->parse($file, $interpreter);
179
180
            // extract one line
181
            foreach ($valuesToImport as $key => $row) {
182
                //increment number of lines found
183
                ++$line_number;
184
185
                //Check number of fields. MUST be 5. if not stop importation
186
                if (count($row) != 5) {
187
                    $importation_possible = false;
188
                    //Stop if file has not expected structure
189
                    if ($importation_possible === false) {
190
                        echo '[{"error":"bad_structure"}]';
191
                        break;
192
                    }
193
                }
194
195
                //If any comment is on several lines, then replace 'lf' character
196
                $row['Comments'] = str_replace(array("\r\n", "\n", "\r"), '<br>', $row['Comments']);
197
198
                // Check if current line contains a "<br>" character in order to identify an ITEM on several CSV lines
199
                if (substr_count($row['Comments'], '<br>') > 0 || substr_count($row['Label'], '<br>') > 0) {
200
                    $continue_on_next_line = true;
201
                    $comment .= addslashes($row['Label']);
202
                } else {
203
                    // Store in variable values from previous line
204
                    if (empty($account) === false) {
205
                        if ($continue_on_next_line === false) {
206
                            // Prepare listing that will be shown to user
207
                            array_push(
208
                                $itemsArray,
209
                                array(
210
                                    'label' => $account,
211
                                    'login' => $login,
212
                                    'pwd' => $pwd,
213
                                    'url' => $url,
214
                                    'comment' => $comment,
215
                                )
216
                            );
217
218
                            // Initialize this variable in order to restart from scratch
219
                            $account = '';
220
                        }
221
                    }
222
                }
223
224
                // Get values of current line
225
                if ($account === '' && $continue_on_next_line === false) {
226
                    $account = trim(htmlspecialchars($row['Label'], ENT_QUOTES, 'UTF-8'));
227
                    $login = trim(htmlspecialchars($row['Login'], ENT_QUOTES, 'UTF-8'));
228
                    $pwd = trim(str_replace('"', '&quot;', $row['Password']));
229
                    $url = trim($row['url']);
230
                    $to_find = array('"', "'");
231
                    $to_ins = array('&quot', '&#39;');
232
                    $comment = htmlentities(
233
                        addslashes(str_replace($to_find, $to_ins, $row['Comments'])),
234
                        ENT_QUOTES,
235
                        'UTF-8'
236
                    );
237
238
                    $continue_on_next_line = false;
239
                }
240
            }
241
            // close file
242
            fclose($fp);
243
        } else {
244
            echo prepareExchangedData(
245
                array(
246
                    'error' => true,
247
                    'message' => $lang->get('cannot_open_file'),
248
                ),
249
                'encode'
250
            );
251
252
            //delete file
253
            unlink($file);
254
            break;
255
        }
256
257
        if ($line_number > 0) {
258
            array_push(
259
                $itemsArray,
260
                array(
261
                    'label' => $account,
262
                    'login' => $login,
263
                    'pwd' => $pwd,
264
                    'url' => $url,
265
                    'comment' => $comment,
266
                )
267
            );
268
269
            // Show results to user.
270
            echo prepareExchangedData(
271
                array(
272
                    'error' => false,
273
                    'message' => '',
274
                    'output' => $itemsArray,
275
                    'number' => $line_number++,
276
                ),
277
                'encode'
278
            );
279
        }
280
281
        //delete file
282
        unlink($file);
283
284
        break;
285
286
    //Insert into DB the items the user has selected
287
    case 'import_items':
288
        // Check KEY and rights
289
        if ($post_key !== $session->get('key')) {
290
            echo prepareExchangedData(
291
                array(
292
                    'error' => true,
293
                    'message' => $lang->get('key_is_not_correct'),
294
                ),
295
                'encode'
296
            );
297
            break;
298
        }
299
300
        // Init
301
        $list = [];
302
303
        // Decrypt and retreive data in JSON format
304
        $dataReceived = prepareExchangedData(
305
            $post_data,
306
            'decode'
307
        );
308
309
        // Init post variable
310
        $post_folder = filter_var($dataReceived['folder-id'], FILTER_SANITIZE_NUMBER_INT);
311
312
        // Clean each array entry and exclude password as it will be hashed
313
        $post_items = [];
314
        foreach ($dataReceived['items'] as $item) {
315
            $filtered_item = [];
316
            foreach ($item as $key => $value) {
317
                if ($key !== 'pwd') {
318
                    $value = filter_var($value, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
319
                }
320
                $filtered_item[$key] = $value;
321
            }
322
            $post_items[] = $filtered_item;
323
        }
324
325
        $post_edit_role = filter_var($dataReceived['edit-role'], FILTER_SANITIZE_NUMBER_INT);
326
        $post_edit_all = filter_var($dataReceived['edit-all'], FILTER_SANITIZE_NUMBER_INT);
327
328
        // Get title for this folder
329
        $data_fld = DB::queryFirstRow(
330
            'SELECT title
331
            FROM '.prefixTable('nested_tree').'
332
            WHERE id = %i',
333
            $post_folder
334
        );
335
336
        //Get some info about personal folder
337
        if (in_array($post_folder, $session->get('user-personal_folders')) === true) {
338
            $personalFolder = 1;
339
        } else {
340
            $personalFolder = 0;
341
        }
342
343
        // Clean each array entry
344
        if (is_array($post_items) === true) {
0 ignored issues
show
introduced by
The condition is_array($post_items) === true is always true.
Loading history...
345
            array_walk_recursive($post_items, 'cleanOutput');
346
        }
347
        
348
        // Loop on array
349
        foreach ($post_items as $item) {
350
            //For each item, insert into DB
351
352
            // Handle case where pw is empty
353
            // if not allowed then warn user
354
            if (($session->has('user-create_item_without_password') && null !== $session->get('user-create_item_without_password')
355
                && (int) $session->get('user-create_item_without_password') !== 1
356
                ) ||
357
                empty($item['pwd']) === false
358
            ) {
359
                // NEW ENCRYPTION
360
                $cryptedStuff = doDataEncryption($item['pwd']);
361
            } else {
362
                $cryptedStuff['encrypted'] = '';
363
            }
364
            $post_password = $cryptedStuff['encrypted'];
365
366
            // Insert new item in table ITEMS
367
            DB::insert(
368
                prefixTable('items'),
369
                array(
370
                    'label' => substr($item['label'], 0, 500),
371
                    'description' => empty($item['comment']) === true ? '' : $item['comment'],
372
                    'pw' => $post_password,
373
                    'pw_iv' => '',
374
                    'url' => empty($item['url']) === true ? '' : substr($item['url'], 0, 500),
375
                    'id_tree' => $post_folder,
376
                    'login' => empty($item['login']) === true ? '' : substr($item['login'], 0, 200),
377
                    'anyone_can_modify' => $post_edit_all,
378
                    'encryption_type' => 'teampass_aes',
379
                )
380
            );
381
            $newId = DB::insertId();
382
383
            // Create sharekeys for users
384
            storeUsersShareKey(
385
                prefixTable('sharekeys_items'),
386
                (int) $personalFolder,
387
                (int) $post_folder,
388
                (int) $newId,
389
                $cryptedStuff['objectKey'],
390
            );
391
392
            //if asked, anyone in role can modify
393
            if ((int) $post_edit_role === 1) {
394
                foreach ($session->get('system-array_roles') as $role) {
395
                    DB::insert(
396
                        prefixTable('restriction_to_roles'),
397
                        array(
398
                            'role_id' => $role['id'],
399
                            'item_id' => $newId,
400
                        )
401
                    );
402
                }
403
            }
404
405
            // Insert new item in table LOGS_ITEMS
406
            DB::insert(
407
                prefixTable('log_items'),
408
                array(
409
                    'id_item' => $newId,
410
                    'date' => time(),
411
                    'id_user' => $session->get('user-id'),
412
                    'action' => 'at_creation',
413
                )
414
            );
415
416
            array_push($list, $item['row']);
417
418
            //Add entry to cache table
419
            DB::insert(
420
                prefixTable('cache'),
421
                array(
422
                    'id' => $newId,
423
                    'label' => substr($item['label'], 0, 500),
424
                    'description' => empty($item['comment']) ? '' : $item['comment'],
425
                    'id_tree' => $post_folder,
426
                    'url' => '0',
427
                    'perso' => $personalFolder === 0 ? 0 : 1,
428
                    'login' => empty($item['login']) ? '' : substr($item['login'], 0, 500),
429
                    'folder' => $data_fld['title'],
430
                    'author' => $session->get('user-id'),
431
                    'timestamp' => time(),
432
                    'tags' => '',
433
                    'restricted_to' => '0',
434
                    'renewal_period' => '0',
435
                    'timestamp' => time(),
436
                )
437
            );
438
        }
439
440
        echo prepareExchangedData(
441
            array(
442
                'error' => false,
443
                'message' => '',
444
                'items' => $list,
445
            ),
446
            'encode'
447
        );
448
        break;
449
450
    //Check if import KEEPASS file format is what expected
451
    case 'import_file_format_keepass':
452
        // Check KEY and rights
453
        if ($post_key !== $session->get('key')) {
454
            echo prepareExchangedData(
455
                array(
456
                    'error' => true,
457
                    'message' => $lang->get('key_is_not_correct'),
458
                ),
459
                'encode'
460
            );
461
            break;
462
        }
463
464
        // Decrypt and retreive data in JSON format
465
        $receivedParameters = prepareExchangedData(
466
            $post_data,
467
            'decode'
468
        );
469
        $post_operation_id = filter_var($receivedParameters['file'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
470
        $post_folder_id = filter_var($receivedParameters['folder-id'], FILTER_SANITIZE_NUMBER_INT);
471
472
        // Get filename from database
473
        $data = DB::queryFirstRow(
474
            'SELECT valeur
475
            FROM '.prefixTable('misc').'
476
            WHERE increment_id = %i AND type = "temp_file"',
477
            $post_operation_id
478
        );
479
480
        // Delete operation id
481
        DB::delete(
482
            prefixTable('misc'),
483
            'increment_id = %i AND type = "temp_file"',
484
            $post_operation_id
485
        );
486
487
        // do some initializations
488
        $file = $data['valeur'];
489
490
        //read xml file
491
        if (file_exists($SETTINGS['path_to_files_folder'].'/'.$file)) {
492
            $xml = simplexml_load_file(
493
                $SETTINGS['path_to_files_folder'].'/'.$file
494
            );
495
        }
496
497
        // Convert XML to associative array
498
        $xmlfile = file_get_contents($SETTINGS['path_to_files_folder'].'/'.$file);
499
        $new = simplexml_load_string($xmlfile);
500
        $con = json_encode($new);
501
        $newArr = json_decode($con, true);
502
503
504
        /**
505
         * Recursive function to process the Keepass XML structure.
506
         * 
507
         * @param array $array The current array to process.
508
         * @param string $previousFolder The parent folder ID.
509
         * @param array $newItemsToAdd The new items to add to the database.
510
         * @param int $level The current level of the recursion.
511
         * 
512
         * @return array The new items to add to the database.
513
         */
514
        function recursive($array, $previousFolder, $newItemsToAdd, $level) : array
515
        {
516
            // Handle entries (items)
517
            if (isset($array['Entry'])) {
518
                $newItemsToAdd = handleEntries($array['Entry'], $previousFolder, $newItemsToAdd);
519
            }
520
521
            // Handle groups (folders)
522
            if (isset($array['Group']) && is_array($array['Group'])) {
523
                $newItemsToAdd = handleGroups($array['Group'], $previousFolder, $newItemsToAdd, $level);
524
            }
525
526
            return $newItemsToAdd;
527
        }
528
529
        /**
530
         * Handle entries (items) within the structure.
531
         * It processes each entry and adds it to the new items list.
532
         * 
533
         * @param array $entries The entries to process.
534
         * @param string $previousFolder The parent folder ID.
535
         * @param array $newItemsToAdd The new items to add to the database.
536
         * 
537
         * @return array The new items to add to the database.
538
         */
539
        function handleEntries(array $entries, string $previousFolder, array $newItemsToAdd) : array
540
        {
541
            foreach ($entries as $key => $value) {
542
                // Check if the entry has a 'String' field and process it
543
                if (isset($value['String'])) {
544
                    $newItemsToAdd['items'][] = buildItemDefinition($value['String'], $previousFolder);
545
                } 
546
                // If it's a direct 'String' item, build a simple item
547
                elseif ($key === 'String') {
548
                    $newItemsToAdd['items'][] = buildSimpleItem($value, $previousFolder);
549
                }
550
            }
551
552
            return $newItemsToAdd;
553
        }
554
555
        /**
556
         * Build an item definition from the 'String' fields.
557
         * Converts the key-value pairs into a usable item format.
558
         * 
559
         * @param array $strings The 'String' fields to process.
560
         * @param string $previousFolder The parent folder ID.
561
         * 
562
         * @return array The item definition.
563
         */
564
        function buildItemDefinition(array $strings, string $previousFolder) : array
565
        {
566
            $itemDefinition = [];
567
            // Loop through each 'String' entry and map keys and values
568
            foreach ($strings as $entry) {
569
                $itemDefinition[$entry['Key']] = is_array($entry['Value']) ? '' : $entry['Value'];
570
            }
571
572
            // Set the parent folder and ensure default values for certain fields
573
            $itemDefinition['parentFolderId'] = $previousFolder;
574
            $itemDefinition['Notes'] = $itemDefinition['Notes'] ?? '';
575
            $itemDefinition['URL'] = $itemDefinition['URL'] ?? '';
576
            $itemDefinition['Password'] = $itemDefinition['Password'] ?? '';
577
578
            return $itemDefinition;
579
        }
580
581
        /**
582
         * Build a simple item with predefined fields.
583
         * This is used when there is no associated key, just ordered values.
584
         * 
585
         * @param array $value The ordered values to process.
586
         * @param string $previousFolder The parent folder ID.
587
         * 
588
         * @return array The simple item definition.
589
         */
590
        function buildSimpleItem(array $value, string $previousFolder) : array
591
        {
592
            return [
593
                'Notes' => is_array($value[0]['Value']) ? '' : $value[0]['Value'],
594
                'Title' => is_array($value[2]['Value']) ? '' : $value[2]['Value'],
595
                'Password' => is_array($value[1]['Value']) ? '' : $value[1]['Value'],
596
                'URL' => is_array($value[3]['Value']) ? '' : $value[3]['Value'],
597
                'UserName' => is_array($value[4]['Value']) ? '' : $value[4]['Value'],
598
                'parentFolderId' => $previousFolder,
599
            ];
600
        }
601
602
        /**
603
         * Handle groups (folders) within the structure.
604
         * It processes each group and recursively goes deeper into subgroups and subentries.
605
         * 
606
         * @param array $groups The groups to process.
607
         * @param string $previousFolder The parent folder ID.
608
         * @param array $newItemsToAdd The new items to add to the database.
609
         * 
610
         * @return array The new items to add to the database.
611
         */
612
        function handleGroups($groups, string $previousFolder, array $newItemsToAdd, int $level) : array
613
        {
614
            // If a single group is found, wrap it into an array
615
            if (isset($groups['UUID'])) {
616
                $groups = [$groups];
617
            }
618
619
            // Save the current folder ID to restore it after recursion
620
            $currentFolderId = $previousFolder;
0 ignored issues
show
Unused Code introduced by
The assignment to $currentFolderId is dead and can be removed.
Loading history...
621
622
            foreach ($groups as $group) {
623
                // Add the current group (folder) to the list
624
                $newItemsToAdd['folders'][] = [
625
                    'folderName' => $group['Name'],
626
                    'uuid' => $group['UUID'],
627
                    'parentFolderId' => $previousFolder,
628
                    'level' => $level,
629
                ];
630
631
                // Recursively process entries and subgroups inside this group
632
                $newItemsToAdd = recursive(
633
                    [
634
                        'Entry' => $group['Entry'] ?? '',
635
                        'Group' => $group['Group'] ?? '',
636
                    ],
637
                    $group['UUID'],
638
                    $newItemsToAdd,
639
                    $level + 1
640
                );
641
            }
642
643
            return $newItemsToAdd;
644
        }
645
646
        // Start the recursive processing
647
        $ret = recursive(
648
            array_merge(
649
                ['Entry' => $newArr['Root']['Group']['Entry']],
650
                ['Group' => $newArr['Root']['Group']['Group']],
651
            ),
652
            $post_folder_id,
653
            [
654
                'folders' => [],
655
                'items' => []
656
            ],
657
            1,
658
        );
659
660
        
661
        echo prepareExchangedData(
662
            array(
663
                'error' => false,
664
                'message' => '',
665
                'data' => $ret,
666
            ),
667
            'encode'
668
        );
669
670
        break;
671
672
    // KEEPASS - CREATE FOLDERS
673
    case 'keepass_create_folders':
674
        // Check KEY and rights
675
        if ($post_key !== $session->get('key')) {
676
            echo prepareExchangedData(
677
                array(
678
                    'error' => true,
679
                    'message' => $lang->get('key_is_not_correct'),
680
                ),
681
                'encode'
682
            );
683
            break;
684
        }
685
686
        // Decrypt and retreive data in JSON format
687
        $receivedParameters = prepareExchangedData(
688
            $post_data,
689
            'decode'
690
        );
691
692
        $post_folder_id = filter_var($receivedParameters['folder-id'], FILTER_SANITIZE_NUMBER_INT);
693
        $post_edit_all = filter_var($receivedParameters['edit-all'], FILTER_SANITIZE_NUMBER_INT);
694
        $post_edit_role = filter_var($receivedParameters['edit-role'], FILTER_SANITIZE_NUMBER_INT);
695
        $post_folders = filter_var_array(
696
            $receivedParameters['folders'],
697
            FILTER_SANITIZE_FULL_SPECIAL_CHARS
698
        );
699
700
        // get destination folder informations
701
        $destinationFolderInfos = getFolderComplexity($post_folder_id, $session->get('user-personal_folders'));
702
        $arrFolders[$post_folder_id] = [
703
            'id' => (int) $post_folder_id,
704
            'level' => 1,
705
            'isPF' => false,
706
        ];
707
        $startPathLevel = 1;
708
709
        foreach($post_folders as $folder) {
710
            // get parent id
711
            $parentId = $arrFolders[$folder['parentFolderId']];
712
713
            // create folder in DB
714
            $folderId = createFolder(
715
                $folder['folderName'],
716
                $parentId['id'],
717
                $folder['level'],
718
                $startPathLevel,
719
                $destinationFolderInfos['levelPwComplexity']
720
            );
721
722
            // manage parent
723
            $arrFolders[$folder['uuid']] = [
724
                'id' => (int) $folderId,
725
                'level' => (int) ($folder['level'] + $startPathLevel),
726
                'isPF' => $destinationFolderInfos['importPF'],
727
            ];
728
        }
729
        //rebuild full tree
730
        $tree->rebuild();
731
732
733
        echo prepareExchangedData(
734
            array(
735
                'error' => false,
736
                'message' => '',
737
                'folders' => $arrFolders,
738
            ),
739
            'encode'
740
        );
741
742
        break;
743
744
    // KEEPASS - CREATE ITEMS
745
    case 'keepass_create_items':
746
        // Check KEY and rights
747
        if ($post_key !== $session->get('key')) {
748
            echo prepareExchangedData(
749
                array(
750
                    'error' => true,
751
                    'message' => $lang->get('key_is_not_correct'),
752
                ),
753
                'encode'
754
            );
755
            break;
756
        }
757
758
        // Decrypt and retreive data in JSON format
759
        $receivedParameters = prepareExchangedData(
760
            $post_data,
761
            'decode'
762
        );
763
764
        $post_edit_all = filter_var($receivedParameters['edit-all'], FILTER_SANITIZE_NUMBER_INT);
765
        $post_edit_role = filter_var($receivedParameters['edit-role'], FILTER_SANITIZE_NUMBER_INT);
766
        $post_folders = filter_var_array(
767
            $receivedParameters['folders'],
768
            FILTER_SANITIZE_FULL_SPECIAL_CHARS
769
        );
770
        $item = filter_var_array(
771
            $receivedParameters['items'],
772
            FILTER_SANITIZE_FULL_SPECIAL_CHARS
773
        );
774
        $ret = '';
775
776
        //foreach($post_items as $item) {
777
            // get info about this folder
778
            $destinationFolderMore = DB::queryFirstRow(
779
                'SELECT title FROM '.prefixTable('nested_tree').' WHERE id = %i',
780
                (int) $post_folders[$item['parentFolderId']]['id']
781
            );
782
783
            // Handle case where pw is empty
784
            // if not allowed then warn user
785
            if (($session->has('user-create_item_without_password') && null !== $session->get('user-create_item_without_password')
786
                && (int) $session->get('user-create_item_without_password') !== 1
787
                ) ||
788
                empty($item['Password']) === false
789
            ) {
790
                // NEW ENCRYPTION
791
                $cryptedStuff = doDataEncryption($item['Password']);
792
            } else {
793
                $cryptedStuff['encrypted'] = '';
794
                $cryptedStuff['objectKey'] = '';
795
            }
796
            $post_password = $cryptedStuff['encrypted'];
797
798
            //ADD item
799
            DB::insert(
800
                prefixTable('items'),
801
                array(
802
                    'label' => substr($item['Title'], 0, 500),
803
                    'description' => $item['Notes'],
804
                    'pw' => $cryptedStuff['encrypted'],
805
                    'pw_iv' => '',
806
                    'url' => substr($item['URL'], 0, 500),
807
                    'id_tree' => $post_folders[$item['parentFolderId']]['id'],
808
                    'login' => substr($item['UserName'], 0, 500),
809
                    'anyone_can_modify' => $post_edit_all,
810
                    'encryption_type' => 'teampass_aes',
811
                    'inactif' => 0,
812
                    'restricted_to' => '',
813
                    'perso' => $post_folders[$item['parentFolderId']]['isPF'] === true ? 1 : 0,
814
                )
815
            );
816
            $newId = DB::insertId();
817
818
            // Create sharekeys for users
819
            storeUsersShareKey(
820
                prefixTable('sharekeys_items'),
821
                $post_folders[$item['parentFolderId']]['isPF'] === true ? 1 : 0,
822
                (int) $post_folders[$item['parentFolderId']]['id'],
823
                (int) $newId,
824
                $cryptedStuff['objectKey'],
825
            );
826
827
            //if asked, anyone in role can modify
828
            if ($post_edit_role === 1) {
829
                foreach ($session->get('system-array_roles') as $role) {
830
                    DB::insert(
831
                        prefixTable('restriction_to_roles'),
832
                        array(
833
                            'role_id' => $role['id'],
834
                            'item_id' => $newId,
835
                        )
836
                    );
837
                }
838
            }
839
840
            //Add log
841
            DB::insert(
842
                prefixTable('log_items'),
843
                array(
844
                    'id_item' => $newId,
845
                    'date' => time(),
846
                    'id_user' => $session->get('user-id'),
847
                    'action' => 'at_creation',
848
                    'raison' => 'at_import',
849
                )
850
            );
851
852
            //Add entry to cache table
853
            DB::insert(
854
                prefixTable('cache'),
855
                array(
856
                    'id' => $newId,
857
                    'label' => substr(stripslashes($item['Title']), 0, 500),
858
                    'description' => stripslashes($item['Notes']),
859
                    'url' => substr(stripslashes($item['URL']), 0, 500),
860
                    'tags' => '',
861
                    'id_tree' => $post_folders[$item['parentFolderId']]['id'],
862
                    'perso' => $post_folders[$item['parentFolderId']]['isPF'] === 0 ? 0 : 1,
863
                    'login' => substr(stripslashes($item['UserName']), 0, 500),
864
                    'restricted_to' => '0',
865
                    'folder' => $destinationFolderMore['title'],
866
                    'author' => $session->get('user-id'),
867
                    'renewal_period' => '0',
868
                    'timestamp' => time(),
869
                )
870
            );
871
872
            // prepare return
873
            $ret .= "<li>".substr(stripslashes($item['Title']), 0, 500)." [".$destinationFolderMore['title']."]</li>";
874
        //}
875
876
877
        echo prepareExchangedData(
878
            array(
879
                'error' => false,
880
                'message' => '',
881
                'info' => "<ul>".$ret."</ul>",
882
            ),
883
            'encode'
884
        );
885
886
        break;
887
    }
888
889
890
891
/**
892
 * Create folders during importation
893
 *
894
 * @param string $folderTitle
895
 * @param integer $parentId
896
 * @param integer $folderLevel
897
 * @param integer $startPathLevel
898
 * @param integer $levelPwComplexity
899
 * @return integer
900
 */
901
function createFolder($folderTitle, $parentId, $folderLevel, $startPathLevel, $levelPwComplexity)
902
{
903
    $session = SessionManager::getSession();
904
    //create folder - if not exists at the same level
905
    DB::query(
906
        'SELECT * FROM '.prefixTable('nested_tree').'
907
        WHERE nlevel = %i AND title = %s AND parent_id = %i LIMIT 1',
908
        intval($folderLevel + $startPathLevel),
909
        $folderTitle,
910
        $parentId
911
    );
912
    if (DB::count() === 0) {
913
        //do query
914
        DB::insert(
915
            prefixTable('nested_tree'),
916
            array(
917
                'parent_id' => $parentId,
918
                'title' => stripslashes($folderTitle),
919
                'nlevel' => $folderLevel,
920
                'categories' => '',
921
            )
922
        );
923
        $id = DB::insertId();
924
        //Add complexity level => level is set to "medium" by default.
925
        DB::insert(
926
            prefixTable('misc'),
927
            array(
928
                'type' => 'complex',
929
                'intitule' => $id,
930
                'valeur' => $levelPwComplexity,
931
                'created_at' => time(),
932
            )
933
        );
934
935
        // Indicate that a change has been done to force tree user reload
936
        DB::update(
937
            prefixTable('misc'),
938
            array(
939
                'valeur' => time(),
940
                'updated_at' => time(),
941
            ),
942
            'type = %s AND intitule = %s',
943
            'timestamp',
944
            'last_folder_change'
945
        );
946
947
        //For each role to which the user depends on, add the folder just created.
948
        foreach ($session->get('system-array_roles') as $role) {
949
            DB::insert(
950
                prefixTable('roles_values'),
951
                array(
952
                    'role_id' => $role['id'],
953
                    'folder_id' => $id,
954
                    'type' => 'W',
955
                )
956
            );
957
        }
958
959
        //Add this new folder to the list of visible folders for the user.
960
        $session->set('user-accessible_folders', array_unique(array_merge($session->get('user-accessible_folders'), [$id]), SORT_NUMERIC));
961
962
        return $id;
963
    }
964
    
965
    //get folder actual ID
966
    $data = DB::queryFirstRow(
967
        'SELECT id FROM '.prefixTable('nested_tree').'
968
        WHERE nlevel = %i AND title = %s AND parent_id = %i',
969
        intval($folderLevel + $startPathLevel),
970
        $folderTitle,
971
        $parentId
972
    );
973
    return $data['id'];
974
}
975
976
/** 
977
 * getFolderComplexity
978
 * 
979
 * @param int $folderId
980
 * @param boolean $isFolderPF
981
 * 
982
 * @return array
983
*/
984
function getFolderComplexity($folderId, $isFolderPF)
985
{
986
    // If destination is not ROOT then get the complexity level
987
    if ($isFolderPF === true) {
988
        return [
989
            'levelPwComplexity' => 50,
990
            'startPathLevel' => 1,
991
            'importPF' => true
992
        ];
993
    } elseif ($folderId > 0) {
994
        $data = DB::queryFirstRow(
995
            'SELECT m.valeur as value, t.nlevel as nlevel
996
            FROM '.prefixTable('misc').' as m
997
            INNER JOIN '.prefixTable('nested_tree').' as t ON (m.intitule = t.id)
998
            WHERE m.type = %s AND m.intitule = %s',
999
            'complex',
1000
            $folderId
1001
        );
1002
        return [
1003
            'levelPwComplexity' => $data['value'],
1004
            'startPathLevel' => $data['nlevel'],
1005
            'importPF' => false
1006
        ];
1007
    }
1008
    return [
1009
        'levelPwComplexity' => 50,
1010
        'startPathLevel' => 0,
1011
        'importPF' => false
1012
    ];
1013
}
1014
1015
spl_autoload_register(function ($class) {
1016
    $prefix = 'League\\Csv\\';
1017
    $base_dir = __DIR__.'/src/';
1018
    $len = strlen($prefix);
1019
    if (strncmp($prefix, $class, $len) !== 0) {
1020
        // no, move to the next registered autoloader
1021
        return;
1022
    }
1023
    $relative_class = substr($class, $len);
1024
    $file = $base_dir.str_replace('\\', '/', $relative_class).'.php';
1025
    if (file_exists($file)) {
1026
        require $file;
1027
    }
1028
});
1029
1030
/**
1031
 * Used to format the string ready for insertion in to the database.
1032
 *
1033
 * @param string $str             String to clean
1034
 * @param string $crLFReplacement Replacement
1035
 *
1036
 * @return string
1037
 */
1038
function sanitiseString($str, $crLFReplacement)
1039
{
1040
    $str = preg_replace('#[\r\n]#', $crLFReplacement, (string) $str);
1041
    $str = str_replace('\\', '&#92;', $str);
1042
    $str = str_replace('"', '&quot;', $str);
1043
    if (!empty($str)) {
1044
        addslashes($str);
1045
    }
1046
1047
    return $str;
1048
}
1049
1050
/**
1051
 * Clean array values.
1052
 *
1053
 * @param string $value String to clean
1054
 *
1055
 * @return string
1056
 */
1057
function cleanOutput(&$value)
1058
{
1059
    return htmlspecialchars_decode($value);
1060
}
1061