Passed
Push — master ( 69fa1a...d5bf88 )
by Nils
04:26
created

ItemModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 79
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B getItems() 0 65 8
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This library is distributed in the hope that it will be useful,
6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8
 * ---
9
 *
10
 * @project   Teampass
11
 * @version    API
12
 *
13
 * @file      ItemModel.php
14
 * ---
15
 *
16
 * @author    Nils Laumaillé ([email protected])
17
 *
18
 * @copyright 2009-2023 Teampass.net
19
 *
20
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
21
 * ---
22
 *
23
 * @see       https://www.teampass.net
24
 */
25
require_once API_ROOT_PATH . "/Model/Database.php";
26
27
class ItemModel extends Database
28
{
29
30
31
    /**
32
     * Get the list of items to return
33
     *
34
     * @param string $sqlExtra
35
     * @param integer $limit
36
     * @param string $userPrivateKey
37
     * @param integer $userId
38
     * 
39
     * @return array
40
     */
41
    public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, int $userId): array
42
    {
43
        $rows = $this->select(
44
            "SELECT i.id, label, description, i.pw, i.url, i.id_tree, i.login, i.email, i.viewed_no, i.fa_icon, i.inactif, i.perso, t.title as folder_label
45
            FROM ".prefixTable('items')." as i
46
            LEFT JOIN ".prefixTable('nested_tree')." as t ON (t.id = i.id_tree) ".
47
            $sqlExtra . 
48
            " ORDER BY i.id ASC" .
49
            ($limit > 0 ? " LIMIT ?". ["i", $limit] : '')
0 ignored issues
show
Bug introduced by
Are you sure array('i', $limit) of type array<integer,integer|string> can be used in concatenation? ( Ignorable by Annotation )

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

49
            ($limit > 0 ? " LIMIT ?". /** @scrutinizer ignore-type */ ["i", $limit] : '')
Loading history...
50
        );
51
        $ret = [];
52
        foreach ($rows as $row) {
53
            $userKey = $this->select(
54
                'SELECT share_key
55
                FROM ' . prefixTable('sharekeys_items') . '
56
                WHERE user_id = '.$userId.' AND object_id = '.$row['id']                
57
            );
58
            if (count($userKey) === 0 || empty($row['pw']) === true) {
59
                // No share key found
60
                $pwd = '';
61
            } else {
62
                $pwd = base64_decode(doDataDecryption(
63
                    $row['pw'],
64
                    decryptUserObjectKey(
65
                        $userKey[0]['share_key'],
66
                        $userPrivateKey
67
                    )
68
                ));
69
            }
70
71
            // get path to item
72
            require_once API_ROOT_PATH. '/../includes/libraries/Tree/NestedTree/NestedTree.php';
73
            $tree = new Tree\NestedTree\NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
74
            $arbo = $tree->getPath($row['id_tree'], false);
75
            $path = '';
76
            foreach ($arbo as $elem) {
77
                if (empty($path) === true) {
78
                    $path = htmlspecialchars(stripslashes(htmlspecialchars_decode($elem->title, ENT_QUOTES)), ENT_QUOTES);
79
                } else {
80
                    $path .= '>' . htmlspecialchars(stripslashes(htmlspecialchars_decode($elem->title, ENT_QUOTES)), ENT_QUOTES);
81
                }
82
            }
83
84
            array_push(
85
                $ret,
86
                [
87
                    'id' => (int) $row['id'],
88
                    'label' => $row['label'],
89
                    'description' => $row['description'],
90
                    'pwd' => $pwd,
91
                    'url' => $row['url'],
92
                    'login' => $row['login'],
93
                    'email' => $row['email'],
94
                    'viewed_no' => (int) $row['viewed_no'],
95
                    'fa_icon' => $row['fa_icon'],
96
                    'inactif' => (int) $row['inactif'],
97
                    'perso' => (int) $row['perso'],
98
                    'id_tree' => (int) $row['id_tree'],
99
                    'folder_label' => $row['folder_label'],
100
                    'path' => empty($path) === true ? '' : $path,
101
                ]
102
            );
103
        }
104
105
        return $ret;
106
    }
107
    //end getItems() 
108
}