Passed
Push — teampass_3.0 ( 870788...766a47 )
by Nils
02:47
created

deleteUserObjetsKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 2
dl 0
loc 37
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author        Nils Laumaillé <[email protected]>
4
 *
5
 * @version       2.1.27
6
 *
7
 * @copyright     2009-2018 Nils Laumaillé
8
 * @license       GNU GPL-3.0
9
 *
10
 * @see          https://www.teampass.net
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 */
16
require_once 'SecureHandler.php';
17
session_name('teampass_session');
18
session_start();
19
if (!isset($_SESSION['CPM']) || $_SESSION['CPM'] === false || !isset($_SESSION['key']) || empty($_SESSION['key'])) {
20
    die('Hacking attempt...');
21
}
22
23
// Load config
24
if (file_exists('../includes/config/tp.config.php')) {
25
    include_once '../includes/config/tp.config.php';
26
} elseif (file_exists('./includes/config/tp.config.php')) {
27
    include_once './includes/config/tp.config.php';
28
} else {
29
    throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
30
}
31
32
// Do checks
33
require_once $SETTINGS['cpassman_dir'].'/includes/config/include.php';
34
require_once $SETTINGS['cpassman_dir'].'/sources/checks.php';
35
if (checkUser($_SESSION['user_id'], $_SESSION['key'], 'items', $SETTINGS) === false) {
36
    // Not allowed page
37
    $_SESSION['error']['code'] = ERR_NOT_ALLOWED;
38
    include $SETTINGS['cpassman_dir'].'/error.php';
39
    exit();
40
}
41
42
/*
43
 * Define Timezone
44
**/
45
if (isset($SETTINGS['timezone']) === true) {
46
    date_default_timezone_set($SETTINGS['timezone']);
47
} else {
48
    date_default_timezone_set('UTC');
49
}
50
51
require_once $SETTINGS['cpassman_dir'].'/includes/language/'.$_SESSION['user_language'].'.php';
52
require_once $SETTINGS['cpassman_dir'].'/includes/config/settings.php';
53
header('Content-type: text/html; charset=utf-8');
54
header('Cache-Control: no-cache, must-revalidate');
55
require_once 'main.functions.php';
56
57
// Connect to mysql server
58
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php';
59
$link = mysqli_connect(DB_HOST, DB_USER, defuseReturnDecrypted(DB_PASSWD, $SETTINGS), DB_NAME, DB_PORT);
60
$link->set_charset(DB_ENCODING);
61
62
63
64
// Protect POST
65
$post_type = filter_input(INPUT_POST, 'type', FILTER_SANITIZE_STRING);
66
$post_data = filter_input(INPUT_POST, 'data', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
67
68
69
if (null !== $post_type) {
70
    switch ($post_type) {
71
        /*
72
        * CASE
73
        * creating a new user's public/private keys
74
        */
75
        case 'user_change_pair_keys':
76
            // Decrypt and retreive data in JSON format
77
            $dataReceived = prepareExchangedData(
78
                $post_data,
79
                'decode'
80
            );
81
            $post_user_id = filter_var($dataReceived['user_id'], FILTER_SANITIZE_NUMBER_INT);
82
            $post_user_pwd = filter_var($dataReceived['user_pwd'], FILTER_SANITIZE_STRING);
83
84
            // Get user info
85
            $userInfo = DB::queryFirstRow(
86
                "SELECT id, public_key, private_key
87
                FROM ".prefixTable('users')."
88
                WHERE id = %i",
89
                $post_user_id
90
            );
91
            
92
            // Generate keys
93
            $userKeys = generateUserKeys($post_user_pwd);
94
95
            // Store
96
            DB::update(
97
                prefixTable('users'),
98
                array(
99
                    'public_key' => $userKeys['public_key'],
100
                    'private_key' => $userKeys['private_key'],
101
                ),
102
                'id = %i',
103
                $post_user_id
104
            );
105
        break;
106
    }
107
}
108
109
110
111
/**
112
 * Undocumented function
113
 *
114
 * @param [type] $user_id
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
115
 * @param [type] $private_key
116
 * @param [type] $public_key
117
 * @param [type] $SETTINGS
118
 * @return void
119
 */
120
function changeUserObjectKeyForItems($user_id, $private_key, $public_key, $SETTINGS)
0 ignored issues
show
Unused Code introduced by
The parameter $public_key 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

120
function changeUserObjectKeyForItems($user_id, $private_key, /** @scrutinizer ignore-unused */ $public_key, $SETTINGS)

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 $private_key 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

120
function changeUserObjectKeyForItems($user_id, /** @scrutinizer ignore-unused */ $private_key, $public_key, $SETTINGS)

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 $SETTINGS 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

120
function changeUserObjectKeyForItems($user_id, $private_key, $public_key, /** @scrutinizer ignore-unused */ $SETTINGS)

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...
121
{
122
    // Its goal is to adapt all user Items object key
123
124
    // Remove all existing object keys
125
    DB::delete(
126
        prefixTable('sharekeys_items'),
127
        'user_id = %i',
128
        $user_id
129
    );
130
131
    // Create objectkey for each item
132
    $rows = DB::query(
133
        'SELECT id, pw
134
        FROM '.prefixTable('items').'
135
        WHERE perso = 0',
136
        $post_item_id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $post_item_id seems to be never defined.
Loading history...
137
    );
138
    foreach ($rows as $record) {
139
140
    }
141
}
142
143
/**
144
 * Delete all objects keys for one user
145
 *
146
 * @param string $user_id  User id
147
 * @param string $SETTINGS Teampass settings
148
 *
149
 * @return void
150
 */
151
function deleteUserObjetsKeys($user_id, $SETTINGS)
0 ignored issues
show
Unused Code introduced by
The parameter $SETTINGS 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

151
function deleteUserObjetsKeys($user_id, /** @scrutinizer ignore-unused */ $SETTINGS)

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...
152
{
153
    // Its goal is to adapt all user Items object key
154
155
    // Remove all existing object keys
156
    DB::delete(
157
        prefixTable('sharekeys_items'),
158
        'user_id = %i',
159
        $user_id
160
    );
161
162
    // Remove all existing object keys
163
    DB::delete(
164
        prefixTable('sharekeys_logs'),
165
        'user_id = %i',
166
        $user_id
167
    );
168
169
    // Remove all existing object keys
170
    DB::delete(
171
        prefixTable('sharekeys_fields'),
172
        'user_id = %i',
173
        $user_id
174
    );
175
176
    // Remove all existing object keys
177
    DB::delete(
178
        prefixTable('sharekeys_suggestions'),
179
        'user_id = %i',
180
        $user_id
181
    );
182
183
    // Remove all existing object keys
184
    DB::delete(
185
        prefixTable('sharekeys_filess'),
186
        'user_id = %i',
187
        $user_id
188
    );
189
}
190