phpircbot2/modules/userdb.class.php

129 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2010-11-21 23:13:54 +01:00
<?php
/*
* UserDB Class by JPT (http://jpt.redio.de)
*/
class userdb{
public $users;
public $timeout = 86400; //After $timeout seconds of inactivity, the session will expire.
private $db = "users.db";
/*
* General
*/
function __construct(){
$this -> load();
}
private function load(){
$this -> users = (file_exists($this -> db)) ? unserialize(file_get_contents($this -> db)) : array();
}
private function save(){
file_put_contents($this -> db, serialize($this -> users));
}
function __destruct(){
$this -> logoutAll();
$this -> save();
}
/*
* Userfunctions: adding, deleting, listing and verifying
*/
public function addUser($nick, $pw){
$this -> users[$nick]["pw"] = md5($pw);
}
public function listUsers(){
foreach($this -> users as $nick => $data){
$users[$nick] = $data["level"];
}
return $users;
}
public function isUser($nick){
return ($this -> users[$nick]) ? true : false;
}
public function delUser($nick){
unset($this -> users[$nick]);
}
/*
* User-Sessions: activity, verifying passwords, login + logout
*/
public function isActive($nick){
if(isset($nick) && isset($this -> users[$nick]) && ((time() - $this -> users[$nick]["lastactive"]) < $this -> timeout)){
$this -> update($nick);
return true;
}
else{
return false;
}
}
public function update($nick){
$this -> users[$nick]["lastactive"] = time();
$this -> save();
}
public function getLastActivity($nick){
return $this -> users[$nick]["lastactive"];
}
public function checkPW($nick, $pw){
return (md5($pw) == $this -> users[$nick]["pw"]) ? true : false;
}
public function updatePW($nick, $pw){
$this -> users[$nick]["pw"] = md5($pw);
}
public function logout($nick){
if($this -> isActive($nick)) $this -> users[$nick]["lastactive"] = 0;
$this -> save();
}
public function logoutAll(){
foreach($this -> users as $nick => $arr){
$this -> users[$nick]["lastactive"] = 0;
}
$this -> save();
}
/*
* Additional stuff: userlevels
*/
public function getLevel($nick){
if($this -> isActive($nick)) return $this -> users[$nick]["level"];
else return 0;
}
public function setLevel($nick, $level){
$this -> users[$nick]["level"] = $level;
}
/*
* Variable storage
*/
public function getVar($var, $nick){
return (isset($this -> users[$nick]["var"][$var])) ? $this -> users[$nick]["var"][$var] : false;
}
public function setVar($var, $value, $nick){
$this -> users[$nick]["var"][$var] = $value;
}
public function delVar($var, $nick){
unset($this -> users[$nick]["var"][$var]);
}
}
?>