dolibarr  16.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  *
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 require '../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
31 
32 $langs->load("admin");
33 
34 $action = GETPOST('action', 'aZ09');
35 $what = GETPOST('what', 'alpha');
36 $export_type = GETPOST('export_type', 'alpha');
37 $file = GETPOST('filename_template', 'alpha');
38 
39 // Load variable for pagination
40 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
41 $sortfield = GETPOST('sortfield', 'aZ09comma');
42 $sortorder = GETPOST('sortorder', 'aZ09comma');
43 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
44 if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) {
45  $page = 0;
46 } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
47 $offset = $limit * $page;
48 if (!$sortorder) {
49  $sortorder = "DESC";
50 }
51 if (!$sortfield) {
52  $sortfield = "date";
53 }
54 
55 if (!$user->admin) {
57 }
58 
59 if ($file && !$what) {
60  //print DOL_URL_ROOT.'/dolibarr_export.php';
61  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') : ''));
62  exit;
63 }
64 
65 $errormsg = '';
66 
67 
68 /*
69  * Actions
70  */
71 
72 if ($action == 'delete') {
73  $file = $conf->admin->dir_output.'/'.GETPOST('urlfile');
74  $ret = dol_delete_file($file, 1);
75  if ($ret) {
76  setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
77  } else {
78  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
79  }
80  $action = '';
81 }
82 
83 
84 /*
85  * View
86  */
87 
88 $_SESSION["commandbackuplastdone"] = '';
89 $_SESSION["commandbackuptorun"] = '';
90 $_SESSION["commandbackupresult"] = '';
91 
92 // Increase limit of time. Works only if we are not in safe mode
93 $ExecTimeLimit = 600; // Set it to 0 to not use a forced time limit
94 if (!empty($ExecTimeLimit)) {
95  $err = error_reporting();
96  error_reporting(0); // Disable all errors
97  //error_reporting(E_ALL);
98  @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
99  error_reporting($err);
100 }
101 $MemoryLimit = 0;
102 if (!empty($MemoryLimit)) {
103  @ini_set('memory_limit', $MemoryLimit);
104 }
105 
106 
107 //$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
108 //llxHeader('','',$help_url);
109 
110 //print load_fiche_titre($langs->trans("Backup"),'','title_setup');
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 $utils = new Utils($db);
126 
127 $lowmemorydump = GETPOSTISSET("lowmemorydump") ? GETPOST("lowmemorydump") : getDolGlobalString('MAIN_LOW_MEMORY_DUMP');
128 
129 // MYSQL
130 if ($what == 'mysql') {
131  $cmddump = GETPOST("mysqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
132  $cmddump = dol_sanitizePathName($cmddump);
133 
134  if (!empty($dolibarr_main_restrict_os_commands)) {
135  $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
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($cmddump);
138  if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
139  $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
140  }
141  }
142 
143  if (!$errormsg && $cmddump) {
144  dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
145  }
146 
147  if (!$errormsg) {
148  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
149  $errormsg = $utils->error;
150  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
151  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
152  }
153 }
154 
155 // MYSQL NO BIN
156 if ($what == 'mysqlnobin') {
157  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
158 
159  $errormsg = $utils->error;
160  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
161  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
162 }
163 
164 // POSTGRESQL
165 if ($what == 'postgresql') {
166  $cmddump = GETPOST("postgresqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
167  $cmddump = dol_sanitizePathName($cmddump);
168 
169  /* Not required, the command is output on screen but not ran for pgsql
170  if (! empty($dolibarr_main_restrict_os_commands))
171  {
172  $arrayofallowedcommand=explode(',', $dolibarr_main_restrict_os_commands);
173  dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
174  $basenamecmddump=basename($cmddump);
175  if (! in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
176  {
177  $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
178  }
179  } */
180 
181  if (!$errormsg && $cmddump) {
182  dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
183  }
184 
185  if (!$errormsg) {
186  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file, 0, 0, $lowmemorydump);
187  $errormsg = $utils->error;
188  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
189  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
190  }
191 
192  $what = ''; // Clear to show message to run command
193 }
194 
195 
196 if ($errormsg) {
197  setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
198 
199  $resultstring = '';
200  $resultstring .= '<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
201 
202  $_SESSION["commandbackupresult"] = $resultstring;
203 } else {
204  if ($what) {
205  setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
206 
207  $resultstring = '<div class="ok">';
208  $resultstring .= $langs->trans("BackupFileSuccessfullyCreated").'.<br>';
209  $resultstring .= $langs->trans("YouCanDownloadBackupFile");
210  $resultstring .= '<div>';
211 
212  $_SESSION["commandbackupresult"] = $resultstring;
213  }
214  /*else
215  {
216  setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'warnings');
217  }*/
218 }
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') : ''));
dol_sanitizePathName
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
Definition: functions.lib.php:1251
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:484
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:1231
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
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:627
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:386
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6603