62 public function purgeFiles($choices =
'tempfilesold+logfiles', $nbsecondsold = 86400)
64 global $conf, $langs, $dolibarr_main_data_root;
66 $langs->load(
"admin");
68 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
70 if (empty($choices)) {
71 $choices =
'tempfilesold+logfiles';
73 if ($choices ==
'allfiles' && $nbsecondsold > 0) {
74 $choices =
'allfilesold';
77 dol_syslog(
"Utils::purgeFiles choice=".$choices, LOG_DEBUG);
84 $choicesarray = preg_split(
'/[\+,]/', $choices);
85 foreach ($choicesarray as $choice) {
87 $filesarray = array();
89 if ($choice ==
'tempfiles' || $choice ==
'tempfilesold') {
91 if ($dolibarr_main_data_root) {
92 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"directories", 1,
'^temp$',
'',
'name', SORT_ASC, 2, 0,
'', 1);
94 if ($choice ==
'tempfilesold') {
95 foreach ($filesarray as $key => $val) {
96 if ($val[
'date'] > ($now - ($nbsecondsold))) {
97 unset($filesarray[$key]);
104 if ($choice ==
'allfiles') {
106 if ($dolibarr_main_data_root) {
107 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"all", 0,
'',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
111 if ($choice ==
'allfilesold') {
113 if ($dolibarr_main_data_root) {
114 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"files", 1,
'',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1, $nbsecondsold);
118 if ($choice ==
'logfile' || $choice ==
'logfiles') {
120 if ($dolibarr_main_data_root) {
121 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"files", 0,
'.*\.log[\.0-9]*(\.gz)?$',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
125 $filelog = $conf->global->SYSLOG_FILE;
126 $filelog = preg_replace(
'/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
128 $alreadyincluded =
false;
129 foreach ($filesarray as $tmpcursor) {
130 if ($tmpcursor[
'fullname'] == $filelog) {
131 $alreadyincluded =
true;
134 if (!$alreadyincluded) {
135 $filesarray[] = array(
'fullname'=>$filelog,
'type'=>
'file');
140 if (is_array($filesarray) && count($filesarray)) {
141 foreach ($filesarray as $key => $value) {
143 if ($filesarray[$key][
'type'] ==
'dir') {
145 $tmpcountdeleted = 0;
149 if (!in_array($filesarray[$key][
'fullname'], array($conf->api->dir_temp, $conf->user->dir_temp))) {
151 $countdeleted += $tmpcountdeleted;
153 } elseif ($filesarray[$key][
'type'] ==
'file') {
154 if ($choice !=
'allfilesold' || $filesarray[$key][
'date'] < ($now - $nbsecondsold)) {
156 if ($filesarray[$key][
'fullname'] != $filelog || $choice ==
'logfile' || $choice ==
'logfiles') {
171 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmdirectory.class.php';
173 $result = $ecmdirstatic->refreshcachenboffile(1);
179 $langs->load(
"admin");
180 $this->output = $langs->trans(
"PurgeNDirectoriesDeleted", $countdeleted);
181 if ($count > $countdeleted) {
182 $this->output .=
'<br>'.$langs->trans(
"PurgeNDirectoriesFailed", ($count - $countdeleted));
185 $this->output = $langs->trans(
"PurgeNothingToDelete").(in_array(
'tempfilesold', $choicesarray) ?
' (older than 24h for temp files)' :
'');
212 public function dumpDatabase($compression =
'none', $type =
'auto', $usedefault = 1, $file =
'auto', $keeplastnfiles = 0, $execmethod = 0, $lowmemorydump = 0)
214 global
$db, $conf, $langs, $dolibarr_main_data_root;
215 global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
216 global $dolibarr_main_db_character_set;
218 $langs->load(
"admin");
220 dol_syslog(
"Utils::dumpDatabase type=".$type.
" compression=".$compression.
" file=".$file, LOG_DEBUG);
221 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
227 if (!in_array($compression, array(
'none',
'gz',
'bz',
'zip',
'zstd'))) {
228 $langs->load(
"errors");
229 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $compression,
"Compression");
234 if ($type ==
'auto') {
235 $type = $this->db->type;
237 if (!in_array($type, array(
'postgresql',
'pgsql',
'mysql',
'mysqli',
'mysqlnobin'))) {
238 $langs->load(
"errors");
239 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $type,
"Basetype");
244 if ($file ==
'auto') {
247 if (in_array($type, array(
'mysql',
'mysqli'))) {
248 $prefix =
'mysqldump';
252 if (in_array($type, array(
'pgsql'))) {
259 $outputdir = $conf->admin->dir_output.
'/backup';
264 if ($type ==
'mysql' || $type ==
'mysqli') {
265 if (empty($conf->global->SYSTEMTOOLS_MYSQLDUMP)) {
266 $cmddump =
$db->getPathOfDump();
268 $cmddump = $conf->global->SYSTEMTOOLS_MYSQLDUMP;
270 if (empty($cmddump)) {
271 $this->error =
"Failed to detect command to use for mysqldump. Try a manual backup before to set path of command.";
275 $outputfile = $outputdir.
'/'.$file;
277 $compression = $compression ? $compression :
'none';
278 if ($compression ==
'gz') {
279 $outputfile .=
'.gz';
280 } elseif ($compression ==
'bz') {
281 $outputfile .=
'.bz2';
282 } elseif ($compression ==
'zstd') {
283 $outputfile .=
'.zst';
285 $outputerror = $outputfile.
'.err';
286 dol_mkdir($conf->admin->dir_output.
'/backup');
290 $command = preg_replace(
'/(\$|%)/',
'', $command);
291 if (preg_match(
"/\s/", $command)) {
292 $command = escapeshellarg($command);
296 $param = $dolibarr_main_db_name.
" -h ".$dolibarr_main_db_host;
297 $param .=
" -u ".$dolibarr_main_db_user;
298 if (!empty($dolibarr_main_db_port)) {
299 $param .=
" -P ".$dolibarr_main_db_port.
" --protocol=tcp";
301 if (
GETPOST(
"use_transaction",
"alpha")) {
302 $param .=
" --single-transaction";
304 if (
GETPOST(
"disable_fk",
"alpha") || $usedefault) {
307 if (
GETPOST(
"sql_compat",
"alpha") &&
GETPOST(
"sql_compat",
"alpha") !=
'NONE') {
308 $param .=
" --compatible=".escapeshellarg(
GETPOST(
"sql_compat",
"alpha"));
310 if (
GETPOST(
"drop_database",
"alpha")) {
311 $param .=
" --add-drop-database";
313 if (
GETPOST(
"use_mysql_quick_param",
"alpha")) {
314 $param .=
" --quick";
316 if (
GETPOST(
"sql_structure",
"alpha") || $usedefault) {
317 if (
GETPOST(
"drop",
"alpha") || $usedefault) {
318 $param .=
" --add-drop-table=TRUE";
320 $param .=
" --add-drop-table=FALSE";
325 if (
GETPOST(
"disable-add-locks",
"alpha")) {
326 $param .=
" --add-locks=FALSE";
328 if (
GETPOST(
"sql_data",
"alpha") || $usedefault) {
329 $param .=
" --tables";
330 if (
GETPOST(
"showcolumns",
"alpha") || $usedefault) {
333 if (
GETPOST(
"extended_ins",
"alpha") || $usedefault) {
336 $param .=
" --skip-extended-insert";
338 if (
GETPOST(
"delayed",
"alpha")) {
339 $param .=
" --delayed-insert";
341 if (
GETPOST(
"sql_ignore",
"alpha")) {
342 $param .=
" --insert-ignore";
344 if (
GETPOST(
"hexforbinary",
"alpha") || $usedefault) {
345 $param .=
" --hex-blob";
350 if ($dolibarr_main_db_character_set ==
'utf8mb4') {
352 $param .=
" --default-character-set=utf8mb4 --no-tablespaces";
354 $param .=
" --default-character-set=utf8 --no-tablespaces";
356 $paramcrypted = $param;
357 $paramclear = $param;
358 if (!empty($dolibarr_main_db_pass)) {
359 $paramcrypted .=
' -p"'.preg_replace(
'/./i',
'*', $dolibarr_main_db_pass).
'"';
360 $paramclear .=
' -p"'.str_replace(array(
'"',
'`',
'$'), array(
'\"',
'\`',
'\$'), $dolibarr_main_db_pass).
'"';
366 $fullcommandcrypted = $command.
" ".$paramcrypted.
" 2>&1";
367 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
368 if (!$lowmemorydump) {
369 if ($compression ==
'none') {
370 $handle = fopen($outputfile,
'w');
371 } elseif ($compression ==
'gz') {
372 $handle = gzopen($outputfile,
'w');
373 } elseif ($compression ==
'bz') {
374 $handle = bzopen($outputfile,
'w');
375 } elseif ($compression ==
'zstd') {
376 $handle = fopen($outputfile,
'w');
379 if ($compression ==
'none') {
380 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).
'"';
381 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).
'"';
383 } elseif ($compression ==
'gz') {
384 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).
'"';
385 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).
'"';
386 $paramcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip';
388 } elseif ($compression ==
'bz') {
389 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).
'"';
390 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).
'"';
391 $paramcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2';
393 } elseif ($compression ==
'zstd') {
394 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).
'"';
395 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).
'"';
396 $paramcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd';
403 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
404 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
406 if (empty($execmethod)) {
410 dol_syslog(
"Utils::dumpDatabase execmethod=".$execmethod.
" command:".$fullcommandcrypted, LOG_INFO);
415 if (!empty($MemoryLimit)) {
416 @ini_set(
'memory_limit', $MemoryLimit);
420 if ($execmethod == 1) {
421 $output_arr = array();
424 exec($fullcommandclear, $output_arr, $retval);
432 $langs->load(
"errors");
433 dol_syslog(
"Datadump retval after exec=".$retval, LOG_ERR);
434 $errormsg =
'Error '.$retval;
438 if (!empty($output_arr)) {
439 foreach ($output_arr as $key => $read) {
441 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read)) {
446 if (!$lowmemorydump) {
447 fwrite($handle, $read.($execmethod == 2 ?
'' :
"\n"));
448 if (preg_match(
'/'.preg_quote(
'-- Dump completed',
'/').
'/i', $read)) {
450 } elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES',
'/').
'/i', $read)) {
457 } elseif ($lowmemorydump) {
463 if ($execmethod == 2) {
464 $handlein = popen($fullcommandclear,
'r');
467 while (!feof($handlein)) {
469 $read = fgets($handlein);
471 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read)) {
474 fwrite($handle, $read);
475 if (preg_match(
'/'.preg_quote(
'-- Dump completed').
'/i', $read)) {
477 } elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES').
'/i', $read)) {
485 if (!$lowmemorydump) {
486 if ($compression ==
'none') {
488 } elseif ($compression ==
'gz') {
490 } elseif ($compression ==
'bz') {
492 } elseif ($compression ==
'zstd') {
499 $langs->load(
"errors");
500 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
501 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
505 if ($compression ==
'none') {
506 $handle = fopen($outputfile,
'r');
507 } elseif ($compression ==
'gz') {
508 $handle = gzopen($outputfile,
'r');
509 } elseif ($compression ==
'bz') {
510 $handle = bzopen($outputfile,
'r');
511 } elseif ($compression ==
'zstd') {
512 $handle = fopen($outputfile,
'r');
516 $errormsg = fgets($handle, 2048);
520 if ($compression ==
'none') {
522 } elseif ($compression ==
'gz') {
524 } elseif ($compression ==
'bz') {
526 } elseif ($compression ==
'zstd') {
529 if ($ok && preg_match(
'/^-- (MySql|MariaDB)/i', $errormsg)) {
535 @rename($outputfile, $outputerror);
538 $langs->load(
"errors");
539 $errormsg = $langs->trans(
"ErrorFailedToRunExternalCommand");
545 $this->output = $errormsg;
546 $this->error = $errormsg;
547 $this->result = array(
"commandbackuplastdone" => $command.
" ".$paramcrypted,
"commandbackuptorun" =>
"");
552 if ($type ==
'mysqlnobin') {
553 $outputfile = $outputdir.
'/'.$file;
554 $outputfiletemp = $outputfile.
'-TMP.sql';
556 $compression = $compression ? $compression :
'none';
557 if ($compression ==
'gz') {
558 $outputfile .=
'.gz';
560 if ($compression ==
'bz') {
561 $outputfile .=
'.bz2';
563 $outputerror = $outputfile.
'.err';
564 dol_mkdir($conf->admin->dir_output.
'/backup');
566 if ($compression ==
'gz' or $compression ==
'bz') {
567 $this->backupTables($outputfiletemp);
568 dol_compress_file($outputfiletemp, $outputfile, $compression);
569 unlink($outputfiletemp);
571 $this->backupTables($outputfile);
575 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" =>
"");
579 if ($type ==
'postgresql' || $type ==
'pgsql') {
580 $cmddump = $conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
582 $outputfile = $outputdir.
'/'.$file;
584 $compression = $compression ? $compression :
'none';
585 if ($compression ==
'gz') {
586 $outputfile .=
'.gz';
588 if ($compression ==
'bz') {
589 $outputfile .=
'.bz2';
591 $outputerror = $outputfile.
'.err';
592 dol_mkdir($conf->admin->dir_output.
'/backup');
596 $command = preg_replace(
'/(\$|%)/',
'', $command);
597 if (preg_match(
"/\s/", $command)) {
598 $command = escapeshellarg($command);
604 $param .=
" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
605 $param .=
" -U ".$dolibarr_main_db_user;
606 if (!empty($dolibarr_main_db_port)) {
607 $param .=
" -p ".$dolibarr_main_db_port;
610 $param .=
" --disable-dollar-quoting";
612 if (
GETPOST(
"drop_database")) {
615 if (
GETPOST(
"sql_structure")) {
617 $param .=
" --add-drop-table";
624 if (!
GETPOST(
"sql_structure")) {
631 $param .=
' -f "'.$outputfile.
'"';
633 if ($compression ==
'gz') {
637 $paramcrypted = $param;
638 $paramclear = $param;
644 $paramcrypted .=
" -w ".$dolibarr_main_db_name;
645 $paramclear .=
" -w ".$dolibarr_main_db_name;
648 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" => $command.
" ".$paramcrypted);
652 if (!$errormsg && $keeplastnfiles > 0) {
653 $tmpfiles =
dol_dir_list($conf->admin->dir_output.
'/backup',
'files', 0,
'',
'(\.err|\.old|\.sav)$',
'date', SORT_DESC);
655 if (is_array($tmpfiles)) {
656 foreach ($tmpfiles as $key => $val) {
658 if ($i <= $keeplastnfiles) {
666 return ($errormsg ? -1 : 0);
684 public function executeCLI($command, $outputfile, $execmethod = 0, $redirectionfile =
null, $noescapecommand = 0, $redirectionfileerr =
null)
686 global $conf, $langs;
692 if (empty($noescapecommand)) {
693 $command = escapeshellcmd($command);
696 if ($redirectionfile) {
697 $command .=
" > ".dol_sanitizePathName($redirectionfile);
700 if ($redirectionfileerr && ($redirectionfileerr != $redirectionfile)) {
702 $command .=
" 2> ".dol_sanitizePathName($redirectionfileerr);
707 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
708 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
710 if (empty($execmethod)) {
714 dol_syslog(
"Utils::executeCLI execmethod=".$execmethod.
" command=".$command, LOG_DEBUG);
715 $output_arr = array();
717 if ($execmethod == 1) {
719 exec($command, $output_arr, $retval);
722 $langs->load(
"errors");
723 dol_syslog(
"Utils::executeCLI retval after exec=".$retval, LOG_ERR);
724 $error =
'Error '.$retval;
727 if ($execmethod == 2) {
728 $handle = fopen($outputfile,
'w+b');
730 dol_syslog(
"Utils::executeCLI run command ".$command);
731 $handlein = popen($command,
'r');
732 while (!feof($handlein)) {
733 $read = fgets($handlein);
734 fwrite($handle, $read);
735 $output_arr[] = $read;
744 if (is_array($output_arr) && count($output_arr) > 0) {
745 foreach ($output_arr as $val) {
746 $output .= $val.($execmethod == 2 ?
'' :
"\n");
750 dol_syslog(
"Utils::executeCLI result=".$result.
" output=".$output.
" error=".$error, LOG_DEBUG);
752 return array(
'result'=>$result,
'output'=>$output,
'error'=>$error);
763 global $conf, $langs, $user, $mysoc;
768 $modulelowercase = strtolower($module);
772 $dir = $dirins.
'/'.$modulelowercase;
777 dol_include_once($modulelowercase.
'/core/modules/mod'.$module.
'.class.php');
778 $class =
'mod'.$module;
780 if (class_exists($class)) {
782 $moduleobj =
new $class($this->db);
789 $langs->load(
"errors");
790 dol_print_error($langs->trans(
"ErrorFailedToLoadModuleDescriptorForXXX", $module));
794 $arrayversion = explode(
'.', $moduleobj->version, 3);
795 if (count($arrayversion)) {
796 $FILENAMEASCII = strtolower($module).
'.asciidoc';
797 $FILENAMEDOC = strtolower($module).
'.html';
798 $FILENAMEDOCPDF = strtolower($module).
'.pdf';
801 $dirofmoduledoc =
dol_buildpath(strtolower($module), 0).
'/doc';
802 $dirofmoduletmp =
dol_buildpath(strtolower($module), 0).
'/doc/temp';
803 $outputfiledoc = $dirofmoduledoc.
'/'.$FILENAMEDOC;
804 if ($dirofmoduledoc) {
811 if (!is_writable($dirofmoduletmp)) {
812 $this->error =
'Dir '.$dirofmoduletmp.
' does not exists or is not writable';
816 if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR) && empty($conf->global->MODULEBUILDER_ASCIIDOCTORPDF)) {
817 $this->error =
'Setup of module ModuleBuilder not complete';
822 dol_copy($dirofmodule.
'/README.md', $dirofmoduletmp.
'/README.md', 0, 1);
823 dol_copy($dirofmodule.
'/ChangeLog.md', $dirofmoduletmp.
'/ChangeLog.md', 0, 1);
826 $arrayreplacement = array();
827 $arrayreplacement[
'/^#\s.*/m'] =
'';
828 $arrayreplacement[
'/^#/m'] =
'##';
830 dolReplaceInFile($dirofmoduletmp.
'/README.md', $arrayreplacement,
'', 0, 0, 1);
831 dolReplaceInFile($dirofmoduletmp.
'/ChangeLog.md', $arrayreplacement,
'', 0, 0, 1);
834 $destfile = $dirofmoduletmp.
'/'.$FILENAMEASCII;
836 $fhandle = fopen($destfile,
'w+');
838 $specs =
dol_dir_list(
dol_buildpath(strtolower($module).
'/doc', 0),
'files', 1,
'(\.md|\.asciidoc)$', array(
'\/temp\/'));
841 foreach ($specs as $spec) {
842 if (preg_match(
'/notindoc/', $spec[
'relativename'])) {
845 if (preg_match(
'/example/', $spec[
'relativename'])) {
848 if (preg_match(
'/disabled/', $spec[
'relativename'])) {
852 $pathtofile = strtolower($module).
'/doc/'.$spec[
'relativename'];
853 $format =
'asciidoc';
854 if (preg_match(
'/\.md$/i', $spec[
'name'])) {
855 $format =
'markdown';
858 $filecursor = @file_get_contents($spec[
'fullname']);
860 fwrite($fhandle, ($i ?
"\n<<<\n\n" :
"").$filecursor.
"\n");
862 $this->error =
'Failed to concat content of file '.$spec[
'fullname'];
871 $contentreadme = file_get_contents($dirofmoduletmp.
'/README.md');
872 $contentchangelog = file_get_contents($dirofmoduletmp.
'/ChangeLog.md');
874 include DOL_DOCUMENT_ROOT.
'/core/lib/parsemd.lib.php';
877 $arrayreplacement = array(
878 'mymodule'=>strtolower($module),
880 'MYMODULE'=>strtoupper($module),
881 'My module'=>$module,
882 'my module'=>$module,
883 'Mon module'=>$module,
884 'mon module'=>$module,
885 'htdocs/modulebuilder/template'=>strtolower($module),
886 '__MYCOMPANY_NAME__'=>$mysoc->name,
887 '__KEYWORDS__'=>$module,
888 '__USER_FULLNAME__'=>$user->getFullName($langs),
889 '__USER_EMAIL__'=>$user->email,
891 '---Put here your own copyright and developer email---'=>
dol_print_date($now,
'dayrfc').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
''),
892 '__DATA_SPECIFICATION__'=>
'Not yet available',
901 $currentdir = getcwd();
904 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
905 $utils =
new Utils($this->db);
908 $command = $conf->global->MODULEBUILDER_ASCIIDOCTOR.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOC;
909 $outfile = $dirofmoduletmp.
'/out.tmp';
911 $resarray = $utils->executeCLI($command, $outfile);
912 if ($resarray[
'result'] !=
'0') {
913 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
914 $this->errors[] = $this->error;
916 $result = ($resarray[
'result'] == 0) ? 1 : 0;
917 if ($result < 0 && empty($this->errors)) {
918 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOC);
919 $this->errors[] = $this->error;
923 $command = $conf->global->MODULEBUILDER_ASCIIDOCTORPDF.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOCPDF;
924 $outfile = $dirofmoduletmp.
'/outpdf.tmp';
925 $resarray = $utils->executeCLI($command, $outfile);
926 if ($resarray[
'result'] !=
'0') {
927 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
928 $this->errors[] = $this->error;
930 $result = ($resarray[
'result'] == 0) ? 1 : 0;
931 if ($result < 0 && empty($this->errors)) {
932 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOCPDF);
933 $this->errors[] = $this->error;
948 $langs->load(
"errors");
949 $this->error = $langs->trans(
"ErrorCheckVersionIsDefined");
966 if (empty($conf->loghandlers[
'mod_syslog_file'])) {
970 if (!function_exists(
'gzopen')) {
971 $this->error =
'Support for gzopen not available in this PHP';
979 if (empty($conf->global->SYSLOG_FILE)) {
980 $mainlogdir = DOL_DATA_ROOT;
981 $mainlog =
'dolibarr.log';
983 $mainlogfull = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
984 $mainlogdir = dirname($mainlogfull);
985 $mainlog = basename($mainlogfull);
988 $tabfiles =
dol_dir_list(DOL_DATA_ROOT,
'files', 0,
'^(dolibarr_.+|odt2pdf)\.log$');
989 $tabfiles[] = array(
'name' => $mainlog,
'path' => $mainlogdir);
991 foreach ($tabfiles as $file) {
992 $logname = $file[
'name'];
993 $logpath = $file[
'path'];
998 $filter =
'^'.preg_quote($logname,
'/').
'\.([0-9]+)\.gz$';
1000 $gzfilestmp =
dol_dir_list($logpath,
'files', 0, $filter);
1003 foreach ($gzfilestmp as $gzfile) {
1004 $tabmatches = array();
1005 preg_match(
'/'.$filter.
'/i', $gzfile[
'name'], $tabmatches);
1007 $numsave = intval($tabmatches[1]);
1009 $gzfiles[$numsave] = $gzfile;
1012 krsort($gzfiles, SORT_NUMERIC);
1014 foreach ($gzfiles as $numsave => $dummy) {
1015 if (
dol_is_file($logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz')) {
1019 if ($numsave >= $nbSaves) {
1020 dol_delete_file($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', 0, 0, 0,
null,
false, 0);
1022 dol_move($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', $logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz', 0, 1, 0, 0);
1029 $gzfilehandle = gzopen($logpath.
'/'.$logname.
'.1.gz',
'wb9');
1031 if (empty($gzfilehandle)) {
1032 $this->error =
'Failted to open file '.$logpath.
'/'.$logname.
'.1.gz';
1036 $sourcehandle = fopen($logpath.
'/'.$logname,
'r');
1038 if (empty($sourcehandle)) {
1039 $this->error =
'Failed to open file '.$logpath.
'/'.$logname;
1043 while (!feof($sourcehandle)) {
1044 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024));
1047 fclose($sourcehandle);
1048 gzclose($gzfilehandle);
1050 dolChmod($logpath.
'/'.$logname.
'.1.gz');
1056 $newlog = fopen($logpath.
'/'.$logname,
'a+');
1064 $this->output =
'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.
' files) done.';
1078 public function backupTables($outputfile, $tables =
'*')
1084 if (is_a(
$db,
'DoliDBMysqli')) {
1086 $db->db->set_charset(
'utf8');
1089 $db->query(
'SET NAMES utf8');
1090 $db->query(
'SET CHARACTER SET utf8');
1094 if ($tables ==
'*') {
1096 $result =
$db->query(
'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1097 while ($row =
$db->fetch_row($result)) {
1098 $tables[] = $row[0];
1101 $tables = is_array($tables) ? $tables : explode(
',', $tables);
1105 $handle = fopen($outputfile,
'w+');
1106 if (fwrite($handle,
'') ===
false) {
1107 $langs->load(
"errors");
1108 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
1109 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
1115 $sqlhead .=
"-- ".$db::LABEL.
" dump via php with Dolibarr ".DOL_VERSION.
"
1117 -- Host: ".
$db->db->host_info.
" Database: ".
$db->database_name.
"
1118 -- ------------------------------------------------------
1119 -- Server version ".
$db->db->server_info.
"
1134 if (
GETPOST(
"nobin_disable_fk")) {
1135 $sqlhead .=
"SET FOREIGN_KEY_CHECKS=0;\n";
1138 if (
GETPOST(
"nobin_use_transaction")) {
1139 $sqlhead .=
"SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1142 fwrite($handle, $sqlhead);
1145 if (
GETPOST(
"nobin_sql_ignore")) {
1146 $ignore =
'IGNORE ';
1149 if (
GETPOST(
"nobin_delayed")) {
1150 $delayed =
'DELAYED ';
1154 foreach ($tables as $table) {
1156 fwrite($handle,
"\n--\n-- Table structure for table `".$table.
"`\n--\n");
1159 fwrite($handle,
"DROP TABLE IF EXISTS `".$table.
"`;\n");
1161 fwrite($handle,
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1162 fwrite($handle,
"/*!40101 SET character_set_client = utf8 */;\n");
1163 $resqldrop =
$db->query(
'SHOW CREATE TABLE '.$table);
1164 $row2 =
$db->fetch_row($resqldrop);
1165 if (empty($row2[1])) {
1166 fwrite($handle,
"\n-- WARNING: Show create table ".$table.
" return empy string when it should not.\n");
1168 fwrite($handle, $row2[1].
";\n");
1172 fwrite($handle,
"\n--\n-- Dumping data for table `".$table.
"`\n--\n");
1173 if (!
GETPOST(
"nobin_nolocks")) {
1174 fwrite($handle,
"LOCK TABLES `".$table.
"` WRITE;\n");
1176 if (
GETPOST(
"nobin_disable_fk")) {
1177 fwrite($handle,
"ALTER TABLE `".$table.
"` DISABLE KEYS;\n");
1179 fwrite($handle,
"/*!40000 ALTER TABLE `".$table.
"` DISABLE KEYS */;\n");
1182 $sql =
"SELECT * FROM ".$table;
1184 while ($row =
$db->fetch_row($result)) {
1186 fwrite($handle,
"INSERT ".$delayed.$ignore.
"INTO ".$table.
" VALUES (");
1187 $columns = count($row);
1188 for ($j = 0; $j < $columns; $j++) {
1190 if ($row[$j] ==
null && !is_string($row[$j])) {
1193 } elseif (is_string($row[$j]) && $row[$j] ==
'') {
1196 } elseif (is_numeric($row[$j]) && !strcmp($row[$j], $row[$j] + 0)) {
1200 $row[$j] = addslashes($row[$j]);
1201 $row[$j] = preg_replace(
"#\n#",
"\\n", $row[$j]);
1202 $row[$j] =
"'".$row[$j].
"'";
1205 fwrite($handle, implode(
',', $row).
");\n");
1207 if (
GETPOST(
"nobin_disable_fk")) {
1208 fwrite($handle,
"ALTER TABLE `".$table.
"` ENABLE KEYS;\n");
1210 if (!
GETPOST(
"nobin_nolocks")) {
1211 fwrite($handle,
"UNLOCK TABLES;\n");
1213 fwrite($handle,
"\n\n\n");
1237 $sqlfooter =
"\n\n";
1238 if (
GETPOST(
"nobin_use_transaction")) {
1239 $sqlfooter .=
"COMMIT;\n";
1241 if (
GETPOST(
"nobin_disable_fk")) {
1242 $sqlfooter .=
"SET FOREIGN_KEY_CHECKS=1;\n";
1244 $sqlfooter .=
"\n\n-- Dump completed on ".date(
'Y-m-d G-i-s');
1245 fwrite($handle, $sqlfooter);
1265 public function sendBackup($sendto =
'', $from =
'', $subject =
'', $message =
'', $filename =
'', $filter =
'', $sizelimit = 100000000)
1267 global $conf, $langs;
1273 if (!empty($from)) {
1275 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1281 if (!empty($sendto)) {
1283 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1289 if (!empty($subject)) {
1295 if (empty($message)) {
1299 $tmpfiles = array();
1300 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1302 if (
dol_is_file($conf->admin->dir_output.
'/backup/'.$filename)) {
1308 if ($tmpfiles && is_array($tmpfiles)) {
1309 foreach ($tmpfiles as $key => $val) {
1310 if ($key ==
'fullname') {
1311 $filepath = array($val);
1314 if ($key ==
'type') {
1315 $mimetype = array($val);
1317 if ($key ==
'relativename') {
1318 $filename = array($val);
1324 if ($filesize > $sizelimit) {
1325 $message .=
'<br>'.$langs->trans(
"BackupIsTooLargeSend");
1326 $documenturl = $dolibarr_main_url_root.
'/document.php?modulepart=systemtools&atachement=1&file=backup/'.urlencode($filename[0]);
1327 $message .=
'<br><a href='.$documenturl.
'>Lien de téléchargement</a>';
1333 $output =
'No backup file found';
1338 include_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1339 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename,
'',
'', 0, -1);
1340 if ($mailfile->error) {
1342 $output = $mailfile->error;
1347 $result = $mailfile->sendfile();
1350 $output = $mailfile->error;
1356 $this->error = $error;
1357 $this->output = $output;
1359 if ($result ==
true) {
1376 dol_syslog(
"Utils::cleanUnfinishedCronjob Starting cleaning");
1383 $this_job->fetch(-1,
'Utils',
'cleanUnfinishedCronjob');
1384 if (empty($this_job->id) || !empty($this_job->error)) {
1385 dol_syslog(
"Utils::cleanUnfinishedCronjob Unable to fetch himself: ".$this_job->error, LOG_ERR);
1390 $this_job->processing = 0;
1391 if ($this_job->update($user) < 0) {
1392 dol_syslog(
"Utils::cleanUnfinishedCronjob Unable to update himself: ".implode(
', ', $this_job->errors), LOG_ERR);
1397 $cron_job->fetchAll(
'DESC',
't.rowid', 100, 0, 1,
'', 1);
1400 foreach ($cron_job->lines as $job_line) {
1402 if (empty($job_line->pid)) {
1403 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" don't have a PID", LOG_DEBUG);
1408 $job->fetch($job_line->id);
1409 if (empty($job->id) || !empty($job->error)) {
1410 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" can't be fetch: ".$job->error, LOG_ERR);
1415 if (! posix_kill($job->pid, 0)) {
1417 $job->processing = 0;
1421 $job->lastresult = -1;
1422 $job->lastoutput =
'Job killed by job cleanUnfinishedCronjob';
1424 if ($job->update($user) < 0) {
1425 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" can't be updated: ".implode(
', ', $job->errors), LOG_ERR);
1428 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" cleaned");
1432 dol_syslog(
"Utils::cleanUnfinishedCronjob Cleaning completed");