File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/lib/class.user.php
<?php
/**
* This file contains the user object to handle users along with its exception. The base usage is for account details
*
* @package user
**/
define('UserError', 1);
define('UserWarning', 2);
define('UserNotice', 3);
/**
* This is the user object to handle such things as account details
*
* @package user
**/
class UserClass extends iwp_engine {
/**
* The array that holds the data for this user
*
* @var Array $data
**/
protected $data = array();
/**
* The table name in use for this user
*
* @var String $table
**/
protected $table;
/**
* The database object in use
*
* @var Object $db
**/
protected $_db;
/**
* This is a list of the fields within the table that are absolutely necessary (child classes may change / add as needed)
*
* @var Array $tableFields
**/
protected $tableFields = array(
"userid",
"username",
"firstname",
"lastname",
"password",
"status",
);
/**
* The default constructor will be empty, unless something's needed of course
**/
public function __construct() {
parent::__construct();
}
/**
* To handle working with member variables for the user
*
* @param String $name The name of the item
* @param Mixed $val The value to set the item to
* @throws UserException
**/
public function Set($name, $val='') {
if(is_array($name)) {
foreach($name as $key=>$val) {
if($key === "userid" && (int)$val < 1) {
throw new UserException("The User ID field requires an Integer.", UserError);
}
$this->data[$key] = $val;
}
}else{
if(!in_array($name, $this->tableFields)) {
throw new UserException("Table field '" . htmlentities($name) ."' does not exist.", UserError);
}
if($name === "userid" && !is_int($val)) {
throw new UserException("The User ID field requires an Integer.", UserError);
}
$this->data[$name] = $val;
}
}
/**
* Will retrieve the variable
*
* @throws UserException
**/
public function Get($name) {
if(!isset($this->data[$name])) {
// var_dump($this->data);
throw new UserException('Invalid variable "'.htmlspecialchars($name).'" requested.', UserWarning);
}
return $this->data[$name];
}
/**
* To set the name of the table in the database to be used
*
* @param String $table The table name in use
* @throws UserException
**/
public function setTable($table) {
if(is_string($table)) {
$this->Set('table', $table);
} else {
throw new UserException("Table name requires a string.", UserError);
}
}
/**
* Will specify the DB object to be used
*
* @param Object $db The database object in use
* @throws UserException
**/
public function setDb(&$_db) {
if(is_object($_db)) {
$this->_db = $_db;
} else {
throw new UserException("A valid database object is required.", UserError);
}
}
/**
* Will save the current user to the database
*
* @return Boolean Success / Failure
public function Save() {
// Check to ensure all data is set
if(!$this->isComplete()) {
throw new UserException("All data for the user must be set", UserError);
}
$sql = '';
if(is_null($this->data['userid'])) {
// Creating the user
$sql = 'INSERT INTO ' . $this->Get('tableName') . ' (`username`) VALUES (\'' . $this->Get('username') . '\')';
} else {
// Running an update
$sql = 'UPDATE ' . $this->Get('tableName') . ' SET `username`=\'' . $this->Get('username') . '\', ``=\'\' WHERE `userid`=\'' . $this->Get('userid') . '\'';
}
// Run the query
$this->runQuery($sql);
}
**/
/**
* Checks the contents of the data array and ensures values have been added where necessary
*
* @return Boolean
**/
private function isComplete() {
reset($this->Get('tableFields'));
while(list($val) = each($this->Get('tableFields'))) {
if($val !== "userid" && (!$this->Get($val) || $this->Get($val) == "")) {
return false;
}
}
reset($this->Get('tableFields'));
return true;
}
/**
* Will run the provided SQL, this function is designed to be changed in a single location to make life easier for use with different DB objects
*
* @param String $sql The SQL statement to be run
* @return Resource The result set from the provided query
* @throws UserException
**/
private function runQuery($sql) {
if(!is_object($this->db)) {
throw new UserException("A valid database object must be loaded.", UserError);
}
$res = $this->db->Query($sql);
return $res;
}
/**
* Fetch a row from a result resource
*
* @param resource $resource The resource to fetch the next result from
* @return Array The next row from the result
**/
public function fetchResult(&$resource) {
// Fetch the next result
$res = $this->_db->Fetch($resource);
return $res;
}
}
/**
* The exception for the UserClass
*
* @package user
**/
class UserException extends Exception {
/**
* A variable to hold the severity of the error
*
* @var String $severity
**/
private $severity;
/**
* Changing the constructor to handle the severity
*
* @param String $message The exception message being thrown
* @param Integer $severity The degree of severity for this exception
**/
public function __construct($message, $severity) {
$this->message = $message;
$this->severity = $severity;
}
/**
* Will retrieve the severity that was placed for this exception
*
* @return Integer The severity of the exception
**/
public function getSeverity() {
return $this->severity;
}
}