From 2b85e2d2fd0698625405dc465c60ce0e9b8075bd Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 8 Feb 2012 11:45:35 +0000 Subject: [PATCH] all the extensions are simple now --- contrib/artists/main.php | 62 ++++++------ contrib/handle_svg/main.php | 43 +++----- contrib/rating/main.php | 180 ++++++++++++++++----------------- contrib/tag_editcloud/main.php | 78 +++++++------- 4 files changed, 169 insertions(+), 194 deletions(-) diff --git a/contrib/artists/main.php b/contrib/artists/main.php index 361502e8..2b8a79fd 100644 --- a/contrib/artists/main.php +++ b/contrib/artists/main.php @@ -19,41 +19,37 @@ class AuthorSetEvent extends Event { } } -class Artists implements Extension { - var $theme; - - public function get_priority() {return 50;} - - public function receive_event(Event $event) - { +class Artists extends SimpleExtension { + public function onImageInfoSet(ImageInfoSetEvent $event) { global $user; - - if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if ($event instanceof ImageInfoSetEvent) - if (isset($_POST["tag_edit__author"])) - send_event(new AuthorSetEvent($event->image, $user, $_POST["tag_edit__author"])); - - if ($event instanceof AuthorSetEvent) - $this->update_author($event); - - if($event instanceof InitExtEvent) - $this->try_install(); - - if ($event instanceof ImageInfoBoxBuildingEvent) - $this->add_author_field_to_image($event); - - if ($event instanceof PageRequestEvent) - $this->handle_commands($event); - - if ($event instanceof SearchTermParseEvent) { - $matches = array(); - if(preg_match("/^author=(.*)$/", $event->term, $matches)) { - $char = $matches[1]; - $event->add_querylet(new Querylet("Author = :author_char", array("author_char"=>$char))); - } + if (isset($_POST["tag_edit__author"])) { + send_event(new AuthorSetEvent($event->image, $user, $_POST["tag_edit__author"])); } - } + } + + public function onAuthorSet(AuthorSetEvent $event) { + $this->update_author($event); + } + + public function onInitExt(InitExtEvent $event) { + $this->try_install(); + } + + public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { + $this->add_author_field_to_image($event); + } + + public function onPageRequest(PageRequestEvent $event) { + $this->handle_commands($event); + } + + public function onSearchTermParse(SearchTermParseEvent $event) { + $matches = array(); + if(preg_match("/^author=(.*)$/", $event->term, $matches)) { + $char = $matches[1]; + $event->add_querylet(new Querylet("Author = :author_char", array("author_char"=>$char))); + } + } public function try_install() { global $config, $database; diff --git a/contrib/handle_svg/main.php b/contrib/handle_svg/main.php index e2d02a3f..c65edf49 100644 --- a/contrib/handle_svg/main.php +++ b/contrib/handle_svg/main.php @@ -5,15 +5,9 @@ * Description: Handle SVG files */ -class SVGFileHandler implements Extension { - var $theme; - - public function get_priority() {return 50;} - - public function receive_event(Event $event) { - if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { +class SVGFileHandler extends SimpleExtension { + public function onDataUpload(DataUploadEvent $event) { + if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { $hash = $event->hash; $ha = substr($hash, 0, 2); if(!move_upload_to_archive($event)) return; @@ -26,33 +20,28 @@ class SVGFileHandler implements Extension { send_event($iae); $event->image_id = $iae->image->id; } + } - if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) { + public function onThumbnailGeneration(ThumbnailGenerationEvent $event) { + global $config; + if($this->supported_ext($event->type)) { $hash = $event->hash; $ha = substr($hash, 0, 2); - global $config; - -// if($config->get_string("thumb_engine") == "convert") { -// $w = $config->get_int("thumb_width"); -// $h = $config->get_int("thumb_height"); -// $q = $config->get_int("thumb_quality"); -// $mem = $config->get_int("thumb_max_memory") / 1024 / 1024; // IM takes memory in MB -// -// exec("convert images/{$ha}/{$hash}[0] -geometry {$w}x{$h} -quality {$q} jpg:thumbs/{$ha}/{$hash}"); -// } -// else { - copy("ext/handle_svg/thumb.jpg", warehouse_path("thumbs", $hash)); -// } + copy("ext/handle_svg/thumb.jpg", warehouse_path("thumbs", $hash)); } + } - if(($event instanceof DisplayingImageEvent) && $this->supported_ext($event->image->ext)) { - global $page; + public function onDisplayingImage(DisplayingImageEvent $event) { + global $page; + if($this->supported_ext($event->image->ext)) { $this->theme->display_image($page, $event->image); } + } - if(($event instanceof PageRequestEvent) && $event->page_matches("get_svg")) { - global $config, $database, $page; + public function onPageRequest(PageRequestEvent $event) { + global $config, $database, $page; + if($event->page_matches("get_svg")) { $id = int_escape($event->get_arg(0)); $image = Image::by_id($id); $hash = $image->hash; diff --git a/contrib/rating/main.php b/contrib/rating/main.php index b246b14e..71fc0cf8 100644 --- a/contrib/rating/main.php +++ b/contrib/rating/main.php @@ -17,20 +17,13 @@ class RatingSetEvent extends Event { } } -class Ratings implements Extension { - var $theme; +class Ratings extends SimpleExtension { + public function onAdminBuilding(AdminBuildingEvent $event) { + $this->theme->display_bulk_rater(); + } - public function get_priority() {return 50;} - - public function receive_event(Event $event) { - global $config, $database, $page, $user; - if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if($event instanceof AdminBuildingEvent) { - $this->theme->display_bulk_rater(); - } - - if(($event instanceof PageRequestEvent) && $event->page_matches("admin/bulk_rate")) { + public function onPageRequest(PageRequestEvent $event) { + if($event->page_matches("admin/bulk_rate")) { global $database, $user, $page; if(!$user->is_admin()) { throw PermissionDeniedException(); @@ -54,95 +47,98 @@ class Ratings implements Extension { $page->set_redirect(make_link("admin")); } } + } - if($event instanceof InitExtEvent) { - if($config->get_int("ext_ratings2_version") < 2) { - $this->install(); - } + public function onInitExt(InitExtEvent $event) { + global $config; - $config->set_default_string("ext_rating_anon_privs", 'squ'); - $config->set_default_string("ext_rating_user_privs", 'sqeu'); - $config->set_default_string("ext_rating_admin_privs", 'sqeu'); + if($config->get_int("ext_ratings2_version") < 2) { + $this->install(); } - if($event instanceof RatingSetEvent) { - if(empty($event->image->rating)){ - $old_rating = ""; - }else{ - $old_rating = $event->image->rating; - } - $this->set_rating($event->image->id, $event->rating, $old_rating); - } + $config->set_default_string("ext_rating_anon_privs", 'squ'); + $config->set_default_string("ext_rating_user_privs", 'sqeu'); + $config->set_default_string("ext_rating_admin_privs", 'sqeu'); + } - if($event instanceof ImageInfoBoxBuildingEvent) { - if($this->can_rate()) { - $event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating), 80); - } + public function onRatingSet(RatingSetEvent $event) { + if(empty($event->image->rating)){ + $old_rating = ""; + }else{ + $old_rating = $event->image->rating; } + $this->set_rating($event->image->id, $event->rating, $old_rating); + } - if($event instanceof ImageInfoSetEvent) { - if($this->can_rate() && isset($_POST["rating"])) { - send_event(new RatingSetEvent($event->image, $user, $_POST['rating'])); - } - } - - if($event instanceof SetupBuildingEvent) { - $privs = array(); - $privs['Safe Only'] = 's'; - $privs['Safe and Unknown'] = 'su'; - $privs['Safe and Questionable'] = 'sq'; - $privs['Safe, Questionable, Unknown'] = 'squ'; - $privs['All'] = 'sqeu'; - - $sb = new SetupBlock("Image Ratings"); - $sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: "); - $sb->add_choice_option("ext_rating_user_privs", $privs, "
Users: "); - $sb->add_choice_option("ext_rating_admin_privs", $privs, "
Admins: "); - $event->panel->add_block($sb); - } - - if($event instanceof ParseLinkTemplateEvent) { - $event->replace('$rating', $this->theme->rating_to_name($event->image->rating)); - } - - if($event instanceof SearchTermParseEvent) { - $matches = array(); - if(is_null($event->term) && $this->no_rating_query($event->context)) { - $set = Ratings::privs_to_sql(Ratings::get_user_privs($user)); - $event->add_querylet(new Querylet("rating IN ($set)")); - } - if(preg_match("/^rating=([sqeu]+)$/", $event->term, $matches)) { - $sqes = $matches[1]; - $arr = array(); - $length = strlen($sqes); - for($i=0; $i<$length; $i++) { - $arr[] = "'" . $sqes[$i] . "'"; - } - $set = join(', ', $arr); - $event->add_querylet(new Querylet("rating IN ($set)")); - } - if(preg_match("/^rating=(safe|questionable|explicit|unknown)$/", strtolower($event->term), $matches)) { - $text = $matches[1]; - $char = $text[0]; - $event->add_querylet(new Querylet("rating = :img_rating", array("img_rating"=>$char))); - } - } - - if($event instanceof DisplayingImageEvent) { - /** - * Deny images upon insufficient permissions. - **/ - global $user, $database, $page; - $user_view_level = Ratings::get_user_privs($user); - $user_view_level = preg_split('//', $user_view_level, -1); - if(!in_array($event->image->rating, $user_view_level)) { - $page->set_mode("redirect"); - $page->set_redirect(make_link("post/list")); - } + public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { + if($this->can_rate()) { + $event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating), 80); } } - public static function get_user_privs($user) { + public function onImageInfoSet(ImageInfoSetEvent $event) { + global $user; + if($this->can_rate() && isset($_POST["rating"])) { + send_event(new RatingSetEvent($event->image, $user, $_POST['rating'])); + } + } + + public function onSetupBuilding(SetupBuildingEvent $event) { + $privs = array(); + $privs['Safe Only'] = 's'; + $privs['Safe and Unknown'] = 'su'; + $privs['Safe and Questionable'] = 'sq'; + $privs['Safe, Questionable, Unknown'] = 'squ'; + $privs['All'] = 'sqeu'; + + $sb = new SetupBlock("Image Ratings"); + $sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: "); + $sb->add_choice_option("ext_rating_user_privs", $privs, "
Users: "); + $sb->add_choice_option("ext_rating_admin_privs", $privs, "
Admins: "); + $event->panel->add_block($sb); + } + + public function onParseLinkTemplate(ParseLinkTemplateEvent $event) { + $event->replace('$rating', $this->theme->rating_to_name($event->image->rating)); + } + + public function onSearchTermParse(SearchTermParseEvent $event) { + $matches = array(); + if(is_null($event->term) && $this->no_rating_query($event->context)) { + $set = Ratings::privs_to_sql(Ratings::get_user_privs($user)); + $event->add_querylet(new Querylet("rating IN ($set)")); + } + if(preg_match("/^rating=([sqeu]+)$/", $event->term, $matches)) { + $sqes = $matches[1]; + $arr = array(); + $length = strlen($sqes); + for($i=0; $i<$length; $i++) { + $arr[] = "'" . $sqes[$i] . "'"; + } + $set = join(', ', $arr); + $event->add_querylet(new Querylet("rating IN ($set)")); + } + if(preg_match("/^rating=(safe|questionable|explicit|unknown)$/", strtolower($event->term), $matches)) { + $text = $matches[1]; + $char = $text[0]; + $event->add_querylet(new Querylet("rating = :img_rating", array("img_rating"=>$char))); + } + } + + public function onDisplayingImage(DisplayingImageEvent $event) { + /** + * Deny images upon insufficient permissions. + **/ + global $user, $page; + $user_view_level = Ratings::get_user_privs($user); + $user_view_level = preg_split('//', $user_view_level, -1); + if(!in_array($event->image->rating, $user_view_level)) { + $page->set_mode("redirect"); + $page->set_redirect(make_link("post/list")); + } + } + + public static function get_user_privs(User $user) { global $config; if($user->is_anonymous()) { $sqes = $config->get_string("ext_rating_anon_privs"); diff --git a/contrib/tag_editcloud/main.php b/contrib/tag_editcloud/main.php index a6b9ecf0..75819299 100644 --- a/contrib/tag_editcloud/main.php +++ b/contrib/tag_editcloud/main.php @@ -12,56 +12,50 @@ * colorize used tags in cloud || always show used tags in front of cloud * theme junk */ -class TagEditCloud implements Extension { - var $theme; +class TagEditCloud extends SimpleExtension { + public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { + global $config; - public function get_priority() {return 50;} - - public function receive_event(Event $event) { - global $config, $database, $page, $user; - //if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if($event instanceof ImageInfoBoxBuildingEvent) { - if(!$config->get_bool("tageditcloud_disable")) { - if($this->can_tag($event->image)) { - if(!$cfg_minusage=$config->get_int("tageditcloud_minusage")) $cfg_minusage=2; - if(!$cfg_defcount=$config->get_int("tageditcloud_defcount")) $cfg_defcount=40; - if(!$cfg_maxcount=$config->get_int("tageditcloud_maxcount")) $cfg_maxcount=4096; - if($config->get_string("tageditcloud_sort") != "p") { - $event->add_part($this->build_tag_map($event->image,$cfg_minusage,false),40); - } else { - $event->add_part($this->build_tag_map($event->image,$cfg_defcount,$cfg_maxcount),40); - } + if(!$config->get_bool("tageditcloud_disable")) { + if($this->can_tag($event->image)) { + if(!$cfg_minusage=$config->get_int("tageditcloud_minusage")) $cfg_minusage=2; + if(!$cfg_defcount=$config->get_int("tageditcloud_defcount")) $cfg_defcount=40; + if(!$cfg_maxcount=$config->get_int("tageditcloud_maxcount")) $cfg_maxcount=4096; + if($config->get_string("tageditcloud_sort") != "p") { + $event->add_part($this->build_tag_map($event->image,$cfg_minusage,false),40); + } else { + $event->add_part($this->build_tag_map($event->image,$cfg_defcount,$cfg_maxcount),40); } } } + } - if($event instanceof InitExtEvent) { - $config->set_default_bool("tageditcloud_disable",false); - $config->set_default_bool("tageditcloud_usedfirst",true); - $config->set_default_string("tageditcloud_sort",'a'); - $config->set_default_int("tageditcloud_minusage",2); - $config->set_default_int("tageditcloud_defcount",40); - $config->set_default_int("tageditcloud_maxcount",4096); - } + public function onInitExt(InitExtEvent $event) { + global $config; + $config->set_default_bool("tageditcloud_disable",false); + $config->set_default_bool("tageditcloud_usedfirst",true); + $config->set_default_string("tageditcloud_sort",'a'); + $config->set_default_int("tageditcloud_minusage",2); + $config->set_default_int("tageditcloud_defcount",40); + $config->set_default_int("tageditcloud_maxcount",4096); + } - if($event instanceof SetupBuildingEvent) { - $sort_by = array('Alphabetical'=>'a','Popularity'=>'p'); + public function onSetupBuilding(SetupBuildingEvent $event) { + $sort_by = array('Alphabetical'=>'a','Popularity'=>'p'); - $sb = new SetupBlock("Tag Edit Cloud"); - $sb->add_bool_option("tageditcloud_disable", "Disable Tag Selection Cloud: "); - $sb->add_choice_option("tageditcloud_sort", $sort_by, "
Sort the tags by:"); - $sb->add_bool_option("tageditcloud_usedfirst","
Always show used tags first: "); - $sb->add_label("
Alpha sort:
Only show tags used at least "); - $sb->add_int_option("tageditcloud_minusage"); - $sb->add_label(" times.
Popularity sort:
Show "); - $sb->add_int_option("tageditcloud_defcount"); - $sb->add_label(" tags by default.
Show a maximum of "); - $sb->add_int_option("tageditcloud_maxcount"); - $sb->add_label(" tags."); + $sb = new SetupBlock("Tag Edit Cloud"); + $sb->add_bool_option("tageditcloud_disable", "Disable Tag Selection Cloud: "); + $sb->add_choice_option("tageditcloud_sort", $sort_by, "
Sort the tags by:"); + $sb->add_bool_option("tageditcloud_usedfirst","
Always show used tags first: "); + $sb->add_label("
Alpha sort:
Only show tags used at least "); + $sb->add_int_option("tageditcloud_minusage"); + $sb->add_label(" times.
Popularity sort:
Show "); + $sb->add_int_option("tageditcloud_defcount"); + $sb->add_label(" tags by default.
Show a maximum of "); + $sb->add_int_option("tageditcloud_maxcount"); + $sb->add_label(" tags."); - $event->panel->add_block($sb); - } + $event->panel->add_block($sb); } private function tag_link($tag) {