UpgradeService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use service\FileService;
  13. use think\Request;
  14. use service\HttpService;
  15. class UpgradeService extends FileService
  16. {
  17. //请求域名
  18. public static $domain = 'http://shop.crmeb.net/';
  19. //及时更新网址信息
  20. public static $updatewebinfourl = 'index.php/admin/server.upgrade_api/updatewebinfo.html';
  21. //公共接口地址 获取版本号
  22. public static $isNowVersionUrl = 'index.php/admin/server.upgrade_api/now_version.html';
  23. //公共接口地址 获取版本详情
  24. public static $isVersionInfo = 'index.php/admin/server.upgrade_api/version_info.html';
  25. //公共接口地址 获取历史版本列表
  26. public static $isList = 'index.php/admin/server.upgrade_api/get_version_list.html';
  27. //公共接口地址 写入更新版本信息
  28. public static $isInsertLog = 'index.php/admin/server.upgrade_api/set_upgrade_info.html';
  29. //公共接口地址 获取大于当前版本的所有版本
  30. public static $isNowVersion = 'index.php/admin/server.upgrade_api/get_now_version.html';
  31. //公共接口地址 更新网址信息
  32. protected static $UpdateWeBinfo = 'index.php/admin/server.upgrade_api/updatewebinfo.html';
  33. //公共接口地址 获取多少版本未更新
  34. public static $NewVersionCount = 'index.php/admin/server.upgrade_api/new_version_count.html';
  35. //公共接口地址 判断是否有权限 返回1有权限,0无权限
  36. protected static $Isauth = 'index.php/admin/server.upgrade_api/isauth.html';
  37. //相隔付
  38. private static $seperater = "{&&}";
  39. //更新网址信息
  40. public function snyweninfo($serverweb)
  41. {
  42. return self::request_post(self::$UpdateWeBinfo, $serverweb);
  43. }
  44. //判断是否有权限 返回1有权限,0无权限
  45. public function isauth()
  46. {
  47. return self::request_post(self::$Isauth);
  48. }
  49. /*
  50. *获取ip token 生成当前时间和过期时间
  51. * @param string ip
  52. * @param int $valid_peroid 过期周期 15天
  53. */
  54. public static function get_token($ip = '', $valid_peroid = 1296000)
  55. {
  56. $request = Request::instance();
  57. if (empty($ip)) $ip = $request->ip();
  58. $to_ken = $request->domain() . self::$seperater . $ip . self::$seperater . time() . self::$seperater . (time() + $valid_peroid) . self::$seperater;
  59. $token = self::enCode($to_ken);
  60. return $token;
  61. }
  62. private static function getRet($msg, $code = 400)
  63. {
  64. return ['msg' => $msg, 'code' => $code];
  65. }
  66. /**
  67. *
  68. * @param string $url
  69. * @param array $post_data
  70. */
  71. public static function start()
  72. {
  73. $pach = ROOT_PATH . 'version';
  74. $request = Request::instance();
  75. if (!file_exists($pach)) return self::getRet($pach . '升级文件丢失,请联系管理员');
  76. $version = @file($pach);
  77. if (!isset($version[0])) return self::getRet('获取失败');
  78. $lv = self::request_post(self::$isNowVersionUrl, ['token' => self::get_token($request->ip())]);
  79. if (isset($lv['code']) && $lv['code'] == 200)
  80. $version_lv = isset($lv['data']['version']) && $lv['data']['version'] ? $lv['data']['version'] : false;
  81. else
  82. return isset($lv['msg']) ? self::getRet($lv['msg']) : self::getRet('获取失败');
  83. if ($version_lv === false) return self::getRet('获取失败');
  84. if (strstr($version[0], '=') !== false) {
  85. $version = explode('=', $version[0]);
  86. if ($version[1] != $version_lv) {
  87. return self::getRet($version_lv, 200);
  88. }
  89. }
  90. return self::getRet('获取失败');
  91. }
  92. public static function getVersion()
  93. {
  94. $pach = APP_PATH . 'version.php';
  95. if (!file_exists($pach)) return self::getRet($pach . '升级文件丢失,请联系管理员');
  96. $version = @file($pach);
  97. if (!isset($version[0]) && !isset($version[1])) return self::getRet('获取失败');
  98. $arr = [];
  99. foreach ($version as $val) {
  100. list($k, $v) = explode('=', $val);
  101. $arr[$k] = $v;
  102. }
  103. return self::getRet($arr, 200);
  104. }
  105. /**
  106. * 模拟post进行url请求
  107. * @param string $url
  108. * @param array $post_data
  109. */
  110. public static function request_post($url = '', $post_data = array())
  111. {
  112. if (strstr($url, 'http') === false) $url = self::$domain . $url;
  113. if (empty($url)) {
  114. return false;
  115. }
  116. if (!isset($post_data['token'])) $post_data['token'] = self::get_token();
  117. $o = "";
  118. foreach ($post_data as $k => $v) {
  119. $o .= "$k=" . urlencode($v) . "&";
  120. }
  121. $post_data = substr($o, 0, -1);
  122. $postUrl = $url;
  123. $curlPost = $post_data;
  124. $ch = curl_init();//初始化curl
  125. curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
  126. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  128. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  129. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  130. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  131. $data = curl_exec($ch);//运行curl
  132. curl_close($ch);
  133. if ($data) {
  134. $data = json_decode($data, true);
  135. }
  136. return $data;
  137. }
  138. /**
  139. * 验证远程文件是否存在 以及下载
  140. * @param string $url 文件路径
  141. * @param string $savefile 保存地址
  142. */
  143. public static function check_remote_file_exists($url, $savefile)
  144. {
  145. $url = self::$domain . 'public' . DS . 'uploads' . DS . 'upgrade' . DS . $url;
  146. $url = str_replace('\\', '/', $url);
  147. $curl = curl_init($url);
  148. curl_setopt($curl, CURLOPT_NOBODY, true);
  149. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
  150. // 发送请求
  151. $result = curl_exec($curl);
  152. $found = false;
  153. // 如果请求没有发送失败
  154. if ($result !== false) {
  155. // 再检查http响应码是否为200
  156. $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  157. if ($statusCode == 200) {
  158. curl_close($curl);
  159. $fileservice = new self;
  160. //下载文件
  161. $zip = $fileservice->down_remote_file($url, $savefile);
  162. if ($zip['error'] > 0) return false;
  163. if (!isset($zip['save_path']) && empty($zip['save_path'])) return false;
  164. if (!file_exists($zip['save_path'])) return false;
  165. return $zip['save_path'];
  166. }
  167. }
  168. curl_close($curl);
  169. return $found;
  170. }
  171. /**
  172. * 通用加密
  173. * @param String $string 需要加密的字串
  174. * @param String $skey 加密EKY
  175. * @return String
  176. */
  177. private static function enCode($string = '', $skey = 'fb')
  178. {
  179. $skey = array_reverse(str_split($skey));
  180. $strArr = str_split(base64_encode($string));
  181. $strCount = count($strArr);
  182. foreach ($skey as $key => $value) {
  183. $key < $strCount && $strArr[$key] .= $value;
  184. }
  185. return str_replace('=', 'O0O0O', join('', $strArr));
  186. }
  187. /**
  188. * 去除回车,去取空格,去除换行,去除tab
  189. * @param String $str 需要去除的字串
  190. * @return String
  191. */
  192. public static function replace($str)
  193. {
  194. return trim(str_replace(array("\r", "\n", "\t"), '', $str));
  195. }
  196. }