Passed
Push — development ( 7d1ffd...2868bf )
by Nils
03:15
created

generateRandomKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 18.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @package       upgrade.ajax.php
4
 * @author        Nils Laumaillé <[email protected]>
5
 * @version       2.1.27
6
 * @copyright     2009-2018 Nils Laumaillé
7
 * @license       GNU GPL-3.0
8
 * @link          https://www.teampass.net
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/*
16
** Upgrade script for release 2.1.27
17
*/
18
require_once('../sources/SecureHandler.php');
19
session_start();
20
error_reporting(E_ERROR | E_PARSE);
21
$_SESSION['db_encoding'] = "utf8";
22
$_SESSION['CPM'] = 1;
23
24
25
//include librairies
26
require_once '../includes/language/english.php';
27
require_once '../includes/config/include.php';
28
require_once '../includes/config/settings.php';
29
require_once '../sources/main.functions.php';
30
require_once '../includes/libraries/Tree/NestedTree/NestedTree.php';
31
32
$_SESSION['settings']['loaded'] = "";
33
//define pbkdf2 iteration count
34
@define('ITCOUNT', '2072');
35
$return_error = "";
36
$res = "";
37
38
39
//Build tree
40
$tree = new Tree\NestedTree\NestedTree(
41
    $pre.'nested_tree',
42
    'id',
43
    'parent_id',
44
    'title'
45
);
46
47
48
// Prepare POST variables
49
$post_no_maintenance_mode = filter_input(INPUT_POST, 'no_maintenance_mode', FILTER_SANITIZE_NUMBER_INT);
50
$post_index = filter_input(INPUT_POST, 'index', FILTER_SANITIZE_NUMBER_INT);
51
$post_multiple = filter_input(INPUT_POST, 'multiple', FILTER_SANITIZE_STRING);
52
53
// DataBase
54
// Test DB connexion
55
$pass = defuse_return_decrypted($pass);
56
if (mysqli_connect(
57
    $server,
58
    $user,
59
    $pass,
60
    $database,
61
    $port
62
)
63
) {
64
    $db_link = mysqli_connect(
65
        $server,
66
        $user,
67
        $pass,
68
        $database,
69
        $port
70
    );
71
} else {
72
    $res = "Impossible to get connected to server. Error is: ".addslashes(mysqli_connect_error());
73
    echo '[{"finish":"1", "msg":"", "error":"Impossible to get connected to server. Error is: '.addslashes(mysqli_connect_error()).'!"}]';
74
    mysqli_close($db_link);
75
    exit();
76
}
77
78
// Load libraries
79
require_once '../includes/libraries/protect/SuperGlobal/SuperGlobal.php';
80
$superGlobal = new protect\SuperGlobal\SuperGlobal();
81
82
// Set Session
83
$superGlobal->put("db_encoding", "utf8", "SESSION");
84
$_SESSION['settings']['loaded'] = "";
85
$superGlobal->put("fullurl", $post_fullurl, "SESSION");
86
$superGlobal->put("abspath", $abspath, "SESSION");
87
88
// Get Sessions
89
$session_tp_defuse_installed = $superGlobal->get("tp_defuse_installed", "SESSION");
90
91
/**
92
 * Generates a random key
93
 *
94
 * @return void
95
 */
96
function generateRandomKey()
97
{
98
    // load passwordLib library
99
    $path = '../includes/libraries/PasswordGenerator/Generator/';
100
    include_once $path.'ComputerPasswordGenerator.php';
101
102
    $generator = new PasswordGenerator\Generator\ComputerPasswordGenerator();
103
104
    $generator->setLength(40);
105
    $generator->setSymbols(false);
106
    $generator->setLowercase(true);
107
    $generator->setUppercase(true);
108
    $generator->setNumbers(true);
109
110
	$key = $generator->generatePasswords();
111
112
    return $key[0];
113
}
114
115
/**
116
 * Function permits to get the value from a line
117
 * @param  string $val [description]
118
 * @return string      [description]
119
 */
120
function getSettingValue($val)
121
{
122
    $val = trim(strstr($val, "="));
123
    return trim(str_replace('"', '', substr($val, 1, strpos($val, ";") - 1)));
124
}
125
126
/**
127
 * Function permits to check if a column exists, and if not to add it
128
 * @param string $dbname     [description]
129
 * @param string $column     [description]
130
 * @param string $columnAttr [description]
131
 */
132
function addColumnIfNotExist($dbname, $column, $columnAttr = "VARCHAR(255) NULL")
133
{
134
    global $db_link;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
135
    $exists = false;
136
    $columns = mysqli_query($db_link, "show columns from $dbname");
137
    while ($col = mysqli_fetch_assoc($columns)) {
0 ignored issues
show
Bug introduced by
It seems like $columns can also be of type boolean; however, parameter $result of mysqli_fetch_assoc() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

137
    while ($col = mysqli_fetch_assoc(/** @scrutinizer ignore-type */ $columns)) {
Loading history...
138
        if ($col['Field'] == $column) {
139
            $exists = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $exists is dead and can be removed.
Loading history...
140
            return true;
141
        }
142
    }
143
    if (!$exists) {
0 ignored issues
show
introduced by
The condition $exists is always false.
Loading history...
144
        return mysqli_query($db_link, "ALTER TABLE `$dbname` ADD `$column`  $columnAttr");
145
    }
146
147
    return false;
148
}
149
150
/**
151
 * [cleanFields description]
152
 * @param  [type] $txt [description]
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...
153
 * @return [type]      [description]
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...
154
 */
155
function cleanFields($txt)
156
{
157
    $tmp = str_replace(",", ";", trim($txt));
158
    if (empty($tmp)) {
159
        return $tmp;
160
    }
161
    if ($tmp === ";") {
162
        return "";
163
    }
164
    if (strpos($tmp, ';') === 0) {
165
        $tmp = substr($tmp, 1);
166
    }
167
    if (substr($tmp, -1) !== ";") {
168
        $tmp = $tmp.";";
169
    }
170
    return $tmp;
171
}
172
173
/*
174
** Checks if the column exists in the table
175
*/
176
function columnExists($tablename, $column)
177
{
178
    global $db_link;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
179
    $checkcolumn = mysqli_query($db_link, "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='{$tablename}' AND COLUMN_NAME = '{$column}';");
180
    if (mysqli_num_rows($checkcolumn) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $checkcolumn can also be of type boolean; however, parameter $result of mysqli_num_rows() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

180
    if (mysqli_num_rows(/** @scrutinizer ignore-type */ $checkcolumn) > 0) {
Loading history...
181
        return true;
182
    } else {
183
        return false;
184
    }
185
}
186
187
// 2.1.27 introduce new encryption protocol with DEFUSE library.
188
// Now evaluate if current instance has already this version
189
$tmp = mysqli_fetch_row(mysqli_query($db_link, "SELECT valeur FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'teampass_version'"));
0 ignored issues
show
Bug introduced by
It seems like mysqli_query($db_link, '... = 'teampass_version'') can also be of type boolean; however, parameter $result of mysqli_fetch_row() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

189
$tmp = mysqli_fetch_row(/** @scrutinizer ignore-type */ mysqli_query($db_link, "SELECT valeur FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'teampass_version'"));
Loading history...
190
if (count($tmp[0]) === 0 || empty($tmp[0])) {
191
    mysqli_query(
192
        $db_link,
193
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'teampass_version', '".$SETTINGS_EXT['version']."')"
194
    );
195
} else {
196
    mysqli_query(
197
        $db_link,
198
        "UPDATE `".$pre."misc`
199
        SET `valeur` = '".$SETTINGS_EXT['version']."'
200
        WHERE intitule = 'teampass_version' AND type = 'admin'"
201
    );
202
}
203
204
// add new admin setting "migration_to_2127"
205
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'migration_to_2127'"));
0 ignored issues
show
Bug introduced by
It seems like mysqli_query($db_link, '...= 'migration_to_2127'') can also be of type boolean; however, parameter $result of mysqli_num_rows() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

205
$tmp = mysqli_num_rows(/** @scrutinizer ignore-type */ mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'migration_to_2127'"));
Loading history...
206
if (intval($tmp) === 0) {
207
    mysqli_query(
208
        $db_link,
209
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'migration_to_2127', '0')"
210
    );
211
}
212
213
214
// check if library defuse already on-going here
215
// if yes, then don't execute re-encryption
216
if (isset($session_tp_defuse_installed) === false) {
217
    $superGlobal->put("tp_defuse_installed", false, "SESSION");
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $value of protect\SuperGlobal\SuperGlobal::put(). ( Ignorable by Annotation )

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

217
    $superGlobal->put("tp_defuse_installed", /** @scrutinizer ignore-type */ false, "SESSION");
Loading history...
218
    if (columnExists($pre."items", "encryption_type") === true) {
219
        $superGlobal->put("tp_defuse_installed", true, "SESSION");
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of protect\SuperGlobal\SuperGlobal::put(). ( Ignorable by Annotation )

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

219
        $superGlobal->put("tp_defuse_installed", /** @scrutinizer ignore-type */ true, "SESSION");
Loading history...
220
    }
221
}
222
223
// alter table Items
224
mysqli_query($db_link, "ALTER TABLE `".$pre."items` MODIFY pw_len INT(5) NOT NULL DEFAULT '0'");
225
226
// alter table MISC - rename ID is exists
227
$res = addColumnIfNotExist(
228
    $pre."misc",
229
    "increment_id",
230
    "INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
231
);
232
if ($res === true) {
233
    // Change name of field
234
    mysqli_query($db_link, "ALTER TABLE `".$pre."misc` CHANGE `id` `increment_id` INT(12) NOT NULL AUTO_INCREMENT");
235
} elseif ($res === false) {
236
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding increment_id to table misc! '.mysqli_error($db_link).'!"}]';
237
    mysqli_close($db_link);
238
    exit();
239
}
240
241
242
// alter table misc to add an index
243
mysqli_query(
244
    $db_link,
245
    "ALTER TABLE `".$pre."log_items` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
246
);
247
// create index
248
mysqli_query(
249
    $db_link,
250
    "CREATE INDEX teampass_log_items_id_item_IDX ON ".$pre."log_items (id_item, date);"
251
);
252
253
// add field agses-usercardid to Users table
254
$res = addColumnIfNotExist(
255
    $pre."users",
256
    "agses-usercardid",
257
    "VARCHAR(12) NOT NULL DEFAULT '0'"
258
);
259
if ($res === false) {
260
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field agses-usercardid to table Users! '.mysqli_error($db_link).'!"}]';
261
    mysqli_close($db_link);
262
    exit();
263
}
264
265
266
// add field encrypted_data to Categories table
267
$res = addColumnIfNotExist(
268
    $pre."categories",
269
    "encrypted_data",
270
    "TINYINT(1) NOT NULL DEFAULT '1'"
271
);
272
if ($res === false) {
273
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encrypted_data to table categories! '.mysqli_error($db_link).'!"}]';
274
    mysqli_close($db_link);
275
    exit();
276
}
277
278
279
// add field is_mandatory to Categories table
280
$res = addColumnIfNotExist(
281
    $pre."categories",
282
    "is_mandatory",
283
    "BOOLEAN NOT NULL DEFAULT FALSE"
284
);
285
if ($res === false) {
286
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field is_mandatory to table categories! '.mysqli_error($db_link).'!"}]';
287
    mysqli_close($db_link);
288
    exit();
289
}
290
291
292
// alter table USERS - user_language
293
mysqli_query($db_link, "ALTER TABLE `".$pre."users` MODIFY user_language VARCHAR(50) NOT NULL DEFAULT '0'");
294
295
// alter table USERS - just ensure correct naming of IsAdministratedByRole
296
mysqli_query($db_link, "ALTER TABLE `".$pre."users` CHANGE IsAdministratedByRole isAdministratedByRole tinyint(5) NOT NULL DEFAULT '0'");
297
298
// alter table OTV
299
mysqli_query($db_link, "ALTER TABLE `".$pre."otv` CHANGE originator originator int(12) NOT NULL DEFAULT '0'");
300
301
// do clean of users table
302
$fieldsToUpdate = ['groupes_visibles', 'fonction_id', 'groupes_interdits'];
303
$result = mysqli_query($db_link, "SELECT id, groupes_visibles, fonction_id, groupes_interdits FROM `".$pre."users`");
304
while ($row = mysqli_fetch_assoc($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of mysqli_fetch_assoc() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

304
while ($row = mysqli_fetch_assoc(/** @scrutinizer ignore-type */ $result)) {
Loading history...
305
    // check if field contains , instead of ;
306
    foreach ($fieldsToUpdate as $field) {
307
        $tmp = cleanFields($row[$field]);
308
        if ($tmp !== $row[$field]) {
309
            mysqli_query(
310
                $db_link,
311
                "UPDATE `".$pre."users`
312
                SET `".$field."` = '".$tmp."'
313
                WHERE id = '".$row['id']."'"
314
            );
315
        }
316
    }
317
}
318
mysqli_free_result($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of mysqli_free_result() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

318
mysqli_free_result(/** @scrutinizer ignore-type */ $result);
Loading history...
319
320
321
// alter table KB_ITEMS
322
mysqli_query($db_link, "ALTER TABLE `".$pre."kb_items` CHANGE `kb_id` `kb_id` INT(12) NOT NULL");
323
mysqli_query($db_link, "ALTER TABLE `".$pre."kb_items` CHANGE `item_id` `item_id` INT(12) NOT NULL");
324
325
326
// Alter table EXPORT - adapt field Label
327
mysqli_query($db_link, "ALTER TABLE `".$pre."export` CHANGE `label` `label` VARCHAR(500) NOT NULL");
328
329
// add field encrypted_data to CATEGORIES table
330
$res = addColumnIfNotExist(
331
    $pre."categories",
332
    "encrypted_data",
333
    "TINYINT(1) NOT NULL DEFAULT '1'"
334
);
335
if ($res === false) {
336
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encrypted_data to table CATEGORIES! '.mysqli_error($db_link).'!"}]';
337
    mysqli_close($db_link);
338
    exit();
339
}
340
341
mysqli_query(
342
    $db_link,
343
    "UPDATE `".$pre."misc`
344
    SET `valeur` = 'maintenance_mode'
345
    WHERE type = 'admin' AND intitule = '".$post_no_maintenance_mode."'"
346
);
347
348
349
// add field encryption_type to ITEMS table
350
$res = addColumnIfNotExist(
351
    $pre."items",
352
    "encryption_type",
353
    "VARCHAR(20) NOT NULL DEFAULT 'not_set'"
354
);
355
if ($res === false) {
356
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encryption_type to table ITEMS! '.mysqli_error($db_link).'!"}]';
357
    mysqli_close($db_link);
358
    exit();
359
}
360
361
362
// add field encryption_type to categories_items table
363
$res = addColumnIfNotExist(
364
    $pre."categories_items",
365
    "encryption_type",
366
    "VARCHAR(20) NOT NULL DEFAULT 'not_set'"
367
);
368
if ($res === false) {
369
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encryption_type to table categories_items! '.mysqli_error($db_link).'!"}]';
370
    mysqli_close($db_link);
371
    exit();
372
}
373
374
375
// add field encryption_type to LOG_ITEMS table
376
$res = addColumnIfNotExist(
377
    $pre."log_items",
378
    "encryption_type",
379
    "VARCHAR(20) NOT NULL DEFAULT 'not_set'"
380
);
381
if ($res === false) {
382
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encryption_type to table LOG_ITEMS! '.mysqli_error($db_link).'!"}]';
383
    mysqli_close($db_link);
384
    exit();
385
}
386
387
388
// add field URL to CACHE table
389
$res = addColumnIfNotExist(
390
    $pre."cache",
391
    "encryption_type",
392
    "VARCHAR(500) NOT NULL DEFAULT '0'"
393
);
394
if ($res === false) {
395
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field URL to table CACHE! '.mysqli_error($db_link).'!"}]';
396
    mysqli_close($db_link);
397
    exit();
398
}
399
400
401
// add field timestamp to CACHE table
402
$res = addColumnIfNotExist(
403
    $pre."cache",
404
    "timestamp",
405
    "VARCHAR(50) DEFAULT NULL DEFAULT '0'"
406
);
407
if ($res === false) {
408
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field url to table CACHE! '.mysqli_error($db_link).'!"}]';
409
    mysqli_close($db_link);
410
    exit();
411
}
412
413
414
// add field url to CACHE table
415
$res = addColumnIfNotExist(
416
    $pre."cache",
417
    "url",
418
    "VARCHAR(500) DEFAULT NULL"
419
);
420
if ($res === false) {
421
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field timestamp to table CACHE! '.mysqli_error($db_link).'!"}]';
422
    mysqli_close($db_link);
423
    exit();
424
}
425
426
427
// alter table CACHE to add an index
428
mysqli_query(
429
    $db_link,
430
    "ALTER TABLE `".$pre."cache` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
431
);
432
433
434
// alter table EXPORT to add an index
435
mysqli_query(
436
    $db_link,
437
    "ALTER TABLE `".$pre."export` ADD INDEX `id_idx` (`id`)"
438
);
439
mysqli_query(
440
    $db_link,
441
    "ALTER TABLE `".$pre."export` DROP INDEX `id_idx`"
442
);
443
444
445
// alter table EXPORT to add an index
446
mysqli_query(
447
    $db_link,
448
    "ALTER TABLE `".$pre."export` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
449
);
450
451
// alter table ITEMS_EDITION to add an index
452
mysqli_query(
453
    $db_link,
454
    "ALTER TABLE `".$pre."items_edition` ADD INDEX `item_id_idx` (`item_id`)"
455
);
456
mysqli_query(
457
    $db_link,
458
    "ALTER TABLE `".$pre."items_edition` DROP INDEX `item_id_idx`"
459
);
460
461
// alter table items_edition to add an index
462
mysqli_query(
463
    $db_link,
464
    "ALTER TABLE `".$pre."items_edition` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
465
);
466
467
468
// alter table restriction_to_roles to add an index
469
mysqli_query(
470
    $db_link,
471
    "ALTER TABLE `".$pre."restriction_to_roles` ADD INDEX `role_id_idx` (`role_id`)"
472
);
473
mysqli_query(
474
    $db_link,
475
    "ALTER TABLE `".$pre."restriction_to_roles` DROP INDEX `role_id_idx`"
476
);
477
478
// alter table restriction_to_roles to add an index
479
mysqli_query(
480
    $db_link,
481
    "ALTER TABLE `".$pre."restriction_to_roles` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
482
);
483
484
485
// alter table NESTEED_TREE to add an index
486
mysqli_query(
487
    $db_link,
488
    "ALTER TABLE `".$pre."nested_tree` ADD KEY `nested_tree_parent_id` (`parent_id`)"
489
);
490
mysqli_query(
491
    $db_link,
492
    "ALTER TABLE `".$pre."nested_tree` ADD KEY `nested_tree_nleft` (`nleft`)"
493
);
494
mysqli_query(
495
    $db_link,
496
    "ALTER TABLE `".$pre."nested_tree` ADD KEY `nested_tree_nright` (`nright`)"
497
);
498
mysqli_query(
499
    $db_link,
500
    "ALTER TABLE `".$pre."nested_tree` ADD KEY `nested_tree_nlevel` (`nlevel`)"
501
);
502
mysqli_query(
503
    $db_link,
504
    "ALTER TABLE `".$pre."nested_tree` ADD KEY `personal_folder_idx` (`personal_folder`)"
505
);
506
mysqli_query(
507
    $db_link,
508
    "ALTER TABLE `".$pre."nested_tree` ADD KEY `id` (`id`)"
509
);
510
511
512
513
// alter table ROLES_VALUES to add an index
514
mysqli_query(
515
    $db_link,
516
    "ALTER TABLE `".$pre."roles_values` ADD KEY `role_id_idx` (`role_id`)"
517
);
518
519
// alter table ROLES_VALUES to add a primary key
520
mysqli_query(
521
    $db_link,
522
    "ALTER TABLE `".$pre."roles_values` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT PRIMARY KEY"
523
);
524
525
526
// alter table KB_ITEMS to add an index
527
mysqli_query(
528
    $db_link,
529
    "ALTER TABLE `".$pre."kb_items` ADD PRIMARY KEY (`kb_id`)"
530
);
531
mysqli_query(
532
    $db_link,
533
    "ALTER TABLE `".$pre."kb_items` DROP PRIMARY KEY"
534
);
535
536
// alter table kb_items to add an index
537
mysqli_query(
538
    $db_link,
539
    "ALTER TABLE `".$pre."kb_items` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
540
);
541
542
543
// alter table EMAILS to add an index
544
mysqli_query(
545
    $db_link,
546
    "ALTER TABLE `".$pre."emails` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
547
);
548
549
550
// alter table AUTOMATIC_DEL to add an index
551
mysqli_query(
552
    $db_link,
553
    "ALTER TABLE `".$pre."automatic_del` ADD PRIMARY KEY (`item_id`)"
554
);
555
556
557
// alter table CATEGORY_FOLDERS to add an index
558
mysqli_query(
559
    $db_link,
560
    "ALTER TABLE `".$pre."categories_folders` ADD PRIMARY KEY (`id_category`)"
561
);
562
mysqli_query(
563
    $db_link,
564
    "ALTER TABLE `".$pre."categories_folders` DROP PRIMARY KEY"
565
);
566
567
// alter table categories_folders to add an index
568
mysqli_query(
569
    $db_link,
570
    "ALTER TABLE `".$pre."categories_folders` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
571
);
572
573
574
//-- generate new DEFUSE key
575
if (isset($session_tp_defuse_installed) === false || $session_tp_defuse_installed === false) {
576
    $filename = "../includes/config/settings.php";
577
    $settingsFile = file($filename);
578
    foreach ($settingsFile as $key => $val) {
579
        if (substr_count($val, 'require_once "') > 0 && substr_count($val, 'sk.php') > 0) {
580
            $superGlobal->put("sk_file", substr($val, 14, strpos($val, '";') - 14), "SESSION");
581
            $session_sk_file = $superGlobal->get("sk_file", "SESSION");
582
        }
583
    }
584
585
    copy(
586
        SECUREPATH."/teampass-seckey.txt",
0 ignored issues
show
Bug introduced by
The constant SECUREPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
587
        SECUREPATH."/teampass-seckey.txt".'.'.date("Y_m_d", mktime(0, 0, 0, (int) date('m'), (int) date('d'), (int) date('y'))).".".time()
588
    );
589
    $superGlobal->put("tp_defuse_new_key", true, "SESSION");
590
    $new_salt = defuse_generate_key();
591
    file_put_contents(
592
        SECUREPATH."/teampass-seckey.txt",
593
        $new_salt
594
    );
595
    $superGlobal->put("new_salt", $new_salt, "SESSION");
596
597
    // update sk.php file
598
    copy(
599
        $session_sk_file,
600
        $session_sk_file.'.'.date("Y_m_d", mktime(0, 0, 0, (int) date('m'), (int) date('d'), (int) date('y'))).".".time()
601
    );
602
    $data = file($session_sk_file); // reads an array of lines
603
    function replace_a_line($data)
604
    {
605
        if (stristr($data, "@define('SALT'")) {
606
            return "";
607
        }
608
        return $data;
609
    }
610
    $data = array_map('replace_a_line', $data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

610
    $data = array_map('replace_a_line', /** @scrutinizer ignore-type */ $data);
Loading history...
611
    file_put_contents($session_sk_file, implode('', $data));
612
613
    //
614
    //
615
    //-- users need to perform re-encryption of their personal pwds
616
    $result = mysqli_query(
617
        $db_link,
618
        "SELECT valeur FROM `".$pre."misc` WHERE type='admin' AND intitule='encryption_type'"
619
    );
620
    $row = mysqli_fetch_assoc($result);
621
    if ($row['valeur'] !== "defuse") {
622
        $result = mysqli_query(
623
            $db_link,
624
            "SELECT id FROM `".$pre."users`"
625
        );
626
        while ($row_user = mysqli_fetch_assoc($result)) {
627
            $result_items = mysqli_query(
628
                $db_link,
629
                "SELECT i.id AS item_id
630
                FROM `".$pre."nested_tree` AS n
631
                INNER JOIN `".$pre."items` AS i ON (i.id_tree = n.id)
632
                WHERE n.title = ".$row_user['id']
633
            );
634
            if (mysqli_num_rows($result_items) > 0) {
635
                mysqli_query(
636
                    $db_link,
637
                    "UPDATE `".$pre."users`
638
                    SET `upgrade_needed` = '1'
639
                    WHERE id = ".$row_user['id']
640
                );
641
            } else {
642
                mysqli_query(
643
                    $db_link,
644
                    "UPDATE `".$pre."users`
645
                    SET `upgrade_needed` = '0'
646
                    WHERE id = ".$row_user['id']
647
                );
648
            }
649
        }
650
651
        mysqli_query(
652
            $db_link,
653
            "UPDATE `".$pre."misc`
654
            SET `valeur` = 'defuse'
655
            WHERE `type`='admin' AND `initule`='encryption_type'"
656
        );
657
    }
658
} else {
659
    $_SESSION['tp_defuse_new_key'] = false;
660
}
661
//--
662
663
664
// add field encrypted_psk to Users table
665
$res = addColumnIfNotExist(
666
    $pre."users",
667
    "encrypted_psk",
668
    "TEXT NOT NULL"
669
);
670
if ($res === false) {
671
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encrypted_psk to table Users! '.mysqli_error($db_link).'!"}]';
672
    mysqli_close($db_link);
673
    exit();
674
}
675
676
677
// add new admin setting "manager_move_item"
678
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'manager_move_item'"));
679
if (intval($tmp) === 0) {
680
    mysqli_query(
681
        $db_link,
682
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'manager_move_item', '0')"
683
    );
684
}
685
686
// add new admin setting "create_item_without_password"
687
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'create_item_without_password'"));
688
if (intval($tmp) === 0) {
689
    mysqli_query(
690
        $db_link,
691
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'create_item_without_password', '0')"
692
    );
693
}
694
695
// add new admin setting "send_statistics_items"
696
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'send_statistics_items'"));
697
if (intval($tmp) === 0) {
698
    mysqli_query(
699
        $db_link,
700
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'send_statistics_items', 'stat_country;stat_users;stat_items;stat_items_shared;stat_folders;stat_folders_shared;stat_admins;stat_managers;stat_ro;stat_mysqlversion;stat_phpversion;stat_teampassversion;stat_languages;stat_kb;stat_suggestion;stat_customfields;stat_api;stat_2fa;stat_agses;stat_duo;stat_ldap;stat_syslog;stat_stricthttps;stat_fav;stat_pf;')"
701
    );
702
}
703
704
// add new admin setting "send_stats_time"
705
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'send_stats_time'"));
706
if (intval($tmp) === 0) {
707
    mysqli_query(
708
        $db_link,
709
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'send_stats_time', '".(time() - 2592000)."')"
710
    );
711
}
712
713
// add new admin setting "agses_authentication_enabled"
714
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'agses_authentication_enabled'"));
715
if (intval($tmp) === 0) {
716
    mysqli_query(
717
        $db_link,
718
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'agses_authentication_enabled', '0')"
719
    );
720
}
721
722
// add new admin setting "timezone"
723
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'timezone'"));
724
if (intval($tmp) === 0) {
725
    mysqli_query(
726
        $db_link,
727
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'timezone', 'UTC')"
728
    );
729
}
730
731
// add new admin setting "personal_saltkey_security_level"
732
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'personal_saltkey_security_level'"));
733
if (intval($tmp) === 0) {
734
    mysqli_query(
735
        $db_link,
736
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'personal_saltkey_security_level', '0')"
737
    );
738
}
739
740
// add new admin setting "item_extra_fields"
741
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'item_extra_fields'"));
742
if (intval($tmp) === 0) {
743
    mysqli_query(
744
        $db_link,
745
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'item_extra_fields', '0')"
746
    );
747
}
748
749
// add new admin setting "ldap_new_user_is_administrated_by"
750
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'ldap_new_user_is_administrated_by'"));
751
if (intval($tmp) === 0) {
752
    mysqli_query(
753
        $db_link,
754
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'ldap_new_user_is_administrated_by', '0')"
755
    );
756
}
757
758
759
// add new admin setting "ldap_port"
760
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'ldap_port'"));
761
if (intval($tmp) === 0) {
762
    mysqli_query(
763
        $db_link,
764
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'ldap_port', '389')"
765
    );
766
}
767
768
// add new admin setting "offline_key_level"
769
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'offline_key_level'"));
770
if (intval($tmp) === 0) {
771
    mysqli_query(
772
        $db_link,
773
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'offline_key_level', '0')"
774
    );
775
}
776
777
// add new admin setting "enable_http_request_login"
778
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'enable_http_request_login'"));
779
if (intval($tmp) === 0) {
780
    mysqli_query(
781
        $db_link,
782
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'enable_http_request_login', '0')"
783
    );
784
}
785
786
787
// add new language "portuges_br"
788
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'portuguese_br'"));
789
if (intval($tmp) === 0) {
790
    mysqli_query(
791
        $db_link,
792
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('portuguese_br', 'Portuguese_br', 'pr-bt', 'pr-bt.png')"
793
    );
794
}
795
796
797
// add new language "Ukrainian"
798
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'ukrainian'"));
799
if (intval($tmp) === 0) {
800
    mysqli_query(
801
        $db_link,
802
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('ukrainian', 'Ukrainian', 'ua', 'ua.png')"
803
    );
804
}
805
806
807
// add new language "Romanian"
808
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'romanian'"));
809
if (intval($tmp) === 0) {
810
    mysqli_query(
811
        $db_link,
812
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('romanian', 'Romanian', 'ro', 'ro.png')"
813
    );
814
}
815
816
817
// add new language "Polish"
818
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'polish'"));
819
if (intval($tmp) === 0) {
820
    mysqli_query(
821
        $db_link,
822
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('polish', 'Polish', 'po', 'po.png')"
823
    );
824
}
825
826
827
// add new language "Hungarian"
828
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'hungarian'"));
829
if (intval($tmp) === 0) {
830
    mysqli_query(
831
        $db_link,
832
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('hungarian', 'Hungarian', 'hu', 'hu.png')"
833
    );
834
}
835
836
837
// add new language "Greek"
838
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'greek'"));
839
if (intval($tmp) === 0) {
840
    mysqli_query(
841
        $db_link,
842
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('greek', 'Greek', 'gr', 'gr.png')"
843
    );
844
}
845
846
847
// add new language "Bulgarian"
848
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'bulgarian'"));
849
if (intval($tmp) === 0) {
850
    mysqli_query(
851
        $db_link,
852
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('bulgarian', 'Bulgarian', 'bg', 'bg.png')"
853
    );
854
}
855
856
857
// alter table USERS to add a new field "ga_temporary_code"
858
mysqli_query(
859
    $db_link,
860
    "ALTER TABLE `".$pre."users` ADD `ga_temporary_code` VARCHAR(20) NOT NULL DEFAULT 'none' AFTER `ga`;"
861
);
862
863
864
// alter table USERS to add a new field "user_ip"
865
$res = addColumnIfNotExist(
866
    $pre."users",
867
    "user_ip",
868
    "VARCHAR(400) NOT NULL DEFAULT 'none'"
869
);
870
if ($res === true) {
871
    // Change name of field
872
    mysqli_query($db_link, "ALTER TABLE `".$pre."users` CHANGE `user_ip` `user_ip` VARCHAR(400) NOT NULL DEFAULT 'none'");
873
} elseif ($res === false) {
874
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field user_ip to table Users! '.mysqli_error($db_link).'!"}]';
875
    mysqli_close($db_link);
876
    exit();
877
}
878
879
880
// alter table USERS to add a new field "user_api_key"
881
$res = addColumnIfNotExist(
882
    $pre."users",
883
    "user_api_key",
884
    "VARCHAR(500) NOT NULL DEFAULT 'none'"
885
);
886
if ($res === false) {
887
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field user_api_key to table Users! '.mysqli_error($db_link).'!"}]';
888
    mysqli_close($db_link);
889
    exit();
890
}
891
892
893
// alter table USERS to add a new field "yubico_user_key"
894
$res = addColumnIfNotExist(
895
    $pre."users",
896
    "yubico_user_key",
897
    "VARCHAR(100) NOT NULL DEFAULT 'none'"
898
);
899
if ($res === false) {
900
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field yubico_user_key to table Users! '.mysqli_error($db_link).'!"}]';
901
    mysqli_close($db_link);
902
    exit();
903
}
904
905
906
// alter table USERS to add a new field "yubico_user_id"
907
$res = addColumnIfNotExist(
908
    $pre."users",
909
    "yubico_user_id",
910
    "VARCHAR(100) NOT NULL DEFAULT 'none'"
911
);
912
if ($res === false) {
913
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field yubico_user_id to table Users! '.mysqli_error($db_link).'!"}]';
914
    mysqli_close($db_link);
915
    exit();
916
}
917
918
919
// alter table USERS to allow NULL on field "email"
920
mysqli_query(
921
    $db_link,
922
    "ALTER TABLE `".$pre."users` CHANGE `email` `email` VARCHAR(300) NOT NULL DEFAULT 'none';"
923
);
924
925
926
// alter table EXPORT to add a new fields
927
mysqli_query(
928
    $db_link,
929
    "ALTER TABLE `".$pre."export` ADD `email` VARCHAR(500) NOT NULL DEFAULT 'none';"
930
);
931
mysqli_query(
932
    $db_link,
933
    "ALTER TABLE `".$pre."export` ADD `url` VARCHAR(500) NOT NULL DEFAULT 'none';"
934
);
935
mysqli_query(
936
    $db_link,
937
    "ALTER TABLE `".$pre."export` ADD `kbs` VARCHAR(500) NOT NULL DEFAULT 'none';"
938
);
939
mysqli_query(
940
    $db_link,
941
    "ALTER TABLE `".$pre."export` ADD `tags` VARCHAR(500) NOT NULL DEFAULT 'none';"
942
);
943
944
mysqli_query(
945
    $db_link,
946
    "ALTER TABLE `".$pre."misc` CHANGE valeur valeur VARCHAR(500) NOT NULL DEFAULT 'none'"
947
);
948
949
// alter table ITEMS_CHANGE
950
mysqli_query(
951
    $db_link,
952
    "ALTER TABLE `".$pre."items_change` CHANGE user_id user_id INT(12) NOT NULL;"
953
);
954
955
// alter table ITEMS
956
mysqli_query(
957
    $db_link,
958
    "ALTER TABLE `".$pre."items` CHANGE auto_update_pwd_next_date auto_update_pwd_next_date VARCHAR(100) NOT NULL DEFAULT '0';"
959
);
960
961
962
// add new admin setting "otv_is_enabled"
963
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'otv_is_enabled'"));
964
if (intval($tmp) === 0) {
965
    mysqli_query(
966
        $db_link,
967
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'otv_is_enabled', '0')"
968
    );
969
}
970
971
972
// add new admin setting "ldap_and_local_authentication"
973
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'ldap_and_local_authentication'"));
974
if (intval($tmp) === 0) {
975
    mysqli_query(
976
        $db_link,
977
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'ldap_and_local_authentication', '0')"
978
    );
979
}
980
981
982
// add new admin setting "secure_display_image"
983
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'secure_display_image'"));
984
if (intval($tmp) === 0) {
985
    mysqli_query(
986
        $db_link,
987
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'secure_display_image', '1')"
988
    );
989
}
990
991
992
// add new admin setting "upload_zero_byte_file"
993
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'upload_zero_byte_file'"));
994
if (intval($tmp) === 0) {
995
    mysqli_query(
996
        $db_link,
997
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'upload_zero_byte_file', '0')"
998
    );
999
}
1000
1001
1002
// add new admin setting "upload_all_extensions_file"
1003
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'upload_all_extensions_file'"));
1004
if (intval($tmp) === 0) {
1005
    mysqli_query(
1006
        $db_link,
1007
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'upload_all_extensions_file', '0')"
1008
    );
1009
}
1010
1011
1012
// generate new backup key
1013
mysqli_query(
1014
    $db_link,
1015
    "UPDATE `".$pre."misc`
1016
    SET valeur = '".generateRandomKey()."'
0 ignored issues
show
Bug introduced by
Are you sure generateRandomKey() of type void 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

1016
    SET valeur = '"./** @scrutinizer ignore-type */ generateRandomKey()."'
Loading history...
Bug introduced by
Are you sure the usage of generateRandomKey() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1017
    WHERE type = 'admin' AND intitule = 'bck_script_passkey'"
1018
);
1019
1020
1021
1022
// alter table NESTEED_TREE to INT(5) on field "renewal_period"
1023
mysqli_query(
1024
    $db_link,
1025
    "ALTER TABLE `".$pre."nested_tree` CHANGE `renewal_period` `renewal_period` INT(5) NOT null DEFAULT '0';"
1026
);
1027
1028
1029
1030
// add new field for items_change
1031
mysqli_query(
1032
    $db_link,
1033
    "CREATE TABLE IF NOT EXISTS `".$pre."items_change` (
1034
    `id` int(12) NOT NULL AUTO_INCREMENT,
1035
    `item_id` int(12) NOT NULL,
1036
    `label` varchar(255) NOT NULL DEFAULT 'none',
1037
    `pw` text NOT NULL,
1038
    `login` varchar(255) NOT NULL DEFAULT 'none',
1039
    `email` varchar(255) NOT NULL DEFAULT 'none',
1040
    `url` varchar(255) NOT NULL DEFAULT 'none',
1041
    `description` text NOT NULL,
1042
    `comment` text NOT NULL,
1043
    `folder_id` tinyint(12) NOT NULL,
1044
    `user_id` tinyint(12) NOT NULL,
1045
    `timestamp` varchar(50) NOT NULL DEFAULT 'none',
1046
    PRIMARY KEY (`id`)
1047
    ) CHARSET=utf8;"
1048
);
1049
1050
1051
// add field status to FILE table
1052
$res = addColumnIfNotExist(
1053
    $pre."files",
1054
    "content",
1055
    "longblob DEFAULT NULL"
1056
);
1057
if ($res === false) {
1058
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field content to table files! '.mysqli_error($db_link).'!"}]';
1059
    mysqli_close($db_link);
1060
    exit();
1061
}
1062
1063
1064
// add new table for templates
1065
mysqli_query(
1066
    $db_link,
1067
    "CREATE TABLE IF NOT EXISTS `".$pre."templates` (
1068
    `increment_id` int(12) NOT NULL AUTO_INCREMENT,
1069
    `item_id` int(12) NOT NULL,
1070
    `category_id` int(12) NOT NULL,
1071
    PRIMARY KEY (`increment_id`)
1072
    ) CHARSET=utf8;"
1073
);
1074
1075
1076
1077
// File encryption
1078
// add field status to FILE table
1079
$res = addColumnIfNotExist(
1080
    $pre."files",
1081
    "status",
1082
    "VARCHAR(50) NOT NULL DEFAULT '0'"
1083
);
1084
if ($res === false) {
1085
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field agses-usercardid to table Users! '.mysqli_error($db_link).'!"}]';
1086
    mysqli_close($db_link);
1087
    exit();
1088
}
1089
1090
// fill in this new field with the current "encryption-file" status
1091
$tmp = mysqli_fetch_row(mysqli_query($db_link, "SELECT valeur FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'enable_attachment_encryption'"));
1092
if (!empty($tmp[0])) {
1093
    if ($tmp[0] === "1") {
1094
        $status = "encrypted";
1095
    } else {
1096
        $status = "clear";
1097
    }
1098
    mysqli_query($db_link, "update `".$pre."files` set status = '".$status."' where 1 = 1");
1099
}
1100
1101
1102
// add 2 generic users
1103
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."users` WHERE id = '9999991' AND login = 'OTV'"));
1104
if (intval($tmp) === 0) {
1105
    mysqli_query(
1106
        $db_link,
1107
        "INSERT INTO `".$pre."users` (`id`, `login`, `pw`, `groupes_visibles`, `derniers`, `key_tempo`, `last_pw_change`, `last_pw`, `admin`, `fonction_id`, `groupes_interdits`, `last_connexion`, `gestionnaire`, `email`, `favourites`, `latest_items`, `personal_folder`) VALUES ('9999991', 'OTV', '', '', '', '', '', '', '1', '', '', '', '0', '', '', '', '0')"
1108
    );
1109
}
1110
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."users` WHERE id = '9999991' AND login = 'OTV'"));
1111
if (intval($tmp) === 0) {
1112
    mysqli_query(
1113
        $db_link,
1114
        "INSERT INTO `".$pre."users` (`id`, `login`, `pw`, `groupes_visibles`, `derniers`, `key_tempo`, `last_pw_change`, `last_pw`, `admin`, `fonction_id`, `groupes_interdits`, `last_connexion`, `gestionnaire`, `email`, `favourites`, `latest_items`, `personal_folder`) VALUES ('9999999', 'API', '', '', '', '', '', '', '1', '', '', '', '0', '', '', '', '0')"
1115
    );
1116
}
1117
1118
1119
// Update favico to favicon
1120
$result = mysqli_query($db_link, "SELECT valeur FROM `".$pre."misc` WHERE intitule = 'cpassman_url' AND type = 'admin'");
1121
$rows = mysqli_fetch_assoc($result);
1122
mysqli_free_result($result);
1123
mysqli_query(
1124
    $db_link,
1125
    "UPDATE `".$pre."misc`
1126
    SET `valeur` = '".$rows['valeur']."/favicon.ico'
1127
    WHERE intitule = 'favicon' AND type = 'admin'"
1128
);
1129
1130
1131
// Remove some indexes
1132
mysqli_query($db_link, "ALTER TABLE ".$pre."nested_tree` DROP INDEX `id`;");
1133
mysqli_query($db_link, "ALTER TABLE ".$pre."tags` DROP INDEX `id`;");
1134
1135
1136
// add field masked to CATEGORIES table
1137
$res = addColumnIfNotExist(
1138
    $pre."categories",
1139
    "masked",
1140
    "tinyint(1) NOT NULL default '0'"
1141
);
1142
if ($res === false) {
1143
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field masked to table CATEGORIES! '.mysqli_error($db_link).'!"}]';
1144
    mysqli_close($db_link);
1145
    exit();
1146
}
1147
1148
1149
// add field role_visibility to CATEGORIES table
1150
$res = addColumnIfNotExist(
1151
    $pre."categories",
1152
    "role_visibility",
1153
    "VARCHAR(250) NOT NULL DEFAULT 'all'"
1154
);
1155
if ($res === false) {
1156
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field role_visibility to table CATEGORIES! '.mysqli_error($db_link).'!"}]';
1157
    mysqli_close($db_link);
1158
    exit();
1159
}
1160
1161
1162
// add field is_mandatory to CATEGORIES table
1163
$res = addColumnIfNotExist(
1164
    $pre."categories",
1165
    "is_mandatory",
1166
    "tinyint(1) NOT NULL DEFAULT '0'"
1167
);
1168
if ($res === false) {
1169
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field is_mandatory to table CATEGORIES! '.mysqli_error($db_link).'!"}]';
1170
    mysqli_close($db_link);
1171
    exit();
1172
}
1173
1174
1175
// Now perform an operation on table CATEGORIES
1176
// This will change the 'masked' to an attribute of 'text' type
1177
$result = mysqli_query(
1178
    $db_link,
1179
    "SELECT id, type FROM `".$pre."categories` WHERE type = 'masked'"
1180
);
1181
while ($row_field = mysqli_fetch_assoc($result)) {
1182
    mysqli_query(
1183
        $db_link,
1184
        "UPDATE `".$pre."categories`
1185
        SET `type` = 'text', `masked` = '1'
1186
        WHERE id = ".$row_field['id']
1187
    );
1188
}
1189
1190
1191
/*
1192
* Introduce new CONFIG file
1193
*/
1194
$tp_config_file = "../includes/config/tp.config.php";
1195
if (file_exists($tp_config_file)) {
1196
    if (!copy($tp_config_file, $tp_config_file.'.'.date("Y_m_d", mktime(0, 0, 0, (int) date('m'), (int) date('d'), (int) date('y'))))) {
1197
        echo '[{"error" : "includes/config/tp.config.php file already exists and cannot be renamed. Please do it by yourself and click on button Launch.", "result":"", "index" : "'.$post_index.'", "multiple" : "'.$post_multiple.'"}]';
1198
        return false;
1199
    } else {
1200
        unlink($tp_config_file);
1201
    }
1202
}
1203
$file_handler = fopen($tp_config_file, 'w');
1204
$config_text = "";
1205
$any_settings = false;
1206
1207
$result = mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin'");
1208
while ($row = mysqli_fetch_assoc($result)) {
1209
    // append new setting in config file
1210
    $config_text .= "
1211
    '".$row['intitule']."' => '".$row['valeur']."',";
1212
    if ($any_settings === false) {
1213
        $any_settings = true;
1214
    }
1215
}
1216
mysqli_free_result($result);
1217
1218
// write to config file
1219
if ($any_settings === true) {
1220
    $result = fwrite(
1221
        $file_handler,
0 ignored issues
show
Bug introduced by
It seems like $file_handler can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1221
        /** @scrutinizer ignore-type */ $file_handler,
Loading history...
1222
        utf8_encode(
1223
            "<?php
1224
global \$SETTINGS;
1225
\$SETTINGS = array (" . $config_text."
1226
);"
1227
        )
1228
    );
1229
}
1230
fclose($file_handler);
0 ignored issues
show
Bug introduced by
It seems like $file_handler can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

1230
fclose(/** @scrutinizer ignore-type */ $file_handler);
Loading history...
1231
1232
1233
// Generate API key by user
1234
$result = mysqli_query($db_link, "SELECT id FROM `".$pre."users` WHERE login NOT IN ('admin', 'API', 'OTV')");
1235
while ($row = mysqli_fetch_assoc($result)) {
1236
    // Check if key already exists
1237
    $tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."api` WHERE label = '".$row['id']."'"));
1238
    if (intval($tmp) === 0) {
1239
        mysqli_query(
1240
            $db_link,
1241
            "INSERT INTO `".$pre."api` (`type`, `label`, `value`, `timestamp`) VALUES ('user', '".$row['id']."', '".uniqidReal(39)."', '".time()."')"
1242
        );
1243
    }
1244
}
1245
1246
// Finished
1247
echo '[{"finish":"1" , "next":"", "error":""}]';
1248