/mvc/models/account
[return to app]1
<?php
/**
* Functions required by the account controller.
*/
class accountModel extends model {
/**
* Table/collection name - only need to change this if you will be operating multiple Vork account instances
* on the same database
* @var string
*/
protected $_table = 'vorkusers';
/**
* Database type to use
* @var string Either sql or mongo
*/
protected $_db;
/**
* Mongo collection object cache
* @var MongoCollection
*/
protected $_mongo;
/**
* Sets $this->_db and if it is Mongo then it sets the MongoDB collection
*/
public function __construct() {
$this->_db = (!in_array('mongo', config::$modelObjects) ? 'sql' : 'mongo');
}
/**
* Switchboard to route to either SQL or Mongo methods
*
* @param string $name
* @param array $args
*/
public function __call($name, array $args) {
$name = '_' . $this->_db . ucfirst($name);
if ($this->_db == 'mongo' && !$this->_mongo) {
$this->_mongo = $this->mongo->selectCollection($this->_table);
}
return call_user_func_array(array($this, $name), $args);
}
/**
* The columns returned must include the data defined in the $_sessionKeys property of the account component
*
* @param string $email
* @param string $pass
* @return mixed Data array or Boolean false
*/
protected function _sqlValidateLogin($email, $pass) {
$sql = 'select * from ' . $this->_table . ' where email=' . $this->db->cleanString($email)
. ' and pass=' . $this->db->cleanString($pass);
$res = $this->db->query($sql);
return $res->fetch_assoc();
}
protected function _mongoValidateLogin($email, $pass) {
return $this->_mongo->findOne(array('email' => $email, 'pass' => $pass));
}
/**
* Returns a users password if a record exists for them
*
* @param string $email
* @return mixed Password string or Boolean false
*/
protected function _sqlRetrievePass($email) {
$sql = 'select pass from ' . $this->_table . ' where email=' . $this->db->cleanString($email);
$res = $this->db->query($sql);
$pass = $res->fetch_row();
return ($pass ? current($pass) : false);
}
protected function _mongoRetrievePass($email) {
$row = $this->_mongo->findOne(array('email' => $email), array('pass' => true));
return ($row ? $row['pass'] : false);
}
/**
* Checks if there is already an account registered to an email
*
* @param string $email
* @return boolean
*/
protected function _sqlAccountExists($email) {
$sql = 'select email from ' . $this->_table . ' where email=' . $this->db->cleanString($email);
$res = $this->db->query($sql);
return (boolean) $res->fetch_row();
}
protected function _mongoAccountExists($email) {
return (boolean) $this->_mongoRetrievePass($email);
}
/**
* Adds a user
* If using the SQL method and the table does not exist it will get created
* @param string $email
* @param string $pass
* @param array $cols Optional additional data columns
* @return int
*/
protected function _sqlAddUser($email, $pass, array $cols = array()) {
try {
$cols['email'] = $email;
$cols['pass'] = $pass;
$sqlInsert = array('table' => $this->_table, 'vals' => $this->db->cleanString($cols));
$isMysql = (isset($dbParents['mysqli']) || isset($dbParents['mysql']));
if (!$isMysql) {
$sql = 'select max(_id) from ' . $this->_table;
$res = $this->db->query($sql);
$_id = (int) current($res->fetch_row());
$sqlInsert['vals']['_id'] = ++$_id;
}
$sql = $this->db->insertSql($sqlInsert);
$this->db->query($sql);
if ($isMysql) {
$_id = $this->db->insert_id;
}
return $_id;
} catch (Exception $e) {
$this->_sqlInit();
}
}
protected function _mongoAddUser($email, $pass, array $cols = array()) {
if (!isset($cols['_id'])) {
$_id = $this->_mongo->find(array(), array('_id' => true))->sort(array('_id' =>
-1))->limit(1)->getNext();
$_id = ($_id ? (int) $_id['_id'] : 0);
$cols['_id'] = ++$_id;
}
$this->_mongo->ensureIndex(array('email' => 1), array('unique' => true));
$cols['email'] = $email;
$cols['pass'] = $pass;
$this->_mongo->insert($cols);
return $cols['_id'];
}
/**
* Update user data
*
* @param string $_id
* @param array $args
*/
protected function _sqlUpdateUser($_id, array $args) {
$args = $this->db->cleanString($args);
foreach ($args as $key => $val) {
$sqlCols[] = $key . '=' . $val;
}
$sql = 'update ' . $this->_table . ' set ' . implode(',', $sqlCols) . ' where _id=' . (int) $_id;
$this->db->query($sql);
}
protected function _mongoUpdateUser($_id, array $args) {
$args['_id'] = $_id;
$this->_mongo->save($args);
}
/**
* Sets the initial login date of a user
* @param string $_id
*/
protected function _sqlSetInitialLogin($_id) {
$sql = 'update ' . $this->_table . ' set initiallogin=now() where _id=' . (int) $_id;
$this->db->query($sql);
}
protected function _mongoSetInitialLogin($_id) {
$update = array('$set' => array('initiallogin' => date('c'), 'initialLoginTs' => time()));
$this->_mongo->update(compact('_id'), $update);
}
/**
* Creates the SQL database table with the minimal required columns (_id, name, email, pass)
*/
protected function _sqlInit() {
$isMysql = (isset($dbParents['mysqli']) || isset($dbParents['mysql']));
$sql = 'create table ' . $this->_table . ' (
_id mediumint(7) unsigned not null primary key';
if ($isMysql) {
$sql .= ' auto_increment';
}
$sql .= ', name varchar(60) not null, email varchar(60) not null unique, pass varchar(16) not null'
. ', initiallogin datetime null)';
$this->db->query($sql);
}
}

