UpgradeService.php 7.9 KB

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