thinkphp3.2.3 cms网站在线升级功能

3652

一般如果网站太多便会出去难一系列的问题!比如模板更新、系统问题都需要更新或者修复,如果一个个去修改很明显就不明智了!所以直接写了一个网站在线升级功能!只要在线发布补丁所有的网站都能一键更新!


结合thinkphp3.2.3写了一个网站在线升级的功能

<?php
/**
 * 在线升级
 * @author      [我就叫小柯] <[972581428@qq.com]>
 * @copyright     Copyright (c) 2017 [环企优站科技]  (http://www.usezan.com)
 * @version      Usezan企业网站管理系统 v1.0
 */
namespace Usezan\Controller;
use Think\Controller;
use Think\Model;
class UpgradeController extends BaseController {
    //请求地址
private $_patchurl = "http://xxx.usezan.com/package/v1/";
public function index() {
$upgrade_path_base = $this->_patchurl;
//当前版本号
$version = include 'version.php';
//更新内容
$vercon = $this->_patchurl.$version['pc_release'].'.html';
$versioncontent = @file_get_contents($vercon);
if ($versioncontent){
$this->assign('versioncontent',$versioncontent);
}
//获取当前版本号
$current_version = array("pc_version" => $version['pc_version'], "pc_release" => $version['pc_release']);
$pathlist_str = @file_get_contents($upgrade_path_base);
$pathlist = $allpathlist = array();
$key = -1;
preg_match_all("/\"(patch_[\w_]+\.zip)\"/", $pathlist_str, $allpathlist);
$allpathlist = $allpathlist[1];
foreach ($allpathlist as $k => $v ) {
if (strstr($v, "patch_" . $version['pc_release'])) {
$key = $k;
break;
}
}
$key = ($key < 0 ? 9999 : $key);
foreach ($allpathlist as $k => $v ) {
if ($key <= $k) {
$pathlist[$k] = $v;
}
}
if (IS_POST) {
if (empty($pathlist)) {
$this->ajaxReturn(array("status"=>1,"info"=>"恭喜您!升级成功!"));
}
if (!file_exists(CACHE_PATH . "upgrade")) {
@mkdir(CACHE_PATH . "upgrade");
}
foreach ($pathlist as $k => $v) {
$upgradezip_url = $upgrade_path_base . $v;
$upgradezip_path = CACHE_PATH . "upgrade" . DIRECTORY_SEPARATOR . $v;
$upgradezip_source_path = CACHE_PATH . "upgrade" . DIRECTORY_SEPARATOR . basename($v, ".zip");
@file_put_contents($upgradezip_path, @file_get_contents($upgradezip_url)); //下载合并文件
$zip = new \ZipArchive;
if ($zip->open($upgradezip_path) === TRUE) {
$zip->extractTo($upgradezip_source_path);
$zip->close();
} else {
$this->ajaxReturn(array("status"=>0,"info"=>"解压文件失败..."));
}
$copy_from = $upgradezip_source_path . DIRECTORY_SEPARATOR . "upload" . DIRECTORY_SEPARATOR;
$copy_to = "./";
$this->copyfailnum = 0;
$this->copydir($copy_from, $copy_to); //复制替换文件
if (0 < $this->copyfailnum) {
@file_put_contents("./version.php", "<?php return " . var_export($current_version, true) . ";");
$this->ajaxReturn(array("status"=>0,"info"=>"升级失败!请检查目录权限!"));
}
//更新sql语句
$sql_path = $upgradezip_source_path . DIRECTORY_SEPARATOR . "ext" . DIRECTORY_SEPARATOR;
$file_list = glob($sql_path . "*");
$db_model = new Model();
$dbcharset = "utf8";
if (!empty($file_list)) {
foreach ($file_list as $fk => $fv ) {
if (in_array(strtolower(substr($fv, -3, 3)), array("php", "sql"))) {
if ((strtolower(substr($file_list[$fk], -3, 3)) == "sql") && ($data = file_get_contents($file_list[$fk]))) {
// $model_name = substr(basename($fv), 0, -4);
$sqls = explode("{SQL}", $data);
foreach ($sqls as $sql ) {
if (empty($sql)) {
continue;
}
if (("4.1" < mysql_get_server_info) && $dbcharset) {
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "TYPE=\1 DEFAULT CHARSET=" . $dbcharset, $sql);
}
$db_model->query($sql);
}
}
}
}
}
@unlink($upgradezip_path); //更新完删除文件
$this->deletedir($upgradezip_source_path); //删除文件夹更新内容
}
        $this->ajaxReturn(array("status"=>1,"info"=>"恭喜您!升级成功!"));
} else {
$this->assign('version',$version);
$this->assign("pathlist", $pathlist);
$this->display();
}
}
//替换文件
protected function copydir($dirfrom, $dirto, $cover = "") {
if (is_file($dirto)) {
$this->ajaxReturn(array("status"=>0,"info"=>"文件已经存在(" . $dirto . "),且无法替换。请检查文件权限。"));
}
if (!file_exists($dirto)) {
mkdir($dirto);
}
$handle = opendir($dirfrom);
while (false !== $file = readdir($handle)) {
if (($file != ".") && ($file != "..")) {
$filefrom = $dirfrom . DIRECTORY_SEPARATOR . $file;
$fileto = $dirto . DIRECTORY_SEPARATOR . $file;
if (is_dir($filefrom)) {
$this->copydir($filefrom, $fileto, $cover);
} else if (!copy($filefrom, $fileto)) {
$this->copyfailnum++;
$tips = "复制" . $filefrom . $fileto . "失败<br />";
$this->ajaxReturn(array("status"=>0,"info"=>$tips));
}
}
}
}
//删除目录
protected function deletedir($dirname) {
$result = false;
if (!is_dir($dirname)) {
$this->ajaxReturn(array("status"=>0,"info"=>$dirname."is not a dir!"));
}
$handle = opendir($dirname);
while (($file = readdir($handle)) !== false) {
if (($file != ".") && ($file != "..")) {
$dir = $dirname . DIRECTORY_SEPARATOR . $file;
is_dir($dir) ? $this->deletedir($dir) : unlink($dir);
}
}
closedir($handle);
$result = (rmdir($dirname) ? true : false);
return $result;
}
 
}