HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/cs.wgegeghx.com/application/admin/controller/Upgrade.php
<?php
namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Db;
use think\Cache;
use think\Config;

class Upgrade extends Adminbase
{
    protected $noNeedRight = [];
    //更新控制器
    public $gengxin = false; //是否执行了更新
    
    public function index($version='') {
        $version = config('app_version');
        try {
            $data['version'] = $version;//此处为网站根目录/config/app.php下的app_version
            $data['authkey'] = config('app_syskey');//程序的密钥
            $result = yuanJson(config('app_authurl').'/api/Authlook/update',$data);
        } catch (\Exception $e) {
            return json(["msg" => "更新包获取失败,请重试!", "code" => 400]);
        }
        $result = json_decode($result, true);
        if(empty($result)){
            return json(["msg" => "更新包获取失败,请重试!", "code" => 400]);
        }
        if($result["code"] == 100 && $this->gengxin == true){ //循环更新完毕
            //cache(CACHE_PATH, false);
            //缓存是否刷新请自行添加代码
            return json(["msg" => "更新完成!请刷新页面", "code" => 200]);
        }
        //code为400的时候代表没有更新包
        if ($result["code"] == 0) { //没有需要更新的版本
            return json(["msg" => $result["msg"], "code" => 400]);
        }

        $this->gengxin = true; //开始更新版本

        //更新包信息
        $upgrade = $result;
        $file_url = $upgrade["url"];
        $filename = basename($file_url);

        $dir = ROOT_PATH . "runtime/upgrade/";

        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        

        $path = file_exists($dir . $filename) ? $dir . $filename : $this->download_file($file_url, $dir, $filename);
        
        $zip = new \ZipArchive();
        //打开压缩包
        if ($zip->open($path) === true) {
            $toPath = ROOT_PATH;
            try {
                //解压文件到toPath路径下,用于覆盖差异文件
                $zip->extractTo($toPath);
                unlink($path); //删除更新包
            } catch (\Exception $e) {
                return json(["msg" => "没有该目录[" . $toPath . "]的写入权限", "code" => 400]);
            }
            //文件差异覆盖完成,开始更新数据库
            if(file_exists(ROOT_PATH . "/sql.php")){
                include ROOT_PATH . "/sql.php";
                chmod(ROOT_PATH . "/sql.php",0777);
                unlink(ROOT_PATH . "/sql.php");
            }
            return $this->index(); //递归更新

        } else {
            unlink($path); //删除更新包
            return json(["msg" => "更新包解压失败,请重试!", "code" => 400]);
        }

    }
    
    
    public function download_file($url, $dir, $filename = '') {
        if (empty($url)) {
            return false;
        }
        $ext = strrchr($url, '.');
        $dir = realpath($dir);
        //目录+文件
        $filename = (empty($filename) ? '/' . time() . '' . $ext : '/' . $filename);
        $filename = $dir . $filename;
        //开始捕捉
        ob_start();
        readfile($url);
        $img = ob_get_contents();
        ob_end_clean();
        $size = strlen($img);
        $fp2 = fopen($filename, "a");
        fwrite($fp2, $img);
        fclose($fp2);
        return $filename;
    }
    

}