<?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 indexbase extends X
{
    protected $pagesize = 20;
    protected $page = 1;
    protected $smtpl = NULL;
    protected $metawrap = null;
    protected $gb_c = NULL;
    protected $gb_a = NULL;
    protected $appfile = "index.php";


    public function __construct()
    {

        $this->gb_c    = GB_LJCMS_C;
        $this->gb_a    = GB_LJCMS_A;
        $this->appfile = parent::$urlpath . $this->appfile;
        $path_info     = $GLOBALS['path_info'];
        if (!empty($path_info) && isset($path_info['page'])) {
            $this->page = intval($path_info['page']);
        } else {
            $this->page = intval(XRequest::getArgs('page'));
        }
        if ($this->page < 1) {
            $this->page = 1;
        }
        $this->_loadMenu();
        if (isset($_GET['ljcms'])) {
            X::poweredBy();
        }

        $search   = $_REQUEST['content'];
        $language = $this->get_lang($this->gb_c);
        $name   = $_SESSION['username'];
        $button = $this->get_lang('button');
        $lang   = $_SESSION['lang'];
        $model        = parent::model('plugin', 'am');
        $plugins      = $model->getPlugins();

        $check_plugin = array();
        foreach ($plugins as $v) {
            $check_plugin[$v['pluginname']] = 1;
        }
        $chceklang = $this->checklang();

        $check     = $_SESSION['code'];
        $data      = $this->get_headerimg($this->gb_c);
        $user_id= $_SESSION['user_id'];
        $model = parent::model('product', 'im');
        $spdata =$model->getusershopping($user_id);
        $gounum=0;
        foreach ($spdata as $v){
            $gounum += $v['productnum'];
        }
        $model1 = parent::model('language', 'am');
        list($total, $lang_list) = $model1->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>''));

        if($lang==''){
            $langname='简体中文';
        }else{
            $lang_info=$model1->getData1($lang);
            $langname=$lang_info['name'];
        }
        $model = parent::model('setting', 'am');
        $setting= $model->getOptions('site_rewrite');
        $type=$setting['urlsuffix'];
        if($type=='html'){
            if($lang=='en'){
                $questionurl='/about/4.html';
            }else{
                $questionurl='/about/3.html';
            }
            
        }else{
            if($lang=='en'){
                $questionurl='index.php?c=about&a=detail&id=4';
            }else{
                $questionurl='index.php?c=about&a=detail&id=3';
            }
           
        }
        $var_array = array(
            "c"            => $this->gb_c,
            "a"            => $this->gb_a,
            "time"         => time(),
            'year'         => date('Y'),
            'header_img'   => $data,
            'language'     => $language,
            'button'       => $button,
            'lang'         => $lang,
            'search'       => $search,
            'username'     => $name,
            'langname'     => $langname,
            'checklang'    => $chceklang,
            'check'        => $check,
            'check_plugin' => $check_plugin,
            'mpath'        =>'tpl/templets/mshop/',
            'gounum'      =>$gounum,
            'lang_list' =>$lang_list,
            'questionurl'=>$questionurl
        );
        if ($this->gb_c == 'index') {
            $var_array['c'] = 'outside';
        }
        TPL::assign($var_array);
    }
    public function existsTplFile($tplname)
    {
        $res = false;
        if (!empty($tplname)) {
            $tplfile = parent::$tplpath . $tplname . ".tpl";
            if (file_exists(BASE_ROOT . $tplfile)) {
                $res = true;
            }
        }
        return $res;
    }


    public function getTPLFile($tplname)
    {
      //  echo parent::$tplpath;die;
    //    $domain = strstr(parent::$tplpath, 'mishop');
        if(strpos(parent::$tplpath,'mishop') !== false){
            if($this->isMobile()==true || $this->isWeixin()==true){
                $tplfile = "tpl/templets/mshop/" . $tplname . ".tpl";
            }else{
                $tplfile = parent::$tplpath . $tplname . ".tpl";
            }
        }else{
            $tplfile = parent::$tplpath . $tplname . ".tpl";
        }


        if (!file_exists(BASE_ROOT . $tplfile)) {

            XHandle::halt('模板文件[' . $tplfile . ']不存在，请检查！', '', 1);
        } else {
            return $tplfile;
        }
    }

    protected function isMobile() {
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
        if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset($_SERVER['HTTP_VIA'])) {
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger');
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
                return true;
            }
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset ($_SERVER['HTTP_ACCEPT'])) {
            // 如果只支持wml并且不支持html那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
                return true;
            }
        }
        return false;
    }
    protected function isWeixin() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        } else {
            return false;
        }
    }
    protected function view($var_args, $tplname = null)
    {
        if (empty($tplname)) {
            $tplname = $this->c();
        }
        $tpl_file = $this->getTPLFile($tplname);

        TPL::assign($var_args);
        TPL::display($tpl_file);
    }

    protected function fetch($var_args, $tplname = null)
    {
        if (empty($tplname)) {
            $tplname = $this->c();
        }
        $tpl_file = $this->getTPLFile($tplname);
        TPL::assign($var_args);
        $tpl_data = TPL::fetch($tpl_file);
        return $tpl_data;
    }

    protected function c()
    {
        return $this->gb_c;
    }

    protected function url($url_items = null)
    {
        $_url = $this->appfile . "?c=" . $this->c();
        if (!empty($url_items)) {
            $_url .= "&" . $url_items;
        }
        return $_url;
    }


    public function getMeta($idmark)
    {
        $model_seo = parent::model('seo', 'im');
        $data      = $model_seo->getOneData($idmark);
        unset($model_seo);
        $this->metawrap = $data;
        return $data;
    }


    private function _loadMenu()
    {
        $model_seo = parent::model('seo', 'im');
        $model_seo->loadChLabel();
        unset($model_seo);
    }

    public function get_headerimg($c)
    {
        $model_category = parent::model('category', 'im');
        $cid            = $model_category->getCID($c);
        $data           = $model_category->gerimgurl($cid);
        return $data;
    }

    public function checklang()
    {
        $model_category = parent::model('category', 'im');
        $data           = $model_category->checklang();
        if (empty($data)) {
            return 0;
        } else {
            return 1;
        }
    }

    public function get_lang($c)
    {
        $model_category = parent::model('category', 'im');
        $data           = $model_category->getlang();
        foreach ($data as $k => $val) {
            $config[$val['name']] = $val['value'];
        }
        return $config;
        die;
//        $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();
//        $model_category = parent::model('category', 'im');
//        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的形式.
//        }
//        foreach ($ini_items as $k=>$v){
//            $arr['name']=$k;
//            $arr['value']=$v;
//            $arr['lang']=$lang;
//            $model_category->add_lang($arr);
//        }
//        die;
     //   print_r($ini_items);die;
      //  return $ini_items;
//        if ($c == 'search') {
//            $c = 'index';
//        }

    }
}

?>
