2012-01-30 11:23:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2014-02-15 03:44:53 +00:00
|
|
|
* Name: [Beta] Update
|
2012-01-30 11:23:58 +00:00
|
|
|
* Author: DakuTree <dakutree@codeanimu.net>
|
|
|
|
* Link: http://www.codeanimu.net
|
|
|
|
* License: GPLv2
|
2014-02-15 03:44:53 +00:00
|
|
|
* Description: Shimmie updater! (Requires admin panel extension & transload engine (cURL/fopen/Wget))
|
2012-01-30 11:23:58 +00:00
|
|
|
*/
|
2012-02-08 12:07:01 +00:00
|
|
|
class Update extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onInitExt(InitExtEvent $event) {
|
2012-01-30 11:23:58 +00:00
|
|
|
global $config;
|
2014-02-15 03:44:53 +00:00
|
|
|
$config->set_default_string("update_guserrepo", "shish/shimmie2");
|
2012-02-16 07:50:28 +00:00
|
|
|
$config->set_default_string("commit_hash", "unknown");
|
2014-02-15 03:44:53 +00:00
|
|
|
$config->set_default_string("update_time", "01/01/1970");
|
2012-01-30 11:23:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
2012-03-09 21:06:03 +00:00
|
|
|
$sb = new SetupBlock("Update");
|
2014-02-15 03:44:53 +00:00
|
|
|
$sb->add_text_option("update_guserrepo", "User/Repo: ");
|
2012-03-09 21:06:03 +00:00
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event) {
|
2014-02-15 03:44:53 +00:00
|
|
|
global $config;
|
|
|
|
if($config->get_string('transload_engine') !== "none"){
|
|
|
|
$this->theme->display_admin_block();
|
|
|
|
}
|
2012-01-30 14:18:05 +00:00
|
|
|
}
|
2012-01-30 11:23:58 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2014-02-15 03:44:53 +00:00
|
|
|
global $config, $user, $page;
|
|
|
|
if($user->is_admin() && isset($_GET['sha'])){
|
|
|
|
if($event->page_matches("update/download")){
|
|
|
|
$ok = $this->download_shimmie();
|
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
if($ok) $page->set_redirect(make_link("update/update", "sha=".$_GET['sha']));
|
|
|
|
else $page->set_redirect(make_link("admin")); //TODO: Show error?
|
|
|
|
}elseif($event->page_matches("update/update")){
|
|
|
|
$ok = $this->update_shimmie();
|
|
|
|
|
|
|
|
$page->set_mode("redirect");
|
|
|
|
if($ok) $page->set_redirect(make_link("admin")); //TODO: Show success?
|
|
|
|
else $page->set_redirect(make_link("admin")); //TODO: Show error?
|
|
|
|
}
|
2012-01-30 11:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 03:44:53 +00:00
|
|
|
private function download_shimmie() {
|
2012-03-14 20:21:14 +00:00
|
|
|
global $config;
|
|
|
|
|
2014-02-15 03:44:53 +00:00
|
|
|
$commitSHA = $_GET['sha'];
|
|
|
|
$g_userrepo = $config->get_string('update_guserrepo');
|
|
|
|
|
|
|
|
$url = "https://codeload.github.com/".$g_userrepo."/zip/".$commitSHA;
|
|
|
|
$filename = "./data/update_{$commitSHA}.zip";
|
|
|
|
|
|
|
|
log_info("update", "Attempting to download Shimmie commit: ".$commitSHA);
|
|
|
|
if($headers = transload($url, $filename)){
|
|
|
|
if(($headers['Content-Type'] !== "application/zip") || ((int) $headers['Content-Length'] !== filesize($filename))){
|
|
|
|
unlink("./data/update_{$commitSHA}.zip");
|
|
|
|
log_warning("update", "Download failed: not zip / not same size as remote file.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_warning("update", "Download failed to download.");
|
|
|
|
return false;
|
2012-03-09 21:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function update_shimmie() {
|
|
|
|
global $config, $page;
|
|
|
|
|
2014-02-15 03:44:53 +00:00
|
|
|
$commitSHA = $_GET['sha'];
|
|
|
|
$g_userrepo = $config->get_string('update_guserrepo');
|
|
|
|
|
|
|
|
log_info("update", "Download succeeded. Attempting to update Shimmie.");
|
|
|
|
$config->set_bool("in_upgrade", TRUE);
|
|
|
|
$ok = FALSE;
|
|
|
|
|
|
|
|
/** TODO: Backup all folders (except /data, /images, /thumbs) before attempting this?
|
|
|
|
Either that or point to https://github.com/shish/shimmie2/blob/master/README.txt -> Upgrade from 2.3.X **/
|
|
|
|
|
|
|
|
$zip = new ZipArchive;
|
|
|
|
if ($zip->open("./data/update_$commitSHA.zip") === TRUE) {
|
|
|
|
for($i = 1; $i < $zip->numFiles; $i++) {
|
|
|
|
$filename = $zip->getNameIndex($i);
|
|
|
|
|
|
|
|
if(substr($filename, -1) !== "/"){
|
|
|
|
copy("zip://".dirname(dirname(__DIR__)).'/'."./data/update_$commitSHA.zip"."#".$filename, substr($filename, 50));
|
2012-01-30 14:18:05 +00:00
|
|
|
}
|
2012-01-30 11:23:58 +00:00
|
|
|
}
|
2014-02-15 03:44:53 +00:00
|
|
|
$ok = TRUE; //TODO: Do proper checking to see if everything copied properly
|
|
|
|
}else{ log_warning("update", "Update failed to open ZIP."); }
|
2012-01-30 14:18:05 +00:00
|
|
|
|
2014-02-15 03:44:53 +00:00
|
|
|
$zip->close();
|
|
|
|
unlink("./data/update_$commitSHA.zip");
|
|
|
|
$config->set_bool("in_upgrade", FALSE);
|
|
|
|
|
|
|
|
if($ok){
|
|
|
|
$config->set_string("commit_hash", $commitSHA);
|
|
|
|
$config->set_string("update_time", date('d-m-Y'));
|
|
|
|
log_info("update", "Update succeeded?");
|
2012-01-30 11:23:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 03:44:53 +00:00
|
|
|
return $ok;
|
2012-01-30 11:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|