dolibarr  19.0.0-dev
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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
26 // Load Dolibarr environment
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
32 
33 $langs->load("admin");
34 
35 $action = GETPOST('action', 'aZ09');
36 $what = GETPOST('what', 'alpha');
37 $export_type = GETPOST('export_type', 'alpha');
38 $file = dol_sanitizeFileName(GETPOST('filename_template', 'alpha'));
39 
40 // Load variable for pagination
41 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
42 $sortfield = GETPOST('sortfield', 'aZ09comma');
43 $sortorder = GETPOST('sortorder', 'aZ09comma');
44 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
45 if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
46  $page = 0;
47 } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
48 $offset = $limit * $page;
49 if (!$sortorder) {
50  $sortorder = "DESC";
51 }
52 if (!$sortfield) {
53  $sortfield = "date";
54 }
55 
56 if (!$user->admin) {
58 }
59 
60 $errormsg = '';
61 
62 $utils = new Utils($db);
63 
64 
65 /*
66  * Actions
67  */
68 
69 if ($file && !$what) {
70  //print DOL_URL_ROOT.'/dolibarr_export.php';
71  header("Location: ".DOL_URL_ROOT.'/admin/tools/dolibarr_export.php?msg='.urlencode($langs->trans("ErrorFieldRequired", $langs->transnoentities("ExportMethod"))).(GETPOST('page_y', 'int') ? '&page_y='.GETPOST('page_y', 'int') : ''));
72  exit;
73 }
74 
75 if ($action == 'delete') {
76  $file = $conf->admin->dir_output.'/'.dol_sanitizeFileName(GETPOST('urlfile'));
77  $ret = dol_delete_file($file, 1);
78  if ($ret) {
79  setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
80  } else {
81  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
82  }
83  $action = '';
84 }
85 
86 $_SESSION["commandbackuplastdone"] = '';
87 $_SESSION["commandbackuptorun"] = '';
88 $_SESSION["commandbackupresult"] = '';
89 
90 // Increase limit of time. Works only if we are not in safe mode
91 $ExecTimeLimit = 600; // Set it to 0 to not use a forced time limit
92 if (!empty($ExecTimeLimit)) {
93  $err = error_reporting();
94  error_reporting(0); // Disable all errors
95  //error_reporting(E_ALL);
96  @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
97  error_reporting($err);
98 }
99 $MemoryLimit = 0;
100 if (!empty($MemoryLimit)) {
101  @ini_set('memory_limit', $MemoryLimit);
102 }
103 
104 // Start with empty buffer
105 $dump_buffer = '';
106 $dump_buffer_len = 0;
107 
108 // We will send fake headers to avoid browser timeout when buffering
109 $time_start = time();
110 
111 
112 $outputdir = $conf->admin->dir_output.'/backup';
113 $result = dol_mkdir($outputdir);
114 
115 
116 $lowmemorydump = GETPOSTISSET("lowmemorydump") ? GETPOST("lowmemorydump") : getDolGlobalString('MAIN_LOW_MEMORY_DUMP');
117 
118 
119 // MYSQL
120 if ($what == 'mysql') {
121  $cmddump = GETPOST("mysqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
122  $cmddump = dol_sanitizePathName($cmddump);
123 
124  if (!empty($dolibarr_main_restrict_os_commands)) {
125  $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
126  $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
127  dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
128  $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
129  if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
130  $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
131  }
132  }
133 
134  if (!$errormsg && $cmddump) {
135  dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
136  }
137 
138  if (!$errormsg) {
139  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
140  $errormsg = $utils->error;
141  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
142  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
143  }
144 }
145 
146 // MYSQL NO BIN
147 if ($what == 'mysqlnobin') {
148  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
149 
150  $errormsg = $utils->error;
151  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
152  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
153 }
154 
155 // POSTGRESQL
156 if ($what == 'postgresql') {
157  $cmddump = GETPOST("postgresqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
158  $cmddump = dol_sanitizePathName($cmddump);
159 
160  /* Not required, the command is output on screen but not ran for pgsql
161  if (!empty($dolibarr_main_restrict_os_commands))
162  {
163  $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
164  $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
165  dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
166  $basenamecmddump = basename(str_replace('\\', '/', $cmddump));
167  if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
168  {
169  $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
170  }
171  } */
172 
173  if (!$errormsg && $cmddump) {
174  dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
175  }
176 
177  if (!$errormsg) {
178  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
179  $errormsg = $utils->error;
180  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
181  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
182  }
183 
184  $what = ''; // Clear to show message to run command
185 }
186 
187 
188 if ($errormsg) {
189  setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
190 
191  $resultstring = '';
192  $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
193 
194  $_SESSION["commandbackupresult"] = $resultstring;
195 } else {
196  if ($what) {
197  setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
198 
199  $resultstring = '<div class="ok">';
200  $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
201  $resultstring .= $langs->trans("YouCanDownloadBackupFile");
202  $resultstring .= '</div>';
203 
204  $_SESSION["commandbackupresult"] = $resultstring;
205  }
206  /*else
207  {
208  setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
209  }*/
210 }
211 
212 
213 
214 /*
215  * View
216  */
217 
218 top_httphead();
219 
220 $db->close();
221 
222 // Redirect to backup page
223 header("Location: dolibarr_export.php".(GETPOST('page_y', 'int') ? '?page_y='.GETPOST('page_y', 'int') : ''));
224 exit();
top_httphead
if(!defined('NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1494
dol_sanitizePathName
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
Definition: functions.lib.php:1349
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1323
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:609
Utils
Class to manage utility methods.
Definition: utils.class.php:30
dol_delete_file
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.
Definition: files.lib.php:1334
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
Definition: functions.lib.php:8673
getDolGlobalString
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:142
dolibarr_set_const
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).
Definition: admin.lib.php:638
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:509
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
Definition: security.lib.php:1169
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6936