UpgradeService.php 7.1 KB

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