27if (! defined(
'CSRFCHECK_WITH_TOKEN')) {
28 define(
'CSRFCHECK_WITH_TOKEN',
'1');
32require
'../../main.inc.php';
42require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
43require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
44require_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
45require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formfile.class.php';
49$action =
GETPOST(
'action',
'aZ09');
50$what =
GETPOST(
'what',
'alpha');
51$export_type =
GETPOST(
'export_type',
'alpha');
52$file = trim(
GETPOST(
'zipfilename_template',
'alpha'));
53$compression =
GETPOST(
'compression',
'aZ09');
56$file = preg_replace(
'/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i',
'', $file);
58$sortfield =
GETPOST(
'sortfield',
'aZ09comma');
59$sortorder =
GETPOST(
'sortorder',
'aZ09comma');
69} elseif (empty($page)) {
73$offset = $limit * $page;
86if ($action ==
'delete') {
88 $filepath =
$conf->admin->dir_output.
'/'.$filerelative;
91 setEventMessages($langs->trans(
"FileWasRemoved", $filerelative),
null,
'mesgs');
93 setEventMessages($langs->trans(
"ErrorFailToDeleteFile", $filerelative),
null,
'errors');
104$ExecTimeLimit =
getDolGlobalInt(
'MAIN_ADMIN_TOOLS_EXPORT_FILES_EXEC_TIME_LIMIT', 1800);;
105if (!empty($ExecTimeLimit)) {
106 $err = error_reporting();
109 @set_time_limit($ExecTimeLimit);
110 error_reporting($err);
115if (!empty($MemoryLimit)) {
116 @ini_set(
'memory_limit', $MemoryLimit);
119$form =
new Form($db);
138if ($export_type ==
'app') {
139 $outputdir =
$conf->admin->dir_output.
'/backupapp';
141 $outputdir =
$conf->admin->dir_output.
'/documents';
145$utils =
new Utils($db);
150if ($export_type ==
'app' && empty($dolibarr_allow_download_app)) {
151 setEventMessages($langs->trans(
"DownloadOfAppFileDisallowed"),
null,
'errors');
153 header(
"Location: dolibarr_export.php");
157if ($export_type ==
'externalmodule' && !empty($what)) {
159 global $dolibarr_allow_download_app;
160 if (empty($dolibarr_allow_download_app)) {
161 print
'Download of external modules is not allowed by $dolibarr_allow_download_app in conf.php file';
166 if (!preg_match(
'/^[a-z0-9_\-]+$/i', $what) || !is_dir(DOL_DOCUMENT_ROOT.
'/custom/'.
dol_sanitizeFileName($what))) {
167 print
'Bad value for parameter what';
172} elseif ($export_type ==
'app') {
173 $fulldirtocompress = DOL_DOCUMENT_ROOT;
175 $fulldirtocompress = DOL_DATA_ROOT;
177$dirtoswitch = dirname($fulldirtocompress);
178$dirtocompress = basename($fulldirtocompress);
180if ($compression ==
'zip') {
183 $excludefiles =
'/(\.back|\.old|\.log|\.pdf_preview-.*\.png|[\/\\\]temp[\/\\\]|[\/\\\]admin[\/\\\]documents[\/\\\]|[\/\\\]admin[\/\\\]backup[\/\\\]|[\/\\\]admin[\/\\\]backupapp[\/\\\])/i';
189 if ($export_type ==
'externalmodule' && !empty($what)) {
190 $rootdirinzip = $what;
192 if ($export_type ==
'app') {
193 $rootdirinzip = basename(DOL_DOCUMENT_ROOT);
197 $ret = dol_compress_dir($fulldirtocompress, $outputdir.
"/".$file, $compression, $excludefiles, $rootdirinzip);
200 $langs->load(
"errors");
201 $errormsg = $langs->trans(
"ErrNoZipEngine");
203 $langs->load(
"errors");
205 $errormsg = $langs->trans(
"ErrorFailedToWriteInDir", $outputdir).($errormsg ?
"\n" . $errormsg :
"");
208} elseif (in_array($compression, array(
'gz',
'bz',
'zstd'))) {
209 $userlogin = ($user->login ? $user->login :
'unknown');
228 $cmd =
"tar -cf '".escapeshellcmd($tmpfile).
"' --exclude-vcs --exclude-caches-all --exclude='temp' --exclude='*.log' --exclude='*.pdf_preview-*.png' --exclude='admin/documents' --exclude='admin/backup' --exclude='admin/backupapp' -C '".escapeshellcmd(
dol_sanitizePathName($dirtoswitch)).
"' '".escapeshellcmd(
dol_sanitizeFileName($dirtocompress)).
"'";
230 $result = $utils->executeCLI($cmd, $outputfile, 0,
null, 1);
232 $retval = $result[
'error'];
233 if ($result[
'result'] || !empty($retval)) {
234 $langs->load(
"errors");
235 dol_syslog(
"Documents tar retval after exec=".$retval, LOG_ERR);
236 $errormsg =
'Error tar generation return '.$retval;
237 if (file_exists($tmpfile)) {
241 $compressedtmpfile = $tmpfile;
242 if ($compression ==
'gz') {
243 $cmd =
"gzip -f ".$tmpfile;
244 $compressedtmpfile = $tmpfile.
'.gz';
245 } elseif ($compression ==
'bz') {
246 $cmd =
"bzip2 -f ".$tmpfile;
247 $compressedtmpfile = $tmpfile.
'.bz2';
248 } elseif ($compression ==
'zstd') {
249 $cmd =
"zstd -z -9 -q --rm ".$tmpfile;
250 $compressedtmpfile = $tmpfile.
'.zst';
253 $result = $utils->executeCLI($cmd, $outputfile);
255 $retval = $result[
'error'];
256 if ($result[
'result'] || !empty($retval)) {
257 $errormsg =
'Error '.$compression.
' generation return '.$retval;
258 if (file_exists($tmpfile)) {
261 if (file_exists($compressedtmpfile)) {
262 unlink($compressedtmpfile);
266 $finalfile = $outputdir.
'/'.basename($compressedtmpfile);
267 if (!@rename($compressedtmpfile, $finalfile)) {
268 $errormsg =
'Error moving generated archive to '.$outputdir;
269 if (file_exists($compressedtmpfile)) {
270 unlink($compressedtmpfile);
273 $file = basename($compressedtmpfile);
278 $errormsg =
'Bad value for compression method';
285if ($export_type !=
'externalmodule' || empty($what)) {
289 setEventMessages($langs->trans(
"Error").
" : ".$errormsg,
null,
'errors');
291 setEventMessages($langs->trans(
"BackupFileSuccessfullyCreated").
'.<br>'.$langs->trans(
"YouCanDownloadBackupFile"),
null,
'mesgs');
297 $returnto =
'dolibarr_export.php';
298 if ($export_type ==
'app' ||
GETPOSTINT(
'allow_download_app')) {
300 $returnto .=
'?allow_download_app=1';
303 header(
"Location: ".$returnto);
309 $zipname = $outputdir.
"/".$file;
313 header(
'Content-disposition: attachment; filename='.basename($zipname));
314 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.
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.