Initial Commit

This commit is contained in:
Daniel Mason 2016-12-30 18:16:13 +13:00
commit 391177596e
41 changed files with 1233 additions and 0 deletions

1
includes/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
config.php

View file

@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: daniel
* Date: 12/30/16
* Time: 2:09 PM
*/
class Answer extends DataItem
{
public static function _getClass()
{
return "Answer";
}
public static function _getType()
{
return "answer";
}
public function getAnswer()
{
return $this->answerdata_content;
}
public function isCorrect()
{
return $this->answerdata_correct;
}
}

View file

@ -0,0 +1,82 @@
<?php
class DataItem {
protected $_db;
function __construct() {
$this->_db = new db(); //Will optimise this to get existing conn at some point.
}
public function getById($id) {
$this->_db->query("SELECT * FROM `".static::_getType()."` WHERE ".static::_getType()."_id = :id");
$this->_db->bind(":id", $id);
return $this->_db->getObject();
}
private function _getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$Objarr = array();
$typeName = static::_getType();
$className = static::_getClass();
$this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy." ":" ").($limit?"LIMIT ".$limit:""));
$results = $this->_db->resultset();
if(!$results) return false;
foreach ($results as $result) {
$obj = new $className();
foreach ($result as $key=>$val) {
$obj->$key = $val;
}
$Objarr[] = $obj;
}
return $Objarr;
}
public static function getAllWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$me = new static();
return $me->_getAllWhere($where, $orderBy, $join, $limit);
}
private function _getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$typeName = static::_getType();
$className = static::_getClass();
$this->_db = new db();
$this->_db->query("SELECT * FROM `".$typeName."` ".($join?$join:"").($where?" WHERE ".$where."":"")." ".($orderBy?" ".$orderBy."":"").($limit?"LIMIT ".$limit:""));
$result = $this->_db->single();
if(!$result) return false;
$obj = new $className();
foreach ($result as $key=>$val) {
$obj->$key = $val;
}
return $obj;
}
public static function getWhere($where = false, $orderBy = false, $join = false, $limit = false) {
$me = new static();
return $me->_getWhere($where, $orderBy, $join, $limit);
}
public function getId() {
$typeName = static::_getType();
$id = $typeName."_id";
return $this->$id;
}
public static function create($PDOobj)
{
$className = static::_getClass();
$obj = new $className();
foreach ($PDOobj as $key=>$val) {
$obj->$key = $val;
}
return $obj;
}
protected function _getTime() {
return static::_getType()."_time";
}
public function getTime() {
$timeColumn = static::_getCreationTime();
return $this->$timeColumn;
}
}

96
includes/classes/class.db.php Executable file
View file

@ -0,0 +1,96 @@
<?php
class db {
private $host = DB_HOSTNAME;
private $user = DB_USERNAME;
private $pass = DB_PASSWORD;
private $dbname = DB_DATABASE;
private $stmt;
private $dbh;
public function __construct(){
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch (PDOException $e) {
error_log($e->getMessage());
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultSet(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function getObject() {
$this->execute();
return $this->stmt->fetchObject();
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
public function kill(){
$this->dbh = null;
}
}

View file

@ -0,0 +1,40 @@
<?php
/**
* Created by PhpStorm.
* User: daniel
* Date: 12/30/16
* Time: 2:09 PM
*/
class Question extends DataItem {
public static function _getClass()
{
return "Question";
}
public static function _getType()
{
return "question";
}
public static function getQuestions($count = false)
{
$questions = static::getAllWhere(false, false, false, $count);
foreach ($questions as $q) {
$q->answers = $q->getAnswers();
shuffle($q->answers);
}
return $questions;
}
public function getQuestion()
{
return $this->questiondata_content;
}
public function getAnswers()
{
return Answer::getAllWhere("answerdata_question = ".$this->getId());
}
}

64
includes/classes/class.user.php Executable file
View file

@ -0,0 +1,64 @@
<?php
class User extends DataItem {
public $user_id;
public $userdata_username;
public $userdata_password;
public static function _getClass() {
return "User";
}
public static function _getType() {
return "user";
}
public static function login($userName = false, $password = false)
{
if(isset($_SESSION['userName']) && isset($_SESSION['userId'])) return true;
if(isset($userName) && isset($password)) {
return self::_login($userName, $password, false);
}
return false;
}
private static function _login($userName = false, $password = false, $token = false) {
if($token && !$password) {
//query token
} else if ($userName && $password) {
$user = User::getWhere("userdata_username = '".$userName."'");
if (is_object($user)) {
if (password_verify($password, $user->_getHash())) {
setcookie("userName", $user->getUserName(), COOKIE_EXPIRY);
$_SESSION['username'] = $user->getUserName();
$_SESSION['userId'] = $user->getId();
return true;
}
}
}
return false;
}
public static function getUserFromSession()
{
return self::getWhere("user_id = '".$_SESSION['userId']."'");
}
public static function register()
{
}
public function _getHash()
{
return $this->userdata_password;
}
public function getUserName()
{
return $this->userdata_username;
}
}

5
includes/config.php.example Executable file
View file

@ -0,0 +1,5 @@
<?php
define("DB_HOSTNAME", "mysqlserver");
define("DB_DATABASE", "databasename");
define("DB_USERNAME", "mysqlusername");
define("DB_PASSWORD", "mysqlpassword");

30
includes/createDb.sql Executable file
View file

@ -0,0 +1,30 @@
-- base sql
CREATE TABLE IF NOT EXISTS `user` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT,
`user_time` INT(11) NOT NULL,
`userdata_username` varchar(256) NOT NULL,
`userdata_password` varchar(256) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id_UNIQUE` (`user_id`),
UNIQUE KEY `userdata_username_UNIQUE` (`userdata_username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `question` (
`question_id` INT(11) NOT NULL AUTO_INCREMENT,
`question_time` INT(11) NOT NULL,
`questiondata_number` VARCHAR(10) DEFAULT NULL,
`questiondata_content` TEXT DEFAULT NULL,
`questiondata_image` TEXT DEFAULT NULL,
PRIMARY KEY (`question_id`),
UNIQUE KEY `question_id_UNIQUE` (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `answer` (
`answer_id` INT(11) NOT NULL AUTO_INCREMENT,
`answer_time` INT(11) NOT NULL,
`answerdata_question` INT(11) NOT NULL,
`answerdata_content` MEDIUMTEXT DEFAULT NULL,
`answerdata_correct` INT(1) DEFAULT 0,
PRIMARY KEY (`answer_id`),
UNIQUE KEY `answer_id_UNIQUE` (`answer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

8
includes/include.php Executable file
View file

@ -0,0 +1,8 @@
<?php
require_once('config.php'); //DB CREDENTIALS
require_once('classes/class.db.php');
require_once('classes/class.dataitem.php');
require_once('classes/class.user.php');
require_once('classes/class.question.php');
require_once('classes/class.answer.php');