<?php
# MetInfo Enterprise Content Management System
# Copyright (C) MetInfo Co.,Ltd (http://www.metinfo.cn). All rights reserved.

defined('IN_MET') or exit('No permission');

#load::sys_class('admin.class.php');
load::mod_class('base/admin/base_admin');
load::mod_class('ui_set/class/config_ui.class.php');
load::mod_class('ui_set/class/config_tem.class.php');
load::mod_class('ui_set/class/config_metui.class.php');
load::sys_func('file');

class index extends base_admin
{
    public $config;
    public $sys_ui_config;
    public $no;
    public $type;
    public $met_ui_config;

    function __construct()
    {
        global $_M;
        parent::__construct();
        $power = background_privilege();
        if (!($power['navigation'] == 'metinfo' || strstr($power['navigation'], '1802'))) {
            Header("Location: " . $_M['url']['site_admin']);
        }
        $this->no = $_M['form']['met_skin_user'] ? $_M['form']['met_skin_user'] : $_M['config']['met_skin_user'];

        $inc_file = PATH_WEB . "templates/{$this->no}/metinfo.inc.php";
        if (file_exists($inc_file)) {
            require $inc_file;
        }
        if ($template_type == 'ui') {
            $this->type = 'ui';
            $this->config = new config_ui($this->no, $_M['lang']);
        } else {
            $this->type = $template_type;
            $this->config = new config_tem($this->no, $_M['lang']);
        }
        $this->met_ui_config = new config_metui($_M['lang']);
    }

    function doindex()
    {
        global $_M;
        $this->checktem($this->no);

        //后台安全提示框
        $admin_folder_safe = $this->admin_folder_safe();
        // 应用列表
        $applist = array();
        $power = admin_information();
        $app = load::mod_class('myapp/myapp', 'new')->listApp();
        foreach ($app as $key => $value) {
            if ($value['install'] && $value['display'] == 2) {
                if (!strstr($power['admin_type'], 'a' . $value['no']) && !strstr($power['admin_type'], 'metinfo')) {
                    continue;
                }
                $value['url'] = $value['version'] ? str_replace($_M['url']['site_admin'] . '#/', '', $value['url']) : $_M['url']['adminurl'] . "n={$value['m_name']}&c={$value['m_class']}&a={$value['m_action']}";
                $applist[] = $value;
            }
        }
        // iframe地址
        $pageset_iframe_src = $_COOKIE['page_iframe_url'] ? $_COOKIE['page_iframe_url'] : $_M['url']['site'] . 'index.php?lang=' . $_M['lang'];
        preg_match('/lang=(\w+)/', $pageset_iframe_src, $lang);
        if ($_M['$lang'] != $lang[1]) {
            $pageset_iframe_src = str_replace($lang[0], 'lang=' . $_M['lang'], $pageset_iframe_src);
        }
        if (strpos($pageset_iframe_src, 'pageset=1') === false) {
            if (strpos($pageset_iframe_src, '?') !== false) {
                $pageset_iframe_src .= '&pageset=1';
            } else {
                $pageset_iframe_src .= '?pageset=1';
            }
        }
        if (!$_COOKIE['page_iframe_url'] || $_M['$lang'] != $lang[1]) {
            setcookie("page_iframe_url", $pageset_iframe_src, 0, '/');
        }

        $auth_data = $this->get_auth($power);
        $data = array(
            'admin_folder_safe' => $admin_folder_safe,
            'applist' => $applist,
            'pageset_iframe_src' => $pageset_iframe_src,
            'power' => $power,
            'auth' => $auth_data
        );

        $data['license'] = 1;
        $data['license_url'] = 'https://u.mituo.cn/api/metinfo/license';
        if (!file_exists(PATH_WEB . 'upload/file/license.html')) {
            $data['license'] = 0;
        }
        $sys_json = parent::sys_json();
        $data = array_merge($data, $sys_json);

        $this->view('app/index', $data);
    }

    //获取可视化栏目权限
    function get_auth($power = array())
    {
        global $_M;
        $power = $power?$power:admin_information();
        $data = array();
        //判断是否有环境检测的权限
        if (strstr($power['admin_type'], 's1903') || strstr($power['admin_type'], 'metinfo')) {
            $data['environmental_test'] = 1;
        }
        //判断是否有功能大全的权限
        if (strstr($power['admin_type'], 's1902') || strstr($power['admin_type'], 'metinfo')) {
            $data['function_complete'] = 1;
        }
        //判断是否有清空缓存的权限
        if (strstr($power['admin_type'], 's1901') || strstr($power['admin_type'], 'metinfo')) {
            $data['clear_cache'] = 1;
        }
        //判断是否有检测更新的权限
        if (strstr($power['admin_type'], 's1104') || strstr($power['admin_type'], 'metinfo')) {
            $data['checkupdate'] = 1;
            if (!$_M['config']['met_agent_update'] && $_M['config']['met_agents_type']) {
                $data['checkupdate'] = 0;
            }
        }

        //判断是否有基本信息设置权限
        if (strstr($power['admin_type'], 's1007') || strstr($power['admin_type'], 'metinfo')) {
            $data['basic_info'] = 1;
        }

        //判断是否有栏目设置权限
        if (strstr($power['admin_type'], 's1201') || strstr($power['admin_type'], 'metinfo')) {
            $data['column'] = 1;
        }

        //判断是否有内容设置权限
        if (strstr($power['admin_type'], 's1301') || strstr($power['admin_type'], 'metinfo')) {
            $data['content'] = 1;
        }

        //判断是否有网站模板权限
        if (strstr($power['admin_type'], 's1405') || strstr($power['admin_type'], 'metinfo')) {
            $data['site_template'] = 1;
            $query = "SELECT * FROM {$_M['table']['admin_column']} WHERE name = 'lang_appearance'";
            $info=DB::get_one($query);
            if (!$info['type'] && $_M['config']['met_agents_type']) {
                $data['site_template'] = 0;
            }
        }
        //判断是否有水印缩略图权限
        if (strstr($power['admin_type'], 's1003') || strstr($power['admin_type'], 'metinfo')) {
            $data['watermark_thumbnail'] = 1;
        }
        //判断是否有水印缩略图权限
        if (strstr($power['admin_type'], 's1604') || strstr($power['admin_type'], 'metinfo')) {
            $data['banner'] = 1;
        }
        //判断是否有手机菜单权限
        if (strstr($power['admin_type'], 's1605') || strstr($power['admin_type'], 'metinfo')) {
            $data['mobile_menu'] = 1;
        }
        //判断是否有seo权限
        if (strstr($power['admin_type'], 's1404') || strstr($power['admin_type'], 'metinfo')) {
            $data['seo'] = 1;
        }
        //判断是否有友情链接权限
        if (strstr($power['admin_type'], 's1406') || strstr($power['admin_type'], 'metinfo')) {
            $data['link'] = 1;
        }
        //判断是否有语言权限
        if (strstr($power['admin_type'], 's1002') || strstr($power['admin_type'], 'metinfo')) {
            $data['language'] = 1;
        }
        //判断是否有应用插件权限
        if (strstr($power['admin_type'], 's1505') || strstr($power['admin_type'], 'metinfo')) {
            $data['myapp'] = 1;
            $query = "SELECT * FROM {$_M['table']['admin_column']} WHERE name = 'lang_myapp'";
            $info=DB::get_one($query);
            if ($_M['config']['met_agents_type'] && !$info['type']) {
                $data['myapp'] = 0;
            }
        }
        if (strstr($power['admin_type'], 'a10070') || strstr($power['admin_type'], 'metinfo')) {
            $data['met_agents_sms'] = 1;
            if ($_M['config']['met_agents_type'] && !$_M['config']['met_agents_sms']) {
                $data['met_agents_sms'] = 0;
            }
        }
        //判断是否有备份恢复权限
        if (strstr($power['admin_type'], 's1005') || strstr($power['admin_type'], 'metinfo')) {
            $data['databack'] = 1;
        }
        //判断是否有客服管理权限
        if (strstr($power['admin_type'], 's1106') || strstr($power['admin_type'], 'metinfo')) {
            $data['online'] = 1;
        }
        //判断是否有用户管理权限
        if (strstr($power['admin_type'], 's1506') || strstr($power['admin_type'], 'metinfo')) {
            $data['user'] = 1;
        }
         //判断是否有会员管理权限
        if (strstr($power['admin_type'], 's1601') || strstr($power['admin_type'], 'metinfo')) {
            $data['web_user'] = 1;
        }
        //判断是否有安全与效率权限
        if (strstr($power['admin_type'], 's1004') || strstr($power['admin_type'], 'metinfo')) {
            $data['safe'] = 1;
        }
        //判断是否有管理员设置权限
        if (strstr($power['admin_type'], 's1603') || strstr($power['admin_type'], 'metinfo')) {
            $data['admin_user'] = 1;
        }
        //判断是否有企业超市权限
        if (strstr($power['admin_type'], 's1508') || strstr($power['admin_type'], 'metinfo')) {
            $data['partner'] = 1;
            $query = "SELECT * FROM {$_M['table']['admin_column']} WHERE name = 'cooperation_platform'";
            $info=DB::get_one($query);
            if ($_M['config']['met_agents_type'] && !$info['type']) {
                $data['partner'] = 0;
            }
        }
        //判断是否有风格设置权限
        if (strstr($power['admin_type'], 's1905') || strstr($power['admin_type'], 'metinfo')) {
            $data['style_settings'] = 1;
        }
        //判断是否有导航菜单设置权限
        if (strstr($power['admin_type'], 's1904') || strstr($power['admin_type'], 'metinfo')) {
            $data['nav_setting'] = 1;
        }
        return $data;
    }

    /*检测授权*/
    function checktem($file = '')
    {
        global $_M;
        $str = file_get_contents(PATH_WEB . 'templates/' . $file . '/index.php');
        preg_match('/authtemp\(\'([^;]+)\'\);/', $str, $out);
        if (!$out[1]) {
            $str = file_get_contents(PATH_WEB . 'templates/' . $file . '/config.php');
            preg_match('/authtemp\(\'([^;]+)\'\);/', $str, $out);
        }
        if ($out[1]) {
            $auth_domian = "met_muban_auth_" . $file;
            $muban_auth = explode(',', $_M['config'][$auth_domian]);
            $do_auth = 1;
            foreach ($muban_auth as $val) {
                if (stristr($_M['url']['site'], $val)) {
                    $do_auth = 0;
                }
            }
            if ($do_auth) {
                $curl = load::sys_class('curl', 'new');
                $curl->set('file', 'index.php?n=platform&c=temcheck&a=doagain_auth');
                $post = array('type' => 'tem', 'no' => $file, 'cmsver' => $_M['config']['metcms_v'], 'authtemp' => $out[1]);
                $data = $curl->curl_post($post);
                list($suc, $replace, $code, $foot, $domian) = explode('|', $data);
                $replace = PATH_WEB . 'templates/' . $file . '/' . $replace;
                if ($suc == 'suc') {
                    $str = file_get_contents($replace);
                    $str = preg_replace('/authtemp\(\'([^;]+)\'\);/', $code, $str);
                    file_put_contents($replace, $str);
                    $query = "SELECT * FROM {$_M['table']['config']} WHERE name = '{$auth_domian}' and lang='metinfo'";
                    if (DB::get_one($query)) {
                        $query = "update {$_M['table']['config']} SET value='{$domian}' WHERE name = '{$auth_domian}' and lang='metinfo'";
                        DB::query($query);
                    } else {
                        $query = "INSERT INTO {$_M['table']['config']} SET name='{$auth_domian}',value='{$domian}',lang='metinfo'";
                        DB::query($query);
                    }
                }
            }
        }
    }

    public function dochange_skin()
    {
        global $_M;
        $skin_name = $_M['form']['met_skin_user'];
        $change = $this->config->change_skin($skin_name);
        $this->ajaxReturn(array('status' => $change));
    }

    /**
     * 切换集成UI
     */
    public function dochangeUi()
    {
        global $_M;
        $redata = array();
        $mid = $_M['form']['mid'];
        $ui_name = $_M['form']['ui_name'];
        $ui_info = $this->config->changeUi($mid, $ui_name);
        if ($ui_info == true) {
            $redata['status'] = 1;
            $redata['msg'] = $_M['word']['jsok'];
            $this->ajaxReturn($redata);
        }
    }

    /**
     * 获取区块参数、对应系统设置
     * @DateTime 2017-11-06
     */
    public function doset_area()
    {
        global $_M;
        $mid = $_M['form']['mid'];
        $urls = array(
            'member' => array(
                'url' => 'user',
                'head_tab_active' => 3
            ),
            'lang' => array(
                'url' => 'language',
                'head_tab_active' => 2
            ),
            'online' => array(
                'url' => 'online',
                'head_tab_active' => 1
            ),
            'message_form' => array(
                'url' => 'manage',
                'module' => 'message',
                'class1' => $_M['form']['classnow'],
                'head_tab_active' => 2
            ),
            'feedback' => array(
                'url' => 'manage',
                'module' => 'feedback',
                'class1' => $_M['form']['classnow'],
                'head_tab_active' => 2
            ),
            'search_global' => array(
                'title' => $_M['word']['mod11'],
                'url' => 'search/global/?c=index&a=doGetGlobalSearch'
            ),
            'search_column' => array(
                'title' => $_M['word']['column_search'],
                'url' => 'search/column/?c=index&a=doGetColumnSearch'
            ),
            'search_advanced' => array(
                'title' => $_M['word']['advanced_search'],
                'url' => 'search/advanced/?c=index&a=doGetAdvancedSearch'
            ),
        );
        $redata = array('status' => 1);
        if ($urls[$_M['form']['type']]) {
            $redata['data'] = $urls[$_M['form']['type']];
            $this->ajaxReturn($redata);
        }
        if ($urls[$_M['form']['mid']]) {
            $redata['data'] = $urls[$_M['form']['mid']];
            $this->ajaxReturn($redata);
        } else if (isset($mid)) {
            $ui_data = $this->config->list_data($mid);
            $redata['data'] = $ui_data;
            $redata['data']['template_type'] = $this->type;
            $this->ajaxReturn($redata);
        }
    }

    /**
     * 区块参数保存
     * @return [type] [description]
     */
    function doeditor()
    {
        global $_M;
        $this->config->save_config($_M['form']);
        self::_clear_cahce();
        $redata = array(
            'status' => 1,
            'msg' => $_M['word']['jsok']
        );
        $this->ajaxReturn($redata);
    }

    /**
     * 获取区块对应系统内容设置
     * @DateTime 2017-11-06
     * @return  string $url
     */
    public function doset_content()
    {
        global $_M;
        $mid = $_M['form']['mid'];
        $classnow = $_M['form']['classnow'];
        if ($_M['form']['module'] == 1) {
            $_M['form']['id'] = $classnow;
            $_M['form']['table'] = load::sys_class('handle', 'new')->mod_to_file($_M['form']['module']);
            // unset($classnow);
        } else {
            $_M['form']['table'] = load::sys_class('handle', 'new')->mod_to_file($_M['form']['module']);
        }
        $urls = array(
            'banner' => 'banner',
            'head_nav' => 'column',
            'head_seo' => 'seo',
            'foot_nav' => 'column',
            'foot' => 'webset',
            'member' => 'user',
            'lang' => 'language',
            'online' => 'online',
            'menu' => 'menu',
            'link' => array(
                'url' => 'seo',
                'head_tab_active' => 5
            ),
            'message_list' => array(
                'url' => 'manage',
                'module' => 'message',
                'class1' => $classnow,
                'head_tab_active' => 0
            ),
            'message_form' => array(
                'url' => 'manage',
                'module' => 'message',
                'class1' => $classnow,
                'head_tab_active' => 1
            ),
            'feedback' => array(
                'url' => 'manage',
                'module' => 'feedback',
                'class1' => $classnow,
                'head_tab_active' => 1
            )
        );

        $redata = array('status' => 1);
        if ($urls[$_M['form']['type']]) {
            $redata['data'] = $urls[$_M['form']['type']];
            $this->ajaxReturn($redata);
        }
        if ($urls[$_M['form']['mid']]) {
            $redata['data'] = $urls[$_M['form']['mid']];
            $this->ajaxReturn($redata);
        }
        if ($_M['form']['id']) {
            $id = $_M['form']['id'];
            $module = $_M['form']['table'] == 'column' ? 'about' : $_M['form']['table'];
            $url = "{$module}/edit/?module={$urls['module']}&c={$module}_admin&a=doeditor&id={$id}";
        } else {
            $res = $this->config->get_config_column($mid);
            if (!$res) {
                $cid = $classnow;
            } elseif (is_numeric($res)) {
                $redata['data'] = 'manage';
                $this->ajaxReturn($redata);
            } else {
                if (is_numeric($mid)) {
                    $cid = $res['uip_value'] ? $res['uip_value'] : $res['uip_default'];
                } else {
                    $cid = $res['value'] ? $res['value'] : $res['defaultvalue'];
                }
            }
            // $mod = load::sys_class('label', 'new')->get('column')->get_column_id($cid);
            $class = load::sys_class('label', 'new')->get('column')->get_class123_reclass($cid);
            $module = $class['class3']['module'] ? $class['class3']['module'] : ($class['class2']['module'] ? $class['class2']['module'] : $class['class1']['module']);
            $mod_name = load::sys_class('handle', 'new')->mod_to_file($module);
            $url = array(
                'url' => 'manage',
                'module' => $mod_name,
                'class1' => $class['class1']['id'],
                'class2' => $class['class2']['id'],
                'class3' => $class['class3']['id']
            );
        }
        if ($_M['form']['type'] == 'displayimgs') {
            $url .= '&displayimgs=1';
        }
        $redata['data'] = $url;
        $this->ajaxReturn($redata);
    }

    /**
     * 前台时间格式
     * @return [type] [description]
     */
    public function get_time()
    {
        global $_M;
        //列表页设置
        $m_now_time = time();
        $met_timetype = array(
            array(value => 'Y-m-d H:i:s', name => date('Y-m-d H:i:s', $m_now_time)),
            array(value => 'Y-m-d', name => date('Y-m-d', $m_now_time)),
            array(value => 'Y/m/d', name => date('Y/m/d', $m_now_time)),
            array(value => 'Ymd', name => date('Ymd', $m_now_time)),
            array(value => 'Y-m', name => date('Y-m', $m_now_time)),
            array(value => 'Y/m', name => date('Y/m', $m_now_time)),
            array(value => 'Ym', name => date('Ym', $m_now_time)),
            array(value => 'm-d', name => date('m-d', $m_now_time)),
            array(value => 'm/d', name => date('m/d', $m_now_time)),
            array(value => 'md', name => date('md', $m_now_time))
        );
        return $met_timetype;
    }

    /**
     * 前端通过表、字段、id来获取文本内容
     * @DateTime 2017-11-09
     * @return   json
     */
    public function doget_text_content()
    {
        global $_M;
        $table = $_M['form']['table'];
        $field = $_M['form']['field'];
        $id = $_M['form']['id'];

        $sys_compile = load::sys_class('view/sys_compile', 'new');
        $content = $sys_compile->get_field_text($table, $field, $id);

        $this->ajaxReturn($content);
    }


    /**
     * 前端更新指定数据内容
     * @DateTime 2017-11-09
     * @return json 更新结果状态
     */
    public function doset_text_content()
    {
        global $_M;
        $table = $_M['form']['table'];
        $field = $_M['form']['field'];
        $id = $_M['form']['id'];
        $text = $_M['form']['text'];
        $sys_compile = load::sys_class('view/sys_compile', 'new');
        $content = $sys_compile->set_field_text($table, $field, $id, $text);

        self::_clear_cahce();
        $this->ajaxReturn($content);
    }

    /**
     * 图片修改保存
     * @return [type] [description]
     */
    public function dosave_img()
    {
        global $_M;
        $mid = $_M['form']['id'];
        $table = $_M['form']['table'];
        $field = $_M['form']['field'];
        $new_img = $_M['form']['new_img'];

        if (strpos($new_img, PATH_WEB) === false) {
            $sys_compile = load::sys_class('view/sys_compile', 'new');
            $update = $sys_compile->save_img_field($table, $field, $mid, $new_img);

            if ($_M['config']['met_big_wate'] && ($table == 'product' || $table == 'news' || $table == 'img')) {
                $new_img = str_replace($_M['url']['site'], '', $new_img);
                $mark = load::sys_class('watermark', 'new');
                $mark->set_system_bigimg();
                $mark_res = $mark->create($new_img);

                if (!$mark_res['error']) {
                    if (!$mark_res['path']) {
                        $mark_res['path'] = $new_img;
                    }
                    $sys_compile->save_img_field($table, $field, $mid, $mark_res['path']);
                }
            }
            $this->ajaxReturn(array('status' => intval($update)));
        }

        buffer::clearColumn();

        $this->ajaxReturn(array('status' => 1));
    }

    /**
     * 获取模板公共参数
     * @return [type] [description]
     */
    public function doget_public_config()
    {
        global $_M;
        $redata = array();
        $data['config_list'] = $this->config->parse_config($this->config->get_public_config());
        $time_list = $this->get_time();
        $data['other_config_list'] = array(
            array(
                'type' => 'select',
                'name' => 'met_listtime',
                'value' => $_M['config']['met_listtime'],
                'data' => $time_list,
                'label' => $_M['word']['setskinListPage'] . $_M['word']['setskindatecontent']
            ),
            array(
                'type' => 'select',
                'name' => 'met_contenttime',
                'value' => $_M['config']['met_contenttime'],
                'data' => $time_list,
                'label' => $_M['word']['content'] . $_M['word']['setskindatecontent']
            ),
            array(
                'type' => 'radio',
                'name' => 'met_pnorder',
                'value' => $_M['config']['met_pnorder'] ? $_M['config']['met_pnorder'] : 0,
                'data' => array(
                    array(
                        'name' => $_M['word']['settopcolumns'],
                        'value' => 0
                    ),
                    array(
                        'name' => $_M['word']['setequivalentcolumns'],
                        'value' => 1,
                    )
                ),
                'label' => $_M['word']['page_range']
            )
        );
        $data['template_type'] = $this->type;

        $redata['status'] = 1;
        $redata['data'] = $data;
        $this->ajaxReturn($redata);
    }

    /**
     * 保存模板公共参数
     * @return [type] [description]
     */
    public function doset_public_config()
    {
        global $_M;
        $configs = array(
            'met_listtime' => $_M['form']['met_listtime'],
            'met_contenttime' => $_M['form']['met_contenttime'],
            'met_pnorder' => $_M['form']['met_pnorder']
        );
        $this->_set_page_config($configs);
        $update = $this->config->set_public_config($_M['form']);

        self::_clear_cahce();
        $this->ajaxReturn($update);
    }

    /**
     * 栏目样式配置页面配置
     * @return array
     */
    public function doGetClassInfo()
    {
        global $_M;
        $redata = array();
        $classnow = $_M['form']['classnow'];
        $class_set = self::getPageInfo($classnow);

        $redata = $class_set;

        if(is_mobile()){
            $this->ajaxReturn(array('status' => 1, 'data'=>$redata));
        }
        return $redata;
    }

    /**
     * 详情页配置
     */
    public function getPageInfo($classnow = '')
    {
        global $_M;
        $redata = array();
        $class_label = load::mod_class('column/column_label', 'new');

        $c = $class_label->get_column_id($classnow);
        $redata['thumb_list'] = $c['thumb_list'];
        $redata['thumb_detail'] = $c['thumb_detail'];
        $redata['list_length'] = $c['list_length'] ? $c['list_length'] : '';
        $redata['tab_num'] = $c['tab_num'] ? $c['tab_num'] : 0;
        $redata['tab_name'] = $c['tab_name'];

        $data = self::classExt($c);
        $redata['thumb_list_default'] = $data['thumb_list_default'];
        $redata['thumb_detail_default'] = $data['thumb_detail_default'];
        $redata['list_length_default'] = $data['list_length_default'];
        $redata['tab_num_default'] = $data['tab_num_default'];
        $redata['tab_name_default'] = $data['tab_name_default'];
        return $redata;
    }

    /**
     * 额外栏目信息
     * @param array $c
     */
    protected function classExt($c = array())
    {
        global $_M;
        $column_lable = load::sys_class('label', 'new')->get('column');
        $c123 = $column_lable->get_class123_no_reclass($c['id']);

        $thumb_list_default = array(800, 500);
        $thumb_detail_default = array(800, 500);

        //新闻
        if ($c['module'] == 2) {
            $thumb_list_default = array($_M['config']['met_newsimg_x'], $_M['config']['met_newsimg_y']);
        }

        //产品
        if ($c['module'] == 3) {
            $thumb_list_default = array($_M['config']['met_productimg_x'], $_M['config']['met_productimg_y']);
            $thumb_detail_default = array($_M['config']['met_productdetail_x'], $_M['config']['met_productdetail_y']);
        }

        //图片
        if ($c['module'] == 5) {
            $thumb_list_default = array($_M['config']['met_imgs_x'], $_M['config']['met_imgs_y']);
            $thumb_detail_default = array($_M['config']['met_imgdetail_x'], $_M['config']['met_imgdetail_y']);
        }

        //栏目配置分页条数及说略图尺寸信息
        $c_lev = $c['classtype'];

        //三级栏目
        if ($c_lev == 3) {
            //list_length
            $list_length = $c123['class3']['list_length'] ? $c123['class3']['list_length'] : ($c123['class2']['list_length'] ? $c123['class2']['list_length'] : ($c123['class1']['list_length'] ? $c123['class1']['list_length'] : 8));
            $list_length_default = $c123['class2']['list_length'] ? $c123['class2']['list_length'] : ($c123['class1']['list_length'] ? $c123['class1']['list_length'] : 8);

            //thumb_list
            if ($c123['class3']['thumb_list'] && $c123['class3']['thumb_list'] != '|') {
                $thumb_list = explode("|", $c123['class3']['thumb_list']);
            } else {
                if ($c123['class2']['thumb_list'] && $c123['class2']['thumb_list'] != '|') {
                    $thumb_list = explode("|", $c123['class2']['thumb_list']);
                } else {
                    if ($c123['class1']['thumb_list'] && $c123['class1']['thumb_list'] != '|') {
                        $thumb_list = explode("|", $c123['class1']['thumb_list']);
                    } else {
                        $thumb_list = $thumb_list_default;
                    }
                }
            }

            //thumb_detail
            if ($c123['class3']['thumb_detail'] && $c123['class3']['thumb_detail'] != '|') {
                $thumb_detail = explode("|", $c123['class3']['thumb_detail']);
            } else {
                if ($c123['class2']['thumb_detail'] && $c123['class2']['thumb_detail'] != '|') {
                    $thumb_detail = explode("|", $c123['class2']['thumb_detail']);
                } else {
                    if ($c123['class1']['thumb_detail'] && $c123['class1']['thumb_detail'] != '|') {
                        $thumb_detail = explode("|", $c123['class1']['thumb_detail']);
                    } else {
                        #$thumb_detail = array(600, 600);
                        $thumb_detail = $thumb_detail_default;
                    }
                }
            }

            //tab_num
            $tab_num = $c123['class3']['tab_num'] ? $c123['class3']['tab_num'] : ($c123['class2']['tab_num'] ? $c123['class2']['tab_num'] : ($c123['class1']['tab_num'] ? $c123['class1']['tab_num'] : 3));
            $tab_num_default = $c123['class2']['tab_num'] ? $c123['class2']['tab_num'] : ($c123['class1']['tab_num'] ? $c123['class1']['tab_num'] : 3);

            //tab_name
            if ($c123['class3']['tab_name'] && trim($c123['class3']['tab_name'], "|")) {
                $tab_name = explode("|", $c123['class3']['tab_name']);
            } else {
                if ($c123['class2']['tab_name'] && trim($c123['class2']['tab_name'], "|")) {
                    $tab_name = explode("|", $c123['class2']['tab_name']);
                } else {
                    if ($c123['class1']['tab_name'] && trim($c123['class1']['tab_name'], "|")) {
                        $tab_name = explode("|", $c123['class1']['tab_name']);
                    } else {
                        $tab_name = array(
                            $_M['config']['met_productTabname'],
                            $_M['config']['met_productTabname_1'],
                            $_M['config']['met_productTabname_2'],
                            $_M['config']['met_productTabname_3'],
                            $_M['config']['met_productTabname_4']
                        );
                    }
                }
            }
        }

        //二级栏目将
        if ($c_lev == 2) {
            //list_length
            $list_length = $c123['class2']['list_length'] ? $c123['class2']['list_length'] : ($c123['class1']['list_length'] ? $c123['class1']['list_length'] : 8);
            $list_length_default = $c123['class1']['list_length'] ? $c123['class1']['list_length'] : 8;

            //thumb_list
            if ($c123['class2']['thumb_list'] && $c123['class2']['thumb_list'] != '|') {
                $thumb_list = explode("|", $c123['class2']['thumb_list']);
            } else {
                if ($c123['class1']['thumb_list'] && $c123['class1']['thumb_list'] != '|') {
                    $thumb_list = explode("|", $c123['class1']['thumb_list']);
                } else {
                    $thumb_list = $thumb_list_default;
                }
            }

            //thumb_detail
            if ($c123['class2']['thumb_detail'] && $c123['class2']['thumb_detail'] != '|') {
                $thumb_detail = explode("|", $c123['class2']['thumb_detail']);
            } else {
                if ($c123['class1']['thumb_detail'] && $c123['class1']['thumb_detail'] != '|') {
                    $thumb_detail = explode("|", $c123['class1']['thumb_detail']);
                } else {
                    #$thumb_detail = array(600, 600);
                    $thumb_detail = $thumb_detail_default;
                }
            }

            //tab_num
            $tab_num = $c123['class2']['tab_num'] ? $c123['class2']['tab_num'] : ($c123['class1']['tab_num'] ? $c123['class1']['tab_num'] : 3);
            $tab_num_default = $c123['class1']['tab_num'] ? $c123['class1']['tab_num'] : 3;

            //tab_name
            if ($c123['class2']['tab_name'] && trim($c123['class2']['tab_name'], "|")) {
                $tab_name = explode("|", $c123['class2']['tab_name']);
            } else {
                if ($c123['class1']['tab_name'] && trim($c123['class1']['tab_name'], "|")) {
                    $tab_name = explode("|", $c123['class1']['tab_name']);
                } else {
                    $tab_name = array(
                        $_M['config']['met_productTabname'],
                        $_M['config']['met_productTabname_1'],
                        $_M['config']['met_productTabname_2'],
                        $_M['config']['met_productTabname_3'],
                        $_M['config']['met_productTabname_4']
                    );
                }
            }
        }

        //一级栏目
        if ($c_lev == 1) {
            //
            $list_length = $c123['class1']['list_length'] ? $c123['class1']['list_length'] : 8;
            $list_length_default = 8;

            //thumb_list
            if ($c123['class1']['thumb_list'] && $c123['class1']['thumb_list'] != '|') {
                $thumb_list = explode("|", $c123['class1']['thumb_list']);
            } else {
                $thumb_list = $thumb_list_default;
            }

            //thumb_detail
            if ($c123['class1']['thumb_detail'] && $c123['class1']['thumb_detail'] != '|') {
                $thumb_detail = explode("|", $c123['class1']['thumb_detail']);
            } else {
                #$thumb_detail = array(600, 600);
                $thumb_detail = $thumb_detail_default;
            }

            //tab_num
            $tab_num = $c123['class1']['tab_num'] ? $c123['class1']['tab_num'] : 3;
            $tab_num_default = 3;

            //tab_name
            if ($c123['class1']['tab_name'] && trim($c123['class1']['tab_name'], "|")) {
                $tab_name = explode("|", $c123['class1']['tab_name']);
            } else {
                $tab_name = array(
                    $_M['config']['met_productTabname'],
                    $_M['config']['met_productTabname_1'],
                    $_M['config']['met_productTabname_2'],
                    $_M['config']['met_productTabname_3'],
                    $_M['config']['met_productTabname_4']
                );
            }
        }

        $redata = array();
        $redata['tab_num'] = $tab_num;
        $redata['list_length_default'] = $list_length_default;
        $redata['thumb_list_default'] = implode("|", $thumb_list);
        $redata['thumb_detail_default'] = implode("|", $thumb_detail);
        $redata['tab_num_default'] = $tab_num_default;
        $redata['tab_name_default'] = implode("|", $tab_name);
        return $redata;
    }

    /**
     * 保存页面配置
     */
    public function doset_page_config()
    {
        global $_M;
        //config中的数据统一用config_tem处理
        $form = $_M['form'];

        //修改全局配置
        $this->_set_page_config($form);

        //修改栏目配置
        $res = self::SaveClassInfo($form);

        if ($res == true) {
            $this->success('', $_M['word']['jsok']);
        } else {
            $this->error($_M['word']['dataerror']);
        }
    }

    /**
     * 保存全局配置
     * @param array $config
     * @return array
     */
    public function _set_page_config($config = array())
    {
        global $_M;
        foreach ($config as $key => $val) {
            $query = "UPDATE {$_M['table']['config']} SET value='{$val}' WHERE name='{$key}' AND lang='{$_M['lang']}'";
            DB::query($query);
        }

        buffer::clearConfig();
        return array('status' => 1);
    }

    /**
     * 保存栏目样式配置
     * @param string $classnow
     * @param string $thumb_x
     * @param string $thumb_y
     * @param string $list_length
     * @param int $list_type
     * @return bool
     */
    public function SaveClassInfo($data = array())
    {
        global $_M;
        $save_data = array();
        $classnow = $data['classnow'];
        if (is_numeric($classnow)) {
            $class_label = load::mod_class('column/column_label', 'new');
            $class = $class_label->get_column_id($classnow);
            $save_data['id'] = $classnow;

            if(isset($data['thumb_list_x'])){
                $save_data['thumb_list'] = "{$data['thumb_list_x']}|{$data['thumb_list_y']}";
                if (str_replace('|', '', $save_data['thumb_list']) == '') {
                    $save_data['thumb_list'] = '';
                }
            }
            if(isset($data['thumb_detail_x'])){
                $save_data['thumb_detail'] = "{$data['thumb_detail_x']}|{$data['thumb_detail_y']}";
                if (str_replace('|', '', $save_data['thumb_detail']) == '') {
                    $save_data['thumb_detail'] = '';
                }
            }
            if(isset($data['list_length'])){
                $save_data['list_length'] = $data['list_length'];
            }

            if ($class['module'] == 3) {
                if (!$data['tab_name_0'] && !$data['tab_name_1'] && !$data['tab_name_2'] && !$data['tab_name_3'] && !$data['tab_name_4']) {
                    $save_data['tab_name'] = '';
                } else {
                    $save_data['tab_name'] = "{$data['tab_name_0']}|{$data['tab_name_1']}|{$data['tab_name_2']}|{$data['tab_name_3']}|{$data['tab_name_4']}";
                    if (str_replace('|', '', $save_data['tab_name']) == '') {
                        $save_data['tab_name'] = '';
                    }
                }
                $save_data['tab_num'] = $data['tab_num'];
            }

            load::mod_class('column/class/column_database', 'new')->update_by_id($save_data);

            buffer::clearColumn();

            return true;
        }
        $this->error[] = 'no id';
        return false;
    }

    /**
     * 清除缓存
     * @return [type] [description]
     */
    public function doclear_cache()
    {
        global $_M;
        self::_clear_cahce();
        $this->ajaxReturn(array('status' => 1, 'msg' => $_M['word']['jsok']));
    }

    /**
     *
     */
    private function _clear_cahce()
    {
        global $_M;
        if (file_exists(PATH_WEB . 'cache')) {
            deldir(PATH_WEB . 'cache', 1);
        }

        /*if(file_exists(PATH_WEB.'upload/thumb_src')){
            deldir(PATH_WEB.'upload/thumb_src');
        }*/

        $inc_file = PATH_WEB . "templates/{$this->no}/metinfo.inc.php";
        if (file_exists($inc_file)) {
            require $inc_file;
            if ($template_type) {
                deldir(PATH_WEB . 'templates/' . $_M['config']['met_skin_user'] . '/cache', 1);
            }
        }
    }

    /**
     * 清除缩略图
     */
    public function doClearThumb()
    {
        global $_M;
        if (file_exists(PATH_WEB . 'upload/thumb_src')) {
            deldir(PATH_WEB . 'upload/thumb_src');
        }
        $this->ajaxReturn(array('status' => 1, 'msg' => $_M['word']['jsok']));
    }

    /**
     * 可视化页面导航设置
     * @return [type] [description]
     */
    public function doapplist()
    {
        global $_M;
        $query = "select * from {$_M[table][applist]}";
        $list = DB::get_all($query);
        $apphandle = load::mod_class('ui_set/class/config_app.class.php', 'new');
        foreach ($list as $value) {
            if ($value['display'] && $value['no'] != 50002) {
                $applist[] = $apphandle->standard($value);
            }
        }
        $data['applist'] = $applist;
        return $data;
    }

    /**
     * 可视化页面导航修改保存
     * @return [type] [description]
     */
    public function dosave_pageset_nav()
    {
        global $_M;
        $applist_show = explode(',', $_M[form][applist]);
        $applist = $this->doapplist();
        foreach ($applist['applist'] as $key => $value) {
            $display = in_array($value[id], $applist_show) ? 2 : 1;
            $query = "update {$_M[table][applist]} set display='{$display}' where id='{$value[id]}'";
            DB::query($query);
        }
        $this->ajaxReturn(array('status' => 1, 'msg' => $_M['word']['jsok']));
    }

    /**
     * 取消系统安全提示
     */
    public function dono_uisetguide()
    {
        $configlist = array();
        $configlist[] = 'met_uiset_guide';
        configsave($configlist, array('met_uiset_guide' => 0));
        die('met_uiset_guide saved');
    }

    public function doagreement()
    {
        global $_M;
        if ($_M['form']['license']) {
            $string = @file_get_contents('https://u.mituo.cn/api/metinfo/license');
            file_put_contents(PATH_WEB . 'upload/file/license.html', $string);
        }
    }
}

# This program is an open source system, commercial use, please consciously to purchase commercial license.
# Copyright (C) MetInfo Co., Ltd. (http://www.metinfo.cn). All rights reserved.
