<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class loginSModel extends X {

    public function doLogin($username, $password){
        $status = 0;
        $md5password = md5($password);
        $query  = "SELECT `adminid`, `adminname`, `password`, `super`, `flag` FROM ".DB_PREFIXSA."admin".
                    " WHERE `adminname`='{$username}' AND `password`='{$md5password}'";
                    // var_dump($query);die;
        $data   = parent::$obj->fetch_first($query);
        if (!empty($data)) {
            if (intval($data['flag']) == 0){ 
                $status = 1;
            }
            else { 
                $last_time = time();
                $array  = array(
                    'logintimeline'=>$last_time,
                    'logintimes'=>'[[logintimes+1]]',
                    'loginip'=>XRequest::getip(),
                );
                parent::$obj->update(DB_PREFIXSA.'admin', $array, 'adminid='.intval($data['adminid']).'');
                $status = 3;
                $_SESSION['sale_name']=$username;
                $_SESSION['sale_id']=$data['adminid'];
                $_SESSION['super']=$data['super'];
                
                $sid = '';
                $sid .= $data['adminid'].'\t';
                $sid .= $data['adminname'].'\t';
                $sid .= $data['password'].'\t';
                $sid .= $last_time;
                $code = parent::import('code', 'util');
                $sid = $code->authCode($sid, 'ENCODE', LJCMS_RANDKEY);
                unset($code);                    
                parent::loadUtil('cookie');
                XCookie::set('app_admininfo', $sid);
                                
              //  $this->_output();
                unset($sid);
                
            }
        }
        
        else {
            $status = 2;
        }
        return $status;
    }

    public function logout(){
        parent::loadUtil('cookie');
        XCookie::del('app_admininfo');
        parent::$wrap_admin['super'] = NULL;
        parent::$wrap_admin['adminid'] = NULL;
        parent::$wrap_admin['adminname'] = NULL;
        parent::$wrap_admin['groupname'] = NULL;
        parent::$wrap_admin['auths'] = NULL;
        parent::$wrap_admin['lasttime'] = NULL;
    }


    public function checkLogin(){
        $status = false;
        parent::loadUtil('cookie');
        $sid = XCookie::get('app_admininfo');
        $code = parent::import('code', 'util');
        $sid = $code->authCode($sid, 'DECODE', LJCMS_RANDKEY);
       
        unset($code);
        if (empty($sid)) {
            $status = false;
        }
        else {
            $adminInfo = explode('\t', $sid);
            $adminData = $this->_getUser(intval($adminInfo[0]));
            if (empty($adminData)) {
                $status = false;
            }
            else {
                if ($adminData['adminname'] != $adminInfo[1] || $adminData['password'] != $adminInfo[2]){
                    $status = false;
                }
                else {
                    parent::$wrap_admin = array(
                        'super'=>intval($adminData['super']),
                        'adminid'=>intval($adminData['adminid']),
                        'adminname'=>$adminData['adminname'],
                        'lasttime'=>$adminData['logintimeline'],
                        'groupname'=>$adminData['groupname'],
                        'auths'=>$adminData['auths'],
                    );
                    $status = true;
                }
            }
            
        }
        return $status;
    }

    private function _getUser($uid) {
        $sql = "SELECT v.*,g.groupname,g.auths".
                " FROM ".DB_PREFIXSA."admin AS v".
                " LEFT JOIN ".DB_PREFIX."authgroup AS g ON v.groupid=g.groupid".
                " WHERE v.adminid='".intval($uid)."'";
        return parent::$obj->fetch_first($sql);     
    }





        function check_login($map){ 
        	$emp_no= $map['emp_no'];
        	$is_del =$map['is_del'];
        	$password=$map['password'];
        	$sql = "SELECT * FROM ".DB_PREFIXOA."user WHERE emp_no='".$emp_no."' OR name='".$emp_no."' AND is_del='".$is_del."' AND password='".$password."'";
        	return parent::$obj->fetch_first($sql);
        }
        public function save($id,$arr){
           return parent::$obj->update(DB_PREFIXOA.'user', $arr, 'id='.$id.'');
        }
        function get_system_config($code) {
            $sql = "SELECT * FROM ".DB_PREFIXOA."System_Config WHERE code='".$code."'";
            return parent::$obj->fetch_first($sql);
        }
}
?>
