From 1f07a273f63fcbb81d3d7a4ba969c55b996fe0d5 Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 9 Mar 2012 21:06:03 +0000 Subject: [PATCH] move copy & pasted transload code into a shared function --- contrib/update/main.php | 141 +++++++++++++++------------------------- core/util.inc.php | 50 ++++++++++++++ ext/upload/main.php | 43 ++---------- 3 files changed, 105 insertions(+), 129 deletions(-) diff --git a/contrib/update/main.php b/contrib/update/main.php index 91ada364..e6a9c8a2 100644 --- a/contrib/update/main.php +++ b/contrib/update/main.php @@ -4,7 +4,7 @@ * Author: DakuTree * Link: http://www.codeanimu.net * License: GPLv2 - * Description: Shimmie updater! + * Description: Shimmie updater! (Requires php-curl library and admin panel extension) */ class Update extends Extension { public function onInitExt(InitExtEvent $event) { @@ -16,34 +16,30 @@ class Update extends Extension { } public function onSetupBuilding(SetupBuildingEvent $event) { + $sb = new SetupBlock("Update"); + $sb->add_text_option("update_guser", "User: "); + $sb->add_text_option("update_grepo", "
Repo: "); + $event->panel->add_block($sb); + } + + public function onAdminBuilding(AdminBuildingEvent $event) { global $config; - //Grab latest info via JSON. - $base = "https://api.github.com/repos/shish/shimmie2/commits"; - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, $base); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - $content = curl_exec($curl); - curl_close($curl); + $latestCommit = $this->get_latest_commit(); + if(is_null($latestCommit)) return; - $commits = json_decode($content, true); - $latestCommit = $commits[0]; $commitMessage = $latestCommit["commit"]["message"]; $commitDT = explode("T", $latestCommit["commit"]["committer"]["date"]); $commitTD = explode("-", $commitDT[1]); $commitDateTime = $commitDT[0]." (".$commitTD[0].")"; $commitSHA = substr($latestCommit["sha"],0,7); - //Would prefer to use the admin panel for this. - //But since the admin panel is optional...kind of stuck to using this. - $sb = new SetupBlock("Update"); - $sb->position = 75; - $sb->add_label("Current Commit: ".$config->get_string('commit_hash')." | ".$config->get_string('commit_time')); - $sb->add_label("
Latest Commit: ".$commitSHA." | ".$commitDateTime." | ".$commitMessage); - $sb->add_text_option("update_guser", "
User: "); - $sb->add_text_option("update_grepo", "Repo: "); - $sb->add_label("
Update"); - $event->panel->add_block($sb); + + $html .= "". + "Current Commit: ".$config->get_string('commit_hash')." | ".$config->get_string('commit_time'). + "
Latest Commit: ".$commitSHA." | ".$commitDateTime." | ".$commitMessage. + "
Update". + ""; + $page->add_block(new Block("Software Update", $html, "main")); } public function onPageRequest(PageRequestEvent $event) { @@ -53,10 +49,10 @@ class Update extends Extension { } } - private function update_shimmie() { - //This is a REALLY ugly function. (Damn my limited PHP knowledge >_<) - global $config, $page; - $html = ""; + private function get_latest_commit() { + if(!function_exists("curl_init")) return null; + + //Grab latest info via JSON. $g_user = $config->get_string("update_guser"); $g_repo = $config->get_string("update_grepo"); $base = "https://api.github.com/repos/".$g_user."/".$g_repo."/commits"; @@ -67,19 +63,28 @@ class Update extends Extension { $content = curl_exec($curl); curl_close($curl); $commits = json_decode($content, true); - $latestCommit = $commits[0]; + return $commits[0]; + } + + private function update_shimmie() { + //This is a REALLY ugly function. (Damn my limited PHP knowledge >_<) + global $config, $page; + + $latestCommit = $this->get_latest_commit(); + if(is_null($latestCommit)) return; + $commitDT = explode("T", $latestCommit["commit"]["committer"]["date"]); $commitTD = explode("-", $commitDT[1]); $commitDateTime = $commitDT[0]." (".$commitTD[0].")"; $commitSHA = substr($latestCommit["sha"],0,7); - + $html = ""; $url = "http://nodeload.github.com/".$g_user."/".$g_repo."/zipball/".$commitSHA; $mfile = "master.zip"; - if(glob("*-shimmie2-".$commitSHA)){ //#3 + if(glob("*-shimmie2-".$commitSHA)) { //#3 $dir = glob("*-shimmie2-".$commitSHA); preg_match('@^([a-zA-Z0-9]+\-[0-9a-z]+\-)([^/]+)@i', $dir[0], $matches); - if(!empty($matches[2])){ + if(!empty($matches[2])) { $html .= "commit: ".$matches[2]; $commit = $matches[2]; mkdir("./backup"); @@ -120,10 +125,12 @@ class Update extends Extension { $config->set_string("commit_hash", $commit); $config->set_string("commit_time", $commitDateTime); $html .= "
new commit_hash has been set!"; - }else{ + } + else { $html .= "Error! Folder does not exist!?"; //Although this should be impossible, shall have it anyway. } - }elseif (file_exists($mfile)){ //#2 + } + elseif (file_exists($mfile)) { //#2 $zip = new ZipArchive; if ($zip->open($mfile) === TRUE) { $zip->extractTo('./'); @@ -131,69 +138,23 @@ class Update extends Extension { $html .= "extracted!"; $html .= "
refresh the page to continue!"; unlink($mfile); //Deletes master.zip - } else { + } + else { $html .= "failed!"; } - }else{ //#1 + } + else { //#1 //Taken from the upload ext. - if($config->get_string("transload_engine") == "curl" && function_exists("curl_init")) { - $ch = curl_init($url); - $fp = fopen($mfile, "w"); + transload($url, $mfile); - 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); - if(file_exists($mfile)){ - $html .= "downloaded!"; - $html .= "
refresh the page to continue!"; - }else{ - $html .= "download failed!"; - $html .= "
refresh to try again!"; - $html .= "
if you keep having this problem, you may have a problem with your transload engine!"; - } - }elseif($config->get_string("transload_engine") == "wget") { - //this doesn't work? - $s_url = escapeshellarg($url); - system("wget $s_url --output-document=$mfile"); - if(file_exists($mfile)){ - $html .= "downloaded!"; - $html .= "
refresh the page to continue!"; - }else{ - $html .= "download failed!"; - $html .= "
refresh to try again!"; - $html .= "
if you keep having this problem, you may have a problem with your transload engine!"; - } - }elseif($config->get_string("transload_engine") == "fopen") { - $fp = @fopen($url, "r"); - if(!$fp) { - return false; - } - $data = ""; - $length = 0; - while(!feof($fp) && $length <= $config->get_int('upload_size')) { - $data .= fread($fp, 8192); - $length = strlen($data); - } - fclose($fp); - - $fp = fopen($mfile, "w"); - fwrite($fp, $data); - fclose($fp); - if(file_exists($mfile)){ - $html .= "downloaded!"; - $html .= "
refresh the page to continue!"; - }else{ - $html .= "download failed!"; - $html .= "
refresh to try again!"; - $html .= "
if you keep having this problem, you may have a problem with your transload engine!"; - } - }elseif($config->get_string("transload_engine") == "none"){ - $html .= "no transload engine set!"; + if(file_exists($mfile)) { + $html .= "downloaded!"; + $html .= "
refresh the page to continue!"; + } + else { + $html .= "download failed!"; + $html .= "
refresh to try again!"; + $html .= "
if you keep having this problem, you may have a problem with your transload engine!"; } } diff --git a/core/util.inc.php b/core/util.inc.php index a873bb1b..fb302bfb 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -666,6 +666,56 @@ function warehouse_path(/*string*/ $base, /*string*/ $hash, /*bool*/ $create=tru return $pa; } +function transload($url, $file) { + global $config; + + 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); + + return true; + } + + if($config->get_string("transload_engine") == "wget") { + $s_url = escapeshellarg($url); + $s_mfile = escapeshellarg($mfile); + system("wget $s_url --output-document=$s_mfile"); + + return file_exists($mfile); + } + + if($config->get_string("transload_engine") == "fopen") { + $fp = @fopen($url, "r"); + if(!$fp) { + return false; + } + $data = ""; + $length = 0; + while(!feof($fp) && $length <= $config->get_int('upload_size')) { + $data .= fread($fp, 8192); + $length = strlen($data); + } + fclose($fp); + + $fp = fopen($mfile, "w"); + fwrite($fp, $data); + fclose($fp); + + return true; + } + + return false; +} + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * Logging convenience * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ diff --git a/ext/upload/main.php b/ext/upload/main.php index 9277826c..9d796aa3 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -359,45 +359,10 @@ class Upload extends Extension { $tmp_filename = tempnam("/tmp", "shimmie_transload"); $filename = basename($url); - if($config->get_string("transload_engine") == "fopen") { - $fp = @fopen($url, "r"); - if(!$fp) { - $this->theme->display_upload_error($page, "Error with ".html_escape($filename), - "Error reading from ".html_escape($url)); - return false; - } - $data = ""; - $length = 0; - while(!feof($fp) && $length <= $config->get_int('upload_size')) { - $data .= fread($fp, 8192); - $length = strlen($data); - } - fclose($fp); - - $fp = fopen($tmp_filename, "w"); - fwrite($fp, $data); - fclose($fp); - } - - if($config->get_string("transload_engine") == "curl" && function_exists("curl_init")) { - $ch = curl_init($url); - $fp = fopen($tmp_filename, "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); - } - - if($config->get_string("transload_engine") == "wget") { - $ua = "Shimmie-".VERSION; - $s_url = escapeshellarg($url); - $s_tmp = escapeshellarg($tmp_filename); - system("wget $s_url --output-document=$s_tmp --user-agent=$ua --referer=$s_url"); + if(!transload($url, $tmp_filename)) { + $this->theme->display_upload_error($page, "Error with ".html_escape($filename), + "Error reading from ".html_escape($url)); + return false; } if(filesize($tmp_filename) == 0) {