57 public function purgeFiles($choices =
'tempfilesold,logfile', $nbsecondsold = 86400)
59 global $conf, $langs, $dolibarr_main_data_root;
61 $langs->load(
"admin");
63 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
65 if (empty($choices)) $choices =
'tempfilesold,logfile';
67 dol_syslog(
"Utils::purgeFiles choice=".$choices, LOG_DEBUG);
73 $choicesarray = explode(
',', $choices);
74 foreach ($choicesarray as $choice) {
75 $filesarray = array();
77 if ($choice ==
'tempfiles' || $choice ==
'tempfilesold')
80 if ($dolibarr_main_data_root)
82 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"directories", 1,
'^temp$',
'',
'name', SORT_ASC, 2, 0,
'', 1);
84 if ($choice ==
'tempfilesold')
87 foreach ($filesarray as $key => $val)
89 if ($val[
'date'] > ($now - ($nbsecondsold))) unset($filesarray[$key]);
95 if ($choice ==
'allfiles')
98 if ($dolibarr_main_data_root)
100 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"all", 0,
'',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
104 if ($choice ==
'logfile')
107 if ($dolibarr_main_data_root)
109 $filesarray =
dol_dir_list($dolibarr_main_data_root,
"files", 0,
'.*\.log[\.0-9]*(\.gz)?$',
'install\.lock$',
'name', SORT_ASC, 0, 0,
'', 1);
113 if (!empty($conf->syslog->enabled))
115 $filelog = $conf->global->SYSLOG_FILE;
116 $filelog = preg_replace(
'/DOL_DATA_ROOT/i', DOL_DATA_ROOT, $filelog);
118 $alreadyincluded =
false;
119 foreach ($filesarray as $tmpcursor)
121 if ($tmpcursor[
'fullname'] == $filelog) { $alreadyincluded =
true; }
123 if (!$alreadyincluded) $filesarray[] = array(
'fullname'=>$filelog,
'type'=>
'file');
127 if (is_array($filesarray) && count($filesarray)) {
128 foreach ($filesarray as $key => $value)
131 if ($filesarray[$key][
'type'] ==
'dir') {
133 $tmpcountdeleted = 0;
137 if (!in_array($filesarray[$key][
'fullname'], array($conf->api->dir_temp, $conf->user->dir_temp))) {
139 $countdeleted += $tmpcountdeleted;
141 } elseif ($filesarray[$key][
'type'] ==
'file') {
143 if ($filesarray[$key][
'fullname'] != $filelog || $choice ==
'logfile')
158 if (!empty($conf->ecm->enabled) && $choice ==
'allfiles')
160 require_once DOL_DOCUMENT_ROOT.
'/ecm/class/ecmdirectory.class.php';
162 $result = $ecmdirstatic->refreshcachenboffile(1);
168 $this->output = $langs->trans(
"PurgeNDirectoriesDeleted", $countdeleted);
169 if ($count > $countdeleted) $this->output .=
'<br>'.$langs->trans(
"PurgeNDirectoriesFailed", ($count - $countdeleted));
171 $this->output = $langs->trans(
"PurgeNothingToDelete").(in_array(
'tempfilesold', $choicesarray) ?
' (older than 24h for temp files)' :
'');
175 if (!empty($conf->api->enabled)) {
197 public function dumpDatabase($compression =
'none', $type =
'auto', $usedefault = 1, $file =
'auto', $keeplastnfiles = 0, $execmethod = 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;
202 $langs->load(
"admin");
204 dol_syslog(
"Utils::dumpDatabase type=".$type.
" compression=".$compression.
" file=".$file, LOG_DEBUG);
205 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
208 if (!in_array($compression, array(
'none',
'gz',
'bz',
'zip')))
210 $langs->load(
"errors");
211 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $compression,
"Compression");
216 if ($type ==
'auto') $type = $this->
db->type;
217 if (!in_array($type, array(
'postgresql',
'pgsql',
'mysql',
'mysqli',
'mysqlnobin')))
219 $langs->load(
"errors");
220 $this->error = $langs->transnoentitiesnoconv(
"ErrorBadValueForParameter", $type,
"Basetype");
229 if (in_array($type, array(
'mysql',
'mysqli'))) { $prefix =
'mysqldump'; $ext =
'sql'; }
231 if (in_array($type, array(
'pgsql'))) { $prefix =
'pg_dump'; $ext =
'sql'; }
232 $file = $prefix.
'_'.$dolibarr_main_db_name.
'_'.
dol_sanitizeFileName(DOL_VERSION).
'_'.strftime(
"%Y%m%d%H%M").
'.'.$ext;
235 $outputdir = $conf->admin->dir_output.
'/backup';
240 if ($type ==
'mysql' || $type ==
'mysqli')
242 $cmddump = $conf->global->SYSTEMTOOLS_MYSQLDUMP;
245 $outputfile = $outputdir.
'/'.$file;
247 $compression = $compression ? $compression :
'none';
248 if ($compression ==
'gz') $outputfile .=
'.gz';
249 if ($compression ==
'bz') $outputfile .=
'.bz2';
250 $outputerror = $outputfile.
'.err';
251 dol_mkdir($conf->admin->dir_output.
'/backup');
255 $command = preg_replace(
'/(\$|%)/',
'', $command);
256 if (preg_match(
"/\s/", $command)) $command = escapeshellarg($command);
259 $param = $dolibarr_main_db_name.
" -h ".$dolibarr_main_db_host;
260 $param .=
" -u ".$dolibarr_main_db_user;
261 if (!empty($dolibarr_main_db_port)) $param .=
" -P ".$dolibarr_main_db_port;
262 if (!
GETPOST(
"use_transaction",
"alpha")) $param .=
" -l --single-transaction";
263 if (
GETPOST(
"disable_fk",
"alpha") || $usedefault) $param .=
" -K";
264 if (
GETPOST(
"sql_compat",
"alpha") &&
GETPOST(
"sql_compat",
"alpha") !=
'NONE') $param .=
" --compatible=".escapeshellarg(
GETPOST(
"sql_compat",
"alpha"));
265 if (
GETPOST(
"drop_database",
"alpha")) $param .=
" --add-drop-database";
266 if (
GETPOST(
"use_mysql_quick_param",
"alpha"))$param .=
" --quick";
267 if (
GETPOST(
"sql_structure",
"alpha") || $usedefault)
269 if (
GETPOST(
"drop",
"alpha") || $usedefault) $param .=
" --add-drop-table=TRUE";
270 else $param .=
" --add-drop-table=FALSE";
274 if (
GETPOST(
"disable-add-locks",
"alpha")) $param .=
" --add-locks=FALSE";
275 if (
GETPOST(
"sql_data",
"alpha") || $usedefault)
277 $param .=
" --tables";
278 if (
GETPOST(
"showcolumns",
"alpha") || $usedefault) $param .=
" -c";
279 if (
GETPOST(
"extended_ins",
"alpha") || $usedefault) $param .=
" -e";
280 else $param .=
" --skip-extended-insert";
281 if (
GETPOST(
"delayed",
"alpha")) $param .=
" --delayed-insert";
282 if (
GETPOST(
"sql_ignore",
"alpha")) $param .=
" --insert-ignore";
283 if (
GETPOST(
"hexforbinary",
"alpha") || $usedefault) $param .=
" --hex-blob";
287 $param .=
" --default-character-set=utf8";
288 $paramcrypted = $param;
289 $paramclear = $param;
290 if (!empty($dolibarr_main_db_pass))
292 $paramcrypted .=
' -p"'.preg_replace(
'/./i',
'*', $dolibarr_main_db_pass).
'"';
293 $paramclear .=
' -p"'.str_replace(array(
'"',
'`',
'$'), array(
'\"',
'\`',
'\$'), $dolibarr_main_db_pass).
'"';
299 $fullcommandcrypted = $command.
" ".$paramcrypted.
" 2>&1";
300 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
301 if ($compression ==
'none') $handle = fopen($outputfile,
'w');
302 if ($compression ==
'gz') $handle = gzopen($outputfile,
'w');
303 if ($compression ==
'bz') $handle = bzopen($outputfile,
'w');
308 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
309 if (empty($execmethod)) $execmethod = 1;
311 dol_syslog(
"Utils::dumpDatabase execmethod=".$execmethod.
" command:".$fullcommandcrypted, LOG_DEBUG);
314 if ($execmethod == 1)
316 $output_arr = array(); $retval = null;
317 exec($fullcommandclear, $output_arr, $retval);
321 $langs->load(
"errors");
322 dol_syslog(
"Datadump retval after exec=".$retval, LOG_ERR);
323 $errormsg =
'Error '.$retval;
327 if (!empty($output_arr))
329 foreach ($output_arr as $key => $read)
332 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read))
continue;
333 fwrite($handle, $read.($execmethod == 2 ?
'' :
"\n"));
334 if (preg_match(
'/'.preg_quote(
'-- Dump completed').
'/i', $read)) $ok = 1;
335 elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES').
'/i', $read)) $ok = 1;
340 if ($execmethod == 2)
342 $handlein = popen($fullcommandclear,
'r');
344 while (!feof($handlein))
347 $read = fgets($handlein);
349 if ($i == 1 && preg_match(
'/Warning.*Using a password/i', $read))
continue;
350 fwrite($handle, $read);
351 if (preg_match(
'/'.preg_quote(
'-- Dump completed').
'/i', $read)) $ok = 1;
352 elseif (preg_match(
'/'.preg_quote(
'SET SQL_NOTES=@OLD_SQL_NOTES').
'/i', $read)) $ok = 1;
358 if ($compression ==
'none') fclose($handle);
359 if ($compression ==
'gz') gzclose($handle);
360 if ($compression ==
'bz') bzclose($handle);
362 if (!empty($conf->global->MAIN_UMASK))
363 @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
365 $langs->load(
"errors");
366 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
367 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
371 if ($compression ==
'none') $handle = fopen($outputfile,
'r');
372 if ($compression ==
'gz') $handle = gzopen($outputfile,
'r');
373 if ($compression ==
'bz') $handle = bzopen($outputfile,
'r');
377 $errormsg = fgets($handle, 2048);
381 if ($compression ==
'none') fclose($handle);
382 if ($compression ==
'gz') gzclose($handle);
383 if ($compression ==
'bz') bzclose($handle);
384 if ($ok && preg_match(
'/^-- (MySql|MariaDB)/i', $errormsg)) {
390 @rename($outputfile, $outputerror);
394 $langs->load(
"errors");
395 $errormsg = $langs->trans(
"ErrorFailedToRunExternalCommand");
401 $this->output = $errormsg;
402 $this->error = $errormsg;
403 $this->result = array(
"commandbackuplastdone" => $command.
" ".$paramcrypted,
"commandbackuptorun" =>
"");
408 if ($type ==
'mysqlnobin')
410 $outputfile = $outputdir.
'/'.$file;
411 $outputfiletemp = $outputfile.
'-TMP.sql';
413 $compression = $compression ? $compression :
'none';
414 if ($compression ==
'gz') $outputfile .=
'.gz';
415 if ($compression ==
'bz') $outputfile .=
'.bz2';
416 $outputerror = $outputfile.
'.err';
417 dol_mkdir($conf->admin->dir_output.
'/backup');
419 if ($compression ==
'gz' or $compression ==
'bz')
421 $this->backupTables($outputfiletemp);
422 dol_compress_file($outputfiletemp, $outputfile, $compression);
423 unlink($outputfiletemp);
425 $this->backupTables($outputfile);
429 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" =>
"");
433 if ($type ==
'postgresql' || $type ==
'pgsql')
435 $cmddump = $conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
437 $outputfile = $outputdir.
'/'.$file;
439 $compression = $compression ? $compression :
'none';
440 if ($compression ==
'gz') $outputfile .=
'.gz';
441 if ($compression ==
'bz') $outputfile .=
'.bz2';
442 $outputerror = $outputfile.
'.err';
443 dol_mkdir($conf->admin->dir_output.
'/backup');
447 $command = preg_replace(
'/(\$|%)/',
'', $command);
448 if (preg_match(
"/\s/", $command)) $command = escapeshellarg($command);
453 $param .=
" --no-tablespaces --inserts -h ".$dolibarr_main_db_host;
454 $param .=
" -U ".$dolibarr_main_db_user;
455 if (!empty($dolibarr_main_db_port)) $param .=
" -p ".$dolibarr_main_db_port;
456 if (
GETPOST(
"sql_compat") &&
GETPOST(
"sql_compat") ==
'ANSI') $param .=
" --disable-dollar-quoting";
457 if (
GETPOST(
"drop_database")) $param .=
" -c -C";
460 if (
GETPOST(
"drop")) $param .=
" --add-drop-table";
461 if (!
GETPOST(
"sql_data")) $param .=
" -s";
465 if (!
GETPOST(
"sql_structure")) $param .=
" -a";
466 if (
GETPOST(
"showcolumns")) $param .=
" -c";
468 $param .=
' -f "'.$outputfile.
'"';
470 if ($compression ==
'gz') $param .=
' -Z 9';
472 $paramcrypted = $param;
473 $paramclear = $param;
479 $paramcrypted .=
" -w ".$dolibarr_main_db_name;
480 $paramclear .=
" -w ".$dolibarr_main_db_name;
483 $this->result = array(
"commandbackuplastdone" =>
"",
"commandbackuptorun" => $command.
" ".$paramcrypted);
487 if (!$errormsg && $keeplastnfiles > 0)
489 $tmpfiles =
dol_dir_list($conf->admin->dir_output.
'/backup',
'files', 0,
'',
'(\.err|\.old|\.sav)$',
'date', SORT_DESC);
491 foreach ($tmpfiles as $key => $val)
494 if ($i <= $keeplastnfiles)
continue;
499 return ($errormsg ? -1 : 0);
512 public function executeCLI($command, $outputfile, $execmethod = 0)
514 global $conf, $langs;
520 $command = escapeshellcmd($command);
523 if (!empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod = $conf->global->MAIN_EXEC_USE_POPEN;
524 if (empty($execmethod)) $execmethod = 1;
527 dol_syslog(
"Utils::executeCLI execmethod=".$execmethod.
" system:".$command, LOG_DEBUG);
528 $output_arr = array();
530 if ($execmethod == 1)
533 exec($command, $output_arr, $retval);
537 $langs->load(
"errors");
538 dol_syslog(
"Utils::executeCLI retval after exec=".$retval, LOG_ERR);
539 $error =
'Error '.$retval;
542 if ($execmethod == 2)
544 $handle = fopen($outputfile,
'w+b');
547 dol_syslog(
"Utils::executeCLI run command ".$command);
548 $handlein = popen($command,
'r');
549 while (!feof($handlein))
551 $read = fgets($handlein);
552 fwrite($handle, $read);
553 $output_arr[] = $read;
558 if (!empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
562 if (is_array($output_arr) && count($output_arr) > 0)
564 foreach ($output_arr as $val)
566 $output .= $val.($execmethod == 2 ?
'' :
"\n");
570 dol_syslog(
"Utils::executeCLI result=".$result.
" output=".$output.
" error=".$error, LOG_DEBUG);
572 return array(
'result'=>$result,
'output'=>$output,
'error'=>$error);
583 global $conf, $langs, $user, $mysoc;
588 $modulelowercase = strtolower($module);
592 $dir = $dirins.
'/'.$modulelowercase;
597 dol_include_once($modulelowercase.
'/core/modules/mod'.$module.
'.class.php');
598 $class =
'mod'.$module;
600 if (class_exists($class))
603 $moduleobj =
new $class($this->
db);
611 $langs->load(
"errors");
612 dol_print_error($langs->trans(
"ErrorFailedToLoadModuleDescriptorForXXX", $module));
616 $arrayversion = explode(
'.', $moduleobj->version, 3);
617 if (count($arrayversion))
619 $FILENAMEASCII = strtolower($module).
'.asciidoc';
620 $FILENAMEDOC = strtolower($module).
'.html';
621 $FILENAMEDOCPDF = strtolower($module).
'.pdf';
624 $dirofmoduledoc =
dol_buildpath(strtolower($module), 0).
'/doc';
625 $dirofmoduletmp =
dol_buildpath(strtolower($module), 0).
'/doc/temp';
626 $outputfiledoc = $dirofmoduledoc.
'/'.$FILENAMEDOC;
631 if (!is_writable($dirofmoduletmp))
633 $this->error =
'Dir '.$dirofmoduletmp.
' does not exists or is not writable';
637 if (empty($conf->global->MODULEBUILDER_ASCIIDOCTOR) && empty($conf->global->MODULEBUILDER_ASCIIDOCTORPDF))
639 $this->error =
'Setup of module ModuleBuilder not complete';
644 dol_copy($dirofmodule.
'/README.md', $dirofmoduletmp.
'/README.md', 0, 1);
645 dol_copy($dirofmodule.
'/ChangeLog.md', $dirofmoduletmp.
'/ChangeLog.md', 0, 1);
648 $arrayreplacement = array();
649 $arrayreplacement[
'/^#\s.*/m'] =
'';
650 $arrayreplacement[
'/^#/m'] =
'##';
652 dolReplaceInFile($dirofmoduletmp.
'/README.md', $arrayreplacement,
'', 0, 0, 1);
653 dolReplaceInFile($dirofmoduletmp.
'/ChangeLog.md', $arrayreplacement,
'', 0, 0, 1);
656 $destfile = $dirofmoduletmp.
'/'.$FILENAMEASCII;
658 $fhandle = fopen($destfile,
'w+');
661 $specs =
dol_dir_list(
dol_buildpath(strtolower($module).
'/doc', 0),
'files', 1,
'(\.md|\.asciidoc)$', array(
'\/temp\/'));
664 foreach ($specs as $spec)
666 if (preg_match(
'/notindoc/', $spec[
'relativename']))
continue;
667 if (preg_match(
'/example/', $spec[
'relativename']))
continue;
668 if (preg_match(
'/disabled/', $spec[
'relativename']))
continue;
670 $pathtofile = strtolower($module).
'/doc/'.$spec[
'relativename'];
671 $format =
'asciidoc';
672 if (preg_match(
'/\.md$/i', $spec[
'name'])) $format =
'markdown';
674 $filecursor = @file_get_contents($spec[
'fullname']);
677 fwrite($fhandle, ($i ?
"\n<<<\n\n" :
"").$filecursor.
"\n");
679 $this->error =
'Failed to concat content of file '.$spec[
'fullname'];
688 $contentreadme = file_get_contents($dirofmoduletmp.
'/README.md');
689 $contentchangelog = file_get_contents($dirofmoduletmp.
'/ChangeLog.md');
691 include DOL_DOCUMENT_ROOT.
'/core/lib/parsemd.lib.php';
694 $arrayreplacement = array(
695 'mymodule'=>strtolower($module),
697 'MYMODULE'=>strtoupper($module),
698 'My module'=>$module,
699 'my module'=>$module,
700 'Mon module'=>$module,
701 'mon module'=>$module,
702 'htdocs/modulebuilder/template'=>strtolower($module),
703 '__MYCOMPANY_NAME__'=>$mysoc->name,
704 '__KEYWORDS__'=>$module,
705 '__USER_FULLNAME__'=>$user->getFullName($langs),
706 '__USER_EMAIL__'=>$user->email,
708 '---Put here your own copyright and developer email---'=>
dol_print_date($now,
'dayrfc').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
''),
709 '__DATA_SPECIFICATION__'=>
'Not yet available',
718 $currentdir = getcwd();
721 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
725 $command = $conf->global->MODULEBUILDER_ASCIIDOCTOR.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOC;
726 $outfile = $dirofmoduletmp.
'/out.tmp';
728 $resarray = $utils->executeCLI($command, $outfile);
729 if ($resarray[
'result'] !=
'0')
731 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
733 $result = ($resarray[
'result'] == 0) ? 1 : 0;
736 $command = $conf->global->MODULEBUILDER_ASCIIDOCTORPDF.
' '.$destfile.
' -n -o '.$dirofmoduledoc.
'/'.$FILENAMEDOCPDF;
737 $outfile = $dirofmoduletmp.
'/outpdf.tmp';
738 $resarray = $utils->executeCLI($command, $outfile);
739 if ($resarray[
'result'] !=
'0')
741 $this->error = $resarray[
'error'].
' '.$resarray[
'output'];
743 $result = ($resarray[
'result'] == 0) ? 1 : 0;
755 $langs->load(
"errors");
756 $this->error = $langs->trans(
"ErrorFailToGenerateFile", $outputfiledoc);
760 $langs->load(
"errors");
761 $this->error = $langs->trans(
"ErrorCheckVersionIsDefined");
778 if (empty($conf->loghandlers[
'mod_syslog_file'])) {
782 if (!function_exists(
'gzopen')) {
783 $this->error =
'Support for gzopen not available in this PHP';
789 $nbSaves = empty($conf->global->SYSLOG_FILE_SAVES) ? 10 : intval($conf->global->SYSLOG_FILE_SAVES);
791 if (empty($conf->global->SYSLOG_FILE)) {
792 $mainlogdir = DOL_DATA_ROOT;
793 $mainlog =
'dolibarr.log';
795 $mainlogfull = str_replace(
'DOL_DATA_ROOT', DOL_DATA_ROOT, $conf->global->SYSLOG_FILE);
796 $mainlogdir = dirname($mainlogfull);
797 $mainlog = basename($mainlogfull);
800 $tabfiles =
dol_dir_list(DOL_DATA_ROOT,
'files', 0,
'^(dolibarr_.+|odt2pdf)\.log$');
801 $tabfiles[] = array(
'name' => $mainlog,
'path' => $mainlogdir);
803 foreach ($tabfiles as $file) {
804 $logname = $file[
'name'];
805 $logpath = $file[
'path'];
811 $filter =
'^'.preg_quote($logname,
'/').
'\.([0-9]+)\.gz$';
813 $gzfilestmp =
dol_dir_list($logpath,
'files', 0, $filter);
816 foreach ($gzfilestmp as $gzfile) {
817 $tabmatches = array();
818 preg_match(
'/'.$filter.
'/i', $gzfile[
'name'], $tabmatches);
820 $numsave = intval($tabmatches[1]);
822 $gzfiles[$numsave] = $gzfile;
825 krsort($gzfiles, SORT_NUMERIC);
827 foreach ($gzfiles as $numsave => $dummy) {
828 if (
dol_is_file($logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz')) {
832 if ($numsave >= $nbSaves) {
833 dol_delete_file($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', 0, 0, 0, null,
false, 0);
835 dol_move($logpath.
'/'.$logname.
'.'.$numsave.
'.gz', $logpath.
'/'.$logname.
'.'.($numsave + 1).
'.gz', 0, 1, 0, 0);
842 $gzfilehandle = gzopen($logpath.
'/'.$logname.
'.1.gz',
'wb9');
844 if (empty($gzfilehandle)) {
845 $this->error =
'Failted to open file '.$logpath.
'/'.$logname.
'.1.gz';
849 $sourcehandle = fopen($logpath.
'/'.$logname,
'r');
851 if (empty($sourcehandle)) {
852 $this->error =
'Failed to open file '.$logpath.
'/'.$logname;
856 while (!feof($sourcehandle)) {
857 gzwrite($gzfilehandle, fread($sourcehandle, 512 * 1024));
860 fclose($sourcehandle);
861 gzclose($gzfilehandle);
863 @chmod($logpath.
'/'.$logname.
'.1.gz', octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
869 $newlog = fopen($logpath.
'/'.$logname,
'a+');
873 @chmod($logpath.
'/'.$logname, octdec(empty($conf->global->MAIN_UMASK) ?
'0664' : $conf->global->MAIN_UMASK));
877 $this->output =
'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.
' files) done.';
891 public function backupTables($outputfile, $tables =
'*')
897 if (is_a($db,
'DoliDBMysqli')) {
899 $db->db->set_charset(
'utf8');
902 $db->query(
'SET NAMES utf8');
903 $db->query(
'SET CHARACTER SET utf8');
910 $result = $db->query(
'SHOW FULL TABLES WHERE Table_type = \'BASE TABLE\'');
911 while ($row = $db->fetch_row($result))
916 $tables = is_array($tables) ? $tables : explode(
',', $tables);
920 $handle = fopen($outputfile,
'w+');
921 if (fwrite($handle,
'') ===
false)
923 $langs->load(
"errors");
924 dol_syslog(
"Failed to open file ".$outputfile, LOG_ERR);
925 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir");
931 $sqlhead .=
"-- ".$db::LABEL.
" dump via php with Dolibarr ".DOL_VERSION.
"
933 -- Host: ".$db->db->host_info.
" Database: ".$db->database_name.
"
934 -- ------------------------------------------------------
935 -- Server version ".$db->db->server_info.
"
950 if (
GETPOST(
"nobin_disable_fk")) $sqlhead .=
"SET FOREIGN_KEY_CHECKS=0;\n";
952 if (
GETPOST(
"nobin_use_transaction")) $sqlhead .=
"SET AUTOCOMMIT=0;\nSTART TRANSACTION;\n";
954 fwrite($handle, $sqlhead);
957 if (
GETPOST(
"nobin_sql_ignore")) $ignore =
'IGNORE ';
959 if (
GETPOST(
"nobin_delayed")) $delayed =
'DELAYED ';
962 foreach ($tables as $table)
965 fwrite($handle,
"\n--\n-- Table structure for table `".$table.
"`\n--\n");
967 if (
GETPOST(
"nobin_drop")) fwrite($handle,
"DROP TABLE IF EXISTS `".$table.
"`;\n");
968 fwrite($handle,
"/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
969 fwrite($handle,
"/*!40101 SET character_set_client = utf8 */;\n");
970 $resqldrop = $db->query(
'SHOW CREATE TABLE '.$table);
971 $row2 = $db->fetch_row($resqldrop);
974 fwrite($handle,
"\n-- WARNING: Show create table ".$table.
" return empy string when it should not.\n");
976 fwrite($handle, $row2[1].
";\n");
980 fwrite($handle,
"\n--\n-- Dumping data for table `".$table.
"`\n--\n");
981 if (!
GETPOST(
"nobin_nolocks")) fwrite($handle,
"LOCK TABLES `".$table.
"` WRITE;\n");
982 if (
GETPOST(
"nobin_disable_fk")) fwrite($handle,
"ALTER TABLE `".$table.
"` DISABLE KEYS;\n");
983 else fwrite($handle,
"/*!40000 ALTER TABLE `".$table.
"` DISABLE KEYS */;\n");
985 $sql =
'SELECT * FROM '.$table;
986 $result = $db->query($sql);
987 while ($row = $db->fetch_row($result))
990 fwrite($handle,
'INSERT '.$delayed.$ignore.
'INTO `'.$table.
'` VALUES (');
991 $columns = count($row);
992 for ($j = 0; $j < $columns; $j++) {
994 if ($row[$j] == null && !is_string($row[$j])) {
997 } elseif (is_string($row[$j]) && $row[$j] ==
'') {
1000 } elseif (is_numeric($row[$j]) && !strcmp($row[$j], $row[$j] + 0)) {
1004 $row[$j] = addslashes($row[$j]);
1005 $row[$j] = preg_replace(
"#\n#",
"\\n", $row[$j]);
1006 $row[$j] =
"'".$row[$j].
"'";
1009 fwrite($handle, implode(
',', $row).
");\n");
1011 if (
GETPOST(
"nobin_disable_fk")) fwrite($handle,
"ALTER TABLE `".$table.
"` ENABLE KEYS;\n");
1012 if (!
GETPOST(
"nobin_nolocks")) fwrite($handle,
"UNLOCK TABLES;\n");
1013 fwrite($handle,
"\n\n\n");
1037 $sqlfooter =
"\n\n";
1038 if (
GETPOST(
"nobin_use_transaction")) $sqlfooter .=
"COMMIT;\n";
1039 if (
GETPOST(
"nobin_disable_fk")) $sqlfooter .=
"SET FOREIGN_KEY_CHECKS=1;\n";
1040 $sqlfooter .=
"\n\n-- Dump completed on ".date(
'Y-m-d G-i-s');
1041 fwrite($handle, $sqlfooter);
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_now($mode= 'auto')
Return date for now.
dol_filesize($pathoffile)
Return size of a file.
executeCLI($command, $outputfile, $execmethod=0)
Execute a CLI command.
dol_is_dir($folder)
Test if filename is a directory.
compressSyslogs()
This saves syslog files and compresses older ones.
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.
$conf db
API class for accounts.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
purgeFiles($choices= 'tempfilesold, logfile', $nbsecondsold=86400)
Purge files into directory of data files.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories) ...
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
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_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.
Class to manage ECM directories.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)