dolibarr 24.0.3
utils.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.fr>
5 * Copyright (C) 2023-2024 William Mead <william.mead@manchenumerique.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
33class Utils
34{
38 public $db;
39
44 public $error;
45
49 public $errors;
50
54 public $output;
55
59 public $result;
60
66 public function __construct($db)
67 {
68 $this->db = $db;
69 }
70
71
80 public function purgeFiles($choices = 'tempfilesold+logfiles', $nbsecondsold = 86400)
81 {
82 global $conf, $langs, $user;
83 global $dolibarr_main_data_root;
84
85 $langs->load("admin");
86
87 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
88
89 if (empty($choices)) {
90 $choices = 'tempfilesold+logfiles';
91 }
92 if ($choices == 'allfiles' && $nbsecondsold > 0) {
93 $choices = 'allfilesold';
94 }
95
96 dol_syslog("Utils::purgeFiles choice=".$choices, LOG_DEBUG);
97
98 // For dangerous action, we check the user is admin
99 if (in_array($choices, array('allfiles', 'allfilesold'))) {
100 if (empty($user->admin)) {
101 $this->output = 'Error: to erase data files, user running the batch (currently '.$user->login.') must be an admin user';
102 return 1;
103 }
104 }
105
106 $count = 0;
107 $countdeleted = 0;
108 $counterror = 0;
109 $filelog = '';
110
111 $choicesarray = preg_split('/[\+,]/', $choices);
112 foreach ($choicesarray as $choice) {
113 $now = dol_now();
114 $filesarray = array();
115
116 if ($choice == 'tempfiles' || $choice == 'tempfilesold') {
117 // Delete temporary files
118 if ($dolibarr_main_data_root) {
119 $filesarray = dol_dir_list($dolibarr_main_data_root, "directories", 1, '^temp$', '', 'name', SORT_ASC, 2, 0, '', 1); // Do not follow symlinks
120
121 if ($choice == 'tempfilesold') {
122 foreach ($filesarray as $key => $val) {
123 if ($val['date'] > ($now - ($nbsecondsold))) {
124 unset($filesarray[$key]); // Discard temp dir if modified during the last $nbsecondsold
125 }
126 }
127 }
128 }
129 }
130
131 if ($choice == 'allfiles') {
132 // Delete all files (except .lock and .unlock files, do not follow symbolic links)
133 if ($dolibarr_main_data_root) {
134 $filesarray = dol_dir_list($dolibarr_main_data_root, "all", 0, '', '(\.lock|\.unlock)$', 'name', SORT_ASC, 0, 0, '', 1); // No need to use recursive, we will delete directory
135 }
136 }
137
138 if ($choice == 'allfilesold') {
139 // Delete all files (except .lock and .unlock files, do not follow symbolic links)
140 if ($dolibarr_main_data_root) {
141 $filesarray = dol_dir_list($dolibarr_main_data_root, "files", 1, '', '(\.lock|\.unlock)$', 'name', SORT_ASC, 0, 0, '', 1, $nbsecondsold); // No need to use recursive, we will delete directory
142 }
143 }
144
145 if ($choice == 'logfile' || $choice == 'logfiles') {
146 // Define files log
147 if ($dolibarr_main_data_root) {
148 $filesarray = dol_dir_list($dolibarr_main_data_root, "files", 0, '.*\.log[\.0-9]*(\.gz)?$', '(\.lock|\.unlock)$', 'name', SORT_ASC, 0, 0, '', 1);
149 }
150
151 if (isModEnabled('syslog')) {
152 $filelog = getDolGlobalString('SYSLOG_FILE');
153 $filelog = preg_replace('/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
154
155 $alreadyincluded = false;
156 foreach ($filesarray as $tmpcursor) {
157 if ($tmpcursor['fullname'] == $filelog) {
158 $alreadyincluded = true;
159 }
160 }
161 if (!$alreadyincluded) {
162 $filesarray[] = array('fullname' => $filelog, 'type' => 'file');
163 }
164 }
165 }
166
167 if (is_array($filesarray) && count($filesarray)) {
168 foreach ($filesarray as $key => $value) {
169 //print "x ".$filesarray[$key]['fullname']."-".$filesarray[$key]['type']."<br>\n";
170 if ($filesarray[$key]['type'] == 'dir') {
171 $startcount = 0;
172 $tmpcountdeleted = 0;
173
174 $result = dol_delete_dir_recursive($filesarray[$key]['fullname'], $startcount, 1, 0, $tmpcountdeleted);
175
176 $excluded = [
177 $conf->user->dir_temp,
178 ];
179 if (isModEnabled('api')) {
180 $excluded[] = $conf->api->dir_temp;
181 }
182 // The 2 directories $conf->api->dir_temp and $conf->user->dir_temp are recreated at end, so we do not count them
183 if (!in_array($filesarray[$key]['fullname'], $excluded)) {
184 $count += $result;
185 $countdeleted += $tmpcountdeleted;
186 }
187 } elseif ($filesarray[$key]['type'] == 'file') {
188 if ($choice != 'allfilesold' || $filesarray[$key]['date'] < ($now - $nbsecondsold)) {
189 // If (file that is not logfile) or (if mode is logfile)
190 if ($filesarray[$key]['fullname'] != $filelog || $choice == 'logfile' || $choice == 'logfiles') {
191 $result = dol_delete_file($filesarray[$key]['fullname'], 1, 1);
192 if ($result) {
193 $count++;
194 $countdeleted++;
195 } else {
196 $counterror++;
197 }
198 }
199 }
200 }
201 }
202
203 // Update cachenbofdoc
204 if (isModEnabled('ecm') && $choice == 'allfiles') {
205 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
206 $ecmdirstatic = new EcmDirectory($this->db);
207 $result = $ecmdirstatic->refreshcachenboffile(1);
208 }
209 }
210 }
211
212 if ($count > 0) {
213 $langs->load("admin");
214 $this->output = $langs->trans("PurgeNDirectoriesDeleted", $countdeleted);
215 if ($count > $countdeleted) {
216 $this->output .= '<br>'.$langs->trans("PurgeNDirectoriesFailed", ($count - $countdeleted));
217 }
218 } else {
219 $this->output = $langs->trans("PurgeNothingToDelete").(in_array('tempfilesold', $choicesarray) ? ' (older than 24h for temp files)' : '');
220 }
221
222 // Recreate temp dir that are not automatically recreated by core code, we need them
223 if (isModEnabled('api')) {
224 // We should create also dir x/api/temp for multicompany dirs, but this has become useless because dir is now recreated by constructor of api.class.php
225 dol_mkdir($conf->api->dir_temp);
226 }
227 dol_mkdir($conf->user->dir_temp);
228
229 //return $count;
230 return 0; // This function can be called by cron so must return 0 if OK
231 }
232
233
240 public function getShellPasswordParam($password)
241 {
242 if (preg_match('/^win/i', PHP_OS)) {
243 // Behavior kept as it was on Windows
244 return '-p"'.str_replace(array('"', '`', '$'), array('\\"', '\\`', '\\$'), $password).'"';
245 }
246
247 // Inside double quotes, a shell still interprets \, $ and ` so we use single quotes
248 return '-p'.escapeshellarg($password);
249 }
250
251
265 public function dumpDatabase($compression = 'none', $type = 'auto', $usedefault = 1, $file = 'auto', $keeplastnfiles = 0, $execmethod = 0, $lowmemorydump = 0)
266 {
267 global $db, $conf, $langs;
268 global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
269 global $dolibarr_main_db_character_set;
270 global $dolibarr_main_restrict_os_commands;
271
272 $langs->load("admin");
273
274 dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG);
275 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
276
277 // Clean data
278 $file = dol_sanitizeFileName($file);
279
280 // Check compression parameter
281 if (!in_array($compression, array('none', 'gz', 'bz', 'zip', 'zstd'))) {
282 $langs->load("errors");
283 $this->error = $langs->transnoentitiesnoconv("ErrorBadValueForParameter", $compression, "Compression");
284 return -1;
285 }
286
287 // Check type parameter
288 if ($type == 'auto') {
289 $type = $this->db->type;
290 }
291 if (!in_array($type, array('postgresql', 'pgsql', 'mysql', 'mysqli', 'mysqlnobin'))) {
292 $langs->load("errors");
293 $this->error = $langs->transnoentitiesnoconv("ErrorBadValueForParameter", $type, "Basetype");
294 return -1;
295 }
296
297 // Check file parameter
298 if ($file == 'auto') {
299 $prefix = 'dump';
300 $ext = 'sql';
301 if (in_array($type, array('mysql', 'mysqli'))) {
302 $prefix = 'mysqldump';
303 }
304 //if ($label == 'PostgreSQL') { $prefix='pg_dump'; $ext='dump'; }
305 if (in_array($type, array('pgsql'))) {
306 $prefix = 'pg_dump';
307 }
308 $file = $prefix.'_'.$dolibarr_main_db_name.'_'.dol_sanitizeFileName(DOL_VERSION).'_'.dol_print_date(dol_now('gmt'), "dayhourlogsmall", 'tzuser').'.'.$ext;
309 }
310
311 $outputdir = $conf->admin->dir_output.'/backup';
312 $result = dol_mkdir($outputdir);
313 $errormsg = '';
314
315 // MYSQL
316 if ($type == 'mysql' || $type == 'mysqli') {
317 if (!getDolGlobalString('SYSTEMTOOLS_MYSQLDUMP')) {
318 $cmddump = $db->getPathOfDump();
319 } else {
320 $cmddump = getDolGlobalString('SYSTEMTOOLS_MYSQLDUMP');
321 }
322 if (empty($cmddump)) {
323 $this->error = "Failed to detect command to use for mysqldump. Try a manual backup before to set path of command.";
324 return -1;
325 }
326
327 $outputfile = $outputdir.'/'.$file;
328 // for compression format, we add extension
329 $compression = $compression ? $compression : 'none';
330 if ($compression == 'gz') {
331 $outputfile .= '.gz';
332 } elseif ($compression == 'bz') {
333 $outputfile .= '.bz2';
334 } elseif ($compression == 'zstd') {
335 $outputfile .= '.zst';
336 }
337 $outputerror = $outputfile.'.err';
338 dol_mkdir($conf->admin->dir_output.'/backup');
339
340 $cmddump = dol_sanitizePathName($cmddump); // Sanitize path
341 $cmddump = dol_string_nospecial($cmddump, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
342 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
343
344 // Sanitize and validate $cmddump
345 if (!empty($dolibarr_main_restrict_os_commands)) {
346 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
347 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
348 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
349 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
350 $langs->load("errors");
351 $this->error = $langs->trans('CommandIsNotInsideAllowedCommands');
352 $this->error .= '<br>'.$langs->trans('ErrorCheckTheCommandInsideTheAdvancedOptions');
353 return -1;
354 }
355 }
356
357 // Parameters execution
358 $command = $cmddump;
359 $command = preg_replace('/(\$|%)/', '', $command); // We removed chars that can be used to inject vars that contains space inside path of command without seeing there is a space to bypass the escapeshellarg.
360 if (preg_match("/\s/", $command)) {
361 $command = escapeshellarg($command); // If there is spaces, we add quotes on command to be sure $command is only a program and not a program+parameters
362 }
363
364 //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
365 $param = $dolibarr_main_db_name." -h ".$dolibarr_main_db_host;
366 $param .= " -u ".$dolibarr_main_db_user;
367 if (!empty($dolibarr_main_db_port)) {
368 $param .= " -P ".$dolibarr_main_db_port." --protocol=tcp";
369 }
370 if (GETPOST("use_transaction", "alpha")) {
371 $param .= " --single-transaction";
372 }
373 if (GETPOST("disable_fk", "alpha") || $usedefault) {
374 $param .= " -K";
375 }
376 if (GETPOST("sql_compat", "alpha") && GETPOST("sql_compat", "alpha") != 'NONE') {
377 $param .= " --compatible=".escapeshellarg(GETPOST("sql_compat", "alpha"));
378 }
379 if (GETPOST("drop_database", "alpha")) {
380 $param .= " --add-drop-database";
381 }
382 if (GETPOST("use_mysql_quick_param", "alpha")) {
383 $param .= " --quick";
384 }
385 if (GETPOST("use_force", "alpha")) {
386 $param .= " -f";
387 }
388 if (GETPOST("sql_structure", "alpha") || $usedefault) {
389 if (GETPOST("drop", "alpha") || $usedefault) {
390 $param .= " --add-drop-table=TRUE";
391 } else {
392 $param .= " --add-drop-table=FALSE";
393 }
394 } else {
395 $param .= " -t";
396 }
397 if (GETPOST("disable-add-locks", "alpha")) {
398 $param .= " --add-locks=FALSE";
399 }
400 if (GETPOST("sql_data", "alpha") || $usedefault) {
401 $param .= " --tables";
402 if (GETPOST("showcolumns", "alpha") || $usedefault) {
403 $param .= " -c";
404 }
405 if (GETPOST("extended_ins", "alpha") || $usedefault) {
406 $param .= " -e";
407 } else {
408 $param .= " --skip-extended-insert";
409 }
410 if (GETPOST("delayed", "alpha")) {
411 $param .= " --delayed-insert";
412 }
413 if (GETPOST("sql_ignore", "alpha")) {
414 $param .= " --insert-ignore";
415 }
416 if (GETPOST("hexforbinary", "alpha") || $usedefault) {
417 $param .= " --hex-blob";
418 }
419 } else {
420 $param .= " -d"; // No row information (no data)
421 }
422 if ($dolibarr_main_db_character_set == 'utf8mb4') {
423 // We save output into utf8mb4 charset
424 $param .= " --default-character-set=utf8mb4 --no-tablespaces";
425 } else {
426 $param .= " --default-character-set=utf8 --no-tablespaces"; // We always save output into utf8 charset
427 }
428 $paramcrypted = $param;
429 $paramclear = $param;
430 if (!empty($dolibarr_main_db_pass)) {
431 $paramcrypted .= ' -p"'.preg_replace('/./i', '*', $dolibarr_main_db_pass).'"';
432 $paramclear .= ' '.$this->getShellPasswordParam($dolibarr_main_db_pass);
433 }
434
435 $handle = '';
436
437 // Start call method to execute dump
438 $fullcommandcrypted = $command." ".$paramcrypted." 2>&1";
439 $fullcommandclear = $command." ".$paramclear." 2>&1";
440 if (!$lowmemorydump) {
441 if ($compression == 'none') {
442 $handle = fopen($outputfile, 'w');
443 } elseif ($compression == 'gz') {
444 $handle = gzopen($outputfile, 'w');
445 } elseif ($compression == 'bz') {
446 $handle = bzopen($outputfile, 'w');
447 } elseif ($compression == 'zstd') {
448 $handle = fopen("compress.zstd://" . $outputfile, "wb");
449 }
450 } else {
451 // TODO Add a pipe into script to decrypt dolCrypted values
452
453 if ($compression == 'none') {
454 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).'"';
455 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).'"';
456 $handle = 1;
457 } elseif ($compression == 'gz') {
458 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).'"';
459 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).'"';
460 $paramcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip';
461 $handle = 1;
462 } elseif ($compression == 'bz') {
463 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).'"';
464 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).'"';
465 $paramcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2';
466 $handle = 1;
467 } elseif ($compression == 'zstd') {
468 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).'"';
469 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).'"';
470 $paramcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd';
471 $handle = 1;
472 }
473 }
474
475 $ok = 0;
476 if ($handle) {
477 if (getDolGlobalString('MAIN_EXEC_USE_POPEN')) {
478 $execmethod = getDolGlobalString('MAIN_EXEC_USE_POPEN');
479 }
480 if (empty($execmethod)) {
481 $execmethod = 1;
482 }
483
484 dol_syslog("Utils::dumpDatabase execmethod=".$execmethod.", lowmemorydump=".$lowmemorydump.", command=".$fullcommandcrypted, LOG_INFO);
485
486
487 /* If value has been forced with a php_admin_value, this has no effect. Example of value: '512M' */
488 $MemoryLimit = getDolGlobalString('MAIN_MEMORY_LIMIT_DUMP');
489 if (!empty($MemoryLimit)) {
490 @ini_set('memory_limit', $MemoryLimit);
491 }
492
493
494 if ($execmethod == 1) {
495 $output_arr = array();
496 $retval = null;
497
498 exec($fullcommandclear, $output_arr, $retval); // @phan-suppress-current-line PhanPluginConstantVariableNull
499 // TODO Replace this exec with Utils->executeCLI() function.
500 // We must check that the case for $lowmemorydump works too...
501 //$utils = new Utils($db);
502 //$outputfile = $conf->admin->dir_temp.'/dump.tmp';
503 //$utils->executeCLI($fullcommandclear, $outputfile, 0);
504
505 if ($retval != 0) {
506 $langs->load("errors");
507 dol_syslog("Datadump retval after exec=".$retval, LOG_ERR);
508 $errormsg = 'Error '.$retval;
509 $ok = 0;
510 } else {
511 $i = 0;
512 if (!empty($output_arr)) {
513 foreach ($output_arr as $key => $read) {
514 $i++; // output line number
515 if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) {
516 continue;
517 }
518 // Now check into the result file, that the file end with "-- Dump completed"
519 // This is possible only if $output_arr is the clear dump file, so not possible with $lowmemorydump set because file is already compressed.
520 if (!$lowmemorydump) {
521 // TODO decrypt dolCrypted values from $read
522
523 fwrite($handle, $read.($execmethod == 2 ? '' : "\n"));
524 if (preg_match('/'.preg_quote('-- Dump completed', '/').'/i', $read)) {
525 $ok = 1;
526 } elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES', '/').'/i', $read)) {
527 $ok = 1;
528 }
529 } else {
530 // If we have a result here in lowmemorydump mode, something is strange
531 }
532 }
533 } elseif ($lowmemorydump) {
534 $ok = 1;
535 }
536 }
537 }
538
539 if ($execmethod == 2) { // With this method, there is no way to get the return code, only output
540 $handlein = popen($fullcommandclear, 'r');
541 $i = 0;
542 if ($handlein) {
543 while (!feof($handlein)) {
544 $i++; // output line number
545 $read = fgets($handlein);
546 // Exclude warning line we don't want
547 if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) {
548 continue;
549 }
550 fwrite($handle, $read);
551 if (preg_match('/'.preg_quote('-- Dump completed', '/').'/i', $read)) {
552 $ok = 1;
553 } elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES', '/').'/i', $read)) {
554 $ok = 1;
555 }
556 }
557 pclose($handlein);
558 }
559 }
560
561 if (!$lowmemorydump) {
562 if ($compression == 'none') {
563 fclose($handle);
564 } elseif ($compression == 'gz') {
565 gzclose($handle);
566 } elseif ($compression == 'bz') {
567 fclose($handle);
568 } elseif ($compression == 'zstd') {
569 fclose($handle);
570 }
571 }
572
573 dolChmod($outputfile);
574 } else {
575 $langs->load("errors");
576 dol_syslog("Failed to open file ".$outputfile, LOG_ERR);
577 $errormsg = $langs->trans("ErrorFailedToWriteInDir");
578 }
579
580 // Get errorstring
581 if ($compression == 'none') {
582 $handle = fopen($outputfile, 'r');
583 } elseif ($compression == 'gz') {
584 $handle = gzopen($outputfile, 'r');
585 } elseif ($compression == 'bz') {
586 $handle = bzopen($outputfile, 'r');
587 } elseif ($compression == 'zstd') {
588 $handle = fopen("compress.zstd://" . $outputfile, "rb");
589 }
590 if ($handle) {
591 // Get 2048 first chars of error message.
592 $errormsg = fgets($handle, 2048);
593 //$ok=0;$errormsg=''; To force error
594
595 // Close file
596 if ($compression == 'none') {
597 fclose($handle);
598 } elseif ($compression == 'gz') {
599 gzclose($handle);
600 } elseif ($compression == 'bz') {
601 fclose($handle);
602 } elseif ($compression == 'zstd') {
603 fclose($handle);
604 }
605 if ($ok && preg_match('/^-- (MySql|MariaDB)/i', $errormsg) || preg_match('/^\/\*M?!999999/', $errormsg)) { // Start of file is ok, NOT an error
606 $errormsg = '';
607 } else {
608 // Rename file out into a file error
609 //print "$outputfile -> $outputerror";
610 @dol_delete_file($outputerror, 1, 0, 0, null, false, 0);
611 @dol_move($outputfile, $outputerror, '0', 1, 0, 0);
612 // Si safe_mode on et command hors du parameter exec, on a un fichier out vide donc errormsg vide
613 if (!$errormsg) {
614 $langs->load("errors");
615 $errormsg = $langs->trans("ErrorFailedToRunExternalCommand");
616 }
617 }
618 }
619 // Fin execution commande
620
621 $this->output = $errormsg;
622 $this->error = $errormsg;
623 $this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => "");
624 //if (empty($this->output)) $this->output=$this->result['commandbackuplastdone'];
625 }
626
627 // MYSQL NO BIN
628 if ($type == 'mysqlnobin') {
629 $outputfile = $outputdir.'/'.$file;
630 $outputfiletemp = $outputfile.'-TMP.sql';
631 // for compression format, we add extension
632 $compression = $compression ? $compression : 'none';
633 if ($compression == 'gz') {
634 $outputfile .= '.gz';
635 }
636 if ($compression == 'bz') {
637 $outputfile .= '.bz2';
638 }
639 $outputerror = $outputfile.'.err';
640 dol_mkdir($conf->admin->dir_output.'/backup');
641
642 if ($compression == 'gz' or $compression == 'bz') {
643 $this->backupTables($outputfiletemp);
644 dol_compress_file($outputfiletemp, $outputfile, $compression);
645 unlink($outputfiletemp);
646 } else {
647 $this->backupTables($outputfile);
648 }
649
650 $this->output = "";
651 $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => "");
652 }
653
654 // POSTGRESQL
655 if ($type == 'postgresql' || $type == 'pgsql') {
656 $cmddump = getDolGlobalString('SYSTEMTOOLS_POSTGRESQLDUMP');
657
658 $outputfile = $outputdir.'/'.$file;
659 // for compression format, we add extension
660 $compression = $compression ? $compression : 'none';
661 if ($compression == 'gz') {
662 $outputfile .= '.gz';
663 }
664 if ($compression == 'bz') {
665 $outputfile .= '.bz2';
666 }
667 $outputerror = $outputfile.'.err';
668 dol_mkdir($conf->admin->dir_output.'/backup');
669
670 // Sanitize and validate $cmddump
671 $cmddump = dol_sanitizePathName($cmddump); // Sanitize path
672 $cmddump = dol_string_nospecial($cmddump, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
673 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
674
675 if (!empty($dolibarr_main_restrict_os_commands)) {
676 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
677 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
678 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
679 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
680 $langs->load("errors");
681 $this->error = $langs->trans('CommandIsNotInsideAllowedCommands');
682 $this->error .= '<br>'.$langs->trans('ErrorCheckTheCommandInsideTheAdvancedOptions');
683 return -1;
684 }
685 }
686
687 // Parameters execution
688 $command = $cmddump;
689 $command = preg_replace('/(\$|%)/', '', $command); // We removed chars that can be used to inject vars that contains space inside path of command without seeing there is a space to bypass the escapeshellarg.
690 if (preg_match("/\s/", $command)) {
691 $command = escapeshellarg($command); // If there is spaces, we add quotes on command to be sure $command is only a program and not a program+parameters
692 }
693
694 //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
695 //$param="-F c";
696 $param = "-F p";
697 $param .= " --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
698 $param .= " -U ".$dolibarr_main_db_user;
699 if (!empty($dolibarr_main_db_port)) {
700 $param .= " -p ".$dolibarr_main_db_port;
701 }
702 if (GETPOST("sql_compat") && GETPOST("sql_compat") == 'ANSI') {
703 $param .= " --disable-dollar-quoting";
704 }
705 if (GETPOST("drop_database")) {
706 $param .= " -c -C";
707 }
708 if (GETPOST("sql_structure")) {
709 if (GETPOST("drop")) {
710 $param .= " --add-drop-table";
711 }
712 if (!GETPOST("sql_data")) {
713 $param .= " -s";
714 }
715 }
716 if (GETPOST("sql_data")) {
717 if (!GETPOST("sql_structure")) {
718 $param .= " -a";
719 }
720 if (GETPOST("showcolumns")) {
721 $param .= " -c";
722 }
723 }
724 $param .= ' -f "'.$outputfile.'"';
725 //if ($compression == 'none')
726 if ($compression == 'gz') {
727 $param .= ' -Z 9';
728 }
729 //if ($compression == 'bz')
730 $paramcrypted = $param;
731 $paramclear = $param;
732 /*if (!empty($dolibarr_main_db_pass))
733 {
734 $paramcrypted.=" -W".preg_replace('/./i','*',$dolibarr_main_db_pass);
735 $paramclear.=" -W".$dolibarr_main_db_pass;
736 }*/
737 $paramcrypted .= " -w ".$dolibarr_main_db_name;
738 $paramclear .= " -w ".$dolibarr_main_db_name;
739
740 $this->output = "";
741 $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => $command." ".$paramcrypted);
742 }
743
744 // Clean old files
745 if (!$errormsg && $keeplastnfiles > 0) {
746 $tmpfiles = dol_dir_list($conf->admin->dir_output.'/backup', 'files', 0, '', '(\.err|\.old|\.sav)$', 'date', SORT_DESC);
747 $i = 0;
748 if (is_array($tmpfiles)) {
749 foreach ($tmpfiles as $key => $val) {
750 $i++;
751 if ($i <= $keeplastnfiles) {
752 continue;
753 }
754 dol_delete_file($val['fullname'], 0, 0, 0, null, false, 0);
755 }
756 }
757 }
758
759 return ($errormsg ? -1 : 0);
760 }
761
762
763
777 public function executeCLI($command, $outputfile, $execmethod = 0, $redirectionfile = null, $noescapecommand = 0, $redirectionfileerr = null)
778 {
779 global $langs;
780
781 $result = 0;
782 $output = '';
783 $error = '';
784
785 global $dolibarr_main_restrict_os_commands;
786 if (!empty($dolibarr_main_restrict_os_commands)) {
787 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
788 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
789
790 $commands = preg_split('/\|\||&&|\||&/', $command);
791 foreach ($commands as $newcommand) {
792 $newcommand = trim($newcommand);
793 $matches = array();
794 // If command is "'ab cd.exe' param1 param2" or '"ab cd.exe" param1 param2' or 'abcd.exe param1 param2', we must extract ab...cd.exe only.
795 if (preg_match('/^(?:\'([^\']*)\'|"([^"]*)"|(\S+))/', $newcommand, $matches)) {
796 $newcommand = $matches[1] ?: ($matches[2] ?: $matches[3]);
797 }
798 if (!in_array(basename($newcommand), $arrayofallowedcommand)) {
799 dol_syslog("files.lib.php::executeCLI canceled because target filename ".basename($newcommand)." is not in the whitelist of allowed commands.", LOG_WARNING);
800 return array('result' => -1, 'output' => '', 'error' => 'Command '.basename($newcommand).' is not in the whitelist of allowed commands');
801 }
802 }
803 }
804
805 if (empty($noescapecommand)) {
806 $command = escapeshellcmd($command);
807 }
808
809 if ($redirectionfile) {
810 $command .= " > ".dol_sanitizePathName($redirectionfile);
811 }
812
813 if ($redirectionfileerr && ($redirectionfileerr != $redirectionfile)) {
814 // If we ask a redirect of stderr on a given file not already used for stdout
815 $command .= " 2> ".dol_sanitizePathName($redirectionfileerr);
816 } else {
817 $command .= " 2>&1";
818 }
819
820 if (getDolGlobalString('MAIN_EXEC_USE_POPEN')) {
821 $execmethod = getDolGlobalString('MAIN_EXEC_USE_POPEN');
822 }
823 if (empty($execmethod)) {
824 $execmethod = 1;
825 }
826 //$execmethod=1;
827 dol_syslog("Utils::executeCLI execmethod=".$execmethod." command=".$command, LOG_DEBUG);
828 $output_arr = array();
829
830 if ($execmethod == 1) {
831 $retval = null;
832 exec($command, $output_arr, $retval); // @phan-suppress-current-line PhanPluginConstantVariableNull
833 $result = $retval;
834 if ($retval != 0) {
835 $langs->load("errors");
836 dol_syslog("Utils::executeCLI retval after exec=".$retval, LOG_ERR);
837 $error = 'Error '.$retval;
838 }
839 }
840 if ($execmethod == 2) { // With this method, there is no way to get the return code, only output
841 $handle = fopen($outputfile, 'w+b');
842 if ($handle) {
843 dol_syslog("Utils::executeCLI run command ".$command);
844 $handlein = popen($command, 'r');
845 while (!feof($handlein)) {
846 $read = fgets($handlein);
847 fwrite($handle, $read);
848 $output_arr[] = $read;
849 }
850 pclose($handlein);
851 fclose($handle);
852 }
853 dolChmod($outputfile);
854 }
855
856 // Update with result
857 if (is_array($output_arr) && count($output_arr) > 0) {
858 foreach ($output_arr as $val) {
859 $output .= $val.($execmethod == 2 ? '' : "\n");
860 }
861 }
862
863 dol_syslog("Utils::executeCLI result=".$result." output=".$output." error=".$error, LOG_DEBUG);
864
865 return array('result' => $result, 'output' => $output, 'error' => $error);
866 }
867
874 public function generateDoc($module)
875 {
876 global $langs, $user, $mysoc;
877 global $dirins;
878
879 $error = 0;
880
881 $modulelowercase = strtolower($module);
882 $now = dol_now();
883
884 // Dir for module
885 $dir = $dirins.'/'.$modulelowercase;
886 // Zip file to build
887 $FILENAMEDOC = '';
888
889 // Load module
890 dol_include_once($modulelowercase.'/core/modules/mod'.$module.'.class.php');
891 $class = 'mod'.$module;
892
893 $moduleobj = null;
894 if (class_exists($class)) {
895 try {
896 $moduleobj = new $class($this->db);
897 } catch (Exception $e) {
898 $error++;
899 dol_print_error(null, $e->getMessage());
900 }
901 } else {
902 $error++;
903 $langs->load("errors");
904 dol_print_error(null, $langs->trans("ErrorFailedToLoadModuleDescriptorForXXX", $module));
905 exit;
906 }
907
908 $arrayversion = $moduleobj === null ? array() : explode('.', $moduleobj->version, 3);
909 if (count($arrayversion)) {
910 $FILENAMEASCII = strtolower($module).'.asciidoc';
911 $FILENAMEDOC = strtolower($module).'.html';
912 $FILENAMEDOCPDF = strtolower($module).'.pdf';
913
914 $dirofmodule = dol_buildpath(strtolower($module), 0);
915 $dirofmoduledoc = dol_buildpath(strtolower($module), 0).'/doc';
916 $dirofmoduletmp = dol_buildpath(strtolower($module), 0).'/doc/temp';
917 $outputfiledoc = $dirofmoduledoc.'/'.$FILENAMEDOC;
918 if ($dirofmoduledoc) {
919 if (!dol_is_dir($dirofmoduledoc)) {
920 dol_mkdir($dirofmoduledoc);
921 }
922 if (!dol_is_dir($dirofmoduletmp)) {
923 dol_mkdir($dirofmoduletmp);
924 }
925 if (!is_writable($dirofmoduletmp)) {
926 $this->error = 'Dir '.$dirofmoduletmp.' does not exists or is not writable';
927 return -1;
928 }
929
930 if (!getDolGlobalString('MODULEBUILDER_ASCIIDOCTOR') && !getDolGlobalString('MODULEBUILDER_ASCIIDOCTORPDF')) {
931 $this->error = 'Setup of module ModuleBuilder not complete';
932 return -1;
933 }
934
935 // Copy some files into temp directory, so instruction include::ChangeLog.md[] will works inside the asciidoc file.
936 dol_copy($dirofmodule.'/README.md', $dirofmoduletmp.'/README.md', '0', 1);
937 dol_copy($dirofmodule.'/ChangeLog.md', $dirofmoduletmp.'/ChangeLog.md', '0', 1);
938
939 // Replace into README.md and ChangeLog.md (in case they are included into documentation with tag __README__ or __CHANGELOG__)
940 $arrayreplacement = array();
941 $arrayreplacement['/^#\s.*/m'] = ''; // Remove first level of title into .md files
942 $arrayreplacement['/^#/m'] = '##'; // Add on # to increase level
943
944 dolReplaceInFile($dirofmoduletmp.'/README.md', $arrayreplacement, '', '0', 0, 1);
945 dolReplaceInFile($dirofmoduletmp.'/ChangeLog.md', $arrayreplacement, '', '0', 0, 1);
946
947
948 $destfile = $dirofmoduletmp.'/'.$FILENAMEASCII;
949
950 $fhandle = fopen($destfile, 'w+');
951 if ($fhandle) {
952 $specs = dol_dir_list(dol_buildpath(strtolower($module).'/doc', 0), 'files', 1, '(\.md|\.asciidoc)$', array('\/temp\/'));
953
954 $i = 0;
955 foreach ($specs as $spec) {
956 if (preg_match('/notindoc/', $spec['relativename'])) {
957 continue; // Discard file
958 }
959 if (preg_match('/example/', $spec['relativename'])) {
960 continue; // Discard file
961 }
962 if (preg_match('/disabled/', $spec['relativename'])) {
963 continue; // Discard file
964 }
965
966 $pathtofile = strtolower($module).'/doc/'.$spec['relativename'];
967 $format = 'asciidoc';
968 if (preg_match('/\.md$/i', $spec['name'])) {
969 $format = 'markdown';
970 }
971
972 $filecursor = @file_get_contents($spec['fullname']);
973 if ($filecursor) {
974 fwrite($fhandle, ($i ? "\n<<<\n\n" : "").$filecursor."\n");
975 } else {
976 $this->error = 'Failed to concat content of file '.$spec['fullname'];
977 return -1;
978 }
979
980 $i++;
981 }
982
983 fclose($fhandle);
984
985 $contentreadme = file_get_contents($dirofmoduletmp.'/README.md');
986 $contentchangelog = file_get_contents($dirofmoduletmp.'/ChangeLog.md');
987
988 include DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
989
990 //var_dump($phpfileval['fullname']);
991 $arrayreplacement = array(
992 'mymodule' => strtolower($module),
993 'MyModule' => $module,
994 'MYMODULE' => strtoupper($module),
995 'My module' => $module,
996 'my module' => $module,
997 'Mon module' => $module,
998 'mon module' => $module,
999 'htdocs/modulebuilder/template' => strtolower($module),
1000 '__MYCOMPANY_NAME__' => $mysoc->name,
1001 '__KEYWORDS__' => $module,
1002 '__USER_FULLNAME__' => $user->getFullName($langs),
1003 '__USER_EMAIL__' => $user->email,
1004 '__YYYY-MM-DD__' => dol_print_date($now, 'dayrfc'),
1005 '---Put here your own copyright and developer email---' => dol_print_date($now, 'dayrfc').' '.$user->getFullName($langs).($user->email ? ' <'.$user->email.'>' : ''),
1006 '__DATA_SPECIFICATION__' => 'Not yet available',
1007 '__README__' => dolMd2Asciidoc($contentreadme),
1008 '__CHANGELOG__' => dolMd2Asciidoc($contentchangelog),
1009 );
1010
1011 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1012 dolReplaceInFile($destfile, $arrayreplacement);
1013 }
1014
1015 // Launch doc generation
1016 $currentdir = getcwd();
1017 chdir($dirofmodule);
1018
1019 require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1020 $utils = new Utils($this->db);
1021
1022 // Build HTML doc
1023 $command = getDolGlobalString('MODULEBUILDER_ASCIIDOCTOR') . ' '.$destfile.' -n -o '.$dirofmoduledoc.'/'.$FILENAMEDOC;
1024 $outfile = $dirofmoduletmp.'/out.tmp';
1025
1026 $resarray = $utils->executeCLI($command, $outfile);
1027 if ($resarray['result'] != '0') {
1028 $this->error = $resarray['error'].' '.$resarray['output'];
1029 $this->errors[] = $this->error;
1030 }
1031 $result = ($resarray['result'] == 0) ? 1 : 0;
1032 if ($result < 0 && empty($this->errors)) {
1033 $this->error = $langs->trans("ErrorFailToGenerateFile", $FILENAMEDOC);
1034 $this->errors[] = $this->error;
1035 }
1036
1037 // Build PDF doc
1038 $command = getDolGlobalString('MODULEBUILDER_ASCIIDOCTORPDF') . ' '.$destfile.' -n -o '.$dirofmoduledoc.'/'.$FILENAMEDOCPDF;
1039 $outfile = $dirofmoduletmp.'/outpdf.tmp';
1040 $resarray = $utils->executeCLI($command, $outfile);
1041 if ($resarray['result'] != '0') {
1042 $this->error = $resarray['error'].' '.$resarray['output'];
1043 $this->errors[] = $this->error;
1044 }
1045 $result = ($resarray['result'] == 0) ? 1 : 0;
1046 if ($result < 0 && empty($this->errors)) {
1047 $this->error = $langs->trans("ErrorFailToGenerateFile", $FILENAMEDOCPDF);
1048 $this->errors[] = $this->error;
1049 }
1050
1051 chdir($currentdir);
1052 } else {
1053 $result = 0;
1054 }
1055
1056 if ($result > 0) {
1057 return 1;
1058 } else {
1059 $error++;
1060 }
1061 } else {
1062 $error++;
1063 $langs->load("errors");
1064 $this->error = $langs->trans("ErrorCheckVersionIsDefined");
1065 }
1066
1067 return -1;
1068 }
1069
1077 public function compressSyslogs()
1078 {
1079 global $conf;
1080
1081 if (empty($conf->loghandlers['mod_syslog_file'])) { // File Syslog disabled
1082 return 0;
1083 }
1084
1085 if (!function_exists('gzopen')) {
1086 $this->error = 'Support for gzopen not available in this PHP';
1087 return -1;
1088 }
1089
1090 require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
1091
1092 $nbSaves = intval(getDolGlobalString('SYSLOG_FILE_SAVES', 10));
1093
1094 if (!getDolGlobalString('SYSLOG_FILE')) {
1095 $mainlogdir = DOL_DATA_ROOT;
1096 $mainlog = 'dolibarr.log';
1097 } else {
1098 $mainlogfull = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
1099 $mainlogdir = dirname($mainlogfull);
1100 $mainlog = basename($mainlogfull);
1101 }
1102
1103 $tabfiles = dol_dir_list(DOL_DATA_ROOT, 'files', 0, '^(dolibarr_.+|odt2pdf)\.log$'); // Also handle other log files like dolibarr_install.log
1104 $tabfiles[] = array('name' => $mainlog, 'path' => $mainlogdir);
1105
1106 foreach ($tabfiles as $file) {
1107 $logname = $file['name'];
1108 $logpath = $file['path'];
1109
1110 if (dol_is_file($logpath.'/'.$logname) && dol_filesize($logpath.'/'.$logname) > 0) { // If log file exists and is not empty
1111 // Handle already compressed files to rename them and add +1
1112
1113 $filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';
1114
1115 $gzfilestmp = dol_dir_list($logpath, 'files', 0, $filter);
1116 $gzfiles = array();
1117
1118 foreach ($gzfilestmp as $gzfile) {
1119 $tabmatches = array();
1120 preg_match('/'.$filter.'/i', $gzfile['name'], $tabmatches);
1121
1122 $numsave = intval($tabmatches[1]);
1123
1124 $gzfiles[$numsave] = $gzfile;
1125 }
1126
1127 krsort($gzfiles, SORT_NUMERIC);
1128
1129 foreach ($gzfiles as $numsave => $dummy) {
1130 if (dol_is_file($logpath.'/'.$logname.'.'.($numsave + 1).'.gz')) {
1131 return -2;
1132 }
1133
1134 if ($numsave >= $nbSaves) {
1135 dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz', 0, 0, 0, null, false, 0);
1136 } else {
1137 dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave + 1).'.gz', '0', 1, 0, 0);
1138 }
1139 }
1140
1141 // Compress current file and recreate it
1142
1143 if ($nbSaves > 0) { // If $nbSaves is 1, we keep 1 archive .gz file, If 2, we keep 2 .gz files
1144 $gzfilehandle = gzopen($logpath.'/'.$logname.'.1.gz', 'wb9');
1145
1146 if (empty($gzfilehandle)) {
1147 $this->error = 'Failted to open file '.$logpath.'/'.$logname.'.1.gz';
1148 return -3;
1149 }
1150
1151 $sourcehandle = fopen($logpath.'/'.$logname, 'r');
1152
1153 if (empty($sourcehandle)) {
1154 $this->error = 'Failed to open file '.$logpath.'/'.$logname;
1155 return -4;
1156 }
1157
1158 while (!feof($sourcehandle)) {
1159 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024)); // Read 512 kB at a time
1160 }
1161
1162 fclose($sourcehandle);
1163 gzclose($gzfilehandle);
1164
1165 dolChmod($logpath.'/'.$logname.'.1.gz');
1166 }
1167
1168 dol_delete_file($logpath.'/'.$logname, 0, 0, 0, null, false, 0);
1169
1170 // Create empty file
1171 $newlog = fopen($logpath.'/'.$logname, 'a+');
1172 fclose($newlog);
1173
1174 //var_dump($logpath.'/'.$logname." - ".octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
1175 dolChmod($logpath.'/'.$logname);
1176 }
1177 }
1178
1179 $this->output = 'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.' files) done.';
1180 return 0;
1181 }
1182
1193 public function backupTables($outputfile, $tables = '*')
1194 {
1195 global $db, $langs;
1196 global $errormsg;
1197 global $dolibarr_main_db_character_set;
1198
1199 // Set to UTF-8. Use utf8mb4 when the database uses it, as with utf8 the 4-byte
1200 // characters (emojis...) are read as '?' and are lost when the dump is restored.
1201 $charset = ($dolibarr_main_db_character_set == 'utf8mb4' ? 'utf8mb4' : 'utf8');
1202 $sanitizedcharset = $db->sanitize($charset);
1203 if (is_a($db, 'DoliDBMysqli')) {
1205 $db->db->set_charset($charset);
1206 } else {
1208 $db->query('SET NAMES '.$db->sanitize($charset));
1209 $db->query('SET CHARACTER SET '.$db->sanitize($charset));
1210 }
1211
1212 //get all of the tables
1213 if ($tables == '*') {
1214 $tables = array();
1215 $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1216 while ($row = $db->fetch_row($result)) {
1217 $tables[] = $row[0];
1218 }
1219 } else {
1220 $tables = is_array($tables) ? $tables : explode(',', $tables);
1221 }
1222
1223 //cycle through
1224 $handle = fopen($outputfile, 'w+');
1225 if (fwrite($handle, '') === false) {
1226 $langs->load("errors");
1227 dol_syslog("Failed to open file ".$outputfile, LOG_ERR);
1228 $errormsg = $langs->trans("ErrorFailedToWriteInDir");
1229 return -1;
1230 }
1231
1232 // Print headers and global mysql config vars
1233 $sqlhead = '';
1234 $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION."
1235--
1236-- Host: ".$db->db->host_info." Database: ".$db->database_name."
1237-- ------------------------------------------------------
1238-- Server version ".$db->db->server_info."
1239
1240;
1241;
1242;
1243;
1244;
1245;
1246;
1247;
1248;
1249;
1250
1251";
1252
1253 if (GETPOST("nobin_disable_fk")) {
1254 $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n";
1255 }
1256 //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n";
1257 if (GETPOST("nobin_use_transaction")) {
1258 $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1259 }
1260
1261 fwrite($handle, $sqlhead);
1262
1263 $ignore = '';
1264 if (GETPOST("nobin_sql_ignore")) {
1265 $ignore = 'IGNORE ';
1266 }
1267 $delayed = '';
1268 if (GETPOST("nobin_delayed")) {
1269 $delayed = 'DELAYED ';
1270 }
1271
1272 // Process each table and print their definition + their datas
1273 foreach ($tables as $table) {
1274 // Saving the table structure
1275 fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n");
1276
1277 if (GETPOST("nobin_drop")) {
1278 fwrite($handle, "DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it
1279 }
1280 fwrite($handle, "/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1281 fwrite($handle, "/*!40101 SET character_set_client = ".$sanitizedcharset." */;\n");
1282 $resqldrop = $db->query('SHOW CREATE TABLE '.$table);
1283 $row2 = $db->fetch_row($resqldrop);
1284 if (empty($row2[1])) {
1285 fwrite($handle, "\n-- WARNING: Show create table ".$table." return empty string when it should not.\n");
1286 } else {
1287 fwrite($handle, $row2[1].";\n");
1288 //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n");
1289
1290 // Dumping the data (locking the table and disabling the keys check while doing the process)
1291 fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n");
1292 if (!GETPOST("nobin_nolocks")) {
1293 fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back)
1294 }
1295 if (GETPOST("nobin_disable_fk")) {
1296 fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n");
1297 } else {
1298 fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n");
1299 }
1300
1301 $sql = "SELECT * FROM ".$table; // Here SELECT * is allowed because we don't have definition of columns to take
1302 $result = $db->query($sql);
1303 while ($row = $db->fetch_row($result)) {
1304 // For each row of data we print a line of INSERT
1305 fwrite($handle, "INSERT ".$delayed.$ignore."INTO ".$table." VALUES (");
1306 $columns = count($row);
1307 for ($j = 0; $j < $columns; $j++) {
1308 // Processing each columns of the row to ensure that we correctly save the value (eg: add quotes for string - in fact we add quotes for everything, it's easier)
1309 if ($row[$j] == null && !is_string($row[$j])) {
1310 // IMPORTANT: if the field is NULL we set it NULL
1311 $row[$j] = 'NULL';
1312 } elseif (is_string($row[$j]) && $row[$j] == '') {
1313 // if it's an empty string, we set it as an empty string
1314 $row[$j] = "''";
1315 } elseif (is_numeric($row[$j]) && !strcmp((string) $row[$j], (string) ((float) $row[$j] + 0))) { // test if it's a numeric type and the numeric version ($nb+0) == string version (eg: if we have 01, it's probably not a number but rather a string, else it would not have any leading 0)
1316 // if it's a number, we return it as-is
1317 // $row[$j] = $row[$j];
1318 } else { // else for all other cases we escape the value and put quotes around
1319 $row[$j] = addslashes($row[$j]);
1320 $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
1321 $row[$j] = "'".$row[$j]."'";
1322 }
1323 }
1324 fwrite($handle, implode(',', $row).");\n");
1325 }
1326 if (GETPOST("nobin_disable_fk")) {
1327 fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking
1328 } else {
1329 fwrite($handle, "/*!40000 ALTER TABLE `".$table."` ENABLE KEYS */;\n");
1330 }
1331 if (!GETPOST("nobin_nolocks")) {
1332 fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table
1333 }
1334 fwrite($handle, "\n\n\n");
1335 }
1336 }
1337
1338 /* Backup Procedure structure*/
1339 /*
1340 $result = $db->query('SHOW PROCEDURE STATUS');
1341 if ($db->num_rows($result) > 0)
1342 {
1343 while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; }
1344 foreach($procedures as $proc)
1345 {
1346 fwrite($handle,"DELIMITER $$\n\n");
1347 fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n");
1348 $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'");
1349 $row2 = $db->fetch_row($resqlcreateproc);
1350 fwrite($handle,"\n".$row2[2]."$$\n\n");
1351 fwrite($handle,"DELIMITER ;\n\n");
1352 }
1353 }
1354 */
1355 /* Backup Procedure structure*/
1356
1357 // Write the footer (restore the previous database settings)
1358 $sqlfooter = "\n\n";
1359 if (GETPOST("nobin_use_transaction")) {
1360 $sqlfooter .= "COMMIT;\n";
1361 }
1362 if (GETPOST("nobin_disable_fk")) {
1363 $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n";
1364 }
1365 $sqlfooter .= "\n\n-- Dump completed on ".date('Y-m-d G-i-s');
1366 fwrite($handle, $sqlfooter);
1367
1368 fclose($handle);
1369
1370 return 1;
1371 }
1372
1386 public function sendBackup($sendto = '', $from = '', $subject = '', $message = '', $filename = '', $filter = '', $sizelimit = 100000000)
1387 {
1388 global $conf, $langs;
1390
1391 $filepath = '';
1392 $filesize = -1;
1393 $output = '';
1394 $error = 0;
1395 $mimetype = '';
1396
1397 if (!empty($from)) {
1398 $from = dol_escape_htmltag($from);
1399 } elseif (getDolGlobalString('MAIN_MAIL_EMAIL_FROM')) {
1400 $from = dol_escape_htmltag(getDolGlobalString('MAIN_MAIL_EMAIL_FROM'));
1401 } else {
1402 $error++;
1403 }
1404
1405 if (!empty($sendto)) {
1406 $sendto = dol_escape_htmltag($sendto);
1407 } elseif (getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')) {
1408 $sendto = dol_escape_htmltag(getDolGlobalString('MAIN_INFO_SOCIETE_MAIL'));
1409 } else {
1410 $error++;
1411 }
1412
1413 if (!empty($subject)) {
1414 $subject = dol_escape_htmltag($subject);
1415 } else {
1416 $subject = dol_escape_htmltag($langs->trans('MakeSendLocalDatabaseDumpShort'));
1417 }
1418
1419 if (empty($message)) {
1420 $message = dol_escape_htmltag($langs->trans('MakeSendLocalDatabaseDumpShort'));
1421 }
1422
1423 $tmpfiles = array();
1424 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1425 if ($filename) {
1426 if (dol_is_file($conf->admin->dir_output.'/backup/'.$filename)) {
1427 $tmpfiles = dol_most_recent_file($conf->admin->dir_output.'/backup', $filename);
1428 }
1429 } else {
1430 $tmpfiles = dol_most_recent_file($conf->admin->dir_output.'/backup', $filter);
1431 }
1432 if ($tmpfiles && is_array($tmpfiles)) {
1433 foreach ($tmpfiles as $key => $val) {
1434 if ($key == 'fullname') {
1435 $filepath = array($val);
1436 $filesize = dol_filesize($val);
1437 }
1438 if ($key == 'type') {
1439 $mimetype = array($val);
1440 }
1441 if ($key == 'relativename') {
1442 $filename = array($val);
1443 }
1444 }
1445 }
1446
1447 if ($filepath) {
1448 if ($filesize > $sizelimit) {
1449 $message .= '<br>'.$langs->trans("BackupIsTooLargeSend");
1450 $documenturl = $dolibarr_main_url_root.'/document.php?modulepart=systemtools&atachement=1&file=backup/'.urlencode($filename[0]);
1451 $message .= '<br><a href='.$documenturl.'>Download link</a>';
1452 $filepath = '';
1453 $mimetype = '';
1454 $filename = '';
1455 }
1456 } else {
1457 $output = 'No backup file found';
1458 $error++;
1459 }
1460
1461 $mailfile = null;
1462 if (!$error) {
1463 include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
1464 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, 1);
1465 // $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, 1);
1466 if ($mailfile->error) {
1467 $error++;
1468 $output = $mailfile->error;
1469 }
1470 }
1471
1472 $result = false;
1473 $output = '';
1474 if (!$error && $mailfile !== null) {
1475 $result = $mailfile->sendfile();
1476 if (!$result) {
1477 $error++;
1478 $output = $mailfile->error;
1479 }
1480 }
1481
1482 dol_syslog(__METHOD__, LOG_DEBUG);
1483
1484 $this->error = "Error sending backup file ".((string) $error);
1485 $this->output = $output;
1486
1487 if ($result) {
1488 return 0;
1489 } else {
1490 return -1;
1491 }
1492 }
1493
1501 public function cleanUnfinishedCronjob()
1502 {
1503 global $db, $user;
1504 dol_syslog("Utils::cleanUnfinishedCronjob Starting cleaning");
1505
1506 // Import Cronjob class if not present
1507 require_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php';
1508
1509 // Get this job object
1510 $this_job = new Cronjob($db);
1511 $this_job->fetch(-1, 'Utils', 'cleanUnfinishedCronjob');
1512 if (empty($this_job->id) || !empty($this_job->error)) {
1513 dol_syslog("Utils::cleanUnfinishedCronjob Unable to fetch himself: ".$this_job->error, LOG_ERR);
1514 return -1;
1515 }
1516
1517 // Set this job processing to 0 to avoid being locked by his processing state
1518 $this_job->processing = 0;
1519 if ($this_job->update($user) < 0) {
1520 dol_syslog("Utils::cleanUnfinishedCronjob Unable to update himself: ".implode(', ', $this_job->errors), LOG_ERR);
1521 return -1;
1522 }
1523
1524 $cron_job = new Cronjob($db);
1525 $cron_job->fetchAll('DESC', 't.rowid', 100, 0, 1, [], 1); // Fetch jobs that are currently running
1526
1527 // Iterate over all jobs in processing (this can't be this job since his state is set to 0 before)
1528 foreach ($cron_job->lines as $job_line) {
1529 // Avoid job with no PID
1530 if (empty($job_line->pid)) {
1531 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." don't have a PID", LOG_DEBUG);
1532 continue;
1533 }
1534
1535 $job = new Cronjob($db);
1536 $job->fetch($job_line->id);
1537 if (empty($job->id) || !empty($job->error)) {
1538 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." can't be fetch: ".$job->error, LOG_ERR);
1539 continue;
1540 }
1541
1542 // Calling posix_kill with the 0 kill signal will return true if the process is running, false otherwise.
1543 if (! posix_kill($job->pid, 0)) {
1544 // Clean processing and pid values
1545 $job->processing = 0;
1546 $job->pid = null;
1547
1548 // Set last result as an error and add the reason on the last output
1549 $job->lastresult = strval(-1);
1550 $job->lastoutput = 'Job killed by job cleanUnfinishedCronjob';
1551
1552 if ($job->update($user) < 0) {
1553 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." can't be updated: ".implode(', ', $job->errors), LOG_ERR);
1554 continue;
1555 }
1556 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." cleaned");
1557 }
1558 }
1559
1560 dol_syslog("Utils::cleanUnfinishedCronjob Cleaning completed");
1561 return 0;
1562 }
1563}
global $dolibarr_main_url_root
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Cron Job class.
Class to manage ECM directories.
Class to manage utility methods.
executeCLI($command, $outputfile, $execmethod=0, $redirectionfile=null, $noescapecommand=0, $redirectionfileerr=null)
Execute a CLI command.
__construct($db)
Constructor.
generateDoc($module)
Generate documentation of a Module.
getShellPasswordParam($password)
Return the -p parameter of mysqldump with the password, escaped to be used in a shell command line.
compressSyslogs()
This saves syslog files and compresses older ones.
sendBackup($sendto='', $from='', $subject='', $message='', $filename='', $filter='', $sizelimit=100000000)
Make a send last backup of database or fil in param CAN BE A CRON TASK.
dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0, $execmethod=0, $lowmemorydump=0)
Make a backup of database CAN BE A CRON TASK.
cleanUnfinishedCronjob()
Clean unfinished cronjob in processing when pid is no longer present in the system CAN BE A CRON TASK...
purgeFiles($choices='tempfilesold+logfiles', $nbsecondsold=86400)
Purge files into directory of data files.
global $mysoc
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=null)
Move a file into another name.
dol_filesize($pathoffile)
Return size of a file.
dol_copy($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_is_file($pathoffile)
Return if path is a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:64
dol_most_recent_file($dir, $regexfilter='', $excludefilter=array('(\.meta|_preview.*\.png) $', '^\.'), $nohook=0, $mode=0)
Return file(s) into a directory (by default most recent)
dol_is_dir($folder)
Test if filename is a directory.
dolReplaceInFile($srcfile, $arrayreplacement, $destfile='', $newmask='0', $indexdatabase=0, $arrayreplacementisregex=0)
Make replacement of strings into a file.
dol_now($mode='gmt')
Return date for now.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
$moduleobj
Definition index.php:3492
dolMd2Asciidoc($content, $parser='dolibarr', $replaceimagepath=null)
Function to parse MD content into ASCIIDOC.