Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
define("LIB_DIR", dirname(__FILE__));
define("COOKIE_DOMAIN", "");

// Set the default controller hte user is directed to (aka homepage).
// Set the default controller the user is directed to (aka homepage).
define('ROUTER_DEFAULT_CONTROLLER', 'site');
define('ROUTER_DEFAULT_ACTION', 'home');

Expand All @@ -33,4 +33,5 @@
require_once(LIB_DIR."/helpers.php");
require_once(LIB_DIR."/models/cache.php");
require_once(LIB_DIR."/models/user.php");
require_once(LIB_DIR."/models/template.php");
require_once(LIB_DIR."/models/template.php");

48 changes: 24 additions & 24 deletions lib/controllers/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

class App {

/**
* Main constructor function, used to initialize the models, connect to the DB and
* route the user to where they need to go.
*/
/**
* Main constructor function, used to initialize the models, connect to the DB and
* route the user to where they need to go.
*/
function __construct(){
global $template, $user, $db;

Expand All @@ -29,10 +29,10 @@ function __construct(){
// Connect to the database.
try {
// If your application uses MySQL, use the following line instead:
// $db = new PDO("mssql:host=$host;dbname=$dbname, $user, $pass");
$db = new PDO('sqlite:' . BASE_DIR . '/db/sqlite.db');
// $db = new PDO("mysql:host=$host;dbname=$dbname, $user, $pass");
$db = new PDO('sqlite:' . BASE_DIR . '/db/sqlite.db');
} catch (Exception $e) {
die($e);
die($e);
}

// Check to see if the 'user' table exists and if not, create it.
Expand All @@ -47,10 +47,10 @@ function __construct(){
$this->router();
}

/**
* Figure out where the user is trying to get to and route them to the
* appropriate controller/action.
*/
/**
* Figure out where the user is trying to get to and route them to the
* appropriate controller/action.
*/
function router() {

// Create a new Router instance.
Expand Down Expand Up @@ -94,24 +94,24 @@ function router() {
}else Site::load_page('home');
}

/**
* Check to see if the proper tables exist in the database and if not,
* create them.
*/
/**
* Check to see if the proper tables exist in the database and if not,
* create them.
*/
function check_db() {
global $db;

$sql = 'CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
password TEXT,
create_ip TEXT,
create_date TEXT,
status INTEGER
)';
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
password TEXT,
create_ip TEXT,
create_date TEXT,
status INTEGER
)';
$query = $db->prepare($sql);
$query->execute();
}
}
?>

177 changes: 88 additions & 89 deletions lib/controllers/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,96 +15,95 @@
*/

class Router {
public $request_uri;
public $routes;
public $controller, $controller_name;
public $action, $id;
public $params;
public $route_found = false;
public function __construct() {
$request = $_SERVER['REQUEST_URI'];
$pos = strpos($request, '?');
if ($pos) $request = substr($request, 0, $pos);
$this->request_uri = $request;
$this->routes = array();
}
public function map($rule, $target=array(), $conditions=array()) {
$this->routes[$rule] = new Route($rule, $this->request_uri, $target, $conditions);
}
public function default_routes() {
$this->map('/:controller');
$this->map('/:controller/:action');
$this->map('/:controller/:action/:id');
}
private function set_route($route) {
$this->route_found = true;
$params = $route->params;
$this->controller = $params['controller']; unset($params['controller']);
$this->action = $params['action']; unset($params['action']);
$this->id = $params['id'];
$this->params = array_merge($params, $_GET);
if (empty($this->controller)) $this->controller = ROUTER_DEFAULT_CONTROLLER;
if (empty($this->action)) $this->action = ROUTER_DEFAULT_ACTION;
if (empty($this->id)) $this->id = null;
$w = explode('_', $this->controller);
foreach($w as $k => $v) $w[$k] = ucfirst($v);
$this->controller_name = implode('', $w);
}
public function execute() {
foreach($this->routes as $route) {
if ($route->is_matched) {
$this->set_route($route);
break;
}
}
}
public $request_uri;
public $routes;
public $controller, $controller_name;
public $action, $id;
public $params;
public $route_found = false;

public function __construct() {
$request = $_SERVER['REQUEST_URI'];
$pos = strpos($request, '?');
if ($pos) $request = substr($request, 0, $pos);

$this->request_uri = $request;
$this->routes = array();
}

public function map($rule, $target=array(), $conditions=array()) {
$this->routes[$rule] = new Route($rule, $this->request_uri, $target, $conditions);
}

public function default_routes() {
$this->map('/:controller');
$this->map('/:controller/:action');
$this->map('/:controller/:action/:id');
}

private function set_route($route) {
$this->route_found = true;
$params = $route->params;
$this->controller = $params['controller']; unset($params['controller']);
$this->action = $params['action']; unset($params['action']);
$this->id = $params['id'];
$this->params = array_merge($params, $_GET);

if (empty($this->controller)) $this->controller = ROUTER_DEFAULT_CONTROLLER;
if (empty($this->action)) $this->action = ROUTER_DEFAULT_ACTION;
if (empty($this->id)) $this->id = null;

$w = explode('_', $this->controller);
foreach($w as $k => $v) $w[$k] = ucfirst($v);
$this->controller_name = implode('', $w);
}

public function execute() {
foreach($this->routes as $route) {
if ($route->is_matched) {
$this->set_route($route);
break;
}
}
}
}

class Route {
public $is_matched = false;
public $params;
public $url;
private $conditions;
function __construct($url, $request_uri, $target, $conditions) {
$this->url = $url;
$this->params = array();
$this->conditions = $conditions;
$p_names = array(); $p_values = array();
preg_match_all('@:([\w]+)@', $url, $p_names, PREG_PATTERN_ORDER);
$p_names = $p_names[0];
$url_regex = preg_replace_callback('@:[\w]+@', array($this, 'regex_url'), $url);
$url_regex .= '/?';
if (preg_match('@^' . $url_regex . '$@', $request_uri, $p_values)) {
array_shift($p_values);
foreach($p_names as $index => $value) $this->params[substr($value,1)] = urldecode($p_values[$index]);
foreach($target as $key => $value) $this->params[$key] = $value;
$this->is_matched = true;
}
unset($p_names); unset($p_values);
}
function regex_url($matches) {
$key = str_replace(':', '', $matches[0]);
if (array_key_exists($key, $this->conditions)) {
return '('.$this->conditions[$key].')';
}
else {
return '([a-zA-Z0-9_\+\-%]+)';
}
}
public $is_matched = false;
public $params;
public $url;
private $conditions;

function __construct($url, $request_uri, $target, $conditions) {
$this->url = $url;
$this->params = array();
$this->conditions = $conditions;
$p_names = array(); $p_values = array();

preg_match_all('@:([\w]+)@', $url, $p_names, PREG_PATTERN_ORDER);
$p_names = $p_names[0];

$url_regex = preg_replace_callback('@:[\w]+@', array($this, 'regex_url'), $url);
$url_regex .= '/?';

if (preg_match('@^' . $url_regex . '$@', $request_uri, $p_values)) {
array_shift($p_values);
foreach($p_names as $index => $value) $this->params[substr($value,1)] = urldecode($p_values[$index]);
foreach($target as $key => $value) $this->params[$key] = $value;
$this->is_matched = true;
}

unset($p_names); unset($p_values);
}

function regex_url($matches) {
$key = str_replace(':', '', $matches[0]);
if (array_key_exists($key, $this->conditions)) {
return '('.$this->conditions[$key].')';
}
else {
return '([a-zA-Z0-9_\+\-%]+)';
}
}
}

?>
11 changes: 6 additions & 5 deletions lib/controllers/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

class Site {

/**
* Loads a particular page from the 'site' directory in views
*
* @param $name The name of the page to load (should match filename)
*/
/**
* Loads a particular page from the 'site' directory in views
*
* @param $name The name of the page to load (should match filename)
*/
public static function load_page($name){
global $template;
$standard = array("faq", "terms", "about");
Expand All @@ -27,3 +27,4 @@ public static function load_page($name){
$template->render("site", $name, true);
}
}

42 changes: 21 additions & 21 deletions lib/controllers/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

class User {

/**
* Default user profile page.
*/
/**
* Default user profile page.
*/
function index(){
global $template, $user;
login_required();
Expand All @@ -28,12 +28,12 @@ function index(){
$template->render("user","profile",true);
}

/**
* Login page.
*
* Sends the user to the homepage if they're already logged in. If they try to
* login, validates their info and redirects them to homepage.
*/
/**
* Login page.
*
* Sends the user to the homepage if they're already logged in. If they try to
* login, validates their info and redirects them to homepage.
*/
function login(){
global $user, $template;
if($user->is_logged)
Expand All @@ -52,11 +52,11 @@ function login(){
$template->render("user","login",true);
}

/**
* Logout page.
*
* Simply logs the user out if they're logged in, then renders the login page.
*/
/**
* Logout page.
*
* Simply logs the user out if they're logged in, then renders the login page.
*/
function logout(){
global $user, $template;
if($user->is_logged()){
Expand All @@ -66,9 +66,9 @@ function logout(){
return_to('user/login');
}

/**
* Edit profile page.
*/
/**
* Edit profile page.
*/
function edit(){
global $user, $template;
login_required();
Expand All @@ -82,9 +82,9 @@ function edit(){
$template->render("user","edit",true);
}

/**
* User registration page.
*/
/**
* User registration page.
*/
function register(){
global $user, $template;
if($_POST){
Expand All @@ -102,5 +102,5 @@ function register(){
$template->set_title('Register');
$template->render("user","register",true);
}
}

}
1 change: 0 additions & 1 deletion lib/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,3 @@ function __($id) {
echo $template->variables[$id];
}

?>
Loading