From bab893bbdac590b95642defd67d2f8a48e21e5ea Mon Sep 17 00:00:00 2001
From: Daku <dakutree@codeanimu.net>
Date: Mon, 30 Jan 2012 11:23:58 +0000
Subject: [PATCH] really ugly update ext (although it works, the code is a mess
 :x)

---
 contrib/update/main.php | 122 ++++++++++++++++++++++++++++++++++++++++
 core/util.inc.php       |   8 ++-
 2 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 contrib/update/main.php

diff --git a/contrib/update/main.php b/contrib/update/main.php
new file mode 100644
index 00000000..1a655d67
--- /dev/null
+++ b/contrib/update/main.php
@@ -0,0 +1,122 @@
+<?php
+/**
+ * Name: Update
+ * Author: DakuTree <dakutree@codeanimu.net>
+ * Link: http://www.codeanimu.net
+ * License: GPLv2
+ * Description: Update shimmie!
+ */
+class Update extends SimpleExtension {
+	public function onInitExt(Event $event) {
+		global $config;
+		$config->set_default_string("update_url", "http://nodeload.github.com/shish/shimmie2/zipball/master");
+		//TODO: Check current VERSION > use commit hash.
+		$config->set_default_string("commit_hash", "");
+	}
+
+	public function onPageRequest(Event $event) {
+		global $config, $page;
+		if($event->page_matches("update/a")) {
+			if($config->get_string("commit_hash") == ""){
+				$c_commit = "Unknown";
+			}else{
+				$c_commit = $config->get_string("commit_hash");
+			}
+			$tmp_filename = tempnam("/tmp", "shimmie_transload");
+
+			//$u_commit = ""; // Get redirected url > grab hash from new filename.
+
+			//Would prefer to use the admin panel for this.
+			//But since the admin panel is optional...kind of stuck to using this.
+			$html = "".make_form(make_link("update/b"))."
+					Current commit hash: $c_commit
+					<br><input id='updatebutton' type='submit' value='Update'>
+					</form>";
+
+			$page->add_block(new Block("Update", $html));
+		}
+
+		if($event->page_matches("update/b")) {
+			$html = "Updating?";
+			$ok = $this->update_shimmie();
+		}
+	}
+
+	private function update_shimmie() {
+		global $config, $page;
+		//This is a REALLY ugly function. (Damn my limited PHP knowledge >_<)
+		$html = "";
+		$url = $config->get_string("update_url");
+		$mfile = "master.zip";
+		if(glob("*-shimmie2*")){ //#3
+			$dir = glob("*-shimmie2*");
+			preg_match('@^([a-zA-Z0-9]+\-[0-9a-z]+\-)([^/]+)@i', $dir[0], $matches);
+			if(!empty($matches[2])){
+				$html .= "commit: ".$matches[2];
+				$commit = $matches[2];
+				mkdir("./backup");
+				//Must be better way to do this...
+				//FIXME: Somehow get rid of this massive rename list.
+				rename ("./core", "./backup/core");
+				rename ("./".$matches[0]."/core", "./core");
+				rmdir("data");
+				mkdir("data");
+				rename ("./ext", "./backup/ext");
+				rename ("./".$matches[0]."/ext", "./ext");
+				rename ("./lib", "./backup/lib");
+				rename ("./".$matches[0]."/lib", "./lib");
+				rename ("./themes", "./backup/themes");
+				rename ("./".$matches[0]."/themes", "./themes");
+				rename ("./.htaccess", "./backup/.htaccess");
+				rename ("./".$matches[0]."/.htaccess", "./.htaccess");
+				copy ("./config.php", "./backup/config.php");//Although this stays the same, will keep backup just incase.
+				rename ("./doxygen.conf", "./backup/doxygen.conf");
+				rename ("./".$matches[0]."/doxygen.conf", "./doxygen.conf");
+				rename ("./index.php", "./backup/index.php");
+				rename ("./".$matches[0]."/index.php", "./index.php");
+				rename ("./install.php", "./backup/install.php");
+				rename ("./".$matches[0]."/install.php", "./install.php");
+				rename ("./ext", "./backup/ext");
+				rename ("./".$matches[0]."/ext", "./ext");
+				rename ("./contrib", "./backup/contrib");
+				rename ("./".$matches[0]."/contrib", "./contrib");
+				rmdir ("./".$matches[0]);
+				$config->set_string("commit_hash", $commit);
+			}else{
+				$html .= "Error! Folder does not exist!?"; //Although this should be impossible, shall have it anyway.
+			}
+		}elseif (file_exists($mfile)){ //#2
+			$zip = new ZipArchive;
+			if ($zip->open($mfile) === TRUE) {
+				$zip->extractTo('./');
+				$zip->close();
+				$html .= "extracted!";
+				$html .= "<br>refresh the page to continue!";
+				unlink($mfile); //Deletes master.zip
+			} else {
+				$html .= "failed!";
+			}
+		}else{ //#1
+			//TODO: Add other transload_engines!
+			if($config->get_string("transload_engine") == "curl" && function_exists("curl_init")) {
+				$ch = curl_init($url);
+				$fp = fopen($mfile, "w");
+
+				curl_setopt($ch, CURLOPT_FILE, $fp);
+				curl_setopt($ch, CURLOPT_HEADER, 0);
+				curl_setopt($ch, CURLOPT_REFERER, $url);
+				curl_setopt($ch, CURLOPT_USERAGENT, "Shimmie-".VERSION);
+
+				curl_exec($ch);
+				curl_close($ch);
+				fclose($fp);
+				$html .= "downloaded!";
+				$html .= "<br>refresh the page to continue!";
+			}
+		}
+
+		$page->add_block(new Block("Update", $html));
+	}
+}
+
+?>
diff --git a/core/util.inc.php b/core/util.inc.php
index 9b80a8c5..43a53bdb 100644
--- a/core/util.inc.php
+++ b/core/util.inc.php
@@ -845,6 +845,12 @@ function get_debug_info() {
 	else {
 		$i_mem = "???";
 	}
+
+	if($config->get_string("commit_hash") == ""){
+		$commit = "";
+	}else{
+		$commit = " (".$config->get_string("commit_hash").")";
+	}
 	$time = sprintf("%5.2f", microtime(true) - $_load_start);
 	$i_files = count(get_included_files());
 	$hits = $database->cache->get_hits();
@@ -854,7 +860,7 @@ function get_debug_info() {
 	$debug .= "; Used $i_files files and $_execs queries";
 	$debug .= "; Sent $_event_count events";
 	$debug .= "; $hits cache hits and $miss misses";
-	$debug .= "; Shimmie version ". VERSION; // .", SCore Version ". SCORE_VERSION;
+	$debug .= "; Shimmie version ". VERSION . $commit; // .", SCore Version ". SCORE_VERSION;
 
 	return $debug;
 }