<?php
/**
*
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2009 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class profile extends dbobject {
private static $REALM;
private static $PROFILE_CACHE;
public function __construct($cnf){
if ( FALSE === @constant('CORE_AUTH') ){
parent::__construct($cnf);
return;
} //no auth, profile as ip
if ( !isset(self::$REALM) ){
$pid = browsing::get_active();
$realm = pages::get_realm($pid);
if ( empty($realm) ){ return; } //no page realm, so no auth, so no profile.
self::$REALM = new realm($realm);
if ( !self::$REALM->isloged() ){ self::$REALM->authenticate(); }
}
parent::__construct($cnf);
}
private function precheck(){
if ( FALSE === @constant('CORE_AUTH') ){ return client::host(); } //no auth, profile as ip
if ( !isset(self::$REALM) ){ return FALSE; }
if ( !self::$REALM->isloged() ){ return FALSE; }
return self::$REALM->logedas();
}
public static function client(){
if ( FALSE === @constant('CORE_AUTH') ){ return client::host(); } //no auth, profile as ip
if ( !isset(self::$REALM) ){ return FALSE; }
if ( !self::$REALM->isloged() ){ return FALSE; }
return self::$REALM->logedas();
}
public function isdefined($ref){
if ( FALSE === ( $ruid = $this->precheck()) ){ return FALSE; }
if ( isset(self::$PROFILE_CACHE[md5($ruid)][$ref]) ){ return (self::$PROFILE_CACHE[md5($ruid)][$ref] !== FALSE); }
$arg=Array('ref'=>md5($ruid), 'var'=>$ref);
$r = $this->dbquery($arg, 'exists');
if ( $r ){
$data = $this->dbquery($arg, 'read', 'data');
self::$PROFILE_CACHE[md5($ruid)][$ref] = $data;
} else {
self::$PROFILE_CACHE[md5($ruid)][$ref] = FALSE;
}
return $r;
//dbispresent on ref, $ruid
}
public function read($ref){
if ( FALSE === $this->isdefined($ref) ){ return FALSE; }
if ( FALSE === ( $ruid = $this->precheck()) ){ return FALSE; }
if ( isset(self::$PROFILE_CACHE[md5($ruid)][$ref]) ){ return unserialize(base64_decode(self::$PROFILE_CACHE[md5($ruid)][$ref])); }
$arg=Array('ref'=>md5($ruid), 'var'=>$ref);
$data = $this->dbquery($arg, 'read', 'data');
self::$PROFILE_CACHE[md5($ruid)][$ref] = $data;
return unserialize(base64_decode($data));
//dbread on ref, logedas
}
public function set($ref, $val){
if ( FALSE === ( $ruid = $this->precheck()) ){ return FALSE; }
$arg=Array('ref'=>md5($ruid), 'var'=>$ref, 'data'=>base64_encode(serialize($val)) );
self::$PROFILE_CACHE[md5($ruid)][$ref] = $arg['data'];
if ( $this->isdefined($ref) ){ return $this->dbquery($arg, 'update'); }
return $this->dbquery($arg, 'create');
}
public function del($ref){
if ( FALSE === ( $ruid = $this->precheck()) ){ return FALSE; }
if ( isset(self::$PROFILE_CACHE[md5($ruid)][$ref]) ){ unset(self::$PROFILE_CACHE[md5($ruid)][$ref]); }
$arg=Array('ref'=>md5($ruid), 'var'=>$ref);
return $this->dbquery($arg, 'delone');
//dbdel on ref, logedas
}
public function clear(){
if ( FALSE === ( $ruid = $this->precheck()) ){ return FALSE; }
if ( isset(self::$PROFILE_CACHE[md5($ruid)]) ){ unset(self::$PROFILE_CACHE[md5($ruid)]); }
$arg=Array('ref'=>md5($ruid));
return $this->dbquery($arg, 'delbyref');
//dbdel on ref, logedas
}
}