UpgradeService.php 7.1 KB

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