Passed
Push — teampass_3.0 ( b97e88...b1369b )
by Nils
05:44
created

deleteUserObjetsKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 38
rs 9.584
1
<?php
2
3
/**
4
 * Teampass - a collaborative passwords manager.
5
 * ---
6
 * This library is distributed in the hope that it will be useful,
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
 * ---
10
 * @project   Teampass
11
 * @file      aes.functions.php
12
 * ---
13
 * @author    Nils Laumaillé ([email protected])
14
 * @copyright 2009-2019 Teampass.net
15
 * @license   https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
16
 * ---
17
 * @see       https://www.teampass.net
18
 */
19
20
21
require_once 'SecureHandler.php';
22
session_name('teampass_session');
23
session_start();
24
if (!isset($_SESSION['CPM']) || $_SESSION['CPM'] === false || !isset($_SESSION['key']) || empty($_SESSION['key'])) {
25
    die('Hacking attempt...');
26
}
27
28
// Load config
29
if (file_exists('../includes/config/tp.config.php')) {
30
    include_once '../includes/config/tp.config.php';
31
} elseif (file_exists('./includes/config/tp.config.php')) {
32
    include_once './includes/config/tp.config.php';
33
} else {
34
    throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
35
}
36
37
// Do checks
38
require_once $SETTINGS['cpassman_dir'] . '/includes/config/include.php';
39
require_once $SETTINGS['cpassman_dir'] . '/sources/checks.php';
40
if (checkUser($_SESSION['user_id'], $_SESSION['key'], 'items', $SETTINGS) === false) {
41
    // Not allowed page
42
    $_SESSION['error']['code'] = ERR_NOT_ALLOWED;
43
    include $SETTINGS['cpassman_dir'] . '/error.php';
44
    exit();
45
}
46
47
/*
48
 * Define Timezone
49
**/
50
if (isset($SETTINGS['timezone']) === true) {
51
    date_default_timezone_set($SETTINGS['timezone']);
52
} else {
53
    date_default_timezone_set('UTC');
54
}
55
56
require_once $SETTINGS['cpassman_dir'] . '/includes/language/' . $_SESSION['user_language'] . '.php';
57
require_once $SETTINGS['cpassman_dir'] . '/includes/config/settings.php';
58
header('Content-type: text/html; charset=utf-8');
59
header('Cache-Control: no-cache, must-revalidate');
60
require_once 'main.functions.php';
61
62
// Connect to mysql server
63
require_once $SETTINGS['cpassman_dir'] . '/includes/libraries/Database/Meekrodb/db.class.php';
64
$link = mysqli_connect(DB_HOST, DB_USER, defuseReturnDecrypted(DB_PASSWD, $SETTINGS), DB_NAME, DB_PORT);
0 ignored issues
show
Bug introduced by
The constant DB_USER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DB_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DB_PORT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DB_PASSWD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DB_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
//$link->set_charset(DB_ENCODING);
66
67
// Protect POST
68
$post_type = filter_input(INPUT_POST, 'type', FILTER_SANITIZE_STRING);
69
$post_data = filter_input(INPUT_POST, 'data', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
70
71
if (null !== $post_type) {
72
    switch ($post_type) {
73
            /*
74
        * CASE
75
        * creating a new user's public/private keys
76
        */
77
        case 'user_change_pair_keys':
78
            // Decrypt and retreive data in JSON format
79
            $dataReceived = prepareExchangedData(
80
                $post_data,
81
                'decode'
82
            );
83
            $post_user_id = filter_var($dataReceived['user_id'], FILTER_SANITIZE_NUMBER_INT);
84
            $post_user_pwd = filter_var($dataReceived['user_pwd'], FILTER_SANITIZE_STRING);
85
86
            // Get user info
87
            $userInfo = DB::queryFirstRow(
88
                'SELECT id, public_key, private_key
89
                FROM ' . prefixTable('users') . '
90
                WHERE id = %i',
91
                $post_user_id
92
            );
93
94
            // Generate keys
95
            $userKeys = generateUserKeys($post_user_pwd);
96
97
            // Store
98
            DB::update(
99
                prefixTable('users'),
100
                array(
101
                    'public_key' => $userKeys['public_key'],
102
                    'private_key' => $userKeys['private_key'],
103
                ),
104
                'id = %i',
105
                $post_user_id
106
            );
107
            break;
108
    }
109
}
110
111
112
/**
113
 * Delete all objects keys for one user.
114
 *
115
 * @param string $user_id  User id
116
 * @param string $SETTINGS Teampass settings
117
 *
118
 * @return void
119
 */
120
function deleteUserObjetsKeys($user_id, $SETTINGS)
121
{
122
    // Its goal is to adapt all user Items object key
123
    include_once $SETTINGS['cpassman_dir'] . '/sources/main.functions.php';
124
125
    // Remove all existing object keys
126
    DB::delete(
127
        prefixTable('sharekeys_items'),
128
        'user_id = %i',
129
        $user_id
130
    );
131
132
    // Remove all existing object keys
133
    DB::delete(
134
        prefixTable('sharekeys_logs'),
135
        'user_id = %i',
136
        $user_id
137
    );
138
139
    // Remove all existing object keys
140
    DB::delete(
141
        prefixTable('sharekeys_fields'),
142
        'user_id = %i',
143
        $user_id
144
    );
145
146
    // Remove all existing object keys
147
    DB::delete(
148
        prefixTable('sharekeys_suggestions'),
149
        'user_id = %i',
150
        $user_id
151
    );
152
153
    // Remove all existing object keys
154
    DB::delete(
155
        prefixTable('sharekeys_files'),
156
        'user_id = %i',
157
        $user_id
158
    );
159
}
160