-
Notifications
You must be signed in to change notification settings - Fork 39
/
database.php
343 lines (294 loc) · 10.8 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
// Purpose do all the tasks concerning databases
// Author Lutz Brueckner <[email protected]>
// Copyright (c) 2000-2006 by Lutz Brueckner,
// published under the terms of the GNU General Public Licence v.2,
// see file LICENCE for details
require './inc/script_start.inc.php';
//
// script is called from login form
//
if (isset($_POST['db_login_doit'])) {
// close existing connection, if any
if (!empty($dbhandle)) {
fbird_close($dbhandle);
}
$s_login['database'] = $_POST['db_login_database'];
$s_login['user'] = strtoupper($_POST['db_login_user']);
$s_login['host'] = $_POST['db_login_host'];
// don't set s_login['password'] if it contains only stars
// (from function password_stars())
$pw = $_POST['db_login_password'];
if (strspn($pw, '*') != strlen($pw)) {
$s_login['password'] = $pw;
}
$s_login['role'] = !empty($_POST['db_login_role']) ? $_POST['db_login_role'] : '';
$s_login['cache'] = !empty($_POST['db_login_cache']) ? $_POST['db_login_cache'] : '';
if ($s_login['cache'] != null && $s_login['cache'] < 10) {
$s_login['cache'] = 10;
}
$s_login['charset'] = !empty($_POST['db_login_charset']) ? $_POST['db_login_charset'] : '';
$s_login['dialect'] = !empty($_POST['db_login_dialect']) ? $_POST['db_login_dialect'] : '';
$s_login['server'] = !empty($_POST['db_login_server']) ? $_POST['db_login_server'] : '';
if ($s_login['database'] == '') {
$error = $ERRORS['NO_DB_SELECTED'];
} elseif (!is_allowed_db($s_login['database'])) {
$error = sprintf($ERRORS['DB_NOT_ALLOWED'], $s_login['database']);
}
if (empty($error)) {
if ($dbhandle = db_connect()) {
// connected successfully
$s_connected = true;
remove_edit_panels();
$s_charsets = get_charsets();
$message = $info_strings['SuccessLogin'];
} else {
// connect failed
$fb_error = fbird_errmsg();
$s_login['password'] = '';
$s_connected = false;
}
}
cleanup_session();
}
//
// the Logout-Button from the login-panel
//
if (isset($_POST['db_logout_doit'])) {
if (!empty($dbhandle)) {
fbird_close($dbhandle);
}
remove_edit_panels();
cleanup_session();
$s_login['password'] = '';
$s_connected = false;
}
//
// script is called from create database form
//
if (have_panel_permissions($s_login['user'], 'db_create')
&& isset($_POST['db_create_doit'])
&& isset($_POST['db_create_database'])
&& !empty($_POST['db_create_database'])) {
$s_create_db = trim($_POST['db_create_database']);
$s_create_host = trim($_POST['db_create_host']);
$s_create_user = trim($_POST['db_create_user']);
// dont set $s_create_pw if it contains only stars
// (from function password_stars() )
$pw = $_POST['db_create_password'];
if (strspn($pw, '*') != strlen($pw)) {
$s_create_pw = $pw;
}
$s_create_pagesize = $_POST['db_create_pagesize'];
$s_create_charset = $_POST['db_create_charset'];
if (!is_allowed_db($s_create_db)) {
$error = sprintf($ERRORS['DB_NOT_ALLOWED'], $s_create_db);
}
// close existing connection, if any
if (!empty($dbhandle) && empty($error)) {
fbird_close($dbhandle);
}
// build a sql statement from the values
// we received from the db_create_form
if (empty($error)) {
$db_str = (!empty($s_create_host)) ? $s_create_host.':'.$s_create_db : $s_create_db;
$sql = 'CREATE DATABASE \''.$db_str.'\'';
if (strlen($s_create_user) > 0) {
$sql .= ' USER \''.$s_create_user.'\'';
if (strlen($s_create_pw) > 0) {
$sql .= ' PASSWORD \''.$s_create_pw.'\'';
}
}
if ($s_create_pagesize != 4096) {
// 4096 is the default page size
$sql .= ' PAGE_SIZE = '.$s_create_pagesize;
}
if ($s_create_charset != 'NONE') {
// NONE is the default character set
$sql .= ' DEFAULT CHARACTER SET '.$s_create_charset;
}
$sql .= ';';
list($binary_output, $binary_error) = isql_execute($sql);
if (empty($binary_error) && !is_file($s_create_db)) {
$error = sprintf($ERRORS['CREATE_DB_FAILED'], $s_create_db);
} else { // connect the new created database
$s_login['charset'] = $s_create_charset;
$s_login['database'] = $s_create_db;
$s_login['host'] = $s_create_host;
$s_login['user'] = strtoupper($s_create_user);
$s_login['password'] = $s_create_pw;
if ($dbhandle = db_connect()) {
$s_connected = true;
remove_edit_panels();
cleanup_session();
$message = sprintf($MESSAGES['CREATE_DB_SUCCESS'], $s_create_db);
} else {
$fb_error = fbird_errmsg();
}
}
}
}
//
// script is called from delete database form
//
if (have_panel_permissions($s_login['user'], 'db_delete')
&& isset($_POST['db_delete_doit'])
&& isset($_POST['db_delete_database'])
&& !empty($_POST['db_delete_database'])) {
$pw = get_request_data('db_delete_password');
if (strspn($pw, '*') == strlen($pw)) {
$pw = $s_delete_db['password'];
}
$s_delete_db = array('database' => get_request_data('db_delete_database'),
'user' => get_request_data('db_delete_user'),
'host' => get_request_data('db_delete_host'),
'password' => $pw,
);
// cannot delete the current database
if ($s_login['database'] == $s_delete_db) {
$message = sprintf($MESSAGES['DELETE_CON_DB'], $s_delete_db);
$s_login['database'] = '';
$s_connected = false;
remove_edit_panels();
cleanup_session();
}
if (!is_allowed_db($s_delete_db['database'])) {
$error = sprintf($ERRORS['DB_NOT_ALLOWED'], $s_delete_db['database']);
} elseif ($s_cust['askdel'] == true) {
$s_confirmations['database'] =
array('msg' => sprintf($MESSAGES['CONFIRM_DB_DELETE'], $s_delete_db['database']));
}
}
// deleting a database is confirmed
if (isset($_POST['confirm_yes']) ||
(isset($_POST['db_delete_doit']) && $s_cust['askdel'] == false && empty($error))) {
$fb_error = drop_database($s_delete_db, $s_login);
unset($s_confirmations['database']);
if (empty($fb_error)) {
if ($s_login['database'] == $s_delete_db['database']) {
$s_login['database'] = '';
$s_connected = false;
remove_edit_panels();
cleanup_session();
}
$s_delete_db['database'] = '';
}
}
// deleting a database is canceled
if (isset($_POST['confirm_no'])) {
unset($s_confirmations['database']);
$s_delete_db['database'] = '';
}
//
// get the data for the metadata panel
//
if (have_panel_permissions($s_login['user'], 'db_meta', true)) {
// remove pending metadata-jobs from session
$s_iframejobs = array_filter($s_iframejobs, function($a) {return '$a["job"]!="metadata";';} );
$iframekey_meta = md5(uniqid('meta'));
$s_iframejobs[$iframekey_meta] = array('job' => 'metadata',
'timestamp' => time(), );
//
// Save to File on the Metadata panel was selected
//
if (isset($_POST['db_meta_save'])) {
list($metadata, $binary_error) = isql_get_metadata($s_login['user'], $s_login['password'], $s_login['database'], $s_login['host']);
if (empty($binary_error) && count($metadata) > 0) {
send_export_headers('application/octet-stream', 'meta.sql');
print(implode("\n", $metadata));
exit();
}
}
}
//
// something happened on the System Tables panel
//
if (isset($_POST['db_systable_select'])) {
if ($s_systable['table'] != $_POST['db_systable']) {
$s_systable['order'] = '';
$s_systable['dir'] = 'ASC';
}
$s_systable['table'] = $_POST['db_systable'];
$s_systable['ffield'] = $_POST['db_sysfield'];
$s_systable['fvalue'] = $_POST['db_sysvalue'];
$s_systable['sysdata'] = (isset($_POST['db_sysdata']) ? true : false);
}
if (isset($_GET['order'])) {
$s_systable['dir'] = ($_GET['order'] == $s_systable['order'] && $s_systable['dir'] == 'ASC')
? 'DESC'
: 'ASC';
$s_systable['order'] = $_GET['order'];
}
// determine the informations for the selected system table
if (have_panel_permissions($s_login['user'], 'db_systable', true)) {
$js_stack .= js_request_filter_fields();
if (false && strpos($s_systable['table'], 'MON$') === 0) {
// DISABLED !
$have_refresh = true;
// TODO: replace with XMLHttpRequest and markableTable()
$js_stack .= js_jsrs_refresh_systable();
} else {
$have_refresh = false;
}
if ($s_connected && !empty($s_systable['table'])) {
include './inc/system_table.inc.php';
$systable = get_systable($s_systable);
}
}
//
// determine the accessible databases for the login panel
//
$dbfiles = array();
if (isset($ALLOWED_FILES) && count($ALLOWED_FILES) > 0) {
foreach ($ALLOWED_FILES as $file) {
if ((strpos($file, '/') === false && strpos($file, '\\') === false) ||
is_file($file)) {
$dbfiles[] = $file;
}
}
} elseif (isset($ALLOWED_DIRS) && count($ALLOWED_DIRS) > 0) {
foreach ($ALLOWED_DIRS as $dir) {
if (!@is_readable($dir)) {
$warning .= sprintf($WARNINGS['CAN_NOT_ACCESS_DIR'], $dir);
} else {
$dirhandle = opendir($dir);
while ($filename = readdir($dirhandle)) {
$dbfiles[] = $dir.$filename;
}
closedir($dirhandle);
}
}
}
sort($dbfiles);
//
// print out all the panels
//
$s_page = 'Database';
$panels = $s_database_panels;
require './inc/script_end.inc.php';
//
// drop the database specified in the 'delete database' panel;
// return an empty string on success and an error message on failure
//
function drop_database($db, $login)
{
$success = '';
// make a connection to the selected database
// or use the global dbhandle if it is the one firebirdwebadmin is currently connected to
if ($db['database'] == $login['database'] &&
$db['host'] == $login['host']) {
$dbh = $GLOBALS['dbhandle'];
} else {
$db_path = ($db['host'] == '') ? $db['database'] : $db['host'].':'.$db['database'];
if (($dbh = fbird_connect($db_path, $db['user'], $db['password'])) == false) {
$success = fbird_errmsg();
}
}
// drop it if we got a handle
if (is_resource($dbh) &&
fbird_drop_db($dbh) == false) {
$success = fbird_errmsg();
fbird_close($dbh);
}
return $success;
}