Completed
Branch develop (09fbe4)
by
unknown
29:23
created

export.php ➔ backup_tables()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 160

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
nc 74886
nop 2
dl 0
loc 160
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2006-2014  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2011       Juanjo Menent       <[email protected]>
4
 * Copyright (C) 2015       Raphaël Doursenaud  <[email protected]>
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 <http://www.gnu.org/licenses/>.
18
*/
19
20
/**
21
 *		\file 		htdocs/admin/tools/export.php
22
 *		\brief      Page to export a database into a dump file
23
 */
24
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','alpha');
34
$what=GETPOST('what','alpha');
35
$export_type=GETPOST('export_type','alpha');
36
$file=GETPOST('filename_template','alpha');
37
38
$sortfield = GETPOST('sortfield','alpha');
39
$sortorder = GETPOST('sortorder','alpha');
40
$page = GETPOST("page",'int');
41
if (! $sortorder) $sortorder="DESC";
42
if (! $sortfield) $sortfield="date";
43
if ($page < 0) { $page = 0; }
44
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
45
$offset = $limit * $page;
46
47
if (! $user->admin) accessforbidden();
48
49
if ($file && ! $what)
50
{
51
    //print DOL_URL_ROOT.'/dolibarr_export.php';
52
    header("Location: ".DOL_URL_ROOT.'/admin/tools/dolibarr_export.php?msg='.urlencode($langs->trans("ErrorFieldRequired",$langs->transnoentities("ExportMethod"))));
53
    exit;
54
}
55
56
$errormsg='';
57
58
59
/*
60
 * Actions
61
 */
62
63
if ($action == 'delete')
64
{
65
	$file=$conf->admin->dir_output.'/'.GETPOST('urlfile');
66
	$ret=dol_delete_file($file, 1);
67
	if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
68
	else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
69
	$action='';
70
}
71
72
73
/*
74
 * View
75
 */
76
77
$_SESSION["commandbackuplastdone"]='';
78
$_SESSION["commandbackuptorun"]='';
79
$_SESSION["commandbackupresult"]='';
80
81
// Increase limit of time. Works only if we are not in safe mode
82
$ExecTimeLimit=600;
83
if (!empty($ExecTimeLimit))
84
{
85
    $err=error_reporting();
86
    error_reporting(0);     // Disable all errors
87
    //error_reporting(E_ALL);
88
    @set_time_limit($ExecTimeLimit);   // Need more than 240 on Windows 7/64
89
    error_reporting($err);
90
}
91
$MemoryLimit=0;
92
if (!empty($MemoryLimit))
93
{
94
    @ini_set('memory_limit', $MemoryLimit);
95
}
96
97
$form=new Form($db);
98
$formfile = new FormFile($db);
99
100
//$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
101
//llxHeader('','',$help_url);
102
103
//print load_fiche_titre($langs->trans("Backup"),'','title_setup');
104
105
106
// Start with empty buffer
107
$dump_buffer = '';
108
$dump_buffer_len = 0;
109
110
// We will send fake headers to avoid browser timeout when buffering
111
$time_start = time();
112
113
114
$outputdir  = $conf->admin->dir_output.'/backup';
115
$result=dol_mkdir($outputdir);
116
117
118
$utils = new Utils($db);
119
120
121
// MYSQL
122
if ($what == 'mysql')
123
{
124
125
    $cmddump=GETPOST("mysqldump");	// 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
        $ok=0;
132
        dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that on of this command is inside ".$cmddump);
133
        foreach($arrayofallowedcommand as $allowedcommand)
134
        {
135
            if (preg_match('/'.preg_quote($allowedcommand,'/').'/', $cmddump))
136
            {
137
                $ok=1;
138
                break;
139
            }
140
        }
141
        if (! $ok)
142
        {
143
            $errormsg=$langs->trans('CommandIsNotInsideAllowedCommands');
144
        }
145
    }
146
147
    if (! $errormsg && $cmddump)
148
    {
149
        dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
150
    }
151
152
    if (! $errormsg)
153
    {
154
        $utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
155
        $errormsg=$utils->error;
156
        $_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
157
        $_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
158
    }
159
}
160
161
// MYSQL NO BIN
162
if ($what == 'mysqlnobin')
163
{
164
    $utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
165
166
    $errormsg=$utils->error;
167
    $_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
168
    $_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
169
}
170
171
// POSTGRESQL
172
if ($what == 'postgresql')
173
{
174
    $cmddump=GETPOST("postgresqldump");	// Do not sanitize here with 'alpha', will be sanitize later by dol_sanitizePathName and escapeshellarg
175
    $cmddump=dol_sanitizePathName($cmddump);
176
177
    if (! $errormsg && $cmddump)
178
    {
179
        dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
180
    }
181
182
    if (! $errormsg)
183
    {
184
        $utils->dumpDatabase(GETPOST('compression','alpha'), $what, 0, $file);
185
        $errormsg=$utils->error;
186
        $_SESSION["commandbackuplastdone"]=$utils->result['commandbackuplastdone'];
187
        $_SESSION["commandbackuptorun"]=$utils->result['commandbackuptorun'];
188
    }
189
190
    $what='';   // Clear to show message to run command
191
}
192
193
194
195
if ($errormsg)
196
{
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
}
204
else
205
{
206
	if ($what)
207
	{
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
$filearray=dol_dir_list($conf->admin->dir_output.'/backup','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);
226
$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"));
227
228
print '<br>';
229
*/
230
231
// Redirect t backup page
232
header("Location: dolibarr_export.php");
233
234
$time_end = time();
235
236
$db->close();
237
238