Browse Source

moved database init to login.inc.php

Erinn 8 years ago
parent
commit
41e0fb3bdb
9 changed files with 70 additions and 94 deletions
  1. 1 2
      .gitignore
  2. 0 32
      config.inc.php
  3. 0 56
      db/dbinit.sqlite3.script
  4. 1 1
      index.php
  5. 58 0
      login.inc.php
  6. 1 1
      register.php
  7. 6 0
      sample.config.inc.php
  8. 2 1
      solns.ajax.php
  9. 1 1
      todo

+ 1 - 2
.gitignore

@@ -1,3 +1,2 @@
-*.sqlite3
-db/*
+config.inc.php
 

+ 0 - 32
config.inc.php

@@ -1,32 +0,0 @@
-<?php
-
-$db = new SQLite3('db/netsim.sqlite3');
-
-session_set_cookie_params(3600 * 24 * 30);
-session_start();
-$userq = $db->prepare("SELECT * FROM user WHERE name = :name");
-
-if (isset($_POST['username']) && isset($_POST['password'])) {
-	$userq->bindValue(':name', $_POST['username']);
-	$res = $userq->execute();
-
-	if ($res === false) {
-		$login_error = "Username or password incorrect.";
-	} else {
-		$res = $res->fetchArray();
-		if (password_verify($_POST['password'], $res['password'])) {
-			$_SESSION['cs4g_user_id'] = $res['id'];
-			header('Location: ./');
-		} else {
-			$login_error = "Username or password incorrect.";
-		}
-	}
-} else if (isset($_GET['logout'])) {
-	unset($_SESSION['cs4g_user_id']);
-	session_destroy();
-	header('Location: ./');
-}
-
-define('LOGGEDIN', isset($_SESSION['cs4g_user_id']));
-
-?>

+ 0 - 56
db/dbinit.sqlite3.script

@@ -1,56 +0,0 @@
-CREATE TABLE user (
-	id integer PRIMARY KEY,
-	name text,
-	password text
-);
-
-INSERT INTO user (name, password) VALUES ('erinn','$2y$10$n5ajLY.kMZVjLCNsUuPXFO70VUYLoolpQRGl3RCXOBVIaY4/peWXS');
-
-
-CREATE TABLE category (
-	id integer PRIMARY KEY,
-	name text,
-	orderby integer
-);
-
-INSERT INTO category (name, orderby) VALUES
-	('Basics', 1),
-	('Spoofs', 2),
-	('Denial of Service', 3),
-	('Attacks', 4)
-;
-
-CREATE TABLE level (
-	id integer PRIMARY KEY,
-	category_id integer,
-	name text,
-	orderby integer,
-	filename text
-);
-
-INSERT INTO level (category_id, name, orderby, filename) VALUES
-	(1, 'Basics 1', 1, '01 Basics/level01'),
-	(1, 'Basics 2', 2, '01 Basics/level02'),
-	(1, 'Basics 3', 3, '01 Basics/level03'),
-	(1, 'Basics 4', 4, '01 Basics/level04'),
-	(1, 'Basics 5', 5, '01 Basics/level05'),
-
-	(2, 'Spoofs 1', 1, '02 Spoofs/spoofs01'),
-	(2, 'Spoofs 2', 2, '02 Spoofs/spoofs02'),
-	
-	(3, 'DoS 1', 1, '03 DoS/dos01'),
-	(3, 'DoS 2', 2, '03 DoS/dos02'),
-	(3, 'DoS 3', 3, '03 DoS/dos03'),
-
-	(4, 'Attacks 1', 1, '04 Attacks/attacks01'),
-	(4, 'Attacks 2', 2, '04 Attacks/attacks02')
-;
-
-CREATE TABLE solns (
-	id integer PRIMARY KEY,
-	user_id integer,
-	level_id integer,
-	completed integer,
-	json text
-);
-

+ 1 - 1
index.php

@@ -1,6 +1,6 @@
 <?php
 
-require_once 'config.inc.php';
+require_once 'login.inc.php';
 
 if (LOGGEDIN && !isset($_GET['level'])) {
 	include 'listing.inc.php';

+ 58 - 0
login.inc.php

@@ -0,0 +1,58 @@
+<?php
+
+require_once 'config.inc.php';
+
+if (!file_exists(DB_FILE)) {
+	include "header.inc.php";
+	echo "<h2>Netsim installation</h3>\n";
+	echo "<p>No database file was found at ".DB_FILE.", so attempting to create it now...</p>\n";
+
+	try {
+		$db = new SQLite3(DB_FILE);
+		$db->exec("CREATE TABLE user (id integer PRIMARY KEY,name text,password text)");
+		$db->exec("INSERT INTO user (name, password) VALUES ('erinn','$2y$10$n5ajLY.kMZVjLCNsUuPXFO70VUYLoolpQRGl3RCXOBVIaY4/peWXS')");
+		$db->exec("CREATE TABLE category (id integer PRIMARY KEY,name text,orderby integer)");
+		$db->exec("INSERT INTO category (name, orderby) VALUES('Basics', 1),('Spoofs', 2),('Denial of Service', 3),('Attacks', 4)");
+		$db->exec("CREATE TABLE level (id integer PRIMARY KEY,category_id integer,name text,orderby integer,filename text)");
+		$db->exec("INSERT INTO level (category_id, name, orderby, filename) VALUES(1, 'Basics 1', 1, '01 Basics/level01'),(1, 'Basics 2', 2, '01 Basics/level02'),(1, 'Basics 3', 3, '01 Basics/level03'),(1, 'Basics 4', 4, '01 Basics/level04'),(1, 'Basics 5', 5, '01 Basics/level05'),(2, 'Spoofs 1', 1, '02 Spoofs/spoofs01'),(2, 'Spoofs 2', 2, '02 Spoofs/spoofs02'),(3, 'DoS 1', 1, '03 DoS/dos01'),(3, 'DoS 2', 2, '03 DoS/dos02'),(3, 'DoS 3', 3, '03 DoS/dos03'),(4, 'Attacks 1', 1, '04 Attacks/attacks01'),(4, 'Attacks 2', 2, '04 Attacks/attacks02')");
+		$db->exec("CREATE TABLE solns (id integer PRIMARY KEY,user_id integer,level_id integer,completed integer,json text)");
+
+		echo "<p>The database was initialized successfully! <a href=\"./\">Continue...</a></p>\n";
+	} catch (Exception $e) {
+		echo "<p>Failed to create file: ".$e->getMessage()."</p>\n";
+	}
+	
+	include "footer.inc.php";
+	exit();
+}
+
+$db = new SQLite3(DB_FILE);
+
+session_set_cookie_params(3600 * 24 * 30);
+session_start();
+$userq = $db->prepare("SELECT * FROM user WHERE name = :name");
+
+if (isset($_POST['username']) && isset($_POST['password'])) {
+	$userq->bindValue(':name', $_POST['username']);
+	$res = $userq->execute();
+
+	if ($res === false) {
+		$login_error = "Username or password incorrect.";
+	} else {
+		$res = $res->fetchArray();
+		if (password_verify($_POST['password'], $res['password'])) {
+			$_SESSION['cs4g_user_id'] = $res['id'];
+			header('Location: ./');
+		} else {
+			$login_error = "Username or password incorrect.";
+		}
+	}
+} else if (isset($_GET['logout'])) {
+	unset($_SESSION['cs4g_user_id']);
+	session_destroy();
+	header('Location: ./');
+}
+
+define('LOGGEDIN', isset($_SESSION['cs4g_user_id']));
+
+?>

+ 1 - 1
register.php

@@ -1,6 +1,6 @@
 <?php
 
-require_once 'config.inc.php';
+require_once 'login.inc.php';
 
 if (isset($_POST['reg_username']) && isset($_POST['reg_password'])) {
 	$userq->bindValue(':name', $_POST['reg_username']);

+ 6 - 0
sample.config.inc.php

@@ -0,0 +1,6 @@
+<?php
+
+// database file for user accounts and level info. should be writable by the webserver.
+define('DB_FILE', '/var/www-data/netsim.sqlite3');
+
+?>

+ 2 - 1
solns.ajax.php

@@ -1,6 +1,7 @@
 <?php
 
-require_once 'config.inc.php';
+require_once 'login.inc.php';
+
 if (!LOGGEDIN || !isset($_GET['method']) || !isset($_GET['level'])) exit('[]');
 $l = (int)$_GET['level'];
 if ($l < 0) exit('lol oops');

+ 1 - 1
todo

@@ -1,10 +1,10 @@
 blocking
 - update "you win" dialog to handle no nextLevel property
 - prettier level listing
-- resize game on resize viewport (attempt at this at top of ui.js but it's laggy)
 - method to work on test levels
 
 nice to have
+- resize game on resize viewport (attempt at this at top of ui.js but it's laggy)
 - screenshots for level selector?
 - differentiate launcher buttons
 - coloured packets