Initial commit
Some checks failed
定时更新GitHub源插件 / 自动更新GitHub插件 (push) Has been cancelled

This commit is contained in:
chorblack
2026-03-07 11:19:25 +08:00
commit e75f275ef4
4484 changed files with 645480 additions and 0 deletions

202
Update/Action.php Normal file
View File

@@ -0,0 +1,202 @@
<?php
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->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 = $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>
<script type="text/javascript">
function update() {
var steps = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
var notices = [
'正在备份当前版本',
'正在从<a href="https://github.com/typecho/typecho">Github</a>下载最新版本',
'正在解压缩升级文件',
'正在复制升级最新版本',
'正在清除临时文件',
'Typecho升级成功'
];
function printmsg(text, loading) {
loading = arguments.length > 1 ? loading : true;
text = loading ? text + "<span class='loading'>...</span>" : text;
text = "<p>"+text+"</p>";
document.getElementById("progress").innerHTML += text;
}
function ajax(url, callback) {
var xhr;
if(window.XMLHttpRequest) xhr = new XMLHttpRequest();
else if(window.ActiveXObject) xhr = new ActiveXObject("Microsoft.XMLHTTP");
else return alert("Your browser does not support XMLHTTP.");;
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 ) {
if(xhr.status == 200) callback(xhr.responseText);
else return alert("Network Error.");
}
}
xhr.open("GET", url, true);
xhr.send(null);
}
(function step(s) {
[].slice.call(document.querySelectorAll(".loading")).forEach(function(item){
item.innerHTML='....';
item.className=''
});
//终章结语
if(s==steps.length) {
setTimeout(function() {
location.href="<?php echo $adminUrl; ?>";
}, 2000);
return printmsg("欢迎使用Typecho我们将自动为你跳转到后台。如果没有自动跳转请<a href='<?php echo $adminUrl; ?>'>点击这里</a>。", false);
}
printmsg(notices[s]);
ajax("<?php echo $url; ?>"+steps[s], function(data) {
if( data != "" ) return printmsg(data);
step(s+1);
});
})(0)
setInterval(function() {
l = document.querySelector(".loading");
if(l.innerHTML.length == 4) l.innerHTML = '';
l.innerHTML += '.';
}, 500);
}
update();
</script>
<?php
}
//第一步先备份
public function first() {
include "pclzip.lib.php";
$backname = "$this->backdir/Backup".date("YmdHis").".zip";
$zip = new pclZip($backname);
$res = $zip->create(__TYPECHO_ROOT_DIR__,
PCLZIP_OPT_REMOVE_PATH, __TYPECHO_ROOT_DIR__);
if( $res == 0 ) return $this->log( $zip->errorInfo(true) );
return $backname;
}
//第二步下载新版本
public function second() {
$temp = $this->tempdir."/".basename(self::lastestUrl);
$source = fopen(self::lastestUrl, "rb");
if($source) $target = fopen($temp, "wb");
if($target) {
while(!feof($source)) {
$res = fwrite($target, fread($source, 1024*8), 1024*8);
if(!$res) return $this->log("下载新版本写入本地错误");
}
}
if($source) fclose($source);
if($target) fclose($target);
return $temp;
}
//第三步解压新版本
public function third() {
include "pclzip.lib.php";
$file = $this->tempdir."/master.zip";
$dir = dirname($file);
$zip = new PclZip($file);
if( !$zip->extract(PCLZIP_OPT_PATH, $dir) === 0 ) {
return $this->log( $zip->errorInfo(true) );
}
return $dir;
}
//第四步更新
public function fourth() {
$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",
"install.php" => __TYPECHO_ROOT_DIR__."/install.php"
);
foreach( $overWrite as $name => $to ) {
$from = "$lastestDir/$name";
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->tempdir);
}
//第六步终章提示升级完毕进入后台
public function sixth() {
}
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 错误");
}
}
return rmdir($dir);
}
private function copy($from, $to) {
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 文件夹创建失败");
}
if( !copy($item, $tar) ) {
$error = error_get_last();
return $this->log("更新 $tar 文件发生错误,错误类型 {$error['type']} ,错误信息:{$error['message']}");
}
}
}
private function log($text) {
$text = date("Y-m-d h:i:s")." $text\r\n";
error_log($text, 3, $this->_dir."error.log");
echo $text;
}
public function action() {
$this->security->protect();
$this->on($this->request);
}
}
?>

89
Update/Plugin.php Normal file
View File

@@ -0,0 +1,89 @@
<?php
/**
* Typecho后台一键升级开发版
*
* @package Update
* @author 公子
* @version 0.0.3
* @link https://imnerd.org
*/
class Update_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('admin/menu.php')->navBar = array('Update_Plugin', 'show');
Helper::addRoute('update', '/update/[step]', 'Update_Action', NULL);
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){
Helper::removeRoute('update');
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
public static function show() {
$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( $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>');
}
</script>
<style>
.update-check a.update {
padding:5px 10px;
}
.update-check a.update:hover {
color:#8A1F11!important;
text-decoration:underline;
}
</style>
<?php
}
}
}

12
Update/README.md Normal file
View File

@@ -0,0 +1,12 @@
### 一键升级开发版插件Update v0.0.3
在导航条右上方显示升级按钮或在后台提示系统升级时附加开发版链接可一键执行备份文件到backup子目录、下载实时zip包并解压升级等操作。
: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

1
Update/error.log Normal file
View File

@@ -0,0 +1 @@

5698
Update/pclzip.lib.php Normal file

File diff suppressed because it is too large Load Diff