dolibarr 24.0.0-beta
export_files.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
5 * Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27if (! defined('CSRFCHECK_WITH_TOKEN')) {
28 define('CSRFCHECK_WITH_TOKEN', '1'); // Force use of CSRF protection with tokens even for GET
29}
30
31// Load Dolibarr environment
32require '../../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
37
46$langs->load("admin");
47
48$action = GETPOST('action', 'aZ09');
49$what = GETPOST('what', 'alpha');
50$export_type = GETPOST('export_type', 'alpha');
51$file = trim(GETPOST('zipfilename_template', 'alpha'));
52$compression = GETPOST('compression', 'aZ09');
53
54$file = dol_sanitizeFileName($file, '_', 1, 1);
55$file = preg_replace('/(\.zip|\.tar|\.tgz|\.gz|\.tar\.gz|\.bz2|\.zst)$/i', '', $file);
56
57$sortfield = GETPOST('sortfield', 'aZ09comma');
58$sortorder = GETPOST('sortorder', 'aZ09comma');
59$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
60if (!$sortorder) {
61 $sortorder = "DESC";
62}
63if (!$sortfield) {
64 $sortfield = "date";
65}
66if ($page < 0) {
67 $page = 0;
68} elseif (empty($page)) {
69 $page = 0;
70}
71$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
72$offset = $limit * $page;
73
74if (!$user->admin) {
76}
77
78$errormsg = '';
79
80
81/*
82 * Actions
83 */
84
85if ($action == 'delete') {
86 $filerelative = dol_sanitizeFileName(GETPOST('urlfile', 'alpha'));
87 $filepath = $conf->admin->dir_output.'/'.$filerelative;
88 $ret = dol_delete_file($filepath, 1);
89 if ($ret) {
90 setEventMessages($langs->trans("FileWasRemoved", $filerelative), null, 'mesgs');
91 } else {
92 setEventMessages($langs->trans("ErrorFailToDeleteFile", $filerelative), null, 'errors');
93 }
94 $action = '';
95}
96
97
98/*
99 * View
100 */
101
102// Increase limit of time. Works only if we are not in safe mode
103$ExecTimeLimit = getDolGlobalInt('MAIN_ADMIN_TOOLS_EXPORT_FILES_EXEC_TIME_LIMIT', 1800);; // 30mn
104if (!empty($ExecTimeLimit)) {
105 $err = error_reporting();
106 error_reporting(0); // Disable all errors
107 //error_reporting(E_ALL);
108 @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
109 error_reporting($err);
110}
111
112/* If value has been forced with a php_admin_value, this has no effect. Example of value: '512M' */
113$MemoryLimit = getDolGlobalString('MAIN_MEMORY_LIMIT_ARCHIVE_DATAROOT');
114if (!empty($MemoryLimit)) {
115 @ini_set('memory_limit', $MemoryLimit);
116}
117
118$form = new Form($db);
119$formfile = new FormFile($db);
120
121//$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
122//llxHeader('','',$help_url);
123
124//print load_fiche_titre($langs->trans("Backup"),'','title_setup');
125
126
127// Start with empty buffer
128$dump_buffer = '';
129$dump_buffer_len = 0;
130
131// We will send fake headers to avoid browser timeout when buffering
132$time_start = time();
133
134
135$outputdir = $conf->admin->dir_output.'/documents';
136$result = dol_mkdir($outputdir);
137
138$utils = new Utils($db);
139
140if ($export_type == 'externalmodule' && !empty($what)) {
141 $fulldirtocompress = DOL_DOCUMENT_ROOT.'/custom/'.dol_sanitizeFileName($what);
142} else {
143 $fulldirtocompress = DOL_DATA_ROOT;
144}
145$dirtoswitch = dirname($fulldirtocompress);
146$dirtocompress = basename($fulldirtocompress);
147
148if ($compression == 'zip') {
149 $file .= '.zip';
150
151 $excludefiles = '/(\.back|\.old|\.log|\.pdf_preview-.*\.png|[\/\\\]temp[\/\\\]|[\/\\\]admin[\/\\\]documents[\/\\\])/i';
152
153 //var_dump($fulldirtocompress);
154 //var_dump($outputdir."/".$file);exit;
155
156 $rootdirinzip = '';
157 if ($export_type == 'externalmodule' && !empty($what)) {
158 $rootdirinzip = $what;
159
160 global $dolibarr_allow_download_external_modules;
161 if (empty($dolibarr_allow_download_external_modules)) {
162 print 'Download of external modules is not allowed by $dolibarr_allow_download_external_modules in conf.php file';
163 $db->close();
164 exit();
165 }
166 }
167
168 global $errormsg;
169 $ret = dol_compress_dir($fulldirtocompress, $outputdir."/".$file, $compression, $excludefiles, $rootdirinzip); // Can modify $errormsg
170 if ($ret < 0) {
171 if ($ret == -2) {
172 $langs->load("errors");
173 $errormsg = $langs->trans("ErrNoZipEngine");
174 } else {
175 $langs->load("errors");
176 // @phpstan-ignore-next-line The $errormsg can have been modified by the dol_compress_dir function.
177 $errormsg = $langs->trans("ErrorFailedToWriteInDir", $outputdir).($errormsg ? "\n" . $errormsg : "");
178 }
179 }
180} elseif (in_array($compression, array('gz', 'bz', 'zstd'))) {
181 $userlogin = ($user->login ? $user->login : 'unknown');
182
183 $outputfile = $conf->admin->dir_temp.'/'.dol_sanitizeFileName('export_files.'.$userlogin.'.out'); // File used with popen method
184
185 $file .= '.tar';
186
187 // Write the tar into a temp directory outside the documents tree.
188 // If we wrote it directly under $outputdir (= DOL_DATA_ROOT/admin/documents),
189 // tar would notice its own output directory growing as it reads the source
190 // and exit with code 1 / 'file changed as we read it', even though the archive
191 // is complete. The error short-circuited compression at line 194 and left
192 // users with an uncompressed .tar plus a misleading error (#37266).
193 $tmpfile = $conf->admin->dir_temp.'/'.dol_sanitizeFileName($file);
194
195 // We also exclude '/temp/' dir and 'documents/admin/documents'
196 // We make escapement here and call executeCLI without escapement because we don't want to have the '*.log' escaped.
197 $cmd = "tar -cf '".escapeshellcmd($tmpfile)."' --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))."'";
198
199 $result = $utils->executeCLI($cmd, $outputfile, 0, null, 1);
200
201 $retval = $result['error'];
202 if ($result['result'] || !empty($retval)) {
203 $langs->load("errors");
204 dol_syslog("Documents tar retval after exec=".$retval, LOG_ERR);
205 $errormsg = 'Error tar generation return '.$retval;
206 if (file_exists($tmpfile)) {
207 unlink($tmpfile);
208 }
209 } else {
210 $compressedtmpfile = $tmpfile;
211 if ($compression == 'gz') {
212 $cmd = "gzip -f ".$tmpfile;
213 $compressedtmpfile = $tmpfile.'.gz';
214 } elseif ($compression == 'bz') {
215 $cmd = "bzip2 -f ".$tmpfile;
216 $compressedtmpfile = $tmpfile.'.bz2';
217 } elseif ($compression == 'zstd') {
218 $cmd = "zstd -z -9 -q --rm ".$tmpfile;
219 $compressedtmpfile = $tmpfile.'.zst';
220 }
221
222 $result = $utils->executeCLI($cmd, $outputfile);
223
224 $retval = $result['error'];
225 if ($result['result'] || !empty($retval)) {
226 $errormsg = 'Error '.$compression.' generation return '.$retval;
227 if (file_exists($tmpfile)) {
228 unlink($tmpfile);
229 }
230 if (file_exists($compressedtmpfile)) {
231 unlink($compressedtmpfile);
232 }
233 } else {
234 // Move the compressed archive from temp to the final outputdir.
235 $finalfile = $outputdir.'/'.basename($compressedtmpfile);
236 if (!@rename($compressedtmpfile, $finalfile)) {
237 $errormsg = 'Error moving generated archive to '.$outputdir;
238 if (file_exists($compressedtmpfile)) {
239 unlink($compressedtmpfile);
240 }
241 } else {
242 $file = basename($compressedtmpfile);
243 }
244 }
245 }
246} else {
247 $errormsg = 'Bad value for compression method';
248 print $errormsg;
249}
250
251
252// Output export
253
254if ($export_type != 'externalmodule' || empty($what)) {
255 top_httphead();
256
257 if ($errormsg) {
258 setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
259 } else {
260 setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
261 }
262
263 $db->close();
264
265 // Redirect to calling page
266 $returnto = 'dolibarr_export.php';
267
268 header("Location: ".$returnto);
269
270 exit();
271} else {
272 top_httphead('application/zip');
273
274 $zipname = $outputdir."/".$file;
275
276 // Then download the zipped file.
277
278 header('Content-disposition: attachment; filename='.basename($zipname));
279 header('Content-Length: '.filesize($zipname));
280 readfile($zipname);
281
282 dol_delete_file($zipname);
283
284 $db->close();
285
286 exit();
287}
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
Class to manage utility methods.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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)
Return value of a param into GET or POST supervariable.
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.