<?php
/************************************************************************
* *
* Copyright (C) 2001 Stuart Reeves *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* The GNU General Public License is available from: *
* http://www.gnu.org/copyleft/gpl.html *
* *
************************************************************************/
/* Stuart's lns (lame node system) */
/* Basic initialisation script creates the lns database and tables. Currently, the authentication
* information and node tables are created. Reserved nodes are then inserted into the node
* table. */
require_once("../include/header.php.inc");
require_once("../include/Auth.php");
require_once("../include/Util.php");
/****************************************************
* Create database and tables *
****************************************************/
echo "<html bgcolor=#fafafa>
<head><title>lns initialisation</title></head>
<body>
Running init script...<br /><br />";
$link = mysql_connect(HOSTNAME, USERNAME, PASSWORD);
// Create the database
if (!mysql_select_db(DB_NAME, $link)) {
mysql_create_db(DB_NAME, $link);
echo "<i>Created database \"" . DB_NAME . "\".</i><br />";
} else
echo "NOTE: Database \"" . DB_NAME . "\" already exists.<br />";
// Create the table(s)
$tablelist = mysql_list_tables(DB_NAME, $link);
$i = 0;
$nodeExists = false;
$authExists = false;
// See if NODE_TABLE already exists
while ($i < mysql_num_rows($tablelist)) {
$name = mysql_tablename($tablelist, $i);
if ($name == NODE_TABLE)
$nodeExists = true;
else if ($name == AUTHINFO_TABLE)
$authExists = true;
++$i;
}
createTable(CREATE_NODE_TABLE, NODE_TABLE, $nodeExists);
createTable(CREATE_AUTHINFO_TABLE, AUTHINFO_TABLE, $authExists);
function createTable($query, $table, $exists) {
if (!$exists) {
mysql_query($query);
echo "<i>Created table \"$table\".</i><br />";
} else
echo "NOTE: Table \"$table\" already exists.<br />";
}
/****************************************************
* Create hidden nodes *
****************************************************/
// Create the hidden (i.e. reserved) nodes based on an array of names in $HIDDEN_NODES. The
// hidden nodes have a username set to NULL so that no edits can be made.
$q_all_nodes = mysql_query("SELECT * FROM " . NODE_TABLE);
if (mysql_num_rows($q_all_nodes) < sizeof($HIDDEN_NODES)) {
// Delete ALL rows, i.e. clear out any debris
mysql_query("DELETE FROM " . NODE_TABLE);
global $HIDDEN_NODES;
foreach ($HIDDEN_NODES as $hNode) {
mysql_query("INSERT INTO " . NODE_TABLE .
" VALUES (\"$hNode\",
\"\",
NULL,
\"\",
\"\",
\"\",
\"t\",
\"t\",
NULL,
\"" . RAWTYPE_DEFAULT . "\",
\"" . METATYPE_DEFAULT . "\")");
}
echo "<i> Created hidden nodes.</i><br />";
} else if (isset($_POST["_setroot"]) && isset($_POST["_root"])) {
// Create the root user if it does not exist, otherwise ensure that they are marked as
// a superuser.
$dbquery = mysql_query("SELECT * FROM ". AUTHINFO_TABLE . " WHERE username = '" .
$_POST["_root"] . "'");
if (mysql_num_rows($dbquery) >= 1) {
mysql_query("UPDATE " . AUTHINFO_TABLE . " SET superuser = 't'
WHERE username = '" . $_POST["_root"] . "'");
echo "Set user <strong>" . $_POST["_root"] . "</strong> to superuser<br />";
} else {
$auth = new Auth(HOSTNAME, USERNAME, PASSWORD, DB_NAME, AUTHINFO_TABLE);
$password = $auth->createUser($_POST["_root"], true);
echo "Created user <strong>" . $_POST["_root"] . "</strong>, password is: $password<br />";
}
mysql_query("UPDATE " . NODE_TABLE . " SET username = '$_root' WHERE hidden = 't'");
echo "Set <strong>" . $_POST["_root"] . "</strong> as owner of hidden nodes<br />";
} else {
echo "NOTE: Hidden nodes already exist.<br />
<form action=\"$PHP_SELF\" method=\"post\" name=\"superuser\">
Admin username: <input type=\"text\" name=\"_root\" value=\"admin\">
<input type=\"submit\" name=\"_setroot\" value=\"Set as admin\">
</form>";
}
if (isset($_POST["_md5passwords"])) {
$dbquery = mysql_query("SELECT username, password FROM " . AUTHINFO_TABLE .
" WHERE LENGTH(password) != 32");
while ($authdata = mysql_fetch_object($dbquery)) {
$md5Password = md5($authdata->password);
mysql_query("UPDATE " . AUTHINFO_TABLE . " SET password = '$md5Password'
WHERE username = '$authdata->username'");
}
echo "Password hashes calculated";
} else {
echo "<form action=\"$PHP_SELF\" method=\"post\" name=\"passwords\">
<input type=\"submit\" name=\"_md5passwords\" value=\"Convert passwords to md5 hashes\">
(Warning: will affect all passwords of < 32 characters in length)
</form>";
}
echo "</body></html>";
mysql_close($link);
?>