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';
71 dol_syslog(
"Utils::purgeFiles choice=".$choices, LOG_DEBUG);
78 $choicesarray = preg_split(
'/[\+,]/', $choices);
79 foreach ($choicesarray as $choice) {
80 $filesarray = array();
82 if ($choice ==
'tempfiles' || $choice ==
'tempfilesold') {
84 if ($dolibarr_main_data_root) {
85 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"directories", 1,
'^temp$',
'',
'name', SORT_ASC, 2, 0,
'', 1);
87 if ($choice ==
'tempfilesold') {
89 foreach ($filesarray as $key => $val) {
90 if ($val[
'date'] > ($now - ($nbsecondsold))) {
91 unset($filesarray[$key]);
98 if ($choice ==
'allfiles') {
100 if ($dolibarr_main_data_root) {
101 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"all", 0,
'',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
105 if ($choice ==
'logfile' || $choice ==
'logfiles') {
107 if ($dolibarr_main_data_root) {
108 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"files", 0,
'.*\.log[\.0-9]*(\.gz)?$',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
111 if (!empty($conf->syslog->enabled)) {
112 $filelog = $conf->global->SYSLOG_FILE;
113 $filelog = preg_replace(
'/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
115 $alreadyincluded =
false;
116 foreach ($filesarray as $tmpcursor) {
117 if ($tmpcursor[
'fullname'] == $filelog) {
118 $alreadyincluded =
true;
121 if (!$alreadyincluded) {
122 $filesarray[] = array(
'fullname'=>$filelog,
'type'=>
'file');
127 if (is_array($filesarray) && count($filesarray)) {
128 foreach ($filesarray as $key => $value) {
130 if ($filesarray[$key][
'type'] ==
'dir') {
132 $tmpcountdeleted = 0;
136 if (!in_array($filesarray[$key][
'fullname'], array($conf->api->dir_temp, $conf->user->dir_temp))) {
138 $countdeleted += $tmpcountdeleted;
140 } elseif ($filesarray[$key][
'type'] ==
'file') {
142 if ($filesarray[$key][
'fullname'] != $filelog || $choice ==
'logfile' || $choice ==
'logfiles') {
155 if (!empty($conf->ecm->enabled) && $choice ==
'allfiles') {
156 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmdirectory.class.php';
158 $result = $ecmdirstatic->refreshcachenboffile(1);
164 $this->output = $langs->trans(
"PurgeNDirectoriesDeleted", $countdeleted);
165 if ($count > $countdeleted) {
166 $this->output .=
'<br>'.$langs->trans(
"PurgeNDirectoriesFailed", ($count - $countdeleted));
169 $this->output = $langs->trans(
"PurgeNothingToDelete").(in_array(
'tempfilesold', $choicesarray) ?
' (older than 24h for temp files)' :
'');
173 if (!empty($conf->api->enabled)) {
195 public function dumpDatabase($compression =
'none', $type =
'auto', $usedefault = 1, $file =
'auto', $keeplastnfiles = 0, $execmethod = 0)
197 global $db, $conf, $langs, $dolibarr_main_data_root;
198 global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
199 global $dolibarr_main_db_character_set;
201 $langs->load(
"admin");
203 dol_syslog(
"Utils::dumpDatabase type=".$type.
" compression=".$compression.
" file=".$file, LOG_DEBUG);
204 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
207 if (!in_array($compression, array(
'none',
'gz',
'bz',
'zip',
'zstd'))) {
208 $langs->load(
"errors");
209 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $compression,
"Compression");
214 if ($type ==
'auto') {
215 $type = $this->
db->type;
217 if (!in_array($type, array(
'postgresql',
'pgsql',
'mysql',
'mysqli',
'mysqlnobin'))) {
218 $langs->load(
"errors");
219 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $type,
"Basetype");
224 if ($file ==
'auto') {
227 if (in_array($type, array(
'mysql',
'mysqli'))) {
228 $prefix =
'mysqldump';
232 if (in_array($type, array(
'pgsql'))) {
239 $outputdir = $conf->admin->dir_output.
'/backup';
244 if ($type ==
'mysql' || $type ==
'mysqli') {
245 if (empty($conf->global->SYSTEMTOOLS_MYSQLDUMP)) {
246 $cmddump = $db->getPathOfDump();
248 $cmddump = $conf->global->SYSTEMTOOLS_MYSQLDUMP;
250 if (empty($cmddump)) {
251 $this->error =
"Failed to detect command to use for mysqldump. Try a manual backup before to set path of command.";
255 $outputfile = $outputdir.
'/'.$file;
257 $compression = $compression ? $compression :
'none';
258 if ($compression ==
'gz') {
259 $outputfile .=
'.gz';
260 } elseif ($compression ==
'bz') {
261 $outputfile .=
'.bz2';
262 } elseif ($compression ==
'zstd') {
263 $outputfile .=
'.zst';
265 $outputerror = $outputfile.
'.err';
266 dol_mkdir($conf->admin->dir_output.
'/backup');
270 $command = preg_replace(
'/(\$|%)/',
'', $command);
271 if (preg_match(
"/\s/", $command)) {
272 $command = escapeshellarg($command);
276 $param = $dolibarr_main_db_name.
" -h ".$dolibarr_main_db_host;
277 $param .=
" -u ".$dolibarr_main_db_user;
278 if (!empty($dolibarr_main_db_port)) {
279 $param .=
" -P ".$dolibarr_main_db_port.
" --protocol=tcp";
281 if (
GETPOST(
"use_transaction",
"alpha")) {
282 $param .=
" --single-transaction";
284 if (
GETPOST(
"disable_fk",
"alpha") || $usedefault) {
287 if (
GETPOST(
"sql_compat",
"alpha") &&
GETPOST(
"sql_compat",
"alpha") !=
'NONE') {
288 $param .=
" --compatible=".escapeshellarg(
GETPOST(
"sql_compat",
"alpha"));
290 if (
GETPOST(
"drop_database",
"alpha")) {
291 $param .=
" --add-drop-database";
293 if (
GETPOST(
"use_mysql_quick_param",
"alpha")) {
294 $param .=
" --quick";
296 if (
GETPOST(
"sql_structure",
"alpha") || $usedefault) {
297 if (
GETPOST(
"drop",
"alpha") || $usedefault) {
298 $param .=
" --add-drop-table=TRUE";
300 $param .=
" --add-drop-table=FALSE";
305 if (
GETPOST(
"disable-add-locks",
"alpha")) {
306 $param .=
" --add-locks=FALSE";
308 if (
GETPOST(
"sql_data",
"alpha") || $usedefault) {
309 $param .=
" --tables";
310 if (
GETPOST(
"showcolumns",
"alpha") || $usedefault) {
313 if (
GETPOST(
"extended_ins",
"alpha") || $usedefault) {
316 $param .=
" --skip-extended-insert";
318 if (
GETPOST(
"delayed",
"alpha")) {
319 $param .=
" --delayed-insert";
321 if (
GETPOST(
"sql_ignore",
"alpha")) {
322 $param .=
" --insert-ignore";
324 if (
GETPOST(
"hexforbinary",
"alpha") || $usedefault) {
325 $param .=
" --hex-blob";
330 if ($dolibarr_main_db_character_set ==
'utf8mb4') {
332 $param .=
" --default-character-set=utf8mb4 --no-tablespaces";
334 $param .=
" --default-character-set=utf8 --no-tablespaces";
336 $paramcrypted = $param;
337 $paramclear = $param;
338 if (!empty($dolibarr_main_db_pass)) {
339 $paramcrypted .=
' -p"'.preg_replace(
'/./i',
'*', $dolibarr_main_db_pass).
'"';
340 $paramclear .=
' -p"'.str_replace(array(
'"',
'`',
'$'), array(
'\"',
'\`',
'\$'), $dolibarr_main_db_pass).
'"';
348 $fullcommandcrypted = $command.
" ".$paramcrypted.
" 2>&1";
349 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
350 if (!$lowmemorydump) {
351 if ($compression ==
'none') {
352 $handle = fopen($outputfile,
'w');
353 } elseif ($compression ==
'gz') {
354 $handle = gzopen($outputfile,
'w');
355 } elseif ($compression ==
'bz') {
356 $handle = bzopen($outputfile,
'w');
357 } elseif ($compression ==
'zstd') {
358 $handle = fopen($outputfile,
'w');
361 if ($compression ==
'none') {
362 $fullcommandclear .=
" > ".$outputfile;
363 $fullcommandcrypted .=
" > ".$outputfile;
365 } elseif ($compression ==
'gz') {
366 $fullcommandclear .=
" | gzip > ".$outputfile;
367 $fullcommandcrypted .=
" | gzip > ".$outputfile;
368 $paramcrypted.=
" | gzip";
370 } elseif ($compression ==
'bz') {
371 $fullcommandclear .=
" | bzip2 > ".$outputfile;
372 $fullcommandcrypted .=
" | bzip2 > ".$outputfile;
373 $paramcrypted.=
" | bzip2";
375 } elseif ($compression ==
'zstd') {
376 $fullcommandclear .=
" | zstd > ".$outputfile;
377 $fullcommandcrypted .=
" | zstd > ".$outputfile;
378 $paramcrypted.=
" | zstd";
385 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
386 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
388 if (empty($execmethod)) {
392 dol_syslog(
"Utils::dumpDatabase execmethod=".$execmethod.
" command:".$fullcommandcrypted, LOG_INFO);
397 if (!empty($MemoryLimit)) {
398 @ini_set(
'memory_limit', $MemoryLimit);
404 if ($execmethod == 1) {
405 $output_arr = array();
408 exec($fullcommandclear, $output_arr, $retval);
411 $langs->load(
"errors");
412 dol_syslog(
"Datadump retval after exec=".$retval, LOG_ERR);
413 $errormsg =
'Error '.$retval;
417 if (!empty($output_arr)) {
418 foreach ($output_arr as $key => $read) {
420 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read)) {
423 if (!$lowmemorydump) {
424 fwrite($handle, $read.($execmethod == 2 ?
'' :
"\n"));
425 if (preg_match(
'/'.preg_quote(
'-- Dump completed',
'/').
'/i', $read)) {
427 } elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES',
'/').
'/i', $read)) {
434 } elseif ($lowmemorydump) {
440 if ($execmethod == 2) {
441 $handlein = popen($fullcommandclear,
'r');
444 while (!feof($handlein)) {
446 $read = fgets($handlein);
448 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read)) {
451 fwrite($handle, $read);
452 if (preg_match(
'/'.preg_quote(
'-- Dump completed').
'/i', $read)) {
454 } elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES').
'/i', $read)) {
463 if ($compression ==
'none') {
465 } elseif ($compression ==
'gz') {
467 } elseif ($compression ==
'bz') {
469 } elseif ($compression ==
'zstd') {
473 if (!empty($conf->global->MAIN_UMASK)) {
474 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
477 $langs->load(
"errors");
478 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
479 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
483 if ($compression ==
'none') {
484 $handle = fopen($outputfile,
'r');
485 } elseif ($compression ==
'gz') {
486 $handle = gzopen($outputfile,
'r');
487 } elseif ($compression ==
'bz') {
488 $handle = bzopen($outputfile,
'r');
489 } elseif ($compression ==
'zstd') {
490 $handle = fopen($outputfile,
'r');
494 $errormsg = fgets($handle, 2048);
498 if ($compression ==
'none') {
500 } elseif ($compression ==
'gz') {
502 } elseif ($compression ==
'bz') {
504 } elseif ($compression ==
'zstd') {
507 if ($ok && preg_match(
'/^-- (MySql|MariaDB)/i', $errormsg)) {
513 @rename($outputfile, $outputerror);
516 $langs->load(
"errors");
517 $errormsg = $langs->trans(
"ErrorFailedToRunExternalCommand");
523 $this->output = $errormsg;
524 $this->error = $errormsg;
525 $this->result = array(
"commandbackuplastdone" => $command.
" ".$paramcrypted,
"commandbackuptorun" =>
"");
530 if ($type ==
'mysqlnobin') {
531 $outputfile = $outputdir.
'/'.$file;
532 $outputfiletemp = $outputfile.
'-TMP.sql';
534 $compression = $compression ? $compression :
'none';
535 if ($compression ==
'gz') {
536 $outputfile .=
'.gz';
538 if ($compression ==
'bz') {
539 $outputfile .=
'.bz2';
541 $outputerror = $outputfile.
'.err';
542 dol_mkdir($conf->admin->dir_output.
'/backup');
544 if ($compression ==
'gz' or $compression ==
'bz') {
545 $this->backupTables($outputfiletemp);
546 dol_compress_file($outputfiletemp, $outputfile, $compression);
547 unlink($outputfiletemp);
549 $this->backupTables($outputfile);
553 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" =>
"");
557 if ($type ==
'postgresql' || $type ==
'pgsql') {
558 $cmddump = $conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
560 $outputfile = $outputdir.
'/'.$file;
562 $compression = $compression ? $compression :
'none';
563 if ($compression ==
'gz') {
564 $outputfile .=
'.gz';
566 if ($compression ==
'bz') {
567 $outputfile .=
'.bz2';
569 $outputerror = $outputfile.
'.err';
570 dol_mkdir($conf->admin->dir_output.
'/backup');
574 $command = preg_replace(
'/(\$|%)/',
'', $command);
575 if (preg_match(
"/\s/", $command)) {
576 $command = escapeshellarg($command);
582 $param .=
" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
583 $param .=
" -U ".$dolibarr_main_db_user;
584 if (!empty($dolibarr_main_db_port)) {
585 $param .=
" -p ".$dolibarr_main_db_port;
588 $param .=
" --disable-dollar-quoting";
590 if (
GETPOST(
"drop_database")) {
593 if (
GETPOST(
"sql_structure")) {
595 $param .=
" --add-drop-table";
602 if (!
GETPOST(
"sql_structure")) {
609 $param .=
' -f "'.$outputfile.
'"';
611 if ($compression ==
'gz') {
615 $paramcrypted = $param;
616 $paramclear = $param;
622 $paramcrypted .=
" -w ".$dolibarr_main_db_name;
623 $paramclear .=
" -w ".$dolibarr_main_db_name;
626 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" => $command.
" ".$paramcrypted);
630 if (!$errormsg && $keeplastnfiles > 0) {
631 $tmpfiles =
dol_dir_list($conf->admin->dir_output.
'/backup',
'files', 0,
'',
'(\.err|\.old|\.sav)$',
'date', SORT_DESC);
633 foreach ($tmpfiles as $key => $val) {
635 if ($i <= $keeplastnfiles) {
642 return ($errormsg ? -1 : 0);
660 public function executeCLI($command, $outputfile, $execmethod = 0, $redirectionfile = null, $noescapecommand = 0, $redirectionfileerr = null)
662 global $conf, $langs;
668 if (empty($noescapecommand)) {
669 $command = escapeshellcmd($command);
672 if ($redirectionfile) {
673 $command .=
" > ".dol_sanitizePathName($redirectionfile);
676 if ($redirectionfileerr && ($redirectionfileerr != $redirectionfile)) {
678 $command .=
" 2> ".dol_sanitizePathName($redirectionfileerr);
683 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
684 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
686 if (empty($execmethod)) {
690 dol_syslog(
"Utils::executeCLI execmethod=".$execmethod.
" command=".$command, LOG_DEBUG);
691 $output_arr = array();
693 if ($execmethod == 1) {
695 exec($command, $output_arr, $retval);
698 $langs->load(
"errors");
699 dol_syslog(
"Utils::executeCLI retval after exec=".$retval, LOG_ERR);
700 $error =
'Error '.$retval;
703 if ($execmethod == 2) {
704 $handle = fopen($outputfile,
'w+b');
706 dol_syslog(
"Utils::executeCLI run command ".$command);
707 $handlein = popen($command,
'r');
708 while (!feof($handlein)) {
709 $read = fgets($handlein);
710 fwrite($handle, $read);
711 $output_arr[] = $read;
716 if (!empty($conf->global->MAIN_UMASK)) {
717 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
722 if (is_array($output_arr) && count($output_arr) > 0) {
723 foreach ($output_arr as $val) {
724 $output .= $val.($execmethod == 2 ?
'' :
"\n");
728 dol_syslog(
"Utils::executeCLI result=".$result.
" output=".$output.
" error=".$error, LOG_DEBUG);
730 return array(
'result'=>$result,
'output'=>$output,
'error'=>$error);
741 global $conf, $langs, $user, $mysoc;
746 $modulelowercase = strtolower($module);
750 $dir = $dirins.
'/'.$modulelowercase;
755 dol_include_once($modulelowercase.
'/core/modules/mod'.$module.
'.class.php');
756 $class =
'mod'.$module;
758 if (class_exists($class)) {
760 $moduleobj =
new $class($this->
db);
767 $langs->load(
"errors");
768 dol_print_error($langs->trans(
"ErrorFailedToLoadModuleDescriptorForXXX", $module));
772 $arrayversion = explode(
'.', $moduleobj->version, 3);
773 if (count($arrayversion)) {
774 $FILENAMEASCII = strtolower($module).
'.asciidoc';
775 $FILENAMEDOC = strtolower($module).
'.html';
776 $FILENAMEDOCPDF = strtolower($module).
'.pdf';
779 $dirofmoduledoc =
dol_buildpath(strtolower($module), 0).
'/doc';
780 $dirofmoduletmp =
dol_buildpath(strtolower($module), 0).
'/doc/temp';
781 $outputfiledoc = $dirofmoduledoc.
'/'.$FILENAMEDOC;
782 if ($dirofmoduledoc) {
789 if (!is_writable($dirofmoduletmp)) {
790 $this->error =
'Dir '.$dirofmoduletmp.
' does not exists or is not writable';
794 if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR) && empty($conf->global->MODULEBUILDER_ASCIIDOCTORPDF)) {
795 $this->error =
'Setup of module ModuleBuilder not complete';
800 dol_copy($dirofmodule.
'/README.md', $dirofmoduletmp.
'/README.md', 0, 1);
801 dol_copy($dirofmodule.
'/ChangeLog.md', $dirofmoduletmp.
'/ChangeLog.md', 0, 1);
804 $arrayreplacement = array();
805 $arrayreplacement[
'/^#\s.*/m'] =
'';
806 $arrayreplacement[
'/^#/m'] =
'##';
808 dolReplaceInFile($dirofmoduletmp.
'/README.md', $arrayreplacement,
'', 0, 0, 1);
809 dolReplaceInFile($dirofmoduletmp.
'/ChangeLog.md', $arrayreplacement,
'', 0, 0, 1);
812 $destfile = $dirofmoduletmp.
'/'.$FILENAMEASCII;
814 $fhandle = fopen($destfile,
'w+');
816 $specs =
dol_dir_list(
dol_buildpath(strtolower($module).
'/doc', 0),
'files', 1,
'(\.md|\.asciidoc)$', array(
'\/temp\/'));
819 foreach ($specs as $spec) {
820 if (preg_match(
'/notindoc/', $spec[
'relativename'])) {
823 if (preg_match(
'/example/', $spec[
'relativename'])) {
826 if (preg_match(
'/disabled/', $spec[
'relativename'])) {
830 $pathtofile = strtolower($module).
'/doc/'.$spec[
'relativename'];
831 $format =
'asciidoc';
832 if (preg_match(
'/\.md$/i', $spec[
'name'])) {
833 $format =
'markdown';
836 $filecursor = @file_get_contents($spec[
'fullname']);
838 fwrite($fhandle, ($i ?
"\n<<<\n\n" :
"").$filecursor.
"\n");
840 $this->error =
'Failed to concat content of file '.$spec[
'fullname'];
849 $contentreadme = file_get_contents($dirofmoduletmp.
'/README.md');
850 $contentchangelog = file_get_contents($dirofmoduletmp.
'/ChangeLog.md');
852 include DOL_DOCUMENT_ROOT.
'/core/lib/parsemd.lib.php';
855 $arrayreplacement = array(
856 'mymodule'=>strtolower($module),
858 'MYMODULE'=>strtoupper($module),
859 'My module'=>$module,
860 'my module'=>$module,
861 'Mon module'=>$module,
862 'mon module'=>$module,
863 'htdocs/modulebuilder/template'=>strtolower($module),
864 '__MYCOMPANY_NAME__'=>$mysoc->name,
865 '__KEYWORDS__'=>$module,
866 '__USER_FULLNAME__'=>$user->getFullName($langs),
867 '__USER_EMAIL__'=>$user->email,
869 '---Put here your own copyright and developer email---'=>
dol_print_date($now,
'dayrfc').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
''),
870 '__DATA_SPECIFICATION__'=>
'Not yet available',
879 $currentdir = getcwd();
882 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
886 $command = $conf->global->MODULEBUILDER_ASCIIDOCTOR.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOC;
887 $outfile = $dirofmoduletmp.
'/out.tmp';
889 $resarray = $utils->executeCLI($command, $outfile);
890 if ($resarray[
'result'] !=
'0') {
891 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
892 $this->errors[] = $this->error;
894 $result = ($resarray[
'result'] == 0) ? 1 : 0;
895 if ($result < 0 && empty($this->errors)) {
896 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOC);
897 $this->errors[] = $this->error;
901 $command = $conf->global->MODULEBUILDER_ASCIIDOCTORPDF.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOCPDF;
902 $outfile = $dirofmoduletmp.
'/outpdf.tmp';
903 $resarray = $utils->executeCLI($command, $outfile);
904 if ($resarray[
'result'] !=
'0') {
905 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
906 $this->errors[] = $this->error;
908 $result = ($resarray[
'result'] == 0) ? 1 : 0;
909 if ($result < 0 && empty($this->errors)) {
910 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOCPDF);
911 $this->errors[] = $this->error;
926 $langs->load(
"errors");
927 $this->error = $langs->trans(
"ErrorCheckVersionIsDefined");
944 if (empty($conf->loghandlers[
'mod_syslog_file'])) {
948 if (!function_exists(
'gzopen')) {
949 $this->error =
'Support for gzopen not available in this PHP';
957 if (empty($conf->global->SYSLOG_FILE)) {
958 $mainlogdir = DOL_DATA_ROOT;
959 $mainlog =
'dolibarr.log';
961 $mainlogfull = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
962 $mainlogdir = dirname($mainlogfull);
963 $mainlog = basename($mainlogfull);
966 $tabfiles =
dol_dir_list(DOL_DATA_ROOT,
'files', 0,
'^(dolibarr_.+|odt2pdf)\.log$');
967 $tabfiles[] = array(
'name' => $mainlog,
'path' => $mainlogdir);
969 foreach ($tabfiles as $file) {
970 $logname = $file[
'name'];
971 $logpath = $file[
'path'];
976 $filter =
'^'.preg_quote($logname,
'/').
'\.([0-9]+)\.gz$';
978 $gzfilestmp =
dol_dir_list($logpath,
'files', 0, $filter);
981 foreach ($gzfilestmp as $gzfile) {
982 $tabmatches = array();
983 preg_match(
'/'.$filter.
'/i', $gzfile[
'name'], $tabmatches);
985 $numsave = intval($tabmatches[1]);
987 $gzfiles[$numsave] = $gzfile;
990 krsort($gzfiles, SORT_NUMERIC);
992 foreach ($gzfiles as $numsave => $dummy) {
993 if (
dol_is_file($logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz')) {
997 if ($numsave >= $nbSaves) {
998 dol_delete_file($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', 0, 0, 0, null,
false, 0);
1000 dol_move($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', $logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz', 0, 1, 0, 0);
1007 $gzfilehandle = gzopen($logpath.
'/'.$logname.
'.1.gz',
'wb9');
1009 if (empty($gzfilehandle)) {
1010 $this->error =
'Failted to open file '.$logpath.
'/'.$logname.
'.1.gz';
1014 $sourcehandle = fopen($logpath.
'/'.$logname,
'r');
1016 if (empty($sourcehandle)) {
1017 $this->error =
'Failed to open file '.$logpath.
'/'.$logname;
1021 while (!feof($sourcehandle)) {
1022 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024));
1025 fclose($sourcehandle);
1026 gzclose($gzfilehandle);
1028 @chmod($logpath.
'/'.$logname.
'.1.gz', octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1034 $newlog = fopen($logpath.
'/'.$logname,
'a+');
1038 @chmod($logpath.
'/'.$logname, octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1042 $this->output =
'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.
' files) done.';
1056 public function backupTables($outputfile, $tables =
'*')
1062 if (is_a($db,
'DoliDBMysqli')) {
1064 $db->db->set_charset(
'utf8');
1067 $db->query(
'SET NAMES utf8');
1068 $db->query(
'SET CHARACTER SET utf8');
1072 if ($tables ==
'*') {
1074 $result = $db->query(
'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1075 while ($row = $db->fetch_row($result)) {
1076 $tables[] = $row[0];
1079 $tables = is_array($tables) ? $tables : explode(
',', $tables);
1083 $handle = fopen($outputfile,
'w+');
1084 if (fwrite($handle,
'') ===
false) {
1085 $langs->load(
"errors");
1086 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
1087 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
1093 $sqlhead .=
"-- ".$db::LABEL.
" dump via php with Dolibarr ".DOL_VERSION.
"
1095 -- Host: ".$db->db->host_info.
" Database: ".$db->database_name.
"
1096 -- ------------------------------------------------------
1097 -- Server version ".$db->db->server_info.
"
1112 if (
GETPOST(
"nobin_disable_fk")) {
1113 $sqlhead .=
"SET FOREIGN_KEY_CHECKS=0;\n";
1116 if (
GETPOST(
"nobin_use_transaction")) {
1117 $sqlhead .=
"SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1120 fwrite($handle, $sqlhead);
1123 if (
GETPOST(
"nobin_sql_ignore")) {
1124 $ignore =
'IGNORE ';
1127 if (
GETPOST(
"nobin_delayed")) {
1128 $delayed =
'DELAYED ';
1132 foreach ($tables as $table) {
1134 fwrite($handle,
"\n--\n-- Table structure for table `".$table.
"`\n--\n");
1137 fwrite($handle,
"DROP TABLE IF EXISTS `".$table.
"`;\n");
1139 fwrite($handle,
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1140 fwrite($handle,
"/*!40101 SET character_set_client = utf8 */;\n");
1141 $resqldrop = $db->query(
'SHOW CREATE TABLE '.$table);
1142 $row2 = $db->fetch_row($resqldrop);
1143 if (empty($row2[1])) {
1144 fwrite($handle,
"\n-- WARNING: Show create table ".$table.
" return empy string when it should not.\n");
1146 fwrite($handle, $row2[1].
";\n");
1150 fwrite($handle,
"\n--\n-- Dumping data for table `".$table.
"`\n--\n");
1151 if (!
GETPOST(
"nobin_nolocks")) {
1152 fwrite($handle,
"LOCK TABLES `".$table.
"` WRITE;\n");
1154 if (
GETPOST(
"nobin_disable_fk")) {
1155 fwrite($handle,
"ALTER TABLE `".$table.
"` DISABLE KEYS;\n");
1157 fwrite($handle,
"/*!40000 ALTER TABLE `".$table.
"` DISABLE KEYS */;\n");
1160 $sql =
"SELECT * FROM ".$table;
1161 $result = $db->query($sql);
1162 while ($row = $db->fetch_row($result)) {
1164 fwrite($handle,
"INSERT ".$delayed.$ignore.
"INTO ".$table.
" VALUES (");
1165 $columns = count($row);
1166 for ($j = 0; $j < $columns; $j++) {
1168 if ($row[$j] == null && !is_string($row[$j])) {
1171 } elseif (is_string($row[$j]) && $row[$j] ==
'') {
1174 } elseif (is_numeric($row[$j]) && !strcmp($row[$j], $row[$j] + 0)) {
1178 $row[$j] = addslashes($row[$j]);
1179 $row[$j] = preg_replace(
"#\n#",
"\\n", $row[$j]);
1180 $row[$j] =
"'".$row[$j].
"'";
1183 fwrite($handle, implode(
',', $row).
");\n");
1185 if (
GETPOST(
"nobin_disable_fk")) {
1186 fwrite($handle,
"ALTER TABLE `".$table.
"` ENABLE KEYS;\n");
1188 if (!
GETPOST(
"nobin_nolocks")) {
1189 fwrite($handle,
"UNLOCK TABLES;\n");
1191 fwrite($handle,
"\n\n\n");
1215 $sqlfooter =
"\n\n";
1216 if (
GETPOST(
"nobin_use_transaction")) {
1217 $sqlfooter .=
"COMMIT;\n";
1219 if (
GETPOST(
"nobin_disable_fk")) {
1220 $sqlfooter .=
"SET FOREIGN_KEY_CHECKS=1;\n";
1222 $sqlfooter .=
"\n\n-- Dump completed on ".date(
'Y-m-d G-i-s');
1223 fwrite($handle, $sqlfooter);
1242 public function sendBackup($sendto =
'', $from =
'', $subject =
'', $message =
'', $filename =
'', $filter =
'')
1244 global $conf, $langs;
1250 if (!empty($from)) {
1252 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1258 if (!empty($sendto)) {
1260 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1266 if (!empty($subject)) {
1272 if (empty($message)) {
1276 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1278 if (
dol_is_file($conf->admin->dir_output.
'/backup/'.$filename)) {
1285 foreach ($tmpfiles as $key => $val) {
1286 if ($key ==
'fullname') {
1287 $filepath = array($val);
1290 if ($key ==
'type') {
1291 $mimetype = array($val);
1293 if ($key ==
'relativename') {
1294 $filename = array($val);
1300 if ($filesize > 100000000) {
1301 $output =
'Sorry, last backup file is too large to be send by email';
1305 $output =
'No backup file found';
1310 include_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1311 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename,
'',
'', 0, -1);
1312 if ($mailfile->error) {
1314 $output = $mailfile->error;
1319 $result = $mailfile->sendfile();
1322 $output = $mailfile->error;
1328 $this->error = $error;
1329 $this->output = $output;
1331 if ($result ==
true) {
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage utility methods.
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
dol_mkdir($dir, $dataroot= '', $newmask= '')
Creation of a directory (this can create recursive subdir)
$conf db
API class for accounts.
dol_now($mode= 'auto')
Return date for now.
purgeFiles($choices= 'tempfilesold+logfiles', $nbsecondsold=86400)
Purge files into directory of data files.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default= '')
Return dolibarr global constant string value.
dol_filesize($pathoffile)
Return size of a file.
dol_is_dir($folder)
Test if filename is a directory.
compressSyslogs()
This saves syslog files and compresses older ones.
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_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dumpDatabase($compression= 'none', $type= 'auto', $usedefault=1, $file= 'auto', $keeplastnfiles=0, $execmethod=0)
Make a backup of database CAN BE A CRON TASK.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form...
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) ...
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile->sendfile();.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
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_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
generateDoc($module)
Generate documentation of a Module.
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_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dolMd2Asciidoc($content, $parser= 'dolibarr', $replaceimagepath=null)
Function to parse MD content into ASCIIDOC.
__construct($db)
Constructor.
sendBackup($sendto= '', $from= '', $subject= '', $message= '', $filename= '', $filter= '')
Make a send last backup of database or fil in param CAN BE A CRON TASK.
Class to manage ECM directories.
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.
executeCLI($command, $outputfile, $execmethod=0, $redirectionfile=null, $noescapecommand=0, $redirectionfileerr=null)
Execute a CLI command.