dolibarr 22.0.5
export.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 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2025 Anthony Berton <anthony.berton@bb2a.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29// Load Dolibarr environment
30require '../../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
35
46$langs->load("admin");
47
48$action = GETPOST('action', 'aZ09');
49$what = GETPOST('what', 'alpha');
50$export_type = GETPOST('export_type', 'alpha');
51$file = dol_sanitizeFileName(GETPOST('filename_template', 'alpha'));
52
53// Load variable for pagination
54$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
55$sortfield = GETPOST('sortfield', 'aZ09comma');
56$sortorder = GETPOST('sortorder', 'aZ09comma');
57$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
58if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
59 $page = 0;
60} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
61$offset = $limit * $page;
62if (!$sortorder) {
63 $sortorder = "DESC";
64}
65if (!$sortfield) {
66 $sortfield = "date";
67}
68
69if (!$user->admin) {
71}
72
73$errormsg = '';
74
75$utils = new Utils($db);
76
77
78/*
79 * Actions
80 */
81
82if ($file && !$what) {
83 //print DOL_URL_ROOT.'/dolibarr_export.php';
84 header("Location: ".DOL_URL_ROOT.'/admin/tools/dolibarr_export.php?msg='.urlencode($langs->trans("ErrorFieldRequired", $langs->transnoentities("ExportMethod"))).(GETPOSTINT('page_y') ? '&page_y='.GETPOSTINT('page_y') : ''));
85 exit;
86}
87
88if ($action == 'delete') {
89 $file = $conf->admin->dir_output.'/'.dol_sanitizeFileName(GETPOST('urlfile'));
90 $ret = dol_delete_file($file, 1);
91 if ($ret) {
92 setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
93 } else {
94 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
95 }
96 $action = '';
97}
98
99$_SESSION["commandbackuplastdone"] = '';
100$_SESSION["commandbackuptorun"] = '';
101$_SESSION["commandbackupresult"] = '';
102
103// Increase limit of time. Works only if we are not in safe mode
104$ExecTimeLimit = 600; // Set it to 0 to not use a forced time limit
105if (!empty($ExecTimeLimit)) {
106 $err = error_reporting();
107 error_reporting(0); // Disable all errors
108 //error_reporting(E_ALL);
109 @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
110 error_reporting($err);
111}
112$MemoryLimit = 0;
113if (!empty($MemoryLimit)) {
114 @ini_set('memory_limit', $MemoryLimit);
115}
116
117// We will send fake headers to avoid browser timeout when buffering
118$time_start = time();
119
120
121$outputdir = $conf->admin->dir_output.'/backup';
122$result = dol_mkdir($outputdir);
123
124
125$lowmemorydump = (int) (GETPOSTISSET("lowmemorydump") ? GETPOSTINT("lowmemorydump") : getDolGlobalInt('MAIN_LOW_MEMORY_DUMP'));
126
127
128// MYSQL
129if ($what == 'mysql') {
130 $cmddump = GETPOST("mysqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
131 $cmddump = dol_sanitizePathName($cmddump);
132 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
133
134 // Add a fallback when we detect something wrong with the path of the dump command
135 if (preg_match('/\//', str_replace('\\', '/', $cmddump))) { // If command is a full path
136 if (!dol_is_file($cmddump)) { // And if file not reachable with its full path
137 $reg = array();
138 if (preg_match('/mysqldump(\.exe)?$/', $cmddump, $reg)) { // And if command ends with mysqldump
139 $cmddump = 'mysqldump'.(empty($reg[1]) ? '' : $reg[1]); // Then we try the command with no forced path
140 }
141 }
142 }
143
144 if (!empty($dolibarr_main_restrict_os_commands)) {
145 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
146 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
147 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
148 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
149 $langs->load("errors");
150 $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
151 $errormsg .= '<br>'.$langs->trans('ErrorCheckTheCommandInsideTheAdvancedOptions');
152 }
153 }
154
155 if (!$errormsg && $cmddump) {
156 dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', 0);
157 }
158
159 if (!$errormsg) {
160 $result = $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
161
162 $errormsg = $utils->error;
163 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
164 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
165 }
166}
167
168// MYSQL NO BIN
169if ($what == 'mysqlnobin') {
170 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
171
172 $errormsg = $utils->error;
173 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
174 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
175}
176
177// POSTGRESQL
178if ($what == 'postgresql') {
179 $cmddump = GETPOST("postgresqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
180 $cmddump = dol_sanitizePathName($cmddump);
181
182 /* Not required, the command is output on screen but not ran for pgsql
183 if (!empty($dolibarr_main_restrict_os_commands))
184 {
185 $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
186 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
187 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
188 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
189 if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
190 {
191 $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
192 }
193 } */
194
195 if (!$errormsg && $cmddump) {
196 dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', 0);
197 }
198
199 if (!$errormsg) {
200 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
201 $errormsg = $utils->error;
202 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
203 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
204 }
205
206 $what = ''; // Clear to show message to run command
207}
208
209
210if ($errormsg) {
211 setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
212
213 $resultstring = '';
214 $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
215
216 $_SESSION["commandbackupresult"] = $resultstring;
217} else {
218 if ($what) {
219 setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
220
221 $resultstring = '<div class="ok">';
222 $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
223 $resultstring .= $langs->trans("YouCanDownloadBackupFile");
224 $resultstring .= '</div>';
225
226 $_SESSION["commandbackupresult"] = $resultstring;
227 }
228 /*else
229 {
230 setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
231 }*/
232}
233
234
235
236/*
237 * View
238 */
239
241
242$db->close();
243
244// Redirect to backup page
245header("Location: dolibarr_export.php".(GETPOSTINT('page_y') ? '?page_y='.GETPOSTINT('page_y') : ''));
246exit();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
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_is_file($pathoffile)
Return if path is a file.
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.
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
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.