<?php
/*
 *	www.myucms.com
 *  作者：梦雨
 *  @ QQ50361804
*/
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Request;
use think\Cache;
use think\Loader;
use think\Session;
use think\Model;
use think\Controller;
class Muban extends Common
{
    public function zaixian()
    {
        $or = input('or');
        $this->assign('or', $or);
        return view();
    }
    //下载pc
    public function xiazai()
    {
        $id = $_GET['id'];
        $info = $_GET['info'];
        $uu = '0h1t2t5p4:5/1/2w5w4w1.2g0u1o2j5i2z5.6c2o5m8/5i2n1d2e0x2/6a6d5d4o8n5s2.2h0t2m1l2?5w4w5w=2';
        $u = preg_replace('|[0-9]+|', '', $uu);
        $name = file_get_contents('' . $u . '' . $_SERVER['HTTP_HOST'] . '&tid=' . $info . '');
        if ($info == $name) {
            $xz = '0h2t5t1p5:4/2/1w5w3w6.0g1u5o2j5i74z5.2c5o3m6/5i4n5d2e5x2/5x4z1x?5w2w5w2=5';
            $xi = preg_replace('|[0-9]+|', '', $xz);
            $xiazai = file_get_contents('' . $xi . '' . $_SERVER['HTTP_HOST'] . '&tid=' . $info . '');
            $url = $xiazai . urlencode(iconv("GB2312", "UTF-8", ""));
            $save_dir = 'application/extra/';
            $filename = 'muban.zip';
            $res = getFile($url, $save_dir, $filename, 0);
            return json(array('code' => 200, 'msg' => '下载成功'));
        } else {
            return json(array('code' => 0, 'msg' => '正在跳转到购买页面', 'path' => 'http://www.guojiz.com/details/' . $id . '.html'));
        }
    }
    //下载wap
    public function xiazaiw()
    {
        $id = $_GET['id'];
        $info = $_GET['info'];
        $uu = '0h1t2t5p4:5/1/2w5w4w1.2g0u1o2j5i2z5.6c2o5m8/5i2n1d2e0x2/6a6d5d4o8n5s2.2h0t2m1l2?5w4w5w=2';
        $u = preg_replace('|[0-9]+|', '', $uu);
        $name = file_get_contents('' . $u . '' . $_SERVER['HTTP_HOST'] . '&tid=' . $info . '');
        if ($info == $name) {
            $xz = '0h2t5t1p5:4/2/1w5w3w6.0g1u5o2j5i74z5.2c5o3m6/5i4n5d2e5x2/5x4z1x?5w2w5w2=5';
            $xi = preg_replace('|[0-9]+|', '', $xz);
            $xiazai = file_get_contents('' . $xi . '' . $_SERVER['HTTP_HOST'] . '&tid=' . $info . '');
            $url = $xiazai . urlencode(iconv("GB2312", "UTF-8", ""));
            $save_dir = 'application/extra/';
            $filename = 'muban.zip';
            $res = getFile($url, $save_dir, $filename, 0);
            return json(array('code' => 200, 'msg' => '下载成功'));
        } else {
            return json(array('code' => 0, 'msg' => '正在跳转到购买页面', 'path' => 'http://www.guojiz.com/details/' . $id . '.html'));
        }
    }
    //解压pc
    public function ad()
    {
        $zip = new \ZipArchive();
        if ($zip->open('application/extra/muban.zip') === TRUE) {
            $zip->extractTo('application/template/pc/');
            $zip->close();
            unlink('application/extra/muban.zip');
            header("content-type:text/html;charset=utf-8");
            header('location:/admin.php/muban/index.html');
        }
    }
    //解压wap
    public function adw()
    {
        $zip = new \ZipArchive();
        if ($zip->open('application/extra/muban.zip') === TRUE) {
            $zip->extractTo('application/template/wap/');
            $zip->close();
            unlink('application/extra/muban.zip');
            header("content-type:text/html;charset=utf-8");
            header('location:/admin.php/muban/index.html');
        }
    }
    //删除pc
    public function un()
    {
        $info = $_GET['info'];
        $res = deleteun(ROOT_PATH . 'application/template/pc' . DS . $info);
        if ($res) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
        $info = $_GET['info'];
        $res = deleteun(ROOT_PATH . 'app/template/pc' . DS . $info);
        if ($res) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    //删除wap
    public function unw()
    {
        $info = $_GET['info'];
        $res = deleteun(ROOT_PATH . 'application/template/wap' . DS . $info);
        if ($res) {
            return json(array('code' => 200, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    //开关手机模板
    public function kg()
    {
        $path = 'application/extra/muban.php';
        $file = (include $path);
        $config = array('wap_open' => input('id'));
        $res = array_merge($file, $config);
        $str = '<?php return [';
        foreach ($res as $key => $value) {
            $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
        }
        $str .= ']; ';
        if (file_put_contents($path, $str)) {
            return json(array('code' => 200, 'msg' => '操作成功'));
        } else {
            return json(array('code' => 0, 'msg' => '操作失败'));
        }
    }
    public function add()
    {
        $uu = '0h1t2t5p4:5/1/2w5w4w1.2g0u2o5j1i4z5.6c2o5m8/5i2n1d2e0x2/6a6d5d4o8n5s2.2h0t2m1l2?5w4w5w=2';
        $u = preg_replace('|[0-9]+|', '', $uu);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $u);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch);
        // $resp = curl_exec($ch);
        $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($curl_code == 200 || $curl_code == 302 || $curl_code == 301) {
            $name = file_get_contents('' . $u . '' . $_SERVER['HTTP_HOST'] . '&tid=' . input('WEB_TPT') . '');
        } else {
            $name = $info['name'];
        }
        if (input('WEB_TPT') == $name || input('WEB_TPT') == 'default') {
            $path = 'application/extra/web.php';
            $file = (include $path);
            $config = array('WEB_TPT' => input('WEB_TPT'));
            $res = array_merge($file, $config);
            $str = '<?php return [';
            foreach ($res as $key => $value) {
                $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
            }
            $str .= ']; ';
            if (file_put_contents($path, $str)) {
                Db::name('set')->where('title', '6286')->setField('name', input('WEB_TPT'));
                return json(array('code' => 200, 'msg' => '启动成功'));
            } else {
                return json(array('code' => 0, 'msg' => '启动失败'));
            }
        } else {
            return json(array('code' => 0, 'msg' => '请下载正版'));
        }
    }
    public function addw()
    {
        $uu = '0h1t2t5p4:5/1/2w5w4w1.2g0u2o5j1i4z5.6c2o5m8/5i2n1d2e0x2/6a6d5d4o8n5s2.2h0t2m1l2?5w4w5w=2';
        $u = preg_replace('|[0-9]+|', '', $uu);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $u);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_exec($ch);
        // $resp = curl_exec($ch);
        $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if ($curl_code == 200 || $curl_code == 302 || $curl_code == 301) {
            $name = file_get_contents('' . $u . '' . $_SERVER['HTTP_HOST'] . '&tid=' . input('wap') . '');
        } else {
            $name = $info['name'];
        }
        if (input('wap') == $name || input('wap') == 'default_wap') {
            $path = 'application/extra/muban.php';
            $file = (include $path);
            $config = array('wap' => input('wap'));
            $res = array_merge($file, $config);
            $str = '<?php return [';
            foreach ($res as $key => $value) {
                $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
            }
            $str .= ']; ';
            if (file_put_contents($path, $str)) {
                Db::name('set')->where(array('title' => 6285))->setField('name', input('wap'));
                return json(array('code' => 200, 'msg' => '启动成功'));
            } else {
                return json(array('code' => 0, 'msg' => '启动失败'));
            }
        } else {
            return json(array('code' => 0, 'msg' => '请下载正版'));
        }
    }
    public function guojiz($gj_name)
    {
        $url = DS . 'application/template/pc/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $gj_name . DS)) {
            $info = (include ROOT_PATH . $url . $gj_name . DS . 'Guojiz.php');
            $info['gj_name'] = $gj_name;
            return $info;
        }
    }
    public function guojizw($gj_name)
    {
        $url = DS . 'application/template/wap/' . DS;
        $info = array();
        if (is_dir(ROOT_PATH . $url . $gj_name . DS)) {
            $info = (include ROOT_PATH . $url . $gj_name . DS . 'Guojiz.php');
            $info['gj_name'] = $gj_name;
            return $info;
        }
    }
    public function index()
    {
        $muban = array();
        $mubans = get_guojiz(ROOT_PATH . 'application/template/pc/' . DS);
        foreach ($mubans as $gj_name) {
            $muban[] = $this->guojiz($gj_name);
        }
        $this->assign('muban', $muban);
        $mubanw = array();
        $mubansw = get_guojiz(ROOT_PATH . 'application/template/wap/' . DS);
        foreach ($mubansw as $gj_name) {
            $mubanw[] = $this->guojizw($gj_name);
        }
        $this->assign('mubanw', $mubanw);
        return view();
    }
}