<?php
class uauth {

	var $is_login = false;
	var $user_id = 0;
    var $user_grps = array();
	var $user_obj;
    var $obj_name = 'shop_users';
    var $grp_data = array();
    var $sess_id = false;
    var $sess_data = array(); // Данные по сессии
    var $my_price = 'price';
    var $my_discount = 0;

	function __construct() {
		global $core_db;
        session_set_cookie_params(32140800,'/',_COOKIE_DOMAIN_); // Сессия на год
        //setcookie($ses, $_COOKIE[$ses], time() + $time, "/"); // prolong sesiion test
        session_start();
        $sess_id = session_id();
        $this->sess_id = $sess_id;
        $this->user_obj = new cobject( $this->obj_name );
        $this->sess_obj = new cobject( 'shop_user_sessions' );
        $db2 = $this->sess_obj->getDB()->q( $this->sess_obj->getSelect( ' AND `session_id` = "'.db_escape_string( $sess_id ).'" AND `shop_user_id` ' ) );
        if ( $db2->nr() ) {
            $this->_intAuth($db2->f('shop_user_id'));
        } else {
            $this->is_login = false;   
        }
        

	}
    
    function _intAuth( $user_id ) {
		global $core_db;
        $this->user_obj->byId($user_id);
        if ($this->user_obj->id && !$this->user_obj->get('_sys_disabled') ) {
            $this->is_login = true;
            $this->user_id = $user_id;
            if ( $this->user_obj->get('grp') ) {
                $this->user_grps = explode( ',', $this->user_obj->get('grp') );
                foreach ( $this->user_grps as $v  ) {
                    $grpobj = new cobject('shop_users_grp_'.$v);
                    $db3 = $grpobj->getDB()->q( $grpobj->getSelect( ' AND `shop_user` = "'.$this->user_id.'"' ) );
                    if ( $db3->nr() ) {
                        $this->grp_data[$v] = $db3->lastRow;
                    } else {
                        $grpobj->set('shop_user', $this->user_id);
                        $grpobj->save();
                    }
                }
            }
            
            $pcobj = new cobject('shop_price_columns');
            $pcobj->byId($this->user_obj->get('price_column'));
            if ( $pcobj->get('attr_name') ) {
                $this->my_price      = $pcobj->get('attr_name');
                $this->my_discount   = $pcobj->get('skidka_pr');
                $this->my_price_name = $pcobj->get('title');
            }
            
        } else {
            $this->do_logout();
        }
    }
    
    function addToGrp( $grp_id ) {
        $grps = explode(',', $this->user_obj->get('grp'));
        $grps = array_diff( $grps, array(''));
        $grps[] = $grp_id;
        $this->user_obj->set('grp',implode(',',$grps));
        $this->user_obj->save();
    }
	
	function do_login( $user_id ) {
        $this->do_logout();
        $this->sess_obj->set('session_id', session_id() );
        $this->sess_obj->set('shop_user_id', $user_id );
        $this->sess_obj->save();
        $this->_intAuth($user_id);
	}
	
	function do_logout() {
        $this->sess_obj->getDB()->q( 'DELETE FROM `shop_user_sessions` WHERE `session_id` = "' . db_escape_string(session_id()) . '" ' );
        $this->is_login  = false;
		$this->user_obj->byId( 0 );
        $this->user_id = 0;
	}
    
}	
?>