dolibarr 24.0.0-beta
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';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
44
45$langs->load("admin");
46
47$action = GETPOST('action', 'aZ09');
48$what = GETPOST('what', 'alpha');
49$export_type = GETPOST('export_type', 'alpha');
50$file = dol_sanitizeFileName(GETPOST('filename_template', 'alpha'));
51
52// Load variable for pagination
53$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
54$sortfield = GETPOST('sortfield', 'aZ09comma');
55$sortorder = GETPOST('sortorder', 'aZ09comma');
56$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
57if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
58 $page = 0;
59} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
60$offset = $limit * $page;
61if (!$sortorder) {
62 $sortorder = "DESC";
63}
64if (!$sortfield) {
65 $sortfield = "date";
66}
67
68if (!$user->admin) {
70}
71
72$errormsg = '';
73
74$utils = new Utils($db);
75
76
77/*
78 * Actions
79 */
80
81if ($file && !$what) {
82 //print DOL_URL_ROOT.'/dolibarr_export.php';
83 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') : ''));
84 exit;
85}
86
87if ($action == 'delete') {
88 $file = $conf->admin->dir_output.'/'.dol_sanitizeFileName(GETPOST('urlfile'));
89 $ret = dol_delete_file($file, 1);
90 if ($ret) {
91 setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
92 } else {
93 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
94 }
95 $action = '';
96}
97
98$_SESSION["commandbackuplastdone"] = '';
99$_SESSION["commandbackuptorun"] = '';
100$_SESSION["commandbackupresult"] = '';
101
102// Increase limit of time. Works only if we are not in safe mode
103$ExecTimeLimit = 600; // Set it to 0 to not use a forced time limit
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$MemoryLimit = 0;
112if (!empty($MemoryLimit)) {
113 @ini_set('memory_limit', $MemoryLimit);
114}
115
116// We will send fake headers to avoid browser timeout when buffering
117$time_start = time();
118
119
120$outputdir = $conf->admin->dir_output.'/backup';
121$result = dol_mkdir($outputdir);
122
123
124$lowmemorydump = (int) (GETPOSTISSET("lowmemorydump") ? GETPOSTINT("lowmemorydump") : getDolGlobalInt('MAIN_LOW_MEMORY_DUMP'));
125
126
127// MYSQL
128if ($what == 'mysql') {
129 $cmddump = GETPOST("mysqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
130 $cmddump = dol_sanitizePathName($cmddump);
131 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
132
133 // Add a fallback when we detect something wrong with the path of the dump command
134 if (preg_match('/\//', str_replace('\\', '/', $cmddump))) { // If command is a full path
135 if (!dol_is_file($cmddump)) { // And if file not reachable with its full path
136 $reg = array();
137 if (preg_match('/mysqldump(\.exe)?$/', $cmddump, $reg)) { // And if command ends with mysqldump
138 $cmddump = 'mysqldump'.(empty($reg[1]) ? '' : $reg[1]); // Then we try the command with no forced path
139 }
140 }
141 }
142
143 if (!empty($dolibarr_main_restrict_os_commands)) {
144 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
145 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
146 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
147 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
148 $langs->load("errors");
149 $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
150 $errormsg .= '<br>'.$langs->trans('ErrorCheckTheCommandInsideTheAdvancedOptions');
151 }
152 }
153
154 if (!$errormsg && $cmddump) {
155 dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', 0);
156 }
157
158 if (!$errormsg) {
159 $result = $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
160
161 $errormsg = $utils->error;
162 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
163 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
164 }
165}
166
167// MYSQL NO BIN
168if ($what == 'mysqlnobin') {
169 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
170
171 $errormsg = $utils->error;
172 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
173 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
174}
175
176// POSTGRESQL
177if ($what == 'postgresql') {
178 $cmddump = GETPOST("postgresqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
179 $cmddump = dol_sanitizePathName($cmddump);
180
181 /* Not required, the command is output on screen but not ran for pgsql
182 if (!empty($dolibarr_main_restrict_os_commands))
183 {
184 $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
185 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
186 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
187 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
188 if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
189 {
190 $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
191 }
192 } */
193
194 if (!$errormsg && $cmddump) {
195 dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', 0);
196 }
197
198 if (!$errormsg) {
199 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
200 $errormsg = $utils->error;
201 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
202 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
203 }
204
205 $what = ''; // Clear to show message to run command
206}
207
208
209if ($errormsg) {
210 setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
211
212 $resultstring = '';
213 $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
214
215 $_SESSION["commandbackupresult"] = $resultstring;
216} else {
217 if ($what) {
218 setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
219
220 $resultstring = '<div class="ok">';
221 $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
222 $resultstring .= $langs->trans("YouCanDownloadBackupFile");
223 $resultstring .= '</div>';
224
225 $_SESSION["commandbackupresult"] = $resultstring;
226 }
227 /*else
228 {
229 setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
230 }*/
231}
232
233
234
235/*
236 * View
237 */
238
240
241$db->close();
242
243// Redirect to backup page
244header("Location: dolibarr_export.php".(GETPOSTINT('page_y') ? '?page_y='.GETPOSTINT('page_y') : ''));
245exit();
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.
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.
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.
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.
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.