<?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 adminbase extends X {
    protected $cpfile; 
    protected $cptpl; 
    protected $pagesize = 30;
    protected $page = 1;
    protected $gb_c = "";
    protected $gb_a = "";

    
    public function __construct() {
        //echo 123;die;
        $this->gb_c = GB_LJCMS_C;
        $this->gb_a = GB_LJCMS_A;
    
        if ($this->gb_c != "login") {
              
            $this->checkLogin(); 
        }
        $this->checkAuth($this->gb_a);       
        $this->cpfile = __ADMIN_FILE__;
        $this->cptpl = 'tpl/'.__ADMIN_TPLDIR__.'/';
        $this->page = intval(XRequest::getArgs('page'));
        if ($this->page<1) {
            $this->page = 1;
        }
         $lang=$_REQUEST["language"];
        if($lang==''){
            $lang='cn';
        }
        session_start();
        $_SESSION["admin_lang"]=$lang;
               $model = parent::model('plugin', 'am');
       $plugins = $model->getPlugins();
       $check_plugin=array();
        foreach($plugins as $v){
           $check_plugin[$v['pluginname']]=1;  
        }
        $model1 = parent::model('language', 'am');
        list($total, $lang_list) = $model1->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>''));
        $lang = $this->get_lang($this->gb_c);
        $_var_array = array(
            "c"=>$this->gb_c,
            "a"=>$this->gb_a,
            "time"=>time(),
            "cpfile"=>$this->cpfile,
             'check_plugin'=>$check_plugin,
            'lang_list' =>$lang_list,
            'lang'=>$lang
        );
        TPL::assign($_var_array);
        if (isset($_GET['ljcms'])) {
            X::poweredBy();
        }
       // echo 123;die;
    }
    
    public function checkLogin() {
        if(false === parent::model('login', 'am')->checkLogin()){
            XHandle::halt('对不起，您还没有登录！', $this->cpfile.'?c=login', 4);
        //    XHandle::redirect($this->cpfile.'?c=login');
        }
          
    }

    
    public function checkAuth($value) {
   
        $auth = parent::model('login', 'am');
        $auth->checkAuth($value);
        unset($auth);
    }


        public function log($em, $oplog = '', $result = 1) {
        
        require_once BASE_ROOT.'source/include/elements.php';
        $emarray = array();
        $emarray = auth_get_em_array();

        
        $log_string = '';
        if (!empty($em)) {
            $log_string = $emarray[$em];
        }
        if (!empty($oplog)) {
            $log_string .= !empty($log_string) ? '-'.$oplog : $oplog;
        }

        
        if (!empty($log_string)) {
            $logid = parent::$obj->fetch_newid("SELECT MAX(logid) FROM ".DB_PREFIX."log", 1);
            $log_array = array(
                'logid'=>$logid,
                'username'=>parent::$wrap_admin['adminname'],
                'ip'=>XRequest::getip(),
                'content'=>$log_string,
                'logtype'=>1,
                'timeline'=>time(),
                'success'=>$result,
            );
            parent::$obj->insert(DB_PREFIX.'log', $log_array);
            unset($log_array);
        }
    }

    
    public function saveLoginLog($username, $content, $success=0) {
        $logid = parent::$obj->fetch_newid("SELECT MAX(logid) FROM ".DB_PREFIX."log", 1);
        $log_array = array(
            'logid'=>$logid,
            'username'=>$username,
            'ip'=>XRequest::getip(),
            'content'=>$content,
            'logtype'=>1,
            'timeline'=>time(),
            'success'=>$success,
        );
        parent::$obj->insert(DB_PREFIX.'log', $log_array);
        unset($username, $content, $success, $logid, $log_array);
    }

    protected function c() { 
        return $this->gb_c;
    }
    protected function url($url_items=null) { 
        $_url = $this->cpfile."?c=".$this->c();
        if (!empty($url_items)) {
            $_url .= "&".$url_items;
        }
        return $_url;
    }
    public function get_lang($c)
    {
        $lang= parent::$lang;
        $file_name='data/attachment/language/language_'.$lang.'.ini';
        $str=file_get_contents($file_name);//读取ini文件存到一个字符串中.
        $ini_list = explode("\r\n",$str);//按换行拆开,放到数组中.
        $ini_items = array();
        foreach($ini_list as $item){
            $one_item = explode("=",$item);
            if(isset($one_item[0])&&isset($one_item[1])) $ini_items[trim($one_item[0])] = trim($one_item[1]); //存成key=>value的形式.
        }
        //    print_r($ini_items);die;
        return $ini_items;
        die;
    }
}
?>
