summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2013-10-16 19:34:08 +0200
committerSimon Rettberg2013-10-16 19:34:08 +0200
commite74e32a0eb4b2bb9691a079d6dc579925d7bb0ce (patch)
tree01f686385bab29fe14b13a819fdbb87ba6ea1100
parentAdd simple menu bar (diff)
downloadslx-admin-e74e32a0eb4b2bb9691a079d6dc579925d7bb0ce.tar.gz
slx-admin-e74e32a0eb4b2bb9691a079d6dc579925d7bb0ce.tar.xz
slx-admin-e74e32a0eb4b2bb9691a079d6dc579925d7bb0ce.zip
New stuff
-rw-r--r--config.php1
-rw-r--r--inc/menu.inc.php21
-rw-r--r--inc/render.inc.php22
-rw-r--r--inc/user.inc.php59
-rw-r--r--inc/util.inc.php7
-rw-r--r--index.php28
-rw-r--r--modules/main.inc.php12
-rw-r--r--modules/session.inc.php39
-rw-r--r--style/default.css38
-rw-r--r--templates/main-menu.html36
-rw-r--r--templates/menu-login.html1
-rw-r--r--templates/menu-logout.html1
-rw-r--r--templates/messagebox-error.html1
-rw-r--r--templates/page-login.html12
14 files changed, 270 insertions, 8 deletions
diff --git a/config.php b/config.php
index fd388657..910c14dd 100644
--- a/config.php
+++ b/config.php
@@ -1,4 +1,5 @@
<?php
define('CONFIG_SESSION_DIR', '/tmp/openslx');
+define('CONFIG_SESSION_TIMEOUT', 86400);
diff --git a/inc/menu.inc.php b/inc/menu.inc.php
new file mode 100644
index 00000000..7b2502b9
--- /dev/null
+++ b/inc/menu.inc.php
@@ -0,0 +1,21 @@
+<?php
+
+require_once('inc/render.inc.php');
+require_once('inc/user.inc.php');
+
+class Menu
+{
+
+ public function loginPanel()
+ {
+ if (User::getName() === false) return Render::parse('menu-login');
+ return Render::parse('menu-logout', array('user' => User::getName()));
+ }
+
+ public function token()
+ {
+ return 123;
+ }
+
+}
+
diff --git a/inc/render.inc.php b/inc/render.inc.php
index 455fd87d..41b8e2b8 100644
--- a/inc/render.inc.php
+++ b/inc/render.inc.php
@@ -46,10 +46,12 @@ class Render
,
' </head>
<body>
+ <div class="container">
',
self::$body
,
- ' <script src="script/jquery.js"></script>
+ ' </div>
+ <script src="script/jquery.js"></script>
<script src="script/bootstrap.min.js"></script></body>
</html>'
;
@@ -74,12 +76,28 @@ class Render
/**
* Add the given template to the output, using the given params for placeholders in the template
*/
- public static function parse($template, $params)
+ public static function addTemplate($template, $params = false)
{
self::$body .= self::$mustache->render(self::getTemplate($template), $params);
}
/**
+ * Add error message to page
+ */
+ public static function addError($message)
+ {
+ self::addTemplate('messagebox-error', array('message' => $message));
+ }
+
+ /**
+ * Parse template with given params and return; do not add to body
+ */
+ public static function parse($template, $params = false)
+ {
+ return self::$mustache->render(self::getTemplate($template), $params);
+ }
+
+ /**
* Open the given html tag, optionally adding the passed assoc array of params
*/
public static function openTag($tag, $params = false)
diff --git a/inc/user.inc.php b/inc/user.inc.php
new file mode 100644
index 00000000..de615932
--- /dev/null
+++ b/inc/user.inc.php
@@ -0,0 +1,59 @@
+<?php
+
+require_once('inc/session.inc.php');
+
+class User
+{
+ private static $user = false;
+ private static $session = false;
+
+ public static function isLoggedIn()
+ {
+ return self::$user !== false;
+ }
+
+ public static function getName()
+ {
+ if (self::$user === false) return false;
+ return self::$user['name'];
+ }
+
+ public static function load()
+ {
+ if (isset($_REQUEST['PHPSESSID']) || isset($_COOKIE['PHPSESSID'])) {
+ session_start();
+ if (!isset($_SESSION['uid']) || !is_numeric($_SESSION['uid'])) {
+ self::logout();
+ return false;
+ }
+ // TODO: Query user db for persistent data
+ $user['name'] = 'Hans';
+ return true;
+ }
+ return false;
+ }
+
+ public static function login($user, $pass)
+ {
+ if ($user == 'test' && $pass == 'test') {
+ session_start();
+ $_SESSION['uid'] = 1;
+ $_SESSION['token'] = md5(rand() . time() . rand() . $_SERVER['REMOTE_ADDR'] . rand() . $_SERVER['REMOTE_PORT'] . rand() . $_SERVER['HTTP_USER_AGENT']);
+ session_write_close();
+ return true;
+ }
+ return false;
+ }
+
+ public static function logout()
+ {
+ session_unset();
+ session_destroy();
+ if (setcookie('PHPSESSID', '', time() - 86400)) {
+ Header('Location: ?do=main&fromlogout');
+ }
+ exit(0);
+ }
+
+}
+
diff --git a/inc/util.inc.php b/inc/util.inc.php
index 75cd914e..793902ec 100644
--- a/inc/util.inc.php
+++ b/inc/util.inc.php
@@ -17,5 +17,12 @@ class Util
}
exit(0);
}
+
+ public static function redirect($location)
+ {
+ session_write_close();
+ Header('Location: ' . $location);
+ exit(0);
+ }
}
diff --git a/index.php b/index.php
index 470cd3ef..2bf1b17a 100644
--- a/index.php
+++ b/index.php
@@ -1,16 +1,32 @@
<?php
-require_once('inc/session.inc.php');
+error_reporting(E_ALL);
+
+require_once('inc/user.inc.php');
require_once('inc/render.inc.php');
+require_once('inc/menu.inc.php');
+require_once('inc/util.inc.php');
+
+if (empty($_REQUEST['do'])) {
+ // No specific module - set default
+ $module = 'main';
+} else {
+ $module = preg_replace('/[^a-z]/', '', $_REQUEST['do']);
+}
+
+$module = 'modules/' . $module . '.inc.php';
-Render::setTitle('Wurstgesicht');
+if (!file_exists($module)) {
+ Util::traceError('Invalid module: ' . $module);
+}
-Render::parse('main-menu', false);
+require_once($module);
+unset($module);
-Render::openTag('h1', array('class' => 'wurst kacke'));
-Render::closeTag('h1');
+$menu = new Menu;
+Render::addTemplate('main-menu', $menu);
-Render::parse('helloworld', array('wurst' => 'käse & bier'));
+render_module();
Render::output();
diff --git a/modules/main.inc.php b/modules/main.inc.php
new file mode 100644
index 00000000..ef83f1c4
--- /dev/null
+++ b/modules/main.inc.php
@@ -0,0 +1,12 @@
+<?php
+
+function render_module()
+{
+ Render::setTitle('Wurstgesicht');
+
+ Render::openTag('h1', array('class' => 'wurst kacke'));
+ Render::closeTag('h1');
+
+ Render::addTemplate('helloworld', array('wurst' => 'käse & bier'));
+}
+
diff --git a/modules/session.inc.php b/modules/session.inc.php
new file mode 100644
index 00000000..5b8c5f4d
--- /dev/null
+++ b/modules/session.inc.php
@@ -0,0 +1,39 @@
+<?php
+
+if (!isset($_REQUEST['action'])) Util::traceError('No action on module init');
+
+User::load();
+
+if (isset($_POST['action']) && $_POST['action'] === 'login') {
+ // Login - see if already logged in
+ if (User::isLoggedIn()) {
+ Util::redirect('?do=main');
+ }
+ // Else, try to log in
+ if (User::login($_POST['user'], $_POST['pass'])) {
+ Util::redirect('?do=main');
+ }
+ // Login credentials wrong
+ Util::redirect('?do=session&action=fail');
+}
+
+if ($_REQUEST['action'] === 'logout') {
+ // Log user out (or do nothing if not logged in)
+ exit(0);
+}
+
+function render_module()
+{
+ if (!isset($_GET['action'])) Util::traceError('No action on render');
+ if ($_GET['action'] === 'login') {
+ Render::setTitle('Anmelden');
+ Render::addTemplate('page-login');
+ return;
+ }
+ if ($_GET['action'] === 'fail') {
+ Render::setTitle('Fehler');
+ Render::addError('Benutzer oder Passwort falsch');
+ return;
+ }
+}
+
diff --git a/style/default.css b/style/default.css
new file mode 100644
index 00000000..052fa0b1
--- /dev/null
+++ b/style/default.css
@@ -0,0 +1,38 @@
+body {
+ padding-top: 70px;
+}
+
+.form-signin {
+ max-width: 330px;
+ padding: 15px;
+ margin: 0 auto;
+}
+.form-signin .form-signin-heading,
+.form-signin .checkbox {
+ margin-bottom: 10px;
+}
+.form-signin .checkbox {
+ font-weight: normal;
+}
+.form-signin .form-control {
+ position: relative;
+ font-size: 16px;
+ height: auto;
+ padding: 10px;
+ -webkit-box-sizing: border-box;
+ -moz-box-sizing: border-box;
+ box-sizing: border-box;
+}
+.form-signin .form-control:focus {
+ z-index: 2;
+}
+.form-signin input[type="text"] {
+ margin-bottom: -1px;
+ border-bottom-left-radius: 0;
+ border-bottom-right-radius: 0;
+}
+.form-signin input[type="password"] {
+ margin-bottom: 10px;
+ border-top-left-radius: 0;
+ border-top-right-radius: 0;
+}
diff --git a/templates/main-menu.html b/templates/main-menu.html
new file mode 100644
index 00000000..7e9c3bdc
--- /dev/null
+++ b/templates/main-menu.html
@@ -0,0 +1,36 @@
+<!-- Fixed navbar -->
+<div class="navbar navbar-default navbar-fixed-top">
+ <div class="container">
+ <div class="navbar-header">
+ <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
+ <span class="icon-bar"></span>
+ <span class="icon-bar"></span>
+ <span class="icon-bar"></span>
+ </button>
+ <a class="navbar-brand" href="#">OpenSLX Admin</a>
+ </div>
+ <div class="navbar-collapse collapse">
+ <ul class="nav navbar-nav">
+ <li class="active"><a href="#">Home</a></li>
+ <li><a href="#about">About</a></li>
+ <li><a href="#contact">Contact</a></li>
+ <li class="dropdown">
+ <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<b class="caret"></b></a>
+ <ul class="dropdown-menu">
+ <li><a href="#">Format C:</a></li>
+ <li><a href="#">Kompaliere Kernel</a></li>
+ <li class="divider"></li>
+ <li class="dropdown-header">Nav header</li>
+ <li><a href="#">1</a></li>
+ </ul>
+ </li>
+ </ul>
+ <ul class="nav navbar-nav navbar-right">
+ <li><a href="http://www.openslx.org/">OpenSLX</a></li>
+ <li><a href="http://mltk.boot.openslx.org/">mltk</a></li>
+ {{{loginPanel}}}
+ </ul>
+ </div>
+ </div>
+</div>
+
diff --git a/templates/menu-login.html b/templates/menu-login.html
new file mode 100644
index 00000000..c5ee6a2a
--- /dev/null
+++ b/templates/menu-login.html
@@ -0,0 +1 @@
+<li><a href="?do=session&amp;action=login">Anmelden</a></li>
diff --git a/templates/menu-logout.html b/templates/menu-logout.html
new file mode 100644
index 00000000..d99fac58
--- /dev/null
+++ b/templates/menu-logout.html
@@ -0,0 +1 @@
+<li><a href="?do=session&amp;action=logout&amp;token={{{token}}}">{{user}} abmelden</a></li>
diff --git a/templates/messagebox-error.html b/templates/messagebox-error.html
new file mode 100644
index 00000000..dc2dbc8a
--- /dev/null
+++ b/templates/messagebox-error.html
@@ -0,0 +1 @@
+<div class="alert alert-danger">{{message}}</div>
diff --git a/templates/page-login.html b/templates/page-login.html
new file mode 100644
index 00000000..4c2e7220
--- /dev/null
+++ b/templates/page-login.html
@@ -0,0 +1,12 @@
+<div class="container">
+ <form class="form-signin" action="?do=session" method="post">
+ <h2 class="form-signin-heading">Anmeldung</h2>
+ <input type="text" name="user" class="form-control" placeholder="Benutzername" autofocus>
+ <input type="password" name="pass" class="form-control" placeholder="Passwort">
+ <label class="checkbox">
+ <input type="checkbox" name="remember" value="remember-me"> Angemeldet bleiben
+ </label>
+ <button class="btn btn-lg btn-primary btn-block" type="submit">Anmelden</button>
+ <input type="hidden" name="action" value="login">
+ </form>
+</div>