Location: PHPKode > projects > ezStats for XBOX Live > ezStats2_xbl/install/index.php
<?php
	if (true) error_reporting (E_ALL|E_STRICT); else error_reporting (0);
	if (!version_compare(PHP_VERSION, '5.2.0', '>=')) die ("You get this error message because your webserver supports PHP 4 only. But ezStats requires PHP version 5.2 or higher. Please contact your webhost and let update your webpackage.");
	function get_template($template) { return str_replace("\"", "\\\"", file_get_contents("templates/".$template.".html")); }
	
	require '../plugins/phpQuery.php';
	require '../classes/settings.php';
	require '../classes/mysql.php';
	require '../classes/security.php';
	require '../classes/functions.php';
	
	$config = array();
	$config['version'] = "1.03";
	$config['sqlfile'] = "../tmp/sql.php";
	$config['defaults'] = "../tmp/defaults.js";
	$config['chmod_test'] = Array("Folder 'tmp'" => "../tmp");
	$config['server_test'] = Array("json" => "JSON", "curl" => "cURL", "gd" => "GD library");
	$config['disable_test'] = Array("curl_init", "curl_setopt", "curl_exec", "curl_getinfo", "curl_close", "json_decode", "file_get_contents", "mb_eregi");
	
	
	// Startseite anzeigen
	if (!isset($_POST['step'])) {
		// Start der Installations-Routine
		eval ("\$welcome = \"".get_template("welcome")."\";");
		$welcome = phpQuery::newDocumentHTML($welcome);
		
		eval ("\$index = \"".get_template("_index")."\";");
		$index = phpQuery::newDocumentHTML($index);
		
		pq('#content')->append($welcome);
		pq('#button a')->attr("href", "chmod")->text("Next");
		die ($index);
	} 
	
	
	// CHMOD-Test, SQL-File erstellen, SQL-Formular generieren
	if ($_POST['step'] == "chmod") {
		// Check der Dateien und Ordner aus $config['chmod_test'], ob sie CHMOD 777 haben
		$check = true;
		$list = phpQuery::newDocumentHTML('<ul></ul>');
		
		foreach ($config['chmod_test'] as $key => $value) {
			if (is_writable($value)) {
				pq('ul')->append('<li>- '.$key.'&nbsp;&nbsp;&nbsp;... OK!</li>');
			} else {
				pq('ul')->append('<li>- '.$key.'&nbsp;&nbsp;&nbsp;... FAILED!</li>');
				$check = false;
			}
		}
		
		// Check der Datei "sql.php" wenn vorhanden
		if (file_exists($config['sqlfile'])) {
			if (is_writable($config['sqlfile'])) {
				pq('ul')->append('<li>- File \''.$config['sqlfile'].'\'&nbsp;&nbsp;&nbsp;... OK!</li>');
			} else {
				pq('ul')->append('<li>- File \''.$config['sqlfile'].'\'&nbsp;&nbsp;&nbsp;... FAILED!</li>');
				$check = false;
			}
		}
		
		// CHMOD-Test ist fehlgeschlagen
		if (!$check) {
			eval ("\$html = \"".get_template("chmod")."\";");
			$html = phpQuery::newDocumentHTML($html);
			pq("#list")->append($list);
			
			$result = Array(
				"html" => $html->__toString(),
				"step" => "chmod",
				"button" => "retry",
				"error" => ""
			);
			die (json_encode($result));
		}
		
		
		// SQL-File existiert nicht; wird mit Standardwerten gefüllt
		if (!file_exists($config['sqlfile'])) {
			$handle = fopen($config['sqlfile'], "w") 
				or die (json_encode(Array(
					"html" => "",
					"step" => "chmod",
					"button" => "retry",
					"error" => "Error: Failure during creation of file sql.php"
				)));
			
			fwrite($handle, '<?php define (\'SQL\', \' {"debug": "0", "sqldb": "ezstats2", "sqlhost": "localhost", "sqluser": "root", "sqlpwd": "", "prefix": "ez2xbl_"} \'); ?>');
			fclose($handle);
		}
		
		
		// Auslesen der SQL-Daten und Erstellen des Formulars
		require ($config['sqlfile']);
		$sqldata = json_decode(SQL);
		
		eval ("\$html = \"".get_template("sqldata")."\";");
		$html = phpQuery::newDocumentHTML($html);
		
		$result = Array(
			"html" => $html->__toString(),
			"step" => "sqldata",
			"button" => "next",
			"error" => ""
		);
		die (json_encode($result));
	}
	
	
	// SQL-File schreiben, Webspace testen
	if ($_POST['step'] == "sqldata") {
		// SQL-Daten in die Datei "sql.php" schreiben, wenn das Forumular mit den Werten übergeben wurde
		if (isset($_POST['input']['sqlhost'])) {
			// Prüfen ob eine Verbindung mit der DB aufgenommen werden kann
			$connect  = @mysql_connect($_POST['input']['sqlhost'], $_POST['input']['sqluser'],$_POST['input']['sqlpwd']);
			$select   = @mysql_select_db($_POST['input']['sqldb'], $connect);
			
			
			// Bei fehlgeschlagenem Test zurück zum SQL-Eingabeformular samt Fehlermeldung
			if (!$connect OR !$select) {
				$result = Array(
					"html" => "",
					"step" => "sqldata",
					"button" => "retry",
					"error" => "Could not connect to database. Please check your values."
				);
				die (json_encode($result));
			} else mysql_close($connect);
			
			
			// Schreiben der SQL-Daten in die Datei sql.php
			$handle = fopen($config['sqlfile'], "w") 
				or die (json_encode(Array("html" => "Error: Failure during creation of file sql.php")));
			
			fwrite($handle, '<?php define (\'SQL\', \' {"debug": "0", "sqldb": "'.$_POST['input']['sqldb'].'", "sqlhost": "'.$_POST['input']['sqlhost'].'", "sqluser": "'.$_POST['input']['sqluser'].'", "sqlpwd": "'.$_POST['input']['sqlpwd'].'", "prefix": "'.$_POST['input']['prefix'].'"} \'); ?>');
			fclose($handle);
		}
		
		
		// Test des Webspace/Servers ob er die nötigen Eigenschaften erfüllt
		$check = true; $test="";
		$extensions = get_loaded_extensions();
		$list = phpQuery::newDocumentHTML('<ul></ul>');
		
		if (!version_compare(PHP_VERSION, '5.2.0', '>=')) $check = false;
		pq('ul')->append('<li>- Needed PHP-Version: 5.2.0 - Your PHP-Version: '.PHP_VERSION.'</li>');
		
		foreach ($config['server_test'] as $key => $value) {
			if (in_array($key, $extensions)) {
				pq('ul')->append('<li>- Extension '.$value.'&nbsp;&nbsp;&nbsp;... INSTALLED!</li>');
			} else {
				pq('ul')->append('<li>- Extension '.$value.'&nbsp;&nbsp;&nbsp;... NOT INSTALLED!</li>');
				$check = false;
			}
		}
		
		foreach ($config['disable_test'] as $function) {
			if (function_exists($function)) {
				pq('ul')->append('<li>- Function '.$function.'&nbsp;&nbsp;&nbsp;... ENABLED!</li>');
			} else {
				pq('ul')->append('<li>- Function '.$function.'&nbsp;&nbsp;&nbsp;... DISABLED!</li>');
				$check = false;
			}
		}
		
		
		// Server-Test ist fehlgeschlagen
		if (!$check) {
			eval ("\$html = \"".get_template("server")."\";");
			$html = phpQuery::newDocumentHTML($html);
			pq("p:last")->append($list);
			
			$result = Array(
				"html" => $html->__toString(),
				"step" => "sqldata",
				"button" => "retry",
				"error" => ""
			);
			die (json_encode($result));
		}
		
		
		// Server-Test ist gelungen. Weiter mit der DB-Installation
		eval ("\$html = \"".get_template("ready")."\";");
		
		$result = Array(
			"html" => $html,
			"step" => "tables",
			"button" => "... run the install",
			"error" => ""
		);
		die (json_encode($result));
	}
	
	
	// Erstellen der Datenbanktabellen, Standardsettings schreiben, Adminkonto-Formular generieren
	if ($_POST['step'] == "tables") {
		require $config['sqlfile'];
		function get_query($file, $prefix) { return str_replace("#PREFIX#", $prefix, file_get_contents("queries/".$file.".sql")); }
		
		$sql = new MySQL();
		$sql->debug = true;
		$secure = new Security();
		
		
		// Anlegen der Datenbanktabellen
		if (true) {
			// Anlegen der Tabellen (v1.0)
			$sql->query(get_query("localization_01", $sql->prefix));
			$sql->query(get_query("localization_02", $sql->prefix));
			$sql->query(get_query("localization_03", $sql->prefix));
			$sql->query(get_query("localization_04", $sql->prefix));
			$sql->query(get_query("overview", $sql->prefix));
			$sql->query(get_query("player", $sql->prefix));
			$sql->query(get_query("plugins_01", $sql->prefix));
			$sql->query(get_query("plugins_02", $sql->prefix));
			$sql->query(get_query("plugins_03", $sql->prefix));
			$sql->query(get_query("settings", $sql->prefix));
			$sql->query(get_query("signatures", $sql->prefix));
			$sql->query(get_query("style", $sql->prefix));
			$sql->query(get_query("users", $sql->prefix));
		}
		
		
		// Speichern der Standardsettings (automatisiert)
		if ($defaults = json_decode(@file_get_contents($config['defaults']))) {
			foreach ($defaults as $table => $presets) {
				foreach ($presets as $name => $value) {
					$sql->query('SELECT value FROM '.$sql->prefix . $table.' WHERE name = "'.$name.'"');
					if (!$sql->count()) $sql->query('INSERT INTO '.$sql->prefix . $table.' (name, value) VALUES ("'.$name.'", "'.$value.'")');
				}
			}
		} else {
			$result = json_encode(Array(
				"html" => "",
				"step" => "tables",
				"button" => "retry",
				"error" => "Error: Failure during loading of file defaults.js"
			));
			die ($result);
		}
		
		
		// Speichern der Standardsettings (manuell)
		$sql->query('UPDATE '.$sql->prefix.'settings SET value = "'.$config['version'].'" WHERE name = "version"');
		$sql->query('REPLACE INTO '.$sql->prefix.'player (id) VALUES (1)');
		
		
		// Abrufen und Speichern von IDENT und KEY für den mohwstats-Server
		if (function_exists('curl_init')) {
			$sql->query('SELECT value FROM '.$sql->prefix .'settings WHERE name = "cms"');        $cms = $sql->fetchRow("value");
			$sql->query('SELECT value FROM '.$sql->prefix .'settings WHERE name = "signatures"'); $sig = $sql->fetchRow("value");
			
			// Telemetrie-Daten
			$data = Array(
				"test"      => "curl",
				"game"      => "xbl", // Dies auch in "update.php" ändern!
				"version"   => $config['version'],
				"cms"       => $cms,
				"sig"       => $sig,
				"baseurl"   => $_SERVER['HTTP_HOST'],
				"scripturl" => $_SERVER['HTTP_HOST']."/".str_replace($_SERVER['DOCUMENT_ROOT'], "", $_SERVER['SCRIPT_FILENAME'])
			);
			
			$ch = curl_init("http://curl.ezstats.org/");
			curl_setopt($ch, CURLOPT_POST, true); 
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
			$data = curl_exec($ch); curl_close($ch);
			$data = explode('||', $data);
			
			if (isset($data[0])) $sql->query('UPDATE '.$sql->prefix.'settings SET value = "'.$data[0].'" WHERE name = "ident"');
			if (isset($data[1])) $sql->query('UPDATE '.$sql->prefix.'settings SET value = "'.$data[1].'" WHERE name = "key"');
		} else die("Error: cURL-function required to proceed");
		
		
		// Formular erzeugen für das Adminkonto
		eval ("\$html = \"".get_template("admin")."\";");
		$html = phpQuery::newDocumentHTML($html);
		
		$sql->query('SELECT * FROM '.$sql->prefix.'users WHERE id = 1');
		if ($sql->count()) {
			$row = $sql->fetchRow();
			pq('#name')->val($row['name']);
			pq('#password')->val($row['password']);
		}
		
		$result = Array(
			"html" => $html->__toString(),
			"step" => "admin",
			"button" => "next",
			"error" => ""
		);
		die (json_encode($result));
	}
	
	
	// Adminkonto anlegen, Settings-Formular generieren
	if ($_POST['step'] == "admin") {
		require $config['sqlfile'];
		$sql = new MySQL();
		$secure = new Security();
		$func = new Functions();
		
		
		// Speichern des Adminaccounts, wenn Formulardaten übergeben wurden
		if (isset($_POST['input']['name'])) {
			// Wurde für Name ein Wert angegeben?
			if ($_POST['input']['name'] == "") {
				$result = Array(
					"html" => "",
					"step" => "admin",
					"button" => "retry",
					"error" => "Error: Please enter a username!"
				);
				die (json_encode($result));
			} 
			else {
				// Existiert User mit ID=1?
				$sql->query('SELECT * FROM '.$sql->prefix.'users WHERE id = 1');
				if ($sql->count()) {
					// Sind die Namen identisch?
					$user = $sql->fetchRow();
					if ($user['name'] == $_POST['input']['name']) {
						// Sind die Passwörter identisch?
						if ($user['password'] == $_POST['input']['password']) {
							// Nichts machen, weiter mit dem nächsten Schritt
						} else {
							// Passwort abspeichern mit md5()
							$sql->query('UPDATE '.$sql->prefix.'users SET password = "'.md5($_POST['input']['password']).'" WHERE id = 1');
						}
					} else {
						// Existiert der User mit dem Namen schon?
						$sql->query('SELECT id FROM '.$sql->prefix.'users WHERE name = "'.$_POST['input']['name'].'"');
						if ($sql->count()) {
							// Diesen User vorher löschen
							$user = $sql->fetchRow();
							$sql->query('DELETE FROM '.$sql->prefix.'users WHERE id = "'.$user['id'].'"');
						}
						
						// Den Datensatz ID=1 überschreiben
						$sql->query('
							UPDATE '.$sql->prefix.'users SET 
								name = "'.$_POST['input']['name'].'", 
								password = "'.md5($_POST['input']['password']).'"
							WHERE id = 1
						');
					}
				} else {
					// Den Account so abspeichern
					$sql->query('INSERT INTO '.$sql->prefix.'users (
						name, 
						password, 
						adminpower
					) VALUES (
						"'.$_POST['input']['name'].'", 
						"'.md5($_POST['input']['password']).'",
						1
					)');
				}
			}
		}
		
		
		// Formular erzeugen für die Settings
		if (true) {
			// Array mit allen unterstützten Sprachen erzeugen
			$languages = Array();
			$sql->query('SHOW COLUMNS FROM '.$sql->prefix.'localization');
			
			while ($row = $sql->fetchRow()) {
				if ($row['Field'] != "id" AND $row['Field'] != "category" AND $row['Field'] != "wildcard") 
				$languages[] = $row['Field'];
			}
		}
		
		eval ("\$html = \"".get_template("settings")."\";");
		$html = phpQuery::newDocumentHTML($html);
		
		// Gespeicherte Settings in das Formular einfügen
		if (true) {
			// Clanname
			$sql->query('SELECT value FROM '.$sql->prefix.'settings WHERE name = "clanname"');
			$result = $sql->fetchRow();
			pq('#clanname')->val($result['value']);
			
			// Clanpage
			$sql->query('SELECT value FROM '.$sql->prefix.'settings WHERE name = "clanpage"');
			$result = $sql->fetchRow();
			pq('#clanpage')->val($result['value']);
			
			// Plattform
			$sql->query('SELECT value FROM '.$sql->prefix.'settings WHERE name = "platform"');
			$result = $sql->fetchRow();
			pq('input[name=platform][value='.$result['value'].']')->attr('checked', 'checked');
			
			// Sprache
			$sql->query('SELECT value FROM '.$sql->prefix.'settings WHERE name = "language"');
			$result = $sql->fetchRow();
			foreach ($languages as $lang) {
				if ($lang == $result['value'])
					 pq('<option/>')->text($lang)->attr('selected', 'selected')->appendTo('#language');
				else pq('<option/>')->text($lang)->appendTo('#language');
			}
			
			// Zeitzone
			$sql->query('SELECT value FROM '.$sql->prefix.'settings WHERE name = "timezone"');
			$result = $sql->fetchRow();
			pq('#timezone')->append($func->select_timezone($result['value']));
		}
		
		$result = Array(
			"html" => $html->__toString(),
			"step" => "settings",
			"button" => "next",
			"error" => ""
		);
		die (json_encode($result));
	}
	
	
	// Settings abspeichern, Ergebnisscreen anzeigen
	if ($_POST['step'] == "settings") {
		require $config['sqlfile'];
		$sql = new MySQL();
		$secure = new Security();
		
		
		// Speichern der Settings aus dem Formular
		if (isset($_POST['input'])) {
			foreach ($_POST['input'] as $name => $value) {
				$sql->query('UPDATE '.$sql->prefix.'settings SET value = "'.$value.'" WHERE name = "'.$name.'"');
			}
		}
		
		
		// Ergebnisscreen erzeugen
		$sql->query('SELECT name FROM '.$sql->prefix.'users WHERE id = "1"');
		$result = $sql->fetchRow(); $adminname = $result['name'];
		
		eval ("\$html = \"".get_template("done")."\";");
		$html = phpQuery::newDocumentHTML($html);
		
		$result = Array(
			"html" => $html->__toString(),
			"step" => "settings",
			"button" => "Log into the adminpanel",
			"error" => "",
			"href" => "../admin/"
		);
		die (json_encode($result));
	}
?>




Return current item: ezStats for XBOX Live