-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ^^^^^ */ | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
require_once('global.php'); | ||
|
||
LoadModule('core.FAssertUser'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
?> |