2008-05-19 03:30:56 +00:00
|
|
|
<?php
|
2009-08-20 22:37:17 +00:00
|
|
|
/*
|
2008-05-19 03:30:56 +00:00
|
|
|
* Name: Random Image
|
|
|
|
* Author: Shish <webmaster@shishnet.org>
|
2012-02-08 02:52:11 +00:00
|
|
|
* Link: http://code.shishnet.org/shimmie2/
|
2008-05-19 03:30:56 +00:00
|
|
|
* License: GPLv2
|
|
|
|
* Description: Do things with a random image
|
2009-01-16 08:18:41 +00:00
|
|
|
* Documentation:
|
|
|
|
* <b>Viewing a random image</b>
|
|
|
|
* <br>Visit <code>/random_image/view</code>
|
|
|
|
* <p><b>Downloading a random image</b>
|
|
|
|
* <br>Link to <code>/random_image/download</code>. This will give
|
|
|
|
* the raw data for an image (no HTML). This is useful so that you
|
|
|
|
* can set your desktop wallpaper to be the download URL, refreshed
|
|
|
|
* every couple of hours.
|
|
|
|
* <p><b>Getting a random image from a subset</b>
|
|
|
|
* <br>Adding a slash and some search terms will get a random image
|
|
|
|
* from those results. This can be useful if you want a specific size
|
|
|
|
* of random image, or from a category. You could link to
|
2009-08-20 19:51:25 +00:00
|
|
|
* <code>/random_image/download/size=1024x768+cute</code>
|
2008-05-19 03:30:56 +00:00
|
|
|
*/
|
|
|
|
|
2012-02-08 12:07:01 +00:00
|
|
|
class RandomImage extends Extension {
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event) {
|
2009-05-11 14:04:33 +00:00
|
|
|
global $config, $database, $page, $user;
|
2009-07-21 06:36:12 +00:00
|
|
|
if($event->page_matches("random_image")) {
|
2012-02-09 06:01:36 +00:00
|
|
|
var $action;
|
2008-05-19 03:30:56 +00:00
|
|
|
if($event->count_args() == 1) {
|
|
|
|
$action = $event->get_arg(0);
|
|
|
|
$search_terms = array();
|
|
|
|
}
|
|
|
|
else if($event->count_args() == 2) {
|
|
|
|
$action = $event->get_arg(0);
|
|
|
|
$search_terms = explode(' ', $event->get_arg(1));
|
|
|
|
}
|
2010-04-05 08:21:44 +00:00
|
|
|
else {
|
|
|
|
# FIXME: throw exception
|
|
|
|
}
|
2009-05-11 14:04:33 +00:00
|
|
|
$image = Image::by_random($search_terms);
|
2008-05-19 03:30:56 +00:00
|
|
|
|
2012-02-09 06:01:36 +00:00
|
|
|
if($action === "download") {
|
2008-05-19 03:30:56 +00:00
|
|
|
if(!is_null($image)) {
|
2009-05-11 14:04:33 +00:00
|
|
|
$page->set_mode("data");
|
|
|
|
$page->set_type("image/jpeg");
|
|
|
|
$page->set_data(file_get_contents($image->get_image_filename()));
|
2008-05-19 03:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-09 06:01:36 +00:00
|
|
|
if($action === "view") {
|
2008-06-09 11:21:44 +00:00
|
|
|
if(!is_null($image)) {
|
2009-05-11 14:04:33 +00:00
|
|
|
send_event(new DisplayingImageEvent($image, $page));
|
2008-06-09 11:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-19 03:30:56 +00:00
|
|
|
}
|
2009-07-21 06:36:12 +00:00
|
|
|
}
|
2009-01-16 08:18:15 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
2009-07-21 06:36:12 +00:00
|
|
|
$sb = new SetupBlock("Random Image");
|
|
|
|
$sb->add_bool_option("show_random_block", "Show Random Block: ");
|
|
|
|
$event->panel->add_block($sb);
|
|
|
|
}
|
2009-01-16 08:18:15 +00:00
|
|
|
|
2012-02-08 11:24:25 +00:00
|
|
|
public function onPostListBuilding(PostListBuildingEvent $event) {
|
2009-07-21 06:36:12 +00:00
|
|
|
global $config, $page;
|
|
|
|
if($config->get_bool("show_random_block")) {
|
|
|
|
$image = Image::by_random($event->search_terms);
|
|
|
|
if(!is_null($image)) {
|
|
|
|
$this->theme->display_random($page, $image);
|
2009-01-16 08:18:15 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-19 03:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|