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

ItemModel   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
dl 0
loc 102
rs 10
c 1
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addItem() 0 5 1
B getItems() 0 75 9
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
use TeampassClasses\NestedTree\NestedTree;
26
27
require_once API_ROOT_PATH . "/Model/Database.php";
28
29
class ItemModel extends Database
30
{
31
32
33
    /**
34
     * Get the list of items to return
35
     *
36
     * @param string $sqlExtra
37
     * @param integer $limit
38
     * @param string $userPrivateKey
39
     * @param integer $userId
40
     * 
41
     * @return array
42
     */
43
    public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, int $userId): array
44
    {
45
        $rows = $this->select(
46
            "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
47
            FROM ".prefixTable('items')." as i
48
            LEFT JOIN ".prefixTable('nested_tree')." as t ON (t.id = i.id_tree) ".
49
            $sqlExtra . 
50
            " ORDER BY i.id ASC" .
51
            //($limit > 0 ? " LIMIT ?". ["i", $limit] : '')
52
            ($limit > 0 ? " LIMIT ". $limit : '')
53
        );
54
        $ret = [];
55
        foreach ($rows as $row) {
56
            $userKey = $this->select(
57
                'SELECT share_key
58
                FROM ' . prefixTable('sharekeys_items') . '
59
                WHERE user_id = '.$userId.' AND object_id = '.$row['id']                
60
            );
61
            if (count($userKey) === 0 || empty($row['pw']) === true) {
62
                // No share key found
63
                // Exit this item
64
                continue;
65
            }
66
67
            // Get password
68
            try {
69
                $pwd = base64_decode(
70
                    (string) doDataDecryption(
71
                        $row['pw'],
72
                        decryptUserObjectKey(
73
                            $userKey[0]['share_key'],
74
                            $userPrivateKey
75
                        )
76
                    )
77
                );
78
            } catch (Exception $e) {
79
                // Password is not encrypted
80
                echo "ERROR";
81
            }
82
            
83
84
            // get path to item
85
            $tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title');
86
            $arbo = $tree->getPath($row['id_tree'], false);
87
            $path = '';
88
            foreach ($arbo as $elem) {
89
                if (empty($path) === true) {
90
                    $path = htmlspecialchars(stripslashes(htmlspecialchars_decode($elem->title, ENT_QUOTES)), ENT_QUOTES);
91
                } else {
92
                    $path .= '/' . htmlspecialchars(stripslashes(htmlspecialchars_decode($elem->title, ENT_QUOTES)), ENT_QUOTES);
93
                }
94
            }
95
96
            array_push(
97
                $ret,
98
                [
99
                    'id' => (int) $row['id'],
100
                    'label' => $row['label'],
101
                    'description' => $row['description'],
102
                    'pwd' => $pwd,
103
                    'url' => $row['url'],
104
                    'login' => $row['login'],
105
                    'email' => $row['email'],
106
                    'viewed_no' => (int) $row['viewed_no'],
107
                    'fa_icon' => $row['fa_icon'],
108
                    'inactif' => (int) $row['inactif'],
109
                    'perso' => (int) $row['perso'],
110
                    'id_tree' => (int) $row['id_tree'],
111
                    'folder_label' => $row['folder_label'],
112
                    'path' => empty($path) === true ? '' : $path,
113
                ]
114
            );
115
        }
116
117
        return $ret;
118
    }
119
    //end getItems() 
120
121
    /**
122
     * Add item
123
     *
124
     * @return bool
125
     */
126
    public function addItem(string $idTree, string $userName, string $hostname, string $password) : bool
0 ignored issues
show
Unused Code introduced by
The parameter $idTree 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

126
    public function addItem(/** @scrutinizer ignore-unused */ string $idTree, string $userName, string $hostname, string $password) : bool

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...
Unused Code introduced by
The parameter $hostname 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

126
    public function addItem(string $idTree, string $userName, /** @scrutinizer ignore-unused */ string $hostname, string $password) : bool

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...
Unused Code introduced by
The parameter $userName 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

126
    public function addItem(string $idTree, /** @scrutinizer ignore-unused */ string $userName, string $hostname, string $password) : bool

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...
Unused Code introduced by
The parameter $password 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

126
    public function addItem(string $idTree, string $userName, string $hostname, /** @scrutinizer ignore-unused */ string $password) : bool

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...
127
    {
128
        // TODO ecrire
129
        
130
        return true;
131
    }
132
}