123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace crmeb\services;
- class UpgradeService extends FileService
- {
-
- public static $domain = 'http://shop.crmeb.net/';
-
- public static $updatewebinfourl = 'index.php/admin/server.upgrade_api/updatewebinfo.html';
-
- public static $isNowVersionUrl = 'index.php/admin/server.upgrade_api/now_version.html';
-
- public static $isVersionInfo = 'index.php/admin/server.upgrade_api/version_info.html';
-
- public static $isList = 'index.php/admin/server.upgrade_api/get_version_list.html';
-
- public static $isInsertLog = 'index.php/admin/server.upgrade_api/set_upgrade_info.html';
-
- public static $isNowVersion = 'index.php/admin/server.upgrade_api/get_now_version.html';
-
- protected static $UpdateWeBinfo = 'index.php/admin/server.upgrade_api/updatewebinfo.html';
-
- public static $NewVersionCount = 'index.php/admin/server.upgrade_api/new_version_count.html';
-
- protected static $Isauth = 'index.php/admin/server.upgrade_api/isauth.html';
-
- private static $seperater = "{&&}";
-
- public function snyweninfo($serverweb)
- {
- return self::request_post(self::$UpdateWeBinfo, $serverweb);
- }
-
- public function isauth()
- {
- return self::request_post(self::$Isauth);
- }
-
- public static function get_token($ip = '', $valid_peroid = 1296000)
- {
- $request = app('request');
- if (empty($ip)) $ip = $request->ip();
- $to_ken = $request->domain() . self::$seperater . $ip . self::$seperater . time() . self::$seperater . (time() + $valid_peroid) . self::$seperater;
- $token = self::enCode($to_ken);
- return $token;
- }
- private static function getRet($msg, $code = 400)
- {
- return ['msg' => $msg, 'code' => $code];
- }
-
- public static function start()
- {
- $pach = app()->getRootPath() . 'version';
- $request = app('request');
- if (!file_exists($pach)) return self::getRet($pach . '升级文件丢失,请联系管理员');
- $version = @file($pach);
- if (!isset($version[0])) return self::getRet('获取失败');
- $lv = self::request_post(self::$isNowVersionUrl, ['token' => self::get_token($request->ip())]);
- if (isset($lv['code']) && $lv['code'] == 200)
- $version_lv = isset($lv['data']['version']) && $lv['data']['version'] ? $lv['data']['version'] : false;
- else
- return isset($lv['msg']) ? self::getRet($lv['msg']) : self::getRet('获取失败');
- if ($version_lv === false) return self::getRet('获取失败');
- if (strstr($version[0], '=') !== false) {
- $version = explode('=', $version[0]);
- if ($version[1] != $version_lv) {
- return self::getRet($version_lv, 200);
- }
- }
- return self::getRet('获取失败');
- }
- public static function getVersion()
- {
- $pach = app()->getRootPath() . '.version';
- if (!file_exists($pach)) return self::getRet($pach . '升级文件丢失,请联系管理员');
- $version = @file($pach);
- if (!isset($version[0]) && !isset($version[1])) return self::getRet('获取失败');
- $arr = [];
- foreach ($version as $val) {
- list($k, $v) = explode('=', $val);
- $arr[$k] = $v;
- }
- return self::getRet($arr, 200);
- }
-
- public static function request_post($url = '', $post_data = array())
- {
- if (strstr($url, 'http') === false) $url = self::$domain . $url;
- if (empty($url)) {
- return false;
- }
- if (!isset($post_data['token'])) $post_data['token'] = self::get_token();
- $o = "";
- foreach ($post_data as $k => $v) {
- $o .= "$k=" . urlencode($v) . "&";
- }
- $post_data = substr($o, 0, -1);
- $postUrl = $url;
- $curlPost = $post_data;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $postUrl);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
- curl_setopt($ch, CURLOPT_TIMEOUT, 20);
- $data = curl_exec($ch);
- curl_close($ch);
- if ($data) {
- $data = json_decode($data, true);
- }
- return $data;
- }
-
- public static function check_remote_file_exists($url, $savefile)
- {
- $url = self::$domain . 'public' . DS . 'uploads' . DS . 'upgrade' . DS . $url;
- $url = str_replace('\\', '/', $url);
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_NOBODY, true);
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
-
- $result = curl_exec($curl);
- $found = false;
-
- if ($result !== false) {
-
- $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if ($statusCode == 200) {
- curl_close($curl);
- $fileservice = new self;
-
- $zip = $fileservice->down_remote_file($url, $savefile);
- if ($zip['error'] > 0) return false;
- if (!isset($zip['save_path']) && empty($zip['save_path'])) return false;
- if (!file_exists($zip['save_path'])) return false;
- return $zip['save_path'];
- }
- }
- curl_close($curl);
- return $found;
- }
-
- private static function enCode($string = '', $skey = 'fb')
- {
- $skey = array_reverse(str_split($skey));
- $strArr = str_split(base64_encode($string));
- $strCount = count($strArr);
- foreach ($skey as $key => $value) {
- $key < $strCount && $strArr[$key] .= $value;
- }
- return str_replace('=', 'O0O0O', join('', $strArr));
- }
-
- public static function replace($str)
- {
- return trim(str_replace(array("\r", "\n", "\t"), '', $str));
- }
- }
|