dolibarr 25.0.0-alpha
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-2026 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); // Sanitize path
131 $cmddump = dol_string_nospecial($cmddump, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
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 $langs->load("errors");
141 setEventMessage($langs->trans("WarningFullPathWasNotFoundSoWeUseAnAlternativeValue", $cmddump), 'warnings');
142 }
143 }
144 }
145
146 if ($cmddump) {
147 dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', 0);
148 }
149
150 $result = $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
151
152 $errormsg = $utils->error;
153 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
154 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
155}
156
157// MYSQL NO BIN
158if ($what == 'mysqlnobin') {
159 $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// POSTGRESQL
167if ($what == 'postgresql') {
168 $cmddump = GETPOST("postgresqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
169 $cmddump = dol_sanitizePathName($cmddump);
170 $cmddump = dol_string_nospecial($cmddump, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
171
172 /* Not required, the command is output on screen but not ran for pgsql
173 if (!empty($dolibarr_main_restrict_os_commands))
174 {
175 $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
176 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
177 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
178 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
179 if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
180 {
181 $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
182 }
183 } */
184
185 if (!$errormsg && $cmddump) {
186 dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', 0);
187 }
188
189 if (!$errormsg) {
190 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
191 $errormsg = $utils->error;
192 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
193 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
194 }
195
196 $what = ''; // Clear to show message to run command
197}
198
199
200if ($errormsg) {
201 setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
202
203 $resultstring = '';
204 $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
205
206 $_SESSION["commandbackupresult"] = $resultstring;
207} else {
208 if ($what) {
209 setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
210
211 $resultstring = '<div class="ok">';
212 $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
213 $resultstring .= $langs->trans("YouCanDownloadBackupFile");
214 $resultstring .= '</div>';
215
216 $_SESSION["commandbackupresult"] = $resultstring;
217 }
218 /*else
219 {
220 setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
221 }*/
222}
223
224
225
226/*
227 * View
228 */
229
231
232$db->close();
233
234// Redirect to backup page
235header("Location: dolibarr_export.php".(GETPOSTINT('page_y') ? '?page_y='.GETPOSTINT('page_y') : ''));
236exit();
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.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
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.
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.