This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/user.class.php

158 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/** @private */
2009-05-30 13:47:35 +00:00
function _new_user($row) {
return new User($row);
}
2009-07-19 07:38:13 +00:00
/**
* An object representing a row in the "users" table.
2009-07-19 07:38:13 +00:00
*
* The currently logged in user will always be accessable via the global variable $user
*/
class User {
var $id;
var $name;
var $email;
var $join_date;
var $admin;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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"); *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2009-07-19 07:38:13 +00:00
/**
* One will very rarely construct a user directly, more common
* would be to use User::by_id, User::by_session, etc
*/
public function User($row) {
$this->id = int_escape($row['id']);
$this->name = $row['name'];
$this->email = $row['email'];
$this->join_date = $row['joindate'];
$this->admin = ($row['admin'] == 'Y');
}
public static function by_session($name, $session) {
global $config, $database;
if($database->engine->name == "mysql") {
$query = "SELECT * FROM users WHERE name = ? AND md5(concat(pass, ?)) = ?";
}
else {
$query = "SELECT * FROM users WHERE name = ? AND md5(pass || ?) = ?";
}
$row = $database->get_row($query, array($name, get_session_ip($config), $session));
return is_null($row) ? null : new User($row);
}
public static function by_id($id) {
assert(is_numeric($id));
global $database;
$row = $database->get_row("SELECT * FROM users WHERE id = ?", array($id));
return is_null($row) ? null : new User($row);
}
public static function by_name($name) {
assert(is_string($name));
global $database;
$row = $database->get_row("SELECT * FROM users WHERE name = ?", array($name));
return is_null($row) ? null : new User($row);
}
public static function by_name_and_hash($name, $hash) {
assert(is_string($name));
assert(is_string($hash));
assert(strlen($hash) == 32);
global $database;
$row = $database->get_row("SELECT * FROM users WHERE name = ? AND pass = ?", array($name, $hash));
return is_null($row) ? null : new User($row);
}
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);
}
/*
* useful user object functions start here
*/
2009-07-19 07:38:13 +00:00
/**
* Test if this user is anonymous (not logged in)
*
* @retval bool
2009-07-19 07:38:13 +00:00
*/
public function is_anonymous() {
global $config;
return ($this->id == $config->get_int('anon_id'));
}
2010-01-12 15:01:34 +00:00
/**
* Test if this user is logged in
*
* @retval bool
*/
public function is_logged_in() {
global $config;
return ($this->id != $config->get_int('anon_id'));
}
2009-07-19 07:38:13 +00:00
/**
* Test if this user is an administrator
*
* @retval bool
2009-07-19 07:38:13 +00:00
*/
public function is_admin() {
return $this->admin;
}
public function set_admin($admin) {
assert(is_bool($admin));
global $database;
$yn = $admin ? 'Y' : 'N';
$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");
}
public function set_password($password) {
global $database;
$hash = md5(strtolower($this->name) . $password);
$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}");
}
2009-08-11 16:07:03 +00:00
public function set_email($address) {
global $database;
$database->Execute("UPDATE users SET email=? WHERE id=?", array($address, $this->id));
log_info("core-user", "Set email for {$this->name}");
}
2009-10-08 01:57:28 +00:00
/**
* Get a snippet of HTML which will render the user's avatar, be that
* a local file, a remote file, a gravatar, a something else, etc
*/
public function get_avatar_html() {
// FIXME: configurable
2009-10-08 11:40:52 +00:00
global $config;
if($config->get_string("avatar_host") == "gravatar") {
if(!empty($this->email)) {
$hash = md5(strtolower($this->email));
2010-04-21 16:56:01 +00:00
$s = $config->get_string("avatar_gravatar_size");
$d = $config->get_string("avatar_gravatar_default");
$r = $config->get_string("avatar_gravatar_rating");
return "<img class=\"avatar gravatar\" src=\"http://www.gravatar.com/avatar/$hash.jpg?s=$s&d=$d&r=$r\">";
2009-10-08 11:40:52 +00:00
}
2009-10-08 01:57:28 +00:00
}
return "";
}
}
?>