mirror of
https://github.com/danpros/htmly.git
synced 2026-04-20 20:46:26 +05:30
added AutoUpdate
This commit is contained in:
parent
e5d3739b46
commit
4e1d990b91
5 changed files with 198 additions and 0 deletions
3
cache/installedVersion.json
vendored
Normal file
3
cache/installedVersion.json
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"tag_name": "v2.1"
|
||||||
|
}
|
||||||
5
system/admin/views/updated-to.html.php
Normal file
5
system/admin/views/updated-to.html.php
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
echo $updater->printOne();
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -6,6 +6,7 @@ date_default_timezone_set('Asia/Jakarta');
|
||||||
// Explicitly including the dispatch framework,
|
// Explicitly including the dispatch framework,
|
||||||
// and our functions.php file
|
// and our functions.php file
|
||||||
require 'system/includes/dispatch.php';
|
require 'system/includes/dispatch.php';
|
||||||
|
require 'system/includes/updater.php';
|
||||||
require 'system/includes/functions.php';
|
require 'system/includes/functions.php';
|
||||||
require 'system/admin/admin.php';
|
require 'system/admin/admin.php';
|
||||||
require 'system/includes/session.php';
|
require 'system/includes/session.php';
|
||||||
|
|
@ -1109,6 +1110,26 @@ get('/feed/opml',function(){
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
get('/admin/update/now/:csrf',function($CSRF){
|
||||||
|
|
||||||
|
$proper = is_csrf_proper($CSRF);
|
||||||
|
$updater = new Updater;
|
||||||
|
if( login() && $proper && $updater->updateAble())
|
||||||
|
{
|
||||||
|
$updater->update();
|
||||||
|
config('views.root', 'system/admin/views');
|
||||||
|
render('updated-to', array(
|
||||||
|
'head_contents' => head_contents('Updated - ' . blog_title(), blog_description(), site_url()),
|
||||||
|
'updater' => $updater,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$login = site_url() . 'login';
|
||||||
|
header("location: $login");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// If we get here, it means that
|
// If we get here, it means that
|
||||||
// nothing has been matched above
|
// nothing has been matched above
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1673,6 +1673,10 @@ function toolbar() {
|
||||||
$role = user('role', $user);
|
$role = user('role', $user);
|
||||||
$base = site_url();
|
$base = site_url();
|
||||||
|
|
||||||
|
$CSRF = get_csrf();
|
||||||
|
|
||||||
|
$updater = new Updater;
|
||||||
|
|
||||||
echo <<<EOF
|
echo <<<EOF
|
||||||
<link href="{$base}themes/default/css/toolbar.css" rel="stylesheet" />
|
<link href="{$base}themes/default/css/toolbar.css" rel="stylesheet" />
|
||||||
EOF;
|
EOF;
|
||||||
|
|
@ -1686,6 +1690,10 @@ EOF;
|
||||||
echo '<li><a href="'.$base.'admin/import">Import</a></li>';
|
echo '<li><a href="'.$base.'admin/import">Import</a></li>';
|
||||||
echo '<li><a href="'.$base.'admin/backup">Backup</a></li>';
|
echo '<li><a href="'.$base.'admin/backup">Backup</a></li>';
|
||||||
echo '<li><a href="'.$base.'admin/clear-cache">Clear cache</a></li>';
|
echo '<li><a href="'.$base.'admin/clear-cache">Clear cache</a></li>';
|
||||||
|
if( $updater->updateAble())
|
||||||
|
{
|
||||||
|
echo '<li><a href="'.$base.'admin/update/now/' . $CSRF . '">UpdateMe[' . $updater->getName() . ']</a></li>';
|
||||||
|
}
|
||||||
echo '<li><a href="'.$base.'logout">Logout</a></li>';
|
echo '<li><a href="'.$base.'logout">Logout</a></li>';
|
||||||
|
|
||||||
echo '</ul></div>';
|
echo '</ul></div>';
|
||||||
|
|
|
||||||
161
system/includes/updater.php
Normal file
161
system/includes/updater.php
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class CacheOneFile
|
||||||
|
{
|
||||||
|
protected $fileName = "";
|
||||||
|
protected $holdTime = 43200;//12h
|
||||||
|
|
||||||
|
public function __construct($fileName , $holdTime = 43200)
|
||||||
|
{
|
||||||
|
$this->fileName = $fileName;
|
||||||
|
$this->holdTime = $holdTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function is()
|
||||||
|
{
|
||||||
|
if(! file_exists($this->fileName))
|
||||||
|
return false;
|
||||||
|
if(filemtime($this->fileName) < ( time() - $this->holdTime ) )
|
||||||
|
{
|
||||||
|
unlink($this->fileName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public function get()
|
||||||
|
{
|
||||||
|
return file_get_contents($this->fileName);
|
||||||
|
}
|
||||||
|
public function set($content)
|
||||||
|
{
|
||||||
|
file_put_contents($this->fileName,$content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Updater
|
||||||
|
{
|
||||||
|
protected $cachedInfo = "cache/downloadInfo.json";
|
||||||
|
protected $versionFile = "cache/installedVersion.json";
|
||||||
|
protected $zipFile = "cache/tmpZipFile.zip";
|
||||||
|
|
||||||
|
protected $infos = [];
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
if(! file_exists("cache/"))
|
||||||
|
{
|
||||||
|
mkdir("cache/");
|
||||||
|
}
|
||||||
|
$this->cachedInfo = new CacheOneFile($this->cachedInfo);
|
||||||
|
$this->infos = $this->getInfos();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getInfos()
|
||||||
|
{
|
||||||
|
$path = "https://api.github.com/repos/danpros/htmly/releases";
|
||||||
|
if($this->cachedInfo->is())
|
||||||
|
{
|
||||||
|
$fileContent = $this->cachedInfo->get();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$fileContent = @file_get_contents($path,false, stream_context_create(['http'=>['header'=>"User-Agent: Awesome-Update-My-Self\r\n"]]));
|
||||||
|
if($fileContent == false)
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$json = json_decode($fileContent,true);
|
||||||
|
$fileContent = json_encode($json, JSON_PRETTY_PRINT);
|
||||||
|
$this->cachedInfo->set($fileContent);
|
||||||
|
return $json;
|
||||||
|
}
|
||||||
|
return json_decode($fileContent,true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateAble()
|
||||||
|
{
|
||||||
|
if(empty($this->infos))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(file_exists($this->versionFile))
|
||||||
|
{
|
||||||
|
$fileContent = file_get_contents($this->versionFile);
|
||||||
|
$current = json_decode($fileContent,true);
|
||||||
|
|
||||||
|
if(isset($current['id']) && $current['id'] == $this->infos[0]['id'])
|
||||||
|
return false;
|
||||||
|
if(isset($current['tag_name']) && $current['tag_name'] == $this->infos[0]['tag_name'])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update()
|
||||||
|
{
|
||||||
|
if($this->updateAble())
|
||||||
|
{
|
||||||
|
if($this->download("https://github.com/danpros/htmly/archive/" . $this->infos[0]['tag_name'] . ".zip"))
|
||||||
|
{
|
||||||
|
if($this->unZip())
|
||||||
|
{
|
||||||
|
unlink($this->zipFile);
|
||||||
|
file_put_contents($this->versionFile, json_encode([
|
||||||
|
"id" => $this->infos[0]['id'],
|
||||||
|
"tag_name" => $this->infos[0]['tag_name']
|
||||||
|
], JSON_PRETTY_PRINT));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
protected function download($url)
|
||||||
|
{
|
||||||
|
$file = @fopen($url, 'r');
|
||||||
|
if($file == false)
|
||||||
|
return false;
|
||||||
|
file_put_contents($this->zipFile, $file);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
protected function unZip()
|
||||||
|
{
|
||||||
|
$path = $this->zipFile;
|
||||||
|
$zip = new ZipArchive;
|
||||||
|
if ($zip->open($path) === true) {
|
||||||
|
$cutLength = strlen($zip->getNameIndex(0));
|
||||||
|
for($i = 1; $i < $zip->numFiles; $i++) {//iterate throw the Zip
|
||||||
|
$fileName = $zip->getNameIndex($i);
|
||||||
|
if($zip->statIndex($i)["crc"] == 0)
|
||||||
|
{
|
||||||
|
$dirName = substr($fileName,$cutLength);
|
||||||
|
if(! file_exists($dirName))
|
||||||
|
{
|
||||||
|
//mkdir($dirName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//copy("zip://".$path."#".$filename, substr($filename,$cutLength));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$zip->close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function printOne()
|
||||||
|
{
|
||||||
|
$releases = $this->infos;
|
||||||
|
$string = "<h3>Updated to<h3>";
|
||||||
|
$string .= "<h2>[" . $releases[0]['tag_name'] . "] " . $releases[0]['name'] . "</h2>\n";
|
||||||
|
$string .= "<p>" . $releases[0]['body'] . "</p>\n";
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return $this->infos[0]['tag_name'];
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue