login.inc.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. require_once 'config.inc.php';
  3. if (!file_exists(DB_FILE)) {
  4. include "header.inc.php";
  5. echo "<h2>Netsim installation</h3>\n";
  6. echo "<p>No database file was found at ".DB_FILE.", so attempting to create it now...</p>\n";
  7. try {
  8. $db = new SQLite3(DB_FILE);
  9. $db->exec("CREATE TABLE user (id integer PRIMARY KEY,name text,password text)");
  10. $db->exec("INSERT INTO user (name, password) VALUES ('erinn','$2y$10$n5ajLY.kMZVjLCNsUuPXFO70VUYLoolpQRGl3RCXOBVIaY4/peWXS')");
  11. $db->exec("CREATE TABLE category (id integer PRIMARY KEY,name text,orderby integer)");
  12. $db->exec("INSERT INTO category (name, orderby) VALUES('Basics', 1),('Spoofs', 2),('Denial of Service', 3),('Attacks', 4)");
  13. $db->exec("CREATE TABLE level (id integer PRIMARY KEY,category_id integer,name text,orderby integer,filename text)");
  14. $db->exec("INSERT INTO level (category_id, name, orderby, filename) VALUES(1, 'Getting started', 1, '01 Basics/level01'),(1, 'Packet fields', 2, '01 Basics/level02'),(1, 'Ping', 3, '01 Basics/level03'),(1, 'Routing', 4, '01 Basics/level04'),(1, 'Modems', 5, '01 Basics/level05'),(1, 'Encryption', 6, '01 Basics/level06'),(2, 'IP Spoofing', 1, '02 Spoofs/spoofs01'),(2, 'Stealing packets', 2, '02 Spoofs/spoofs02'),(3, 'Basic DoS', 1, '03 DoS/dos01'),(3, 'Distributed DoS', 2, '03 DoS/dos02'),(3, 'Smurf attack', 3, '03 DoS/dos03'),(4, 'Mallory-in-the-middle', 1, '04 Attacks/attacks01'),(4, 'Censorship', 2, '04 Attacks/attacks02'), (4, 'Traceroute', 3, '04 Attacks/attacks03')");
  15. $db->exec("CREATE TABLE solns (id integer PRIMARY KEY,user_id integer,level_id integer,completed integer,json text)");
  16. echo "<p>The database was initialized successfully! <a href=\"./\">Continue...</a></p>\n";
  17. } catch (Exception $e) {
  18. echo "<p>Failed to create file: ".$e->getMessage()."</p>\n";
  19. }
  20. include "footer.inc.php";
  21. exit();
  22. }
  23. $db = new SQLite3(DB_FILE);
  24. session_set_cookie_params(3600 * 24 * 30);
  25. session_start();
  26. $userq = $db->prepare("SELECT * FROM user WHERE name = :name");
  27. if (isset($_POST['username']) && isset($_POST['password'])) {
  28. $userq->bindValue(':name', $_POST['username']);
  29. $res = $userq->execute();
  30. if ($res === false) {
  31. $login_error = "Username or password incorrect.";
  32. } else {
  33. $res = $res->fetchArray();
  34. if (password_verify($_POST['password'], $res['password'])) {
  35. $_SESSION['cs4g_user_id'] = $res['id'];
  36. header('Location: ./');
  37. } else {
  38. $login_error = "Username or password incorrect.";
  39. }
  40. }
  41. } else if (isset($_GET['logout'])) {
  42. unset($_SESSION['cs4g_user_id']);
  43. session_destroy();
  44. header('Location: ./');
  45. }
  46. define('LOGGEDIN', isset($_SESSION['cs4g_user_id']));
  47. ?>