Example #1
0
 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 PageRequestEvent && $event->page_matches("admin")) {
         if (!$user->is_admin()) {
             $this->theme->display_permission_denied($page);
         } else {
             send_event(new AdminBuildingEvent($page));
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("admin_utils")) {
         if ($user->is_admin() && $user->check_auth_token()) {
             log_info("admin", "Util: {$_POST['action']}");
             set_time_limit(0);
             $redirect = false;
             switch ($_POST['action']) {
                 case 'delete by query':
                     $this->delete_by_query($_POST['query']);
                     $redirect = true;
                     break;
                 case 'lowercase all tags':
                     $this->lowercase_all_tags();
                     $redirect = true;
                     break;
                 case 'recount tag use':
                     $this->recount_tag_use();
                     $redirect = true;
                     break;
                 case 'purge unused tags':
                     $this->purge_unused_tags();
                     $redirect = true;
                     break;
                 case 'convert to innodb':
                     $this->convert_to_innodb();
                     $redirect = true;
                     break;
                 case 'database dump':
                     $this->dbdump($page);
                     break;
             }
             if ($redirect) {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("admin"));
             }
         }
     }
     if ($event instanceof AdminBuildingEvent) {
         $this->theme->display_page($page);
         $this->theme->display_form($page);
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("Board Admin", make_link("admin"));
         }
     }
 }
Example #2
0
 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 PageRequestEvent && $event->page_matches("tag_edit")) {
         if ($event->get_arg(0) == "replace") {
             if ($user->is_admin() && isset($_POST['search']) && isset($_POST['replace'])) {
                 $search = $_POST['search'];
                 $replace = $_POST['replace'];
                 $this->mass_tag_edit($search, $replace);
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("admin"));
             }
         }
     }
     if ($event instanceof ImageInfoSetEvent) {
         if ($this->can_tag()) {
             send_event(new TagSetEvent($event->image, $_POST['tag_edit__tags']));
             if ($this->can_source()) {
                 send_event(new SourceSetEvent($event->image, $_POST['tag_edit__source']));
             }
         } else {
             $this->theme->display_error($page, "Error", "Anonymous tag editing is disabled");
         }
     }
     if ($event instanceof TagSetEvent) {
         $event->image->set_tags($event->tags);
     }
     if ($event instanceof SourceSetEvent) {
         $event->image->set_source($event->source);
     }
     if ($event instanceof ImageDeletionEvent) {
         $event->image->delete_tags_from_image();
     }
     if ($event instanceof AdminBuildingEvent) {
         $this->theme->display_mass_editor($page);
     }
     // When an alias is added, oldtag becomes inaccessable
     if ($event instanceof AddAliasEvent) {
         $this->mass_tag_edit($event->oldtag, $event->newtag);
     }
     if ($event instanceof ImageInfoBoxBuildingEvent) {
         if ($config->get_bool("tag_edit_anon") || !$user->is_anonymous()) {
             $event->add_part($this->theme->get_tag_editor_html($event->image), 40);
         }
         if ($config->get_bool("source_edit_anon") || !$user->is_anonymous()) {
             $event->add_part($this->theme->get_source_editor_html($event->image), 41);
         }
     }
     if ($event instanceof SetupBuildingEvent) {
         $sb = new SetupBlock("Tag Editing");
         $sb->add_bool_option("tag_edit_anon", "Allow anonymous tag editing: ");
         $sb->add_bool_option("source_edit_anon", "<br>Allow anonymous source editing: ");
         $event->panel->add_block($sb);
     }
 }
Example #3
0
 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 InitExtEvent) {
         if ($config->get_int("ext_ipban_version") < 5) {
             $this->install();
         }
         $this->check_ip_ban();
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("ip_ban")) {
         if ($user->is_admin()) {
             if ($event->get_arg(0) == "add" && $user->check_auth_token()) {
                 if (isset($_POST['ip']) && isset($_POST['reason']) && isset($_POST['end'])) {
                     if (empty($_POST['end'])) {
                         $end = null;
                     } else {
                         $end = $_POST['end'];
                     }
                     send_event(new AddIPBanEvent($_POST['ip'], $_POST['reason'], $end));
                     $page->set_mode("redirect");
                     $page->set_redirect(make_link("ip_ban/list"));
                 }
             } else {
                 if ($event->get_arg(0) == "remove" && $user->check_auth_token()) {
                     if (isset($_POST['id'])) {
                         send_event(new RemoveIPBanEvent($_POST['id']));
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("ip_ban/list"));
                     }
                 } else {
                     if ($event->get_arg(0) == "list") {
                         $bans = isset($_GET["all"]) ? $this->get_bans() : $this->get_active_bans();
                         $this->theme->display_bans($page, $bans);
                     }
                 }
             }
         } else {
             $this->theme->display_permission_denied($page);
         }
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("IP Bans", make_link("ip_ban/list"));
         }
     }
     if ($event instanceof AddIPBanEvent) {
         $this->add_ip_ban($event->ip, $event->reason, $event->end, $user);
     }
     if ($event instanceof RemoveIPBanEvent) {
         $database->Execute("DELETE FROM bans WHERE id = :id", array("id" => $event->id));
         $database->cache->delete("ip_bans");
     }
 }
Example #4
0
 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 InitExtEvent) {
         $config->set_default_int("history_limit", -1);
         // shimmie is being installed so call install to create the table.
         if ($config->get_int("ext_tag_history_version") < 3) {
             $this->install();
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("tag_history")) {
         if ($event->get_arg(0) == "revert") {
             // this is a request to revert to a previous version of the tags
             if ($config->get_bool("tag_edit_anon") || !$user->is_anonymous()) {
                 $this->process_revert_request($_POST['revert']);
             }
         } else {
             if ($event->count_args() == 1) {
                 // must be an attempt to view a tag history
                 $image_id = int_escape($event->get_arg(0));
                 $this->theme->display_history_page($page, $image_id, $this->get_tag_history_from_id($image_id));
             } else {
                 $this->theme->display_global_page($page, $this->get_global_tag_history());
             }
         }
     }
     if ($event instanceof DisplayingImageEvent) {
         // handle displaying a link on the view page
         $this->theme->display_history_link($page, $event->image->id);
     }
     if ($event instanceof ImageDeletionEvent) {
         // handle removing of history when an image is deleted
         $this->delete_all_tag_history($event->image->id);
     }
     if ($event instanceof SetupBuildingEvent) {
         $sb = new SetupBlock("Tag History");
         $sb->add_label("Limit to ");
         $sb->add_int_option("history_limit");
         $sb->add_label(" entires per image");
         $sb->add_label("<br>(-1 for unlimited)");
         $event->panel->add_block($sb);
     }
     if ($event instanceof TagSetEvent) {
         $this->add_tag_history($event->image, $event->tags);
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("Tag Changes", make_link("tag_history"));
         }
     }
 }
Example #5
0
 public function receive_event(Event $event)
 {
     // Check if someone is accessing /api/danbooru (us)
     if ($event instanceof PageRequestEvent && $event->page_matches("api") && $event->get_arg(0) == 'danbooru') {
         // execute the danbooru processing code
         $this->api_danbooru($event);
     }
     if ($event instanceof SearchTermParseEvent) {
         $matches = array();
         if (preg_match("/^md5:([0-9a-fA-F]*)\$/i", $event->term, $matches)) {
             $hash = strtolower($matches[1]);
             $event->set_querylet(new Querylet("images.hash = '{$hash}'"));
         }
     }
 }
Example #6
0
 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 PageRequestEvent && $event->page_matches("system_info")) {
         if ($user->is_admin()) {
             $this->theme->display_info_page($page, $this->get_info());
         }
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("System Info", make_link("system_info"));
         }
     }
 }
Example #7
0
 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 PageRequestEvent && $event->page_matches("bookmark")) {
         if ($event->get_arg(0) == "add") {
             if (isset($_POST['url'])) {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("user"));
             }
         } else {
             if ($event->get_arg(0) == "remove") {
                 if (isset($_POST['id'])) {
                     $page->set_mode("redirect");
                     $page->set_redirect(make_link("user"));
                 }
             }
         }
     }
 }
Example #8
0
 public function receive_event(Event $event)
 {
     if ($event instanceof PageRequestEvent && $event->page_matches("tagger/tags")) {
         global $page;
         //$match_tags = null;
         //$image_tags = null;
         $tags = null;
         if (isset($_GET['s'])) {
             // tagger/tags[/...]?s=$string
             // return matching tags in XML form
             $tags = $this->match_tag_list($_GET['s']);
         } else {
             if ($event->get_arg(0)) {
                 // tagger/tags/$int
                 // return arg[1] AS image_id's tag list in XML form
                 $tags = $this->image_tag_list($event->get_arg(0));
             }
         }
         $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . "<tags>" . $tags . "</tags>";
         $page->set_mode("data");
         $page->set_type("text/xml");
         $page->set_data($xml);
     }
 }
Example #9
0
 public function receive_event(Event $event)
 {
     global $config, $database, $page, $user;
     if (is_null($this->theme)) {
         $this->theme = get_theme_object($this);
     }
     // f*****g PHP "security" measures -_-;;;
     $free_num = @disk_free_space(realpath("./images/"));
     if ($free_num === FALSE) {
         $is_full = false;
     } else {
         $is_full = $free_num < 100 * 1024 * 1024;
     }
     if ($event instanceof InitExtEvent) {
         $config->set_default_int('upload_count', 3);
         $config->set_default_int('upload_size', '1MB');
         $config->set_default_bool('upload_anon', false);
         $config->set_default_bool('upload_replace', true);
     }
     if ($event instanceof PostListBuildingEvent) {
         if ($this->can_upload($user)) {
             if ($is_full) {
                 $this->theme->display_full($page);
             } else {
                 $this->theme->display_block($page);
             }
         }
     }
     if ($event instanceof PageRequestEvent) {
         if ($event->page_matches("upload/replace")) {
             /* Upload & Replace Image Request */
             if (!$config->get_bool("upload_replace")) {
                 throw new UploadException("Upload Replacing Images is not enabled.");
             }
             // check if the user is an administrator and can upload files.
             if (!$user->is_admin()) {
                 $this->theme->display_permission_denied($page);
             } else {
                 if ($is_full) {
                     throw new UploadException("Can not replace Image: disk nearly full");
                 }
                 // Try to get the image ID
                 $image_id = int_escape($event->get_arg(0));
                 if (empty($image_id)) {
                     $image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
                 }
                 if (empty($image_id)) {
                     throw new UploadException("Can not replace Image: No valid Image ID given.");
                 }
                 $image_old = Image::by_id($image_id);
                 if (is_null($image_old)) {
                     $this->theme->display_error($page, "Image not found", "No image in the database has the ID #{$image_id}");
                 }
                 if (count($_FILES) + count($_POST) > 0) {
                     if (count($_FILES) > 1) {
                         throw new UploadException("Can not upload more than one image for replacing.");
                     }
                     $source = isset($_POST['source']) ? $_POST['source'] : null;
                     $tags = '';
                     // Tags aren't changed when uploading. Set to null to stop PHP warnings.
                     if (count($_FILES)) {
                         foreach ($_FILES as $file) {
                             $ok = $this->try_upload($file, $tags, $source, $image_id);
                             break;
                             // leave the foreach loop.
                         }
                     } else {
                         foreach ($_POST as $name => $value) {
                             if (substr($name, 0, 3) == "url" && strlen($value) > 0) {
                                 $ok = $this->try_transload($value, $tags, $source, $image_id);
                                 break;
                                 // leave the foreach loop.
                             }
                         }
                     }
                     $this->theme->display_upload_status($page, $ok);
                 } else {
                     if (!empty($_GET['url'])) {
                         $url = $_GET['url'];
                         $ok = $this->try_transload($url, $tags, $url, $image_id);
                         $this->theme->display_upload_status($page, $ok);
                     } else {
                         $this->theme->display_replace_page($page, $image_id);
                     }
                 }
             }
             // END of if admin / can_upload
         } else {
             if ($event->page_matches("upload")) {
                 if (!$this->can_upload($user)) {
                     $this->theme->display_permission_denied($page);
                 } else {
                     /* Regular Upload Image */
                     if (count($_FILES) + count($_POST) > 0) {
                         $tags = Tag::explode($_POST['tags']);
                         $source = isset($_POST['source']) ? $_POST['source'] : null;
                         $ok = true;
                         foreach ($_FILES as $file) {
                             $ok = $ok & $this->try_upload($file, $tags, $source);
                         }
                         foreach ($_POST as $name => $value) {
                             if (substr($name, 0, 3) == "url" && strlen($value) > 0) {
                                 $ok = $ok & $this->try_transload($value, $tags, $source);
                             }
                         }
                         $this->theme->display_upload_status($page, $ok);
                     } else {
                         if (!empty($_GET['url'])) {
                             $url = $_GET['url'];
                             $tags = array('tagme');
                             if (!empty($_GET['tags']) && $_GET['tags'] != "null") {
                                 $tags = Tag::explode($_GET['tags']);
                             }
                             $ok = $this->try_transload($url, $tags, $url);
                             $this->theme->display_upload_status($page, $ok);
                         } else {
                             if (!$is_full) {
                                 $this->theme->display_page($page);
                             }
                         }
                     }
                 }
                 // END of if  can_upload
             }
         }
     }
     // END of if PageRequestEvent
     if ($event instanceof SetupBuildingEvent) {
         $tes = array();
         $tes["Disabled"] = "none";
         if (function_exists("curl_init")) {
             $tes["cURL"] = "curl";
         }
         $tes["fopen"] = "fopen";
         $tes["WGet"] = "wget";
         $sb = new SetupBlock("Upload");
         $sb->position = 10;
         // Output the limits from PHP so the user has an idea of what they can set.
         $sb->add_label("<i>PHP's Upload Limit = " . ini_get('max_file_uploads') . "</i><br/>");
         $sb->add_int_option("upload_count", "Max uploads: ");
         $sb->add_label("<br/><i>PHP's Max Size Upload = " . ini_get('upload_max_filesize') . "</i><br/>");
         $sb->add_shorthand_int_option("upload_size", "<br/>Max size per file: ");
         $sb->add_bool_option("upload_anon", "<br/>Allow anonymous uploads: ");
         $sb->add_bool_option("upload_replace", "<br/>Allow replacing images: ");
         $sb->add_choice_option("transload_engine", $tes, "<br/>Transload: ");
         $event->panel->add_block($sb);
     }
     if ($event instanceof DataUploadEvent) {
         if ($is_full) {
             throw new UploadException("Upload failed; disk nearly full");
         }
         if (filesize($event->tmpname) > $config->get_int('upload_size')) {
             $size = to_shorthand_int(filesize($event->tmpname));
             $limit = to_shorthand_int($config->get_int('upload_size'));
             throw new UploadException("File too large ({$size} &gt; {$limit})");
         }
     }
 }
Example #10
0
 public function receive_event(Event $event)
 {
     global $page;
     global $config;
     if ($event instanceof InitExtEvent) {
         $config->set_default_string("search_suggestions_results_order", 'a');
     }
     // Add in header code to let the browser know that the search plugin exists
     if ($event instanceof PageRequestEvent) {
         // We need to build the data for the header
         global $config;
         $search_title = $config->get_string('title');
         $search_file_url = make_link('browser_search/please_dont_use_this_tag_as_it_would_break_stuff__search.xml');
         $page->add_html_header("<link rel='search' type='application/opensearchdescription+xml' title='{$search_title}' href='{$search_file_url}'>");
     }
     // The search.xml file that is generated on the fly
     if ($event instanceof PageRequestEvent && $event->page_matches("browser_search/please_dont_use_this_tag_as_it_would_break_stuff__search.xml")) {
         // First, we need to build all the variables we'll need
         $search_title = $config->get_string('title');
         //$search_form_url =  $config->get_string('base_href'); //make_link('post/list');
         $search_form_url = make_link('post/list/{searchTerms}');
         $suggenton_url = make_link('browser_search/') . "{searchTerms}";
         $icon_b64 = base64_encode(file_get_contents("favicon.ico"));
         // Now for the XML
         $xml = "\n\t\t\t\t<SearchPlugin xmlns='http://www.mozilla.org/2006/browser/search/' xmlns:os='http://a9.com/-/spec/opensearch/1.1/'>\n\t\t\t\t<os:ShortName>{$search_title}</os:ShortName>\n\t\t\t\t<os:InputEncoding>UTF-8</os:InputEncoding>\n\t\t\t\t<os:Image width='16' height='16'>data:image/x-icon;base64,{$icon_b64}</os:Image>\n\t\t\t\t<SearchForm>{$search_form_url}</SearchForm>\n\t\t\t\t<os:Url type='text/html' method='GET' template='{$search_form_url}'>\n\t\t\t\t  <os:Param name='search' value='{searchTerms}'/>\n\t\t\t\t</os:Url>\n\t\t\t\t<Url type='application/x-suggestions+json' template='{$suggenton_url}'/>\n\t\t\t\t</SearchPlugin>\n\t\t\t";
         // And now to send it to the browser
         $page->set_mode("data");
         $page->set_type("text/xml");
         $page->set_data($xml);
     } else {
         if ($event instanceof PageRequestEvent && ($event->page_matches("browser_search") && !$config->get_bool("disable_search_suggestions"))) {
             global $database;
             // We have to build some json stuff
             $tag_search = $event->get_arg(0);
             // Now to get DB results
             if ($config->get_string("search_suggestions_results_order") == "a") {
                 $tags = $database->execute("SELECT tag FROM tags WHERE tag LIKE ? AND count > 0 ORDER BY tag ASC LIMIT 30", array($tag_search . "%"));
             } else {
                 $tags = $database->execute("SELECT tag FROM tags WHERE tag LIKE ? AND count > 0 ORDER BY count DESC LIMIT 30", array($tag_search . "%"));
             }
             // And to do stuff with it. We want our output to look like:
             // ["shimmie",["shimmies","shimmy","shimmie","21 shimmies","hip shimmies","skea shimmies"],[],[]]
             $json_tag_list = "";
             $tags_array = array();
             foreach ($tags as $tag) {
                 array_push($tags_array, $tag['tag']);
             }
             $json_tag_list .= implode("\",\"", $tags_array);
             //			$json_tag_list = implode($tags_array,", ");
             //			$json_tag_list = "\"".implode($tags_array,"\", \"")."\"";
             // And now for the final output
             $json_string = "[\"{$tag_search}\",[\"{$json_tag_list}\"],[],[]]";
             $page->set_mode("data");
             $page->set_data($json_string);
         }
     }
     if ($event instanceof SetupBuildingEvent) {
         $sort_by = array();
         $sort_by['Alphabetical'] = 'a';
         $sort_by['Tag Count'] = 't';
         $sb = new SetupBlock("Browser Search");
         $sb->add_bool_option("disable_search_suggestions", "Disable search suggestions: ");
         $sb->add_label("<br>");
         $sb->add_choice_option("search_suggestions_results_order", $sort_by, "Sort the suggestions by:");
         $event->panel->add_block($sb);
     }
 }
Example #11
0
 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 PageRequestEvent && $event->page_matches("alias")) {
         if ($event->get_arg(0) == "add") {
             if ($user->is_admin()) {
                 if (isset($_POST['oldtag']) && isset($_POST['newtag'])) {
                     try {
                         $aae = new AddAliasEvent($_POST['oldtag'], $_POST['newtag']);
                         send_event($aae);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("alias/list"));
                     } catch (AddAliasException $ex) {
                         $this->theme->display_error($page, "Error adding alias", $ex->getMessage());
                     }
                 }
             }
         } else {
             if ($event->get_arg(0) == "remove") {
                 if ($user->is_admin()) {
                     if (isset($_POST['oldtag'])) {
                         $database->Execute("DELETE FROM aliases WHERE oldtag=?", array($_POST['oldtag']));
                         log_info("alias_editor", "Deleted alias for " . $_POST['oldtag']);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("alias/list"));
                     }
                 }
             } else {
                 if ($event->get_arg(0) == "list") {
                     $page_number = $event->get_arg(1);
                     if (is_null($page_number) || !is_numeric($page_number)) {
                         $page_number = 0;
                     } else {
                         if ($page_number <= 0) {
                             $page_number = 0;
                         } else {
                             $page_number--;
                         }
                     }
                     $alias_per_page = $config->get_int('alias_items_per_page', 30);
                     if ($database->engine->name == "mysql") {
                         $query = "SELECT oldtag, newtag FROM aliases ORDER BY newtag ASC LIMIT ?, ?";
                     } else {
                         $query = "SELECT oldtag, newtag FROM aliases ORDER BY newtag ASC OFFSET ? LIMIT ?";
                     }
                     $alias = $database->db->GetAssoc($query, array($page_number * $alias_per_page, $alias_per_page));
                     $total_pages = ceil($database->db->GetOne("SELECT COUNT(*) FROM aliases") / $alias_per_page);
                     $this->theme->display_aliases($page, $alias, $user->is_admin(), $page_number + 1, $total_pages);
                 } else {
                     if ($event->get_arg(0) == "export") {
                         $page->set_mode("data");
                         $page->set_type("text/plain");
                         $page->set_data($this->get_alias_csv($database));
                     } else {
                         if ($event->get_arg(0) == "import") {
                             if ($user->is_admin()) {
                                 print_r($_FILES);
                                 if (count($_FILES) > 0) {
                                     global $database;
                                     $tmp = $_FILES['alias_file']['tmp_name'];
                                     $contents = file_get_contents($tmp);
                                     $this->add_alias_csv($database, $contents);
                                     $page->set_mode("redirect");
                                     $page->set_redirect(make_link("alias/list"));
                                 } else {
                                     $this->theme->display_error($page, "No File Specified", "You have to upload a file");
                                 }
                             } else {
                                 $this->theme->display_error($page, "Admins Only", "Only admins can edit the alias list");
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($event instanceof AddAliasEvent) {
         global $database;
         $pair = array($event->oldtag, $event->newtag);
         if ($database->db->GetRow("SELECT * FROM aliases WHERE oldtag=? AND lower(newtag)=lower(?)", $pair)) {
             throw new AddAliasException("That alias already exists");
         } else {
             $database->Execute("INSERT INTO aliases(oldtag, newtag) VALUES(?, ?)", $pair);
             log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}");
         }
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("Alias Editor", make_link("alias/list"));
         }
     }
 }
Example #12
0
 public function receive_event(Event $event)
 {
     global $config, $database, $page, $user;
     if ($this->theme == null) {
         $this->theme = get_theme_object($this);
     }
     if ($event instanceof InitExtEvent) {
         $config->set_default_int("tag_list_length", 15);
         $config->set_default_int("popular_tag_list_length", 15);
         $config->set_default_int("tags_min", 3);
         $config->set_default_string("info_link", 'http://en.wikipedia.org/wiki/$tag');
         $config->set_default_string("tag_list_image_type", 'related');
         $config->set_default_bool("tag_list_pages", false);
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("tags")) {
         $this->theme->set_navigation($this->build_navigation());
         switch ($event->get_arg(0)) {
             default:
             case 'map':
                 $this->theme->set_heading("Tag Map");
                 $this->theme->set_tag_list($this->build_tag_map());
                 break;
             case 'alphabetic':
                 $this->theme->set_heading("Alphabetic Tag List");
                 $this->theme->set_tag_list($this->build_tag_alphabetic());
                 break;
             case 'popularity':
                 $this->theme->set_heading("Tag List by Popularity");
                 $this->theme->set_tag_list($this->build_tag_popularity());
                 break;
             case 'categories':
                 $this->theme->set_heading("Popular Categories");
                 $this->theme->set_tag_list($this->build_tag_categories());
                 break;
         }
         $this->theme->display_page($page);
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("api/internal/tag_list/complete")) {
         $all = $database->get_all("SELECT tag FROM tags WHERE tag LIKE :search AND count > 0 LIMIT 10", array("search" => $_GET["s"] . "%"));
         $res = array();
         foreach ($all as $row) {
             $res[] = $row["tag"];
         }
         $page->set_mode("data");
         $page->set_type("text/plain");
         $page->set_data(implode("\n", $res));
     }
     if ($event instanceof PostListBuildingEvent) {
         if ($config->get_int('tag_list_length') > 0) {
             if (!empty($event->search_terms)) {
                 $this->add_refine_block($page, $event->search_terms);
             } else {
                 $this->add_popular_block($page);
             }
         }
     }
     if ($event instanceof DisplayingImageEvent) {
         if ($config->get_int('tag_list_length') > 0) {
             if ($config->get_string('tag_list_image_type') == 'related') {
                 $this->add_related_block($page, $event->image);
             } else {
                 $this->add_tags_block($page, $event->image);
             }
         }
     }
     if ($event instanceof SetupBuildingEvent) {
         $sb = new SetupBlock("Tag Map Options");
         $sb->add_int_option("tags_min", "Only show tags used at least ");
         $sb->add_label(" times");
         $sb->add_bool_option("tag_list_pages", "<br>Paged tag lists: ");
         $event->panel->add_block($sb);
         $sb = new SetupBlock("Popular / Related Tag List");
         $sb->add_int_option("tag_list_length", "Show top ");
         $sb->add_label(" related tags");
         $sb->add_int_option("popular_tag_list_length", "<br>Show top ");
         $sb->add_label(" popular tags");
         $sb->add_text_option("info_link", "<br>Tag info link: ");
         $sb->add_choice_option("tag_list_image_type", array("Image's tags only" => "tags", "Show related" => "related"), "<br>Image tag list: ");
         $sb->add_bool_option("tag_list_numbers", "<br>Show tag counts: ");
         $event->panel->add_block($sb);
     }
 }
Example #13
0
 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 InitExtEvent) {
         if ($config->get_int("ext_numeric_score_version", 0) < 1) {
             $this->install();
         }
     }
     if ($event instanceof DisplayingImageEvent) {
         if (!$user->is_anonymous()) {
             $html = $this->theme->get_voter_html($event->image);
             $page->add_block(new Block("Image Score", $html, "left", 20));
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("numeric_score_vote")) {
         if (!$user->is_anonymous()) {
             $image_id = int_escape($_POST['image_id']);
             $char = $_POST['vote'];
             $score = 0;
             if ($char == "up") {
                 $score = 1;
             } else {
                 if ($char == "down") {
                     $score = -1;
                 }
             }
             if ($score != 0) {
                 send_event(new NumericScoreSetEvent($image_id, $user, $score));
             }
             $page->set_mode("redirect");
             $page->set_redirect(make_link("post/view/{$image_id}"));
         }
     }
     if ($event instanceof NumericScoreSetEvent) {
         $this->add_vote($event->image_id, $user->id, $event->score);
     }
     if ($event instanceof ImageDeletionEvent) {
         $database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($event->image->id));
     }
     if ($event instanceof ParseLinkTemplateEvent) {
         $event->replace('$score', $event->image->numeric_score);
     }
     if ($event instanceof SearchTermParseEvent) {
         $matches = array();
         if (preg_match("/^score(<|<=|=|>=|>)(\\d+)\$/", $event->term, $matches)) {
             $cmp = $matches[1];
             $score = $matches[2];
             $event->add_querylet(new Querylet("numeric_score {$cmp} {$score}"));
         }
         if (preg_match("/^upvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=1)", array($duser->id)));
         }
         if (preg_match("/^downvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=-1)", array($duser->id)));
         }
     }
 }
Example #14
0
 public function receive_event(Event $event)
 {
     global $config, $database, $page, $user;
     if (is_null($this->theme)) {
         $this->theme = get_theme_object($this);
     }
     $is_full = disk_free_space(realpath("./images/")) < 100 * 1024 * 1024;
     if ($event instanceof InitExtEvent) {
         global $config;
         $config->set_default_int('upload_count', 3);
         $config->set_default_int('upload_size', '1MB');
         $config->set_default_bool('upload_anon', false);
     }
     if ($event instanceof PostListBuildingEvent) {
         global $user;
         if ($this->can_upload($user)) {
             if ($is_full) {
                 $this->theme->display_full($page);
             } else {
                 $this->theme->display_block($page);
             }
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("upload")) {
         if (count($_FILES) + count($_POST) > 0) {
             $tags = Tag::explode($_POST['tags']);
             $source = isset($_POST['source']) ? $_POST['source'] : null;
             if ($this->can_upload($user)) {
                 $ok = true;
                 foreach ($_FILES as $file) {
                     $ok = $ok & $this->try_upload($file, $tags, $source);
                 }
                 foreach ($_POST as $name => $value) {
                     if (substr($name, 0, 3) == "url" && strlen($value) > 0) {
                         $ok = $ok & $this->try_transload($value, $tags, $source);
                     }
                 }
                 $this->theme->display_upload_status($page, $ok);
             } else {
                 $this->theme->display_permission_denied($page);
             }
         } else {
             if (!empty($_GET['url'])) {
                 global $user;
                 if ($this->can_upload($user)) {
                     $url = $_GET['url'];
                     $tags = array('tagme');
                     if (!empty($_GET['tags']) && $_GET['tags'] != "null") {
                         $tags = Tag::explode($_GET['tags']);
                     }
                     $ok = $this->try_transload($url, $tags, $url);
                     $this->theme->display_upload_status($page, $ok);
                 } else {
                     $this->theme->display_permission_denied($page);
                 }
             } else {
                 if (!$is_full) {
                     $this->theme->display_page($page);
                 }
             }
         }
     }
     if ($event instanceof SetupBuildingEvent) {
         $sb = new SetupBlock("Upload");
         $sb->position = 10;
         $sb->add_int_option("upload_count", "Max uploads: ");
         $sb->add_shorthand_int_option("upload_size", "<br>Max size per file: ");
         $sb->add_bool_option("upload_anon", "<br>Allow anonymous uploads: ");
         $sb->add_choice_option("transload_engine", array("Disabled" => "none", "cURL" => "curl", "fopen" => "fopen", "WGet" => "wget"), "<br>Transload: ");
         $event->panel->add_block($sb);
     }
     if ($event instanceof DataUploadEvent) {
         global $config;
         if ($is_full) {
             throw new UploadException("Upload failed; disk nearly full");
         }
         if (filesize($event->tmpname) > $config->get_int('upload_size')) {
             $size = to_shorthand_int(filesize($event->tmpname));
             $limit = to_shorthand_int($config->get_int('upload_size'));
             throw new UploadException("File too large ({$size} &gt; {$limit})");
         }
     }
 }
Example #15
0
 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")) {
         global $database, $user, $page;
         if (!$user->is_admin()) {
             throw PermissionDeniedException();
         } else {
             $n = 0;
             while (true) {
                 $images = Image::find_images($n, 100, Tag::explode($_POST["query"]));
                 if (count($images) == 0) {
                     break;
                 }
                 foreach ($images as $image) {
                     send_event(new RatingSetEvent($image, $user, $_POST['rating']));
                 }
                 $n += 100;
             }
             #$database->execute("
             #	update images set rating=? where images.id in (
             #		select image_id from image_tags join tags
             #		on image_tags.tag_id = tags.id where tags.tag = ?);
             #	", array($_POST["rating"], $_POST["tag"]));
             $page->set_mode("redirect");
             $page->set_redirect(make_link("admin"));
         }
     }
     if ($event instanceof InitExtEvent) {
         if ($config->get_int("ext_ratings2_version") < 2) {
             $this->install();
         }
         $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 RatingSetEvent) {
         $this->set_rating($event->image->id, $event->rating);
     }
     if ($event instanceof ImageInfoBoxBuildingEvent) {
         if ($this->can_rate()) {
             $event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating), 80);
         }
     }
     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, "<br>Users: ");
         $sb->add_choice_option("ext_rating_admin_privs", $privs, "<br>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();
             for ($i = 0; $i < strlen($sqes); $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 = ?", array($char)));
         }
     }
 }
Example #16
0
 public function onPageRequest(Event $event)
 {
     global $page, $database, $user;
     if ($event->page_matches("blotter")) {
         switch ($event->get_arg(0)) {
             case "editor":
                 /**
                  * Displays the blotter editor.
                  */
                 if (!$user->is_admin()) {
                     $this->theme->display_permission_denied($page);
                 } else {
                     $entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
                     $this->theme->display_editor($entries);
                 }
                 break;
             case "add":
                 /**
                  * Adds an entry
                  */
                 if (!$user->is_admin() || !$user->check_auth_token()) {
                     $this->theme->display_permission_denied($page);
                 } else {
                     $entry_text = $_POST['entry_text'];
                     if ($entry_text == "") {
                         die("No entry message!");
                     }
                     if (isset($_POST['important'])) {
                         $important = 'Y';
                     } else {
                         $important = 'N';
                     }
                     // Now insert into db:
                     $database->execute("INSERT INTO blotter (entry_date, entry_text, important) VALUES (now(), ?, ?)", array($entry_text, $important));
                     log_info("blotter", "Added Message: {$entry_text}");
                     $page->set_mode("redirect");
                     $page->set_redirect(make_link("blotter/editor"));
                 }
                 break;
             case "remove":
                 /**
                  * Removes an entry
                  */
                 if (!$user->is_admin() || !$user->check_auth_token()) {
                     $this->theme->display_permission_denied($page);
                 } else {
                     $id = int_escape($_POST['id']);
                     if (!isset($id)) {
                         die("No ID!");
                     }
                     $database->Execute("DELETE FROM blotter WHERE id=:id", array("id" => $id));
                     log_info("blotter", "Removed Entry #{$id}");
                     $page->set_mode("redirect");
                     $page->set_redirect(make_link("blotter/editor"));
                 }
                 break;
             case "":
                 /**
                  * Displays all blotter entries
                  */
                 $entries = $database->get_all("SELECT * FROM blotter ORDER BY id DESC");
                 $this->theme->display_blotter_page($entries);
                 break;
         }
     }
     /**
      * Finally, display the blotter on whatever page we're viewing.
      */
     $this->display_blotter();
 }
Example #17
0
 public function onPageRequest(Event $event)
 {
     global $config, $database, $page, $user;
     // user info is shown on all pages
     if ($user->is_anonymous()) {
         $this->theme->display_login_block($page);
     } else {
         $ubbe = new UserBlockBuildingEvent();
         send_event($ubbe);
         ksort($ubbe->parts);
         $this->theme->display_user_block($page, $user, $ubbe->parts);
     }
     if ($event->page_matches("user_admin")) {
         if ($event->get_arg(0) == "login") {
             if (isset($_POST['user']) && isset($_POST['pass'])) {
                 $this->login($page);
             } else {
                 $this->theme->display_login_page($page);
             }
         } else {
             if ($event->get_arg(0) == "logout") {
                 set_prefixed_cookie("session", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/");
                 if (CACHE_HTTP) {
                     # to keep as few versions of content as possible,
                     # make cookies all-or-nothing
                     set_prefixed_cookie("user", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/");
                 }
                 log_info("user", "Logged out");
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link());
             } else {
                 if ($event->get_arg(0) == "change_pass") {
                     $this->change_password_wrapper($page);
                 } else {
                     if ($event->get_arg(0) == "change_email") {
                         $this->change_email_wrapper($page);
                     } else {
                         if ($event->get_arg(0) == "recover") {
                             $user = User::by_name($_POST['username']);
                             if (is_null($user)) {
                                 $this->theme->display_error($page, "Error", "There's no user with that name");
                             }
                             if (is_null($user->email)) {
                                 //
                             }
                         } else {
                             if ($event->get_arg(0) == "create") {
                                 if (!$config->get_bool("login_signup_enabled")) {
                                     $this->theme->display_signups_disabled($page);
                                 } else {
                                     if (!isset($_POST['name'])) {
                                         $this->theme->display_signup_page($page);
                                     } else {
                                         if ($_POST['pass1'] != $_POST['pass2']) {
                                             $this->theme->display_error($page, "Password Mismatch", "Passwords don't match");
                                         } else {
                                             try {
                                                 if (!captcha_check()) {
                                                     throw new UserCreationException("Error in captcha");
                                                 }
                                                 $uce = new UserCreationEvent($_POST['name'], $_POST['pass1'], $_POST['email']);
                                                 send_event($uce);
                                                 $this->set_login_cookie($uce->username, $uce->password);
                                                 $page->set_mode("redirect");
                                                 $page->set_redirect(make_link("user"));
                                             } catch (UserCreationException $ex) {
                                                 $this->theme->display_error($page, "User Creation Error", $ex->getMessage());
                                             }
                                         }
                                     }
                                 }
                             } else {
                                 if ($event->get_arg(0) == "set_more") {
                                     $this->set_more_wrapper($page);
                                 } else {
                                     if ($event->get_arg(0) == "list") {
                                         // select users.id,name,joindate,admin,
                                         // (select count(*) from images where images.owner_id=users.id) as images,
                                         // (select count(*) from comments where comments.owner_id=users.id) as comments from users;
                                         // select users.id,name,joindate,admin,image_count,comment_count
                                         // from users
                                         // join (select owner_id,count(*) as image_count from images group by owner_id) as _images on _images.owner_id=users.id
                                         // join (select owner_id,count(*) as comment_count from comments group by owner_id) as _comments on _comments.owner_id=users.id;
                                         $this->theme->display_user_list($page, User::by_list(0), $user);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("user")) {
         $display_user = $event->count_args() == 0 ? $user : User::by_name($event->get_arg(0));
         if ($event->count_args() == 0 && $user->is_anonymous()) {
             $this->theme->display_error($page, "Not Logged In", "You aren't logged in. First do that, then you can see your stats.");
         } else {
             if (!is_null($display_user)) {
                 send_event(new UserPageBuildingEvent($display_user));
             } else {
                 $this->theme->display_error($page, "No Such User", "If you typed the ID by hand, try again; if you came from a link on this " . "site, it might be bug report time...");
             }
         }
     }
 }
Example #18
0
 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 InitExtEvent) {
         $config->set_default_bool('report_image_show_thumbs', true);
         if ($config->get_int("ext_report_image_version") < 1) {
             $this->install();
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("image_report")) {
         if ($event->get_arg(0) == "add") {
             if (isset($_POST['image_id']) && isset($_POST['reason'])) {
                 $image_id = int_escape($_POST['image_id']);
                 send_event(new AddReportedImageEvent($image_id, $user->id, $_POST['reason']));
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/view/{$image_id}"));
             }
         } else {
             if ($event->get_arg(0) == "remove") {
                 if (isset($_POST['id'])) {
                     if ($user->is_admin()) {
                         send_event(new RemoveReportedImageEvent($_POST['id']));
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("image_report/list"));
                     }
                 }
             } else {
                 if ($event->get_arg(0) == "list") {
                     if ($user->is_admin()) {
                         $this->theme->display_reported_images($page, $this->get_reported_images());
                     }
                 }
             }
         }
     }
     if ($event instanceof AddReportedImageEvent) {
         $database->Execute("INSERT INTO image_reports(image_id, reporter_id, reason)\n\t\t\t\t\tVALUES (?, ?, ?)", array($event->image_id, $event->reporter_id, $event->reason));
     }
     if ($event instanceof RemoveReportedImageEvent) {
         $database->Execute("DELETE FROM image_reports WHERE id = ?", array($event->id));
     }
     if ($event instanceof DisplayingImageEvent) {
         if ($config->get_bool('report_image_anon') || !$user->is_anonymous()) {
             $this->theme->display_image_banner($page, $event->image);
         }
     }
     if ($event instanceof SetupBuildingEvent) {
         $sb = new SetupBlock("Report Image Options");
         $sb->add_bool_option("report_image_anon", "Allow anonymous image reporting: ");
         $sb->add_bool_option("report_image_show_thumbs", "<br>Show thumbnails in admin panel: ");
         $event->panel->add_block($sb);
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("Reported Images", make_link("image_report/list"));
         }
     }
     if ($event instanceof ImageDeletionEvent) {
         $database->Execute("DELETE FROM image_reports WHERE image_id = ?", array($event->image->id));
     }
 }
Example #19
0
 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 InitExtEvent) {
         if ($config->get_int("ext_numeric_score_version", 0) < 1) {
             $this->install();
         }
     }
     if ($event instanceof DisplayingImageEvent) {
         if (!$user->is_anonymous()) {
             $html = $this->theme->get_voter_html($event->image);
             $page->add_block(new Block("Image Score", $html, "left", 20));
         }
     }
     if ($event instanceof UserPageBuildingEvent) {
         $html = $this->theme->get_nuller_html($event->display_user);
         $page->add_block(new Block("Votes", $html, "main", 60));
     }
     if ($event instanceof PageRequestEvent) {
         if ($event->page_matches("numeric_score_votes")) {
             $image_id = int_escape($event->get_arg(0));
             $x = $database->get_all("SELECT users.name as username, user_id, score \n\t\t\t\t\tFROM numeric_score_votes \n\t\t\t\t\tJOIN users ON numeric_score_votes.user_id=users.id\n\t\t\t\t\tWHERE image_id=?", array($image_id));
             $html = "<table>";
             foreach ($x as $vote) {
                 $html .= "<tr><td>";
                 $html .= "<a href='/user/{$vote['username']}'>{$vote['username']}</a>";
                 $html .= "</td><td>";
                 $html .= $vote['score'];
                 $html .= "</td></tr>";
             }
             die($html);
         }
         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'];
                 $score = null;
                 if ($char == "up") {
                     $score = 1;
                 } else {
                     if ($char == "null") {
                         $score = 0;
                     } else {
                         if ($char == "down") {
                             $score = -1;
                         }
                     }
                 }
                 if (!is_null($score) && $image_id > 0) {
                     send_event(new NumericScoreSetEvent($image_id, $user, $score));
                 }
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/view/{$image_id}"));
             }
         }
         if ($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) {
             if ($user->is_admin()) {
                 $image_id = int_escape($_POST['image_id']);
                 $database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($image_id));
                 $database->execute("UPDATE images SET numeric_score=0 WHERE id=?", array($image_id));
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/view/{$image_id}"));
             }
         }
         if ($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) {
             if ($user->is_admin()) {
                 $user_id = int_escape($_POST['user_id']);
                 $image_ids = $database->get_col("SELECT image_id FROM numeric_score_votes WHERE user_id=?", array($user_id));
                 $database->execute("DELETE FROM numeric_score_votes WHERE user_id=? AND image_id IN ?", array($user_id, $image_ids));
                 $database->execute("UPDATE images SET numeric_score=(SELECT SUM(score) FROM numeric_score_votes WHERE image_id=images.id) WHERE images.id IN ?", array($image_ids));
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link());
             }
         }
     }
     if ($event instanceof NumericScoreSetEvent) {
         log_info("numeric_score", "Rated Image #{$event->image_id} as {$event->score}");
         $this->add_vote($event->image_id, $user->id, $event->score);
     }
     if ($event instanceof ImageDeletionEvent) {
         $database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($event->image->id));
     }
     if ($event instanceof ParseLinkTemplateEvent) {
         $event->replace('$score', $event->image->numeric_score);
     }
     if ($event instanceof SearchTermParseEvent) {
         $matches = array();
         if (preg_match("/^score(<|<=|=|>=|>)(\\d+)\$/", $event->term, $matches)) {
             $cmp = $matches[1];
             $score = $matches[2];
             $event->add_querylet(new Querylet("numeric_score {$cmp} {$score}"));
         }
         if (preg_match("/^upvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=1)", array($duser->id)));
         }
         if (preg_match("/^downvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=-1)", array($duser->id)));
         }
         if (preg_match("/^upvoted_by_id=(\\d+)\$/", $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=? AND score=1)", array($iid)));
         }
         if (preg_match("/^downvoted_by_id=(\\d+)\$/", $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=? AND score=-1)", array($iid)));
         }
     }
 }
Example #20
0
 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 InitExtEvent) {
         if ($config->get_int("ext_imageban_version") < 1) {
             $this->install();
         }
     }
     if ($event instanceof DataUploadEvent) {
         $row = $database->db->GetRow("SELECT * FROM image_bans WHERE hash = ?", $event->hash);
         if ($row) {
             log_info("image_hash_ban", "Blocked image ({$event->hash})");
             throw new UploadException("Image " . html_escape($row["hash"]) . " has been banned, reason: " . format_text($row["reason"]));
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("image_hash_ban")) {
         if ($user->is_admin()) {
             if ($event->get_arg(0) == "add") {
                 if (isset($_POST['hash']) && isset($_POST['reason'])) {
                     send_event(new AddImageHashBanEvent($_POST['hash'], $_POST['reason']));
                     $page->set_mode("redirect");
                     $page->set_redirect(make_link("image_hash_ban/list/1"));
                 }
                 if (isset($_POST['image_id'])) {
                     $image = Image::by_id(int_escape($_POST['image_id']));
                     if ($image) {
                         send_event(new ImageDeletionEvent($image));
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("post/list"));
                     }
                 }
             } else {
                 if ($event->get_arg(0) == "remove") {
                     if (isset($_POST['hash'])) {
                         send_event(new RemoveImageHashBanEvent($_POST['hash']));
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("image_hash_ban/list/1"));
                     }
                 } else {
                     if ($event->get_arg(0) == "list") {
                         $page_num = 0;
                         if ($event->count_args() == 2) {
                             $page_num = int_escape($event->get_arg(1));
                         }
                         $page_size = 100;
                         $page_count = ceil($database->db->getone("SELECT COUNT(id) FROM image_bans") / $page_size);
                         $this->theme->display_Image_hash_Bans($page, $page_num, $page_count, $this->get_image_hash_bans($page_num, $page_size));
                     }
                 }
             }
         }
     }
     if ($event instanceof UserBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_link("Image Bans", make_link("image_hash_ban/list/1"));
         }
     }
     if ($event instanceof AddImageHashBanEvent) {
         $this->add_image_hash_ban($event->hash, $event->reason);
     }
     if ($event instanceof RemoveImageHashBanEvent) {
         $this->remove_image_hash_ban($event->hash);
     }
     if ($event instanceof ImageAdminBlockBuildingEvent) {
         if ($user->is_admin()) {
             $event->add_part($this->theme->get_buttons_html($event->image));
         }
     }
 }