UpgradeService.php 7.5 KB

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