Skip to content

Commit

Permalink
Update:修复升级无效等bug,增加导航栏按钮。
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwalk committed Jul 30, 2018
1 parent d480ec1 commit ed015b7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 46 deletions.
86 changes: 56 additions & 30 deletions Update/Action.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
<?php
class Update_Action extends Typecho_Widget implements Widget_Interface_Do {
public $_dir;
class Update_Action extends Typecho_Widget {
private $options;
private $security;
private $_dir;
private $backdir;
private $tempdir;
const lastestUrl = "https://github.com/typecho/typecho/archive/master.zip";

public function __construct($request, $response, $params = NULL) {
parent::__construct($request, $response, $params);

$this->_dir ='.'. __TYPECHO_PLUGIN_DIR__.'/Update/';
$this->options = Helper::options();
$this->security = Helper::security();
$this->_dir = __TYPECHO_ROOT_DIR__. __TYPECHO_PLUGIN_DIR__."/Update/";
$this->backdir = $this->_dir."backup";
$this->tempdir = $this->_dir."temp";
if(method_exists($this, $this->request->step))
call_user_func(array($this, $this->request->step));
else $this->zero();
}

//升级初章开启进程线
public function zero() {
$url = Helper::options()->index."/update/";
$adminUrl = Helper::options()->adminUrl;
$url = $this->options->index."/update/";
$adminUrl = $this->options->adminUrl."upgrade.php";
if( !file_exists($this->backdir) && !mkdir($this->backdir, 0777, true) ) {
return $this->log("备份文件夹创建失败");
}
if( !file_exists($this->tempdir) && !mkdir($this->tempdir, 0777, true) ) {
return $this->log("临时文件夹创建失败");
}
?>
<h2>升级Typecho</h2>
<div id="progress"></div>
Expand Down Expand Up @@ -83,9 +97,7 @@ function ajax(url, callback) {
//第一步先备份
public function first() {
include "pclzip.lib.php";
$backdir = $this->_dir."/backup";
$this->clean($backdir);
$backname = "$backdir/Backup".date("YmdHis").".zip";
$backname = "$this->backdir/Backup".date("YmdHis").".zip";
$zip = new pclZip($backname);
$res = $zip->create(__TYPECHO_ROOT_DIR__,
PCLZIP_OPT_REMOVE_PATH, __TYPECHO_ROOT_DIR__);
Expand All @@ -94,7 +106,7 @@ public function first() {
}
//第二步下载新版本
public function second() {
$temp = $this->_dir."/temp/".basename(self::lastestUrl);
$temp = $this->tempdir."/".basename(self::lastestUrl);
$source = fopen(self::lastestUrl, "rb");
if($source) $target = fopen($temp, "wb");
if($target) {
Expand All @@ -110,7 +122,7 @@ public function second() {
//第三步解压新版本
public function third() {
include "pclzip.lib.php";
$file = $this->_dir."/temp/master.zip";
$file = $this->tempdir."/master.zip";
$dir = dirname($file);
$zip = new PclZip($file);
if( !$zip->extract(PCLZIP_OPT_PATH, $dir) === 0 ) {
Expand All @@ -120,55 +132,69 @@ public function third() {
}
//第四步更新
public function fourth() {
$lastestDir = $this->_dir."/temp/typecho-master";
$lastestDir = $this->tempdir."/typecho-master";
$overWrite = array(
"admin"=>__TYPECHO_ROOT_DIR__.__TYPECHO_ADMIN_DIR__,
"var" => __TYPECHO_ROOT_DIR__."/var",
"index.php" => __TYPECHO_ROOT_DIR__."/index.php"
"index.php" => __TYPECHO_ROOT_DIR__."/index.php",
"install.php" => __TYPECHO_ROOT_DIR__."/install.php"
);
foreach( $overWrite as $name => $to ) {
$from = "$lastestDir/$name";
if( is_dir($from) ) $this->copy($from, $to);
if( is_dir($from) ) {
$this->copy($from, $to);
}else{
if( !copy($from, $to) ) {
$error = error_get_last();
return $this->log("更新 $to 文件发生错误,错误类型 {$error['type']} ,错误信息:{$error['message']}");
}
}
}
}
//第五步清空临时文件
public function fifth() {
$this->clean($this->_dir."/temp", true);
$this->clean($this->tempdir);
}
//第六步终章提示升级完毕进入后台
public function sixth() {
}

private function clean($d, $r = false) {
foreach(glob("$d/*") as $f) {
if( is_dir($f) ) $this->clean($f, $r);
else if(!unlink($f)) return $this->log("删除文件 $f 错误");
private function clean($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
if(is_dir("$dir/$file")) {
$this->clean("$dir/$file");
}elseif(!unlink("$dir/$file")) {
return $this->log("删除文件 $dir/$file 错误");
}
}
foreach(glob("$d/*") as $f)
is_dir($f) ? $this->clean($f, $r) : unlink($f);
if($r) @rmdir($d);
return rmdir($dir);
}
private function copy($from, $to) {
foreach(glob("$from/*") as $item) {
foreach( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($from)) as $filename ){
if (!is_dir($filename)) {
$items[] = $filename;
}
}
foreach($items as $item) {
$tar = str_replace($from, $to, $item);
$tar_dir = dirname($tar);
if( !file_exists($tar_dir) && !mkdir($tar_dir, 0777, true) ) {
return $this->log("$tar_dir 不存在并创建失败");
return $this->log("$tar_dir 文件夹创建失败");
}
if( !copy($item, $tar) ) {
$error = error_get_last();
return $this->log("更新 $tar 文件发生错误,错误类型 {$error['type']} ,错误信息:{$error['message']}");
}
if( !is_dir($item) ) {
if( !copy($item, $tar) ) {
$error = error_get_last();
return $this->log("更新 $tar 文件发生错误,错误类型 {$error['type']} ,错误信息:{$error['message']}");
}
} else $this->copy_dir($item, $tar);
}
}
private function log($text) {
$text = date("Y-m-d h:i:s")." $text\r\n";
error_log($text, 3, $this->_dir."/error.log");
error_log($text, 3, $this->_dir."error.log");
echo $text;
}
public function action() {
$this->security->protect();
$this->on($this->request);
}
}
Expand Down
28 changes: 16 additions & 12 deletions Update/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Typecho 后台自动升级
* Typecho后台一键升级开发版
*
* @package Update
* @author 公子
* @version 0.0.2
* @link http://zh.eming.li#update
* @version 0.0.3
* @link https://imnerd.org
*/
class Update_Plugin implements Typecho_Plugin_Interface
{
Expand Down Expand Up @@ -55,19 +55,23 @@ public static function config(Typecho_Widget_Helper_Form $form)
public static function personalConfig(Typecho_Widget_Helper_Form $form){}

public static function show() {
$curl = curl_init(Helper::options()->index."/action/ajax?do=checkVersion");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$version = json_decode(curl_exec($curl), true);
curl_close($curl);
$json = __TYPECHO_ROOT_DIR__.__TYPECHO_PLUGIN_DIR__.'/Update/latest.version';
if( !file_exists($json) || (time()-filemtime($json)) > 48 * 3600 ) {
$commonfile = file_get_contents('https://raw.githubusercontent.com/typecho/typecho/master/var/Typecho/Common.php');
preg_match('/const VERSION = \'\s*\K[\d\.]+?\/(\s*\K[\d\.]+?)\';/', $commonfile, $latest);
file_put_contents($json, $latest[1]);
}else{
$latest[1] = file_get_contents($json);
}
$version = explode('/', Helper::options()->version);

if(!$version['avaliable']) {
$url = Helper::options()->index."/update/zero";
if( $latest[1] > $version[1] ) {
$url = Helper::security()->getIndex('/update/zero');
echo '<a href="'.$url.'"><span class="message btn-warn">升级到开发版</span></a>';
?>
<script>
window.onload = function() {
document.querySelector(".update-check strong") && (document.querySelector(".update-check strong").innerHTML += '<a href="<?php echo $url; ?>" class="update message error" style="margin-left:15px;">升级到新版!</a>');
document.querySelector(".update-check strong") && (document.querySelector(".update-check strong").innerHTML += '<a href="<?php echo $url; ?>" class="update message error" style="margin-left:15px;">升级到开发版</a>');
}
</script>
<style>
Expand Down
11 changes: 8 additions & 3 deletions Update/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
### 自动升级开发版插件Update v0.0.2
### 一键升级开发版插件Update v0.0.3

在后台提示版本更新时附加自动安装按钮,可一键执行备份原文件到backup子目录、下载实时开发版并解压升级等操作
在导航条右上方显示升级按钮或在后台提示系统升级时附加开发版链接,可一键执行备份文件到backup子目录、下载实时zip包并解压升级等操作

> 补更至v0.0.2,修正路径兼容Typecho1.0。
:warning:开发版可能存在未知bug不建议在正式环境中使用。

- v0.0.3(18-7-24):([@羽中](https://github.com/jzwalk)
修正文件递归扫描方式解决升级无效bug和临时目录残留问题,修正开发版检测方式,增加控制栏按钮,修正pclzip.lib兼容PHP7.0。

- 补更至v0.0.2,修正action路径。

###### 更多详见作者博客:https://imnerd.org/Update-plugin-for-typecho.html
6 changes: 5 additions & 1 deletion Update/pclzip.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class PclZip
// Note that no real action is taken, if the archive does not exist it is not
// created. Use create() for that.
// --------------------------------------------------------------------------------
function PclZip($p_zipname)
function __construct($p_zipname)
{

// ----- Tests the zlib
Expand All @@ -229,6 +229,10 @@ function PclZip($p_zipname)
// ----- Return
return;
}
function PclZip($p_zipname)
{
self::__construct();
}
// --------------------------------------------------------------------------------

// --------------------------------------------------------------------------------
Expand Down

0 comments on commit ed015b7

Please sign in to comment.