dolibarr  13.0.2
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  *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19 
25 require '../../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30 
31 $langs->load("admin");
32 
33 $action = GETPOST('action', 'aZ09');
34 $what = GETPOST('what', 'alpha');
35 $export_type = GETPOST('export_type', 'alpha');
36 $file = GETPOST('filename_template', 'alpha');
37 
38 // Load variable for pagination
39 $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
40 $sortfield = GETPOST('sortfield', 'aZ09comma');
41 $sortorder = GETPOST('sortorder', 'aZ09comma');
42 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
43 if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) { $page = 0; } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action
44 $offset = $limit * $page;
45 if (!$sortorder) $sortorder = "DESC";
46 if (!$sortfield) $sortfield = "date";
47 
48 if (!$user->admin) accessforbidden();
49 
50 if ($file && !$what)
51 {
52  //print DOL_URL_ROOT.'/dolibarr_export.php';
53  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') : ''));
54  exit;
55 }
56 
57 $errormsg = '';
58 
59 
60 /*
61  * Actions
62  */
63 
64 if ($action == 'delete')
65 {
66  $file = $conf->admin->dir_output.'/'.GETPOST('urlfile');
67  $ret = dol_delete_file($file, 1);
68  if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
69  else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
70  $action = '';
71 }
72 
73 
74 /*
75  * View
76  */
77 
78 $_SESSION["commandbackuplastdone"] = '';
79 $_SESSION["commandbackuptorun"] = '';
80 $_SESSION["commandbackupresult"] = '';
81 
82 // Increase limit of time. Works only if we are not in safe mode
83 $ExecTimeLimit = 600;
84 if (!empty($ExecTimeLimit))
85 {
86  $err = error_reporting();
87  error_reporting(0); // Disable all errors
88  //error_reporting(E_ALL);
89  @set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
90  error_reporting($err);
91 }
92 $MemoryLimit = 0;
93 if (!empty($MemoryLimit))
94 {
95  @ini_set('memory_limit', $MemoryLimit);
96 }
97 
98 $form = new Form($db);
99 $formfile = new FormFile($db);
100 
101 //$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
102 //llxHeader('','',$help_url);
103 
104 //print load_fiche_titre($langs->trans("Backup"),'','title_setup');
105 
106 
107 // Start with empty buffer
108 $dump_buffer = '';
109 $dump_buffer_len = 0;
110 
111 // We will send fake headers to avoid browser timeout when buffering
112 $time_start = time();
113 
114 
115 $outputdir = $conf->admin->dir_output.'/backup';
116 $result = dol_mkdir($outputdir);
117 
118 
119 $utils = new Utils($db);
120 
121 
122 // MYSQL
123 if ($what == 'mysql')
124 {
125  $cmddump = GETPOST("mysqldump", 'none'); // Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
126  $cmddump = dol_sanitizePathName($cmddump);
127 
128  if (!empty($dolibarr_main_restrict_os_commands))
129  {
130  $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
131  dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$cmddump);
132  $basenamecmddump = basename($cmddump);
133  if (!in_array($basenamecmddump, $arrayofallowedcommand)) // the provided command $cmddump must be an allowed command
134  {
135  $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
136  }
137  }
138 
139  if (!$errormsg && $cmddump)
140  {
141  dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
142  }
143 
144  if (!$errormsg)
145  {
146  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file);
147  $errormsg = $utils->error;
148  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
149  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
150  }
151 }
152 
153 // MYSQL NO BIN
154 if ($what == 'mysqlnobin')
155 {
156  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file);
157 
158  $errormsg = $utils->error;
159  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
160  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
161 }
162 
163 // POSTGRESQL
164 if ($what == 'postgresql')
165 {
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  {
183  dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump, 'chaine', 0, '', $conf->entity);
184  }
185 
186  if (!$errormsg)
187  {
188  $utils->dumpDatabase(GETPOST('compression', 'alpha'), $what, 0, $file);
189  $errormsg = $utils->error;
190  $_SESSION["commandbackuplastdone"] = $utils->result['commandbackuplastdone'];
191  $_SESSION["commandbackuptorun"] = $utils->result['commandbackuptorun'];
192  }
193 
194  $what = ''; // Clear to show message to run command
195 }
196 
197 
198 if ($errormsg)
199 {
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  {
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 $filearray=dol_dir_list($conf->admin->dir_output.'/backup','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);
227 $result=$formfile->list_of_documents($filearray,null,'systemtools','',1,'backup/',1,0,($langs->trans("NoBackupFileAvailable").'<br>'.$langs->trans("ToBuildBackupFileClickHere",DOL_URL_ROOT.'/admin/tools/dolibarr_export.php')),0,$langs->trans("PreviousDumpFiles"));
228 
229 print '<br>';
230 */
231 
232 // Redirect to backup page
233 header("Location: dolibarr_export.php".(GETPOST('page_y', 'int') ? '?page_y='.GETPOST('page_y', 'int') : ''));
234 
235 $time_end = time();
236 
237 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage utility methods.
Definition: utils.class.php:28
dol_sanitizePathName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a path name.
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:575
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1144
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 ...
Class to offer components to list and upload files.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)