Skip to content

Commit

Permalink
Simple PHP Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
imzcy committed Apr 27, 2013
1 parent 1d8e27e commit e95cd74
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 0 deletions.
34 changes: 34 additions & 0 deletions global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/* ##### Change Parameters Here ##### */

define('SITE_URL', 'http://survey.sitex.imzcy.com');
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'survey');
define('MYSQL_PASS', 'survey');
define('MYSQL_DBNAME', 'survey');


/* ^^^^^ End Of Change Parameter Here ^^^^^ */

/* ##### Don't Change Here If Not Have To ##### */

// Define Constants
$include_path = array();
define('SITE_ROOT', rtrim(dirname(__FILE__), '/\\') . DIRECTORY_SEPARATOR);
define('LIBRARY_ROOT', SITE_ROOT . 'library' . DIRECTORY_SEPARATOR);

define('SIGNIN_URL', SITE_URL . '/signin/');

define('UUID_LENGTH', 128);

date_default_timezone_set('Asia/Singapore');

// Load Loader Modules
require_once(LIBRARY_ROOT . 'Loader.php');

// Load Modules
LoadModule('core.LDefaultErrorHandler');

/* ^^^^^ End Of Don't Change Here If Not Have To ^^^^^ */
?>
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

require_once('global.php');

LoadModule('core.FAssertUser');
62 changes: 62 additions & 0 deletions library/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/********************************
*
* File Name: Loader.php
* Description: Module loader
*
*******************************/

function LoadModule($module, $params = NULL) {

$matches = array();
$reason = '';

$ret = preg_match('/^(?P<package>(?:[a-z\\d\\-]+\\.)+)(?P<name>(?P<flag>[lfc]{1})[a-z\\d\\-]+)$/i', $module, $matches);

if ($ret === FALSE) {
$reason = 'Illegal character in module name.';
goto errorHandler;
}

if ($ret === 0) {
$reason = 'There was an error in the package name you\'ve specified.';
goto errorHandler;
}

$package = $matches['package'];
$name = $matches['name'];
$flag = $matches['flag'];

$path = LIBRARY_ROOT . str_replace('.', DIRECTORY_SEPARATOR, $package) . $name . '.php';

if (!file_exists($path)) {
$reason = "Module not found. File path = '$path'.";
goto errorHandler;
}

require_once($path);
switch ($flag) {
case 'l': case 'L':
// Load functions from file.
// Functions are global by default
// Load only
return TRUE;

case 'f': case 'F':
// Load functions from file.
// Functions are global by default
// Load and execute
return $name($params);

case 'c': case 'C':
// Load classes from file.
// Must create return an object
return new $name($params);
}

errorHandler:
trigger_error("Error loading module '$module'. Reason: $reason.", E_USER_ERROR);
return FALSE;
}
?>
69 changes: 69 additions & 0 deletions library/core/CUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

class CUser {
private $isUser = '';
private $isModerator = '';
private $isAdministrator = '';
private $id = '';
private $email = '';
private $name = '';

function __construct($params) {
if (isset($params)) {
if (isset($params['id'])) {
// Create user by lookup token

$dbh = LoadModule('core.FDatabase');

$stmt = $dbh->prepare('SELECT id, email, name, is_user, is_moderator, is_administrator FROM user WHERE id = :id;');
$stmt->bindParam(':id', $params['id']);


if ($stmt->execute()) {
if ($stmt->rowCount() == 1) {
$result = $stmt->fetch();

$this->isUser = $result['is_user'];
$this->isModerator = $result['is_moderator'];
$this->isAdministrator = $result['is_administrator'];
$this->id = $result['id'];
$this->email = $result['email'];
$this->name = $result['name'];

return;
} else {
$this->isUser = 0;
$this->isModerator = 0;
$this->isAdministrator = 0;

return;
}
$this->isUser = 0;
$this->isModerator = 0;
$this->isAdministrator = 0;

return;
}
return;
}
} else {
// Return a new user

return;
}
}

function isUser() {
return $this->isUser;
}

function isModerator() {
return $this->isModerator;
}

function isAdministrator() {
return $this->isAdministrator;
}
}

?>
13 changes: 13 additions & 0 deletions library/core/FAssertAdministrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function FAssertUser($iPrivilege) {
$currentUser = LoadModule('core.FCurrentUser');

if ($currentUser->isAdministrator()) {
return;
}

header('Location', SIGNIN_URL);
}

?>
13 changes: 13 additions & 0 deletions library/core/FAssertModerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function FAssertUser($iPrivilege) {
$currentUser = LoadModule('core.FCurrentUser');

if ($currentUser->isModerator()) {
return;
}

header('Location', SITE_URL . SIGNIN_URL);
}

?>
14 changes: 14 additions & 0 deletions library/core/FAssertUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

function FAssertUser($iPrivilege) {
$currentUser = LoadModule('core.FCurrentUser');

if ($currentUser->isUser()) {
echo 'is a user';
return;
}

header('Location: ' . SIGNIN_URL);
}

?>
14 changes: 14 additions & 0 deletions library/core/FCurrentUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

function FCurrentUser() {
global $_CurrentUser;

if (isset($_CurrentUser)) {
return $_CurrentUser;
}

$_CurrentUser = LoadModule('core.CUser', array('id' => LoadModule('core.FToken')));
return $_CurrentUser;
}

?>
15 changes: 15 additions & 0 deletions library/core/FDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

function FDatabase() {
global $_Database;
echo isset($_Database);
if (!isset($_Database)) {
// Setup new connection
$_Database = new PDO('mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DBNAME, MYSQL_USER, MYSQL_PASS);
$_Database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}

return $_Database;
}

?>
7 changes: 7 additions & 0 deletions library/core/FPrivileges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

define('PRIVILEGE_USER', 0x1);
define('PRIVILEGE_MODERATOR', 0x2);
define('PRIVILEGE_ADMINISTRATOR', 0x4);

?>
28 changes: 28 additions & 0 deletions library/core/FRToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

function FRToken() {
$token = isset($_COOKIE['rtoken']) ? $_COOKIE['rtoken'] : '';

if (preg_match('/^[a-zA-Z\\d]{' . UUID_LENGTH . '}$/', $token)) {
$dbh = LoadModule('core.FDatabase');

// Remove expired token
$dbh->query('DELETE FROM token WHERE level = 1 AND time < UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -2 HOUR));');
$stmt = $dbh->prepare('SELECT id FROM token WHERE token = :token AND level = 1;');
$stmt->bindParam(':token', $token);

if ($stmt->execute()) {
if ($stmt->rowCount() == 1) {
$result = $stmt->fetch();
return $result['id'];
} else {
return -1;
}
return -1;
}
} else {
return -1;
}
}

?>
26 changes: 26 additions & 0 deletions library/core/FToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

function FToken() {
$token = isset($_COOKIE['token']) ? $_COOKIE['token'] : '';

if (preg_match('/^[a-zA-Z\\d]{' . UUID_LENGTH . '}$/', $token)) {
$dbh = LoadModule('core.FDatabase');

$stmt = $dbh->prepare('SELECT id FROM token WHERE token = :token AND level = 0;');
$stmt->bindParam(':token', $token);

if ($stmt->execute()) {
if ($stmt->rowCount() == 1) {
$result = $stmt->fetch();
return $result['id'];
} else {
return -1;
}
return -1;
}
} else {
return -1;
}
}

?>
15 changes: 15 additions & 0 deletions library/core/FUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

function FUuid() {
$len = UUID_LENGTH;
$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $l = 61;

$str = '';
while ($len-- > 0) {
$str .= $chars{mt_rand(0, $l)};
}

return $str;
}

?>
14 changes: 14 additions & 0 deletions library/core/LDefaultErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

function __LDefaultErrorHandler($errno, $errstr, $errfile, $errline) {
echo $errno . "\n";
echo $errstr . "\n";
echo $errfile . "\n";
echo $errline . "\n";

//die();
}

set_error_handler('__LDefaultErrorhandler', E_ALL);

?>

0 comments on commit e95cd74

Please sign in to comment.