-
-
Notifications
You must be signed in to change notification settings - Fork 531
/
tp.functions.php
executable file
·505 lines (450 loc) · 12.3 KB
/
tp.functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
use Hackzilla\PasswordGenerator\Generator\ComputerPasswordGenerator;
use Hackzilla\PasswordGenerator\RandomGenerator\Php7RandomGenerator;
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Exception as CryptoException;
// new SECUREFILE - 3.0.0.23
function handleSecurefileConstant()
{
if (defined('SECUREFILE') === false || SECUREFILE === 'teampass-seckey.txt' || file_exists(SECUREPATH.'/teampass-seckey.txt') === true) {
// Anonymize the file if needed
if (defined('SECUREFILE') === false) {
define('SECUREFILE', generateRandomKey());
}
// manage the file itself by renaming it
if (rename(SECUREPATH.'/teampass-seckey.txt', SECUREPATH.'/'.SECUREFILE) === false) {
echo '[{
"error" : "File `'.SECUREPATH.'/teampass-seckey.txt` could not be renamed. Please do it by yourself and click on button Launch.",
"index" : ""
}]';
exit;
}
// Ensure DB is read as UTF8
if (defined('DB_ENCODING') === false) {
define('DB_ENCODING', "utf8");
}
// Now create new file
$file_handled = fopen('../includes/config/settings.php', 'w');
$settingsTxt = '<?php
// DATABASE connexion parameters
define("DB_HOST", "' . DB_HOST . '");
define("DB_USER", "' . DB_USER . '");
define("DB_PASSWD", "' . DB_PASSWD . '");
define("DB_NAME", "' . DB_NAME . '");
define("DB_PREFIX", "' . DB_PREFIX . '");
define("DB_PORT", "' . DB_PORT . '");
define("DB_ENCODING", "' . DB_ENCODING . '");';
if (isset(DB_SSL['key']) === true && empty(DB_SSL['key']) === false) {
$settingsTxt .= '
define("DB_SSL", false); // if DB over SSL then comment this line
// if DB over SSL then uncomment the following lines
//define("DB_SSL", array(
// "key" => "",
// "cert" => "",
// "ca_cert" => "",
// "ca_path" => "",
// "cipher" => ""
//));';
} else {
$settingsTxt .= '
//define("DB_SSL", false); // if DB over SSL then comment this line
// if DB over SSL then uncomment the following lines
define("DB_SSL", array(
"key" => "'.DB_SSL['key'].'",
"cert" => "'.DB_SSL['cert'].'",
"ca_cert" => ""'.DB_SSL['ca_cert'].',
"ca_path" => "'.DB_SSL['ca_path'].'",
"cipher" => "'.DB_SSL['cipher'].'"
));';
}
$settingsTxt .= '
define("DB_CONNECT_OPTIONS", array(
MYSQLI_OPT_CONNECT_TIMEOUT => 10
));
define("SECUREPATH", "' . str_replace('\\', '\\\\', SECUREPATH) . '");
define("SECUREFILE", "' . SECUREFILE. '");';
if (defined('IKEY') === true) $settingsTxt .= '
define("IKEY", "' . IKEY . '");';
else $settingsTxt .= '
define("IKEY", "");';
if (defined('SKEY') === true) $settingsTxt .= '
define("SKEY", "' . SKEY . '");';
else $settingsTxt .= '
define("SKEY", "");';
if (defined('HOST') === true) $settingsTxt .= '
define("HOST", "' . HOST . '");';
else $settingsTxt .= '
define("HOST", "");';
$settingsTxt .= '
if (isset($_SESSION[\'settings\'][\'timezone\']) === true) {
date_default_timezone_set($_SESSION[\'settings\'][\'timezone\']);
}
';
$fileCreation = fwrite(
$file_handled,
utf8_encode($settingsTxt)
);
fclose($file_handled);
sleep(3);
if ($fileCreation === false) {
return [
'error' => true,
'message' => 'Setting.php file could not be created in /includes/config/ folder. Please check the path and the rights.',
];
}
return [
'error' => false,
'message' => ''
];
}
}
/**
* Undocumented function
*
* @param string $message Message
* @param string $ascii_key Key
* @param string $type Type
*
* @return array
*/
function defuseCryption($message, $ascii_key, $type)
{
include_once '../includes/config/settings.php';
// init
$err = '';
if (empty($ascii_key) === true) {
// new check - 3.0.0.23
$ascii_key = file_get_contents(SECUREPATH.'/'.SECUREFILE);
}
// convert KEY
$key = Key::loadFromAsciiSafeString($ascii_key);
try {
if ($type === 'encrypt') {
$text = Crypto::encrypt($message, $key);
} elseif ($type === 'decrypt') {
$text = Crypto::decrypt($message, $key);
}
} catch (CryptoException\WrongKeyOrModifiedCiphertextException $ex) {
$err = 'an attack! either the wrong key was loaded, or the ciphertext has changed since it was created either corrupted in the database or intentionally modified by someone trying to carry out an attack.';
} catch (CryptoException\BadFormatException $ex) {
$err = $ex;
} catch (CryptoException\EnvironmentIsBrokenException $ex) {
$err = $ex;
} catch (CryptoException\CryptoException $ex) {
$err = $ex;
} catch (CryptoException\IOException $ex) {
$err = $ex;
}
return array(
'string' => isset($text) ? $text : '',
'error' => $err,
);
}
/**
* Decrypt a defuse string if encrypted
*
* @param string $value Encrypted string
*
* @return string
*/
function defuse_return_decrypted($value)
{
if (substr($value, 0, 3) === "def") {
$value = defuseCryption(
$value,
"",
"decrypt"
)['string'];
}
return $value;
}
/**
* Function permits to get the value from a line
*
* @param string $val A string
*
* @return string
*/
function getSettingValue($val): string
{
$val = trim(strstr($val, "="));
return trim(str_replace('"', '', substr($val, 1, strpos($val, ";") - 1)));
}
/**
* Undocumented function
*
* @param string $dbname DB
* @param string $column Column
* @param string $columnAttr Attribute
*
* @return boolean
*/
function addColumnIfNotExist($dbname, $column, $columnAttr = "VARCHAR(255) NULL")
{
global $db_link;
$exists = false;
$columns = mysqli_query($db_link, "show columns from $dbname");
while ($col = mysqli_fetch_assoc($columns)) {
if ((string) $col['Field'] === $column) {
$exists = true;
return true;
}
}
if (!$exists && empty($column) === false) {
return mysqli_query($db_link, "ALTER TABLE `$dbname` ADD `$column` $columnAttr");
}
return false;
}
/**
* Remove column from table if exists
*
* @param string $table Table
* @param string $column Column
*
* @return boolean
*/
function removeColumnIfNotExist($table, $column): bool
{
global $db_link;
$exists = false;
$columns = mysqli_query($db_link, "show columns from $table");
while ($col = mysqli_fetch_assoc($columns)) {
if ((string) $col['Field'] === $column) {
$exists = true;
}
}
if ($exists === true) {
return mysqli_query($db_link, "ALTER TABLE `$table` DROP `$column`;");
}
return false;
}
/**
* Check if an INDEX exist, run the SQL query if not
*
* @param string $table Table
* @param string $index Index
* @param string $sql SQL
*
* @return array
*/
function checkIndexExist($table, $index, $sql)
{
global $db_link;
$mysqli_result = mysqli_query($db_link, "SHOW INDEX FROM $table WHERE key_name LIKE \"$index\"");
$res = mysqli_fetch_row($mysqli_result);
// if index does not exist, then add it
if (!$res) {
$res = mysqli_query(
$db_link,
"ALTER TABLE `$table` ".$sql
);
}
return $res;
}
/**
* Check if a table exists in DB
*
* @param string $tablename Table
*
* @return boolean
*/
function tableExists($tablename)
{
global $db_link, $database;
$res = mysqli_query(
$db_link,
"SELECT COUNT(*) as count
FROM information_schema.tables
WHERE table_schema = '".$database."'
AND table_name = '$tablename'"
);
if ($res > 0) {
return true;
}
return false;
}
/**
* Undocumented function
*
* @param string $txt My text
*
* @return string
*/
function cleanFields($txt)
{
$tmp = str_replace(",", ";", trim($txt));
if (empty($tmp)) {
return $tmp;
}
if ($tmp === ";") {
return "";
}
if (strpos($tmp, ';') === 0) {
$tmp = substr($tmp, 1);
}
if (substr($tmp, -1) !== ";") {
$tmp = $tmp.";";
}
return $tmp;
}
/**
* Undocumented function
*
* @return string
*/
function generateRandomKey()
{
$generator = new ComputerPasswordGenerator();
$generator->setLength(40);
$generator->setSymbols(false);
$generator->setLowercase(true);
$generator->setUppercase(true);
$generator->setNumbers(true);
$key = $generator->generatePasswords();
return $key[0];
}
/**
* Undocumented function
*
* @param string $table
* @param string $type
* @param string $label
* @param string $value
* @return void
*/
function addNewSetting($table, $type, $label, $value): void
{
global $db_link;
// check if setting already exists
$data = mysqli_fetch_row(mysqli_query($db_link, "SELECT COUNT(*) FROM ".$table." WHERE type = '".$type."' AND intitule = '".$label."'"));
if ((int) $data[0] === 0) {
// add setting
mysqli_query(
$db_link,
"INSERT INTO ".$table."
(`type`, `intitule`, `valeur`)
VALUES ('".$type."', '".$label."', '".$value."')"
);
}
}
/**
* Permits to remove a setting
*
* @param string $table
* @param string $type
* @param string $label
* @return void
*/
function removeSetting($table, $type, $label): void
{
global $db_link;
// check if setting already exists
$data = mysqli_fetch_row(mysqli_query($db_link, "SELECT COUNT(*) FROM ".$table." WHERE type = '".$type."' AND intitule = '".$label."'"));
if ((int) $data[0] === 1) {
// delete setting
mysqli_query(
$db_link,
"DELETE FROM ".$table."
WHERE type = '".$type."' AND intitule = '".$label."'"
);
}
}
/**
* Permits to change a column name if exists
*
* @param string $table
* @param string $oldName
* @param string $newName
* @param string $type
* @return void
*/
function changeColumnName($table, $oldName, $newName, $type): void
{
global $db_link;
// check if column already exists
$columns = mysqli_query($db_link, "show columns from `" . $table . "`");
while ($col = mysqli_fetch_assoc($columns)) {
if ((string) $col['Field'] === $oldName) {
// change column name
mysqli_query(
$db_link,
"ALTER TABLE ".$table." CHANGE `".$oldName."` `".$newName."` ".$type
);
break;
}
}
}
/**
* Permits to locate the php binary
*
* @return array
*/
function findPhpBinary(): array
{
$phpPath = '';
// Essayer de trouver le fichier binaire de PHP dans les chemins de recherche standards
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
$phpBinary = $path . DIRECTORY_SEPARATOR . 'php';
if (is_executable($phpBinary)) {
$phpPath = $phpBinary;
break;
}
}
// Si le fichier binaire de PHP n'a pas été trouvé, on essaie de le chercher via les variables d'environnement
if (!$phpPath && getenv('PHP_BINARY')) {
$phpPath = getenv('PHP_BINARY');
}
// Si on n'a toujours pas trouvé le fichier binaire de PHP, on lance une exception
if (!$phpPath) {
return [
'path' => '',
'error' => true,
];
}
return [
'path' => $phpPath,
'error' => false,
];
}
/**
* delete all files and directories
*
* @param array $folders
* @return void
*/
function deleteAll(array $folders)
{
foreach($folders as $folder) {
deleteAllFolder($folder);
}
}
/**
* Delete recursively a folder
*
* @param string $str
* @return void
*/
function deleteAllFolder(string $str)
{
// Check for files
if (is_file($str)) {
// If it is file then remove by
// using unlink function
@unlink($str);
}
// If it is a directory.
elseif (is_dir($str)) {
// Get the list of the files in this
// directory
$scan = glob(rtrim($str, '/').'/*');
// Loop through the list of files
foreach($scan as $index=>$path) {
// Call recursive function
deleteAllFolder($path);
}
// Remove the directory itself
@rmdir($str);
}
}