Projekt

Obecné

Profil

Stáhnout (12.4 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2

    
3
	/**
4
	 * Manage databases within a server
5
	 *
6
	 * $Id: all_db.php,v 1.41 2005/10/18 03:45:15 chriskl Exp $
7
	 */
8

    
9
	// Include application functions
10
	include_once('./libraries/lib.inc.php');
11
	
12
	$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
13
	if (!isset($msg)) $msg = '';
14
	$PHP_SELF = $_SERVER['PHP_SELF'];
15

    
16
	/**
17
	 * Display a form for alter and perform actual alter
18
	 */
19
	function doAlter($confirm) {
20
		global $data, $misc, $_reload_browser;
21
		global $PHP_SELF, $lang;
22
	
23
		if ($confirm) {
24
			$misc->printTrail('database');
25
			$misc->printTitle($lang['stralter'], 'pg.database.alter');
26
			
27
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
28
			echo "<table>\n";
29
			echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
30
			echo "<td class=\"data1\">";
31
			echo "<input name=\"newname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", 
32
				htmlspecialchars($_REQUEST['alterdatabase']), "\" /></td></tr>\n";
33
			
34
			$server_info = $misc->getServerInfo();
35
				
36
			if ($data->hasAlterDatabaseOwner() && $data->isSuperUser($server_info['username'])) {
37
				// Fetch all users
38
				
39
				$rs = $data->getDatabaseOwner($_REQUEST['alterdatabase']);
40
				$owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : '';
41
				$users = $data->getUsers();
42
				
43
				echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
44
				echo "<td class=\"data1\"><select name=\"owner\">";
45
				while (!$users->EOF) {
46
					$uname = $users->f['usename'];
47
					echo "<option value=\"", htmlspecialchars($uname), "\"",
48
						($uname == $owner) ? ' selected="selected"' : '', ">", htmlspecialchars($uname), "</option>\n";
49
					$users->moveNext();
50
				}
51
				echo "</select></td></tr>\n";
52
			}
53
			echo "</table>\n";
54
			echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
55
			echo $misc->form;
56
			echo "<input type=\"hidden\" name=\"oldname\" value=\"", 
57
				htmlspecialchars($_REQUEST['alterdatabase']), "\" />\n";
58
			echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
59
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
60
			echo "</form>\n";
61
		}
62
		else {
63
			//all versions that support the alter database functionality (starting 7.4) support renaming a db
64
			$newOwner = isset($_POST['owner']) ? $_POST['owner'] : '';
65
			if ($data->AlterDatabase($_POST['oldname'], $_POST['newname'], $newOwner) == 0) {
66
				$_reload_browser = true;
67
				doDefault($lang['strdatabasealtered']);
68
			}
69
			else
70
				doDefault($lang['strdatabasealteredbad']);
71
		}
72
	}
73
	
74
	/**
75
	 * Show confirmation of drop and perform actual drop
76
	 */
77
	function doDrop($confirm) {
78
		global $data, $misc;
79
		global $PHP_SELF, $lang, $_reload_drop_database;
80

    
81
		if ($confirm) {
82
			$misc->printTrail('database');
83
			$misc->printTitle($lang['strdrop'], 'pg.database.drop');
84
			
85
			echo "<p>", sprintf($lang['strconfdropdatabase'], $misc->printVal($_REQUEST['dropdatabase'])), "</p>\n";	
86
			echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
87
			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
88
			echo $misc->form;
89
			echo "<input type=\"hidden\" name=\"dropdatabase\" value=\"", htmlspecialchars($_REQUEST['dropdatabase']), "\" />\n";
90
			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
91
			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
92
			echo "</form>\n";
93
		}
94
		else {
95
			$status = $data->dropDatabase($_POST['dropdatabase']);
96
			if ($status == 0) {
97
				$_reload_drop_database = true;
98
				doDefault($lang['strdatabasedropped']);
99
			}
100
			else
101
				doDefault($lang['strdatabasedroppedbad']);
102
		}
103
	}
104
	
105
	/**
106
	 * Displays a screen where they can enter a new database
107
	 */
108
	function doCreate($msg = '') {
109
		global $data, $misc;
110
		global $PHP_SELF, $lang;
111
		
112
		$misc->printTrail('server');
113
		$misc->printTitle($lang['strcreatedatabase'], 'pg.database.create');
114
		$misc->printMsg($msg);
115
		
116
		if (!isset($_POST['formName'])) $_POST['formName'] = '';
117
		// Default encoding is that in language file
118
		if (!isset($_POST['formEncoding'])) {
119
			if (isset($lang['appdbencoding']))
120
				$_POST['formEncoding'] = $lang['appdbencoding'];
121
			else
122
				$_POST['formEncoding'] = '';
123
		}
124
		if (!isset($_POST['formSpc'])) $_POST['formSpc'] = '';
125
		
126
		// Fetch all tablespaces from the database
127
		if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();
128

    
129
		echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
130
		echo "<table>\n";
131
		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
132
		echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
133
			htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n";
134
		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n";
135
		echo "\t\t<td class=\"data1\">\n";
136
		echo "\t\t\t<select name=\"formEncoding\">\n";
137
		echo "\t\t\t\t<option value=\"\"></option>\n";
138
		while (list ($key) = each ($data->codemap)) {
139
		    echo "\t\t\t\t<option value=\"", htmlspecialchars($key), "\"", 
140
				($key == $_POST['formEncoding']) ? ' selected="selected"' : '', ">",
141
				$misc->printVal($key), "</option>\n";
142
		}
143
		echo "\t\t\t</select>\n";
144
		echo "\t\t</td>\n\t</tr>\n";
145
		
146
		// Tablespace (if there are any)
147
		if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
148
			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
149
			echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n";
150
			// Always offer the default (empty) option
151
			echo "\t\t\t\t<option value=\"\"",
152
				($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n";
153
			// Display all other tablespaces
154
			while (!$tablespaces->EOF) {
155
				$spcname = htmlspecialchars($tablespaces->f['spcname']);
156
				echo "\t\t\t\t<option value=\"{$spcname}\"",
157
					($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
158
				$tablespaces->moveNext();
159
			}
160
			echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
161
		}
162
		
163
		echo "</table>\n";
164
		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
165
		echo $misc->form;
166
		echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
167
		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
168
		echo "</form>\n";
169
	}
170
	
171
	/**
172
	 * Actually creates the new view in the database
173
	 */
174
	function doSaveCreate() {
175
		global $data, $lang, $_reload_browser;
176
		
177
		// Default tablespace to null if it isn't set
178
		if (!isset($_POST['formSpc'])) $_POST['formSpc'] = null;
179

    
180
		// Check that they've given a name and a definition
181
		if ($_POST['formName'] == '') doCreate($lang['strdatabaseneedsname']);
182
		else {
183
			$status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc']);
184
			if ($status == 0) {
185
				$_reload_browser = true;
186
				doDefault($lang['strdatabasecreated']);
187
			}
188
			else
189
				doCreate($lang['strdatabasecreatedbad']);
190
		}
191
	}	
192

    
193
	/**
194
	 * Displays options for cluster download
195
	 */
196
	function doExport($msg = '') {
197
		global $data, $misc;
198
		global $PHP_SELF, $lang;
199

    
200
		$misc->printTrail('server');
201
		$misc->printTabs('server','export');
202
		$misc->printMsg($msg);
203

    
204
		echo "<form action=\"dbexport.php\" method=\"post\">\n";
205
		echo "<table>\n";
206
		echo "<tr><th class=\"data\">{$lang['strformat']}</th><th class=\"data\" colspan=\"2\">{$lang['stroptions']}</th></tr>\n";
207
		// Data only
208
		echo "<tr><th class=\"data left\" rowspan=\"2\">";
209
		echo "<input type=\"radio\" name=\"what\" value=\"dataonly\" checked=\"checked\" />{$lang['strdataonly']}</th>\n";
210
		echo "<td>{$lang['strformat']}</td>\n";
211
		echo "<td><select name=\"d_format\">\n";
212
		echo "<option value=\"copy\">COPY</option>\n";
213
		echo "<option value=\"sql\">SQL</option>\n";
214
		echo "</select>\n</td>\n</tr>\n";
215
		echo "<td>{$lang['stroids']}</td><td><input type=\"checkbox\" name=\"d_oids\" /></td>\n</tr>\n";
216
		// Structure only
217
		echo "<tr><th class=\"data left\"><input type=\"radio\" name=\"what\" value=\"structureonly\" />{$lang['strstructureonly']}</th>\n";
218
		echo "<td>{$lang['strdrop']}</td><td><input type=\"checkbox\" name=\"s_clean\" /></td>\n</tr>\n";
219
		// Structure and data
220
		echo "<tr><th class=\"data left\" rowspan=\"3\">";
221
		echo "<input type=\"radio\" name=\"what\" value=\"structureanddata\" />{$lang['strstructureanddata']}</th>\n";
222
		echo "<td>{$lang['strformat']}</td>\n";
223
		echo "<td><select name=\"sd_format\">\n";
224
		echo "<option value=\"copy\">COPY</option>\n";
225
		echo "<option value=\"sql\">SQL</option>\n";
226
		echo "</select>\n</td>\n</tr>\n";
227
		echo "<td>{$lang['strdrop']}</td><td><input type=\"checkbox\" name=\"sd_clean\" /></td>\n</tr>\n";
228
		echo "<td>{$lang['stroids']}</td><td><input type=\"checkbox\" name=\"sd_oids\" /></td>\n</tr>\n";
229
		echo "</table>\n";
230
		
231
		echo "<h3>{$lang['stroptions']}</h3>\n";
232
		echo "<p><input type=\"radio\" name=\"output\" value=\"show\" checked=\"checked\" />{$lang['strshow']}\n";
233
		echo "<br/><input type=\"radio\" name=\"output\" value=\"download\" />{$lang['strdownload']}</p>\n";
234

    
235
		echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
236
		echo "<p><input type=\"hidden\" name=\"subject\" value=\"server\" />\n";		
237
		echo $misc->form;
238
		echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
239
		echo "</form>\n";
240
	}
241

    
242
	/**
243
	 * Show default list of databases in the server
244
	 */
245
	function doDefault($msg = '') {
246
		global $data, $conf, $misc;
247
		global $PHP_SELF, $lang;
248

    
249
		$misc->printTrail('server');
250
		$misc->printTabs('server','databases');
251
		$misc->printMsg($msg);
252
		
253
		$databases = $data->getDatabases();
254

    
255
		$columns = array(
256
			'database' => array(
257
				'title' => $lang['strdatabase'],
258
				'field' => 'datname',
259
			),
260
			'owner' => array(
261
				'title' => $lang['strowner'],
262
				'field' => 'datowner',
263
			),
264
			'encoding' => array(
265
				'title' => $lang['strencoding'],
266
				'field' => 'datencoding',
267
			),
268
			'tablespace' => array(
269
				'title' => $lang['strtablespace'],
270
				'field' => 'tablespace',
271
			),
272
			'actions' => array(
273
				'title' => $lang['stractions'],
274
			),
275
			'comment' => array(
276
				'title' => $lang['strcomment'],
277
				'field' => 'datcomment',
278
			),
279
		);
280
		
281
		$actions = array(
282
			'properties' => array(
283
				'title' => $lang['strproperties'],
284
				'url'   => "redirect.php?subject=database&amp;{$misc->href}&amp;",
285
				'vars'  => array('database' => 'datname'),
286
			),
287
			'drop' => array(
288
				'title' => $lang['strdrop'],
289
				'url'   => "{$PHP_SELF}?action=confirm_drop&amp;subject=database&amp;{$misc->href}&amp;",
290
				'vars'  => array('dropdatabase' => 'datname'),
291
			),
292
			'privileges' => array(
293
				'title' => $lang['strprivileges'],
294
				'url'   => "privileges.php?subject=database&amp;{$misc->href}&amp;",
295
				'vars'  => array('database' => 'datname'),
296
			)
297
		);
298
		if ($data->hasAlterDatabase() ) {
299
			$actions['alter'] = array(
300
				'title' => $lang['stralter'],
301
				'url'   => "{$PHP_SELF}?action=confirm_alter&amp;subject=database&amp;{$misc->href}&amp;",
302
				'vars'  => array('alterdatabase' => 'datname')
303
			);
304
		}
305
		
306
		if (!$data->hasTablespaces()) unset($columns['tablespace']);
307
		if (!isset($data->privlist['database'])) unset($actions['privileges']);
308
		
309
		$misc->printTable($databases, $columns, $actions, $lang['strnodatabases']);
310

    
311
		echo "<p><a class=\"navlink\" href=\"$PHP_SELF?action=create&amp;{$misc->href}\">{$lang['strcreatedatabase']}</a></p>\n";
312

    
313
	}
314
	
315
	function doTree() {
316
		global $misc, $data, $lang;
317
		
318
		$databases = $data->getDatabases();
319
		
320
		$reqvars = $misc->getRequestVars('database');
321
		
322
		$attrs = array(
323
			'text'   => field('datname'),
324
			'icon'   => 'database',
325
			'toolTip'=> field('datcomment'),
326
			'action' => url('redirect.php',
327
							$reqvars,
328
							array('database' => field('datname'))
329
						),
330
			'branch' => url('database.php',
331
							$reqvars,
332
							array(
333
								'action' => 'tree',
334
								'database' => field('datname')
335
							)
336
						),
337
		);
338
		
339
		$misc->printTreeXML($databases, $attrs);
340
		exit;
341
	}
342

    
343
	if ($action == 'tree') doTree();
344
	
345
	$misc->printHeader($lang['strdatabases']);
346
	$misc->printBody();
347

    
348
	switch ($action) {
349
		case 'export':
350
			doExport();
351
			break;
352
		case 'save_create':
353
			if (isset($_POST['cancel'])) doDefault();
354
			else doSaveCreate();
355
			break;
356
		case 'create':
357
			doCreate();
358
			break;
359
		case 'drop':
360
			if (isset($_REQUEST['drop'])) doDrop(false);
361
			else doDefault();
362
			break;
363
		case 'confirm_drop':
364
			doDrop(true);
365
			break;
366
		case 'alter':
367
			if (isset($_POST['oldname']) && isset($_POST['newname']) && !isset($_POST['cancel']) ) doAlter(false);
368
			else doDefault();
369
			break;			
370
		case 'confirm_alter':
371
			doAlter(true);
372
			break;
373
		default:
374
			doDefault();
375
			break;
376
	}	
377

    
378
	$misc->printFooter();
379

    
380
?>
(10-10/53)