<?php
/**
 * CDS for N.u.C.o.K.e.
 * Requires N.u.C.o.K.e. v2.0.1664
 * © Hsilamot 2011
 * developed by Hidalgo Rionda
 * This library is intended to be used inside N.u.C.o.K.e. Class
 *
 * +----------------------------------------------------------------------+
 * |                             CDS v 1.0.141                            |
 * |             released under Creative Commons BY-NC-SA 3.0             |
 * |          http://creativecommons.org/licenses/by-nc-sa/3.0/           |
 * +----------------------------------------------------------------------+
 *
 * +----------------------------------------------------------------------+
 * |                              changelog                               |
 * | v1.0.1 (Hsilamot) first release                                      |
 * +----------------------------------------------------------------------+
 *
 * @access public
 * @author Hsilamot <php@hsilamot.com>
 * @license http://creativecommons.org/licenses/by-nc-sa/3.0/
 * @version 1.0.141
 */
class CDS extends NuCoKe {
	/**
	 * parent nucoke
	 * @access public
	 * @var object nucoke object
	 */
	private $nucoke;
	/**
	 * session data
	 * @access public
	 * @var array session data
	 */
	private $session=false;
	/**
	 * user token data
	 * @access private
	 * @var array user token data
	 */
	private $token=false;
	/**
	 * user data
	 * @access public
	 * @var array user data
	 */
	private $user=false;
	/**
	 * will initialize the class
	 * @access public
	 * @version 1.0.132
	 * @param array $config array containing any extra configuration from the user
	 * @param object $nucoke previously initialized nucoke
	 * @return boolean if success
	 */
	public function __construct($config=array(),$nucoke=null) {
		$this->nucoke = $nucoke;
		parent::__construct(null,$nucoke);
		$version = $this->version(); $this->user_agent_add('CDS v'.$version[0].'.'.$version[1]); /* will add self to the user agent string */
		/* we will now set the default configuration */
		$this->config['cds_db'] = null;
		$this->config['cds_table'] = null;
		$this->config['cds_sess_table'] = null;
		$this->config['cds_token_table'] = null;
		$this->config['cds_guest'] = null;
		$this->config['cds_crypto'] = 'CDS_Default';
		$this->config['cookie'] = 'nucoke_session';
		$this->config['login'] = 'nucoke_login';
		if ((php_sapi_name()=='cli')) {
			$this->config['domain'] = $_SERVER['HOSTNAME'];
		} else {
			$this->config['domain'] = $_SERVER['HTTP_HOST'];
		}
		/* we will parse any configuration added from user end */
		if (!is_array($config)) { $config = array(); trigger_error('NuCoKe (CDS): Unknown configuration type passed on __initialize($config)',E_USER_NOTICE); }
		foreach ($config as $name => $value) {
			switch ($name) {
				case 'database':
					$this->config['cds_db'] = $value; break;
				case 'table':
					$this->config['cds_table'] = $value; break;
				case 'tokens':
					$this->config['cds_token_table'] = $value; break;
				case 'sessions':
					$this->config['cds_sess_table'] = $value; break;
				case 'guestid':
					$this->config['cds_guest'] = $value; break;
				case 'crypto':
					$this->config['cds_crypto'] = $value; break;
				case 'cookie':
					$this->config['cds_cookie'] = $value; break;
				case 'domain':
					$this->config['cds_cookie_domain'] = $value; break;
				case 'login':
					$this->config['cds_login_cookie'] = $value; break;
				default:
					trigger_error('NuCoKe (CDS): Unknown configuration name \''.$name.'\'',E_USER_NOTICE); break;
			}
		}
		$session = $this->session_init();
		if (!$session) {
			return false;
		}
		if (isset($_COOKIE[$this->config['cds_login_cookie']])) {
			$login = $this->login_token($_COOKIE[$this->config['cds_login_cookie']]);
			if ($login) {
				return true;
			}
		}
		$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$this->config['cds_guest'].'\' LIMIT 0,1';
		$user = $this->sql($sql,$this->config['cds_db']);;
		if (count($user)>0) {
			$user[0]['meta'] = unserialize($user[0]['meta']);
			$this->user = $user[0];
			return true;
		}
		return false;
	}
	/**
	 * verifies if user has access to certain area
	 * @access public
	 * @version 1.0.26
	 * @return bool if access is granted
	 */
	public function access($level,$user=false) {
		if ($user) {
			$get_info = $this->get_user_info($user);
			$access = explode(',',$get_info['access']);
		} else {
			$access = explode(',',$this->user['access']);
		}
		if (in_array($level,$access)) {
			return true;
		}
		if (in_array('root',$access)) {
			return true;
		}
		return false;
	}
	/**
	 * Grants user level access
	 * @access public
	 * @version 1.0.1
	 * @param string $level level to grant
	 * @param int $user user to grant for (if not specified grants for current user)
	 * @return bool true if success
	 */
	public function access_grant($level,$user=false) {
		if ($user) {
			$get_info = $this->get_user_info($user);
			$access = explode(',',$get_info['access']);
			$userid = $get_info['ID'];
		} else {
			$access = explode(',',$this->user['access']);
			$userid = $this->user['ID'];
		}
		if (!in_array($level,$access)) {
			$access[] = $level;
			$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `access` = \''.implode(',',$access).'\' WHERE `ID` = \''.$userid.'\' LIMIT 1';
			$this->sql($sql);
			return true;
		}
		return true;
	}
	/**
	 * will change users password but will set innactive all current tokens of the user
	 * @access public
	 * @version 1.0.0
	 * @return bool if password was changed
	 */
	public function change_password($password,$newpassword) {
		if ($this->check_password($password)) {
			$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$this->user['ID'].'\' LIMIT 0,1';
			$search = $this->sql($sql,$this->config['cds_db']);;
			if (count($search)>0) {
				$search = $search[0];
				$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `pass` = UNHEX(\''.$this->crypt_do($newpassword).'\') WHERE `ID` = \''.$search['ID'].'\'';
				$this->sql($sql,$this->config['cds_db']);;
				if ($this->check_password($newpassword)) {
					$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `status` = b\'0\' WHERE `user` = \''.$search['ID'].'\' AND `status` = b\'1\'';
					$this->sql($sql,$this->config['cds_db']);;
					return true;
				}
			}
		}
		return false;
	}
	/**
	 * check if password is correct
	 * @access public
	 * @version 1.0.0
	 * @return bool if password is correct
	 */
	public function check_password($password) {
		if (strlen($password)<1) {
			return false;
		}
		$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$this->user['ID'].'\' LIMIT 0,1';
		$search = $this->sql($sql,$this->config['cds_db']);;
		if (count($search)>0) {
			$search = $search[0];
			if (sha1($this->crypt_undo(bin2hex($search['pass'])))==sha1($password)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * will encript a string using the key on configuration
	 * @access public
	 * @version 1.3.451
	 * @param string $text text to encrypt
	 * @return string encripted string
	 */
	public function crypt_do($text) {
		$sal = rand(0,4294967295);
		$salt = rand(0,4294967295);
		if ($sal==$salt) {
			$urand = fopen('/dev/urandom','rb');
			$datos = fread($urand,4);
			fclose($urand);
			$salt = hexdec(bin2hex($datos));
		}
		$salt = strtoupper(dechex($salt));
		while (strlen($salt)<8) { $salt = '0'.$salt; }
		$clave = substr(sha1($this->config['cds_crypto']),0,24);
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size,MCRYPT_RAND);
		$text = '1.5>'.$text;
		$encriptado = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$clave.$salt,$text,MCRYPT_MODE_ECB,$iv);
		$final = strtoupper(bin2hex($encriptado));
		$final = $salt.$final;
		return $final;
	}
	/**
	 * will decript a string using the key on configuration
	 * @access public
	 * @version 1.3.435
	 * @param string $text text to decrypt
	 * @return string decripted string
	 */
	public function crypt_undo($text) {
		$clave = substr(sha1($this->config['cds_crypto']),0,24);
		$text = strtoupper($text);
		$salt = substr($text,0,8);
		$text = substr($text,8);
		$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB);
		$iv = mcrypt_create_iv($iv_size,MCRYPT_RAND);
		$procesar = pack('H*',$text);
		$desencriptado = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$clave.$salt,$procesar,MCRYPT_MODE_ECB,$iv);
		$final = trim($desencriptado);
		if (substr($final,0,4)!='1.5>') { return false; }
		$final = substr($final,4);
		return $final;
	}
	/**
	 * gets current token
	 * @access public
	 * @version 1.0.1
	 * @return string current username
	 */
	public function get_token() {
		return $this->token;
	}
	/**
	 * gets current username
	 * @access public
	 * @version 1.0.8
	 * @param string $user to obtain user id from another user
	 * @return string current username
	 */
	public function get_user($user=false) {
		if ($user) {
			$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$this->sql_vaccine($user).'\' OR lower(`user`) = \''.strtolower($this->sql_vaccine($user)).'\' LIMIT 0,1';
			$user = $this->sql($sql);
			if ($user) {
				$user = $user[0];
				return $user['user'];
			} else {
				return false;
			}
		} else {
			return $this->user['user'];
		}
	}
	/**
	 * gets user information
	 * @access public
	 * @version 1.0.27
	 * @param string $user to obtain user id from another user
	 * @return array user data
	 */
	public function get_user_info($user=false) {
		if ($user) {
			$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$this->sql_vaccine($user).'\' OR lower(`user`) = \''.strtolower($this->sql_vaccine($user)).'\' LIMIT 0,1';
			$user = $this->sql($sql);
			if ($user) {
				$user = $user[0];
				unset($user['pass']);
				return $user;
			} else {
				return false;
			}
		} else {
			return $this->user;
		}
	}
	/**
	 * gets the desired user ID
	 * @access public
	 * @version 1.0.0
	 * @param string $user to obtain user id from another user
	 * @return int desired ID
	 */
	public function get_user_id($user=false) {
		if ($user) {
			$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$this->sql_vaccine($user).'\' OR lower(`user`) = \''.strtolower($this->sql_vaccine($user)).'\' LIMIT 0,1';
			$user = $this->sql($sql);
			if ($user) {
				$user = $user[0];
				return $user['ID'];
			} else {
				return false;
			}
		} else {
			return $this->user['ID'];
		}
	}
	/**
	 * returns if the user is guest
	 * @access public
	 * @version 1.0.0
	 * @return bool true if is guest
	 */
	public function is_guest() {
		if ($this->user['ID']==$this->config['cds_guest']) {
			return true;
		} else {
			return false;
		}
		return NULL;
	}
	/**
	 * will try to login using password access for the specified user
	 * @access public
	 * @version 1.0.51
	 * @return bool if login was successful
	 */
	public function login_password($user,$password) {
		if (strlen($password)<1) {
			return false;
		}
		$sql = 'SELECT `ID`,HEX(`pass`) as `pass` FROM `'.$this->config['cds_table'].'` WHERE lower(`user`) = \''.strtolower($this->sql_vaccine($user)).'\' LIMIT 0,1';
		$search = $this->sql($sql,$this->config['cds_db']);;
		if (count($search)>0) {
			$search = $search[0];
			if (sha1($this->crypt_undo($search['pass']))==sha1($password)) {
				$token = $this->token_create($search['ID']);
				if ($token) {
					$this->nucoke->http_cookie($this->config['cds_login_cookie'],$token,31536000,'/',$this->config['cds_cookie_domain']);
					return true;
				} else {
				return false;
				}
			} else {
				return false;
			}
		}
		return false;
	}
	/**
	 * will try to login using token (this is session dependent)
	 * @access public
	 * @version 1.0.18
	 * @return bool if token login was successful
	 */
	public function login_token($token) {
		$sql = 'SELECT * FROM `'.$this->config['cds_token_table'].'` WHERE `token` = \''.hexdec(substr($token,0,8)).'\' AND `key` = UNHEX(\''.substr($token,8).'\') AND `status` = b\'1\' LIMIT 0,1';
		$login = $this->sql($sql,$this->config['cds_db']);;
		if (count($login)>0) {
			$login = $login[0];
			$this->token = $login;
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `hits` = (`hits`+1) WHERE `user` = \''.$login['user'].'\' AND `token` = \''.$login['token'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `last` = NOW() WHERE `user` = \''.$login['user'].'\' AND `token` = \''.$login['token'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$login['user'].'\' LIMIT 0,1';
			$user = $this->sql($sql,$this->config['cds_db']);;
			if (count($user)>0) {
				$user[0]['meta'] = unserialize($user[0]['meta']);
				$this->user = $user[0];
				return true;
			}
		}
		return false;
	}
	/**
	 * will logout the session
	 * @access public
	 * @version 1.0.24
	 * @return bool if login was successful
	 */
	public function logout() {
		if ($this->is_guest()==false) {
			$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `out` = (`out`+1) WHERE `ID` = \''.$this->token['user'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `out_ts` = NOW() WHERE `ID` = \''.$this->token['user'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `status` = b\'0\' WHERE `user` = \''.$this->token['user'].'\' AND `token` = \''.$this->token['token'].'\' LIMIT 1';
			$this->nucoke->http_cookie($this->config['cds_login_cookie'],'',31536000,'/',$this->config['cds_cookie_domain']);
			$this->sql($sql,$this->config['cds_db']);
			return true;
		} else {
			return true;
		}
		return false;
	}
	/**
	 * will start a session
	 * @access private
	 * @version 1.0.80
	 * @return bool if session was started and initialized
	 */
	private function session_init() {
		$renew = false;
		if (isset($_COOKIE[$this->config['cds_cookie']])) {
			$cookie = $_COOKIE[$this->config['cds_cookie']];
			if (!$cookie) { return false; }
			$sql = 'SELECT * FROM `'.$this->config['cds_sess_table'].'` WHERE `ID` = \''.hexdec(substr($cookie,0,8)).'\' AND `key` = UNHEX(\''.substr($cookie,8).'\') AND `active` = b\'1\' LIMIT 0,1';
			$session = $this->sql($sql,$this->config['cds_db']);;
			if (count($session)>0) {
				$session = $session[0];
				if ($this->ip_decode($session['ip'])!=$this->get_ip()) { $renew = $session['ID']; }
				if ($session['agent']!=$this->get_agent()) { $renew = $session['ID']; }
				if (!$renew) {
					$session['meta'] = unserialize($session['meta']);
					$this->session = $session;
					$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `hits` = (`hits`+1) WHERE `ID` = \''.$session['ID'].'\' LIMIT 1';
					$this->sql($sql,$this->config['cds_db']);
					$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `lastts` = NOW() WHERE `ID` = \''.$session['ID'].'\' LIMIT 1';
					$this->sql($sql,$this->config['cds_db']);
					return true;
				} else {
					$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `active` = b\'0\' WHERE `ID` = \''.$session['ID'].'\' LIMIT 1';
					$this->sql($sql,$this->config['cds_db']);
				}
			}
		}
		$key = sha1(time().rand(1,4294967295));
		$sql = 'INSERT INTO `'.$this->config['cds_sess_table'].'` (`key`,`ip`,`agent`,`ts`'.($renew!=false?',`parent`':'').') VALUES (UNHEX(\''.$key.'\'),UNHEX(\''.$this->ip_encode($this->get_ip()).'\'),\''.$this->sql_vaccine($this->get_agent()).'\',NOW()'.($renew!=false?',\''.$renew.'\'':'').')';
		$session = $this->sql($sql,$this->config['cds_db']);
		if ($session) {
			$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `hits` = (`hits`+1) WHERE `ID` = \''.$session.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `lastts` = NOW() WHERE `ID` = \''.$session.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `meta` = 0x613a303a7b7d WHERE `ID` = \''.$session.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$cookie = strtoupper(dechex($session));
			while (strlen($cookie)<8) { $cookie = '0'.$cookie; }
			$cookie = $cookie.strtoupper($key);
			$this->nucoke->http_cookie($this->config['cds_cookie'],$cookie,31536000,'/',$this->config['cds_cookie_domain']);
			$sql = 'SELECT * FROM `'.$this->config['cds_sess_table'].'` WHERE `ID` = \''.$session.'\'';
			$session = $this->sql($sql,$this->config['cds_db']);
			$session = $session[0];
			$session['meta'] = unserialize($session['meta']);
			$this->session = $session;
			return true;
		} else {
			return false;
		}
	}
	/**
	 * returns value of the variable or sets it if corresponds
	 * @access public
	 * @version 1.0.48
	 * @param $variable string Variable Name
	 * @param $value string Variable value (if setting variable)
	 * @return string variable value
	 */
	public function svar($variable,$value=null) {
		$save = false;
		if (isset($this->session['meta'][$variable])) {
			if ($value===null) {
				return $this->session['meta'][$variable];
			} else {
				$this->session['meta'][$variable] = $value;
				$save = true;
			}
		} else {
			if ($value===null) {
				return null;
			} else {
				$this->session['meta'][$variable] = $value;
				$save = true;
			}
		}
		if ($save) {
			$sql = 'UPDATE `'.$this->config['cds_sess_table'].'` SET `meta` = '.$this->sql_texthex(serialize($this->session['meta'])).' WHERE `ID` = \''.$this->session['ID'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
		}
	}
	/**
	 * will create an access token for the corresponding user
	 * @access private
	 * @version 1.0.43
	 * @return string token
	 */
	private function token_create($user,$expires=604800) {
		$sql = 'SELECT * FROM `'.$this->config['cds_table'].'` WHERE `ID` = \''.$user.'\' LIMIT 0,1';
		$search = $this->sql($sql,$this->config['cds_db']);;
		if (count($search)>0) {
			$search = $search[0];
			$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `in` = (`in`+1) WHERE `ID` = \''.$search['ID'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `in_ts` = NOW() WHERE `ID` = \''.$search['ID'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'SELECT * FROM `'.$this->config['cds_token_table'].'` WHERE `user` = \''.$search['ID'].'\' ORDER BY `token` DESC LIMIT 0,1';
			$inc = $this->sql($sql,$this->config['cds_db']);;
			$i = 0;
			if (count($inc)>0) {
				$i = $inc[0]['token'];
			}
			$i++;
			$key = sha1(time().rand(1,4294967295));
			$sql = 'INSERT INTO `'.$this->config['cds_token_table'].'` (`user`,`token`,`key`,`ts`,`ip`,`remember`,`agent`) VALUES (\''.$search['ID'].'\','.$i.',UNHEX(\''.$key.'\'),NOW(),UNHEX(\''.$this->ip_encode($this->get_ip()).'\'),0,\''.$this->sql_vaccine($this->get_agent()).'\')';
			$this->sql($sql,$this->config['cds_db']);;
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `hits` = (`hits`+1) WHERE `user` = \''.$search['ID'].'\' AND `token` = \''.$i.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `last` = NOW() WHERE `user` = \''.$search['ID'].'\' AND `token` = \''.$i.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `expires` = \''.date('Y-m-d H:i:s',time()+$expires).'\' WHERE `user` = \''.$search['ID'].'\' AND `token` = \''.$i.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$sql = 'UPDATE `'.$this->config['cds_token_table'].'` SET `meta` = 0x613a303a7b7d  WHERE `user` = \''.$search['ID'].'\' AND `token` = \''.$i.'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
			$cookie = strtoupper(dechex($i));
			while (strlen($cookie)<8) { $cookie = '0'.$cookie; }
			$cookie = $cookie.strtoupper($key);
			return $cookie;
		}
		return false;
	}
	/**
	 * create a new user, the return codes are (flags):
	 * 1 params are missing
	 * 2 user is already registered
	 * 4 the user length is not correct
	 * 8 the user is not valid
	 * 16 the pass length is not correct
	 * 32 the mail length is not correct
	 * 64 unknown error inserting the registry
	 * @access public
	 * @version 1.0.20
	 * @param array $params parameters (user,pass,etc)
	 * @return flags = 0 if correct
	 */
	public function user_create($params) {
		$return = 0;
		if (!isset($params['user'])) { $return = $return | 1; return $return; }
		if (!isset($params['pass'])) { $return = $return | 1; return $return; }
		if (!isset($params['mail'])) { $return = $return | 1; return $return; }
		if ($this->get_user_id($params['user'])) { $return = $return | 2; }
		if (strlen($params['user'])<4) { $return = $return | 4; }
		$validate = $params['user'];
		$allowed = array(48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,95,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122);
		while (strlen($validate)>0) {
			if (!in_array(ord(substr($validate,0,1)),$allowed)) {
				$return = $return | 8;
			}
			$validate = substr($validate,1);
		}
		if (strlen($params['pass'])<6) { $return = $return | 16; }
		if (strlen($params['mail'])<6) { $return = $return | 32; }
		if ($return==0) {
			$sql  = 'INSERT INTO `'.$this->config['cds_table'].'` (`user`,`pass`,`name`,`access`,`mail`,`reg_ts`,`reg_ip`,`reg_agent`,`meta`) VALUES (';
			$sql .= $this->sql_texthex($params['user']);
			$sql .= ',0x'.$this->crypt_do($params['pass']);
			if (isset($params['name'])) {
				$sql .= ','.$this->sql_texthex($params['name']);
			} else {
				$sql .= ',\'\'';
			}
			$sql .= ',\'user,guest\'';
			$sql .= ','.$this->sql_texthex($params['mail']);
			$sql .= ',NOW()';
			$sql .= ',0x'.$this->ip_encode($this->get_ip());
			$sql .= ','.$this->sql_texthex($this->get_agent());
			$sql .= ',0x613a303a7b7d';
			$sql .= ')';
			$result = $this->sql($sql);
			if ($result==false) {
				$return = $return | 64;
			}
		}
		return $return;
	}
	/**
	 * returns value of the variable or sets it if corresponds
	 * @access public
	 * @version 1.0.38
	 * @param $variable string Variable Name
	 * @param $value string Variable value (if setting variable)
	 * @return string variable value
	 */
	public function uvar($variable,$value=null) {
		$save = false;
		if (isset($this->user['meta'][$variable])) {
			if ($value===null) {
				return $this->user['meta'][$variable];
			} else {
				$this->user['meta'][$variable] = $value;
				$save = true;
			}
		} else {
			if ($value===null) {
				return false;
			} else {
				$this->user['meta'][$variable] = $value;
				$save = true;
			}
		}
		if ($save) {
			$sql = 'UPDATE `'.$this->config['cds_table'].'` SET `meta` = '.$this->sql_texthex(serialize($this->user['meta'])).' WHERE `ID` = \''.$this->user['ID'].'\' LIMIT 1';
			$this->sql($sql,$this->config['cds_db']);
		}
	}
	/**
	 * will return the version of the class in Mayor, Minor and Release
	 * @access public
	 * @version 1.0.65
	 * @return array ( main, minor, release )
	 */
	public function version() {
		return array( 1, 0, 141 );
	}
}
?>