php代码授权和更新

数据表

应用表、
授权时长表、
应用版本表、
用户表
用户会员等级表
应用授权表、
应用订单表、
充值订单表、
提现表、
更新记录表、
下载记录表、

普通授权代码

基本信息
接口位置:/api/check/auth
参数提交方式为 POSTGET
参数1 $app 为客户端系统在授权系统中的ID
参数2 $data 为查询的信息(域名、机器人、IP、密钥等)

返回信息

返回值名 解释 示例
code 状态码 1
msg 状态信息 官网认证授权
endtime 到期时间 9999-12-31 23:59:59
返回值 返回信息 解释
1000 非法入侵检测 参数未填写完整
1100 系统不存在或者网站负责人未设置安全秘钥,无法检测授权。 检查不到应用的存在
1201 1202 1203 1204 请输入正确的授权信息 域名、机器人、IP、密钥信息不正确
1301 授权模式关闭状态 可能是文章模式
1302 授权模式未开启 关闭了授权验证
1401 远程获取的信息 授权被封禁
1402 远程获取的信息 授权过期
0 远程获取的信息 授权不存在
1 (授权信息)已认证官方正版授权 正版授权

高端授权代码

基本信息
接口位置:/api/check/authtop
参数提交方式为 POSTGET
参数1 $appic 为客户端系统在授权系统中的应用识别码
参数2 $data 为查询的信息(域名、IP等)
参数3 $key 为授权系统用户中心/我的授权中的授权代码

独家授权代码

接口位置:/api/check/authsole
参数提交方式为 POST
参数1 $appic 为客户端系统在授权系统中的应用识别码
参数2 $data 为查询的信息(域名等)

在线更新

口位置:/api/check/update
参数提交方式为 POST

参数1 $appic 为客户端系统在授权系统中的应用识别码
参数2 $version 为本地的版本号信息
参数3 $data 为查询的信息(域名、IP、密钥等)

返回信息

返回值名 解释 示例
code 状态码 1
msg 状态信息 目前是最新版本,无需更新
version 应用版本 1.0.0
link 更新包地址 http://1.cn/1.zip
sql 数据库地址 http://1.cn/1.sql
data 版本信息 [{version=>“1.0.0”,msg=>“

测试更新

”}]
返回值 返回信息 解释
1000 非法入侵检测 参数未填写完整
1111 请勿篡改系统版本 本地版本云端检测不到
2222 发生错误:未进行授权 未检测到授权
3333 系统不存在或者网站负责人未设置安全秘钥,无法检测授权。 检查不到应用的存在
1301 授权模式关闭状态 可能是文章模式
1302 授权模式未开启 关闭了授权验证
1401 远程获取的信息 授权被封禁
1402 远程获取的信息 授权过期
1102 更新服务器正在维护,请稍后访问! 未开启更新
1 远程获取的信息 程序版本更新
2 远程获取的信息 数据库也要更新
3 远程获取的信息 最新版本
4 目前是最新版本,无需更新 本地大于云端版本

所谓在线更新;就是下载和解压文件

ZipFolder.php

<?php
class ZipFolder
{
    protected $zip;
    protected $root;
    protected $ignored_names;
    public function __construct(){
        $this->zip = new ZipArchive;
    }
    /**
     * 解压zip文件到指定文件夹
     *
     * @access public
     * @param string $zipfile 压缩文件路径
     * @param string $path  压缩包解压到的目标路径
     * @return booleam 解压成功返回 true 否则返回 false
     */
    public function unzip ($zipfile, $path) {
        if ($this->zip->open($zipfile) === true) {
            $file_tmp = @fopen($zipfile, "rb");
            $bin = fread($file_tmp, 15); //只读15字节 各个不同文件类型,头信息不一样。
            fclose($file_tmp);
            /* 只针对zip的压缩包进行处理 */
            if (true === $this->getTypeList($bin))
            {
                $result = $this->zip->extractTo($path);
                $this->zip->close();
                return $result;
            }
            else
            {
                return false;
            }
        }
        return false;
    }
    /**
     * 创建压缩文件
     *
     * @access public
     * @param string $zipfile 将要生成的压缩文件路径
     * @param strng $folder 将要被压缩的文件夹路径
     * @param array $ignored 要忽略的文件列表
     * @return booleam 压缩包生成成功返回true 否则返回 false
     */
    public function zip ($zipfile, $folder, $ignored = null) {
        if ($this->zip->open($zipfile, ZIPARCHIVE::CREATE) !== true) {
            throw new Exception("cannot open <$zipfile>n");
        }
        $folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder)-1) : $folder;
        if(strstr($folder, '/')) {
            $this->root = substr($folder, 0, strrpos($folder, '/')+1);
            $folder = substr($folder, strrpos($folder, '/')+1);
        }
        $this->createZip($folder);
        return $this->zip->close();
    }
    /**
     * 递归添加文件到压缩包
     *
     * @access private
     * @param string $folder 添加到压缩包的文件夹路径
     * @param string $parent 添加到压缩包的文件夹上级路径
     * @return void
     */
    private function createZip ($folder, $parent=null) {
        $full_path = $this->root . $parent . $folder;
        $zip_path = $parent . $folder;
        $this->zip->addEmptyDir($zip_path);
        $dir = new DirectoryIterator($full_path);
        foreach($dir as $file) {
            if(!$file->isDot()) {
                $filename = $file->getFilename();
                if(!in_array($filename, $this->ignored_names)) {
                    if($file->isDir()) {
                        $this->createZip($filename, $zip_path.'/');
                    }else {
                        $this->zip->addFile($full_path.'/'.$filename, $zip_path.'/'.$filename);
                    }
                }
            }
        }
    }
    /**
     * 读取压缩包文件与目录列表
     *
     * @access public
     * @param string $zipfile 压缩包文件
     * @return array 文件与目录列表
     */
    public function fileList($zipfile) {
        $file_dir_list = array();
        $file_list = array();
        if ($this->zip->open($zipfile) == true) {
            for ($i = 0; $i < $this->zip->numFiles; $i++) {
                $numfiles = $this->zip->getNameIndex($i);
                if (preg_match('//$/i', $numfiles))
                {
                    $file_dir_list[] = $numfiles;
                }
                else
                {
                    $file_list[] = $numfiles;
                }
            }
        }
        return array('files'=>$file_list, 'dirs'=>$file_dir_list);
    }
    /**
     * 得到文件头与文件类型映射表
     *
     * @author  
     * @date 2013-08-10
     * @param $bin string 文件的二进制前一段字符
     * @return boolean
     */
    private function getTypeList ($bin)
    {
        $array = array(
            array("504B0304", "zip")
        );
        foreach ($array as $v)
        {
            $blen = strlen(pack("H*", $v[0])); //得到文件头标记字节数
            $tbin = substr($bin, 0, intval($blen)); ///需要比较文件头长度
            if(strtolower($v[0]) == strtolower(array_shift(unpack("H*", $tbin))))
            {
                return true;
            }
        }
        return false;
    }
}
?>

ajax.php

<?php
$type = $_POST['type'];
$url = '';//你的授权站定域名(例如http://baidu.com)
$appic = '';//应用识别码
$version = '1.0';//程序版本(本地的)
$data = $_SERVER['HTTP_HOST'];//授权信息(域名变量$_SERVER['HTTP_HOST'])
$host = '';//数据库地址
$name = '';//数据库库名
$port = '3306';//数据库端口
$user = '';//数据库用户吗
$pwd = '';//数据库密码
function curl_post($url,$data){
    $ch = curl_init();//初始化cURL
    curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串并输出到屏幕上
    curl_setopt($ch,CURLOPT_POST,1);//Post请求方式
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//Post变量
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);//执行并获得HTML内容
    curl_close($ch);
    return $output;
}
// var_dump(array('appic'=>$appic, 'version'=>$version, 'data'=>$data))
/*接口判断*/
$json = json_decode(curl_post($url.'/api/check/update',array('appic'=>$appic, 'version'=>$version, 'data'=>$data)),true);


if($type=='data'){
	/*检查更新*/
    if($json['code']==1 or $json['code']==2){//有更新
        $return = array("code" => 1000, "msg" => "检测到更新,请升级系统", "data" => $json['data'], "version" => $json['version']);
    }elseif($json['code']==3){//最新版本
        $return = array("code" => 2000, "msg" => "最新版本,无需更新", "data" => $json['data']);
    }else{//最新版本
        $return = array("code" => 3000, "msg" => "最新版本,无需更新", "data" => $json['msg']);
    }
}elseif($type=='update'){
	/*检查更新*/
    if($json['code']==1 or $json['code']==2){//有更新
	
        if (isset($json['link'])) {
			//因为有可能只是更新数据库
			// 下载压缩包
            @file_put_contents('./update.zip', file_get_contents($json['link']));
        }
		/*更新数据库*/
        if (isset($json['sql'])) {
            $db = new PDO("mysql:host=$host;dbname=$name;port=$port",$user,$pwd);
            $sql_array = preg_split("/;[rn]+/", file_get_contents($json['sql']));
            $s = $g = 0; //$s=>总数 $g=>错误数
            foreach($sql_array as $k => $v){
                try{
                    ++$s;
                    $db->exec($v);
                } catch (PDOException $e){
                    if($e->getMessage())++$g;
                }
            }
        }
		//解压
        include ("ZipFolder.php");
        $zip = new ZipFolder;
        if ($zip->unzip('./update.zip', '../../')) {
			//删除压缩包
            unlink('./update.zip');
            $return = array("code" => 1000, "msg" => "升级完成,请清空浏览器缓存后刷新页面");
        } else {
            $return = array("code" => 0, "msg" => "升级失败,请刷新页面后重新更新");
        }
    }elseif($json['code']==3){//最新版本
        $return = array("code" => 2000, "msg" => "最新版本,无需更新", "data" => $json['data']);
    }else{//最新版本
        $return = array("code" => 3000, "msg" => "最新版本,无需更新", "data"=> $json['msg']);
    }
}else{
    $return = array("code" => 0, "msg" => "非法请求");
}
exit(json_encode($return));