dolibarr 21.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 *
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
27// Load Dolibarr environment
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
33
42$langs->load("admin");
43
44$action = GETPOST('action', 'aZ09');
45$what = GETPOST('what', 'alpha');
46$export_type = GETPOST('export_type', 'alpha');
47$file = dol_sanitizeFileName(GETPOST('filename_template', 'alpha'));
48
49// Load variable for pagination
50$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
51$sortfield = GETPOST('sortfield', 'aZ09comma');
52$sortorder = GETPOST('sortorder', 'aZ09comma');
53$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
54if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
55 $page = 0;
56} // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
57$offset = $limit * $page;
58if (!$sortorder) {
59 $sortorder = "DESC";
60}
61if (!$sortfield) {
62 $sortfield = "date";
63}
64
65if (!$user->admin) {
67}
68
69$errormsg = '';
70
71$utils = new Utils($db);
72
73
74/*
75 * Actions
76 */
77
78if ($file && !$what) {
79 //print DOL_URL_ROOT.'/dolibarr_export.php';
80 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') : ''));
81 exit;
82}
83
84if ($action == 'delete') {
85 $file = $conf->admin->dir_output.'/'.dol_sanitizeFileName(GETPOST('urlfile'));
86 $ret = dol_delete_file($file, 1);
87 if ($ret) {
88 setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
89 } else {
90 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
91 }
92 $action = '';
93}
94
95$_SESSION["commandbackuplastdone"] = '';
96$_SESSION["commandbackuptorun"] = '';
97$_SESSION["commandbackupresult"] = '';
98
99// Increase limit of time. Works only if we are not in safe mode
100$ExecTimeLimit = 600; // Set it to 0 to not use a forced time limit
101if (!empty($ExecTimeLimit)) {
102 $err = error_reporting();
103 error_reporting(0); // Disable all errors
104 //error_reporting(E_ALL);
105 @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
106 error_reporting($err);
107}
108$MemoryLimit = 0;
109if (!empty($MemoryLimit)) {
110 @ini_set('memory_limit', $MemoryLimit);
111}
112
113// Start with empty buffer
114$dump_buffer = '';
115$dump_buffer_len = 0;
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 = GETPOSTISSET("lowmemorydump") ? GETPOST("lowmemorydump") : getDolGlobalString('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
133 if (!empty($dolibarr_main_restrict_os_commands)) {
134 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
135 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
136 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
137 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
138 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
139 $langs->load("errors");
140 $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
141 $errormsg .= '<br>'.$langs->trans('ErrorCheckTheCommandInsideTheAdvancedOptions');
142 }
143 }
144
145 if (!$errormsg && $cmddump) {
146 dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
147 }
148
149 if (!$errormsg) {
150 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
151 $errormsg = $utils->error;
152 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
153 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
154 }
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
171 /* Not required, the command is output on screen but not ran for pgsql
172 if (!empty($dolibarr_main_restrict_os_commands))
173 {
174 $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
175 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
176 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
177 $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
178 if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
179 {
180 $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
181 }
182 } */
183
184 if (!$errormsg && $cmddump) {
185 dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
186 }
187
188 if (!$errormsg) {
189 $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
190 $errormsg = $utils->error;
191 $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
192 $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
193 }
194
195 $what = ''; // Clear to show message to run command
196}
197
198
199if ($errormsg) {
200 setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
201
202 $resultstring = '';
203 $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
204
205 $_SESSION["commandbackupresult"] = $resultstring;
206} else {
207 if ($what) {
208 setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
209
210 $resultstring = '<div class="ok">';
211 $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
212 $resultstring .= $langs->trans("YouCanDownloadBackupFile");
213 $resultstring .= '</div>';
214
215 $_SESSION["commandbackupresult"] = $resultstring;
216 }
217 /*else
218 {
219 setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
220 }*/
221}
222
223
224
225/*
226 * View
227 */
228
230
231$db->close();
232
233// Redirect to backup page
234header("Location: dolibarr_export.php".(GETPOSTINT('page_y') ? '?page_y='.GETPOSTINT('page_y') : ''));
235exit();
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.
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.