Iñigo Valentin 6 gadi atpakaļ
vecāks
revīzija
415ec7780a

+ 38 - 11
application/Controller.php

@@ -32,11 +32,36 @@
             global $UID;
             $page;
 
+            session_start();
             $db = start_db();
 
+            // Parse parameters, get lang and build the route.
+            $pars = [];
+            foreach($params as $p){
+                if (strlen($p) > 0){
+                    array_push($pars, $p);
+                }
+            }
+
+            // Special case: Actions.
+            // Not redirecting to page, just execute a function
+            if (count($pars) > 1 && strtoupper($pars[0]) == "ACTION"){
+                switch (strtoupper($pars[1])){
+                    case "LOGIN":
+                        require_once($path["action"] . "login.php");
+                        action($db);
+                        break;
+                    default:
+                        require_once($path["page"] . "Error_Page.php");
+                        $page = new Error_Page($db, 404);
+                        http_response_code(404);
+                }
+            }
+
             // TODO: Validate private/public user with cookie and redirect
             $UID = $uid;
             if (preg_match("/\d{7}/", $UID)){
+error_log('1');
                 $q = $db->query("SELECT count(uid) AS c FROM player WHERE uid = $UID;");
                 $r = $q->fetchArray(SQLITE3_ASSOC);
                 if ($r["c"] == 0){
@@ -47,17 +72,19 @@
                 }
             }
             else{
-                require_once($path["page"] . "Landing_Page.php");
-                $page = new Landing_Page($db);
-                require_once($page->view);
-                return;
-            }
-
-            // Parse parameters, get lang and build the route.
-            $pars = [];
-            foreach($params as $p){
-                if (strlen($p) > 0){
-                    array_push($pars, $p);
+error_log('2');
+                if ($_SESSION['session'] == true && isset($_SESSION['uid'])){
+error_log('2.1');
+                    $UID = $_SESSION['uid'];
+                    header("Location: /$UID" . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
+                    return;
+                }
+                else{
+error_log('2.2');
+                    require_once($path["page"] . "Landing_Page.php");
+                    $page = new Landing_Page($db);
+                    require_once($page->view);
+                    return;
                 }
             }
 

+ 29 - 0
application/action/login.php

@@ -0,0 +1,29 @@
+<?php
+    function action($db = null){
+        if (!isset($_POST['uname'], $_POST['password'])){
+            return -1;
+        }
+        $uname = SQLite3::escapeString($_POST['uname']);
+        if (strlen($uname) == 0){
+            return -2;
+        }
+        $password = SQLite3::escapeString($_POST['password']);
+        if (strlen($password) == 0){
+            return -3;
+        }
+        $password = hash('sha256', $password);
+        $q = $db->query("SELECT uid FROM player WHERE upper(name) = upper('$uname') AND password = '$password';");
+        $r = $q->fetchArray(SQLITE3_ASSOC);
+        if (!$r){
+            return -4;
+        }
+        $uid = $r["uid"];
+        session_regenerate_id();
+        $_SESSION['session'] = true;
+        $_SESSION['name'] = $uname;
+        $_SESSION['uid'] = $uid;
+        header("Location: /$uid/");
+        die();
+        return 0;
+    }
+?>

+ 1 - 0
application/config.php

@@ -33,6 +33,7 @@
         "controller" => "$base_dir../application/Controller.php",
         "entity" => "$base_dir../application/entity/",
         "page" => "$base_dir../application/page/",
+        "action" => "$base_dir../application/action/",
         "frame" => "$base_dir../application/page/frame/",
         "handler" => "$base_dir../application/handler/",
         "view" => "$base_dir../application/view/",

+ 2 - 12
application/page/Landing_Page.php

@@ -7,11 +7,6 @@
      */
     class Landing_Page extends Page{
 
-        /**
-         * Array of players [uid]=>[name].
-         */
-        public $players = [];
-
         /**
          * Constructor.
          *
@@ -24,13 +19,8 @@
             global $root;
             parent::__construct($db);
             $this->view = $path["view"] . "landing.php";
-            $s = "SELECT uid, name FROM player";
-            $q = $this->db->query($s);
-            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-                $this->players[$r["uid"]] = $r["name"];
-            }
-            $this->title = "Player - SWDB";
-            $this->description = "Select a player";
+            $this->title = "Login - SWDB";
+            $this->description = "Login to SWDB";
             $this->canonical = $root . "landing/";
         }
     }

+ 37 - 26
application/view/inc/header.php

@@ -8,30 +8,41 @@
             <span class='small'>ase</span>
         </h1>
     </div>
-    <nav>
-        <a href='/<?=$UID?>/'>
-            Home
-        </a>
-        <a href='/<?=$UID?>/catalog'>
-            Catalog
-        </a>
-        <a href='/<?=$UID?>/monsters'>
-            Monsters
-        </a>
-        <a href='/<?=$UID?>/runes'>
-            Runes
-        </a>
-        <a href='/<?=$UID?>/fusion'>
-            Fusion
-        </a>
-        <a href='/<?=$UID?>/guild'>
-            Guild
-        </a>
-        <a href='/<?=$UID?>/runs'>
-            Runs
-        </a>
-        <a href='/<?=$UID?>/report'>
-            Reports
-        </a>
-    </nav>
+<?php
+    if ($_SESSION['session'] == true){
+?>
+        <nav id='menu'>
+            <a href='/<?=$UID?>/'>
+                Home
+            </a>
+            <a href='/<?=$UID?>/catalog/'>
+                Catalog
+            </a>
+            <a href='/<?=$UID?>/monsters/'>
+                Monsters
+            </a>
+            <a href='/<?=$UID?>/runes/'>
+                Runes
+            </a>
+            <a href='/<?=$UID?>/fusion/'>
+                Fusion
+            </a>
+            <a href='/<?=$UID?>/guild/'>
+                Guild
+            </a>
+            <a href='/<?=$UID?>/runs/'>
+                Runs
+            </a>
+            <a href='/<?=$UID?>/report/'>
+                Reports
+            </a>
+        </nav>
+        <nav id='profile'>
+            <a href='/<?=$UID?>/profile/'>
+                <?=$_SESSION['name']?>
+            </a>
+        </nav>
+<?php
+    }
+?>
 </header>

+ 28 - 32
application/view/landing.php

@@ -28,26 +28,6 @@
         <meta name='twitter:image' content='<?=$page->icon?>'/>
         <meta name='twitter:url' content='<?=$page->canonical?>'/>
         <meta name='robots' content='index follow'/>
-        <script>
-            /**
-             * Performs the login action, andrredirects to the user home page.
-             *
-             * Intended to be called before parsing form action.
-             *
-             * @return Always false, to stop th actual form to be submitted.
-             */
-            function login(){
-                sel = document.getElementById('uid');
-                uid = sel.options[sel.selectedIndex].value;
-                if (uid.match(/^[0-9]{7}$/g) != null){
-                    document.location='/' + uid + '/';
-                }
-                //else{
-                    // TODO alert('N UID');
-                //}
-                return false;
-            }
-        </script>
     </head>
     <body>
 <?php
@@ -59,18 +39,34 @@
             </h2>
             <article>
                 <!-- TODO: Action via JS, and fllback in controller -->
-                <form method='GET' action='/' onsubmit="return login();">
-                    <select id='uid' name='uid'>
-                        <option value='0'> </option>
-<?php
-                        foreach ($page->players as $player_id => $player_name){
-?>
-                            <option value='<?=$player_id?>'><?=$player_name?></option>
-<?php
-                        }
-?>
-                    </select>
-                    <input type='submit' value='Go'/>
+                <form method='POST' action='/action/login/'>
+                    <table>
+                        <tr>
+                            <td class='label'>
+                                <label for='uname'>
+                                    Player
+                                </label>
+                            </td>
+                            <td class='input'>
+                                <input type='text' name='uname' id='uname' required/>
+                            </td>
+                        </tr>
+                        <tr>
+                            <td class='label'>
+                                <label for='password'>
+                                    Password
+                                </label>
+                            </td>
+                            <td class='input'>
+                                <input type='password' name='password' id='password' required/>
+                            </td>
+                        </tr>
+                        <tr>
+                            <td class='submit' colspan='2'>
+                                <input type='submit' value='Go'/>
+                            </td>
+                        </tr>
+                    </table>
                 </form>
             </article>
         </section> <!-- #catalog -->

+ 15 - 0
public/css/landing.css

@@ -0,0 +1,15 @@
+form table{
+    margin: auto;
+}
+form table td.label{
+    text-align: right;
+    padding-right: 1em;
+}
+form table td.input{
+    text-align: left;
+}
+form table td.submit{
+    text-align: center;
+}
+
+

+ 26 - 5
public/css/ui.css

@@ -182,10 +182,9 @@ header div#logo h1#logo_text span.small{
     font-size: 60%;
     margin-left: -0.5em;
 }
-header nav{
-    display: block;
+header nav#menu{
+    display: inline-block;
     height: 2em;
-    margin-left: 8em;
 }
 header nav a{
     font-size: 110%;
@@ -204,7 +203,7 @@ header nav a{
     vertical-align: middle;
     text-shadow: var(--nav-shadow);
 }
-header nav a:last-of-type{border-right: var(--nav-separator)}
+header nav#menu a:last-of-type{border-right: var(--nav-separator)}
 
 header nav a:hover{
     background-color: var(--nav-background-hover);
@@ -222,6 +221,28 @@ header nav a:hover{
         text-shadow: 0 0 0 #000000;
     }
 }
+header nav#profile{
+    float: right;
+    margin-right: 0.5em;
+}
+header nav#profile a{
+    border-left: 0;
+    /*font-size: 110%;
+    font-weight: bold;
+    text-transform: capitalize;
+    font-variant: small-caps;
+    margin: 0 -0.2em 0 0;
+    text-decoration: none;
+    position: relative;
+    transition: all .4s ease-in-out;
+    color: var(--nav-color);
+    border-left: var(--nav-separator);
+    padding: 0.3em;
+    text-align: center;
+    display: inline-block;
+    vertical-align: middle;
+    text-shadow: var(--nav-shadow);*/
+}
 footer{
     font-size: 80%;
     text-align: center;
@@ -267,7 +288,7 @@ input, select, a.a_button{
     box-shadow: var(--input-box-shadow);
 }
 input[type=button], input[type=submit]{padding: var(--input-button-padding);}
-input[type=text], input[type=number], input[type=date], select{font-size: 80%;}
+input[type=text], input[type=number], input[type=date], input[type=password], select{font-size: 80%;}
 input[type=number]{width: 3em;}
 select[multiple]{scrollbar-width: none;}
 input[type=checkbox]{