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 $langs->load(
"admin");
165 $this->output = $langs->trans(
"PurgeNDirectoriesDeleted", $countdeleted);
166 if ($count > $countdeleted) {
167 $this->output .=
'<br>'.$langs->trans(
"PurgeNDirectoriesFailed", ($count - $countdeleted));
170 $this->output = $langs->trans(
"PurgeNothingToDelete").(in_array(
'tempfilesold', $choicesarray) ?
' (older than 24h for temp files)' :
'');
174 if (!empty($conf->api->enabled)) {
197 public function dumpDatabase($compression =
'none', $type =
'auto', $usedefault = 1, $file =
'auto', $keeplastnfiles = 0, $execmethod = 0, $lowmemorydump = 0)
199 global
$db, $conf, $langs, $dolibarr_main_data_root;
200 global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
201 global $dolibarr_main_db_character_set;
203 $langs->load(
"admin");
205 dol_syslog(
"Utils::dumpDatabase type=".$type.
" compression=".$compression.
" file=".$file, LOG_DEBUG);
206 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
209 if (!in_array($compression, array(
'none',
'gz',
'bz',
'zip',
'zstd'))) {
210 $langs->load(
"errors");
211 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $compression,
"Compression");
216 if ($type ==
'auto') {
217 $type = $this->
db->type;
219 if (!in_array($type, array(
'postgresql',
'pgsql',
'mysql',
'mysqli',
'mysqlnobin'))) {
220 $langs->load(
"errors");
221 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $type,
"Basetype");
226 if ($file ==
'auto') {
229 if (in_array($type, array(
'mysql',
'mysqli'))) {
230 $prefix =
'mysqldump';
234 if (in_array($type, array(
'pgsql'))) {
241 $outputdir = $conf->admin->dir_output.
'/backup';
246 if ($type ==
'mysql' || $type ==
'mysqli') {
247 if (empty($conf->global->SYSTEMTOOLS_MYSQLDUMP)) {
248 $cmddump =
$db->getPathOfDump();
250 $cmddump = $conf->global->SYSTEMTOOLS_MYSQLDUMP;
252 if (empty($cmddump)) {
253 $this->error =
"Failed to detect command to use for mysqldump. Try a manual backup before to set path of command.";
257 $outputfile = $outputdir.
'/'.$file;
259 $compression = $compression ? $compression :
'none';
260 if ($compression ==
'gz') {
261 $outputfile .=
'.gz';
262 } elseif ($compression ==
'bz') {
263 $outputfile .=
'.bz2';
264 } elseif ($compression ==
'zstd') {
265 $outputfile .=
'.zst';
267 $outputerror = $outputfile.
'.err';
268 dol_mkdir($conf->admin->dir_output.
'/backup');
272 $command = preg_replace(
'/(\$|%)/',
'', $command);
273 if (preg_match(
"/\s/", $command)) {
274 $command = escapeshellarg($command);
278 $param = $dolibarr_main_db_name.
" -h ".$dolibarr_main_db_host;
279 $param .=
" -u ".$dolibarr_main_db_user;
280 if (!empty($dolibarr_main_db_port)) {
281 $param .=
" -P ".$dolibarr_main_db_port.
" --protocol=tcp";
283 if (
GETPOST(
"use_transaction",
"alpha")) {
284 $param .=
" --single-transaction";
286 if (
GETPOST(
"disable_fk",
"alpha") || $usedefault) {
289 if (
GETPOST(
"sql_compat",
"alpha") &&
GETPOST(
"sql_compat",
"alpha") !=
'NONE') {
290 $param .=
" --compatible=".escapeshellarg(
GETPOST(
"sql_compat",
"alpha"));
292 if (
GETPOST(
"drop_database",
"alpha")) {
293 $param .=
" --add-drop-database";
295 if (
GETPOST(
"use_mysql_quick_param",
"alpha")) {
296 $param .=
" --quick";
298 if (
GETPOST(
"sql_structure",
"alpha") || $usedefault) {
299 if (
GETPOST(
"drop",
"alpha") || $usedefault) {
300 $param .=
" --add-drop-table=TRUE";
302 $param .=
" --add-drop-table=FALSE";
307 if (
GETPOST(
"disable-add-locks",
"alpha")) {
308 $param .=
" --add-locks=FALSE";
310 if (
GETPOST(
"sql_data",
"alpha") || $usedefault) {
311 $param .=
" --tables";
312 if (
GETPOST(
"showcolumns",
"alpha") || $usedefault) {
315 if (
GETPOST(
"extended_ins",
"alpha") || $usedefault) {
318 $param .=
" --skip-extended-insert";
320 if (
GETPOST(
"delayed",
"alpha")) {
321 $param .=
" --delayed-insert";
323 if (
GETPOST(
"sql_ignore",
"alpha")) {
324 $param .=
" --insert-ignore";
326 if (
GETPOST(
"hexforbinary",
"alpha") || $usedefault) {
327 $param .=
" --hex-blob";
332 if ($dolibarr_main_db_character_set ==
'utf8mb4') {
334 $param .=
" --default-character-set=utf8mb4 --no-tablespaces";
336 $param .=
" --default-character-set=utf8 --no-tablespaces";
338 $paramcrypted = $param;
339 $paramclear = $param;
340 if (!empty($dolibarr_main_db_pass)) {
341 $paramcrypted .=
' -p"'.preg_replace(
'/./i',
'*', $dolibarr_main_db_pass).
'"';
342 $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 .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' > ".$outputfile;
363 $fullcommandcrypted .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' > ".$outputfile;
365 } elseif ($compression ==
'gz') {
366 $fullcommandclear .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | gzip > ".$outputfile;
367 $fullcommandcrypted .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | gzip > ".$outputfile;
368 $paramcrypted.=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | gzip";
370 } elseif ($compression ==
'bz') {
371 $fullcommandclear .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | bzip2 > ".$outputfile;
372 $fullcommandcrypted .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | bzip2 > ".$outputfile;
373 $paramcrypted.=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | bzip2";
375 } elseif ($compression ==
'zstd') {
376 $fullcommandclear .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | zstd > ".$outputfile;
377 $fullcommandcrypted .=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | zstd > ".$outputfile;
378 $paramcrypted.=
" | grep -v 'Warning: Using a password on the command line interface can be insecure.' | 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)) {
462 if (!$lowmemorydump) {
463 if ($compression ==
'none') {
465 } elseif ($compression ==
'gz') {
467 } elseif ($compression ==
'bz') {
469 } elseif ($compression ==
'zstd') {
474 if (!empty($conf->global->MAIN_UMASK)) {
475 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
478 $langs->load(
"errors");
479 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
480 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
484 if ($compression ==
'none') {
485 $handle = fopen($outputfile,
'r');
486 } elseif ($compression ==
'gz') {
487 $handle = gzopen($outputfile,
'r');
488 } elseif ($compression ==
'bz') {
489 $handle = bzopen($outputfile,
'r');
490 } elseif ($compression ==
'zstd') {
491 $handle = fopen($outputfile,
'r');
495 $errormsg = fgets($handle, 2048);
499 if ($compression ==
'none') {
501 } elseif ($compression ==
'gz') {
503 } elseif ($compression ==
'bz') {
505 } elseif ($compression ==
'zstd') {
508 if ($ok && preg_match(
'/^-- (MySql|MariaDB)/i', $errormsg)) {
514 @rename($outputfile, $outputerror);
517 $langs->load(
"errors");
518 $errormsg = $langs->trans(
"ErrorFailedToRunExternalCommand");
524 $this->output = $errormsg;
525 $this->error = $errormsg;
526 $this->result = array(
"commandbackuplastdone" => $command.
" ".$paramcrypted,
"commandbackuptorun" =>
"");
531 if ($type ==
'mysqlnobin') {
532 $outputfile = $outputdir.
'/'.$file;
533 $outputfiletemp = $outputfile.
'-TMP.sql';
535 $compression = $compression ? $compression :
'none';
536 if ($compression ==
'gz') {
537 $outputfile .=
'.gz';
539 if ($compression ==
'bz') {
540 $outputfile .=
'.bz2';
542 $outputerror = $outputfile.
'.err';
543 dol_mkdir($conf->admin->dir_output.
'/backup');
545 if ($compression ==
'gz' or $compression ==
'bz') {
546 $this->backupTables($outputfiletemp);
547 dol_compress_file($outputfiletemp, $outputfile, $compression);
548 unlink($outputfiletemp);
550 $this->backupTables($outputfile);
554 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" =>
"");
558 if ($type ==
'postgresql' || $type ==
'pgsql') {
559 $cmddump = $conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
561 $outputfile = $outputdir.
'/'.$file;
563 $compression = $compression ? $compression :
'none';
564 if ($compression ==
'gz') {
565 $outputfile .=
'.gz';
567 if ($compression ==
'bz') {
568 $outputfile .=
'.bz2';
570 $outputerror = $outputfile.
'.err';
571 dol_mkdir($conf->admin->dir_output.
'/backup');
575 $command = preg_replace(
'/(\$|%)/',
'', $command);
576 if (preg_match(
"/\s/", $command)) {
577 $command = escapeshellarg($command);
583 $param .=
" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
584 $param .=
" -U ".$dolibarr_main_db_user;
585 if (!empty($dolibarr_main_db_port)) {
586 $param .=
" -p ".$dolibarr_main_db_port;
589 $param .=
" --disable-dollar-quoting";
591 if (
GETPOST(
"drop_database")) {
594 if (
GETPOST(
"sql_structure")) {
596 $param .=
" --add-drop-table";
603 if (!
GETPOST(
"sql_structure")) {
610 $param .=
' -f "'.$outputfile.
'"';
612 if ($compression ==
'gz') {
616 $paramcrypted = $param;
617 $paramclear = $param;
623 $paramcrypted .=
" -w ".$dolibarr_main_db_name;
624 $paramclear .=
" -w ".$dolibarr_main_db_name;
627 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" => $command.
" ".$paramcrypted);
631 if (!$errormsg && $keeplastnfiles > 0) {
632 $tmpfiles =
dol_dir_list($conf->admin->dir_output.
'/backup',
'files', 0,
'',
'(\.err|\.old|\.sav)$',
'date', SORT_DESC);
634 foreach ($tmpfiles as $key => $val) {
636 if ($i <= $keeplastnfiles) {
643 return ($errormsg ? -1 : 0);
661 public function executeCLI($command, $outputfile, $execmethod = 0, $redirectionfile =
null, $noescapecommand = 0, $redirectionfileerr =
null)
663 global $conf, $langs;
669 if (empty($noescapecommand)) {
670 $command = escapeshellcmd($command);
673 if ($redirectionfile) {
674 $command .=
" > ".dol_sanitizePathName($redirectionfile);
677 if ($redirectionfileerr && ($redirectionfileerr != $redirectionfile)) {
679 $command .=
" 2> ".dol_sanitizePathName($redirectionfileerr);
684 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) {
685 $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
687 if (empty($execmethod)) {
691 dol_syslog(
"Utils::executeCLI execmethod=".$execmethod.
" command=".$command, LOG_DEBUG);
692 $output_arr = array();
694 if ($execmethod == 1) {
696 exec($command, $output_arr, $retval);
699 $langs->load(
"errors");
700 dol_syslog(
"Utils::executeCLI retval after exec=".$retval, LOG_ERR);
701 $error =
'Error '.$retval;
704 if ($execmethod == 2) {
705 $handle = fopen($outputfile,
'w+b');
707 dol_syslog(
"Utils::executeCLI run command ".$command);
708 $handlein = popen($command,
'r');
709 while (!feof($handlein)) {
710 $read = fgets($handlein);
711 fwrite($handle, $read);
712 $output_arr[] = $read;
717 if (!empty($conf->global->MAIN_UMASK)) {
718 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
723 if (is_array($output_arr) && count($output_arr) > 0) {
724 foreach ($output_arr as $val) {
725 $output .= $val.($execmethod == 2 ?
'' :
"\n");
729 dol_syslog(
"Utils::executeCLI result=".$result.
" output=".$output.
" error=".$error, LOG_DEBUG);
731 return array(
'result'=>$result,
'output'=>$output,
'error'=>$error);
742 global $conf, $langs, $user, $mysoc;
747 $modulelowercase = strtolower($module);
751 $dir = $dirins.
'/'.$modulelowercase;
756 dol_include_once($modulelowercase.
'/core/modules/mod'.$module.
'.class.php');
757 $class =
'mod'.$module;
759 if (class_exists($class)) {
761 $moduleobj =
new $class($this->
db);
768 $langs->load(
"errors");
769 dol_print_error($langs->trans(
"ErrorFailedToLoadModuleDescriptorForXXX", $module));
773 $arrayversion = explode(
'.', $moduleobj->version, 3);
774 if (count($arrayversion)) {
775 $FILENAMEASCII = strtolower($module).
'.asciidoc';
776 $FILENAMEDOC = strtolower($module).
'.html';
777 $FILENAMEDOCPDF = strtolower($module).
'.pdf';
780 $dirofmoduledoc =
dol_buildpath(strtolower($module), 0).
'/doc';
781 $dirofmoduletmp =
dol_buildpath(strtolower($module), 0).
'/doc/temp';
782 $outputfiledoc = $dirofmoduledoc.
'/'.$FILENAMEDOC;
783 if ($dirofmoduledoc) {
790 if (!is_writable($dirofmoduletmp)) {
791 $this->error =
'Dir '.$dirofmoduletmp.
' does not exists or is not writable';
795 if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR) && empty($conf->global->MODULEBUILDER_ASCIIDOCTORPDF)) {
796 $this->error =
'Setup of module ModuleBuilder not complete';
801 dol_copy($dirofmodule.
'/README.md', $dirofmoduletmp.
'/README.md', 0, 1);
802 dol_copy($dirofmodule.
'/ChangeLog.md', $dirofmoduletmp.
'/ChangeLog.md', 0, 1);
805 $arrayreplacement = array();
806 $arrayreplacement[
'/^#\s.*/m'] =
'';
807 $arrayreplacement[
'/^#/m'] =
'##';
809 dolReplaceInFile($dirofmoduletmp.
'/README.md', $arrayreplacement,
'', 0, 0, 1);
810 dolReplaceInFile($dirofmoduletmp.
'/ChangeLog.md', $arrayreplacement,
'', 0, 0, 1);
813 $destfile = $dirofmoduletmp.
'/'.$FILENAMEASCII;
815 $fhandle = fopen($destfile,
'w+');
817 $specs =
dol_dir_list(
dol_buildpath(strtolower($module).
'/doc', 0),
'files', 1,
'(\.md|\.asciidoc)$', array(
'\/temp\/'));
820 foreach ($specs as $spec) {
821 if (preg_match(
'/notindoc/', $spec[
'relativename'])) {
824 if (preg_match(
'/example/', $spec[
'relativename'])) {
827 if (preg_match(
'/disabled/', $spec[
'relativename'])) {
831 $pathtofile = strtolower($module).
'/doc/'.$spec[
'relativename'];
832 $format =
'asciidoc';
833 if (preg_match(
'/\.md$/i', $spec[
'name'])) {
834 $format =
'markdown';
837 $filecursor = @file_get_contents($spec[
'fullname']);
839 fwrite($fhandle, ($i ?
"\n<<<\n\n" :
"").$filecursor.
"\n");
841 $this->error =
'Failed to concat content of file '.$spec[
'fullname'];
850 $contentreadme = file_get_contents($dirofmoduletmp.
'/README.md');
851 $contentchangelog = file_get_contents($dirofmoduletmp.
'/ChangeLog.md');
853 include DOL_DOCUMENT_ROOT.
'/core/lib/parsemd.lib.php';
856 $arrayreplacement = array(
857 'mymodule'=>strtolower($module),
859 'MYMODULE'=>strtoupper($module),
860 'My module'=>$module,
861 'my module'=>$module,
862 'Mon module'=>$module,
863 'mon module'=>$module,
864 'htdocs/modulebuilder/template'=>strtolower($module),
865 '__MYCOMPANY_NAME__'=>$mysoc->name,
866 '__KEYWORDS__'=>$module,
867 '__USER_FULLNAME__'=>$user->getFullName($langs),
868 '__USER_EMAIL__'=>$user->email,
870 '---Put here your own copyright and developer email---'=>
dol_print_date($now,
'dayrfc').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
''),
871 '__DATA_SPECIFICATION__'=>
'Not yet available',
880 $currentdir = getcwd();
883 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
887 $command = $conf->global->MODULEBUILDER_ASCIIDOCTOR.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOC;
888 $outfile = $dirofmoduletmp.
'/out.tmp';
890 $resarray = $utils->executeCLI($command, $outfile);
891 if ($resarray[
'result'] !=
'0') {
892 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
893 $this->errors[] = $this->error;
895 $result = ($resarray[
'result'] == 0) ? 1 : 0;
896 if ($result < 0 && empty($this->errors)) {
897 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOC);
898 $this->errors[] = $this->error;
902 $command = $conf->global->MODULEBUILDER_ASCIIDOCTORPDF.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOCPDF;
903 $outfile = $dirofmoduletmp.
'/outpdf.tmp';
904 $resarray = $utils->executeCLI($command, $outfile);
905 if ($resarray[
'result'] !=
'0') {
906 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
907 $this->errors[] = $this->error;
909 $result = ($resarray[
'result'] == 0) ? 1 : 0;
910 if ($result < 0 && empty($this->errors)) {
911 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $FILENAMEDOCPDF);
912 $this->errors[] = $this->error;
927 $langs->load(
"errors");
928 $this->error = $langs->trans(
"ErrorCheckVersionIsDefined");
945 if (empty($conf->loghandlers[
'mod_syslog_file'])) {
949 if (!function_exists(
'gzopen')) {
950 $this->error =
'Support for gzopen not available in this PHP';
958 if (empty($conf->global->SYSLOG_FILE)) {
959 $mainlogdir = DOL_DATA_ROOT;
960 $mainlog =
'dolibarr.log';
962 $mainlogfull = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
963 $mainlogdir = dirname($mainlogfull);
964 $mainlog = basename($mainlogfull);
967 $tabfiles =
dol_dir_list(DOL_DATA_ROOT,
'files', 0,
'^(dolibarr_.+|odt2pdf)\.log$');
968 $tabfiles[] = array(
'name' => $mainlog,
'path' => $mainlogdir);
970 foreach ($tabfiles as $file) {
971 $logname = $file[
'name'];
972 $logpath = $file[
'path'];
977 $filter =
'^'.preg_quote($logname,
'/').
'\.([0-9]+)\.gz$';
979 $gzfilestmp =
dol_dir_list($logpath,
'files', 0, $filter);
982 foreach ($gzfilestmp as $gzfile) {
983 $tabmatches = array();
984 preg_match(
'/'.$filter.
'/i', $gzfile[
'name'], $tabmatches);
986 $numsave = intval($tabmatches[1]);
988 $gzfiles[$numsave] = $gzfile;
991 krsort($gzfiles, SORT_NUMERIC);
993 foreach ($gzfiles as $numsave => $dummy) {
994 if (
dol_is_file($logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz')) {
998 if ($numsave >= $nbSaves) {
999 dol_delete_file($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', 0, 0, 0,
null,
false, 0);
1001 dol_move($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', $logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz', 0, 1, 0, 0);
1008 $gzfilehandle = gzopen($logpath.
'/'.$logname.
'.1.gz',
'wb9');
1010 if (empty($gzfilehandle)) {
1011 $this->error =
'Failted to open file '.$logpath.
'/'.$logname.
'.1.gz';
1015 $sourcehandle = fopen($logpath.
'/'.$logname,
'r');
1017 if (empty($sourcehandle)) {
1018 $this->error =
'Failed to open file '.$logpath.
'/'.$logname;
1022 while (!feof($sourcehandle)) {
1023 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024));
1026 fclose($sourcehandle);
1027 gzclose($gzfilehandle);
1029 @chmod($logpath.
'/'.$logname.
'.1.gz', octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1035 $newlog = fopen($logpath.
'/'.$logname,
'a+');
1039 @chmod($logpath.
'/'.$logname, octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
1043 $this->output =
'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.
' files) done.';
1057 public function backupTables($outputfile, $tables =
'*')
1063 if (is_a(
$db,
'DoliDBMysqli')) {
1065 $db->db->set_charset(
'utf8');
1068 $db->query(
'SET NAMES utf8');
1069 $db->query(
'SET CHARACTER SET utf8');
1073 if ($tables ==
'*') {
1075 $result =
$db->query(
'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
1076 while ($row =
$db->fetch_row($result)) {
1077 $tables[] = $row[0];
1080 $tables = is_array($tables) ? $tables : explode(
',', $tables);
1084 $handle = fopen($outputfile,
'w+');
1085 if (fwrite($handle,
'') ===
false) {
1086 $langs->load(
"errors");
1087 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
1088 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
1094 $sqlhead .=
"-- ".$db::LABEL.
" dump via php with Dolibarr ".DOL_VERSION.
"
1096 -- Host: ".
$db->db->host_info.
" Database: ".
$db->database_name.
"
1097 -- ------------------------------------------------------
1098 -- Server version ".
$db->db->server_info.
"
1113 if (
GETPOST(
"nobin_disable_fk")) {
1114 $sqlhead .=
"SET FOREIGN_KEY_CHECKS=0;\n";
1117 if (
GETPOST(
"nobin_use_transaction")) {
1118 $sqlhead .=
"SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
1121 fwrite($handle, $sqlhead);
1124 if (
GETPOST(
"nobin_sql_ignore")) {
1125 $ignore =
'IGNORE ';
1128 if (
GETPOST(
"nobin_delayed")) {
1129 $delayed =
'DELAYED ';
1133 foreach ($tables as $table) {
1135 fwrite($handle,
"\n--\n-- Table structure for table `".$table.
"`\n--\n");
1138 fwrite($handle,
"DROP TABLE IF EXISTS `".$table.
"`;\n");
1140 fwrite($handle,
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
1141 fwrite($handle,
"/*!40101 SET character_set_client = utf8 */;\n");
1142 $resqldrop =
$db->query(
'SHOW CREATE TABLE '.$table);
1143 $row2 =
$db->fetch_row($resqldrop);
1144 if (empty($row2[1])) {
1145 fwrite($handle,
"\n-- WARNING: Show create table ".$table.
" return empy string when it should not.\n");
1147 fwrite($handle, $row2[1].
";\n");
1151 fwrite($handle,
"\n--\n-- Dumping data for table `".$table.
"`\n--\n");
1152 if (!
GETPOST(
"nobin_nolocks")) {
1153 fwrite($handle,
"LOCK TABLES `".$table.
"` WRITE;\n");
1155 if (
GETPOST(
"nobin_disable_fk")) {
1156 fwrite($handle,
"ALTER TABLE `".$table.
"` DISABLE KEYS;\n");
1158 fwrite($handle,
"/*!40000 ALTER TABLE `".$table.
"` DISABLE KEYS */;\n");
1161 $sql =
"SELECT * FROM ".$table;
1162 $result =
$db->query($sql);
1163 while ($row =
$db->fetch_row($result)) {
1165 fwrite($handle,
"INSERT ".$delayed.$ignore.
"INTO ".$table.
" VALUES (");
1166 $columns = count($row);
1167 for ($j = 0; $j < $columns; $j++) {
1169 if ($row[$j] ==
null && !is_string($row[$j])) {
1172 } elseif (is_string($row[$j]) && $row[$j] ==
'') {
1175 } elseif (is_numeric($row[$j]) && !strcmp($row[$j], $row[$j] + 0)) {
1179 $row[$j] = addslashes($row[$j]);
1180 $row[$j] = preg_replace(
"#\n#",
"\\n", $row[$j]);
1181 $row[$j] =
"'".$row[$j].
"'";
1184 fwrite($handle, implode(
',', $row).
");\n");
1186 if (
GETPOST(
"nobin_disable_fk")) {
1187 fwrite($handle,
"ALTER TABLE `".$table.
"` ENABLE KEYS;\n");
1189 if (!
GETPOST(
"nobin_nolocks")) {
1190 fwrite($handle,
"UNLOCK TABLES;\n");
1192 fwrite($handle,
"\n\n\n");
1216 $sqlfooter =
"\n\n";
1217 if (
GETPOST(
"nobin_use_transaction")) {
1218 $sqlfooter .=
"COMMIT;\n";
1220 if (
GETPOST(
"nobin_disable_fk")) {
1221 $sqlfooter .=
"SET FOREIGN_KEY_CHECKS=1;\n";
1223 $sqlfooter .=
"\n\n-- Dump completed on ".date(
'Y-m-d G-i-s');
1224 fwrite($handle, $sqlfooter);
1243 public function sendBackup($sendto =
'', $from =
'', $subject =
'', $message =
'', $filename =
'', $filter =
'')
1245 global $conf, $langs;
1251 if (!empty($from)) {
1253 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1259 if (!empty($sendto)) {
1261 } elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
1267 if (!empty($subject)) {
1273 if (empty($message)) {
1277 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1279 if (
dol_is_file($conf->admin->dir_output.
'/backup/'.$filename)) {
1286 foreach ($tmpfiles as $key => $val) {
1287 if ($key ==
'fullname') {
1288 $filepath = array($val);
1291 if ($key ==
'type') {
1292 $mimetype = array($val);
1294 if ($key ==
'relativename') {
1295 $filename = array($val);
1301 if ($filesize > 100000000) {
1302 $output =
'Sorry, last backup file is too large to be send by email';
1306 $output =
'No backup file found';
1311 include_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1312 $mailfile =
new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename,
'',
'', 0, -1);
1313 if ($mailfile->error) {
1315 $output = $mailfile->error;
1320 $result = $mailfile->sendfile();
1323 $output = $mailfile->error;
1329 $this->error = $error;
1330 $this->output = $output;
1332 if ($result ==
true) {