26 if (! defined(
'CSRFCHECK_WITH_TOKEN')) {
27 define(
'CSRFCHECK_WITH_TOKEN',
'1');
30 require
'../../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formfile.class.php';
36 $langs->load(
"admin");
38 $action =
GETPOST(
'action',
'aZ09');
39 $what =
GETPOST(
'what',
'alpha');
40 $export_type =
GETPOST(
'export_type',
'alpha');
41 $file = trim(
GETPOST(
'zipfilename_template',
'alpha'));
42 $compression =
GETPOST(
'compression',
'aZ09');
45 $file = preg_replace(
'/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i',
'', $file);
47 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
48 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
58 } elseif (empty($page)) {
61 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
62 $offset = $limit * $page;
75 if ($action ==
'delete') {
77 $filepath = $conf->admin->dir_output.
'/'.$filerelative;
80 setEventMessages($langs->trans(
"FileWasRemoved", $filerelative),
null,
'mesgs');
82 setEventMessages($langs->trans(
"ErrorFailToDeleteFile", $filerelative),
null,
'errors');
93 $ExecTimeLimit = 1800;
94 if (!empty($ExecTimeLimit)) {
95 $err = error_reporting();
98 @set_time_limit($ExecTimeLimit);
99 error_reporting($err);
104 if (!empty($MemoryLimit)) {
105 @ini_set(
'memory_limit', $MemoryLimit);
119 $dump_buffer_len = 0;
122 $time_start = time();
125 $outputdir = $conf->admin->dir_output.
'/documents';
128 $utils =
new Utils($db);
130 if ($export_type ==
'externalmodule' && ! empty($what)) {
133 $fulldirtocompress = DOL_DATA_ROOT;
135 $dirtoswitch = dirname($fulldirtocompress);
136 $dirtocompress = basename($fulldirtocompress);
138 if ($compression ==
'zip') {
141 $excludefiles =
'/(\.back|\.old|\.log|\.pdf_preview-.*\.png|[\/\\\]temp[\/\\\]|[\/\\\]admin[\/\\\]documents[\/\\\])/i';
147 if ($export_type ==
'externalmodule' && !empty($what)) {
148 $rootdirinzip = $what;
150 global $dolibarr_allow_download_external_modules;
151 if (empty($dolibarr_allow_download_external_modules)) {
152 print
'Download of external modules is not allowed by $dolibarr_allow_download_external_modules in conf.php file';
158 $ret = dol_compress_dir($fulldirtocompress, $outputdir.
"/".$file, $compression, $excludefiles, $rootdirinzip);
161 $langs->load(
"errors");
162 $errormsg = $langs->trans(
"ErrNoZipEngine");
164 $langs->load(
"errors");
165 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir", $outputdir);
168 } elseif (in_array($compression, array(
'gz',
'bz',
'zstd'))) {
169 $userlogin = ($user->login ? $user->login :
'unknown');
171 $outputfile = $conf->admin->dir_temp.
'/export_files.'.$userlogin.
'.out';
177 $cmd =
"tar -cf '".escapeshellcmd($outputdir.
"/".$file).
"' --exclude-vcs --exclude-caches-all --exclude='temp' --exclude='*.log' --exclude='*.pdf_preview-*.png' --exclude='documents/admin/documents' -C '".escapeshellcmd(
dol_sanitizePathName($dirtoswitch)).
"' '".escapeshellcmd(
dol_sanitizeFileName($dirtocompress)).
"'";
179 $result = $utils->executeCLI($cmd, $outputfile, 0,
null, 1);
181 $retval = $result[
'error'];
182 if ($result[
'result'] || !empty($retval)) {
183 $langs->load(
"errors");
184 dol_syslog(
"Documents tar retval after exec=".$retval, LOG_ERR);
185 $errormsg =
'Error tar generation return '.$retval;
187 if ($compression ==
'gz') {
188 $cmd =
"gzip -f ".$outputdir.
"/".$file;
189 } elseif ($compression ==
'bz') {
190 $cmd =
"bzip2 -f ".$outputdir.
"/".$file;
191 } elseif ($compression ==
'zstd') {
192 $cmd =
"zstd -z -9 -q --rm ".$outputdir.
"/".$file;
195 $result = $utils->executeCLI($cmd, $outputfile);
197 $retval = $result[
'error'];
198 if ($result[
'result'] || !empty($retval)) {
199 $errormsg =
'Error '.$compression.
' generation return '.$retval;
200 unlink($outputdir.
"/".$file);
204 $errormsg =
'Bad value for compression method';
208 if ($export_type !=
'externalmodule' || empty($what)) {
212 setEventMessages($langs->trans(
"BackupFileSuccessfullyCreated").
'.<br>'.$langs->trans(
"YouCanDownloadBackupFile"),
null,
'mesgs');
218 $returnto =
'dolibarr_export.php';
220 header(
"Location: ".$returnto);
223 $zipname = $outputdir.
"/".$file;
226 header(
'Content-Type: application/zip');
227 header(
'Content-disposition: attachment; filename='.basename($zipname));
228 header(
'Content-Length: '.filesize($zipname));