Robert Marshall před 10 roky
rodič
revize
ff58073fc0

+ 52 - 0
Controller/Member.php

@@ -0,0 +1,52 @@
+<?php
+class Member {
+	public function Index($params) {
+		return $this->Login($params);
+	}
+	
+	public function Login($params){
+		$user=Session::GetLoggedInUser();
+		if ($user->UserId!=0){
+			header("location:/member/manage");
+			return;
+		}
+		return new View("Member/login.view");
+	}
+	
+	public function Logout($params){
+		Session::Destroy();
+		header("location:/member");
+	}
+	
+	public function Register($params){
+		if (!isset($params['email'],$params['password']))
+			return $this->Login($params);
+		
+		$errors=array();
+		if ($params['email']=="")
+			$errors[]="Email blank";
+		if  ($params['password']=="")
+			$errors[]="Password blank";
+		
+		$user=new User($params['email']);
+		if ($user->UserId!=0)
+			$errors[]="Email already in use";
+		
+		if (count($errors)>0)
+			return new View("Member/login.view",array("errors"=>$errors));
+		
+		$user=new User();
+		$user->UserEmail=$params['email'];
+		$user->UserPassword=$params['password'];
+		$user->UserCreated=time();
+		$user->Save();
+		
+		Session::SetLoggedInUser($user);
+		
+		header("location:/member/manage");
+	}
+	
+	public function Manage($params){
+		return new View("Member/manage.view");
+	}
+}

+ 19 - 0
Controller/Navigation/MemberNav.php

@@ -0,0 +1,19 @@
+<?php
+class MemberNav implements INavigationController{
+	private $_uri, $_items;
+	
+	public function __construct() {
+		$this->_uri=new URI("Members","/member");
+		$this->_items=array(
+			new URI("Login","login")
+		);
+	}
+	
+	public function GetItems() {
+		return $this->_items;
+	}
+
+	public function GetURI() {
+		return $this->_uri;
+	}
+}

+ 7 - 0
DB Scripts/create_sessions.sql

@@ -0,0 +1,7 @@
+CREATE TABLE `sessions` (
+  `session_pkey` INT NOT NULL AUTO_INCREMENT,
+  `session_id` VARCHAR(32) NOT NULL,
+  `user_id` INT NULL DEFAULT 0,
+  `session_expiry` DATETIME NULL,
+  PRIMARY KEY (`session_pkey`),
+  UNIQUE INDEX `session_id_UNIQUE` (`session_id` ASC));

+ 1 - 1
DB Scripts/create_users.sql

@@ -1,5 +1,5 @@
 CREATE TABLE `users` (
-  `user_id` INT NOT NULL,
+  `user_id` INT NOT NULL AUTO_INCREMENT,
   `user_email` VARCHAR(320) NULL,
   `user_password` VARCHAR(255) NULL,
   `user_created` DATETIME NULL,

+ 65 - 3
Model/Session.php

@@ -1,6 +1,68 @@
 <?php
-class Session extends DBObjectAutoCreate {
-	public function __construct($table, $key, $id) {
-		parent::__construct($table, $key, $id);
+ApplicationSettings::RegisterDefaultSetting("session", "expiry_window", "604800"); //a week
+
+class Session extends DBObjectAutoCreate {	
+	private $_expiry;
+	private static $_instance,$_user;
+	
+	public function __construct($id=0) {
+		parent::__construct("sessions", "session_id", $id);
+		$this->_expiry=strtotime($this->SessionExpiry);
+	}
+	
+	public function Save() {
+		if ($this->SessionId==null)
+			$this->SessionId=Utils::GenerateRandomString(32);
+		
+		$expiryWindow=(int)ApplicationSettings::GetSetting("session", "expiry_window");
+		$this->SessionExpiry=time()+$expiryWindow;
+		setcookie("session_id",$this->SessionId,$expiryWindow);
+		
+		parent::Save();
+	}
+
+	public function HasExpired(){
+		return $this->_expiry<time();
+	}
+
+	private static function Instantiate(){
+		if (self::$_instance!=null)
+			return;
+		
+		$instance=null;
+		if (isset($_COOKIE['session_id'])){
+			$instance=new Session($_COOKIE['session_id']);
+			if ($instance->HasExpired())
+				$instance=new Session();
+		} else
+			$instance=new Session();
+		
+		self::$_instance=$instance;
+	}
+	
+	public static function Destroy(){
+		self::Instantiate();
+		
+		$PDO=self::GetPDO();
+		$prep=$PDO->prepare("DELETE FROM sessions WHERE session_id=?");
+		$prep->execute(array(self::$_instance->SessionId));
+		
+		self::$_instance=null;
+	}
+
+	public static function GetLoggedInUser() {
+		self::Instantiate();
+		
+		if (isset(self::$_user))
+			return self::$_user;
+		
+		self::$_user=new User(self::$_instance->UserId);
+		
+		return self::$_user;
+	}
+	
+	public static function SetLoggedInUser($user) {
+		self::$_instance->UserId=$user->UserId;
+		self::$_instance->Save();
 	}
 }

+ 2 - 16
Model/User.php

@@ -1,23 +1,9 @@
 <?php
-class User extends DBObjectAutoCreate {
-	private static $_user;
-	
-	public static function GetLoggedInUser() {
-		if (isset(self::$_user))
-			return self::$_user;
-		
-		self::CreateTable("users");
-		
-		$user=new User();
-		
-		self::$_user=$user;
-		return $user;
-	}
-	
+class User extends DBObjectAutoCreate {	
 	public function __construct($id=0) {
 		$field="user_id";
 		if (!is_numeric($id))
-			$field="username";
+			$field="user_email";
 		parent::__construct("users", $field, $id);
 	}
 	

+ 1 - 0
View/Member/index.view

@@ -0,0 +1 @@
+@Title{Member Area}@

+ 68 - 0
View/Member/login.view

@@ -0,0 +1,68 @@
+@Title{Member Area}@
+@CSS{
+	.form{
+		float:left;
+		width:50%;
+		box-sizing:border-box;
+	}
+	
+	.form:not(:last-child){
+		padding-right:5px
+	}
+	
+	.form:not(:first-child){
+		padding-left:5px
+	}
+	
+	td:nth-child(2){
+		width:100%;
+	}
+	
+	td>input[type=text],td>input[type=password]{
+		width:100%;
+	}
+}@
+@CSSSmall{
+	.form{
+		width:100%;
+		padding:0 !important;
+	}
+}@
+@Body{
+<?php if (isset($errors)) var_dump($errors); ?>
+<div class="form">
+	<h2>Log In</h2>
+	<?= Utils::TableMaker(
+		array(
+			array(
+				"display"=>"Email",
+				"name"=>"email"
+			),
+			array(
+				"display"=>"Password",
+				"name"=>"password",
+				"type"=>"password"
+			)
+		),
+		"Log In"
+	); ?>
+</div>
+<div class="form">
+	<h2>Register</h2>
+	<?= Utils::TableMaker(
+		array(
+			array(
+				"display"=>"Email",
+				"name"=>"email"
+			),
+			array(
+				"display"=>"Password",
+				"name"=>"password",
+				"type"=>"password"
+			)
+		),
+		"Register",
+		"/member/register"
+	); ?>
+</div>
+}@

+ 4 - 0
View/Member/manage.view

@@ -0,0 +1,4 @@
+@Title{Member Area}@
+@Body{
+<?php var_dump(Session::GetLoggedInUser()); ?>
+}@

+ 2 - 2
base/UserRestrictedApplication.php

@@ -2,8 +2,8 @@
 include_once("Application.php");
 
 class UserRestrictedApplication extends Application {
-	protected function LoadPage($page, $action, $params) {
-		$user=User::GetLoggedInUser();
+	protected function LoadPage($page, $action, $params) {		
+		$user=Session::GetLoggedInUser();
 		if ($user->HasAccess($page,$action))
 			parent::LoadPage($page, $action, $params);
 		else

+ 1 - 1
javascript.js

@@ -54,7 +54,7 @@ $(function(){
 	
 	$("#cookiePopup button").click(function(){
 		$("#cookiePopup").fadeOut();
-		CreateCookie("cookiePopupConfirmed",true,0);
+		CreateCookie("cookiePopupConfirmed",true,3650); // ten years should do it
 	});
 	
 	$("#menu-button").click(function(e){

+ 1 - 1
settings.ini

@@ -11,7 +11,7 @@ database=robware-test
 
 [navigation]
 mode=include
-pages=HomeNav,BlogNav,ProjectsNav,GalleryNav
+pages=HomeNav,BlogNav,ProjectsNav,GalleryNav,MemberNav
 
 [blog]
 posts_per_page=10

+ 4 - 0
style.css

@@ -44,6 +44,10 @@ nav a{
 	text-decoration: none;
 }
 
+h2{
+	margin-bottom:10px;
+}
+
 h2 a{
 	color:inherit;
 }