Files
phpmyadmin/scripts/setup.php
2005-11-25 15:49:30 +00:00

543 lines
19 KiB
PHP

<?php
/* $Id$ */
// vim: expandtab sw=4 ts=4 sts=4:
// phpMyAdmin simple setup script by Michal Čihař <michal@cihar.com>
$script_info = 'phpMyAdmin simple setup script by Michal Čihař <michal@cihar.com>';
$script_version = '$Id$';
function remove_slashes($val) {
if (get_magic_quotes_gpc()) {
return stripslashes($val);
}
return $val;
}
// Grab some variables
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else {
$action = '';
}
if (isset($_POST['cfg'])) {
$cfg = unserialize(remove_slashes($_POST['cfg']));
} else {
$cfg = array();
}
if (!isset($cfg['Servers']) || !is_array($cfg['Servers'])) {
$cfg['Servers'] = array();
}
// whether to show html header?
if ($action != 'download') {
// this needs to be echoed otherwise php with short tags complains
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<link rel="icon" href="../favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<title>phpMyAdmin setup</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript">
//<![CDATA[
// show this window in top frame
if (top != self) {
window.top.location.href=location;
}
//]]>
</script>
<style type="text/css">
/* message boxes: warning, error, stolen from original theme */
div.notice {
color: #000000;
background-color: #FFFFDD;
}
h1.notice,
div.notice {
margin: 0.5em 0 0.5em 0;
border: 0.1em solid #FFD700;
background-image: url(../themes/original/img/s_notice.png);
background-repeat: no-repeat;
background-position: 10px 50%;
padding: 10px 10px 10px 36px;
}
div.notice h1 {
border-bottom: 0.1em solid #FFD700;
font-weight: bold;
font-size: large;
text-align: left;
margin: 0 0 0.2em 0;
}
div.warning {
color: #CC0000;
background-color: #FFFFCC;
}
h1.warning,
div.warning {
margin: 0.5em 0 0.5em 0;
border: 0.1em solid #CC0000;
background-image: url(../themes/original/img/s_warn.png);
background-repeat: no-repeat;
background-position: 10px 50%;
padding: 10px 10px 10px 36px;
}
div.warning h1 {
border-bottom: 0.1em solid #cc0000;
font-weight: bold;
text-align: left;
font-size: large;
margin: 0 0 0.2em 0;
}
div.error {
background-color: #FFFFCC;
color: #ff0000;
}
h1.error,
div.error {
margin: 0.5em 0 0.5em 0;
border: 0.1em solid #ff0000;
background-image: url(../themes/original/img/s_error.png);
background-repeat: no-repeat;
background-position: 10px 50%;
padding: 10px 10px 10px 36px;
}
div.error h1 {
border-bottom: 0.1em solid #ff0000;
font-weight: bold;
text-align: left;
font-size: large;
margin: 0 0 0.2em 0;
}
form.action {
display: block;
float: left;
margin: 0;
padding: 0;
border: 1px solid black;
}
form.action input {
margin: 0.3em;
padding: 0.1em;
}
hr.separator {
clear: both;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h1>phpMyAdmin setup</h1>
<?php
} // end show html header
function message($type, $text, $title = '') {
echo '<div class="' . $type . '">' . "\n";
if (!empty($title)) {
echo '<h1>';
echo $title;
echo '</h1>' . "\n";
}
echo $text . "\n";
echo '</div>' . "\n";
}
function show_hidden_cfg() {
global $cfg;
echo '<input type="hidden" name="cfg" value="' . htmlspecialchars(serialize($cfg)) . '" />';
}
function show_action($name, $title, $added = '') {
echo '<form class="action" method="POST">';
echo '<input type="hidden" name="action" value="' . $name . '" />';
echo $added;
echo '<input type="submit" value="' . $title . '" />';
show_hidden_cfg();
echo '</form>';
echo "\n";
}
function footer() {
echo '</body>';
exit;
}
function get_cfg_string() {
global $cfg, $script_info, $script_version;
$c = $cfg;
$ret = "<?php\n/*\n * Generated configuration file\n * Generated by: $script_info\n * Version: $script_version\n * Date: " . gmdate('D, d M Y H:i:s') . " GMT\n */\n\n";
if (count($c['Servers']) > 0) {
$ret .= "/* Servers configuration */\n\$i = 0;\n\n";
$cnt = 1;
foreach($c['Servers'] as $srv) {
$ret .= "/* Server $cnt */\n\$i++;\n";
foreach($srv as $key => $val) {
$ret .= "\$cfg['Servers'][\$i][$key] = '$val';\n";
}
$cnt++;
}
$ret .= "/* End of servers configration */\n\n";
}
unset($c['Servers']);
foreach($c as $key => $val) {
$ret .= "\$cfg['$key'] = " . var_export($val, TRUE) . ";\n";
}
$ret .= "?>\n";
return $ret;
}
function grab_values($list) {
$a = split(';', $list);
$res = array();
foreach($a as $val) {
$v = split(':', $val);
if (!isset($v[1])) $v[1] = '';
switch($v[1]) {
case 'bool':
$res[$v[0]] = isset($_POST[$v[0]]);
break;
default:
$res[$v[0]] = remove_slashes($_POST[$v[0]]);
break;
}
}
return $res;
}
function show_config_form($list, $defaults = array(), $save = 'Add') {
foreach($list as $val) {
$type = 'text';
if (isset($val[3])) {
if (is_array($val[3])) $type = 'select';
elseif (is_bool($val[3])) $type = 'check';
elseif ($val[3] == 'password') $type = 'password';
}
switch ($type) {
case 'text':
case 'password':
echo '<label for="text_' . $val[1] . '" style="float: left; width: 30em;" title="' . $val[2] . '">' . $val[0] . ':</label>';
echo '<input type="' . $type . '" name="' . $val[1] . '" id="text_' . $val[1] . '" title="' . $val[2] . '"';
if (isset($defaults[$val[1]])) {
echo ' value="' . htmlspecialchars($defaults[$val[1]]) . '"';
}
echo ' />';
break;
case 'check':
echo '<input type="checkbox" name="' . $val[1] . '" value="something" id="checkbox_' . $val[1] . '" title="' . $val[2] . '"';
if (isset($defaults[$val[1]]) && $defaults[$val[1]]) {
echo ' checked="checked"';
}
echo ' />';
echo '<label for="checkbox_' . $val[1] . '" title="' . $val[2] . '">' . $val[0] . '</label>';
break;
case 'select':
echo '<label for="select_' . $val[1] . '" style="float: left; width: 30em;" title="' . $val[2] . '">' . $val[0] . ':</label>';
echo '<select name="' . $val[1] . '" id="select_' . $val[1] . '" ' . ' title="' . $val[2] . '">';
foreach ($val[3] as $opt) {
echo '<option value="' . $opt . '"';
if (isset($defaults[$val[1]]) && $defaults[$val[1]] == $opt) {
echo ' selected="selected"';
}
echo '>' . $opt . '</option>';
}
echo '</select>';
break;
}
echo '<br />' . "\n";
}
echo '<div style="float: left; width: 30em;">Actions:</div>';
echo '<input type="submit" name="submit_save" value="' . $save .'" />';
echo '<input type="submit" name="submit_ignore" value="Cancel" />';
echo "\n";
}
function show_server_form($defaults = array(), $number = FALSE) {
?>
<form method="post">
<input type="hidden" name="action" value="addserver_real" />
<?php
show_hidden_cfg();
if (!($number === FALSE)) {
echo '<input type="hidden" name="server" value="' . $number . '" />';
}
show_config_form(array(
array('Server hostname', 'host', 'Hostname where MySQL server is running'),
array('Server port', 'port', 'Port on which MySQL server is listening, leave empty if don\'t know'),
array('Server socked', 'socket', 'Socket on which MySQL server is listening, leave empty if don\'t know'),
array('Connection type', 'connect_type', 'How to connect to server, keep tcp if don\'t know', array('tcp', 'socket')),
array('PHP extension to use', 'extension', 'What PHP extension to use, use mysqli if supported', array('mysql', 'mysqli')),
array('Compress connection', 'compress', 'Whether to compress connection to MySQL server', FALSE),
array('phpMyAdmin control user', 'controluser', 'User which phpMyAdmin can use for various actions'),
array('phpMyAdmin control user password', 'controlpass', 'Password for user which phpMyAdmin can use for various actions', 'password'),
array('Authentication type', 'auth_type', 'Authentication method to use', array('cookie', 'http', 'config')),
array('User for config auth', 'user', 'Leave empty if not using config auth'),
array('Password for config auth', 'password', 'Leave empty if not using config auth', 'password'),
array('Only database to show', 'only_db', 'Limit listing of databases in left frame to this one'),
array('Verbose name of this server', 'verbose', 'Name to display in server selection'),
array('phpMyAdmin database for advanced features', 'pmadb', 'phpMyAdmin will allow much more when you enable this'),
), $defaults, $number === FALSE ? 'Add' : 'Save');
?>
</form>
<?php
}
function get_server_name($val) {
if (!empty($val['verbose'])) {
$ret = htmlspecialchars($val['verbose']);
} else {
$ret = htmlspecialchars($val['host']);
}
$ret .= ' (' . $val['auth_type'] . ')';
return $ret;
}
function get_server_selection() {
global $cfg;
if (count($cfg['Servers']) == 0) return '';
$ret = '<select name="server">';
foreach ($cfg['Servers'] as $key => $val) {
$ret .= '<option value="' . $key . '">' . get_server_name($val) . '</option>';
}
$ret .= '</select>';
return $ret;
}
if ($action != 'download') {
// Check whether we can write to configuration
$fail_dir = FALSE;
$fail_dir = $fail_dir || !is_dir('../config/');
$fail_dir = $fail_dir || !is_writable('../config/config.inc.php');
$config = @fopen('../config/config.inc.php', 'a');
$fail_dir = $fail_dir || ($config === FALSE);
@fclose($config);
if ($fail_dir) {
message('warning', 'Please create web server writable folder config in phpMyAdmin toplevel directory as described in <a href="../Documentation.html">documentation</a>. Otherwise you will be only able to download or display it.', 'Can not write configuration');
}
}
if (empty($action)) {
message('notice', 'You want to configure phpMyAdmin using web interface. Please note that this only allows basic setup, please read <a href="../Documentation.html">documentation</a> to see full description of all configuration directives.', 'Welcome');
if (empty($_SERVER['HTTPS']) || strtolower($_SERVER['HTTPS']) == 'off') {
message('warning', 'You are not using secure connection, all data (including sensitive ones, like passwords) are transfered unencrypted!');
}
}
$show_info = FALSE;
switch ($action) {
case 'download':
header('Content-Type: text/plain');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="config.inc.php"');
header('Pragma: no-cache');
echo get_cfg_string();
exit;
break;
case 'display':
echo '<form method="none"><textarea name="config" cols="50" rows="20" id="textconfig" wrap="off">' . "\n";
echo htmlentities(get_cfg_string());
echo '</textarea></form>' . "\n";
?>
<script language="javascript" type="text/javascript">
<!--
var bodyWidth=null; var bodyHeight=null;
if (document.getElementById('textconfig')) {
bodyWidth = self.innerWidth;
bodyHeight = self.innerHeight;
if(!bodyWidth && !bodyHeight){
if (document.compatMode && document.compatMode == "BackCompat") {
bodyWidth = document.body.clientWidth;
bodyHeight = document.body.clientHeight;
} else if (document.compatMode && document.compatMode == "CSS1Compat") {
bodyWidth = document.documentElement.clientWidth;
bodyHeight = document.documentElement.clientHeight;
}
}
document.getElementById('textconfig').style.width=(bodyWidth-50) + 'px';
document.getElementById('textconfig').style.height=(bodyHeight-100) + 'px';
}
//-->
</script>
<?php
break;
case 'save':
$config = fopen('../config/config.inc.php', 'w');
// FIXME: check errors
fwrite($config, get_cfg_string());
fclose($config);
message('notice', 'Configration saved to file config/config.inc.php in phpMyAdmin top level directory, copy it to top level one and delete directory config to use it.', 'File saved');
break;
case 'load':
$bck_cfg = $cfg;
unset($cfg);
$config_file = '../config/config.inc.php';
if ( file_exists( $config_file ) ) {
$success_apply_user_config = FALSE;
$old_error_reporting = error_reporting( 0 );
if ( function_exists( 'file_get_contents' ) ) {
$success_apply_user_config = eval( '?>' . file_get_contents( $config_file ) );
} else {
$success_apply_user_config =
eval( '?>' . implode( '\n', file( $config_file ) ) );
}
error_reporting( $old_error_reporting );
unset( $old_error_reporting );
if ($success_apply_user_config === FALSE) {
message('error', 'Error while parsing configuraton file!');
$cfg = $bck_cfg;
} elseif (count($cfg) == 0 || (isset($cfg['Servers']) && count($cfg) == 1 || count($cfg['Servers']) == 0)) {
message('error', 'Config file seems to contain no configuration!');
$cfg = $bck_cfg;
} else {
message('notice', 'Configuration loaded');
}
} else {
message('error', 'Configuration file not found!');
$cfg = $bck_cfg;
}
$show_info = TRUE;
break;
case 'addserver_real':
if (isset($_POST['submit_save'])) {
$new_server = grab_values('host;port;socket;connect_type;compress:bool;controluser;controlpass;auth_type;user;password;only_db;verbose;pmadb');
// Just use defaults, should be okay for most users
if (!empty($new_server['pma_db'])) {
$new_server['bookmarktable'] = 'pma_bookmark';
$new_server['relation'] = 'pma_relation';
$new_server['table_info'] = 'pma_table_info';
$new_server['table_coords'] = 'pma_table_coords';
$new_server['pdf_pages'] = 'pma_pdf_pages';
$new_server['column_info'] = 'pma_column_info';
$new_server['history'] = 'pma_history';
}
$err = FALSE;
if (empty($new_server['host'])) {
message('error', 'Empty hostname!');
$err = TRUE;
}
if ($new_server['socket'] && empty($new_server['socket'])) {
message('error', 'Empty socket with socket connection seleted!');
$err = TRUE;
}
if ($new_server['auth_type'] == 'config' && empty($new_server['user'])) {
message('error', 'Empty username while using config authentication method!');
$err = TRUE;
}
if ($new_server['auth_type'] == 'config') {
message('warning', 'Remember to protect your installation while using config authentication method!');
}
if ($err) {
show_server_form($new_server, isset($_POST['server']) ? $_POST['server'] : FALSE);
} else {
if (isset($_POST['server'])) {
$cfg['Servers'][$_POST['server']] = $new_server;
message('notice', 'Changed server number ' . $_POST['server']);
} else {
$cfg['Servers'][] = $new_server;
message('notice', 'New server added');
}
$show_info = TRUE;
}
} else {
message('notice', 'Adding of server canceled');
$show_info = TRUE;
}
break;
case 'addserver':
if (count($cfg['Servers']) == 0) {
show_server_form(array('host' => 'localhost', 'auth_type' => 'config', 'user' => 'root'));
} else {
show_server_form();
}
break;
case 'editserver':
message('notice', 'Editing server number ' . $_POST['server']);
show_server_form($cfg['Servers'][$_POST['server']], $_POST['server']);
break;
case 'deleteserver':
message('notice', 'Deleted server number ' . $_POST['server']);
unset($cfg['Servers'][$_POST['server']]);
// FIXME: compress array here (maybe not needed)
$show_info = TRUE;
break;
case 'servers':
if (count($cfg['Servers']) == 0) {
message('notice', 'No servers defined, so none can not be shown');
} else {
foreach($cfg['Servers'] as $srv) {
// FIXME: more human friendly output
echo '<pre>';
print_r($srv);
echo '</pre>';
}
}
break;
case 'main':
case '':
$show_info = TRUE;
break;
}
if ($show_info) {
echo '<p>Current configuration overview:</p>' . "\n";
echo '<p>You have defined ' . count($cfg['Servers']) . ' servers:';
$sep = ' ';
foreach ($cfg['Servers'] as $val) {
echo $sep;
$sep = ', ';
echo get_server_name($val);
}
unset($sep);
echo '</p>' . "\n";
}
echo '<p>Available global actions (please note that these will delete any changes you could have done above):</p>';
show_action('display', 'Display current configuration');
show_action('download', 'Download current configuration');
if (!$fail_dir) {
show_action('save', 'Save current configuration');
show_action('load', 'Load saved configuration');
}
echo '<hr class="separator" />';
show_action('addserver', 'Add server configuration');
$servers = get_server_selection();
if (!empty($servers)) {
show_action('deleteserver', 'Delete this server', $servers);
show_action('editserver', 'Edit this server', $servers);
}
echo '<hr class="separator" />';
show_action('main', 'Display overview');
show_action('servers', 'Display servers');
echo '</p>';
footer();
?>