2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-05-30 13:47:35 +00:00
|
|
|
function _new_user($row) {
|
|
|
|
return new User($row);
|
|
|
|
}
|
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/*
|
|
|
|
* An object representing a row in the "users" table.
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
class User {
|
2008-08-23 12:05:24 +00:00
|
|
|
var $config;
|
|
|
|
var $database;
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
var $id;
|
|
|
|
var $name;
|
|
|
|
var $email;
|
|
|
|
var $join_date;
|
|
|
|
var $admin;
|
2008-08-23 12:05:24 +00:00
|
|
|
|
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Initialisation *
|
|
|
|
* *
|
|
|
|
* User objects shouldn't be created directly, they should be *
|
|
|
|
* fetched from the database like so: *
|
|
|
|
* *
|
2009-05-30 13:47:35 +00:00
|
|
|
* $user = User::by_name("bob"); *
|
2008-08-23 12:05:24 +00:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
public function User($row) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->id = int_escape($row['id']);
|
|
|
|
$this->name = $row['name'];
|
|
|
|
$this->email = $row['email'];
|
|
|
|
$this->join_date = $row['joindate'];
|
|
|
|
$this->admin = ($row['admin'] == 'Y');
|
|
|
|
}
|
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
public static function by_session($name, $session) {
|
|
|
|
global $config, $database;
|
2008-08-23 12:05:24 +00:00
|
|
|
$row = $database->get_row(
|
2008-09-18 02:48:12 +00:00
|
|
|
"SELECT * FROM users WHERE name = ? AND md5(concat(pass, ?)) = ?",
|
2008-08-23 12:05:24 +00:00
|
|
|
array($name, get_session_ip($config), $session)
|
|
|
|
);
|
2009-05-11 14:04:33 +00:00
|
|
|
return is_null($row) ? null : new User($row);
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
public static function by_id($id) {
|
2008-08-23 12:05:24 +00:00
|
|
|
assert(is_numeric($id));
|
2009-05-11 14:04:33 +00:00
|
|
|
global $database;
|
2008-09-18 02:48:12 +00:00
|
|
|
$row = $database->get_row("SELECT * FROM users WHERE id = ?", array($id));
|
2009-05-11 14:04:33 +00:00
|
|
|
return is_null($row) ? null : new User($row);
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
public static function by_name($name) {
|
2008-08-23 12:05:24 +00:00
|
|
|
assert(is_string($name));
|
2009-05-11 14:04:33 +00:00
|
|
|
global $database;
|
2008-09-18 02:48:12 +00:00
|
|
|
$row = $database->get_row("SELECT * FROM users WHERE name = ?", array($name));
|
2009-05-11 14:04:33 +00:00
|
|
|
return is_null($row) ? null : new User($row);
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 14:04:33 +00:00
|
|
|
public static function by_name_and_hash($name, $hash) {
|
2008-08-23 12:05:24 +00:00
|
|
|
assert(is_string($name));
|
|
|
|
assert(is_string($hash));
|
|
|
|
assert(strlen($hash) == 32);
|
2009-05-11 14:04:33 +00:00
|
|
|
global $database;
|
2008-09-18 02:48:12 +00:00
|
|
|
$row = $database->get_row("SELECT * FROM users WHERE name = ? AND pass = ?", array($name, $hash));
|
2009-05-11 14:04:33 +00:00
|
|
|
return is_null($row) ? null : new User($row);
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2009-05-30 13:47:35 +00:00
|
|
|
public static function by_list($offset, $limit=50) {
|
|
|
|
assert(is_numeric($offset));
|
|
|
|
assert(is_numeric($limit));
|
|
|
|
global $database;
|
|
|
|
$rows = $database->get_all("SELECT * FROM users WHERE id >= ? AND id < ?", array($offset, $offset+$limit));
|
|
|
|
return array_map("_new_user", $rows);
|
|
|
|
}
|
|
|
|
|
2008-08-23 12:05:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* useful user object functions start here
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
public function is_anonymous() {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config;
|
|
|
|
return ($this->id == $config->get_int('anon_id'));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function is_admin() {
|
|
|
|
return $this->admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set_admin($admin) {
|
2008-08-23 12:05:24 +00:00
|
|
|
assert(is_bool($admin));
|
2009-05-11 14:04:33 +00:00
|
|
|
global $database;
|
2007-04-16 11:58:25 +00:00
|
|
|
$yn = $admin ? 'Y' : 'N';
|
2009-05-11 14:04:33 +00:00
|
|
|
$database->Execute("UPDATE users SET admin=? WHERE id=?", array($yn, $this->id));
|
2009-05-08 10:52:29 +00:00
|
|
|
log_info("core-user", "Made {$this->name} admin=$yn");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:51:41 +00:00
|
|
|
public function set_password($password) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $database;
|
2007-11-04 03:51:41 +00:00
|
|
|
$hash = md5(strtolower($this->name) . $password);
|
2009-05-11 14:04:33 +00:00
|
|
|
$database->Execute("UPDATE users SET pass=? WHERE id=?", array($hash, $this->id));
|
2009-05-08 10:52:29 +00:00
|
|
|
log_info("core-user", "Set password for {$this->name}");
|
2007-11-04 03:51:41 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
?>
|