59 public function purgeFiles($choices =
'tempfilesold+logfiles', $nbsecondsold = 86400)
61 global $conf, $langs, $dolibarr_main_data_root;
63 $langs->load(
"admin");
65 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
67 if (empty($choices)) {
68 $choices =
'tempfilesold+logfiles';
70 if ($choices ==
'allfiles' && $nbsecondsold > 0) {
71 $choices =
'allfilesold';
74 dol_syslog(
"Utils::purgeFiles choice=".$choices, LOG_DEBUG);
81 $choicesarray = preg_split(
'/[\+,]/', $choices);
82 foreach ($choicesarray as $choice) {
84 $filesarray = array();
86 if ($choice ==
'tempfiles' || $choice ==
'tempfilesold') {
88 if ($dolibarr_main_data_root) {
89 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"directories", 1,
'^temp$',
'',
'name', SORT_ASC, 2, 0,
'', 1);
91 if ($choice ==
'tempfilesold') {
92 foreach ($filesarray as $key => $val) {
93 if ($val[
'date'] > ($now - ($nbsecondsold))) {
94 unset($filesarray[$key]);
101 if ($choice ==
'allfiles') {
103 if ($dolibarr_main_data_root) {
104 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"all", 0,
'',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
108 if ($choice ==
'allfilesold') {
110 if ($dolibarr_main_data_root) {
111 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"files", 1,
'',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1, $nbsecondsold);
115 if ($choice ==
'logfile' || $choice ==
'logfiles') {
117 if ($dolibarr_main_data_root) {
118 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"files", 0,
'.*\.log[\.0-9]*(\.gz)?$',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
121 if (!empty($conf->syslog->enabled)) {
122 $filelog = $conf->global->SYSLOG_FILE;
123 $filelog = preg_replace(
'/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
125 $alreadyincluded =
false;
126 foreach ($filesarray as $tmpcursor) {
127 if ($tmpcursor[
'fullname'] == $filelog) {
128 $alreadyincluded =
true;
131 if (!$alreadyincluded) {
132 $filesarray[] = array(
'fullname'=>$filelog,
'type'=>
'file');
137 if (is_array($filesarray) && count($filesarray)) {
138 foreach ($filesarray as $key => $value) {
140 if ($filesarray[$key][
'type'] ==
'dir') {
142 $tmpcountdeleted = 0;
146 if (!in_array($filesarray[$key][
'fullname'], array($conf->api->dir_temp, $conf->user->dir_temp))) {
148 $countdeleted += $tmpcountdeleted;
150 } elseif ($filesarray[$key][
'type'] ==
'file') {
151 if ($choice !=
'allfilesold' || $filesarray[$key][
'date'] < ($now - $nbsecondsold)) {
153 if ($filesarray[$key][
'fullname'] != $filelog || $choice ==
'logfile' || $choice ==
'logfiles') {
168 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmdirectory.class.php';
170 $result = $ecmdirstatic->refreshcachenboffile(1);
176 $langs->load(
"admin");
177 $this->output = $langs->trans(
"PurgeNDirectoriesDeleted", $countdeleted);
178 if ($count > $countdeleted) {
179 $this->output .=
'<br>'.$langs->trans(
"PurgeNDirectoriesFailed", ($count - $countdeleted));
182 $this->output = $langs->trans(
"PurgeNothingToDelete").(in_array(
'tempfilesold', $choicesarray) ?
' (older than 24h for temp files)' :
'');
186 if (!empty($conf->api->enabled)) {
209 public function dumpDatabase($compression =
'none', $type =
'auto', $usedefault = 1, $file =
'auto', $keeplastnfiles = 0, $execmethod = 0, $lowmemorydump = 0)
211 global
$db, $conf, $langs, $dolibarr_main_data_root;
212 global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
213 global $dolibarr_main_db_character_set;
215 $langs->load(
"admin");
217 dol_syslog(
"Utils::dumpDatabase type=".$type.
" compression=".$compression.
" file=".$file, LOG_DEBUG);
218 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
224 if (!in_array($compression, array(
'none',
'gz',
'bz',
'zip',
'zstd'))) {
225 $langs->load(
"errors");
226 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $compression,
"Compression");
231 if ($type ==
'auto') {
232 $type = $this->
db->type;
234 if (!in_array($type, array(
'postgresql',
'pgsql',
'mysql',
'mysqli',
'mysqlnobin'))) {
235 $langs->load(
"errors");
236 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $type,
"Basetype");
241 if ($file ==
'auto') {
244 if (in_array($type, array(
'mysql',
'mysqli'))) {
245 $prefix =
'mysqldump';
249 if (in_array($type, array(
'pgsql'))) {
256 $outputdir = $conf->admin->dir_output.
'/backup';
261 if ($type ==
'mysql' || $type ==
'mysqli') {
262 if (empty($conf->global->SYSTEMTOOLS_MYSQLDUMP)) {
263 $cmddump =
$db->getPathOfDump();
265 $cmddump = $conf->global->SYSTEMTOOLS_MYSQLDUMP;
267 if (empty($cmddump)) {
268 $this->error =
"Failed to detect command to use for mysqldump. Try a manual backup before to set path of command.";
272 $outputfile = $outputdir.
'/'.$file;
274 $compression = $compression ? $compression :
'none';
275 if ($compression ==
'gz') {
276 $outputfile .=
'.gz';
277 } elseif ($compression ==
'bz') {
278 $outputfile .=
'.bz2';
279 } elseif ($compression ==
'zstd') {
280 $outputfile .=
'.zst';
282 $outputerror = $outputfile.
'.err';
283 dol_mkdir($conf->admin->dir_output.
'/backup');
287 $command = preg_replace(
'/(\$|%)/',
'', $command);
288 if (preg_match(
"/\s/", $command)) {
289 $command = escapeshellarg($command);
293 $param = $dolibarr_main_db_name.
" -h ".$dolibarr_main_db_host;
294 $param .=
" -u ".$dolibarr_main_db_user;
295 if (!empty($dolibarr_main_db_port)) {
296 $param .=
" -P ".$dolibarr_main_db_port.
" --protocol=tcp";
298 if (
GETPOST(
"use_transaction",
"alpha")) {
299 $param .=
" --single-transaction";
301 if (
GETPOST(
"disable_fk",
"alpha") || $usedefault) {
304 if (
GETPOST(
"sql_compat",
"alpha") &&
GETPOST(
"sql_compat",
"alpha") !=
'NONE') {
305 $param .=
" --compatible=".escapeshellarg(
GETPOST(
"sql_compat",
"alpha"));
307 if (
GETPOST(
"drop_database",
"alpha")) {
308 $param .=
" --add-drop-database";
310 if (
GETPOST(
"use_mysql_quick_param",
"alpha")) {
311 $param .=
" --quick";
313 if (
GETPOST(
"sql_structure",
"alpha") || $usedefault) {
314 if (
GETPOST(
"drop",
"alpha") || $usedefault) {
315 $param .=
" --add-drop-table=TRUE";
317 $param .=
" --add-drop-table=FALSE";
322 if (
GETPOST(
"disable-add-locks",
"alpha")) {
323 $param .=
" --add-locks=FALSE";
325 if (
GETPOST(
"sql_data",
"alpha") || $usedefault) {
326 $param .=
" --tables";
327 if (
GETPOST(
"showcolumns",
"alpha") || $usedefault) {
330 if (
GETPOST(
"extended_ins",
"alpha") || $usedefault) {
333 $param .=
" --skip-extended-insert";
335 if (
GETPOST(
"delayed",
"alpha")) {
336 $param .=
" --delayed-insert";
338 if (
GETPOST(
"sql_ignore",
"alpha")) {
339 $param .=
" --insert-ignore";
341 if (
GETPOST(
"hexforbinary",
"alpha") || $usedefault) {
342 $param .=
" --hex-blob";
347 if ($dolibarr_main_db_character_set ==
'utf8mb4') {
349 $param .=
" --default-character-set=utf8mb4 --no-tablespaces";
351 $param .=
" --default-character-set=utf8 --no-tablespaces";
353 $paramcrypted = $param;
354 $paramclear = $param;
355 if (!empty($dolibarr_main_db_pass)) {
356 $paramcrypted .=
' -p"'.preg_replace(
'/./i',
'*', $dolibarr_main_db_pass).
'"';
357 $paramclear .=
' -p"'.str_replace(array(
'"',
'`',
'$'), array(
'\"',
'\`',
'\$'), $dolibarr_main_db_pass).
'"';
363 $fullcommandcrypted = $command.
" ".$paramcrypted.
" 2>&1";
364 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
365 if (!$lowmemorydump) {
366 if ($compression ==
'none') {
367 $handle = fopen($outputfile,
'w');
368 } elseif ($compression ==
'gz') {
369 $handle = gzopen($outputfile,
'w');
370 } elseif ($compression ==
'bz') {
371 $handle = bzopen($outputfile,
'w');
372 } elseif ($compression ==
'zstd') {
373 $handle = fopen($outputfile,
'w');
376 if ($compression ==
'none') {
377 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).
'"';
378 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." > "'.dol_sanitizePathName($outputfile).
'"';
380 } elseif ($compression ==
'gz') {
381 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).
'"';
382 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip > "'.dol_sanitizePathName($outputfile).
'"';
383 $paramcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | gzip';
385 } elseif ($compression ==
'bz') {
386 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).
'"';
387 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2 > "'.dol_sanitizePathName($outputfile).
'"';
388 $paramcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | bzip2';
390 } elseif ($compression ==
'zstd') {
391 $fullcommandclear .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).
'"';
392 $fullcommandcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd > "'.dol_sanitizePathName($outputfile).
'"';
393 $paramcrypted .=
' | grep -v "Warning: Using a password on the command line interface can be insecure." | zstd';
400 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
401 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
403 if (empty($execmethod)) {
407 dol_syslog(
"Utils::dumpDatabase execmethod=".$execmethod.
" command:".$fullcommandcrypted, LOG_INFO);
412 if (!empty($MemoryLimit)) {
413 @ini_set(
'memory_limit', $MemoryLimit);
419 if ($execmethod == 1) {
420 $output_arr = array();
423 exec($fullcommandclear, $output_arr, $retval);
426 $langs->load(
"errors");
427 dol_syslog(
"Datadump retval after exec=".$retval, LOG_ERR);
428 $errormsg =
'Error '.$retval;
432 if (!empty($output_arr)) {
433 foreach ($output_arr as $key => $read) {
435 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read)) {
438 if (!$lowmemorydump) {
439 fwrite($handle, $read.($execmethod == 2 ?
'' :
"\n"));
440 if (preg_match(
'/'.preg_quote(
'-- Dump completed',
'/').
'/i', $read)) {
442 } elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES',
'/').
'/i', $read)) {
449 } elseif ($lowmemorydump) {
455 if ($execmethod == 2) {
456 $handlein = popen($fullcommandclear,
'r');
459 while (!feof($handlein)) {
461 $read = fgets($handlein);
463 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read)) {
466 fwrite($handle, $read);
467 if (preg_match(
'/'.preg_quote(
'-- Dump completed').
'/i', $read)) {
469 } elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES').
'/i', $read)) {
477 if (!$lowmemorydump) {
478 if ($compression ==
'none') {
480 } elseif ($compression ==
'gz') {
482 } elseif ($compression ==
'bz') {
484 } elseif ($compression ==
'zstd') {
489 if (!empty($conf->global->MAIN_UMASK)) {
490 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
493 $langs->load(
"errors");
494 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
495 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
499 if ($compression ==
'none') {
500 $handle = fopen($outputfile,
'r');
501 } elseif ($compression ==
'gz') {
502 $handle = gzopen($outputfile,
'r');
503 } elseif ($compression ==
'bz') {
504 $handle = bzopen($outputfile,
'r');
505 } elseif ($compression ==
'zstd') {
506 $handle = fopen($outputfile,
'r');
510 $errormsg = fgets($handle, 2048);
514 if ($compression ==
'none') {
516 } elseif ($compression ==
'gz') {
518 } elseif ($compression ==
'bz') {
520 } elseif ($compression ==
'zstd') {
523 if ($ok && preg_match(
'/^-- (MySql|MariaDB)/i', $errormsg)) {
529 @rename($outputfile, $outputerror);
532 $langs->load(
"errors");
533 $errormsg = $langs->trans(
"ErrorFailedToRunExternalCommand");
539 $this->output = $errormsg;
540 $this->error = $errormsg;
541 $this->result = array(
"commandbackuplastdone" => $command.
" ".$paramcrypted,
"commandbackuptorun" =>
"");
546 if ($type ==
'mysqlnobin') {
547 $outputfile = $outputdir.
'/'.$file;
548 $outputfiletemp = $outputfile.
'-TMP.sql';
550 $compression = $compression ? $compression :
'none';
551 if ($compression ==
'gz') {
552 $outputfile .=
'.gz';
554 if ($compression ==
'bz') {
555 $outputfile .=
'.bz2';
557 $outputerror = $outputfile.
'.err';
558 dol_mkdir($conf->admin->dir_output.
'/backup');
560 if ($compression ==
'gz' or $compression ==
'bz') {
561 $this->backupTables($outputfiletemp);
562 dol_compress_file($outputfiletemp, $outputfile, $compression);
563 unlink($outputfiletemp);
565 $this->backupTables($outputfile);
569 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" =>
"");
573 if ($type ==
'postgresql' || $type ==
'pgsql') {
574 $cmddump = $conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
576 $outputfile = $outputdir.
'/'.$file;
578 $compression = $compression ? $compression :
'none';
579 if ($compression ==
'gz') {
580 $outputfile .=
'.gz';
582 if ($compression ==
'bz') {
583 $outputfile .=
'.bz2';
585 $outputerror = $outputfile.
'.err';
586 dol_mkdir($conf->admin->dir_output.
'/backup');
590 $command = preg_replace(
'/(\$|%)/',
'', $command);
591 if (preg_match(
"/\s/", $command)) {
592 $command = escapeshellarg($command);
598 $param .=
" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
599 $param .=
" -U ".$dolibarr_main_db_user;
600 if (!empty($dolibarr_main_db_port)) {
601 $param .=
" -p ".$dolibarr_main_db_port;
604 $param .=
" --disable-dollar-quoting";
606 if (
GETPOST(
"drop_database")) {
609 if (
GETPOST(
"sql_structure")) {
611 $param .=
" --add-drop-table";
618 if (!
GETPOST(
"sql_structure")) {
625 $param .=
' -f "'.$outputfile.
'"';
627 if ($compression ==
'gz') {
631 $paramcrypted = $param;
632 $paramclear = $param;
638 $paramcrypted .=
" -w ".$dolibarr_main_db_name;
639 $paramclear .=
" -w ".$dolibarr_main_db_name;
642 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" => $command.
" ".$paramcrypted);
646 if (!$errormsg && $keeplastnfiles > 0) {
647 $tmpfiles =
dol_dir_list($conf->admin->dir_output.
'/backup',
'files', 0,
'',
'(\.err|\.old|\.sav)$',
'date', SORT_DESC);
649 if (is_array($tmpfiles)) {
650 foreach ($tmpfiles as $key => $val) {
652 if ($i <= $keeplastnfiles) {
660 return ($errormsg ? -1 : 0);
678 public function executeCLI($command, $outputfile, $execmethod = 0, $redirectionfile =
null, $noescapecommand = 0, $redirectionfileerr =
null)
680 global $conf, $langs;
686 if (empty($noescapecommand)) {
687 $command = escapeshellcmd($command);
690 if ($redirectionfile) {
691 $command .=
" > ".dol_sanitizePathName($redirectionfile);
694 if ($redirectionfileerr && ($redirectionfileerr != $redirectionfile)) {
696 $command .=
" 2> ".dol_sanitizePathName($redirectionfileerr);
701 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
702 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
704 if (empty($execmethod)) {
708 dol_syslog(
"Utils::executeCLI execmethod=".$execmethod.
" command=".$command, LOG_DEBUG);
709 $output_arr = array();
711 if ($execmethod == 1) {
713 exec($command, $output_arr, $retval);
716 $langs->load(
"errors");
717 dol_syslog(
"Utils::executeCLI retval after exec=".$retval, LOG_ERR);
718 $error =
'Error '.$retval;
721 if ($execmethod == 2) {
722 $handle = fopen($outputfile,
'w+b');
724 dol_syslog(
"Utils::executeCLI run command ".$command);
725 $handlein = popen($command,
'r');
726 while (!feof($handlein)) {
727 $read = fgets($handlein);
728 fwrite($handle, $read);
729 $output_arr[] = $read;
734 if (!empty($conf->global->MAIN_UMASK)) {
735 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
740 if (is_array($output_arr) && count($output_arr) > 0) {
741 foreach ($output_arr as $val) {
742 $output .= $val.($execmethod == 2 ?
'' :
"\n");
746 dol_syslog(
"Utils::executeCLI result=".$result.
" output=".$output.
" error=".$error, LOG_DEBUG);
748 return array(
'result'=>$result,
'output'=>$output,
'error'=>$error);
759 global $conf, $langs, $user, $mysoc;
764 $modulelowercase = strtolower($module);
768 $dir = $dirins.
'/'.$modulelowercase;
773 dol_include_once($modulelowercase.
'/core/modules/mod'.$module.
'.class.php');
774 $class =
'mod'.$module;
776 if (class_exists($class)) {
778 $moduleobj =
new $class($this->
db);
785 $langs->load(
"errors");
786 dol_print_error($langs->trans(
"ErrorFailedToLoadModuleDescriptorForXXX", $module));
790 $arrayversion = explode(
'.', $moduleobj->version, 3);
791 if (count($arrayversion)) {
792 $FILENAMEASCII = strtolower($module).
'.asciidoc';
793 $FILENAMEDOC = strtolower($module).
'.html';
794 $FILENAMEDOCPDF = strtolower($module).
'.pdf';
797 $dirofmoduledoc =
dol_buildpath(strtolower($module), 0).
'/doc';
798 $dirofmoduletmp =
dol_buildpath(strtolower($module), 0).
'/doc/temp';
799 $outputfiledoc = $dirofmoduledoc.
'/'.$FILENAMEDOC;
800 if ($dirofmoduledoc) {
807 if (!is_writable($dirofmoduletmp)) {
808 $this->error =
'Dir '.$dirofmoduletmp.
' does not exists or is not writable';
812 if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR) && empty($conf->global->MODULEBUILDER_ASCIIDOCTORPDF)) {
813 $this->error =
'Setup of module ModuleBuilder not complete';
818 dol_copy($dirofmodule.
'/README.md', $dirofmoduletmp.
'/README.md', 0, 1);
819 dol_copy($dirofmodule.
'/ChangeLog.md', $dirofmoduletmp.
'/ChangeLog.md', 0, 1);
822 $arrayreplacement = array();
823 $arrayreplacement[
'/^#\s.*/m'] =
'';
824 $arrayreplacement[
'/^#/m'] =
'##';
826 dolReplaceInFile($dirofmoduletmp.
'/README.md', $arrayreplacement,
'', 0, 0, 1);
827 dolReplaceInFile($dirofmoduletmp.
'/ChangeLog.md', $arrayreplacement,
'', 0, 0, 1);
830 $destfile = $dirofmoduletmp.
'/'.$FILENAMEASCII;
832 $fhandle = fopen($destfile,
'w+');
834 $specs =
dol_dir_list(
dol_buildpath(strtolower($module).
'/doc', 0),
'files', 1,
'(\.md|\.asciidoc)$', array(
'\/temp\/'));
837 foreach ($specs as $spec) {
838 if (preg_match(
'/notindoc/', $spec[
'relativename'])) {
841 if (preg_match(
'/example/', $spec[
'relativename'])) {
844 if (preg_match(
'/disabled/', $spec[
'relativename'])) {
848 $pathtofile = strtolower($module).
'/doc/'.$spec[
'relativename'];
849 $format =
'asciidoc';
850 if (preg_match(
'/\.md$/i', $spec[
'name'])) {
851 $format =
'markdown';
854 $filecursor = @file_get_contents($spec[
'fullname']);
856 fwrite($fhandle, ($i ?
"\n<<<\n\n" :
"").$filecursor.
"\n");
858 $this->error =
'Failed to concat content of file '.$spec[
'fullname'];
867 $contentreadme = file_get_contents($dirofmoduletmp.
'/README.md');
868 $contentchangelog = file_get_contents($dirofmoduletmp.
'/ChangeLog.md');
870 include DOL_DOCUMENT_ROOT.
'/core/lib/parsemd.lib.php';
873 $arrayreplacement = array(
874 'mymodule'=>strtolower($module),
876 'MYMODULE'=>strtoupper($module),
877 'My module'=>$module,
878 'my module'=>$module,
879 'Mon module'=>$module,
880 'mon module'=>$module,
881 'htdocs/modulebuilder/template'=>strtolower($module),
882 '__MYCOMPANY_NAME__'=>$mysoc->name,
883 '__KEYWORDS__'=>$module,
884 '__USER_FULLNAME__'=>$user->getFullName($langs),
885 '__USER_EMAIL__'=>$user->email,
887 '---Put here your own copyright and developer email---'=>
dol_print_date($now,
'dayrfc').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
''),
888 '__DATA_SPECIFICATION__'=>
'Not yet available',
897 $currentdir = getcwd();
900 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
904 $command = $conf->global->MODULEBUILDER_ASCIIDOCTOR.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOC;
905 $outfile = $dirofmoduletmp.
'/out.tmp';
907 $resarray = $utils->executeCLI($command, $outfile);
908 if ($resarray[
'result'] !=
'0') {
909 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
910 $this->errors[] = $this->error;
912 $result = ($resarray[
'result'] == 0) ? 1 : 0;
913 if ($result < 0 && empty($this->errors)) {
914 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOC);
915 $this->errors[] = $this->error;
919 $command = $conf->global->MODULEBUILDER_ASCIIDOCTORPDF.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOCPDF;
920 $outfile = $dirofmoduletmp.
'/outpdf.tmp';
921 $resarray = $utils->executeCLI($command, $outfile);
922 if ($resarray[
'result'] !=
'0') {
923 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
924 $this->errors[] = $this->error;
926 $result = ($resarray[
'result'] == 0) ? 1 : 0;
927 if ($result < 0 && empty($this->errors)) {
928 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOCPDF);
929 $this->errors[] = $this->error;
944 $langs->load(
"errors");
945 $this->error = $langs->trans(
"ErrorCheckVersionIsDefined");
962 if (empty($conf->loghandlers[
'mod_syslog_file'])) {
966 if (!function_exists(
'gzopen')) {
967 $this->error =
'Support for gzopen not available in this PHP';
975 if (empty($conf->global->SYSLOG_FILE)) {
976 $mainlogdir = DOL_DATA_ROOT;
977 $mainlog =
'dolibarr.log';
979 $mainlogfull = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
980 $mainlogdir = dirname($mainlogfull);
981 $mainlog = basename($mainlogfull);
984 $tabfiles =
dol_dir_list(DOL_DATA_ROOT,
'files', 0,
'^(dolibarr_.+|odt2pdf)\.log$');
985 $tabfiles[] = array(
'name' => $mainlog,
'path' => $mainlogdir);
987 foreach ($tabfiles as $file) {
988 $logname = $file[
'name'];
989 $logpath = $file[
'path'];
994 $filter =
'^'.preg_quote($logname,
'/').
'\.([0-9]+)\.gz$';
996 $gzfilestmp =
dol_dir_list($logpath,
'files', 0, $filter);
999 foreach ($gzfilestmp as $gzfile) {
1000 $tabmatches = array();
1001 preg_match(
'/'.$filter.
'/i', $gzfile[
'name'], $tabmatches);
1003 $numsave = intval($tabmatches[1]);
1005 $gzfiles[$numsave] = $gzfile;
1008 krsort($gzfiles, SORT_NUMERIC);
1010 foreach ($gzfiles as $numsave => $dummy) {
1011 if (
dol_is_file($logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz')) {
1015 if ($numsave >= $nbSaves) {
1016 dol_delete_file($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', 0, 0, 0,
null,
false, 0);
1018 dol_move($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', $logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz', 0, 1, 0, 0);
1025 $gzfilehandle = gzopen($logpath.
'/'.$logname.
'.1.gz',
'wb9');
1027 if (empty($gzfilehandle)) {
1028 $this->error =
'Failted to open file '.$logpath.
'/'.$logname.
'.1.gz';
1032 $sourcehandle = fopen($logpath.
'/'.$logname,
'r');
1034 if (empty($sourcehandle)) {
1035 $this->error =
'Failed to open file '.$logpath.
'/'.$logname;
1039 while (!feof($sourcehandle)) {
1040 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024));
1043 fclose($sourcehandle);
1044 gzclose($gzfilehandle);
1046 @chmod($logpath.
'/'.$logname.
'.1.gz', octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1052 $newlog = fopen($logpath.
'/'.$logname,
'a+');
1056 @chmod($logpath.
'/'.$logname, octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1060 $this->output =
'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.
' files) done.';
1074 public function backupTables($outputfile, $tables =
'*')
1080 if (is_a(
$db,
'DoliDBMysqli')) {
1082 $db->db->set_charset(
'utf8');
1085 $db->query(
'SET NAMES utf8');
1086 $db->query(
'SET CHARACTER SET utf8');
1090 if ($tables ==
'*') {
1092 $result =
$db->query(
'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1093 while ($row =
$db->fetch_row($result)) {
1094 $tables[] = $row[0];
1097 $tables = is_array($tables) ? $tables : explode(
',', $tables);
1101 $handle = fopen($outputfile,
'w+');
1102 if (fwrite($handle,
'') ===
false) {
1103 $langs->load(
"errors");
1104 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
1105 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
1111 $sqlhead .=
"-- ".$db::LABEL.
" dump via php with Dolibarr ".DOL_VERSION.
"
1113 -- Host: ".
$db->db->host_info.
" Database: ".
$db->database_name.
"
1114 -- ------------------------------------------------------
1115 -- Server version ".
$db->db->server_info.
"
1130 if (
GETPOST(
"nobin_disable_fk")) {
1131 $sqlhead .=
"SET FOREIGN_KEY_CHECKS=0;\n";
1134 if (
GETPOST(
"nobin_use_transaction")) {
1135 $sqlhead .=
"SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1138 fwrite($handle, $sqlhead);
1141 if (
GETPOST(
"nobin_sql_ignore")) {
1142 $ignore =
'IGNORE ';
1145 if (
GETPOST(
"nobin_delayed")) {
1146 $delayed =
'DELAYED ';
1150 foreach ($tables as $table) {
1152 fwrite($handle,
"\n--\n-- Table structure for table `".$table.
"`\n--\n");
1155 fwrite($handle,
"DROP TABLE IF EXISTS `".$table.
"`;\n");
1157 fwrite($handle,
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1158 fwrite($handle,
"/*!40101 SET character_set_client = utf8 */;\n");
1159 $resqldrop =
$db->query(
'SHOW CREATE TABLE '.$table);
1160 $row2 =
$db->fetch_row($resqldrop);
1161 if (empty($row2[1])) {
1162 fwrite($handle,
"\n-- WARNING: Show create table ".$table.
" return empy string when it should not.\n");
1164 fwrite($handle, $row2[1].
";\n");
1168 fwrite($handle,
"\n--\n-- Dumping data for table `".$table.
"`\n--\n");
1169 if (!
GETPOST(
"nobin_nolocks")) {
1170 fwrite($handle,
"LOCK TABLES `".$table.
"` WRITE;\n");
1172 if (
GETPOST(
"nobin_disable_fk")) {
1173 fwrite($handle,
"ALTER TABLE `".$table.
"` DISABLE KEYS;\n");
1175 fwrite($handle,
"/*!40000 ALTER TABLE `".$table.
"` DISABLE KEYS */;\n");
1178 $sql =
"SELECT * FROM ".$table;
1179 $result =
$db->query($sql);
1180 while ($row =
$db->fetch_row($result)) {
1182 fwrite($handle,
"INSERT ".$delayed.$ignore.
"INTO ".$table.
" VALUES (");
1183 $columns = count($row);
1184 for ($j = 0; $j < $columns; $j++) {
1186 if ($row[$j] ==
null && !is_string($row[$j])) {
1189 } elseif (is_string($row[$j]) && $row[$j] ==
'') {
1192 } elseif (is_numeric($row[$j]) && !strcmp($row[$j], $row[$j] + 0)) {
1196 $row[$j] = addslashes($row[$j]);
1197 $row[$j] = preg_replace(
"#\n#",
"\\n", $row[$j]);
1198 $row[$j] =
"'".$row[$j].
"'";
1201 fwrite($handle, implode(
',', $row).
");\n");
1203 if (
GETPOST(
"nobin_disable_fk")) {
1204 fwrite($handle,
"ALTER TABLE `".$table.
"` ENABLE KEYS;\n");
1206 if (!
GETPOST(
"nobin_nolocks")) {
1207 fwrite($handle,
"UNLOCK TABLES;\n");
1209 fwrite($handle,
"\n\n\n");
1233 $sqlfooter =
"\n\n";
1234 if (
GETPOST(
"nobin_use_transaction")) {
1235 $sqlfooter .=
"COMMIT;\n";
1237 if (
GETPOST(
"nobin_disable_fk")) {
1238 $sqlfooter .=
"SET FOREIGN_KEY_CHECKS=1;\n";
1240 $sqlfooter .=
"\n\n-- Dump completed on ".date(
'Y-m-d G-i-s');
1241 fwrite($handle, $sqlfooter);
1261 public function sendBackup($sendto =
'', $from =
'', $subject =
'', $message =
'', $filename =
'', $filter =
'', $sizelimit = 100000000)
1263 global $conf, $langs;
1269 if (!empty($from)) {
1271 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1277 if (!empty($sendto)) {
1279 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1285 if (!empty($subject)) {
1291 if (empty($message)) {
1295 $tmpfiles = array();
1296 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1298 if (
dol_is_file($conf->admin->dir_output.
'/backup/'.$filename)) {
1304 if ($tmpfiles && is_array($tmpfiles)) {
1305 foreach ($tmpfiles as $key => $val) {
1306 if ($key ==
'fullname') {
1307 $filepath = array($val);
1310 if ($key ==
'type') {
1311 $mimetype = array($val);
1313 if ($key ==
'relativename') {
1314 $filename = array($val);
1320 if ($filesize > $sizelimit) {
1321 $message .=
'<br>'.$langs->trans(
"BackupIsTooLargeSend");
1322 $documenturl = $dolibarr_main_url_root.
'/document.php?modulepart=systemtools&atachement=1&file=backup/'.urlencode($filename[0]);
1323 $message .=
'<br><a href='.$documenturl.
'>Lien de téléchargement</a>';
1329 $output =
'No backup file found';
1334 include_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1335 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename,
'',
'', 0, -1);
1336 if ($mailfile->error) {
1338 $output = $mailfile->error;
1343 $result = $mailfile->sendfile();
1346 $output = $mailfile->error;
1352 $this->error = $error;
1353 $this->output = $output;
1355 if ($result ==
true) {
1372 dol_syslog(
"Utils::cleanUnfinishedCronjob Starting cleaning");
1379 $this_job->fetch(-1,
'Utils',
'cleanUnfinishedCronjob');
1380 if (empty($this_job->id) || !empty($this_job->error)) {
1381 dol_syslog(
"Utils::cleanUnfinishedCronjob Unable to fetch himself: ".$this_job->error, LOG_ERR);
1386 $this_job->processing = 0;
1387 if ($this_job->update($user) < 0) {
1388 dol_syslog(
"Utils::cleanUnfinishedCronjob Unable to update himself: ".implode(
', ', $this_job->errors), LOG_ERR);
1393 $cron_job->fetchAll(
'DESC',
't.rowid', 100, 0, 1,
'', 1);
1396 foreach ($cron_job->lines as $job_line) {
1398 if (empty($job_line->pid)) {
1399 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" don't have a PID", LOG_DEBUG);
1404 $job->fetch($job_line->id);
1405 if (empty($job->id) || !empty($job->error)) {
1406 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" can't be fetch: ".$job->error, LOG_ERR);
1411 if (! posix_kill($job->pid, 0)) {
1413 $job->processing = 0;
1417 $job->lastresult = -1;
1418 $job->lastoutput =
'Job killed by job cleanUnfinishedCronjob';
1420 if ($job->update($user) < 0) {
1421 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" can't be updated: ".implode(
', ', $job->errors), LOG_ERR);
1424 dol_syslog(
"Utils::cleanUnfinishedCronjob Cronjob ".$job_line->id.
" cleaned");
1428 dol_syslog(
"Utils::cleanUnfinishedCronjob Cleaning completed");
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
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.
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.
dol_filesize($pathoffile)
Return size of a file.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
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_most_recent_file($dir, $regexfilter='', $excludefilter=array('(\.meta|_preview.*\.png)$', '^\.'), $nohook=false, $mode='')
Return file(s) into a directory (by default most recent)
dol_is_file($pathoffile)
Return if path is a file.
dolReplaceInFile($srcfile, $arrayreplacement, $destfile='', $newmask=0, $indexdatabase=0, $arrayreplacementisregex=0)
Make replacement of strings into a file.
dol_dir_list($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.
dol_is_dir($folder)
Test if filename is a directory.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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)
dolMd2Asciidoc($content, $parser='dolibarr', $replaceimagepath=null)
Function to parse MD content into ASCIIDOC.
$conf db
API class for accounts.