<html>
<head><title>Installer Page STEP 1</title></head>
<body>
<?php 

//------------------------------------------------------------------------------
// Define Script Version
//------------------------------------------------------------------------------
$version = "1.0.0";
//------------------------------------------------------------------------------


echo "INSTALLING...<br>";

// Aufbau der Datenbankverbindung 
@$y = mysql_connect($_REQUEST["dbserv"],$_REQUEST["dbuser"],$_REQUEST["dbpwd"]);
$sql = 'CREATE DATABASE IF NOT EXISTS ' . $_REQUEST["dbname"];
if (mysql_query($sql, $y))
{
	echo "Datenbank erfolgreich angelegt! [<b><font color=lime>OK</b></font>]<br>\n";
	$config = "<?php 
					\$MYSQL_HOST=\"".$_REQUEST["dbserv"]."\"; 
					\$MYSQL_USER=\"".$_REQUEST["dbuser"]."\"; 
					\$MYSQL_PASS=\"".$_REQUEST["dbpwd"]."\"; 
					\$MYSQL_DATABASE=\"".$_REQUEST["dbname"]."\";?>";
	$dateiname = "../Config/_dbconfig_.php";
	$handler = fOpen($dateiname , "w");
	fWrite($handler , $config);
	fClose($handler);
 	
	$config = "user: ".$_REQUEST["dbuser"]. 
			  "\npasswd: ".$_REQUEST["dbpwd"].
			  "\ndatabase: ".$_REQUEST["dbname"].
			  "\nhost: ".$_REQUEST["dbserv"];
	$dateiname = $_REQUEST["vpndir"]."/database";
	$handler = fOpen($dateiname , "w");
	fWrite($handler , $config);
	fClose($handler); 
	exec("chmod 777 ".$dateiname);
	
	echo "Konfiguration erfolgreich angelegt! [<b><font color=lime>OK</b></font>]<br>\n";
	include("../Config/_dbconfig_.php");
} 
else 
{
	die("Fehler beim Anlegen der Datenbank! [<b><font color=red>FAIL</b></font>]<br>");
}

@$x = mysql_select_db($MYSQL_DATABASE);
if (empty($x)) 
{ 
  die ("Keine Verbindung zur Datenbank! [<b><font color=red>FAIL</b></font>]<br>"); 
} 

// MYSQL QUERYS....
mysql_query ("
CREATE TABLE userdata (
  Id Int(11) NOT NULL auto_increment,
  IsAdmin Int(1) NOT NULL default 0,
  VPNId VarChar(2) NOT NULL default '-',
  Nickname VarChar(50) NOT NULL default '',
  Kennwort VarChar(50) NOT NULL default '',
  Nachname VarChar(50) NOT NULL default '',
  Vorname VarChar(50) NOT NULL default '',
  Email VarChar(100) NOT NULL default '',
  Avatar Int(1) NOT NULL default 0,
  NovLogin VarChar(50) NOT NULL default '',
  NovPass VarChar(50) NOT NULL default '',
  PRIMARY KEY (Id)
) 
");

mysql_query ("
CREATE TABLE `connections` (
  `id` int(255) unsigned NOT NULL auto_increment,
  `start` int(14) unsigned NOT NULL default '0',
  `stop` int(14) unsigned NOT NULL default '0',
  `ip` varchar(15) default NULL,
  `local_ip` varchar(15) default NULL,
  `remote_ip` varchar(15) default NULL,
  `client` varchar(255) NOT NULL default '',
  `sent` bigint(255) NOT NULL default '0',
  `received` bigint(255) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=ascii;
");

mysql_query ("
CREATE TABLE `month` (
  `id` int(255) unsigned NOT NULL auto_increment,
  `month` int(2) unsigned NOT NULL default '0',
  `year` int(5) unsigned NOT NULL default '0',
  `sent` bigint(255) unsigned NOT NULL default '0',
  `received` bigint(255) unsigned NOT NULL default '0',
  `client` varchar(255) NOT NULL default '',
  `time` int(255) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=ascii;
");

mysql_query ("
CREATE TABLE `year` (
  `id` int(255) unsigned NOT NULL auto_increment,
  `year` int(5) unsigned NOT NULL default '0',
  `sent` bigint(255) unsigned NOT NULL default '0',
  `received` bigint(255) unsigned NOT NULL default '0',
  `client` varchar(255) NOT NULL default '',
  `time` int(255) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=ascii;
");

mysql_query ("
CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`avatar` varchar(65) NOT NULL default '0',
`closed` varchar(65) NOT NULL default '0',
`category` varchar(65) NOT NULL default '0',
`w_adminonly` varchar(65) NOT NULL default '0',
`w_guest` varchar(65) NOT NULL default '0',
`r_adminonly` varchar(65) NOT NULL default '0',
`r_guest` varchar(65) NOT NULL default '0',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
`lastupdate` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
");

mysql_query ("
CREATE TABLE `forum_answer` (
`question_id` int(4) NOT NULL default '0',
`a_id` int(4) NOT NULL default '0',
`a_name` varchar(65) NOT NULL default '',
`a_avatar` varchar(65) NOT NULL default '',
`a_answer` longtext NOT NULL,
`a_datetime` varchar(25) NOT NULL default '',
`a_attachment` varchar(25) NOT NULL default '',
KEY `a_id` (`a_id`)
) TYPE=MyISAM;
");

mysql_query ("
CREATE TABLE `config` (
`id` int(254) NOT NULL auto_increment,
`attr` varchar(100) NOT NULL default '',
`value` varchar(500) NOT NULL default '',
`extra` varchar(100) NOT NULL default 'NULL',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
");

// Definition des standard Benutzer 
$benutzer[0]["IsAdmin"] =1; 
$benutzer[0]["VPNId"] = "-"; 
$benutzer[0]["Nickname"] =$_REQUEST["name"]; 
$benutzer[0]["Kennwort"] = $_REQUEST["pwd"]; 
$benutzer[0]["Nachname"] = "Admin"; 
$benutzer[0]["Vorname"] = "Server"; 
$benutzer[0]["Email"] = $_REQUEST["email"]; 

// Zuerst alle Datensätze löschen um keine Dopplungen zu bekommen. 
mysql_query ("DELETE FROM userdata"); 

// Daten eintragen 
while (list ($key, $value) = each ($benutzer)) 
{ 
  // SQL-Anweisung erstellen 
  $sql = "INSERT INTO ".
    "userdata (IsAdmin, VPNId, Nickname, Kennwort, Nachname, Vorname, Email) ".
  "VALUES ('".$value["IsAdmin"]."','".
				$value["VPNId"]."', '".
				$value["Nickname"]."', '".
                md5 ($value["Kennwort"])."', '".
                $value["Nachname"]."', '".
                $value["Vorname"]."', '". 
				$value["Email"]."')"; 
  mysql_query ($sql); 

  if (mysql_affected_rows ($y) > 0) 
  { 
    echo "Benutzer erfolgreich angelegt! [<b><font color=lime>OK</b></font>]<br>\n"; 
	echo "
	<form action=\"install2.php\" method=\"post\"> 
	<table>
		<br>
		<tr>
			<td>Sitename:</td>
			<td><input type=\"text\" name=\"sitename\" size=\"50\" value=\"SchulVPN\"><td>
		</tr>
		<tr>
			<td>Sitesubname</td>
			<td><input type=\"text\" name=\"subname\" size=\"50\" value=\"Surf Secure\"><td>
		</tr>
		<tr>
			<td>Siteinfo (Header Marquee, empty = no marquee) </td>
			<td><input type=\"text\" name=\"siteinfo\" size=\"50\"><td>
		</tr>
		<tr>
            <td>Theme:</td>
            <td>
                <select name=\"sitetheme\" size=1>       ";
				// Öffnet ein Unterverzeichnis mit dem Namen "daten"
				$verzeichnis = openDir($_SERVER["DOCUMENT_ROOT"]."/Themes");
				// Verzeichnis lesen
				while ($file = readDir($verzeichnis)) 
				{
					 // Höhere Verzeichnisse nicht anzeigen!
				 	if ($file != "." && $file != "..") 
					{
						$filecut = strpos($file, ".");
						if($filecut > 0)
						{
							$file = substr($file,0,$filecut);
							echo "<option value=\"".$file."\">".$file."</option>";
						}						
				 	}
				}
				 // Verzeichnis schließen
				closeDir($verzeichnis); 

echo "
				</select>
            </td>
        </tr> 
		<tr>
			<td>OpenVPN Config Directory (WITHOUT ending /):</td>
			<td><input type=\"text\" name=\"ovpnconfdir\" size=\"50\" value=\"".$_REQUEST["vpndir"]."\"><td>
		</tr>
        <tr>
			<td>OpenVPN Config File Name:</td>
			<td><input type=\"text\" name=\"ovpnconffile\" size=\"50\" value=\"server.conf\"><td>
		</tr>
		<tr>
			<td>OpenVPN Key Directory (WITHOUT ending /):</td>
			<td><input type=\"text\" name=\"ovpnkeydir\" size=\"50\" value=\"/etc/openvpn/easy-rsa2\"><td>
		</tr>
		<tr>
			<td>OpenVPN Additional Download Directory (e.g. for client.ovpn, ca.crt, readme ...) (WITHOUT ending /):</td>
		    <td><input type=\"text\" name=\"publicdown\" value=\"/var/www/publicdown\"></td>
		</tr>
		<tr>
			<td>Download Directory (for Explorer, WITHOUT ending /):</td>
			<td><input type=\"text\" name=\"downdir\" size=\"50\" value=\"/home/explorer\"><td>
		</tr>
		<tr>
			<td>Enable Mail Support:</td>
		    <td>
				<select name=\"withmail\" size=1>
					<option value=\"1\">Yes</option>
					<option value=\"0\">No</option>
				</select> 
			</td>
		</tr>
		<tr>
			<td>SMTP Server:</td>
			<td><input type=\"text\" name=\"mserv\" size=\"50\" value=\"smtp.live.com\"><td>
		</tr>
		<tr>
			<td>SMTP User:</td>
			<td><input type=\"text\" name=\"muser\" size=\"50\" value=\"user@live.com\"><td>
		</tr>
		<tr>
			<td>SMTP Password:</td>
			<td><input type=\"password\" name=\"mpass\" size=\"50\"><td>
		</tr>
		<tr>
			<td>SMTP Sender Mail-Address:</td>
			<td><input type=\"text\" name=\"mname\" size=\"50\" value=\"noreply@schulvpn.org\"><td>
		</tr>
		<tr>
			<td>Enable Squid Support:</td>
		    <td>
				<select name=\"withsquid\" size=1>
					<option value=\"1\">Yes</option>
					<option value=\"0\">No</option>
				</select> 
			</td>
		</tr>
		<tr>
			<td>SQUID Config Directory (WITHOUT ending /):</td>
			<td><input type=\"text\" name=\"sqdir\" size=\"50\" value=\"/etc/squid\"><td>
		</tr>
		<tr>
			<td>SQUID Config File Name:</td>
			<td><input type=\"text\" name=\"sqconf\" size=\"50\" value=\"squid.conf\"><td>
		</tr>
		<tr>
			<td>SQUID Dynamic Directory (WIHTOUT ending /):</td>
			<td><input type=\"text\" name=\"sqddir\" size=\"50\" value=\"/var/sqd\"><td>
		</tr>
		<tr>
			<td><input type=\"hidden\" name=\"admin\" size=\"50\" value=\""; echo $benutzer[0]["Nickname"]; echo "\"><td>
		</tr>
		<tr>
			<td><input type=\"hidden\" name=\"adress\" size=\"50\" value=\""; echo $_SERVER['HTTP_HOST']; echo "\"><td>
		</tr>
		<tr>
			<td><input type=\"hidden\" name=\"version\" size=\"50\" value=\""; echo $version; echo "\"><td>
		</tr>
	</table>
	<input type=\"submit\" value=\"Install! STEP 2\">
	</form> ";

  } 
  else 
  { 
   echo "Fehler beim Anlegen der Benutzer. [<b><font color=red>FAIL</b></font>]<br>\n"; 
  } 
} 
?>

</body>
</html> 
