dolibarr 25.0.0-alpha
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 // In low memory mode, the output of the dump is piped into an OS compressor that must be allowed too
356 $compressorcommands = array('gz' => 'gzip', 'bz' => 'bzip2', 'zstd' => 'zstd');
357 if ($lowmemorydump && !empty($compressorcommands[$compression]) && !in_array($compressorcommands[$compression], $arrayofallowedcommand)) {
358 $langs->loadLangs(array("admin", "errors"));
359 dol_syslog("Compression command ".$compressorcommands[$compression]." is not inside the allowed commands", LOG_WARNING);
360 $this->error = $langs->trans('CommandIsNotInsideAllowedCommands');
361 $this->error .= '<br>'.$langs->trans('NotAvailableCommandNotAllowed', $compressorcommands[$compression]);
362 return -1;
363 }
364 }
365
366 // Parameters execution
367 $command = $cmddump;
368 $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.
369 if (preg_match("/\s/", $command)) {
370 $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
371 }
372
373 //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
374 $param = $dolibarr_main_db_name." -h ".$dolibarr_main_db_host;
375 $param .= " -u ".$dolibarr_main_db_user;
376 if (!empty($dolibarr_main_db_port)) {
377 $param .= " -P ".$dolibarr_main_db_port." --protocol=tcp";
378 }
379 // The default (scheduled job) uses a transaction too, as the form does, instead of locking
380 // all the tables, which blocks every write into the database during the backup.
381 if (GETPOST("use_transaction", "alpha") || $usedefault) {
382 $param .= " --single-transaction";
383 }
384 if (GETPOST("disable_fk", "alpha") || $usedefault) {
385 $param .= " -K";
386 }
387 if (GETPOST("sql_compat", "alpha") && GETPOST("sql_compat", "alpha") != 'NONE') {
388 $param .= " --compatible=".escapeshellarg(GETPOST("sql_compat", "alpha"));
389 }
390 if (GETPOST("drop_database", "alpha")) {
391 $param .= " --add-drop-database";
392 }
393 if (GETPOST("use_mysql_quick_param", "alpha")) {
394 $param .= " --quick";
395 }
396 if (GETPOST("use_force", "alpha")) {
397 $param .= " -f";
398 }
399 if (GETPOST("sql_structure", "alpha") || $usedefault) {
400 if (GETPOST("drop", "alpha") || $usedefault) {
401 $param .= " --add-drop-table=TRUE";
402 } else {
403 $param .= " --add-drop-table=FALSE";
404 }
405 } else {
406 $param .= " -t";
407 }
408 if (GETPOST("disable-add-locks", "alpha")) {
409 $param .= " --add-locks=FALSE";
410 }
411 if (GETPOST("sql_data", "alpha") || $usedefault) {
412 $param .= " --tables";
413 if (GETPOST("showcolumns", "alpha") || $usedefault) {
414 $param .= " -c";
415 }
416 if (GETPOST("extended_ins", "alpha") || $usedefault) {
417 $param .= " -e";
418 } else {
419 $param .= " --skip-extended-insert";
420 }
421 if (GETPOST("delayed", "alpha")) {
422 $param .= " --delayed-insert";
423 }
424 if (GETPOST("sql_ignore", "alpha")) {
425 $param .= " --insert-ignore";
426 }
427 if (GETPOST("hexforbinary", "alpha") || $usedefault) {
428 $param .= " --hex-blob";
429 }
430 } else {
431 $param .= " -d"; // No row information (no data)
432 }
433 if ($dolibarr_main_db_character_set == 'utf8mb4') {
434 // We save output into utf8mb4 charset
435 $param .= " --default-character-set=utf8mb4 --no-tablespaces";
436 } else {
437 $param .= " --default-character-set=utf8 --no-tablespaces"; // We always save output into utf8 charset
438 }
439 $paramcrypted = $param;
440 $paramclear = $param;
441 if (!empty($dolibarr_main_db_pass)) {
442 $paramcrypted .= ' -p"'.preg_replace('/./i', '*', $dolibarr_main_db_pass).'"';
443 $paramclear .= ' '.$this->getShellPasswordParam($dolibarr_main_db_pass);
444 }
445
446 $handle = '';
447
448 // Start call method to execute dump
449 $fullcommandcrypted = $command." ".$paramcrypted." 2>&1";
450 $fullcommandclear = $command." ".$paramclear." 2>&1";
451 if (!$lowmemorydump) {
452 if ($compression == 'none') {
453 $handle = fopen($outputfile, 'w');
454 } elseif ($compression == 'gz') {
455 $handle = gzopen($outputfile, 'w');
456 } elseif ($compression == 'bz') {
457 $handle = bzopen($outputfile, 'w');
458 } elseif ($compression == 'zstd') {
459 $handle = fopen("compress.zstd://" . $outputfile, "wb");
460 }
461 } else {
462 // TODO Add a pipe into script to decrypt dolCrypted values
463
464 if ($compression == 'none') {
465 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).'"';
466 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).'"';
467 $handle = 1;
468 } elseif ($compression == 'gz') {
469 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).'"';
470 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).'"';
471 $paramcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip';
472 $handle = 1;
473 } elseif ($compression == 'bz') {
474 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).'"';
475 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).'"';
476 $paramcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2';
477 $handle = 1;
478 } elseif ($compression == 'zstd') {
479 $fullcommandclear .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).'"';
480 $fullcommandcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).'"';
481 $paramcrypted .= ' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd';
482 $handle = 1;
483 }
484 }
485
486 $ok = 0;
487 if ($handle) {
488 if (getDolGlobalString('MAIN_EXEC_USE_POPEN')) {
489 $execmethod = getDolGlobalString('MAIN_EXEC_USE_POPEN');
490 }
491 if (empty($execmethod)) {
492 $execmethod = 1;
493 }
494
495 dol_syslog("Utils::dumpDatabase execmethod=".$execmethod.", lowmemorydump=".$lowmemorydump.", command=".$fullcommandcrypted, LOG_INFO);
496
497
498 /* If value has been forced with a php_admin_value, this has no effect. Example of value: '512M' */
499 $MemoryLimit = getDolGlobalString('MAIN_MEMORY_LIMIT_DUMP');
500 if (!empty($MemoryLimit)) {
501 @ini_set('memory_limit', $MemoryLimit);
502 }
503
504
505 if ($execmethod == 1) {
506 $output_arr = array();
507 $retval = null;
508
509 exec($fullcommandclear, $output_arr, $retval); // @phan-suppress-current-line PhanPluginConstantVariableNull
510 // TODO Replace this exec with Utils->executeCLI() function.
511 // We must check that the case for $lowmemorydump works too...
512 //$utils = new Utils($db);
513 //$outputfile = $conf->admin->dir_temp.'/dump.tmp';
514 //$utils->executeCLI($fullcommandclear, $outputfile, 0);
515
516 if ($retval != 0) {
517 $langs->load("errors");
518 dol_syslog("Datadump retval after exec=".$retval, LOG_ERR);
519 $errormsg = 'Error '.$retval;
520 $ok = 0;
521 } else {
522 $i = 0;
523 if (!empty($output_arr)) {
524 foreach ($output_arr as $key => $read) {
525 $i++; // output line number
526 if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) {
527 continue;
528 }
529 // Now check into the result file, that the file end with "-- Dump completed"
530 // This is possible only if $output_arr is the clear dump file, so not possible with $lowmemorydump set because file is already compressed.
531 if (!$lowmemorydump) {
532 // TODO decrypt dolCrypted values from $read
533
534 fwrite($handle, $read.($execmethod == 2 ? '' : "\n"));
535 if (preg_match('/'.preg_quote('-- Dump completed', '/').'/i', $read)) {
536 $ok = 1;
537 } elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES', '/').'/i', $read)) {
538 $ok = 1;
539 }
540 } else {
541 // If we have a result here in lowmemorydump mode, something is strange
542 }
543 }
544 } elseif ($lowmemorydump) {
545 $ok = 1;
546 }
547 }
548 }
549
550 if ($execmethod == 2) { // With this method, there is no way to get the return code, only output
551 $handlein = popen($fullcommandclear, 'r');
552 $i = 0;
553 if ($handlein) {
554 while (!feof($handlein)) {
555 $i++; // output line number
556 $read = fgets($handlein);
557 // Exclude warning line we don't want
558 if ($i == 1 && preg_match('/Warning.*Using a password/i', $read)) {
559 continue;
560 }
561 fwrite($handle, $read);
562 if (preg_match('/'.preg_quote('-- Dump completed', '/').'/i', $read)) {
563 $ok = 1;
564 } elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES', '/').'/i', $read)) {
565 $ok = 1;
566 }
567 }
568 pclose($handlein);
569 }
570 }
571
572 if (!$lowmemorydump) {
573 if ($compression == 'none') {
574 fclose($handle);
575 } elseif ($compression == 'gz') {
576 gzclose($handle);
577 } elseif ($compression == 'bz') {
578 fclose($handle);
579 } elseif ($compression == 'zstd') {
580 fclose($handle);
581 }
582 }
583
584 dolChmod($outputfile);
585 } else {
586 $langs->load("errors");
587 dol_syslog("Failed to open file ".$outputfile, LOG_ERR);
588 $errormsg = $langs->trans("ErrorFailedToWriteInDir");
589 }
590
591 // Get errorstring
592 if ($compression == 'none') {
593 $handle = fopen($outputfile, 'r');
594 } elseif ($compression == 'gz') {
595 $handle = gzopen($outputfile, 'r');
596 } elseif ($compression == 'bz') {
597 $handle = bzopen($outputfile, 'r');
598 } elseif ($compression == 'zstd') {
599 $handle = fopen("compress.zstd://" . $outputfile, "rb");
600 }
601 if ($handle) {
602 // Get 2048 first chars of error message.
603 $errormsg = fgets($handle, 2048);
604 //$ok=0;$errormsg=''; To force error
605
606 // Close file
607 if ($compression == 'none') {
608 fclose($handle);
609 } elseif ($compression == 'gz') {
610 gzclose($handle);
611 } elseif ($compression == 'bz') {
612 fclose($handle);
613 } elseif ($compression == 'zstd') {
614 fclose($handle);
615 }
616 if ($ok && preg_match('/^-- (MySql|MariaDB)/i', $errormsg) || preg_match('/^\/\*M?!999999/', $errormsg)) { // Start of file is ok, NOT an error
617 $errormsg = '';
618 } else {
619 // Rename file out into a file error
620 //print "$outputfile -> $outputerror";
621 @dol_delete_file($outputerror, 1, 0, 0, null, false, 0);
622 @dol_move($outputfile, $outputerror, '0', 1, 0, 0);
623 // If safe_mode is on and command is outside the exec parameter, we get an empty out file so errormsg is empty
624 if (!$errormsg) {
625 $langs->load("errors");
626 $errormsg = $langs->trans("ErrorFailedToRunExternalCommand");
627 }
628 }
629 }
630 // Fin execution commande
631
632 $this->output = $errormsg;
633 $this->error = $errormsg;
634 $this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => "");
635 //if (empty($this->output)) $this->output=$this->result['commandbackuplastdone'];
636 }
637
638 // MYSQL NO BIN
639 if ($type == 'mysqlnobin') {
640 $outputfile = $outputdir.'/'.$file;
641 $outputfiletemp = $outputfile.'-TMP.sql';
642 // for compression format, we add extension
643 $compression = $compression ? $compression : 'none';
644 if ($compression == 'gz') {
645 $outputfile .= '.gz';
646 }
647 if ($compression == 'bz') {
648 $outputfile .= '.bz2';
649 }
650 $outputerror = $outputfile.'.err';
651 dol_mkdir($conf->admin->dir_output.'/backup');
652
653 if ($compression == 'gz' or $compression == 'bz') {
654 $this->backupTables($outputfiletemp);
655 dol_compress_file($outputfiletemp, $outputfile, $compression);
656 unlink($outputfiletemp);
657 } else {
658 $this->backupTables($outputfile);
659 }
660
661 $this->output = "";
662 $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => "");
663 }
664
665 // POSTGRESQL
666 if ($type == 'postgresql' || $type == 'pgsql') {
667 $cmddump = getDolGlobalString('SYSTEMTOOLS_POSTGRESQLDUMP');
668
669 $outputfile = $outputdir.'/'.$file;
670 // for compression format, we add extension
671 $compression = $compression ? $compression : 'none';
672 if ($compression == 'gz') {
673 $outputfile .= '.gz';
674 }
675 if ($compression == 'bz') {
676 $outputfile .= '.bz2';
677 }
678 $outputerror = $outputfile.'.err';
679 dol_mkdir($conf->admin->dir_output.'/backup');
680
681 // Sanitize and validate $cmddump
682 $cmddump = dol_sanitizePathName($cmddump); // Sanitize path
683 $cmddump = dol_string_nospecial($cmddump, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
684 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
685
686 if (!empty($dolibarr_main_restrict_os_commands)) {
687 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
688 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
689 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
690 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
691 $langs->load("errors");
692 $this->error = $langs->trans('CommandIsNotInsideAllowedCommands');
693 $this->error .= '<br>'.$langs->trans('ErrorCheckTheCommandInsideTheAdvancedOptions');
694 return -1;
695 }
696 }
697
698 // Parameters execution
699 $command = $cmddump;
700 $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.
701 if (preg_match("/\s/", $command)) {
702 $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
703 }
704
705 //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
706 //$param="-F c";
707 $param = "-F p";
708 $param .= " --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
709 $param .= " -U ".$dolibarr_main_db_user;
710 if (!empty($dolibarr_main_db_port)) {
711 $param .= " -p ".$dolibarr_main_db_port;
712 }
713 if (GETPOST("sql_compat") && GETPOST("sql_compat") == 'ANSI') {
714 $param .= " --disable-dollar-quoting";
715 }
716 if (GETPOST("drop_database")) {
717 $param .= " -c -C";
718 }
719 if (GETPOST("sql_structure")) {
720 if (GETPOST("drop")) {
721 $param .= " --add-drop-table";
722 }
723 if (!GETPOST("sql_data")) {
724 $param .= " -s";
725 }
726 }
727 if (GETPOST("sql_data")) {
728 if (!GETPOST("sql_structure")) {
729 $param .= " -a";
730 }
731 if (GETPOST("showcolumns")) {
732 $param .= " -c";
733 }
734 }
735 $param .= ' -f "'.$outputfile.'"';
736 //if ($compression == 'none')
737 if ($compression == 'gz') {
738 $param .= ' -Z 9';
739 }
740 //if ($compression == 'bz')
741 $paramcrypted = $param;
742 $paramclear = $param;
743 /*if (!empty($dolibarr_main_db_pass))
744 {
745 $paramcrypted.=" -W".preg_replace('/./i','*',$dolibarr_main_db_pass);
746 $paramclear.=" -W".$dolibarr_main_db_pass;
747 }*/
748 $paramcrypted .= " -w ".$dolibarr_main_db_name;
749 $paramclear .= " -w ".$dolibarr_main_db_name;
750
751 $this->output = "";
752 $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => $command." ".$paramcrypted);
753 }
754
755 // Clean old files
756 if (!$errormsg && $keeplastnfiles > 0) {
757 $tmpfiles = dol_dir_list($conf->admin->dir_output.'/backup', 'files', 0, '', '(\.err|\.old|\.sav)$', 'date', SORT_DESC);
758 $i = 0;
759 if (is_array($tmpfiles)) {
760 foreach ($tmpfiles as $key => $val) {
761 $i++;
762 if ($i <= $keeplastnfiles) {
763 continue;
764 }
765 dol_delete_file($val['fullname'], 0, 0, 0, null, false, 0);
766 }
767 }
768 }
769
770 return ($errormsg ? -1 : 0);
771 }
772
773
774
788 public function executeCLI($command, $outputfile, $execmethod = 0, $redirectionfile = null, $noescapecommand = 0, $redirectionfileerr = null)
789 {
790 global $langs;
791
792 $result = 0;
793 $output = '';
794 $error = '';
795
796 global $dolibarr_main_restrict_os_commands;
797 if (!empty($dolibarr_main_restrict_os_commands)) {
798 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
799 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
800
801 $commands = preg_split('/\|\||&&|\||&/', $command);
802 foreach ($commands as $newcommand) {
803 $newcommand = trim($newcommand);
804 $matches = array();
805 // 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.
806 if (preg_match('/^(?:\'([^\']*)\'|"([^"]*)"|(\S+))/', $newcommand, $matches)) {
807 $newcommand = $matches[1] ?: ($matches[2] ?: $matches[3]);
808 }
809 if (!in_array(basename($newcommand), $arrayofallowedcommand)) {
810 dol_syslog("files.lib.php::executeCLI canceled because target filename ".basename($newcommand)." is not in the whitelist of allowed commands (param dolibarr_main_restrict_os_commands).", LOG_WARNING);
811 return array('result' => -1, 'output' => '', 'error' => 'Command '.basename($newcommand).' is not in the whitelist of allowed commands (param dolibarr_main_restrict_os_commands)');
812 }
813 }
814 }
815
816 if (empty($noescapecommand)) {
817 $command = escapeshellcmd($command);
818 }
819
820 if ($redirectionfile) {
821 $command .= " > ".dol_sanitizePathName($redirectionfile);
822 }
823
824 if ($redirectionfileerr && ($redirectionfileerr != $redirectionfile)) {
825 // If we ask a redirect of stderr on a given file not already used for stdout
826 $command .= " 2> ".dol_sanitizePathName($redirectionfileerr);
827 } else {
828 $command .= " 2>&1";
829 }
830
831 if (getDolGlobalString('MAIN_EXEC_USE_POPEN')) {
832 $execmethod = getDolGlobalString('MAIN_EXEC_USE_POPEN');
833 }
834 if (empty($execmethod)) {
835 $execmethod = 1;
836 }
837 //$execmethod=1;
838 dol_syslog("Utils::executeCLI execmethod=".$execmethod." command=".$command, LOG_DEBUG);
839 $output_arr = array();
840
841 if ($execmethod == 1) {
842 $retval = null;
843 exec($command, $output_arr, $retval); // @phan-suppress-current-line PhanPluginConstantVariableNull
844 $result = $retval;
845 if ($retval != 0) {
846 $langs->load("errors");
847 dol_syslog("Utils::executeCLI retval after exec=".$retval, LOG_ERR);
848 $error = 'Error '.$retval;
849 }
850 }
851 if ($execmethod == 2) { // With this method, there is no way to get the return code, only output
852 $handle = fopen($outputfile, 'w+b');
853 if ($handle) {
854 dol_syslog("Utils::executeCLI run command ".$command);
855 $handlein = popen($command, 'r');
856 while (!feof($handlein)) {
857 $read = fgets($handlein);
858 fwrite($handle, $read);
859 $output_arr[] = $read;
860 }
861 pclose($handlein);
862 fclose($handle);
863 }
864 dolChmod($outputfile);
865 }
866
867 // Update with result
868 if (is_array($output_arr) && count($output_arr) > 0) {
869 foreach ($output_arr as $val) {
870 $output .= $val.($execmethod == 2 ? '' : "\n");
871 }
872 }
873
874 dol_syslog("Utils::executeCLI result=".$result." output=".$output." error=".$error, LOG_DEBUG);
875
876 return array('result' => $result, 'output' => $output, 'error' => $error);
877 }
878
885 public function generateDoc($module)
886 {
887 global $langs, $user, $mysoc;
888 global $dirins;
889
890 $error = 0;
891
892 $modulelowercase = strtolower($module);
893 $now = dol_now();
894
895 // Dir for module
896 $dir = $dirins.'/'.$modulelowercase;
897 // Zip file to build
898 $FILENAMEDOC = '';
899
900 // Load module
901 dol_include_once($modulelowercase.'/core/modules/mod'.$module.'.class.php');
902 $class = 'mod'.$module;
903
904 $moduleobj = null;
905 if (class_exists($class)) {
906 try {
907 $moduleobj = new $class($this->db);
908 } catch (Exception $e) {
909 $error++;
910 dol_print_error(null, $e->getMessage());
911 }
912 } else {
913 $error++;
914 $langs->load("errors");
915 dol_print_error(null, $langs->trans("ErrorFailedToLoadModuleDescriptorForXXX", $module));
916 exit;
917 }
918
919 $arrayversion = $moduleobj === null ? array() : explode('.', $moduleobj->version, 3);
920 if (count($arrayversion)) {
921 $FILENAMEASCII = strtolower($module).'.asciidoc';
922 $FILENAMEDOC = strtolower($module).'.html';
923 $FILENAMEDOCPDF = strtolower($module).'.pdf';
924
925 $dirofmodule = dol_buildpath(strtolower($module), 0);
926 $dirofmoduledoc = dol_buildpath(strtolower($module), 0).'/doc';
927 $dirofmoduletmp = dol_buildpath(strtolower($module), 0).'/doc/temp';
928 $outputfiledoc = $dirofmoduledoc.'/'.$FILENAMEDOC;
929 if ($dirofmoduledoc) {
930 if (!dol_is_dir($dirofmoduledoc)) {
931 dol_mkdir($dirofmoduledoc);
932 }
933 if (!dol_is_dir($dirofmoduletmp)) {
934 dol_mkdir($dirofmoduletmp);
935 }
936 if (!is_writable($dirofmoduletmp)) {
937 $this->error = 'Dir '.$dirofmoduletmp.' does not exists or is not writable';
938 return -1;
939 }
940
941 if (!getDolGlobalString('MODULEBUILDER_ASCIIDOCTOR') && !getDolGlobalString('MODULEBUILDER_ASCIIDOCTORPDF')) {
942 $this->error = 'Setup of module ModuleBuilder not complete';
943 return -1;
944 }
945
946 // Copy some files into temp directory, so instruction include::ChangeLog.md[] will works inside the asciidoc file.
947 dol_copy($dirofmodule.'/README.md', $dirofmoduletmp.'/README.md', '0', 1);
948 dol_copy($dirofmodule.'/ChangeLog.md', $dirofmoduletmp.'/ChangeLog.md', '0', 1);
949
950 // Replace into README.md and ChangeLog.md (in case they are included into documentation with tag __README__ or __CHANGELOG__)
951 $arrayreplacement = array();
952 $arrayreplacement['/^#\s.*/m'] = ''; // Remove first level of title into .md files
953 $arrayreplacement['/^#/m'] = '##'; // Add on # to increase level
954
955 dolReplaceInFile($dirofmoduletmp.'/README.md', $arrayreplacement, '', '0', 0, 1);
956 dolReplaceInFile($dirofmoduletmp.'/ChangeLog.md', $arrayreplacement, '', '0', 0, 1);
957
958
959 $destfile = $dirofmoduletmp.'/'.$FILENAMEASCII;
960
961 $fhandle = fopen($destfile, 'w+');
962 if ($fhandle) {
963 $specs = dol_dir_list(dol_buildpath(strtolower($module).'/doc', 0), 'files', 1, '(\.md|\.asciidoc)$', array('\/temp\/'));
964
965 $i = 0;
966 foreach ($specs as $spec) {
967 if (preg_match('/notindoc/', $spec['relativename'])) {
968 continue; // Discard file
969 }
970 if (preg_match('/example/', $spec['relativename'])) {
971 continue; // Discard file
972 }
973 if (preg_match('/disabled/', $spec['relativename'])) {
974 continue; // Discard file
975 }
976
977 $pathtofile = strtolower($module).'/doc/'.$spec['relativename'];
978 $format = 'asciidoc';
979 if (preg_match('/\.md$/i', $spec['name'])) {
980 $format = 'markdown';
981 }
982
983 $filecursor = @file_get_contents($spec['fullname']);
984 if ($filecursor) {
985 fwrite($fhandle, ($i ? "\n<<<\n\n" : "").$filecursor."\n");
986 } else {
987 $this->error = 'Failed to concat content of file '.$spec['fullname'];
988 return -1;
989 }
990
991 $i++;
992 }
993
994 fclose($fhandle);
995
996 $contentreadme = file_get_contents($dirofmoduletmp.'/README.md');
997 $contentchangelog = file_get_contents($dirofmoduletmp.'/ChangeLog.md');
998
999 include DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
1000
1001 //var_dump($phpfileval['fullname']);
1002 $arrayreplacement = array(
1003 'mymodule' => strtolower($module),
1004 'MyModule' => $module,
1005 'MYMODULE' => strtoupper($module),
1006 'My module' => $module,
1007 'my module' => $module,
1008 'Mon module' => $module,
1009 'mon module' => $module,
1010 'htdocs/modulebuilder/template' => strtolower($module),
1011 '__MYCOMPANY_NAME__' => $mysoc->name,
1012 '__KEYWORDS__' => $module,
1013 '__USER_FULLNAME__' => $user->getFullName($langs),
1014 '__USER_EMAIL__' => $user->email,
1015 '__YYYY-MM-DD__' => dol_print_date($now, 'dayrfc'),
1016 '---Put here your own copyright and developer email---' => dol_print_date($now, 'dayrfc').' '.$user->getFullName($langs).($user->email ? ' <'.$user->email.'>' : ''),
1017 '__DATA_SPECIFICATION__' => 'Not yet available',
1018 '__README__' => dolMd2Asciidoc($contentreadme),
1019 '__CHANGELOG__' => dolMd2Asciidoc($contentchangelog),
1020 );
1021
1022 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
1023 dolReplaceInFile($destfile, $arrayreplacement);
1024 }
1025
1026 // Launch doc generation
1027 $currentdir = getcwd();
1028 chdir($dirofmodule);
1029
1030 require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
1031 $utils = new Utils($this->db);
1032
1033 // Build HTML doc
1034 $command = getDolGlobalString('MODULEBUILDER_ASCIIDOCTOR') . ' '.$destfile.' -n -o '.$dirofmoduledoc.'/'.$FILENAMEDOC;
1035 $outfile = $dirofmoduletmp.'/out.tmp';
1036
1037 $resarray = $utils->executeCLI($command, $outfile);
1038 if ($resarray['result'] != '0') {
1039 $this->error = $resarray['error'].' '.$resarray['output'];
1040 $this->errors[] = $this->error;
1041 }
1042 $result = ($resarray['result'] == 0) ? 1 : 0;
1043 if ($result == 0) {
1044 // Add a human readable message to the raw output of the command
1045 $this->error = $langs->trans("ErrorFailToGenerateFile", $FILENAMEDOC);
1046 $this->errors[] = $this->error;
1047 }
1048
1049 // Build PDF doc
1050 $command = getDolGlobalString('MODULEBUILDER_ASCIIDOCTORPDF') . ' '.$destfile.' -n -o '.$dirofmoduledoc.'/'.$FILENAMEDOCPDF;
1051 $outfile = $dirofmoduletmp.'/outpdf.tmp';
1052 $resarray = $utils->executeCLI($command, $outfile);
1053 if ($resarray['result'] != '0') {
1054 $this->error = $resarray['error'].' '.$resarray['output'];
1055 $this->errors[] = $this->error;
1056 }
1057 $result = ($resarray['result'] == 0) ? 1 : 0;
1058 if ($result == 0) {
1059 // Add a human readable message to the raw output of the command
1060 $this->error = $langs->trans("ErrorFailToGenerateFile", $FILENAMEDOCPDF);
1061 $this->errors[] = $this->error;
1062 }
1063
1064 chdir($currentdir);
1065 } else {
1066 $result = 0;
1067 }
1068
1069 if ($result > 0) {
1070 return 1;
1071 } else {
1072 $error++;
1073 }
1074 } else {
1075 $error++;
1076 $langs->load("errors");
1077 $this->error = $langs->trans("ErrorCheckVersionIsDefined");
1078 }
1079
1080 return -1;
1081 }
1082
1090 public function compressSyslogs()
1091 {
1092 global $conf;
1093
1094 if (empty($conf->loghandlers['mod_syslog_file'])) { // File Syslog disabled
1095 return 0;
1096 }
1097
1098 if (!function_exists('gzopen')) {
1099 $this->error = 'Support for gzopen not available in this PHP';
1100 return -1;
1101 }
1102
1103 require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
1104
1105 $nbSaves = intval(getDolGlobalString('SYSLOG_FILE_SAVES', 10));
1106
1107 if (!getDolGlobalString('SYSLOG_FILE')) {
1108 $mainlogdir = DOL_DATA_ROOT;
1109 $mainlog = 'dolibarr.log';
1110 } else {
1111 $mainlogfull = str_replace('DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
1112 $mainlogdir = dirname($mainlogfull);
1113 $mainlog = basename($mainlogfull);
1114 }
1115
1116 $tabfiles = dol_dir_list(DOL_DATA_ROOT, 'files', 0, '^(dolibarr_.+|odt2pdf)\.log$'); // Also handle other log files like dolibarr_install.log
1117 $tabfiles[] = array('name' => $mainlog, 'path' => $mainlogdir);
1118
1119 foreach ($tabfiles as $file) {
1120 $logname = $file['name'];
1121 $logpath = $file['path'];
1122
1123 if (dol_is_file($logpath.'/'.$logname) && dol_filesize($logpath.'/'.$logname) > 0) { // If log file exists and is not empty
1124 // Handle already compressed files to rename them and add +1
1125
1126 $filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';
1127
1128 $gzfilestmp = dol_dir_list($logpath, 'files', 0, $filter);
1129 $gzfiles = array();
1130
1131 foreach ($gzfilestmp as $gzfile) {
1132 $tabmatches = array();
1133 preg_match('/'.$filter.'/i', $gzfile['name'], $tabmatches);
1134
1135 $numsave = intval($tabmatches[1]);
1136
1137 $gzfiles[$numsave] = $gzfile;
1138 }
1139
1140 krsort($gzfiles, SORT_NUMERIC);
1141
1142 foreach ($gzfiles as $numsave => $dummy) {
1143 if (dol_is_file($logpath.'/'.$logname.'.'.($numsave + 1).'.gz')) {
1144 return -2;
1145 }
1146
1147 if ($numsave >= $nbSaves) {
1148 dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz', 0, 0, 0, null, false, 0);
1149 } else {
1150 dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave + 1).'.gz', '0', 1, 0, 0);
1151 }
1152 }
1153
1154 // Compress current file and recreate it
1155
1156 if ($nbSaves > 0) { // If $nbSaves is 1, we keep 1 archive .gz file, If 2, we keep 2 .gz files
1157 $gzfilehandle = gzopen($logpath.'/'.$logname.'.1.gz', 'wb9');
1158
1159 if (empty($gzfilehandle)) {
1160 $this->error = 'Failted to open file '.$logpath.'/'.$logname.'.1.gz';
1161 return -3;
1162 }
1163
1164 $sourcehandle = fopen($logpath.'/'.$logname, 'r');
1165
1166 if (empty($sourcehandle)) {
1167 $this->error = 'Failed to open file '.$logpath.'/'.$logname;
1168 return -4;
1169 }
1170
1171 while (!feof($sourcehandle)) {
1172 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024)); // Read 512 kB at a time
1173 }
1174
1175 fclose($sourcehandle);
1176 gzclose($gzfilehandle);
1177
1178 dolChmod($logpath.'/'.$logname.'.1.gz');
1179 }
1180
1181 dol_delete_file($logpath.'/'.$logname, 0, 0, 0, null, false, 0);
1182
1183 // Create empty file
1184 $newlog = fopen($logpath.'/'.$logname, 'a+');
1185 fclose($newlog);
1186
1187 //var_dump($logpath.'/'.$logname." - ".octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
1188 dolChmod($logpath.'/'.$logname);
1189 }
1190 }
1191
1192 $this->output = 'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.' files) done.';
1193 return 0;
1194 }
1195
1206 public function backupTables($outputfile, $tables = '*')
1207 {
1208 global $db, $langs;
1209 global $errormsg;
1210 global $dolibarr_main_db_character_set;
1211
1212 // Set to UTF-8. Use utf8mb4 when the database uses it, as with utf8 the 4-byte
1213 // characters (emojis...) are read as '?' and are lost when the dump is restored.
1214 $charset = ($dolibarr_main_db_character_set == 'utf8mb4' ? 'utf8mb4' : 'utf8');
1215 $sanitizedcharset = $db->sanitize($charset);
1216 if (is_a($db, 'DoliDBMysqli')) {
1218 $db->db->set_charset($charset);
1219 } else {
1221 $db->query('SET NAMES '.$db->sanitize($charset));
1222 $db->query('SET CHARACTER SET '.$db->sanitize($charset));
1223 }
1224
1225 //get all of the tables
1226 if ($tables == '*') {
1227 $tables = array();
1228 $result = $db->query('SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1229 while ($row = $db->fetch_row($result)) {
1230 $tables[] = $row[0];
1231 }
1232 } else {
1233 $tables = is_array($tables) ? $tables : explode(',', $tables);
1234 }
1235
1236 //cycle through
1237 $handle = fopen($outputfile, 'w+');
1238 if (fwrite($handle, '') === false) {
1239 $langs->load("errors");
1240 dol_syslog("Failed to open file ".$outputfile, LOG_ERR);
1241 $errormsg = $langs->trans("ErrorFailedToWriteInDir");
1242 return -1;
1243 }
1244
1245 // Read all the tables inside one transaction, so the dump is a consistent snapshot of the
1246 // database even if data is changed during the backup (same as mysqldump --single-transaction).
1247 // Only reads (SHOW CREATE TABLE, SELECT) are done inside it.
1248 $db->begin();
1249
1250 // Print headers and global mysql config vars
1251 $sqlhead = '';
1252 $sqlhead .= "-- ".$db::LABEL." dump via php with Dolibarr ".DOL_VERSION."
1253--
1254-- Host: ".$db->db->host_info." Database: ".$db->database_name."
1255-- ------------------------------------------------------
1256-- Server version ".$db->db->server_info."
1257
1258;
1259;
1260;
1261;
1262;
1263;
1264;
1265;
1266;
1267;
1268
1269";
1270
1271 if (GETPOST("nobin_disable_fk")) {
1272 $sqlhead .= "SET FOREIGN_KEY_CHECKS=0;\n";
1273 }
1274 //$sqlhead .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n";
1275 if (GETPOST("nobin_use_transaction")) {
1276 $sqlhead .= "SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1277 }
1278
1279 fwrite($handle, $sqlhead);
1280
1281 $ignore = '';
1282 if (GETPOST("nobin_sql_ignore")) {
1283 $ignore = 'IGNORE ';
1284 }
1285 $delayed = '';
1286 if (GETPOST("nobin_delayed")) {
1287 $delayed = 'DELAYED ';
1288 }
1289
1290 // Process each table and print their definition + their datas
1291 foreach ($tables as $table) {
1292 // Saving the table structure
1293 fwrite($handle, "\n--\n-- Table structure for table `".$table."`\n--\n");
1294
1295 if (GETPOST("nobin_drop")) {
1296 fwrite($handle, "DROP TABLE IF EXISTS `".$table."`;\n"); // Dropping table if exists prior to re create it
1297 }
1298 fwrite($handle, "/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1299 fwrite($handle, "/*!40101 SET character_set_client = ".$sanitizedcharset." */;\n");
1300 $resqldrop = $db->query('SHOW CREATE TABLE '.$table);
1301 $row2 = $db->fetch_row($resqldrop);
1302 if (empty($row2[1])) {
1303 fwrite($handle, "\n-- WARNING: Show create table ".$table." return empty string when it should not.\n");
1304 } else {
1305 fwrite($handle, $row2[1].";\n");
1306 //fwrite($handle,"/*!40101 SET character_set_client = @saved_cs_client */;\n\n");
1307
1308 // Dumping the data (locking the table and disabling the keys check while doing the process)
1309 fwrite($handle, "\n--\n-- Dumping data for table `".$table."`\n--\n");
1310 if (!GETPOST("nobin_nolocks")) {
1311 fwrite($handle, "LOCK TABLES `".$table."` WRITE;\n"); // Lock the table before inserting data (when the data will be imported back)
1312 }
1313 if (GETPOST("nobin_disable_fk")) {
1314 fwrite($handle, "ALTER TABLE `".$table."` DISABLE KEYS;\n");
1315 } else {
1316 fwrite($handle, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n");
1317 }
1318
1319 $sql = "SELECT * FROM ".$table; // Here SELECT * is allowed because we don't have definition of columns to take
1320 $result = $db->query($sql);
1321 while ($row = $db->fetch_row($result)) {
1322 // For each row of data we print a line of INSERT
1323 fwrite($handle, "INSERT ".$delayed.$ignore."INTO ".$table." VALUES (");
1324 $columns = count($row);
1325 for ($j = 0; $j < $columns; $j++) {
1326 // 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)
1327 if ($row[$j] == null && !is_string($row[$j])) {
1328 // IMPORTANT: if the field is NULL we set it NULL
1329 $row[$j] = 'NULL';
1330 } elseif (is_string($row[$j]) && $row[$j] == '') {
1331 // if it's an empty string, we set it as an empty string
1332 $row[$j] = "''";
1333 } 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)
1334 // if it's a number, we return it as-is
1335 // $row[$j] = $row[$j];
1336 } else { // else for all other cases we escape the value and put quotes around
1337 $row[$j] = addslashes($row[$j]);
1338 $row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
1339 $row[$j] = "'".$row[$j]."'";
1340 }
1341 }
1342 fwrite($handle, implode(',', $row).");\n");
1343 }
1344 if (GETPOST("nobin_disable_fk")) {
1345 fwrite($handle, "ALTER TABLE `".$table."` ENABLE KEYS;\n"); // Enabling back the keys/index checking
1346 }
1347 if (!GETPOST("nobin_nolocks")) {
1348 fwrite($handle, "UNLOCK TABLES;\n"); // Unlocking the table
1349 }
1350 fwrite($handle, "\n\n\n");
1351 }
1352 }
1353
1354 /* Backup Procedure structure*/
1355 /*
1356 $result = $db->query('SHOW PROCEDURE STATUS');
1357 if ($db->num_rows($result) > 0)
1358 {
1359 while ($row = $db->fetch_row($result)) { $procedures[] = $row[1]; }
1360 foreach($procedures as $proc)
1361 {
1362 fwrite($handle,"DELIMITER $$\n\n");
1363 fwrite($handle,"DROP PROCEDURE IF EXISTS '$name'.'$proc'$$\n");
1364 $resqlcreateproc=$db->query("SHOW CREATE PROCEDURE '$proc'");
1365 $row2 = $db->fetch_row($resqlcreateproc);
1366 fwrite($handle,"\n".$row2[2]."$$\n\n");
1367 fwrite($handle,"DELIMITER ;\n\n");
1368 }
1369 }
1370 */
1371 /* Backup Procedure structure*/
1372
1373 // Write the footer (restore the previous database settings)
1374 $sqlfooter = "\n\n";
1375 if (GETPOST("nobin_use_transaction")) {
1376 $sqlfooter .= "COMMIT;\n";
1377 }
1378 if (GETPOST("nobin_disable_fk")) {
1379 $sqlfooter .= "SET FOREIGN_KEY_CHECKS=1;\n";
1380 }
1381 $sqlfooter .= "\n\n-- Dump completed on ".date('Y-m-d G-i-s');
1382 fwrite($handle, $sqlfooter);
1383
1384 fclose($handle);
1385
1386 $db->commit();
1387
1388 return 1;
1389 }
1390
1404 public function sendBackup($sendto = '', $from = '', $subject = '', $message = '', $filename = '', $filter = '', $sizelimit = 100000000)
1405 {
1406 global $conf, $langs;
1408
1409 $filepath = '';
1410 $filesize = -1;
1411 $output = '';
1412 $error = 0;
1413 $mimetype = '';
1414
1415 if (!empty($from)) {
1416 $from = dol_escape_htmltag($from);
1417 } elseif (getDolGlobalString('MAIN_MAIL_EMAIL_FROM')) {
1418 $from = dol_escape_htmltag(getDolGlobalString('MAIN_MAIL_EMAIL_FROM'));
1419 } else {
1420 $error++;
1421 }
1422
1423 if (!empty($sendto)) {
1424 $sendto = dol_escape_htmltag($sendto);
1425 } elseif (getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')) {
1426 $sendto = dol_escape_htmltag(getDolGlobalString('MAIN_INFO_SOCIETE_MAIL'));
1427 } else {
1428 $error++;
1429 }
1430
1431 if (!empty($subject)) {
1432 $subject = dol_escape_htmltag($subject);
1433 } else {
1434 $subject = dol_escape_htmltag($langs->trans('MakeSendLocalDatabaseDumpShort'));
1435 }
1436
1437 if (empty($message)) {
1438 $message = dol_escape_htmltag($langs->trans('MakeSendLocalDatabaseDumpShort'));
1439 }
1440
1441 $tmpfiles = array();
1442 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1443 if ($filename) {
1444 if (dol_is_file($conf->admin->dir_output.'/backup/'.$filename)) {
1445 $tmpfiles = dol_most_recent_file($conf->admin->dir_output.'/backup', $filename);
1446 }
1447 } else {
1448 $tmpfiles = dol_most_recent_file($conf->admin->dir_output.'/backup', $filter);
1449 }
1450 if ($tmpfiles && is_array($tmpfiles)) {
1451 foreach ($tmpfiles as $key => $val) {
1452 if ($key == 'fullname') {
1453 $filepath = array($val);
1454 $filesize = dol_filesize($val);
1455 }
1456 if ($key == 'type') {
1457 $mimetype = array($val);
1458 }
1459 if ($key == 'relativename') {
1460 $filename = array($val);
1461 }
1462 }
1463 }
1464
1465 if ($filepath) {
1466 if ($filesize > $sizelimit) {
1467 $message .= '<br>'.$langs->trans("BackupIsTooLargeSend");
1468 $documenturl = $dolibarr_main_url_root.'/document.php?modulepart=systemtools&atachement=1&file=backup/'.urlencode($filename[0]);
1469 $message .= '<br><a href='.$documenturl.'>Download link</a>';
1470 $filepath = '';
1471 $mimetype = '';
1472 $filename = '';
1473 }
1474 } else {
1475 $output = 'No backup file found';
1476 $error++;
1477 }
1478
1479 $mailfile = null;
1480 if (!$error) {
1481 include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
1482 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, 1);
1483 // $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, 1);
1484 if ($mailfile->error) {
1485 $error++;
1486 $output = $mailfile->error;
1487 }
1488 }
1489
1490 $result = false;
1491 $output = '';
1492 if (!$error && $mailfile !== null) {
1493 $result = $mailfile->sendfile();
1494 if (!$result) {
1495 $error++;
1496 $output = $mailfile->error;
1497 }
1498 }
1499
1500 dol_syslog(__METHOD__, LOG_DEBUG);
1501
1502 $this->error = "Error sending backup file ".((string) $error);
1503 $this->output = $output;
1504
1505 if ($result) {
1506 return 0;
1507 } else {
1508 return -1;
1509 }
1510 }
1511
1519 public function cleanUnfinishedCronjob()
1520 {
1521 global $db, $user;
1522 dol_syslog("Utils::cleanUnfinishedCronjob Starting cleaning");
1523
1524 // Import Cronjob class if not present
1525 require_once DOL_DOCUMENT_ROOT . '/cron/class/cronjob.class.php';
1526
1527 // Get this job object
1528 $this_job = new Cronjob($db);
1529 $this_job->fetch(-1, 'Utils', 'cleanUnfinishedCronjob');
1530 if (empty($this_job->id) || !empty($this_job->error)) {
1531 dol_syslog("Utils::cleanUnfinishedCronjob Unable to fetch himself: ".$this_job->error, LOG_ERR);
1532 return -1;
1533 }
1534
1535 // Set this job processing to 0 to avoid being locked by his processing state
1536 $this_job->processing = 0;
1537 if ($this_job->update($user) < 0) {
1538 dol_syslog("Utils::cleanUnfinishedCronjob Unable to update himself: ".implode(', ', $this_job->errors), LOG_ERR);
1539 return -1;
1540 }
1541
1542 $cron_job = new Cronjob($db);
1543 $cron_job->fetchAll('DESC', 't.rowid', 100, 0, 1, [], 1); // Fetch jobs that are currently running
1544
1545 // Iterate over all jobs in processing (this can't be this job since his state is set to 0 before)
1546 foreach ($cron_job->lines as $job_line) {
1547 // Avoid job with no PID
1548 if (empty($job_line->pid)) {
1549 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." don't have a PID", LOG_DEBUG);
1550 continue;
1551 }
1552
1553 $job = new Cronjob($db);
1554 $job->fetch($job_line->id);
1555 if (empty($job->id) || !empty($job->error)) {
1556 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." can't be fetch: ".$job->error, LOG_ERR);
1557 continue;
1558 }
1559
1560 // Calling posix_kill with the 0 kill signal will return true if the process is running, false otherwise.
1561 if (! posix_kill($job->pid, 0)) {
1562 // Clean processing and pid values
1563 $job->processing = 0;
1564 $job->pid = null;
1565
1566 // Set last result as an error and add the reason on the last output
1567 $job->lastresult = strval(-1);
1568 $job->lastoutput = 'Job killed by job cleanUnfinishedCronjob';
1569
1570 if ($job->update($user) < 0) {
1571 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." can't be updated: ".implode(', ', $job->errors), LOG_ERR);
1572 continue;
1573 }
1574 dol_syslog("Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id." cleaned");
1575 }
1576 }
1577
1578 dol_syslog("Utils::cleanUnfinishedCronjob Cleaning completed");
1579 return 0;
1580 }
1581}
global $dolibarr_main_url_root
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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.
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
global $mysoc
dol_most_recent_file($dir, $regexfilter='', $excludefilter=array('(\.meta|_preview.*\.png) $', '^\.'), $nohook=0, $mode=0, $limit=0)
Return file(s) into a directory (by default most recent)
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:66
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).
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)
Definition html.lib.php:183
$moduleobj
Definition index.php:3694
dolMd2Asciidoc($content, $parser='dolibarr', $replaceimagepath=null)
Function to parse MD content into ASCIIDOC.