move copy & pasted transload code into a shared function
This commit is contained in:
parent
126ed2ee78
commit
1f07a273f6
@ -4,7 +4,7 @@
|
|||||||
* Author: DakuTree <dakutree@codeanimu.net>
|
* Author: DakuTree <dakutree@codeanimu.net>
|
||||||
* Link: http://www.codeanimu.net
|
* Link: http://www.codeanimu.net
|
||||||
* License: GPLv2
|
* License: GPLv2
|
||||||
* Description: Shimmie updater!
|
* Description: Shimmie updater! (Requires php-curl library and admin panel extension)
|
||||||
*/
|
*/
|
||||||
class Update extends Extension {
|
class Update extends Extension {
|
||||||
public function onInitExt(InitExtEvent $event) {
|
public function onInitExt(InitExtEvent $event) {
|
||||||
@ -16,34 +16,30 @@ class Update extends Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onSetupBuilding(SetupBuildingEvent $event) {
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
||||||
|
$sb = new SetupBlock("Update");
|
||||||
|
$sb->add_text_option("update_guser", "User: ");
|
||||||
|
$sb->add_text_option("update_grepo", "<br>Repo: ");
|
||||||
|
$event->panel->add_block($sb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onAdminBuilding(AdminBuildingEvent $event) {
|
||||||
global $config;
|
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"];
|
$commitMessage = $latestCommit["commit"]["message"];
|
||||||
$commitDT = explode("T", $latestCommit["commit"]["committer"]["date"]);
|
$commitDT = explode("T", $latestCommit["commit"]["committer"]["date"]);
|
||||||
$commitTD = explode("-", $commitDT[1]);
|
$commitTD = explode("-", $commitDT[1]);
|
||||||
$commitDateTime = $commitDT[0]." (".$commitTD[0].")";
|
$commitDateTime = $commitDT[0]." (".$commitTD[0].")";
|
||||||
$commitSHA = substr($latestCommit["sha"],0,7);
|
$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.
|
$html .= "".
|
||||||
$sb = new SetupBlock("Update");
|
"Current Commit: ".$config->get_string('commit_hash')." | ".$config->get_string('commit_time').
|
||||||
$sb->position = 75;
|
"<br>Latest Commit: ".$commitSHA." | ".$commitDateTime." | ".$commitMessage.
|
||||||
$sb->add_label("Current Commit: ".$config->get_string('commit_hash')." | ".$config->get_string('commit_time'));
|
"<br><a href='" . make_link('update') . "'>Update</a>".
|
||||||
$sb->add_label("<br>Latest Commit: ".$commitSHA." | ".$commitDateTime." | ".$commitMessage);
|
"";
|
||||||
$sb->add_text_option("update_guser", "<br>User: ");
|
$page->add_block(new Block("Software Update", $html, "main"));
|
||||||
$sb->add_text_option("update_grepo", "Repo: ");
|
|
||||||
$sb->add_label("<br><a href='".make_link('update')."'>Update</a>");
|
|
||||||
$event->panel->add_block($sb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onPageRequest(PageRequestEvent $event) {
|
public function onPageRequest(PageRequestEvent $event) {
|
||||||
@ -53,10 +49,10 @@ class Update extends Extension {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function update_shimmie() {
|
private function get_latest_commit() {
|
||||||
//This is a REALLY ugly function. (Damn my limited PHP knowledge >_<)
|
if(!function_exists("curl_init")) return null;
|
||||||
global $config, $page;
|
|
||||||
$html = "";
|
//Grab latest info via JSON.
|
||||||
$g_user = $config->get_string("update_guser");
|
$g_user = $config->get_string("update_guser");
|
||||||
$g_repo = $config->get_string("update_grepo");
|
$g_repo = $config->get_string("update_grepo");
|
||||||
$base = "https://api.github.com/repos/".$g_user."/".$g_repo."/commits";
|
$base = "https://api.github.com/repos/".$g_user."/".$g_repo."/commits";
|
||||||
@ -67,19 +63,28 @@ class Update extends Extension {
|
|||||||
$content = curl_exec($curl);
|
$content = curl_exec($curl);
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
$commits = json_decode($content, true);
|
$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"]);
|
$commitDT = explode("T", $latestCommit["commit"]["committer"]["date"]);
|
||||||
$commitTD = explode("-", $commitDT[1]);
|
$commitTD = explode("-", $commitDT[1]);
|
||||||
$commitDateTime = $commitDT[0]." (".$commitTD[0].")";
|
$commitDateTime = $commitDT[0]." (".$commitTD[0].")";
|
||||||
$commitSHA = substr($latestCommit["sha"],0,7);
|
$commitSHA = substr($latestCommit["sha"],0,7);
|
||||||
|
|
||||||
|
$html = "";
|
||||||
$url = "http://nodeload.github.com/".$g_user."/".$g_repo."/zipball/".$commitSHA;
|
$url = "http://nodeload.github.com/".$g_user."/".$g_repo."/zipball/".$commitSHA;
|
||||||
$mfile = "master.zip";
|
$mfile = "master.zip";
|
||||||
if(glob("*-shimmie2-".$commitSHA)){ //#3
|
if(glob("*-shimmie2-".$commitSHA)) { //#3
|
||||||
$dir = glob("*-shimmie2-".$commitSHA);
|
$dir = glob("*-shimmie2-".$commitSHA);
|
||||||
preg_match('@^([a-zA-Z0-9]+\-[0-9a-z]+\-)([^/]+)@i', $dir[0], $matches);
|
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];
|
$html .= "commit: ".$matches[2];
|
||||||
$commit = $matches[2];
|
$commit = $matches[2];
|
||||||
mkdir("./backup");
|
mkdir("./backup");
|
||||||
@ -120,10 +125,12 @@ class Update extends Extension {
|
|||||||
$config->set_string("commit_hash", $commit);
|
$config->set_string("commit_hash", $commit);
|
||||||
$config->set_string("commit_time", $commitDateTime);
|
$config->set_string("commit_time", $commitDateTime);
|
||||||
$html .= "<br>new commit_hash has been set!";
|
$html .= "<br>new commit_hash has been set!";
|
||||||
}else{
|
}
|
||||||
|
else {
|
||||||
$html .= "Error! Folder does not exist!?"; //Although this should be impossible, shall have it anyway.
|
$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;
|
$zip = new ZipArchive;
|
||||||
if ($zip->open($mfile) === TRUE) {
|
if ($zip->open($mfile) === TRUE) {
|
||||||
$zip->extractTo('./');
|
$zip->extractTo('./');
|
||||||
@ -131,70 +138,24 @@ class Update extends Extension {
|
|||||||
$html .= "extracted!";
|
$html .= "extracted!";
|
||||||
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> the page to continue!";
|
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> the page to continue!";
|
||||||
unlink($mfile); //Deletes master.zip
|
unlink($mfile); //Deletes master.zip
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
$html .= "failed!";
|
$html .= "failed!";
|
||||||
}
|
}
|
||||||
}else{ //#1
|
}
|
||||||
|
else { //#1
|
||||||
//Taken from the upload ext.
|
//Taken from the upload ext.
|
||||||
if($config->get_string("transload_engine") == "curl" && function_exists("curl_init")) {
|
transload($url, $mfile);
|
||||||
$ch = curl_init($url);
|
|
||||||
$fp = fopen($mfile, "w");
|
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_FILE, $fp);
|
if(file_exists($mfile)) {
|
||||||
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 .= "downloaded!";
|
||||||
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> the page to continue!";
|
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> the page to continue!";
|
||||||
}else{
|
}
|
||||||
|
else {
|
||||||
$html .= "download failed!";
|
$html .= "download failed!";
|
||||||
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> to try again!";
|
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> to try again!";
|
||||||
$html .= "<br>if you keep having this problem, you may have a problem with your transload engine!";
|
$html .= "<br>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 .= "<br><a href='javascript:history.go(0)'>refresh</a> the page to continue!";
|
|
||||||
}else{
|
|
||||||
$html .= "download failed!";
|
|
||||||
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> to try again!";
|
|
||||||
$html .= "<br>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 .= "<br><a href='javascript:history.go(0)'>refresh</a> the page to continue!";
|
|
||||||
}else{
|
|
||||||
$html .= "download failed!";
|
|
||||||
$html .= "<br><a href='javascript:history.go(0)'>refresh</a> to try again!";
|
|
||||||
$html .= "<br>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!";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$page->add_block(new Block("Update", $html));
|
$page->add_block(new Block("Update", $html));
|
||||||
|
@ -666,6 +666,56 @@ function warehouse_path(/*string*/ $base, /*string*/ $hash, /*bool*/ $create=tru
|
|||||||
return $pa;
|
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 *
|
* Logging convenience *
|
||||||
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
@ -359,46 +359,11 @@ class Upload extends Extension {
|
|||||||
$tmp_filename = tempnam("/tmp", "shimmie_transload");
|
$tmp_filename = tempnam("/tmp", "shimmie_transload");
|
||||||
$filename = basename($url);
|
$filename = basename($url);
|
||||||
|
|
||||||
if($config->get_string("transload_engine") == "fopen") {
|
if(!transload($url, $tmp_filename)) {
|
||||||
$fp = @fopen($url, "r");
|
|
||||||
if(!$fp) {
|
|
||||||
$this->theme->display_upload_error($page, "Error with ".html_escape($filename),
|
$this->theme->display_upload_error($page, "Error with ".html_escape($filename),
|
||||||
"Error reading from ".html_escape($url));
|
"Error reading from ".html_escape($url));
|
||||||
return false;
|
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(filesize($tmp_filename) == 0) {
|
if(filesize($tmp_filename) == 0) {
|
||||||
$this->theme->display_upload_error($page, "Error with ".html_escape($filename),
|
$this->theme->display_upload_error($page, "Error with ".html_escape($filename),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user