Skip to content

Commit

Permalink
Starting to move to a MVC pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Nov 25, 2012
1 parent ebae811 commit fc6beab
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 8 deletions.
22 changes: 22 additions & 0 deletions Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* Controller.php
*/

class Controller {
protected $model;
protected $view;

public function __construct($model, $view) {
$this->model = $model;
$this->view = $view;
}
}

class Auth extends Controller {
public $user;
function __construct($model, $view) {
parent::__construct($model, $view);
}
}
63 changes: 63 additions & 0 deletions Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* Models.php
* Contains the definition of super models and other models inherit from that
*/

namespace Model;

class User {
private $userid;
private $auth;
private $level;
private $question;
public function __construct($userid) {
$this->userid = $userid;
$this->auth = false;
//fetch initially the level that's stored in DB
$this->level = getDBLevel();
$this->question = getDBQuestion();
}
public function get($key) {
switch($key) {
case 'id': return $this->userid;
case 'level': return $this->level;
case 'question': return $this->question;
default: return null;
}
}
public function grant() {
$this->auth = true;
}
private function getDBLevel() {
$userAccess = mysql_query("SELECT `to` FROM `user_level` WHERE `userid`='{$this->userid}' ORDER BY `id` DESC LIMIT 1") or die(mysql_error());
$userAccessArray = mysql_fetch_array($userAccess);
$userLevel = $userAccessArray['to'];
if(empty($userLevel)):
//then try to find the starting level from the database
$startNodeQuery = mysql_query("SELECT `value` FROM `config` WHERE `key`='start'") or die(mysql_error());
$startNodeArray = mysql_fetch_assoc($startNodeQuery);
if(empty($startNodeArray['value']))
return 1;
else
return $startNodeArray['value'];
endif;
return $userLevel;
}
private function getDBQuestion() {
$questionQuery = mysql_query("SELECT `url`,`question`,`comments`,`header` FROM `questions` WHERE `level`='{$this->level}' LIMIT 1") or die(mysql_error());
$questionArray = mysql_fetch_assoc($questionQuery);
$question = $questionArray['question'] . "\n" . $questionArray['comments'];
return array(
"question"=>$question,
"header"=>$questionArray['header'],
"comments"=>stripslashes_deep($questionArray['comments']),
"url"=>$questionArray['url']
);
}
}

class Page {
public $pageid;
}
12 changes: 12 additions & 0 deletions auth.lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/*
* Author: Boopathi Rajaa <me@boopathi.in>
* Auth.lib.php
* A class that provides to implement custom auth logic
*/

class Auth {
function __construct(){
$this->auth = false;

56 changes: 56 additions & 0 deletions auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* Authentication part.
* A function auth - returns true if authentication was successfull
* if not, then function error will be called.
*/

require_once('model.lib.php');
namespace Auth {
function authenticate() {

}
};

class Authenticate {
public var $userid;
function __construct() {
$this->userid = null;
//call the authenticate method that returns the authenticate variable;
$this->authenticate = $this->authenticate(); //true or false only
if($this->authenticate == false)
$this->error();
}
function authenticate() {
//Write your authenticate method here
//Return true if authenticated. Note: It should be strictly true

//For PragyanCMS authentication
return this->pragyanAuth();
}
public function error() {
//This function ll be the callback when authenticate returns false.
//You may exit the program here. Not exiting is no harm either.
echo "Authentication Failure.";
return false;
}
function pragyanAuth() {
ini_set("session.name", "PHPSESSID");
ini_set("session.save_path", "/home/boopathi/tmp/sessions");

session_start();

if(!isset($_SESSION['userId'])) {
echo "Session not set ";
return false;
}
if($_SESSION['userId'] == 0 ) {
echo "User Id is 0";
return false;
}
//set the userid if everything is fine.
$this->userid = $_SESSION['userId'];
return true;
}
}
14 changes: 6 additions & 8 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
exit(1);
endif;

ini_set("session.name","PHPSESSID");
ini_set("session.save_path", "/var/www/html/12/cms/uploads/sessions");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

session_start();
//TODO:
//Authenticate Section

require_once("./auth.php");

if(!isset($_SESSION['userId'])){
header("Location: http://www.pragyan.org/12/events/brainwork/labyrinth/play+login");
// $_SESSION["userid"] = rand(5, 100);
if($auth->auth == false){

}


//Set the Constant LABYRINTH
define("LABYRINTH_CONST", "LABYRINTH APPLICATION");

Expand Down

0 comments on commit fc6beab

Please sign in to comment.