Passed
Push — development ( b3bbd1...d832f6 )
by Nils
05:00 queued 01:04
created

cleanFields()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 21
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 21
b 0
f 0
nc 6
nop 1
dl 0
loc 16
rs 8.8571
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
 * Function permits to get the value from a line
93
 * @param  string $val [description]
94
 * @return string      [description]
95
 */
96
function getSettingValue($val)
97
{
98
    $val = trim(strstr($val, "="));
99
    return trim(str_replace('"', '', substr($val, 1, strpos($val, ";") - 1)));
100
}
101
102
/**
103
 * Function permits to check if a column exists, and if not to add it
104
 * @param string $dbname     [description]
105
 * @param string $column     [description]
106
 * @param string $columnAttr [description]
107
 */
108
function addColumnIfNotExist($dbname, $column, $columnAttr = "VARCHAR(255) NULL")
109
{
110
    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...
111
    $exists = false;
112
    $columns = mysqli_query($db_link, "show columns from $dbname");
113
    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

113
    while ($col = mysqli_fetch_assoc(/** @scrutinizer ignore-type */ $columns)) {
Loading history...
114
        if ($col['Field'] == $column) {
115
            $exists = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $exists is dead and can be removed.
Loading history...
116
            return true;
117
        }
118
    }
119
    if (!$exists) {
0 ignored issues
show
introduced by
The condition $exists is always false.
Loading history...
120
        return mysqli_query($db_link, "ALTER TABLE `$dbname` ADD `$column`  $columnAttr");
121
    }
122
123
    return false;
124
}
125
126
/**
127
 * [cleanFields description]
128
 * @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...
129
 * @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...
130
 */
131
function cleanFields($txt)
132
{
133
    $tmp = str_replace(",", ";", trim($txt));
134
    if (empty($tmp)) {
135
        return $tmp;
136
    }
137
    if ($tmp === ";") {
138
        return "";
139
    }
140
    if (strpos($tmp, ';') === 0) {
141
        $tmp = substr($tmp, 1);
142
    }
143
    if (substr($tmp, -1) !== ";") {
144
        $tmp = $tmp.";";
145
    }
146
    return $tmp;
147
}
148
149
/*
150
** Checks if the column exists in the table
151
*/
152
function columnExists($tablename, $column)
153
{
154
    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...
155
    $checkcolumn = mysqli_query($db_link, "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='{$tablename}' AND COLUMN_NAME = '{$column}';");
156
    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

156
    if (mysqli_num_rows(/** @scrutinizer ignore-type */ $checkcolumn) > 0) {
Loading history...
157
        return true;
158
    } else {
159
        return false;
160
    }
161
}
162
163
// 2.1.27 introduce new encryption protocol with DEFUSE library.
164
// Now evaluate if current instance has already this version
165
$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

165
$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...
166
if (count($tmp[0]) === 0 || empty($tmp[0])) {
167
    mysqli_query(
168
        $db_link,
169
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'teampass_version', '".$SETTINGS_EXT['version']."')"
170
    );
171
} else {
172
    mysqli_query(
173
        $db_link,
174
        "UPDATE `".$pre."misc`
175
        SET `valeur` = '".$SETTINGS_EXT['version']."'
176
        WHERE intitule = 'teampass_version' AND type = 'admin'"
177
    );
178
}
179
180
// add new admin setting "migration_to_2127"
181
$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

181
$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...
182
if (intval($tmp) === 0) {
183
    mysqli_query(
184
        $db_link,
185
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'migration_to_2127', '0')"
186
    );
187
}
188
189
190
// check if library defuse already on-going here
191
// if yes, then don't execute re-encryption
192
if (isset($session_tp_defuse_installed) === false) {
193
    $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

193
    $superGlobal->put("tp_defuse_installed", /** @scrutinizer ignore-type */ false, "SESSION");
Loading history...
194
    if (columnExists($pre."items", "encryption_type") === true) {
195
        $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

195
        $superGlobal->put("tp_defuse_installed", /** @scrutinizer ignore-type */ true, "SESSION");
Loading history...
196
    }
197
}
198
199
// alter table Items
200
mysqli_query($db_link, "ALTER TABLE `".$pre."items` MODIFY pw_len INT(5) NOT NULL DEFAULT '0'");
201
202
// alter table MISC - rename ID is exists
203
$res = addColumnIfNotExist(
204
    $pre."misc",
205
    "increment_id",
206
    "INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
207
);
208
if ($res === true) {
209
    // Change name of field
210
    mysqli_query($db_link, "ALTER TABLE `".$pre."misc` CHANGE `id` `increment_id` INT(12) NOT NULL AUTO_INCREMENT");
211
} elseif ($res === false) {
212
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding increment_id to table misc! '.mysqli_error($db_link).'!"}]';
213
    mysqli_close($db_link);
214
    exit();
215
}
216
217
218
// alter table misc to add an index
219
mysqli_query(
220
    $db_link,
221
    "ALTER TABLE `".$pre."log_items` ADD `increment_id` INT(12) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (`increment_id`)"
222
);
223
// create index
224
mysqli_query(
225
    $db_link,
226
    "CREATE INDEX teampass_log_items_id_item_IDX ON ".$pre."log_items (id_item, date);"
227
);
228
229
// add field agses-usercardid to Users table
230
$res = addColumnIfNotExist(
231
    $pre."users",
232
    "agses-usercardid",
233
    "VARCHAR(12) NOT NULL DEFAULT '0'"
234
);
235
if ($res === false) {
236
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field agses-usercardid to table Users! '.mysqli_error($db_link).'!"}]';
237
    mysqli_close($db_link);
238
    exit();
239
}
240
241
242
// add field encrypted_data to Categories table
243
$res = addColumnIfNotExist(
244
    $pre."categories",
245
    "encrypted_data",
246
    "TINYINT(1) NOT NULL DEFAULT '1'"
247
);
248
if ($res === false) {
249
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encrypted_data to table categories! '.mysqli_error($db_link).'!"}]';
250
    mysqli_close($db_link);
251
    exit();
252
}
253
254
255
// add field is_mandatory to Categories table
256
$res = addColumnIfNotExist(
257
    $pre."categories",
258
    "is_mandatory",
259
    "BOOLEAN NOT NULL DEFAULT FALSE"
260
);
261
if ($res === false) {
262
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field is_mandatory to table categories! '.mysqli_error($db_link).'!"}]';
263
    mysqli_close($db_link);
264
    exit();
265
}
266
267
268
// alter table USERS - user_language
269
mysqli_query($db_link, "ALTER TABLE `".$pre."users` MODIFY user_language VARCHAR(50) NOT NULL DEFAULT '0'");
270
271
// alter table USERS - just ensure correct naming of IsAdministratedByRole
272
mysqli_query($db_link, "ALTER TABLE `".$pre."users` CHANGE IsAdministratedByRole isAdministratedByRole tinyint(5) NOT NULL DEFAULT '0'");
273
274
// alter table OTV
275
mysqli_query($db_link, "ALTER TABLE `".$pre."otv` CHANGE originator originator int(12) NOT NULL DEFAULT '0'");
276
277
// do clean of users table
278
$fieldsToUpdate = ['groupes_visibles', 'fonction_id', 'groupes_interdits'];
279
$result = mysqli_query($db_link, "SELECT id, groupes_visibles, fonction_id, groupes_interdits FROM `".$pre."users`");
280
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

280
while ($row = mysqli_fetch_assoc(/** @scrutinizer ignore-type */ $result)) {
Loading history...
281
    // check if field contains , instead of ;
282
    foreach ($fieldsToUpdate as $field) {
283
        $tmp = cleanFields($row[$field]);
284
        if ($tmp !== $row[$field]) {
285
            mysqli_query(
286
                $db_link,
287
                "UPDATE `".$pre."users`
288
                SET `".$field."` = '".$tmp."'
289
                WHERE id = '".$row['id']."'"
290
            );
291
        }
292
    }
293
}
294
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

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

586
    $data = array_map('replace_a_line', /** @scrutinizer ignore-type */ $data);
Loading history...
587
    file_put_contents($session_sk_file, implode('', $data));
588
589
    //
590
    //
591
    //-- users need to perform re-encryption of their personal pwds
592
    $result = mysqli_query(
593
        $db_link,
594
        "SELECT valeur FROM `".$pre."misc` WHERE type='admin' AND intitule='encryption_type'"
595
    );
596
    $row = mysqli_fetch_assoc($result);
597
    if ($row['valeur'] !== "defuse") {
598
        $result = mysqli_query(
599
            $db_link,
600
            "SELECT id FROM `".$pre."users`"
601
        );
602
        while ($row_user = mysqli_fetch_assoc($result)) {
603
            $result_items = mysqli_query(
604
                $db_link,
605
                "SELECT i.id AS item_id
606
                FROM `".$pre."nested_tree` AS n
607
                INNER JOIN `".$pre."items` AS i ON (i.id_tree = n.id)
608
                WHERE n.title = ".$row_user['id']
609
            );
610
            if (mysqli_num_rows($result_items) > 0) {
611
                mysqli_query(
612
                    $db_link,
613
                    "UPDATE `".$pre."users`
614
                    SET `upgrade_needed` = '1'
615
                    WHERE id = ".$row_user['id']
616
                );
617
            } else {
618
                mysqli_query(
619
                    $db_link,
620
                    "UPDATE `".$pre."users`
621
                    SET `upgrade_needed` = '0'
622
                    WHERE id = ".$row_user['id']
623
                );
624
            }
625
        }
626
627
        mysqli_query(
628
            $db_link,
629
            "UPDATE `".$pre."misc`
630
            SET `valeur` = 'defuse'
631
            WHERE `type`='admin' AND `initule`='encryption_type'"
632
        );
633
    }
634
} else {
635
    $_SESSION['tp_defuse_new_key'] = false;
636
}
637
//--
638
639
640
// add field encrypted_psk to Users table
641
$res = addColumnIfNotExist(
642
    $pre."users",
643
    "encrypted_psk",
644
    "TEXT NOT NULL"
645
);
646
if ($res === false) {
647
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field encrypted_psk to table Users! '.mysqli_error($db_link).'!"}]';
648
    mysqli_close($db_link);
649
    exit();
650
}
651
652
653
// add new admin setting "manager_move_item"
654
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'manager_move_item'"));
655
if (intval($tmp) === 0) {
656
    mysqli_query(
657
        $db_link,
658
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'manager_move_item', '0')"
659
    );
660
}
661
662
// add new admin setting "create_item_without_password"
663
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'create_item_without_password'"));
664
if (intval($tmp) === 0) {
665
    mysqli_query(
666
        $db_link,
667
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'create_item_without_password', '0')"
668
    );
669
}
670
671
// add new admin setting "send_statistics_items"
672
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'send_statistics_items'"));
673
if (intval($tmp) === 0) {
674
    mysqli_query(
675
        $db_link,
676
        "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;')"
677
    );
678
}
679
680
// add new admin setting "send_stats_time"
681
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'send_stats_time'"));
682
if (intval($tmp) === 0) {
683
    mysqli_query(
684
        $db_link,
685
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'send_stats_time', '".(time() - 2592000)."')"
686
    );
687
}
688
689
// add new admin setting "agses_authentication_enabled"
690
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'agses_authentication_enabled'"));
691
if (intval($tmp) === 0) {
692
    mysqli_query(
693
        $db_link,
694
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'agses_authentication_enabled', '0')"
695
    );
696
}
697
698
// add new admin setting "timezone"
699
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'timezone'"));
700
if (intval($tmp) === 0) {
701
    mysqli_query(
702
        $db_link,
703
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'timezone', 'UTC')"
704
    );
705
}
706
707
// add new admin setting "personal_saltkey_security_level"
708
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'personal_saltkey_security_level'"));
709
if (intval($tmp) === 0) {
710
    mysqli_query(
711
        $db_link,
712
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'personal_saltkey_security_level', '0')"
713
    );
714
}
715
716
// add new admin setting "item_extra_fields"
717
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'item_extra_fields'"));
718
if (intval($tmp) === 0) {
719
    mysqli_query(
720
        $db_link,
721
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'item_extra_fields', '0')"
722
    );
723
}
724
725
// add new admin setting "ldap_new_user_is_administrated_by"
726
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'ldap_new_user_is_administrated_by'"));
727
if (intval($tmp) === 0) {
728
    mysqli_query(
729
        $db_link,
730
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'ldap_new_user_is_administrated_by', '0')"
731
    );
732
}
733
734
735
// add new admin setting "ldap_port"
736
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'ldap_port'"));
737
if (intval($tmp) === 0) {
738
    mysqli_query(
739
        $db_link,
740
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'ldap_port', '389')"
741
    );
742
}
743
744
// add new admin setting "offline_key_level"
745
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'offline_key_level'"));
746
if (intval($tmp) === 0) {
747
    mysqli_query(
748
        $db_link,
749
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'offline_key_level', '0')"
750
    );
751
}
752
753
// add new admin setting "enable_http_request_login"
754
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'enable_http_request_login'"));
755
if (intval($tmp) === 0) {
756
    mysqli_query(
757
        $db_link,
758
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'enable_http_request_login', '0')"
759
    );
760
}
761
762
763
// add new language "portuges_br"
764
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'portuguese_br'"));
765
if (intval($tmp) === 0) {
766
    mysqli_query(
767
        $db_link,
768
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('portuguese_br', 'Portuguese_br', 'pr-bt', 'pr-bt.png')"
769
    );
770
}
771
772
773
// add new language "Ukrainian"
774
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'ukrainian'"));
775
if (intval($tmp) === 0) {
776
    mysqli_query(
777
        $db_link,
778
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('ukrainian', 'Ukrainian', 'ua', 'ua.png')"
779
    );
780
}
781
782
783
// add new language "Romanian"
784
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'romanian'"));
785
if (intval($tmp) === 0) {
786
    mysqli_query(
787
        $db_link,
788
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('romanian', 'Romanian', 'ro', 'ro.png')"
789
    );
790
}
791
792
793
// add new language "Polish"
794
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'polish'"));
795
if (intval($tmp) === 0) {
796
    mysqli_query(
797
        $db_link,
798
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('polish', 'Polish', 'po', 'po.png')"
799
    );
800
}
801
802
803
// add new language "Hungarian"
804
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'hungarian'"));
805
if (intval($tmp) === 0) {
806
    mysqli_query(
807
        $db_link,
808
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('hungarian', 'Hungarian', 'hu', 'hu.png')"
809
    );
810
}
811
812
813
// add new language "Greek"
814
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'greek'"));
815
if (intval($tmp) === 0) {
816
    mysqli_query(
817
        $db_link,
818
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('greek', 'Greek', 'gr', 'gr.png')"
819
    );
820
}
821
822
823
// add new language "Bulgarian"
824
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."languages` WHERE name = 'bulgarian'"));
825
if (intval($tmp) === 0) {
826
    mysqli_query(
827
        $db_link,
828
        "INSERT INTO `".$pre."languages` (`name`, `label`, `code`, `flag`) VALUES ('bulgarian', 'Bulgarian', 'bg', 'bg.png')"
829
    );
830
}
831
832
833
// alter table USERS to add a new field "ga_temporary_code"
834
mysqli_query(
835
    $db_link,
836
    "ALTER TABLE `".$pre."users` ADD `ga_temporary_code` VARCHAR(20) NOT NULL DEFAULT 'none' AFTER `ga`;"
837
);
838
839
840
// alter table USERS to add a new field "user_ip"
841
$res = addColumnIfNotExist(
842
    $pre."users",
843
    "user_ip",
844
    "VARCHAR(400) NOT NULL DEFAULT 'none'"
845
);
846
if ($res === true) {
847
    // Change name of field
848
    mysqli_query($db_link, "ALTER TABLE `".$pre."users` CHANGE `user_ip` `user_ip` VARCHAR(400) NOT NULL DEFAULT 'none'");
849
} elseif ($res === false) {
850
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field user_ip to table Users! '.mysqli_error($db_link).'!"}]';
851
    mysqli_close($db_link);
852
    exit();
853
}
854
855
856
// alter table USERS to add a new field "user_api_key"
857
$res = addColumnIfNotExist(
858
    $pre."users",
859
    "user_api_key",
860
    "VARCHAR(500) NOT NULL DEFAULT 'none'"
861
);
862
if ($res === false) {
863
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field user_api_key to table Users! '.mysqli_error($db_link).'!"}]';
864
    mysqli_close($db_link);
865
    exit();
866
}
867
868
869
// alter table USERS to add a new field "yubico_user_key"
870
$res = addColumnIfNotExist(
871
    $pre."users",
872
    "yubico_user_key",
873
    "VARCHAR(100) NOT NULL DEFAULT 'none'"
874
);
875
if ($res === false) {
876
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field yubico_user_key to table Users! '.mysqli_error($db_link).'!"}]';
877
    mysqli_close($db_link);
878
    exit();
879
}
880
881
882
// alter table USERS to add a new field "yubico_user_id"
883
$res = addColumnIfNotExist(
884
    $pre."users",
885
    "yubico_user_id",
886
    "VARCHAR(100) NOT NULL DEFAULT 'none'"
887
);
888
if ($res === false) {
889
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field yubico_user_id to table Users! '.mysqli_error($db_link).'!"}]';
890
    mysqli_close($db_link);
891
    exit();
892
}
893
894
895
// alter table USERS to allow NULL on field "email"
896
mysqli_query(
897
    $db_link,
898
    "ALTER TABLE `".$pre."users` CHANGE `email` `email` VARCHAR(300) NOT NULL DEFAULT 'none';"
899
);
900
901
902
// alter table EXPORT to add a new fields
903
mysqli_query(
904
    $db_link,
905
    "ALTER TABLE `".$pre."export` ADD `email` VARCHAR(500) NOT NULL DEFAULT 'none';"
906
);
907
mysqli_query(
908
    $db_link,
909
    "ALTER TABLE `".$pre."export` ADD `url` VARCHAR(500) NOT NULL DEFAULT 'none';"
910
);
911
mysqli_query(
912
    $db_link,
913
    "ALTER TABLE `".$pre."export` ADD `kbs` VARCHAR(500) NOT NULL DEFAULT 'none';"
914
);
915
mysqli_query(
916
    $db_link,
917
    "ALTER TABLE `".$pre."export` ADD `tags` VARCHAR(500) NOT NULL DEFAULT 'none';"
918
);
919
920
mysqli_query(
921
    $db_link,
922
    "ALTER TABLE `".$pre."misc` CHANGE valeur valeur VARCHAR(500) NOT NULL DEFAULT 'none'"
923
);
924
925
// alter table ITEMS_CHANGE
926
mysqli_query(
927
    $db_link,
928
    "ALTER TABLE `".$pre."items_change` CHANGE user_id user_id INT(12) NOT NULL;"
929
);
930
931
// alter table ITEMS
932
mysqli_query(
933
    $db_link,
934
    "ALTER TABLE `".$pre."items` CHANGE auto_update_pwd_next_date auto_update_pwd_next_date VARCHAR(100) NOT NULL DEFAULT '0';"
935
);
936
937
938
// add new admin setting "otv_is_enabled"
939
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'otv_is_enabled'"));
940
if (intval($tmp) === 0) {
941
    mysqli_query(
942
        $db_link,
943
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'otv_is_enabled', '0')"
944
    );
945
}
946
947
948
// add new admin setting "ldap_and_local_authentication"
949
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'ldap_and_local_authentication'"));
950
if (intval($tmp) === 0) {
951
    mysqli_query(
952
        $db_link,
953
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'ldap_and_local_authentication', '0')"
954
    );
955
}
956
957
958
// add new admin setting "secure_display_image"
959
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'secure_display_image'"));
960
if (intval($tmp) === 0) {
961
    mysqli_query(
962
        $db_link,
963
        "INSERT INTO `".$pre."misc` (`type`, `intitule`, `valeur`) VALUES ('admin', 'secure_display_image', '1')"
964
    );
965
}
966
967
968
969
// alter table NESTEED_TREE to INT(5) on field "renewal_period"
970
mysqli_query(
971
    $db_link,
972
    "ALTER TABLE `".$pre."nested_tree` CHANGE `renewal_period` `renewal_period` INT(5) NOT null DEFAULT '0';"
973
);
974
975
976
977
// add new field for items_change
978
mysqli_query(
979
    $db_link,
980
    "CREATE TABLE IF NOT EXISTS `".$pre."items_change` (
981
    `id` int(12) NOT NULL AUTO_INCREMENT,
982
    `item_id` int(12) NOT NULL,
983
    `label` varchar(255) NOT NULL DEFAULT 'none',
984
    `pw` text NOT NULL,
985
    `login` varchar(255) NOT NULL DEFAULT 'none',
986
    `email` varchar(255) NOT NULL DEFAULT 'none',
987
    `url` varchar(255) NOT NULL DEFAULT 'none',
988
    `description` text NOT NULL,
989
    `comment` text NOT NULL,
990
    `folder_id` tinyint(12) NOT NULL,
991
    `user_id` tinyint(12) NOT NULL,
992
    `timestamp` varchar(50) NOT NULL DEFAULT 'none',
993
    PRIMARY KEY (`id`)
994
    ) CHARSET=utf8;"
995
);
996
997
998
// add field status to FILE table
999
$res = addColumnIfNotExist(
1000
    $pre."files",
1001
    "content",
1002
    "longblob DEFAULT NULL"
1003
);
1004
if ($res === false) {
1005
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field content to table files! '.mysqli_error($db_link).'!"}]';
1006
    mysqli_close($db_link);
1007
    exit();
1008
}
1009
1010
1011
1012
// File encryption
1013
// add field status to FILE table
1014
$res = addColumnIfNotExist(
1015
    $pre."files",
1016
    "status",
1017
    "VARCHAR(50) NOT NULL DEFAULT '0'"
1018
);
1019
if ($res === false) {
1020
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field agses-usercardid to table Users! '.mysqli_error($db_link).'!"}]';
1021
    mysqli_close($db_link);
1022
    exit();
1023
}
1024
1025
// fill in this new field with the current "encryption-file" status
1026
$tmp = mysqli_fetch_row(mysqli_query($db_link, "SELECT valeur FROM `".$pre."misc` WHERE type = 'admin' AND intitule = 'enable_attachment_encryption'"));
1027
if (!empty($tmp[0])) {
1028
    if ($tmp[0] === "1") {
1029
        $status = "encrypted";
1030
    } else {
1031
        $status = "clear";
1032
    }
1033
    mysqli_query($db_link, "update `".$pre."files` set status = '".$status."' where 1 = 1");
1034
}
1035
1036
1037
// add 2 generic users
1038
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."users` WHERE id = '9999991' AND login = 'OTV'"));
1039
if (intval($tmp) === 0) {
1040
    mysqli_query(
1041
        $db_link,
1042
        "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')"
1043
    );
1044
}
1045
$tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."users` WHERE id = '9999991' AND login = 'OTV'"));
1046
if (intval($tmp) === 0) {
1047
    mysqli_query(
1048
        $db_link,
1049
        "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')"
1050
    );
1051
}
1052
1053
1054
// Update favico to favicon
1055
$result = mysqli_query($db_link, "SELECT valeur FROM `".$pre."misc` WHERE intitule = 'cpassman_url' AND type = 'admin'");
1056
$rows = mysqli_fetch_assoc($result);
1057
mysqli_free_result($result);
1058
mysqli_query(
1059
    $db_link,
1060
    "UPDATE `".$pre."misc`
1061
    SET `valeur` = '".$rows['valeur']."/favicon.ico'
1062
    WHERE intitule = 'favicon' AND type = 'admin'"
1063
);
1064
1065
1066
// Remove some indexes
1067
mysqli_query($db_link, "ALTER TABLE ".$pre."nested_tree` DROP INDEX `id`;");
1068
mysqli_query($db_link, "ALTER TABLE ".$pre."tags` DROP INDEX `id`;");
1069
1070
1071
// add field masked to CATEGORIES table
1072
$res = addColumnIfNotExist(
1073
    $pre."categories",
1074
    "masked",
1075
    "tinyint(1) NOT NULL default '0'"
1076
);
1077
if ($res === false) {
1078
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field masked to table CATEGORIES! '.mysqli_error($db_link).'!"}]';
1079
    mysqli_close($db_link);
1080
    exit();
1081
}
1082
1083
1084
// add field role_visibility to CATEGORIES table
1085
$res = addColumnIfNotExist(
1086
    $pre."categories",
1087
    "role_visibility",
1088
    "VARCHAR(250) NOT NULL DEFAULT 'all'"
1089
);
1090
if ($res === false) {
1091
    echo '[{"finish":"1", "msg":"", "error":"An error appears when adding field role_visibility to table CATEGORIES! '.mysqli_error($db_link).'!"}]';
1092
    mysqli_close($db_link);
1093
    exit();
1094
}
1095
1096
1097
// Now perform an operation on table CATEGORIES
1098
// This will change the 'masked' to an attribute of 'text' type
1099
$result = mysqli_query(
1100
    $db_link,
1101
    "SELECT id, type FROM `".$pre."categories` WHERE type = 'masked'"
1102
);
1103
while ($row_field = mysqli_fetch_assoc($result)) {
1104
    mysqli_query(
1105
        $db_link,
1106
        "UPDATE `".$pre."categories`
1107
        SET `type` = 'text', `masked` = '1'
1108
        WHERE id = ".$row_field['id']
1109
    );
1110
}
1111
1112
1113
/*
1114
* Introduce new CONFIG file
1115
*/
1116
$tp_config_file = "../includes/config/tp.config.php";
1117
if (file_exists($tp_config_file)) {
1118
    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'))))) {
1119
        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.'"}]';
1120
        return false;
1121
    } else {
1122
        unlink($tp_config_file);
1123
    }
1124
}
1125
$file_handler = fopen($tp_config_file, 'w');
1126
$config_text = "";
1127
$any_settings = false;
1128
1129
$result = mysqli_query($db_link, "SELECT * FROM `".$pre."misc` WHERE type = 'admin'");
1130
while ($row = mysqli_fetch_assoc($result)) {
1131
    // append new setting in config file
1132
    $config_text .= "
1133
    '".$row['intitule']."' => '".$row['valeur']."',";
1134
    if ($any_settings === false) {
1135
        $any_settings = true;
1136
    }
1137
}
1138
mysqli_free_result($result);
1139
1140
// write to config file
1141
if ($any_settings === true) {
1142
    $result = fwrite(
1143
        $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

1143
        /** @scrutinizer ignore-type */ $file_handler,
Loading history...
1144
        utf8_encode(
1145
            "<?php
1146
global \$SETTINGS;
1147
\$SETTINGS = array (" . $config_text."
1148
);"
1149
        )
1150
    );
1151
}
1152
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

1152
fclose(/** @scrutinizer ignore-type */ $file_handler);
Loading history...
1153
1154
1155
// Generate API key by user
1156
$result = mysqli_query($db_link, "SELECT id FROM `".$pre."users` WHERE login NOT IN ('admin', 'API', 'OTV')");
1157
while ($row = mysqli_fetch_assoc($result)) {
1158
    // Check if key already exists
1159
    $tmp = mysqli_num_rows(mysqli_query($db_link, "SELECT * FROM `".$pre."api` WHERE label = '".$row['id']."'"));
1160
    if (intval($tmp) === 0) {
1161
        mysqli_query(
1162
            $db_link,
1163
            "INSERT INTO `".$pre."api` (`type`, `label`, `value`, `timestamp`) VALUES ('user', '".$row['id']."', '".uniqidReal(39)."', '".time()."')"
1164
        );
1165
    }
1166
}
1167
1168
// Finished
1169
echo '[{"finish":"1" , "next":"", "error":""}]';
1170