<?php
/**
 * Created by Sevio®.
 * All rights reserved to Sevio Solutions® SRL
 */		


class class_users
{
	
    //user_rights:0-user,1-admin,2-moderator;user_types:1-advertiser,2-publisher,3-both.
    protected $unique, $unique_refered, $user_type, $user_rights, $balance, $valid,$verified, $date;
    protected $email, $password, $first_name, $last_name, $country, $adress, $city, $zipcode, $phone;
    protected $is_company, $company_name, $company_adress, $company_tax_number, $company_country, $company_city;
    protected $notif_admin, $notif_system, $notif_information, $notif_important;

    static public function add($unique_refered, $user_type, $user_rights, $email, $password, $first_name, $last_name, $country, $adress, $city, $zipcode, $phone, $is_company, $company_name, $company_adress, $company_tax_number, $company_country, $company_city, $notif_admin, $notif_system, $notif_information, $notif_important)
    {  
        $key = rand(1, 999) . uniqid() . rand(1, 999);
        $password = md5($password);
        mysql_query("INSERT INTO `app_users` (`unique`,`unique_refered`,`user_type`,`user_rights`,`balance`,`email`,`password`,`valid`,`date`) VALUES ('" . $key . "','" . $unique_refered . "','" . $user_type . "','" . $user_rights . "','0','" . $email . "','" . $password . "','1','" . date('Y-m-d') . "')");
        mysql_query("INSERT INTO `app_users_details` (`unique`,`first_name`,`last_name`,`country`,`adress`,`city`,`zipcode`,`phone`,`is_company`,`company_name`,`company_adress`,`company_tax_number`,`company_country`,`company_city`) VALUES ('" . $key . "','" . $first_name . "','" . $last_name . "','" . $country . "','" . $adress . "','" . $city . "','" . $zipcode . "','" . $phone . "','" . $is_company . "','" . $company_name . "','" . $company_adress . "','" . $company_tax_number . "','" . $company_country . "','" . $company_city . "')");
        mysql_query("INSERT INTO `app_users_preferences` (`unique`,`notif_admin`,`notif_system`,`notif_information`,`notif_important`) VALUES ('" . $key . "','" . $notif_admin . "','" . $notif_system . "','" . $notif_information . "','" . $notif_important . "')");
        return $key;
    }
	static public function email_exist($value)
	{
		$sql = mysql_query("SELECT * FROM `app_users` WHERE `email`='" . $value . "'");
        $data = mysql_fetch_array($sql);
		if($data)
		return $data[unique];
		else
		return 0;
	}
    public function class_users($unique)
    {
        $sql = mysql_query("SELECT * FROM `app_users` WHERE `unique`='" . $unique . "'");
        $data = mysql_fetch_array($sql);
        if ($data) {
            $this->unique = $data['unique'];
            $this->unique_refered = $data['unique_refered'];
            $this->user_type = $data['user_type'];
            $this->user_rights = $data['user_rights'];
            $this->balance = $data['balance'];
            $this->email = $data['email'];
            $this->valid = $data['valid'];
            $this->verified=$data['verified'];
            $this->date = $data['date'];
            $sql = mysql_query("SELECT * FROM `app_users_details` WHERE `unique`='" . $this->unique . "'");
            $data = mysql_fetch_array($sql);
            if ($data) {
                $this->first_name = $data['first_name'];
                $this->last_name = $data['last_name'];
                $this->country = $data['country'];
                $this->adress = $data['adress'];
                $this->city = $data['city'];
                $this->zipcode = $data['zipcode'];
                $this->phone = $data['phone'];
                $this->is_company = $data['is_company'];
                    $this->company_name = $data['company_name'];
                    $this->company_tax_number = $data['company_tax_number'];
                    $this->company_adress = $data['company_adress'];
                    $this->company_country = $data['company_country'];
                    $this->company_city = $data['company_city'];
                $sql = mysql_query("SELECT * FROM `app_users_preferences` WHERE `unique`='" . $this->unique . "'");
                $data = mysql_fetch_array($sql);
                if ($data) {
                    $this->notif_admin = $data['notif_admin'];
                    $this->notif_important = $data['notif_important'];
                    $this->notif_information = $data['notif_information'];
                    $this->notif_system = $data['notif_system'];
                }
                return 1;
            } else return 0;
        } else return 0;
    }
	public function check_login($email,$password)
	{
		$sql = MYSQL_QUERY("SELECT * FROM `app_users` WHERE `email` ='".$email."' AND `password` = '".md5($password)."' ");
		$checking = mysql_fetch_array($sql); 
		if($checking)
		{	
			if($checking[valid] != 1)
			{
				return 2;
			}
			session_start();
			$_SESSION[email] = $email;
			$_SESSION[unique] = $checking[unique];
			setcookie("logged", 1, time()+3600*24*31);
			return $checking[unique];
		}
		return 0;
		
	}
	public function logout()
	{
		unset($_SESSION['email']);
		unset($_SESSION['unique']);
		$this->unique = 0 ;
		return 1;
	}
    public function get_unique()
    {
        return $this->unique;
    }
    public function get_unique_refered()
    {
        return $this->unique_refered;
    }
    public function get_user_type()
    {
        return $this->user_type;
    }
	public function get_user_type_name()
    {
        if($this->user_type == 1)
		return "Advertiser";
		if($this->user_type == 2)
		return "Publisher";
    }
    public function get_user_rights()
    {
        return $this->user_rights;
    }
    public function get_balance()
    {
        return number_format($this->balance,5);
    }
    public function get_email()
    {
        return $this->email;
    }
    public function set_email($value)
    {
        $this->email = $value;
        mysql_query("UPDATE `app_users` SET `email`='" . $this->email . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function get_valid()
    {
        return $this->valid;
    }
    public function get_verified()
    {
        return $this->verified;
    }
    public function get_date()
    {
        return $this->date;
    }
    public function get_first_name()
    {
        return $this->first_name;
    }
    public function get_last_name()
    {
        return $this->last_name;
    }
    public function get_country()
    {
        return $this->country;
    }
    public function get_adress()
    {
        return $this->adress;
    }
    public function get_city()
    {
        return $this->city;
    }
    public function get_zipcode()
    {
        return $this->zipcode;
    }
    public function get_phone()
    {
        return $this->phone;
    }
    public function get_is_company()
    {
        return $this->is_company;
    }
    public function get_company_name()
    {
        return $this->company_name;
    }
    public function get_company_adress()
    {
        return $this->company_adress;
    }
    public function get_company_tax_number()
    {
        return $this->company_tax_number;
    }
    public function get_company_country()
    {
        return $this->company_country;
    }
    public function get_company_city()
    {
        return $this->company_city;
    }
    public function get_notif_admin()
    {
        return $this->notif_admin;
    }
    public function get_notif_important()
    {
        return $this->notif_important;
    }
    public function get_notif_information()
    {
        return $this->notif_information;
    }
    public function get_notif_system()
    {
        return $this->notif_system;
    }
	public function is_admin()
	{
		if($this->user_rights == 1 || $this->user_rights == 2)
		return $this->user_rights;
		else return 0;
	}
    public function get_status()
    {
        if($this->valid==0)
            return "pending";
        elseif($this->valid==1)
            return "valid";
        elseif($this->valid==2)
            return "hold";
        elseif($this->valid==3)
            return "deny";
    }
    public function add_fonds($value)
    {
        if ($value) {
            $this->balance = $this->balance + $value;
            mysql_query("UPDATE `app_users` SET `balance`='" . $this->balance . "' WHERE `unique`='" . $this->unique . "'");
            return 1;
        } else return 0;
    }
    public function extract_money($value)
    {
        if ($this->balance >= $value) {
            $this->balance = $this->balance - $value;
            mysql_query("UPDATE `app_users` SET `balance`='" . $this->balance . "' WHERE `unique`='" . $this->unique . "'");
            return 1;
        } else
            return 0;
    }
    public function set_valid($value)
    {
        $this->valid = $value;
        mysql_query("UPDATE `app_users` SET `valid`='" . $this->valid . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_user_type($value)
    {
        $this->user_type = $value;
        mysql_query("UPDATE `app_users` SET `user_type`='" . $this->user_type . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_user_rights($value)
    {
        $this->user_rights = $value;
        mysql_query("UPDATE `app_users` SET `user_rights`='" . $this->user_rights . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_balance($value)
    {
            $this->balance = $value;
            mysql_query("UPDATE `app_users` SET `balance`='" . $this->balance . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_password($value)
    {
        $this->password = md5($value);
        mysql_query("UPDATE `app_users` SET `password`='" . $this->password . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_first_name($value)
    {
        $this->first_name = $value;
        mysql_query("UPDATE `app_users_details` SET `first_name`='" . $this->first_name . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_last_name($value)
    {
        $this->last_name = $value;
        mysql_query("UPDATE `app_users_details` SET `last_name`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_country($value)
    {
        $this->country = $value;
        $sql=mysql_query("UPDATE `app_users_details` SET `country`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_adress($value)
    {
        $this->adress = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `adress`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_city($value)
    {
        $this->city = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `city`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_zipcode($value)
    {
        $this->zipcode = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `zipcode`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_phone($value)
    {
        echo "1xxx";
        $this->phone = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `phone`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_is_company($value)
    {
        $this->is_company = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `is_company`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_company_name($value)
    {
        $this->company_name = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `company_name`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_company_tax_number($value)
    {
        $this->company_tax_number = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `company_tax_number`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_company_adress($value)
    {
        $this->company_adress = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `company_adress`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_company_country($value)
    {
        $this->company_country = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `company_country`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_company_city($value)
    {
        $this->company_city = $value;
        $sql = mysql_query("UPDATE `app_users_details` SET `company_city`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_notif_system($value)
    {
        $this->notif_system = $value;
        $sql = mysql_query("UPDATE `app_users_preferences` SET `notif_system`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_notif_information($value)
    {
        $this->notif_information = $value;
        $sql = mysql_query("UPDATE `app_users_preferences` SET `notif_information`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_notif_important($value)
    {
        $this->notif_important = $value;
        $sql = mysql_query("UPDATE `app_users_preferences` SET `notif_important`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_notif_admin($value)
    {
        $this->notif_admin = $value;
        $sql = mysql_query("UPDATE `app_users_preferences` SET `notif_admin`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
    public function set_verified($value)
    {
        $this->verified=$value;
        mysql_query("UPDATE `app_users` SET `verified`='" . $value . "' WHERE `unique`='" . $this->unique . "'");
    }
	
	public function del()
    {
        mysql_query("DELETE FROM `app_users` WHERE `unique`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_users_details` WHERE `unique`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_users_notifications` WHERE `unique`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_users_preferences` WHERE `unique`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_affiliate`  WHERE `unique_affiliate`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_affiliate_transactions`  WHERE `unique_affiliate`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_campaigns` WHERE `unique_user`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_tags` WHERE `unique_user`='" . $this->unique . "' ");
		mysql_query("DELETE FROM `app_tickets` WHERE `unique_user`='" . $this->unique . "' ");
    }
	public function user_view()
	{
			session_start();
			$_SESSION[email] = $this->email;
			$_SESSION[unique] = $this->unique;
			return $this->unique;
	}
}