dolibarr 21.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 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);
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 = 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 $ret = dol_compress_dir($fulldirtocompress, $outputdir."/".$file, $compression, $excludefiles, $rootdirinzip);
169 if ($ret < 0) {
170 if ($ret == -2) {
171 $langs->load("errors");
172 $errormsg = $langs->trans("ErrNoZipEngine");
173 } else {
174 $langs->load("errors");
175 $errormsg = $langs->trans("ErrorFailedToWriteInDir", $outputdir);
176 }
177 }
178} elseif (in_array($compression, array('gz', 'bz', 'zstd'))) {
179 $userlogin = ($user->login ? $user->login : 'unknown');
180
181 $outputfile = $conf->admin->dir_temp.'/export_files.'.$userlogin.'.out'; // File used with popen method
182
183 $file .= '.tar';
184
185 // We also exclude '/temp/' dir and 'documents/admin/documents'
186 // We make escapement here and call executeCLI without escapement because we don't want to have the '*.log' escaped.
187 $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))."'";
188
189 $result = $utils->executeCLI($cmd, $outputfile, 0, null, 1);
190
191 $retval = $result['error'];
192 if ($result['result'] || !empty($retval)) {
193 $langs->load("errors");
194 dol_syslog("Documents tar retval after exec=".$retval, LOG_ERR);
195 $errormsg = 'Error tar generation return '.$retval;
196 } else {
197 if ($compression == 'gz') {
198 $cmd = "gzip -f ".$outputdir."/".$file;
199 } elseif ($compression == 'bz') {
200 $cmd = "bzip2 -f ".$outputdir."/".$file;
201 } elseif ($compression == 'zstd') {
202 $cmd = "zstd -z -9 -q --rm ".$outputdir."/".$file;
203 }
204
205 $result = $utils->executeCLI($cmd, $outputfile);
206
207 $retval = $result['error'];
208 if ($result['result'] || !empty($retval)) {
209 $errormsg = 'Error '.$compression.' generation return '.$retval;
210 unlink($outputdir."/".$file);
211 }
212 }
213} else {
214 $errormsg = 'Bad value for compression method';
215 print $errormsg;
216}
217
218
219// Output export
220
221if ($export_type != 'externalmodule' || empty($what)) {
222 top_httphead();
223
224 if ($errormsg) {
225 setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
226 } else {
227 setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
228 }
229
230 $db->close();
231
232 // Redirect to calling page
233 $returnto = 'dolibarr_export.php';
234
235 header("Location: ".$returnto);
236
237 exit();
238} else {
239 top_httphead('application/zip');
240
241 $zipname = $outputdir."/".$file;
242
243 // Then download the zipped file.
244
245 header('Content-disposition: attachment; filename='.basename($zipname));
246 header('Content-Length: '.filesize($zipname));
247 readfile($zipname);
248
249 dol_delete_file($zipname);
250
251 $db->close();
252
253 exit();
254}
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.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
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_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.