1<?php
2
3	/**
4	 * Manage databases within a server
5	 *
6	 * $Id: all_db.php,v 1.59 2007/10/17 21:40:19 ioguix 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
15	/**
16	 * Display a form for alter and perform actual alter
17	 */
18	function doAlter($confirm) {
19		global $data, $misc, $_reload_browser;
20		global $lang;
21
22		if ($confirm) {
23			$misc->printTrail('database');
24			$misc->printTitle($lang['stralter'], 'pg.database.alter');
25
26			echo "<form action=\"all_db.php\" method=\"post\">\n";
27			echo "<table>\n";
28			echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
29			echo "<td class=\"data1\">";
30			echo "<input name=\"newname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
31				htmlspecialchars($_REQUEST['alterdatabase']), "\" /></td></tr>\n";
32
33			if ($data->hasAlterDatabaseOwner() && $data->isSuperUser()) {
34				// Fetch all users
35
36				$rs = $data->getDatabaseOwner($_REQUEST['alterdatabase']);
37				$owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : '';
38				$users = $data->getUsers();
39
40				echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
41				echo "<td class=\"data1\"><select name=\"owner\">";
42				while (!$users->EOF) {
43					$uname = $users->fields['usename'];
44					echo "<option value=\"", htmlspecialchars($uname), "\"",
45						($uname == $owner) ? ' selected="selected"' : '', ">", htmlspecialchars($uname), "</option>\n";
46					$users->moveNext();
47				}
48				echo "</select></td></tr>\n";
49			}
50			if ($data->hasSharedComments()){
51				$rs = $data->getDatabaseComment($_REQUEST['alterdatabase']);
52				$comment = isset($rs->fields['description']) ? $rs->fields['description'] : '';
53				echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
54				echo "<td class=\"data1\">";
55				echo "<textarea rows=\"3\" cols=\"32\" name=\"dbcomment\">",
56					htmlspecialchars($comment), "</textarea></td></tr>\n";
57			}
58			echo "</table>\n";
59			echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
60			echo $misc->form;
61			echo "<input type=\"hidden\" name=\"oldname\" value=\"",
62				htmlspecialchars($_REQUEST['alterdatabase']), "\" />\n";
63			echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
64			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
65			echo "</form>\n";
66		}
67		else {
68			if (!isset($_POST['owner'])) $_POST['owner'] = '';
69			if (!isset($_POST['dbcomment'])) $_POST['dbcomment'] = '';
70			if ($data->alterDatabase($_POST['oldname'], $_POST['newname'], $_POST['owner'], $_POST['dbcomment']) == 0) {
71				$_reload_browser = true;
72				doDefault($lang['strdatabasealtered']);
73			}
74			else
75				doDefault($lang['strdatabasealteredbad']);
76		}
77	}
78
79	/**
80	 * Show confirmation of drop and perform actual drop
81	 */
82	function doDrop($confirm) {
83		global $data, $misc;
84		global $lang, $_reload_drop_database;
85
86		if (empty($_REQUEST['dropdatabase']) && empty($_REQUEST['ma'])) {
87			doDefault($lang['strspecifydatabasetodrop']);
88			exit();
89		}
90
91		if ($confirm) {
92
93            $misc->printTrail('database');
94            $misc->printTitle($lang['strdrop'], 'pg.database.drop');
95
96	        echo "<form action=\"all_db.php\" method=\"post\">\n";
97            //If multi drop
98            if (isset($_REQUEST['ma'])) {
99
100			    foreach($_REQUEST['ma'] as $v) {
101			        $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
102				    echo "<p>", sprintf($lang['strconfdropdatabase'], $misc->printVal($a['database'])), "</p>\n";
103				    printf('<input type="hidden" name="dropdatabase[]" value="%s" />', htmlspecialchars($a['database']));
104			    }
105
106			} else {
107		            echo "<p>", sprintf($lang['strconfdropdatabase'], $misc->printVal($_REQUEST['dropdatabase'])), "</p>\n";
108			        echo "<input type=\"hidden\" name=\"dropdatabase\" value=\"", htmlspecialchars($_REQUEST['dropdatabase']), "\" />\n";
109            }// END if multi drop
110
111			echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
112        	echo $misc->form;
113			echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
114			echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
115			echo "</form>\n";
116		} // END confirm
117		else {
118            //If multi drop
119            if (is_array($_REQUEST['dropdatabase'])) {
120                $msg = '';
121                foreach($_REQUEST['dropdatabase'] as $d) {
122					$status = $data->dropDatabase($d);
123					if ($status == 0)
124						$msg.= sprintf('%s: %s<br />', htmlentities($d, ENT_QUOTES, 'UTF-8'), $lang['strdatabasedropped']);
125					else {
126						doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($d, ENT_QUOTES, 'UTF-8'), $lang['strdatabasedroppedbad']));
127						return;
128					}
129				}// Everything went fine, back to Default page...
130                $_reload_drop_database = true;
131                doDefault($msg);
132            } else {
133			    $status = $data->dropDatabase($_POST['dropdatabase']);
134			    if ($status == 0) {
135				    $_reload_drop_database = true;
136				    doDefault($lang['strdatabasedropped']);
137			    }
138			    else
139				    doDefault($lang['strdatabasedroppedbad']);
140            }
141		}//END DROP
142    }// END FUNCTION
143
144
145	/**
146	 * Displays a screen where they can enter a new database
147	 */
148	function doCreate($msg = '') {
149		global $data, $misc;
150		global $lang;
151
152		$misc->printTrail('server');
153		$misc->printTitle($lang['strcreatedatabase'], 'pg.database.create');
154		$misc->printMsg($msg);
155
156		if (!isset($_POST['formName'])) $_POST['formName'] = '';
157		// Default encoding is that in language file
158		if (!isset($_POST['formEncoding'])) {
159		    $_POST['formEncoding'] = '';
160		}
161		if (!isset($_POST['formTemplate'])) $_POST['formTemplate'] = 'template1';
162		if (!isset($_POST['formSpc'])) $_POST['formSpc'] = '';
163		if (!isset($_POST['formComment'])) $_POST['formComment'] = '';
164
165		// Fetch a list of databases in the cluster
166		$templatedbs = $data->getDatabases(false);
167
168		// Fetch all tablespaces from the database
169		if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();
170
171		echo "<form action=\"all_db.php\" method=\"post\">\n";
172		echo "<table>\n";
173		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
174		echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
175			htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n";
176
177		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtemplatedb']}</th>\n";
178		echo "\t\t<td class=\"data1\">\n";
179		echo "\t\t\t<select name=\"formTemplate\">\n";
180		// Always offer template0 and template1
181		echo "\t\t\t\t<option value=\"template0\"",
182			($_POST['formTemplate'] == 'template0') ? ' selected="selected"' : '', ">template0</option>\n";
183		echo "\t\t\t\t<option value=\"template1\"",
184			($_POST['formTemplate'] == 'template1') ? ' selected="selected"' : '', ">template1</option>\n";
185		while (!$templatedbs->EOF) {
186			$dbname = htmlspecialchars($templatedbs->fields['datname']);
187			if ($dbname != 'template1') {
188				// filter out for $conf[show_system] users so we don't get duplicates
189				echo "\t\t\t\t<option value=\"{$dbname}\"",
190					($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n";
191			}
192			$templatedbs->moveNext();
193		}
194		echo "\t\t\t</select>\n";
195		echo "\t\t</td>\n\t</tr>\n";
196
197		// ENCODING
198		echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n";
199		echo "\t\t<td class=\"data1\">\n";
200		echo "\t\t\t<select name=\"formEncoding\">\n";
201		echo "\t\t\t\t<option value=\"\"></option>\n";
202		while (list ($key) = each ($data->codemap)) {
203		    echo "\t\t\t\t<option value=\"", htmlspecialchars($key), "\"",
204				($key == $_POST['formEncoding']) ? ' selected="selected"' : '', ">",
205				$misc->printVal($key), "</option>\n";
206		}
207		echo "\t\t\t</select>\n";
208		echo "\t\t</td>\n\t</tr>\n";
209
210		if ($data->hasDatabaseCollation()) {
211			if (!isset($_POST['formCollate'])) $_POST['formCollate'] = '';
212			if (!isset($_POST['formCType'])) $_POST['formCType'] = '';
213
214			// LC_COLLATE
215			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcollation']}</th>\n";
216			echo "\t\t<td class=\"data1\">\n";
217			echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n";
218			echo "\t\t</td>\n\t</tr>\n";
219
220			// LC_CTYPE
221			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strctype']}</th>\n";
222			echo "\t\t<td class=\"data1\">\n";
223			echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n";
224			echo "\t\t</td>\n\t</tr>\n";
225		}
226
227		// Tablespace (if there are any)
228		if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
229			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
230			echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n";
231			// Always offer the default (empty) option
232			echo "\t\t\t\t<option value=\"\"",
233				($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n";
234			// Display all other tablespaces
235			while (!$tablespaces->EOF) {
236				$spcname = htmlspecialchars($tablespaces->fields['spcname']);
237				echo "\t\t\t\t<option value=\"{$spcname}\"",
238					($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
239				$tablespaces->moveNext();
240			}
241			echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
242		}
243
244		// Comments (if available)
245		if ($data->hasSharedComments()) {
246			echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
247			echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
248				htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
249		}
250
251		echo "</table>\n";
252		echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
253		echo $misc->form;
254		echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
255		echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
256		echo "</form>\n";
257	}
258
259	/**
260	 * Actually creates the new view in the database
261	 */
262	function doSaveCreate() {
263		global $data, $lang, $_reload_browser;
264
265		// Default tablespace to null if it isn't set
266		if (!isset($_POST['formSpc'])) $_POST['formSpc'] = null;
267
268		// Default comment to blank if it isn't set
269		if (!isset($_POST['formComment'])) $_POST['formComment'] = null;
270
271		// Default collate to blank if it isn't set
272		if (!isset($_POST['formCollate'])) $_POST['formCollate'] = null;
273
274		// Default ctype to blank if it isn't set
275		if (!isset($_POST['formCType'])) $_POST['formCType'] = null;
276
277		// Check that they've given a name and a definition
278		if ($_POST['formName'] == '') doCreate($lang['strdatabaseneedsname']);
279		else {
280			$status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'],
281				$_POST['formComment'], $_POST['formTemplate'], $_POST['formCollate'], $_POST['formCType']);
282			if ($status == 0) {
283				$_reload_browser = true;
284				doDefault($lang['strdatabasecreated']);
285			}
286			else
287				doCreate($lang['strdatabasecreatedbad']);
288		}
289	}
290
291	/**
292	 * Displays options for cluster download
293	 */
294	function doExport($msg = '') {
295		global $data, $misc;
296		global $lang;
297
298		$misc->printTrail('server');
299		$misc->printTabs('server','export');
300		$misc->printMsg($msg);
301
302		echo "<form action=\"dbexport.php\" method=\"post\">\n";
303		echo "<table>\n";
304		echo "<tr><th class=\"data\">{$lang['strformat']}</th><th class=\"data\">{$lang['stroptions']}</th></tr>\n";
305		// Data only
306		echo "<tr><th class=\"data left\" rowspan=\"". ($data->hasServerOids() ? 2 : 1) ."\">";
307		echo "<input type=\"radio\" id=\"what1\" name=\"what\" value=\"dataonly\" checked=\"checked\" /><label for=\"what1\">{$lang['strdataonly']}</label></th>\n";
308		echo "<td>{$lang['strformat']}\n";
309		echo "<select name=\"d_format\">\n";
310		echo "<option value=\"copy\">COPY</option>\n";
311		echo "<option value=\"sql\">SQL</option>\n";
312		echo "</select>\n</td>\n</tr>\n";
313		if ($data->hasServerOids()) {
314			echo "<tr><td><input type=\"checkbox\" id=\"d_oids\" name=\"d_oids\" /><label for=\"d_oids\">{$lang['stroids']}</label></td>\n</tr>\n";
315		}
316		// Structure only
317		echo "<tr><th class=\"data left\"><input type=\"radio\" id=\"what2\" name=\"what\" value=\"structureonly\" /><label for=\"what2\">{$lang['strstructureonly']}</label></th>\n";
318		echo "<td><input type=\"checkbox\" id=\"s_clean\" name=\"s_clean\" /><label for=\"s_clean\">{$lang['strdrop']}</label></td>\n</tr>\n";
319		// Structure and data
320		echo "<tr><th class=\"data left\" rowspan=\"". ($data->hasServerOids() ? 3 : 2) ."\">";
321		echo "<input type=\"radio\" id=\"what3\" name=\"what\" value=\"structureanddata\" /><label for=\"what3\">{$lang['strstructureanddata']}</label></th>\n";
322		echo "<td>{$lang['strformat']}\n";
323		echo "<select name=\"sd_format\">\n";
324		echo "<option value=\"copy\">COPY</option>\n";
325		echo "<option value=\"sql\">SQL</option>\n";
326		echo "</select>\n</td>\n</tr>\n";
327		echo "<tr><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /><label for=\"sd_clean\">{$lang['strdrop']}</label></td>\n</tr>\n";
328		if ($data->hasServerOids()) {
329			echo "<tr><td><input type=\"checkbox\" id=\"sd_oids\" name=\"sd_oids\" /><label for=\"sd_oids\">{$lang['stroids']}</label></td>\n</tr>\n";
330		}
331		echo "</table>\n";
332
333		echo "<h3>{$lang['stroptions']}</h3>\n";
334		echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n";
335		echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label></p>\n";
336
337		echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
338		echo "<input type=\"hidden\" name=\"subject\" value=\"server\" />\n";
339		echo $misc->form;
340		echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
341		echo "</form>\n";
342	}
343
344	/**
345	 * Show default list of databases in the server
346	 */
347	function doDefault($msg = '') {
348		global $data, $conf, $misc;
349		global $lang;
350
351		$misc->printTrail('server');
352		$misc->printTabs('server','databases');
353		$misc->printMsg($msg);
354
355		$databases = $data->getDatabases();
356
357		$columns = array(
358			'database' => array(
359				'title' => $lang['strdatabase'],
360				'field' => field('datname'),
361				'url'   => "redirect.php?subject=database&amp;{$misc->href}&amp;",
362				'vars'  => array('database' => 'datname'),
363			),
364			'owner' => array(
365				'title' => $lang['strowner'],
366				'field' => field('datowner'),
367			),
368			'encoding' => array(
369				'title' => $lang['strencoding'],
370				'field' => field('datencoding'),
371			),
372			'lc_collate' => array(
373				'title' => $lang['strcollation'],
374				'field' => field('datcollate'),
375			),
376			'lc_ctype' => array(
377				'title' => $lang['strctype'],
378				'field' => field('datctype'),
379			),
380			'tablespace' => array(
381				'title' => $lang['strtablespace'],
382				'field' => field('tablespace'),
383			),
384			'dbsize' => array(
385				'title' => $lang['strsize'],
386				'field' => field('dbsize'),
387				'type' => 'prettysize',
388			),
389			'actions' => array(
390				'title' => $lang['stractions'],
391			),
392			'comment' => array(
393				'title' => $lang['strcomment'],
394				'field' => field('datcomment'),
395			),
396		);
397
398		$actions = array(
399			'multiactions' => array(
400			    'keycols' => array('database' => 'datname'),
401			    'url' => 'all_db.php',
402			    'default' => null,
403			),
404			'drop' => array(
405			    'content' => $lang['strdrop'],
406			    'attr'=> array (
407				'href' => array (
408				    'url' => 'all_db.php',
409				    'urlvars' => array (
410					'subject' => 'database',
411					'action' => 'confirm_drop',
412					'dropdatabase' => field('datname')
413				    )
414				)
415			    ),
416			    'multiaction' => 'confirm_drop',
417			),
418			'privileges' => array(
419			    'content' => $lang['strprivileges'],
420			    'attr'=> array (
421				'href' => array (
422				    'url' => 'privileges.php',
423				    'urlvars' => array (
424					'subject' => 'database',
425					'database' => field('datname')
426				    )
427				)
428			    )
429			)
430		);
431		if ($data->hasAlterDatabase() ) {
432		    $actions['alter'] = array(
433			'content' => $lang['stralter'],
434			'attr'=> array (
435			    'href' => array (
436				'url' => 'all_db.php',
437				'urlvars' => array (
438				    'subject' => 'database',
439				    'action' => 'confirm_alter',
440				    'alterdatabase' => field('datname')
441				)
442			    )
443			)
444		    );
445		}
446
447		if (!$data->hasTablespaces()) unset($columns['tablespace']);
448		if (!$data->hasServerAdminFuncs()) unset($columns['dbsize']);
449		if (!$data->hasDatabaseCollation()) unset($columns['lc_collate'], $columns['lc_ctype']);
450		if (!isset($data->privlist['database'])) unset($actions['privileges']);
451
452		$misc->printTable($databases, $columns, $actions, 'all_db-databases', $lang['strnodatabases']);
453
454		$navlinks = array (
455		    'create' => array (
456			'attr'=> array (
457			    'href' => array (
458				'url' => 'all_db.php',
459				'urlvars' => array (
460				    'action' => 'create',
461				    'server' => $_REQUEST['server']
462				)
463			    )
464			),
465			'content' => $lang['strcreatedatabase']
466		    )
467		);
468		$misc->printNavLinks($navlinks, 'all_db-databases', get_defined_vars());
469	}
470
471	function doTree() {
472		global $misc, $data, $lang;
473
474		$databases = $data->getDatabases();
475
476		$reqvars = $misc->getRequestVars('database');
477
478		$attrs = array(
479			'text'   => field('datname'),
480			'icon'   => 'Database',
481			'toolTip'=> field('datcomment'),
482			'action' => url('redirect.php',
483							$reqvars,
484							array('database' => field('datname'))
485						),
486			'branch' => url('database.php',
487							$reqvars,
488							array(
489								'action' => 'tree',
490								'database' => field('datname')
491							)
492						),
493		);
494
495		$misc->printTree($databases, $attrs, 'databases');
496		exit;
497	}
498
499	if ($action == 'tree') doTree();
500
501	$misc->printHeader($lang['strdatabases']);
502	$misc->printBody();
503
504	switch ($action) {
505		case 'export':
506			doExport();
507			break;
508		case 'save_create':
509			if (isset($_POST['cancel'])) doDefault();
510			else doSaveCreate();
511			break;
512		case 'create':
513			doCreate();
514			break;
515		case 'drop':
516			if (isset($_REQUEST['drop'])) doDrop(false);
517			else doDefault();
518			break;
519		case 'confirm_drop':
520			doDrop(true);
521			break;
522		case 'alter':
523			if (isset($_POST['oldname']) && isset($_POST['newname']) && !isset($_POST['cancel']) ) doAlter(false);
524			else doDefault();
525			break;
526		case 'confirm_alter':
527			doAlter(true);
528			break;
529		default:
530			doDefault();
531			break;
532	}
533
534	$misc->printFooter();
535
536?>
537