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);
124 if (!empty($conf->syslog->enabled)) {
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)' :
'');
189 if (!empty($conf->api->enabled)) {
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') {
497 if (!empty($conf->global->MAIN_UMASK)) {
498 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
501 $langs->load(
"errors");
502 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
503 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
507 if ($compression ==
'none') {
508 $handle = fopen($outputfile,
'r');
509 } elseif ($compression ==
'gz') {
510 $handle = gzopen($outputfile,
'r');
511 } elseif ($compression ==
'bz') {
512 $handle = bzopen($outputfile,
'r');
513 } elseif ($compression ==
'zstd') {
514 $handle = fopen($outputfile,
'r');
518 $errormsg = fgets($handle, 2048);
522 if ($compression ==
'none') {
524 } elseif ($compression ==
'gz') {
526 } elseif ($compression ==
'bz') {
528 } elseif ($compression ==
'zstd') {
531 if ($ok && preg_match(
'/^-- (MySql|MariaDB)/i', $errormsg)) {
537 @rename($outputfile, $outputerror);
540 $langs->load(
"errors");
541 $errormsg = $langs->trans(
"ErrorFailedToRunExternalCommand");
547 $this->output = $errormsg;
548 $this->error = $errormsg;
549 $this->result = array(
"commandbackuplastdone" => $command.
" ".$paramcrypted,
"commandbackuptorun" =>
"");
554 if ($type ==
'mysqlnobin') {
555 $outputfile = $outputdir.
'/'.$file;
556 $outputfiletemp = $outputfile.
'-TMP.sql';
558 $compression = $compression ? $compression :
'none';
559 if ($compression ==
'gz') {
560 $outputfile .=
'.gz';
562 if ($compression ==
'bz') {
563 $outputfile .=
'.bz2';
565 $outputerror = $outputfile.
'.err';
566 dol_mkdir($conf->admin->dir_output.
'/backup');
568 if ($compression ==
'gz' or $compression ==
'bz') {
569 $this->backupTables($outputfiletemp);
570 dol_compress_file($outputfiletemp, $outputfile, $compression);
571 unlink($outputfiletemp);
573 $this->backupTables($outputfile);
577 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" =>
"");
581 if ($type ==
'postgresql' || $type ==
'pgsql') {
582 $cmddump = $conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
584 $outputfile = $outputdir.
'/'.$file;
586 $compression = $compression ? $compression :
'none';
587 if ($compression ==
'gz') {
588 $outputfile .=
'.gz';
590 if ($compression ==
'bz') {
591 $outputfile .=
'.bz2';
593 $outputerror = $outputfile.
'.err';
594 dol_mkdir($conf->admin->dir_output.
'/backup');
598 $command = preg_replace(
'/(\$|%)/',
'', $command);
599 if (preg_match(
"/\s/", $command)) {
600 $command = escapeshellarg($command);
606 $param .=
" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
607 $param .=
" -U ".$dolibarr_main_db_user;
608 if (!empty($dolibarr_main_db_port)) {
609 $param .=
" -p ".$dolibarr_main_db_port;
612 $param .=
" --disable-dollar-quoting";
614 if (
GETPOST(
"drop_database")) {
617 if (
GETPOST(
"sql_structure")) {
619 $param .=
" --add-drop-table";
626 if (!
GETPOST(
"sql_structure")) {
633 $param .=
' -f "'.$outputfile.
'"';
635 if ($compression ==
'gz') {
639 $paramcrypted = $param;
640 $paramclear = $param;
646 $paramcrypted .=
" -w ".$dolibarr_main_db_name;
647 $paramclear .=
" -w ".$dolibarr_main_db_name;
650 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" => $command.
" ".$paramcrypted);
654 if (!$errormsg && $keeplastnfiles > 0) {
655 $tmpfiles =
dol_dir_list($conf->admin->dir_output.
'/backup',
'files', 0,
'',
'(\.err|\.old|\.sav)$',
'date', SORT_DESC);
657 foreach ($tmpfiles as $key => $val) {
659 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;
740 if (!empty($conf->global->MAIN_UMASK)) {
741 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
746 if (is_array($output_arr) && count($output_arr) > 0) {
747 foreach ($output_arr as $val) {
748 $output .= $val.($execmethod == 2 ?
'' :
"\n");
752 dol_syslog(
"Utils::executeCLI result=".$result.
" output=".$output.
" error=".$error, LOG_DEBUG);
754 return array(
'result'=>$result,
'output'=>$output,
'error'=>$error);
765 global $conf, $langs, $user, $mysoc;
770 $modulelowercase = strtolower($module);
774 $dir = $dirins.
'/'.$modulelowercase;
779 dol_include_once($modulelowercase.
'/core/modules/mod'.$module.
'.class.php');
780 $class =
'mod'.$module;
782 if (class_exists($class)) {
784 $moduleobj =
new $class($this->
db);
791 $langs->load(
"errors");
792 dol_print_error($langs->trans(
"ErrorFailedToLoadModuleDescriptorForXXX", $module));
796 $arrayversion = explode(
'.', $moduleobj->version, 3);
797 if (count($arrayversion)) {
798 $FILENAMEASCII = strtolower($module).
'.asciidoc';
799 $FILENAMEDOC = strtolower($module).
'.html';
800 $FILENAMEDOCPDF = strtolower($module).
'.pdf';
803 $dirofmoduledoc =
dol_buildpath(strtolower($module), 0).
'/doc';
804 $dirofmoduletmp =
dol_buildpath(strtolower($module), 0).
'/doc/temp';
805 $outputfiledoc = $dirofmoduledoc.
'/'.$FILENAMEDOC;
806 if ($dirofmoduledoc) {
813 if (!is_writable($dirofmoduletmp)) {
814 $this->error =
'Dir '.$dirofmoduletmp.
' does not exists or is not writable';
818 if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR) && empty($conf->global->MODULEBUILDER_ASCIIDOCTORPDF)) {
819 $this->error =
'Setup of module ModuleBuilder not complete';
824 dol_copy($dirofmodule.
'/README.md', $dirofmoduletmp.
'/README.md', 0, 1);
825 dol_copy($dirofmodule.
'/ChangeLog.md', $dirofmoduletmp.
'/ChangeLog.md', 0, 1);
828 $arrayreplacement = array();
829 $arrayreplacement[
'/^#\s.*/m'] =
'';
830 $arrayreplacement[
'/^#/m'] =
'##';
832 dolReplaceInFile($dirofmoduletmp.
'/README.md', $arrayreplacement,
'', 0, 0, 1);
833 dolReplaceInFile($dirofmoduletmp.
'/ChangeLog.md', $arrayreplacement,
'', 0, 0, 1);
836 $destfile = $dirofmoduletmp.
'/'.$FILENAMEASCII;
838 $fhandle = fopen($destfile,
'w+');
840 $specs =
dol_dir_list(
dol_buildpath(strtolower($module).
'/doc', 0),
'files', 1,
'(\.md|\.asciidoc)$', array(
'\/temp\/'));
843 foreach ($specs as $spec) {
844 if (preg_match(
'/notindoc/', $spec[
'relativename'])) {
847 if (preg_match(
'/example/', $spec[
'relativename'])) {
850 if (preg_match(
'/disabled/', $spec[
'relativename'])) {
854 $pathtofile = strtolower($module).
'/doc/'.$spec[
'relativename'];
855 $format =
'asciidoc';
856 if (preg_match(
'/\.md$/i', $spec[
'name'])) {
857 $format =
'markdown';
860 $filecursor = @file_get_contents($spec[
'fullname']);
862 fwrite($fhandle, ($i ?
"\n<<<\n\n" :
"").$filecursor.
"\n");
864 $this->error =
'Failed to concat content of file '.$spec[
'fullname'];
873 $contentreadme = file_get_contents($dirofmoduletmp.
'/README.md');
874 $contentchangelog = file_get_contents($dirofmoduletmp.
'/ChangeLog.md');
876 include DOL_DOCUMENT_ROOT.
'/core/lib/parsemd.lib.php';
879 $arrayreplacement = array(
880 'mymodule'=>strtolower($module),
882 'MYMODULE'=>strtoupper($module),
883 'My module'=>$module,
884 'my module'=>$module,
885 'Mon module'=>$module,
886 'mon module'=>$module,
887 'htdocs/modulebuilder/template'=>strtolower($module),
888 '__MYCOMPANY_NAME__'=>$mysoc->name,
889 '__KEYWORDS__'=>$module,
890 '__USER_FULLNAME__'=>$user->getFullName($langs),
891 '__USER_EMAIL__'=>$user->email,
893 '---Put here your own copyright and developer email---'=>
dol_print_date($now,
'dayrfc').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
''),
894 '__DATA_SPECIFICATION__'=>
'Not yet available',
903 $currentdir = getcwd();
906 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
910 $command = $conf->global->MODULEBUILDER_ASCIIDOCTOR.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOC;
911 $outfile = $dirofmoduletmp.
'/out.tmp';
913 $resarray = $utils->executeCLI($command, $outfile);
914 if ($resarray[
'result'] !=
'0') {
915 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
916 $this->errors[] = $this->error;
918 $result = ($resarray[
'result'] == 0) ? 1 : 0;
919 if ($result < 0 && empty($this->errors)) {
920 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOC);
921 $this->errors[] = $this->error;
925 $command = $conf->global->MODULEBUILDER_ASCIIDOCTORPDF.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOCPDF;
926 $outfile = $dirofmoduletmp.
'/outpdf.tmp';
927 $resarray = $utils->executeCLI($command, $outfile);
928 if ($resarray[
'result'] !=
'0') {
929 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
930 $this->errors[] = $this->error;
932 $result = ($resarray[
'result'] == 0) ? 1 : 0;
933 if ($result < 0 && empty($this->errors)) {
934 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOCPDF);
935 $this->errors[] = $this->error;
950 $langs->load(
"errors");
951 $this->error = $langs->trans(
"ErrorCheckVersionIsDefined");
968 if (empty($conf->loghandlers[
'mod_syslog_file'])) {
972 if (!function_exists(
'gzopen')) {
973 $this->error =
'Support for gzopen not available in this PHP';
981 if (empty($conf->global->SYSLOG_FILE)) {
982 $mainlogdir = DOL_DATA_ROOT;
983 $mainlog =
'dolibarr.log';
985 $mainlogfull = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
986 $mainlogdir = dirname($mainlogfull);
987 $mainlog = basename($mainlogfull);
990 $tabfiles =
dol_dir_list(DOL_DATA_ROOT,
'files', 0,
'^(dolibarr_.+|odt2pdf)\.log$');
991 $tabfiles[] = array(
'name' => $mainlog,
'path' => $mainlogdir);
993 foreach ($tabfiles as $file) {
994 $logname = $file[
'name'];
995 $logpath = $file[
'path'];
1000 $filter =
'^'.preg_quote($logname,
'/').
'\.([0-9]+)\.gz$';
1002 $gzfilestmp =
dol_dir_list($logpath,
'files', 0, $filter);
1005 foreach ($gzfilestmp as $gzfile) {
1006 $tabmatches = array();
1007 preg_match(
'/'.$filter.
'/i', $gzfile[
'name'], $tabmatches);
1009 $numsave = intval($tabmatches[1]);
1011 $gzfiles[$numsave] = $gzfile;
1014 krsort($gzfiles, SORT_NUMERIC);
1016 foreach ($gzfiles as $numsave => $dummy) {
1017 if (
dol_is_file($logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz')) {
1021 if ($numsave >= $nbSaves) {
1022 dol_delete_file($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', 0, 0, 0,
null,
false, 0);
1024 dol_move($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', $logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz', 0, 1, 0, 0);
1031 $gzfilehandle = gzopen($logpath.
'/'.$logname.
'.1.gz',
'wb9');
1033 if (empty($gzfilehandle)) {
1034 $this->error =
'Failted to open file '.$logpath.
'/'.$logname.
'.1.gz';
1038 $sourcehandle = fopen($logpath.
'/'.$logname,
'r');
1040 if (empty($sourcehandle)) {
1041 $this->error =
'Failed to open file '.$logpath.
'/'.$logname;
1045 while (!feof($sourcehandle)) {
1046 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024));
1049 fclose($sourcehandle);
1050 gzclose($gzfilehandle);
1052 @chmod($logpath.
'/'.$logname.
'.1.gz', octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1058 $newlog = fopen($logpath.
'/'.$logname,
'a+');
1062 @chmod($logpath.
'/'.$logname, octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1066 $this->output =
'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.
' files) done.';
1080 public function backupTables($outputfile, $tables =
'*')
1086 if (is_a(
$db,
'DoliDBMysqli')) {
1088 $db->db->set_charset(
'utf8');
1091 $db->query(
'SET NAMES utf8');
1092 $db->query(
'SET CHARACTER SET utf8');
1096 if ($tables ==
'*') {
1098 $result =
$db->query(
'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1099 while ($row =
$db->fetch_row($result)) {
1100 $tables[] = $row[0];
1103 $tables = is_array($tables) ? $tables : explode(
',', $tables);
1107 $handle = fopen($outputfile,
'w+');
1108 if (fwrite($handle,
'') ===
false) {
1109 $langs->load(
"errors");
1110 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
1111 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
1117 $sqlhead .=
"-- ".$db::LABEL.
" dump via php with Dolibarr ".DOL_VERSION.
"
1119 -- Host: ".
$db->db->host_info.
" Database: ".
$db->database_name.
"
1120 -- ------------------------------------------------------
1121 -- Server version ".
$db->db->server_info.
"
1136 if (
GETPOST(
"nobin_disable_fk")) {
1137 $sqlhead .=
"SET FOREIGN_KEY_CHECKS=0;\n";
1140 if (
GETPOST(
"nobin_use_transaction")) {
1141 $sqlhead .=
"SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1144 fwrite($handle, $sqlhead);
1147 if (
GETPOST(
"nobin_sql_ignore")) {
1148 $ignore =
'IGNORE ';
1151 if (
GETPOST(
"nobin_delayed")) {
1152 $delayed =
'DELAYED ';
1156 foreach ($tables as $table) {
1158 fwrite($handle,
"\n--\n-- Table structure for table `".$table.
"`\n--\n");
1161 fwrite($handle,
"DROP TABLE IF EXISTS `".$table.
"`;\n");
1163 fwrite($handle,
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1164 fwrite($handle,
"/*!40101 SET character_set_client = utf8 */;\n");
1165 $resqldrop =
$db->query(
'SHOW CREATE TABLE '.$table);
1166 $row2 =
$db->fetch_row($resqldrop);
1167 if (empty($row2[1])) {
1168 fwrite($handle,
"\n-- WARNING: Show create table ".$table.
" return empy string when it should not.\n");
1170 fwrite($handle, $row2[1].
";\n");
1174 fwrite($handle,
"\n--\n-- Dumping data for table `".$table.
"`\n--\n");
1175 if (!
GETPOST(
"nobin_nolocks")) {
1176 fwrite($handle,
"LOCK TABLES `".$table.
"` WRITE;\n");
1178 if (
GETPOST(
"nobin_disable_fk")) {
1179 fwrite($handle,
"ALTER TABLE `".$table.
"` DISABLE KEYS;\n");
1181 fwrite($handle,
"/*!40000 ALTER TABLE `".$table.
"` DISABLE KEYS */;\n");
1184 $sql =
"SELECT * FROM ".$table;
1185 $result =
$db->query($sql);
1186 while ($row =
$db->fetch_row($result)) {
1188 fwrite($handle,
"INSERT ".$delayed.$ignore.
"INTO ".$table.
" VALUES (");
1189 $columns = count($row);
1190 for ($j = 0; $j < $columns; $j++) {
1192 if ($row[$j] ==
null && !is_string($row[$j])) {
1195 } elseif (is_string($row[$j]) && $row[$j] ==
'') {
1198 } elseif (is_numeric($row[$j]) && !strcmp($row[$j], $row[$j] + 0)) {
1202 $row[$j] = addslashes($row[$j]);
1203 $row[$j] = preg_replace(
"#\n#",
"\\n", $row[$j]);
1204 $row[$j] =
"'".$row[$j].
"'";
1207 fwrite($handle, implode(
',', $row).
");\n");
1209 if (
GETPOST(
"nobin_disable_fk")) {
1210 fwrite($handle,
"ALTER TABLE `".$table.
"` ENABLE KEYS;\n");
1212 if (!
GETPOST(
"nobin_nolocks")) {
1213 fwrite($handle,
"UNLOCK TABLES;\n");
1215 fwrite($handle,
"\n\n\n");
1239 $sqlfooter =
"\n\n";
1240 if (
GETPOST(
"nobin_use_transaction")) {
1241 $sqlfooter .=
"COMMIT;\n";
1243 if (
GETPOST(
"nobin_disable_fk")) {
1244 $sqlfooter .=
"SET FOREIGN_KEY_CHECKS=1;\n";
1246 $sqlfooter .=
"\n\n-- Dump completed on ".date(
'Y-m-d G-i-s');
1247 fwrite($handle, $sqlfooter);
1267 public function sendBackup($sendto =
'', $from =
'', $subject =
'', $message =
'', $filename =
'', $filter =
'', $sizelimit = 100000000)
1269 global $conf, $langs;
1275 if (!empty($from)) {
1277 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1283 if (!empty($sendto)) {
1285 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1291 if (!empty($subject)) {
1297 if (empty($message)) {
1301 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1303 if (
dol_is_file($conf->admin->dir_output.
'/backup/'.$filename)) {
1310 foreach ($tmpfiles as $key => $val) {
1311 if ($key ==
'fullname') {
1312 $filepath = array($val);
1315 if ($key ==
'type') {
1316 $mimetype = array($val);
1318 if ($key ==
'relativename') {
1319 $filename = array($val);
1325 if ($filesize > $sizelimit) {
1326 $message .=
'<br>'.$langs->trans(
"BackupIsTooLargeSend");
1327 $documenturl = $dolibarr_main_url_root.
'/document.php?modulepart=systemtools&atachement=1&file=backup/'.urlencode($filename[0]);
1328 $message .=
'<br><a href='.$documenturl.
'>Lien de téléchargement</a>';
1334 $output =
'No backup file found';
1339 include_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1340 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename,
'',
'', 0, -1);
1341 if ($mailfile->error) {
1343 $output = $mailfile->error;
1348 $result = $mailfile->sendfile();
1351 $output = $mailfile->error;
1357 $this->error = $error;
1358 $this->output = $output;
1360 if ($result ==
true) {
1377 dol_syslog(
"Utils::cleanUnfinishedCronjob Starting cleaning");
1384 $this_job->fetch(-1,
'Utils',
'cleanUnfinishedCronjob');
1385 if (empty($this_job->id) || !empty($this_job->error)) {
1386 dol_syslog(
"Utils::cleanUnfinishedCronjob Unable to fetch himself: ".$this_job->error, LOG_ERR);
1391 $this_job->processing = 0;
1392 if ($this_job->update($user) < 0) {
1393 dol_syslog(
"Utils::cleanUnfinishedCronjob Unable to update himself: ".implode(
', ', $this_job->errors), LOG_ERR);
1398 $cron_job->fetchAll(
'DESC',
't.rowid', 100, 0, 1,
'', 1);
1401 foreach ($cron_job->lines as $job_line) {
1403 if (empty($job_line->pid)) {
1404 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" don't have a PID", LOG_DEBUG);
1409 $job->fetch($job_line->id);
1410 if (empty($job->id) || !empty($job->error)) {
1411 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" can't be fetch: ".$job->error, LOG_ERR);
1416 if (! posix_kill($job->pid, 0)) {
1418 $job->processing = 0;
1422 $job->lastresult = -1;
1423 $job->lastoutput =
'Job killed by job cleanUnfinishedCronjob';
1425 if ($job->update($user) < 0) {
1426 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" can't be updated: ".implode(
', ', $job->errors), LOG_ERR);
1429 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" cleaned");
1433 dol_syslog(
"Utils::cleanUnfinishedCronjob Cleaning completed");