27if (! defined(
'CSRFCHECK_WITH_TOKEN')) {
28 define(
'CSRFCHECK_WITH_TOKEN',
'1');
32require
'../../main.inc.php';
40require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
41require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
43require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formfile.class.php';
47$action =
GETPOST(
'action',
'aZ09');
48$what =
GETPOST(
'what',
'alpha');
49$export_type =
GETPOST(
'export_type',
'alpha');
50$file = trim(
GETPOST(
'zipfilename_template',
'alpha'));
51$compression =
GETPOST(
'compression',
'aZ09');
54$file = preg_replace(
'/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i',
'', $file);
56$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
57$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
67} elseif (empty($page)) {
71$offset = $limit * $page;
84if ($action ==
'delete') {
86 $filepath =
$conf->admin->dir_output.
'/'.$filerelative;
89 setEventMessages($langs->trans(
"FileWasRemoved", $filerelative),
null,
'mesgs');
91 setEventMessages($langs->trans(
"ErrorFailToDeleteFile", $filerelative),
null,
'errors');
102$ExecTimeLimit =
getDolGlobalInt(
'MAIN_ADMIN_TOOLS_EXPORT_FILES_EXEC_TIME_LIMIT', 1800);;
103if (!empty($ExecTimeLimit)) {
104 $err = error_reporting();
107 @set_time_limit($ExecTimeLimit);
108 error_reporting($err);
113if (!empty($MemoryLimit)) {
114 @ini_set(
'memory_limit', $MemoryLimit);
134$outputdir =
$conf->admin->dir_output.
'/documents';
139if ($export_type ==
'externalmodule' && !empty($what)) {
141 global $dolibarr_allow_download_external_modules;
142 if (empty($dolibarr_allow_download_external_modules)) {
143 print
'Download of external modules is not allowed by $dolibarr_allow_download_external_modules in conf.php file';
148 if (!preg_match(
'/^[a-z0-9_\-]+$/i', $what) || !is_dir(DOL_DOCUMENT_ROOT.
'/custom/'.
dol_sanitizeFileName($what))) {
149 print
'Bad value for parameter what';
155 $fulldirtocompress = DOL_DATA_ROOT;
157$dirtoswitch = dirname($fulldirtocompress);
158$dirtocompress = basename($fulldirtocompress);
160if ($compression ==
'zip') {
163 $excludefiles =
'/(\.back|\.old|\.log|\.pdf_preview-.*\.png|[\/\\\]temp[\/\\\]|[\/\\\]admin[\/\\\]documents[\/\\\]|[\/\\\]admin[\/\\\]backup[\/\\\])/i';
169 if ($export_type ==
'externalmodule' && !empty($what)) {
170 $rootdirinzip = $what;
174 $ret = dol_compress_dir($fulldirtocompress, $outputdir.
"/".$file, $compression, $excludefiles, $rootdirinzip);
177 $langs->load(
"errors");
178 $errormsg = $langs->trans(
"ErrNoZipEngine");
180 $langs->load(
"errors");
182 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir", $outputdir).($errormsg ?
"\n" . $errormsg :
"");
185} elseif (in_array($compression, array(
'gz',
'bz',
'zstd'))) {
186 $userlogin = ($user->login ? $user->login :
'unknown');
204 $cmd =
"tar -cf '".escapeshellcmd($tmpfile).
"' --exclude-vcs --exclude-caches-all --exclude='temp' --exclude='*.log' --exclude='*.pdf_preview-*.png' --exclude='admin/documents' --exclude='admin/backup' -C '".escapeshellcmd(
dol_sanitizePathName($dirtoswitch)).
"' '".escapeshellcmd(
dol_sanitizeFileName($dirtocompress)).
"'";
206 $result = $utils->executeCLI($cmd, $outputfile, 0,
null, 1);
208 $retval = $result[
'error'];
209 if ($result[
'result'] || !empty($retval)) {
210 $langs->load(
"errors");
211 dol_syslog(
"Documents tar retval after exec=".$retval, LOG_ERR);
212 $errormsg =
'Error tar generation return '.$retval;
213 if (file_exists($tmpfile)) {
217 $compressedtmpfile = $tmpfile;
218 if ($compression ==
'gz') {
219 $cmd =
"gzip -f ".$tmpfile;
220 $compressedtmpfile = $tmpfile.
'.gz';
221 } elseif ($compression ==
'bz') {
222 $cmd =
"bzip2 -f ".$tmpfile;
223 $compressedtmpfile = $tmpfile.
'.bz2';
224 } elseif ($compression ==
'zstd') {
225 $cmd =
"zstd -z -9 -q --rm ".$tmpfile;
226 $compressedtmpfile = $tmpfile.
'.zst';
229 $result = $utils->executeCLI($cmd, $outputfile);
231 $retval = $result[
'error'];
232 if ($result[
'result'] || !empty($retval)) {
233 $errormsg =
'Error '.$compression.
' generation return '.$retval;
234 if (file_exists($tmpfile)) {
237 if (file_exists($compressedtmpfile)) {
238 unlink($compressedtmpfile);
242 $finalfile = $outputdir.
'/'.basename($compressedtmpfile);
243 if (!@rename($compressedtmpfile, $finalfile)) {
244 $errormsg =
'Error moving generated archive to '.$outputdir;
245 if (file_exists($compressedtmpfile)) {
246 unlink($compressedtmpfile);
249 $file = basename($compressedtmpfile);
254 $errormsg =
'Bad value for compression method';
261if ($export_type !=
'externalmodule' || empty($what)) {
267 setEventMessages($langs->trans(
"BackupFileSuccessfullyCreated").
'.<br>'.$langs->trans(
"YouCanDownloadBackupFile"),
null,
'mesgs');
273 $returnto =
'dolibarr_export.php';
275 header(
"Location: ".$returnto);
281 $zipname = $outputdir.
"/".$file;
285 header(
'Content-disposition: attachment; filename='.basename($zipname));
286 header(
'Content-Length: '.filesize($zipname));
Class to manage utility methods.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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)
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.