1
0
Fork 0
gnuviechadmin-historic/php/setup.php

186 lines
5.9 KiB
PHP

<?php
require("tools.php");
require("usertypes.php");
$dbh=db_open();
if (isset($_POST["mode"]) and $_POST["mode"]=="finish") {
if (isset($_POST["adminpass1"]) and isset($_POST["adminpass2"])
and $_POST["adminpass1"]!=$_POST["adminpass2"]) {
header("Location: setup.php");
exit;
}
}
function create_country_list() {
global $dbh;
$countries = split("\n", $_POST["countries"]);
foreach ($countries as $country) {
$query = "INSERT INTO country (name) VALUES ('{$country}')";
db_exec($dbh, $query);
}
}
function has_countries() {
global $dbh;
$result=db_query($dbh, "SELECT id FROM country");
return (db_num_rows($result)!=0);
}
function country_selbox($varname, $default=0) {
global $dbh;
$result=db_query($dbh, "SELECT * FROM country ORDER BY name");
printf("<SELECT name=\"%s\">", $varname);
while ($row=db_fetch_array($result)) {
printf("<OPTION value=\"%d\"%s>%s</OPTION>",
$row["id"], ($row["id"]==$default) ? " selected" : "",
$row["name"]);
}
print("</SELECT>");
}
function client_selbox($varname, $default=0) {
global $dbh;
$result=db_query($dbh, "SELECT cl.id, cl.firstname, cl.lastname, ".
"cl.town, co.name FROM client cl, country co ".
"WHERE cl.country=co.id ORDER BY cl.firstname, ".
"cl.lastname");
printf("<SELECT name=\"%s\">", $varname);
while ($row=db_fetch_array($result)) {
printf("<OPTION value=\"%d\"%s>%s %s (%s, %s)</OPTION>",
$row["id"], ($row["id"]==$default) ? " selected" : "",
$row["firstname"], $row["lastname"], $row["town"],
$row["name"]);
}
print("</SELECT>");
}
function enter_personal_data() {
global $dbh;
$query = sprintf("INSERT INTO client (firstname, lastname, ".
"address1, country, town, zipcode, state, ".
"active) VALUES ('%s', '%s', '%s', %d, '%s', ".
"'%05d', '%s', true)",
$_POST["firstname"],
$_POST["lastname"],
$_POST["address"],
$_POST["country"],
$_POST["town"],
$_POST["zipcode"],
$_POST["state"]);
db_exec($dbh, $query);
}
function has_personal_data() {
global $dbh;
$result=db_query($dbh, "SELECT id FROM client");
return (db_num_rows($result)!=0);
}
function setup_admin_account() {
global $dbh;
$query = sprintf("INSERT INTO sysuser (name, type, home, shell, ".
"password, client, toupdate, md5pass, sysuid) ".
"VALUES ('%s', %d, '/root', true, '%s', ".
"%d, false, '%s', 0)",
$_POST["adminuser"],
cUSRADMIN,
$_POST["adminpass1"],
$_POST["clientid"],
md5($_POST["adminpass1"]));
db_exec($dbh, $query);
}
function has_admin_account() {
global $dbh;
$query=sprintf("SELECT id FROM sysuser WHERE type=%d",
cUSRADMIN);
$result=db_query($dbh, $query);
return (db_num_rows($result)!=0);
}
include("commonhead.php");
if (!isset($_POST["mode"])) {
$mode = "step1";
} else {
$mode = $_POST["mode"];
}
switch ($mode) :
case "step1":
?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<input type="hidden" name="mode" value="step2">
<? if (!has_countries()) : ?>
<table>
<tr><td><?php echo _("Initial country list (each line one country)"); ?></td><td><textarea name="countries"></textarea></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="submit" value="<?php echo _("generate country list"); ?>"></td></tr>
</table>
<?php
else:
echo _("You already have countries in your database");
?>
<br/><input type="submit" name="submit" value="<?php echo _("Skip to step 2"); ?>">
<?php
endif;
?>
</form>
<?php
break;
case "step2":
if (!has_countries()) create_country_list();
?>
<form action="<?php echo $PHP_SELF; ?>" method="post">
<input type="hidden" name="mode" value="step3">
<? if (!has_personal_data()) :?>
<table>
<tr><td><?php echo _("First name"); ?></td><td><input type="text" name="firstname"></td></tr>
<tr><td><?php echo _("Last name"); ?></td><td><input type="text" name="lastname"></td></tr>
<tr><td><?php echo _("Address"); ?></td><td><input type="text" name="address"></td></tr>
<tr><td><?php echo _("ZIP Code"); ?></td><td><input type="text" name="zipcode"></td></tr>
<tr><td><?php echo _("City/Town/Village"); ?></td><td><input type="text" name="town"></td></tr>
<tr><td><?php echo _("State"); ?></td><td><input type="text" name="state"></td></tr>
<tr><td><?php echo _("Country"); ?></td><td><?php country_selbox("country"); ?></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="submit" value="<?php echo _("Submit personal data"); ?>"></td></tr>
</table>
<? else:
echo _("You already have personal data in your database");
?>
<br/><input type="submit" name="submit" value="<?php echo _("Skip to step 3"); ?>">
<?php
endif;
?>
</form>
<?php
break;
case "step3":
if (!has_personal_data()) enter_personal_data();
?>
<form action="<? echo $PHP_SELF; ?>" method="post">
<input type="hidden" name="mode" value="finish">
<?php if (!has_admin_account()): ?>
<table>
<tr><td><?php echo _("Administrator client account"); ?></td><td><?php client_selbox("clientid"); ?></td></tr>
<tr><td><?php echo _("Administrator username"); ?></td><td><input type="text" name="adminuser"></td></tr>
<tr><td><?php echo _("Administrator password"); ?></td><td><input type="password" name="adminpass1"></td></tr>
<tr><td><?php echo _("Administrator password (repeat)"); ?></td><td><input type="password" name="adminpass2"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="submit" value="<?php echo _("Submit administrator data"); ?>"></td></tr>
</table>
<?php else:
echo _("You already have an admin user in your database");
?>
<br/><input type="submit" name="submit" value="<?php echo _("finish"); ?>">
<?php endif; ?>
</form>
<?php
break;
case "finish":
if (!has_admin_account()) setup_admin_account();
print(_("Congratulations, you are done with the initial setup!"));
printf(_("You may now log in at the \"<a href=\"%s\">Start page</a>\"."), "start.php");
break;
default: ?>
You are trying to trick me. I don't like this
<?php endswitch; ?>
</body>
</html>