<?php

if (!defined('ROOT')) exit('Can\'t Access !');

class service
{

    public static $me;

    public static function getInstance()
    {
        if (!self::$me) {
            $class = new service();
            self::$me = $class;
        }
        return self::$me;
    }

    public static function cmseayurl($url){
        $varifyurl="https://u.cmseasy.cn/";
        $opts = array(
            'http'=>array(
                'method'=>"GET",
                'timeout'=>1, //设置超时
            )
        );
        $context = stream_context_create($opts);
        if(!self::varify_url($varifyurl)){
            exit("<div class='tip_box' style='width:300px;margin:0px auto;margin-top:50px;padding:20px;border:5px solid #ccc;border-radius: 5px 5px 5px 5px;text-align:center;'>" . lang_admin('no_server') . "</div>");
            exit;
        }
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
        if(self::is_ssl()){
            ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)');
            $data = file_get_contents($url,false,$context);
        }else{
            ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)');
            $url= str_replace("https://","http://",$url);
            $data=file_get_contents($url,false,$context);
        }
        return $data;
    }

    /*
     * 判断网络是否连接
     * */
    public static function varify_url($url){
        if(!self::is_ssl()){
            $url= str_replace("https://","http://",$url);
        }
        $check = @fopen($url,"r");
        if($check){
            $status = true;
        }else{
            $status = false;
        }
        return $status;
    }


    /***
     * 判断是否SSL协议
     * @return boolean
     */
    public static function is_ssl()
    {
        if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
            return true;
        } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
            return true;
        }
        return false;
    }

    /***
     * 登陆服务端用户
     * @return boolean
     */
    function getlogin(){
        $returndata=array();
        if (session::get("user_buy_all")==""){
            $app_login =$this->get_service_users();
            if(!empty($app_login)){
                $url="https://u.cmseasy.cn/index.php?case=client&act=cmseaylogin&username=".$app_login["username"]."&passwrod=".$app_login['passwrod']."&vercode="._NEWVERCODE;  //服务器获取列表的地址
                $url.="&licenstype=2";  //问答的授权
                $data=service::cmseayurl($url);   //获取服务器的数据
                $data=json_decode($data, true);
                if($data["static"]) {
                    //写到缓存
                    $returndata['static'] = 1;
                    $returndata["message"] = $data['message'];  //登陆成功
                    $returndata["userdata"] = $data['userdata'];
                    $returndata["appsdata"] = $data['appsdata'];
                    $returndata["modulesdata"] = $data['modulesdata'];
                    $returndata["modeldata"] = $data['modeldata'];
                    $returndata["licensedata"]=$data['licensedata'];
                    session::set("user_buy_all", $returndata);

                    //修改本地缓存购买状态
                    foreach($data['appsdata'] as $key=>$val){
                        if($_SERVER['SERVER_NAME']==$val['buyip'] || 'www.'.$_SERVER['SERVER_NAME']==$val['buyip']
                            || $_SERVER['SERVER_NAME']=='www.'.$val['buyip']){   //已经购买的 并且域名一直  更新本地缓存
                            apps::getInstance()->rec_update("isbuy=1"," id='".$val['buyid']."'");
                        }
                    }
                    //修改本地缓存购买状态
                  /*  foreach($data['modeldata'] as $key=>$val){
                        if($_SERVER['SERVER_NAME']==$val['buyip'] || 'www.'.$_SERVER['SERVER_NAME']==$val['buyip']
                            || $_SERVER['SERVER_NAME']=='www.'.$val['buyip']) {   //已经购买的 并且域名一直  更新本地缓存
                            cutbuytemplate::getInstance()->rec_update("isbuy=1", " code='" . $val['buyid'] . "'");
                        }
                    }*/
                    //修改本地缓存购买状态
                  /*  foreach($data['modulesdata'] as $key=>$val){
                        if($_SERVER['SERVER_NAME']==$val['buyip'] || 'www.'.$_SERVER['SERVER_NAME']==$val['buyip']
                            || $_SERVER['SERVER_NAME']=='www.'.$val['buyip']) {   //已经购买的 并且域名一直  更新本地缓存
                            cutbuymodules::getInstance()->rec_update("isbuy=1", " code='" . $val['buyid'] . "'");
                        }
                    }*/
                }
            }
            if($returndata['static']!=1 || $returndata==""){
                $returndata['static']=0;
                $returndata["message"]=lang('login_failure');  //登陆失败
                $returndata["modeldata"]="";
                $returndata["userdata"]="";
                $returndata["appsdata"]=array();
                $returndata["modulesdata"]=array();
                $returndata["modeldata"]=array();
                $returndata["licensedata"]=array();
            }
            session::set("user_buy_all", $returndata);
        }else{
            $returndata=session::get("user_buy_all");
        }
        return $returndata;

    }

    //保存组件 服务端账号密码
    public function save_service_users($app_login)
    {
        if (empty($app_login))
            return;
        $data='<?php return ' . var_export($app_login, true) . ';';
        $service_users_path=ROOT."/config/service_users.php";
        //判断文件是否存在！不存在则创建
        if (!file_exists( $service_users_path )) {@fopen($service_users_path, "w");}

        $f = fopen($service_users_path,'w');
        fwrite($f,$data);
        fclose($f);
    }
    //保存组件 服务端账号密码
    public function get_service_users()
    {
        $service_users_path=ROOT."/config/service_users.php";
        //判断文件是否存在！不存在则创建
        if (!file_exists( $service_users_path )) {@fopen($service_users_path, "w");}
        $service_users = include $service_users_path;
        return $service_users;
    }




}