From 50c2271ca6ae41dd1ce9e48ea998d43c7b4dc72c Mon Sep 17 00:00:00 2001 From: Daku Date: Sat, 15 Feb 2014 03:44:53 +0000 Subject: [PATCH 1/7] complete rewrite of the update ext (again) much cleaner than previous version affixed the [beta] tag since this still has a few issues (mainly error checking/displaying etc.) --- ext/update/main.php | 195 ++++++++++++++++--------------------------- ext/update/script.js | 14 ++++ ext/update/theme.php | 14 ++++ 3 files changed, 98 insertions(+), 125 deletions(-) create mode 100644 ext/update/script.js create mode 100644 ext/update/theme.php diff --git a/ext/update/main.php b/ext/update/main.php index 9aad4c2c..6cd29b26 100644 --- a/ext/update/main.php +++ b/ext/update/main.php @@ -1,166 +1,111 @@ * Link: http://www.codeanimu.net * License: GPLv2 - * Description: Shimmie updater! (Requires php-curl library and admin panel extension) + * Description: Shimmie updater! (Requires admin panel extension & transload engine (cURL/fopen/Wget)) */ class Update extends Extension { public function onInitExt(InitExtEvent $event) { global $config; - $config->set_default_string("update_guser", "shish"); - $config->set_default_string("update_grepo", "shimmie2"); + $config->set_default_string("update_guserrepo", "shish/shimmie2"); $config->set_default_string("commit_hash", "unknown"); - $config->set_default_string("commit_time", "unknown"); + $config->set_default_string("update_time", "01/01/1970"); } public function onSetupBuilding(SetupBuildingEvent $event) { $sb = new SetupBlock("Update"); - $sb->add_text_option("update_guser", "User: "); - $sb->add_text_option("update_grepo", "
Repo: "); + $sb->add_text_option("update_guserrepo", "User/Repo: "); $event->panel->add_block($sb); } public function onAdminBuilding(AdminBuildingEvent $event) { - global $config, $page; - - $latestCommit = $this->get_latest_commit(); - if(is_null($latestCommit)) return; - - $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); - - $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")); + global $config; + if($config->get_string('transload_engine') !== "none"){ + $this->theme->display_admin_block(); + } } public function onPageRequest(PageRequestEvent $event) { - global $config, $user; - if($event->page_matches("update") && $user->is_admin()) { - $ok = $this->update_shimmie(); + 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? + } } } - private function get_latest_commit() { + private function download_shimmie() { global $config; - if(!function_exists("curl_init")) return null; + $commitSHA = $_GET['sha']; + $g_userrepo = $config->get_string('update_guserrepo'); - //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"; - $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); - $commits = json_decode($content, true); - return $commits[0]; + $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; } 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; + $commitSHA = $_GET['sha']; + $g_userrepo = $config->get_string('update_guserrepo'); - $commitDT = explode("T", $latestCommit["commit"]["committer"]["date"]); - $commitTD = explode("-", $commitDT[1]); - $commitDateTime = $commitDT[0]." (".$commitTD[0].")"; - $commitSHA = substr($latestCommit["sha"],0,7); + log_info("update", "Download succeeded. Attempting to update Shimmie."); + $config->set_bool("in_upgrade", TRUE); + $ok = FALSE; - $html = ""; - $url = "http://nodeload.github.com/".$g_user."/".$g_repo."/zipball/".$commitSHA; - $mfile = "master.zip"; - 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])) { - $html .= "commit: ".$matches[2]; - $commit = $matches[2]; - mkdir("./backup"); - $html .= "
backup folder created!"; - $d_dir = data_path("cache"); - //This should empty the /data/cache/ folder. - if (is_dir($d_dir)) { - $objects = scandir($d_dir); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (filetype($d_dir."/".$object) == "dir") rmdir($d_dir."/".$object); else unlink($d_dir."/".$object); - } - } - reset($objects); - $html .= "
data folder emptied!"; - } - copy ("./data/config/shimmie.conf.php", "./backup/shimmie.conf.php");//Although this stays the same, will keep backup just incase. - $folders = array("./core", "./lib", "./themes", "./.htaccess", "./doxygen.conf", "./index.php", "./install.php", "./ext", "./contrib"); - foreach($folders as $folder){ - //TODO: Check MD5 of each file, don't rename if same. - rename ($folder, "./backup".substr($folder, 1)); //Move old files to backup - rename ("./".$matches[0].substr($folder, 1), $folder); //Move new files to main - } - $html .= "
old shimmie setup has been moved to /backup/ (excluding images/thumbs)!"; - if (is_dir($matches[0])) { - $objects = scandir($matches[0]); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (filetype($matches[0]."/".$object) == "dir") rmdir($matches[0]."/".$object); else unlink($matches[0]."/".$object); - } - } - reset($objects); - rmdir($matches[0]); - $html .= "
".$matches[0]." deleted!"; - } - $html .= "
shimmie updated (although you may have gotten errors, it should have worked!"; - $html .= "
due to the way shimmie loads extensions, all optional extensions have been disabled"; - $config->set_string("commit_hash", $commit); - $config->set_string("commit_time", $commitDateTime); - $html .= "
new commit_hash has been set!"; - } - 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 .= "
refresh the page to continue!"; - unlink($mfile); //Deletes master.zip - } - else { - $html .= "failed!"; - } - } - else { //#1 - //Taken from the upload ext. - transload($url, $mfile); + /** 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 **/ - 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!"; + $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)); + } } + $ok = TRUE; //TODO: Do proper checking to see if everything copied properly + }else{ log_warning("update", "Update failed to open ZIP."); } + + $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?"); } - $page->add_block(new Block("Update", $html)); + return $ok; } } diff --git a/ext/update/script.js b/ext/update/script.js new file mode 100644 index 00000000..56117653 --- /dev/null +++ b/ext/update/script.js @@ -0,0 +1,14 @@ +$(function() { + if($('#updatecheck').length !== 0){ + $.getJSON('https://api.github.com/repos/shish/shimmie2/commits', function(data){ + var c = data[0]; + $('#updatecheck').html(''+c['sha']+'' + " ("+c['commit']['message']+")"); + + var params = $.param({sha: c['sha'], date: c['commit']['committer']['date']}); + $('#updatelink').attr('href', function(i, val){ return val + "?" + params; }); + $('#updatelink').text("Update"); + }).fail(function(){ + $('#updatecheck').text("Loading failed. (Github down?)"); + }); + } +}); diff --git a/ext/update/theme.php b/ext/update/theme.php new file mode 100644 index 00000000..4823e0c6 --- /dev/null +++ b/ext/update/theme.php @@ -0,0 +1,14 @@ +Current Commit: ".$config->get_string('commit_hash')." | (".$config->get_string('update_time').")". + "
Latest Commit: Loading...". + "
"; + //TODO: Show warning before use. + $page->add_block(new Block("Software Update", $html, "main", 75)); + } +} +?> From 7b8395b341e8660e5d64b522b503ff4e19a36ebb Mon Sep 17 00:00:00 2001 From: Daku Date: Sat, 15 Feb 2014 21:04:26 +0000 Subject: [PATCH 2/7] have pool description use BBcode --- ext/pools/theme.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/pools/theme.php b/ext/pools/theme.php index 444e76b5..4a33bdc2 100644 --- a/ext/pools/theme.php +++ b/ext/pools/theme.php @@ -121,7 +121,9 @@ class PoolsTheme extends Themelet { $this->sidebar_options($page, $pool, $check_all); } } - $page->add_block(new Block(html_escape($pool['title']), html_escape($pool['description']), "main", 10)); + + $bb = new BBCode(); + $page->add_block(new Block(html_escape($pool['title']), $bb->format($pool['description']), "main", 10)); } else { $pool_info = ' From 16bb38f343d7f9817b6a7d4ead8ad9957e106768 Mon Sep 17 00:00:00 2001 From: Daku Date: Sat, 15 Feb 2014 21:26:31 +0000 Subject: [PATCH 3/7] onSearchTermParse regex should be case insensitive --- ext/artists/main.php | 2 +- ext/comment/main.php | 2 +- ext/favorites/main.php | 2 +- ext/index/main.php | 14 +++++++------- ext/notes/main.php | 2 +- ext/numeric_score/main.php | 10 +++++----- ext/pools/main.php | 4 ++-- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/ext/artists/main.php b/ext/artists/main.php index 984b12af..0b1d40b7 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -45,7 +45,7 @@ class Artists extends Extension { public function onSearchTermParse(SearchTermParseEvent $event) { $matches = array(); - if(preg_match("/^author[=|:](.*)$/", $event->term, $matches)) { + if(preg_match("/^author[=|:](.*)$/i", $event->term, $matches)) { $char = $matches[1]; $event->add_querylet(new Querylet("Author = :author_char", array("author_char"=>$char))); } diff --git a/ext/comment/main.php b/ext/comment/main.php index 1c472626..107b2247 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -262,7 +262,7 @@ class CommentList extends Extension { public function onSearchTermParse(SearchTermParseEvent $event) { $matches = array(); - if(preg_match("/^comments([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) { + if(preg_match("/^comments([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $comments = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM comments GROUP BY image_id HAVING count(image_id) $cmp $comments)")); diff --git a/ext/favorites/main.php b/ext/favorites/main.php index 6517377e..21ce0829 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -117,7 +117,7 @@ class Favorites extends Extension { public function onSearchTermParse(SearchTermParseEvent $event) { $matches = array(); - if(preg_match("/^favorites([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) { + if(preg_match("/^favorites([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $favorites = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE favorites $cmp $favorites)")); diff --git a/ext/index/main.php b/ext/index/main.php index 9cf5abfc..f7018512 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -295,12 +295,12 @@ class Index extends Extension { public function onSearchTermParse(SearchTermParseEvent $event) { $matches = array(); // check for tags first as tag based searches are more common. - if(preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) { + if(preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $tags = $matches[2]; $event->add_querylet(new Querylet('images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) '.$cmp.' '.$tags.')')); } - else if(preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/", $event->term, $matches)) { + else if(preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) { $cmp = preg_replace('/^:/', '=', $matches[1]); $args = array("width{$this->stpen}"=>int_escape($matches[2]), "height{$this->stpen}"=>int_escape($matches[3])); $event->add_querylet(new Querylet("width / height $cmp :width{$this->stpen} / :height{$this->stpen}", $args)); @@ -326,28 +326,28 @@ class Index extends Extension { else if(preg_match("/^(source)[=|:](.*)$/i", $event->term, $matches)) { $source = strtolower($matches[2]); - if(preg_match("/^(any|none)$/", $source)){ + if(preg_match("/^(any|none)$/i", $source)){ $not = ($source == "any" ? "NOT" : ""); $event->add_querylet(new Querylet("images.source IS $not NULL")); }else{ $event->add_querylet(new Querylet('images.source LIKE :src', array("src"=>"%$source%"))); } } - else if(preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/", $event->term, $matches)) { + else if(preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $val = $matches[2]; $event->add_querylet(new Querylet("images.posted $cmp :posted{$this->stpen}", array("posted{$this->stpen}"=>$val))); } - else if(preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/", $event->term, $matches)) { + else if(preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $args = array("width{$this->stpen}"=>int_escape($matches[2]), "height{$this->stpen}"=>int_escape($matches[3])); $event->add_querylet(new Querylet("width $cmp :width{$this->stpen} AND height $cmp :height{$this->stpen}", $args)); } - else if(preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) { + else if(preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("width $cmp :width{$this->stpen}", array("width{$this->stpen}"=>int_escape($matches[2])))); } - else if(preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) { + else if(preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("height $cmp :height{$this->stpen}",array("height{$this->stpen}"=>int_escape($matches[2])))); } diff --git a/ext/notes/main.php b/ext/notes/main.php index f29a22e9..ac058c45 100644 --- a/ext/notes/main.php +++ b/ext/notes/main.php @@ -214,7 +214,7 @@ class Notes extends Extension { $notes = int_escape($matches[1]); $event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE note = $notes)")); } - else if(preg_match("/^notes([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)%/", $event->term, $matches)) { + else if(preg_match("/^notes([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)%/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $notes = $matches[2]; $event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)")); diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 2da74577..70c18061 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -217,12 +217,12 @@ class NumericScore extends Extension { public function onSearchTermParse(SearchTermParseEvent $event) { $matches = array(); - if(preg_match("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\d+)$/", $event->term, $matches)) { + if(preg_match("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\d+)$/i", $event->term, $matches)) { $cmp = ltrim($matches[1], ":") ?: "="; $score = $matches[2]; $event->add_querylet(new Querylet("numeric_score $cmp $score")); } - if(preg_match("/^upvoted_by[=|:](.*)$/", $event->term, $matches)) { + else if(preg_match("/^upvoted_by[=|:](.*)$/i", $event->term, $matches)) { $duser = User::by_name($matches[1]); if(is_null($duser)) { throw new SearchTermParseException( @@ -232,7 +232,7 @@ class NumericScore extends Extension { "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)", array("ns_user_id"=>$duser->id))); } - if(preg_match("/^downvoted_by[=|:](.*)$/", $event->term, $matches)) { + else if(preg_match("/^downvoted_by[=|:](.*)$/i", $event->term, $matches)) { $duser = User::by_name($matches[1]); if(is_null($duser)) { throw new SearchTermParseException( @@ -242,13 +242,13 @@ class NumericScore extends Extension { "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", array("ns_user_id"=>$duser->id))); } - if(preg_match("/^upvoted_by_id[=|:](\d+)$/", $event->term, $matches)) { + else if(preg_match("/^upvoted_by_id[=|:](\d+)$/i", $event->term, $matches)) { $iid = int_escape($matches[1]); $event->add_querylet(new Querylet( "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)", array("ns_user_id"=>$iid))); } - if(preg_match("/^downvoted_by_id[=|:](\d+)$/", $event->term, $matches)) { + else if(preg_match("/^downvoted_by_id[=|:](\d+)$/i", $event->term, $matches)) { $iid = int_escape($matches[1]); $event->add_querylet(new Querylet( "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", diff --git a/ext/pools/main.php b/ext/pools/main.php index 6bcd8760..3af892b7 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -295,7 +295,7 @@ class Pools extends Extension { public function onSearchTermParse(SearchTermParseEvent $event) { $matches = array(); - if(preg_match("/^pool[=|:]([0-9]+|any|none)$/", $event->term, $matches)) { + if(preg_match("/^pool[=|:]([0-9]+|any|none)$/i", $event->term, $matches)) { $poolID = $matches[1]; if(preg_match("/^(any|none)$/", $poolID)){ @@ -305,7 +305,7 @@ class Pools extends Extension { $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM pool_images WHERE pool_id = $poolID)")); } } - else if(preg_match("/^pool_by_name[=|:](.*)$/", $event->term, $matches)) { + else if(preg_match("/^pool_by_name[=|:](.*)$/i", $event->term, $matches)) { $poolTitle = str_replace("_", " ", $matches[1]); $pool = $this->get_single_pool_from_title($poolTitle); From eb16ab09edd694e09bc040b6564551e77f0e3afa Mon Sep 17 00:00:00 2001 From: Daku Date: Sat, 15 Feb 2014 21:37:51 +0000 Subject: [PATCH 4/7] order images sorted by score this is solution for issue #17 (Method to sort by top scored images in numeric_score module) --- ext/index/main.php | 1 + ext/numeric_score/main.php | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/ext/index/main.php b/ext/index/main.php index f7018512..255bbcfc 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -109,6 +109,7 @@ *
  • downvoted_by=Username -- search for a user's dislikes *
  • upvoted_by_id=UserID -- search for a user's likes by user ID *
  • downvoted_by_id=UserID -- search for a user's dislikes by user ID + *
  • order=score_(ASC, DESC) -- find all images sorted from by score * *
  • Image Rating *
      diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 70c18061..0c146890 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -254,6 +254,13 @@ class NumericScore extends Extension { "images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)", array("ns_user_id"=>$iid))); } + else if(preg_match("/^order[=|:](numeric_)?(score)[_]?(desc|asc)?$/i", $event->term, $matches)){ + global $order_sql; + $default_order_for_column = "DESC"; + $sort = isset($matches[3]) ? strtoupper($matches[3]) : $default_order_for_column; + $order_sql = "numeric_score $sort"; + $event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag + } } private function install() { From e8607ab6f2b535f4f02e3e5cf4cb318cb1f2ac02 Mon Sep 17 00:00:00 2001 From: Daku Date: Sun, 16 Feb 2014 00:08:11 +0000 Subject: [PATCH 5/7] cleaned up popular_by pages my old code was so messy :< --- ext/numeric_score/main.php | 91 ++++++++++++++----------------------- ext/numeric_score/theme.php | 21 +++++---- 2 files changed, 46 insertions(+), 66 deletions(-) diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 0c146890..a577dad0 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -63,7 +63,7 @@ class NumericScore extends Extension { } die($html); } - if($event->page_matches("numeric_score_vote") && $user->check_auth_token()) { + else if($event->page_matches("numeric_score_vote") && $user->check_auth_token()) { if(!$user->is_anonymous()) { $image_id = int_escape($_POST['image_id']); $char = $_POST['vote']; @@ -76,7 +76,7 @@ class NumericScore extends Extension { $page->set_redirect(make_link("post/view/$image_id")); } } - if($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) { + else if($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) { if($user->can("edit_other_vote")) { $image_id = int_escape($_POST['image_id']); $database->execute( @@ -89,83 +89,62 @@ class NumericScore extends Extension { $page->set_redirect(make_link("post/view/$image_id")); } } - if($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) { + else if($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) { if($user->can("edit_other_vote")) { $this->delete_votes_by(int_escape($_POST['user_id'])); $page->set_mode("redirect"); $page->set_redirect(make_link()); } } - if($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) { - $t_images = $config->get_int("index_images"); + else if($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) { + //FIXME: popular_by isn't linked from anywhere + list($day, $month, $year) = array(date("d"), date("m"), date("Y")); - //TODO: Add Popular_by_week. + if(!empty($_GET['day'])){ + $D = (int) $_GET['day']; + if($D >= 1 && $D <= 31) $day = $D; + } + if(!empty($_GET['month'])){ + $M = (int) $_GET['month']; + if($M >= 1 && $M <= 12) $month = $M; + } + if(!empty($_GET['year'])){ + $Y = (int) $_GET['year']; + if($Y >= 1970 && $Y < 2100) $year = $Y; + } - //year - if(empty($_GET['year'])){ - $year = date("Y"); - }else{ - $year = $_GET['year']; - } - //month - if(empty($_GET['month']) || int_escape($_GET['month']) > 12){ - $month = date("m"); - }else{ - $month = $_GET['month']; - } - //day - if(empty($_GET['day']) || int_escape($_GET['day']) > 31){ - $day = date("d"); - }else{ - $day = $_GET['day']; - } $totaldate = $year."/".$month."/".$day; - $sql = - "SELECT * FROM images - WHERE EXTRACT(YEAR FROM posted) = :year - "; - - $agrs = array("limit" => $t_images, "year" => $year); + $sql = "SELECT id FROM images + WHERE EXTRACT(YEAR FROM posted) = :year + "; + $args = array("limit" => $config->get_int("index_images"), "year" => $year); if($event->page_matches("popular_by_day")){ $sql .= "AND EXTRACT(MONTH FROM posted) = :month - AND EXTRACT(DAY FROM posted) = :day - AND NOT numeric_score=0 - "; - //array_push doesn't seem to like using double arrows - //this requires us to instead create two arrays and merge - $sgra = array("month" => $month, "day" => $day); - $args = array_merge($agrs, $sgra); + AND EXTRACT(DAY FROM posted) = :day"; + $args = array_merge($args, array("month" => $month, "day" => $day)); $dte = array($totaldate, date("F jS, Y", (strtotime($totaldate))), "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m\\&\\d\\a\\y\\=d", "day"); } - if($event->page_matches("popular_by_month")){ - $sql .= - "AND EXTRACT(MONTH FROM posted) = :month - AND NOT numeric_score=0 - "; - $sgra = array("month" => $month); - $args = array_merge($agrs, $sgra); + else if($event->page_matches("popular_by_month")){ + $sql .= "AND EXTRACT(MONTH FROM posted) = :month"; - $title = date("F Y", (strtotime($totaldate))); - $dte = array($totaldate, $title, "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m", "month"); + $args = array_merge($args, array("month" => $month)); + $dte = array($totaldate, date("F Y", (strtotime($totaldate))), "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m", "month"); } - if($event->page_matches("popular_by_year")){ - $sql .= "AND NOT numeric_score=0"; - $dte = array($totaldate, $year, "\y\e\a\\r\=Y", "year"); - $args = $agrs; + else if($event->page_matches("popular_by_year")){ + $dte = array($totaldate, $year, "\\y\\e\\a\\r\=Y", "year"); } - $sql .= " ORDER BY numeric_score DESC LIMIT :limit OFFSET 0"; + $sql .= " AND NOT numeric_score=0 ORDER BY numeric_score DESC LIMIT :limit OFFSET 0"; - //filter images by year/score != 0 > limit to max images on one page > order from highest to lowest score - $result = $database->get_all($sql, $args); + //filter images by score != 0 + date > limit to max images on one page > order from highest to lowest score + $result = $database->get_col($sql, $args); $images = array(); - foreach($result as $singleResult) { - $images[] = Image::by_id($singleResult["id"]); - } + foreach($result as $id) { $images[] = Image::by_id($id); } + $this->theme->view_popular($images, $dte); } } diff --git a/ext/numeric_score/theme.php b/ext/numeric_score/theme.php index e60af00c..e805c929 100644 --- a/ext/numeric_score/theme.php +++ b/ext/numeric_score/theme.php @@ -62,27 +62,28 @@ class NumericScoreTheme extends Themelet { } public function view_popular($images, $dte) { - global $user, $page; + global $page, $config; - $pop_images = ''; + $pop_images = ""; foreach($images as $image) { - $thumb_html = $this->build_thumb_html($image); - $pop_images .= ''. - ''.$thumb_html.''. - ''; + $pop_images .= $this->build_thumb_html($image)."\n"; } $b_dte = make_link("popular_by_".$dte[3]."?".date($dte[2], (strtotime('-1 '.$dte[3], strtotime($dte[0]))))); $f_dte = make_link("popular_by_".$dte[3]."?".date($dte[2], (strtotime('+1 '.$dte[3], strtotime($dte[0]))))); - $html = '

      « '.$dte[1] - .' »' - .'

      -
      '.$pop_images; + $html = "\n". + "
      \n". + "

      \n". + " « {$dte[1]} »\n". + "

      \n". + "
      \n". + "
      \n".$pop_images; $nav_html = "Index"; + $page->set_heading($config->get_string('title')); $page->add_block(new Block("Navigation", $nav_html, "left", 10)); $page->add_block(new Block(null, $html, "main", 30)); } From c065e3814555f9ad7204d0f9ec4f5081bce9ed20 Mon Sep 17 00:00:00 2001 From: Daku Date: Sun, 16 Feb 2014 04:38:58 +0000 Subject: [PATCH 6/7] adds widget/thumb option to random_thumb ext (for #323) this code was suggested in the comments, but never actually added --- ext/random_image/main.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ext/random_image/main.php b/ext/random_image/main.php index ced59d67..0d799855 100644 --- a/ext/random_image/main.php +++ b/ext/random_image/main.php @@ -45,11 +45,18 @@ class RandomImage extends Extension { $page->set_data(file_get_contents($image->get_image_filename())); } } - if($action === "view") { + else if($action === "view") { if(!is_null($image)) { send_event(new DisplayingImageEvent($image, $page)); } } + else if($action === "widget") { + if(!is_null($image)) { + $page->set_mode("data"); + $page->set_type("text/html"); + $page->set_data($this->theme->build_thumb_html($image)); + } + } } } From 1909f9072e4d29faa33cfd4c27372969dfd52f1b Mon Sep 17 00:00:00 2001 From: Daku Date: Fri, 7 Feb 2014 15:51:13 +0000 Subject: [PATCH 7/7] use mt_rand() instead of rand() rand() isn't so random on windows: http://tjl.co/blog/code/followup-php-rand-vs-mt_rand/ mt_rand() is also slightly faster --- core/basethemelet.class.php | 2 +- themes/danbooru/themelet.class.php | 2 +- themes/danbooru2/themelet.class.php | 2 +- themes/futaba/themelet.class.php | 2 +- themes/lite/themelet.class.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/basethemelet.class.php b/core/basethemelet.class.php index 83632d08..f0b4d3ac 100644 --- a/core/basethemelet.class.php +++ b/core/basethemelet.class.php @@ -86,7 +86,7 @@ class BaseThemelet { private function build_paginator($current_page, $total_pages, $base_url, $query) { $next = $current_page + 1; $prev = $current_page - 1; - $rand = rand(1, $total_pages); + $rand = mt_rand(1, $total_pages); $at_start = ($current_page <= 1 || $total_pages <= 1); $at_end = ($current_page >= $total_pages); diff --git a/themes/danbooru/themelet.class.php b/themes/danbooru/themelet.class.php index db9b4116..37108666 100644 --- a/themes/danbooru/themelet.class.php +++ b/themes/danbooru/themelet.class.php @@ -42,7 +42,7 @@ class Themelet extends BaseThemelet { private function build_paginator($current_page, $total_pages, $base_url, $query) { $next = $current_page + 1; $prev = $current_page - 1; - $rand = rand(1, $total_pages); + $rand = mt_rand(1, $total_pages); $at_start = ($current_page <= 3 || $total_pages <= 3); $at_end = ($current_page >= $total_pages -2); diff --git a/themes/danbooru2/themelet.class.php b/themes/danbooru2/themelet.class.php index db9b4116..37108666 100644 --- a/themes/danbooru2/themelet.class.php +++ b/themes/danbooru2/themelet.class.php @@ -42,7 +42,7 @@ class Themelet extends BaseThemelet { private function build_paginator($current_page, $total_pages, $base_url, $query) { $next = $current_page + 1; $prev = $current_page - 1; - $rand = rand(1, $total_pages); + $rand = mt_rand(1, $total_pages); $at_start = ($current_page <= 3 || $total_pages <= 3); $at_end = ($current_page >= $total_pages -2); diff --git a/themes/futaba/themelet.class.php b/themes/futaba/themelet.class.php index 5fc79d3c..74f9fff8 100644 --- a/themes/futaba/themelet.class.php +++ b/themes/futaba/themelet.class.php @@ -50,7 +50,7 @@ class Themelet extends BaseThemelet { private function build_paginator($current_page, $total_pages, $base_url, $query) { $next = $current_page + 1; $prev = $current_page - 1; - $rand = rand(1, $total_pages); + $rand = mt_rand(1, $total_pages); $at_start = ($current_page <= 1 || $total_pages <= 1); $at_end = ($current_page >= $total_pages); diff --git a/themes/lite/themelet.class.php b/themes/lite/themelet.class.php index 8788d9a2..d88d6c78 100644 --- a/themes/lite/themelet.class.php +++ b/themes/lite/themelet.class.php @@ -67,7 +67,7 @@ class Themelet extends BaseThemelet { private function build_paginator($current_page, $total_pages, $base_url, $query) { $next = $current_page + 1; $prev = $current_page - 1; - $rand = rand(1, $total_pages); + $rand = mt_rand(1, $total_pages); $at_start = ($current_page <= 1 || $total_pages <= 1); $at_end = ($current_page >= $total_pages);