<?php

class user
{

	private $id = 0;
	private $name;
	public $data;

	private $udata = array();

	private $counter = 0;

	public function __construct($name = false, $data = false)
	{
		if ($this->counter > 2) return false;
		$this->counter++;

		global $_sql;

		$query = $_sql->query("SELECT * FROM users WHERE userName = '$name'");
		if ($query->num_rows)
		{
			$this->data = $query->fetch_assoc();
			$this->id = $this->data['userId'];
			return true;
		} else
		{
			if ($data && $_sql->query("INSERT INTO users (userFiltrId, userName, userPublicName, userEmail, userRegistered, userRealName) VALUES ('$data[id]', '".$_sql->real_escape_string($name)."', '".$_sql->real_escape_string($data['name'])."', '".$_sql->real_escape_string($data['email'])."', '".time()."', '".$_sql->real_escape_string($data['name'])."')"))
				return $this->__construct($name, $data);
		}
		return false;
	}


	public function get_data()
	{
		if ($this->data)
			return $this->data;

		global $_sql;
		$query = $_sql->query("SELECT * FROM users WHERE ".($this->name ? "userName = '".$this->name."'" : "userId = ".$this->id));
		if ($query->num_rows)
			return $query->fetch_assoc();
		return false;
	}


	public function setPic($status = false)
	{
		global $_sql;
		$this->get_data();
		if ($_sql->query("UPDATE users SET userPic = ".($status ? 1 : 0)." WHERE userId = ".$this->data['userId']))
			return true;
		return false;
	}


	public function setData($field, $value)
	{
		global $_sql;
		$this->get_data();
		if ($_sql->query("UPDATE users SET `$field` = '".sqlprot($value)."' WHERE userId = ".$this->data['userId']))
			return true;
		return false;
	}

}