Example #1
0
 public function onPageRequest($event)
 {
     if ($event->page_matches("sitemap.xml")) {
         $images = Image::find_images(0, 50000, array());
         $this->do_xml($images);
     }
 }
Example #2
0
 public function onPageRequest(PageRequestEvent $event)
 {
     if ($event->page_matches("rss/images")) {
         $search_terms = $event->get_search_terms();
         $page_number = $event->get_page_number();
         $page_size = $event->get_page_size();
         $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
         $this->do_rss($images, $search_terms, $page_number);
     }
 }
Example #3
0
 public function onPageRequest(PageRequestEvent $event)
 {
     global $page, $user;
     if ($event->page_matches("api/shimmie")) {
         $page->set_mode("data");
         $page->set_type("text/plain");
         if ($event->page_matches("api/shimmie/get_tags")) {
             $tag = $event->get_arg(0);
             if (empty($tag) && isset($_GET['tag'])) {
                 $tag = $_GET['tag'];
             }
             $res = $this->api_get_tags($tag);
             $page->set_data(json_encode($res));
         } elseif ($event->page_matches("api/shimmie/get_image")) {
             $arg = $event->get_arg(0);
             if (empty($arg) && isset($_GET['id'])) {
                 $arg = $_GET['id'];
             }
             $image = Image::by_id(int_escape($arg));
             // FIXME: handle null image
             $image->get_tag_array();
             // tag data isn't loaded into the object until necessary
             $safe_image = new _SafeImage($image);
             $page->set_data(json_encode($safe_image));
         } elseif ($event->page_matches("api/shimmie/find_images")) {
             $search_terms = $event->get_search_terms();
             $page_number = $event->get_page_number();
             $page_size = $event->get_page_size();
             $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
             $safe_images = array();
             foreach ($images as $image) {
                 $image->get_tag_array();
                 $safe_images[] = new _SafeImage($image);
             }
             $page->set_data(json_encode($safe_images));
         } elseif ($event->page_matches("api/shimmie/get_user")) {
             $query = $user->id;
             $type = "id";
             if ($event->count_args() == 1) {
                 $query = $event->get_arg(0);
                 $type = "name";
             } elseif (isset($_GET['id'])) {
                 $query = $_GET['id'];
             } elseif (isset($_GET['name'])) {
                 $query = $_GET['name'];
                 $type = "name";
             }
             $all = $this->api_get_user($type, $query);
             $page->set_data(json_encode($all));
         } else {
             $page->set_mode("redirect");
             $page->set_redirect(make_link("ext_doc/shimmie_api"));
         }
     }
 }
Example #4
0
 public function onPageRequest($event)
 {
     if ($event->page_matches("rss/images")) {
         $page_number = 0;
         $search_terms = array();
         if ($event->count_args() == 1) {
             $page_number = int_escape($event->get_arg(0));
         } else {
             if ($event->count_args() == 2) {
                 $search_terms = explode(' ', $event->get_arg(0));
                 $page_number = int_escape($event->get_arg(1));
             }
         }
         $images = Image::find_images(($page_number - 1) * 10, 10, $search_terms);
         $this->do_rss($images, $search_terms, $page_number);
     }
 }
Example #5
0
 public function onPageRequest(PageRequestEvent $event)
 {
     global $database, $page;
     if ($event->page_matches("api/shimmie")) {
         $page->set_mode("data");
         $page->set_type("text/plain");
         if ($event->page_matches("api/shimmie/get_tags")) {
             if ($event->count_args() == 2) {
                 $all = $database->get_all("SELECT tag FROM tags WHERE tag LIKE ?", array($event->get_arg(0) . "%"));
             } else {
                 $all = $database->get_all("SELECT tag FROM tags");
             }
             $res = array();
             foreach ($all as $row) {
                 $res[] = $row["tag"];
             }
             $page->set_data(json_encode($res));
         }
         if ($event->page_matches("api/shimmie/get_image")) {
             $image = Image::by_id(int_escape($event->get_arg(0)));
             $image->get_tag_array();
             // tag data isn't loaded into the object until necessary
             $safe_image = new _SafeImage($image);
             $page->set_data(json_encode($safe_image));
         }
         if ($event->page_matches("api/shimmie/find_images")) {
             $search_terms = $event->get_search_terms();
             $page_number = $event->get_page_number();
             $page_size = $event->get_page_size();
             $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
             $safe_images = array();
             foreach ($images as $image) {
                 $image->get_tag_array();
                 $safe_images[] = new _SafeImage($image);
             }
             $page->set_data(json_encode($safe_images));
         }
     }
 }
Example #6
0
 private function determine_images()
 {
     // set vars
     $images_for_removal = array();
     $error = "";
     $min_id = $_POST['remove_id_min'];
     $max_id = $_POST['remove_id_max'];
     $tags = $_POST['remove_tags'];
     // if using id range to remove (comined removal with tags)
     if ($min_id != "" && $max_id != "") {
         // error if values are not correctly entered
         if (!is_numeric($min_id) || !is_numeric($max_id) || intval($max_id) < intval($min_id)) {
             $error = "Values not correctly entered for removal between id.";
         } else {
             // if min & max id are valid
             // Grab the list of images & place it in the removing array
             foreach (Image::find_images(intval($min_id), intval($max_id)) as $image) {
                 array_push($images_for_removal, $image);
             }
         }
     }
     // refine previous results or create results from tags
     if ($tags != "") {
         $tags_arr = explode(" ", $_POST['remove_tags']);
         // Search all images with the specified tags & add to list
         foreach (Image::find_images(1, 2147483647, $tags_arr) as $image) {
             array_push($images_for_removal, $image);
         }
     }
     // if no images were found with the given info
     if (count($images_for_removal) == 0) {
         $error = "No images selected for removal";
     }
     //var_dump($tags_arr);
     return array("error" => $error, "images_for_removal" => $images_for_removal);
 }
Example #7
0
 public function onPageRequest($event)
 {
     global $config, $database, $page, $user;
     if ($event->page_matches("post/list")) {
         if (isset($_GET['search'])) {
             $search = url_escape(trim($_GET['search']));
             if (empty($search)) {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/list/1"));
             } else {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/list/{$search}/1"));
             }
             return;
         }
         $search_terms = $event->get_search_terms();
         $page_number = $event->get_page_number();
         $page_size = $event->get_page_size();
         try {
             $total_pages = Image::count_pages($search_terms);
             $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
         } catch (SearchTermParseException $stpe) {
             // FIXME: display the error somewhere
             $total_pages = 0;
             $images = array();
         }
         if (count($search_terms) == 0 && count($images) == 0 && $page_number == 1) {
             $this->theme->display_intro($page);
             send_event(new PostListBuildingEvent($search_terms));
         } else {
             if (count($search_terms) > 0 && count($images) == 1 && $page_number == 1) {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/view/{$images[0]->id}"));
             } else {
                 send_event(new PostListBuildingEvent($search_terms));
                 $this->theme->set_page($page_number, $total_pages, $search_terms);
                 $this->theme->display_page($page, $images);
             }
         }
     }
 }
Example #8
0
 public function onPageRequest(PageRequestEvent $event)
 {
     global $user, $page;
     if ($event->page_matches("admin/bulk_rate")) {
         if (!$user->is_admin()) {
             throw new PermissionDeniedException();
         } else {
             $n = 0;
             while (true) {
                 $images = Image::find_images($n, 100, Tag::explode($_POST["query"]));
                 if (count($images) == 0) {
                     break;
                 }
                 reset($images);
                 // rewind to first element in array.
                 foreach ($images as $image) {
                     send_event(new RatingSetEvent($image, $_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("post/list"));
         }
     }
 }
 /**
  * Pick a random image out of a set
  *
  * @retval Image
  */
 public static function by_random($tags = array())
 {
     assert(is_array($tags));
     $max = Image::count_images($tags);
     $rand = mt_rand(0, $max - 1);
     $set = Image::find_images($rand, 1, $tags);
     if (count($set) > 0) {
         return $set[0];
     } else {
         return null;
     }
 }
Example #10
0
 private function mass_tag_edit($search, $replace)
 {
     global $database;
     global $config;
     $search_set = Tag::explode($search);
     $replace_set = Tag::explode($replace);
     $last_id = -1;
     while (true) {
         // make sure we don't look at the same images twice.
         // search returns high-ids first, so we want to look
         // at images with lower IDs than the previous.
         $search_forward = $search_set;
         if ($last_id >= 0) {
             $search_forward[] = "id<{$last_id}";
         }
         $images = Image::find_images(0, 100, $search_forward);
         if (count($images) == 0) {
             break;
         }
         foreach ($images as $image) {
             // remove the search'ed tags
             $before = $image->get_tag_array();
             $after = array();
             foreach ($before as $tag) {
                 if (!in_array($tag, $search_set)) {
                     $after[] = $tag;
                 }
             }
             // add the replace'd tags
             foreach ($replace_set as $tag) {
                 $after[] = $tag;
             }
             $image->set_tags($after);
             $last_id = $image->id;
         }
     }
 }
Example #11
0
 private function handle_full_sitemap()
 {
     global $database, $config;
     // add index
     $index = array();
     $index[0] = $config->get_string("front_page");
     $this->add_sitemap_queue($index, "weekly", "1");
     /* --- Add 20 most used tags --- */
     $popular_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 0,20");
     foreach ($popular_tags as $arrayid => $tag) {
         $tag = $tag['tag'];
         $popular_tags[$arrayid] = "post/list/{$tag}/";
     }
     $this->add_sitemap_queue($popular_tags, "monthly", "0.9");
     /* --- Add latest images to sitemap with higher priority --- */
     $latestimages = Image::find_images(0, 50, array());
     $latestimages_urllist = array();
     foreach ($latestimages as $arrayid => $image) {
         // create url from image id's
         $latestimages_urllist[$arrayid] = "post/view/{$image->id}";
     }
     $this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", strtotime($image->posted)));
     /* --- Add other tags --- */
     $other_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 21,10000000");
     foreach ($other_tags as $arrayid => $tag) {
         $tag = $tag['tag'];
         // create url from tags (tagme ignored)
         if ($tag != "tagme") {
             $other_tags[$arrayid] = "post/list/{$tag}/";
         }
     }
     $this->add_sitemap_queue($other_tags, "monthly", "0.7");
     /* --- Add all other images to sitemap with lower priority --- */
     $otherimages = Image::find_images(51, 10000000, array());
     foreach ($otherimages as $arrayid => $image) {
         // create url from image id's
         $otherimages[$arrayid] = "post/view/{$image->id}";
     }
     $this->add_sitemap_queue($otherimages, "monthly", "0.6", date("Y-m-d", strtotime($image->posted)));
     /* --- Display page --- */
     // when sitemap is ok, display it from the file
     $this->generate_display_sitemap();
 }
 /**
  * Pick a random image out of a set.
  *
  * @param string[] $tags
  * @return Image
  */
 public static function by_random($tags = array())
 {
     assert('is_array($tags)');
     $max = Image::count_images($tags);
     if ($max < 1) {
         return null;
     }
     // From Issue #22 - opened by HungryFeline on May 30, 2011.
     $rand = mt_rand(0, $max - 1);
     $set = Image::find_images($rand, 1, $tags);
     if (count($set) > 0) {
         return $set[0];
     } else {
         return null;
     }
 }
Example #13
0
 private function delete_by_query($query)
 {
     global $page, $user;
     assert(strlen($query) > 1);
     foreach (Image::find_images(0, 1000000, Tag::explode($query)) as $image) {
         send_event(new ImageDeletionEvent($image));
     }
 }
Example #14
0
 /**
  * find_posts()
  * Find all posts that match the search criteria. Posts will be ordered by id descending.
  *
  * Parameters:
  * - md5: md5 hash to search for (comma delimited)
  * - id: id to search for (comma delimited)
  * - tags: what tags to search for
  * - limit: limit
  * - page: page number
  * - after_id: limit results to posts added after this id
  *
  * @return string
  * @throws SCoreException
  */
 private function api_find_posts()
 {
     $results = array();
     $this->authenticate_user();
     $start = 0;
     if (isset($_GET['md5'])) {
         $md5list = explode(",", $_GET['md5']);
         foreach ($md5list as $md5) {
             $results[] = Image::by_hash($md5);
         }
         $count = count($results);
     } elseif (isset($_GET['id'])) {
         $idlist = explode(",", $_GET['id']);
         foreach ($idlist as $id) {
             $results[] = Image::by_id($id);
         }
         $count = count($results);
     } else {
         $limit = isset($_GET['limit']) ? int_escape($_GET['limit']) : 100;
         // Calculate start offset.
         if (isset($_GET['page'])) {
             // Danbooru API uses 'page' >= 1
             $start = (int_escape($_GET['page']) - 1) * $limit;
         } else {
             if (isset($_GET['pid'])) {
                 // Gelbooru API uses 'pid' >= 0
                 $start = int_escape($_GET['pid']) * $limit;
             } else {
                 $start = 0;
             }
         }
         $tags = isset($_GET['tags']) ? Tag::explode($_GET['tags']) : array();
         $count = Image::count_images($tags);
         $results = Image::find_images(max($start, 0), min($limit, 100), $tags);
     }
     // Now we have the array $results filled with Image objects
     // Let's display them
     $xml = "<posts count=\"{$count}\" offset=\"{$start}\">\n";
     foreach ($results as $img) {
         // Sanity check to see if $img is really an image object
         // If it isn't (e.g. someone requested an invalid md5 or id), break out of the this
         if (!is_object($img)) {
             continue;
         }
         $taglist = $img->get_tag_list();
         $owner = $img->get_owner();
         $previewsize = get_thumbnail_size($img->width, $img->height);
         $xml .= xml_tag("post", array("id" => $img->id, "md5" => $img->hash, "file_name" => $img->filename, "file_url" => $img->get_image_link(), "height" => $img->height, "width" => $img->width, "preview_url" => $img->get_thumb_link(), "preview_height" => $previewsize[1], "preview_width" => $previewsize[0], "rating" => "u", "date" => $img->posted, "is_warehoused" => false, "tags" => $taglist, "source" => $img->source, "score" => 0, "author" => $owner->name));
     }
     $xml .= "</posts>";
     return $xml;
 }
Example #15
0
 public function onPageRequest(PageRequestEvent $event)
 {
     global $config, $database, $page, $user;
     if ($event->page_matches("post/list")) {
         if (isset($_GET['search'])) {
             $search = url_escape(Tag::implode(Tag::resolve_aliases(Tag::explode($_GET['search'], false))));
             if (empty($search)) {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/list/1"));
             } else {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link('post/list/' . $search . '/1'));
             }
             return;
         }
         $search_terms = $event->get_search_terms();
         $page_number = $event->get_page_number();
         $page_size = $event->get_page_size();
         $count_search_terms = count($search_terms);
         try {
             #log_debug("index", "Search for ".implode(" ", $search_terms), false, array("terms"=>$search_terms));
             $total_pages = Image::count_pages($search_terms);
             if (SPEED_HAX && $count_search_terms === 0 && $page_number < 10) {
                 // extra caching for the first few post/list pages
                 $images = $database->cache->get("post-list:{$page_number}");
                 if (!$images) {
                     $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
                     $database->cache->set("post-list:{$page_number}", $images, 600);
                 }
             } else {
                 $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
             }
         } catch (SearchTermParseException $stpe) {
             // FIXME: display the error somewhere
             $total_pages = 0;
             $images = array();
         }
         $count_images = count($images);
         if ($count_search_terms === 0 && $count_images === 0 && $page_number === 1) {
             $this->theme->display_intro($page);
             send_event(new PostListBuildingEvent($search_terms));
         } else {
             if ($count_search_terms > 0 && $count_images === 1 && $page_number === 1) {
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link('post/view/' . $images[0]->id));
             } else {
                 $plbe = new PostListBuildingEvent($search_terms);
                 send_event($plbe);
                 $this->theme->set_page($page_number, $total_pages, $search_terms);
                 $this->theme->display_page($page, $images);
                 if (count($plbe->parts) > 0) {
                     $this->theme->display_admin_block($plbe->parts);
                 }
             }
         }
     }
 }
Example #16
0
 public function onPageRequest(PageRequestEvent $event)
 {
     global $database, $page, $user;
     if ($event->page_matches("api/shimmie")) {
         $page->set_mode("data");
         $page->set_type("text/plain");
         if (!$event->page_matches("api/shimmie/get_tags") && !$event->page_matches("api/shimmie/get_image") && !$event->page_matches("api/shimmie/find_images") && !$event->page_matches("api/shimmie/get_user")) {
             $page->set_mode("redirect");
             $page->set_redirect(make_link("ext_doc/shimmie_api"));
         }
         if ($event->page_matches("api/shimmie/get_tags")) {
             $arg = $event->get_arg(0);
             if (!empty($arg)) {
                 $all = $database->get_all("SELECT tag FROM tags WHERE tag LIKE ?", array($arg . "%"));
             } elseif (isset($_GET['tag'])) {
                 $all = $database->get_all("SELECT tag FROM tags WHERE tag LIKE ?", array($_GET['tag'] . "%"));
             } else {
                 $all = $database->get_all("SELECT tag FROM tags");
             }
             $res = array();
             foreach ($all as $row) {
                 $res[] = $row["tag"];
             }
             $page->set_data(json_encode($res));
         }
         if ($event->page_matches("api/shimmie/get_image")) {
             $arg = $event->get_arg(0);
             if (!empty($arg)) {
                 $image = Image::by_id(int_escape($event->get_arg(0)));
             } elseif (isset($_GET['id'])) {
                 $image = Image::by_id(int_escape($_GET['id']));
             }
             // FIXME: handle null image
             $image->get_tag_array();
             // tag data isn't loaded into the object until necessary
             $safe_image = new _SafeImage($image);
             $page->set_data(json_encode($safe_image));
         }
         if ($event->page_matches("api/shimmie/find_images")) {
             $search_terms = $event->get_search_terms();
             $page_number = $event->get_page_number();
             $page_size = $event->get_page_size();
             $images = Image::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
             $safe_images = array();
             foreach ($images as $image) {
                 $image->get_tag_array();
                 $safe_images[] = new _SafeImage($image);
             }
             $page->set_data(json_encode($safe_images));
         }
         if ($event->page_matches("api/shimmie/get_user")) {
             $query = $user->id;
             $type = "id";
             if ($event->count_args() == 1) {
                 $query = $event->get_arg(0);
             } elseif (isset($_GET['id'])) {
                 $query = $_GET['id'];
             } elseif (isset($_GET['name'])) {
                 $query = $_GET['name'];
                 $type = "name";
             }
             $all = $database->get_row("SELECT id,name,joindate,class FROM users WHERE " . $type . "=?", array($query));
             if (!empty($all)) {
                 //FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice..
                 // - it returns data as eg  array(0=>1234, 'id'=>1234, 1=>'bob', 'name'=>bob, ...);
                 for ($i = 0; $i < 4; $i++) {
                     unset($all[$i]);
                 }
                 $all['uploadcount'] = Image::count_images(array("user_id=" . $all['id']));
                 $all['commentcount'] = $database->get_one("SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id", array("owner_id" => $all['id']));
                 if (isset($_GET['recent'])) {
                     $recent = $database->get_all("SELECT * FROM images WHERE owner_id=? ORDER BY id DESC LIMIT 0, 5", array($all['id']));
                     $i = 0;
                     foreach ($recent as $all['recentposts'][$i]) {
                         unset($all['recentposts'][$i]['owner_id']);
                         //We already know the owners id..
                         unset($all['recentposts'][$i]['owner_ip']);
                         for ($x = 0; $x < 14; $x++) {
                             unset($all['recentposts'][$i][$x]);
                         }
                         if (empty($all['recentposts'][$i]['author'])) {
                             unset($all['recentposts'][$i]['author']);
                         }
                         if ($all['recentposts'][$i]['notes'] > 0) {
                             $all['recentposts'][$i]['has_notes'] = "Y";
                         } else {
                             $all['recentposts'][$i]['has_notes'] = "N";
                         }
                         unset($all['recentposts'][$i]['notes']);
                         $i += 1;
                     }
                 }
             }
             $page->set_data(json_encode($all));
         }
     }
 }
Example #17
0
 private function delete_by_query()
 {
     global $page;
     $query = $_POST['query'];
     $reason = @$_POST['reason'];
     assert(strlen($query) > 1);
     log_warning("admin", "Mass deleting: {$query}");
     $count = 0;
     foreach (Image::find_images(0, 1000000, Tag::explode($query)) as $image) {
         if ($reason && class_exists("ImageBan")) {
             send_event(new AddImageHashBanEvent($image->hash, $reason));
         }
         send_event(new ImageDeletionEvent($image));
         $count++;
     }
     log_debug("admin", "Deleted {$count} images", true);
     $page->set_mode("redirect");
     $page->set_redirect(make_link("post/list"));
     return false;
 }
Example #18
0
 private function api_danbooru($event)
 {
     global $page;
     global $config;
     global $database;
     global $user;
     $page->set_mode("data");
     $page->set_type("application/xml");
     //debug
     //$page->set_type("text/plain");
     $results = array();
     /*
     add_post()
     Adds a post to the database.
     Parameters
     * login: login
     * password: password
     * file: file as a multipart form
     * source: source url
     * title: title **IGNORED**
     * tags: list of tags as a string, delimited by whitespace
     * md5: MD5 hash of upload in hexadecimal format
     * rating: rating of the post. can be explicit, questionable, or safe. **IGNORED**
     Notes
     * The only necessary parameter is tags and either file or source.
     * If you want to sign your post, you need a way to authenticate your account, either by supplying login and password, or by supplying a cookie.
     * If an account is not supplied or if it doesn‘t authenticate, he post will be added anonymously.
     * If the md5 parameter is supplied and does not match the hash of what‘s on the server, the post is rejected.
     Response
     The response depends on the method used:
     Post
     * X-Danbooru-Location set to the URL for newly uploaded post.
     Get
     * Redirected to the newly uploaded post.
     */
     if ($event->get_arg(1) == 'add_post' || $event->get_arg(1) == 'post' && $event->get_arg(2) == 'create.xml') {
         // No XML data is returned from this function
         $page->set_type("text/plain");
         // Check first if a login was supplied, if it wasn't check if the user is logged in via cookie
         // If all that fails, it's an anonymous upload
         $this->authenticate_user();
         // Now we check if a file was uploaded or a url was provided to transload
         // Much of this code is borrowed from /ext/upload
         if ($config->get_bool("upload_anon") || !$user->is_anonymous()) {
             $file = null;
             $filename = "";
             $source = "";
             if (isset($_FILES['file'])) {
                 // A file was POST'd in
                 $file = $_FILES['file']['tmp_name'];
                 $filename = $_FILES['file']['name'];
                 // If both a file is posted and a source provided, I'm assuming source is the source of the file
                 if (isset($_REQUEST['source']) && !empty($_REQUEST['source'])) {
                     $source = $_REQUEST['source'];
                 } else {
                     $source = null;
                 }
             } elseif (isset($_FILES['post'])) {
                 $file = $_FILES['post']['tmp_name']['file'];
                 $filename = $_FILES['post']['name']['file'];
                 if (isset($_REQUEST['post']['source']) && !empty($_REQUEST['post']['source'])) {
                     $source = $_REQUEST['post']['source'];
                 } else {
                     $source = null;
                 }
             } elseif (isset($_REQUEST['source']) || isset($_REQUEST['post']['source'])) {
                 // A url was provided
                 $url = isset($_REQUEST['source']) ? $_REQUEST['source'] : $_REQUEST['post']['source'];
                 $source = $url;
                 $tmp_filename = tempnam("/tmp", "shimmie_transload");
                 // Are we using fopen wrappers or curl?
                 if ($config->get_string("transload_engine") == "fopen") {
                     $fp = fopen($url, "r");
                     if (!$fp) {
                         header("HTTP/1.0 409 Conflict");
                         header("X-Danbooru-Errors: fopen read error");
                     }
                     $data = "";
                     $length = 0;
                     while (!feof($fp) && $length <= $config->get_int('upload_size')) {
                         $data .= fread($fp, 8192);
                         $length = strlen($data);
                     }
                     fclose($fp);
                     $fp = fopen($tmp_filename, "w");
                     fwrite($fp, $data);
                     fclose($fp);
                 }
                 if ($config->get_string("transload_engine") == "curl") {
                     $ch = curl_init($url);
                     $fp = fopen($tmp_filename, "w");
                     curl_setopt($ch, CURLOPT_FILE, $fp);
                     curl_setopt($ch, CURLOPT_HEADER, 0);
                     curl_exec($ch);
                     curl_close($ch);
                     fclose($fp);
                 }
                 $file = $tmp_filename;
                 $filename = basename($url);
             } else {
                 // Nothing was specified at all
                 header("HTTP/1.0 409 Conflict");
                 header("X-Danbooru-Errors: no input files");
                 return;
             }
             // Get tags out of url
             $posttags = Tag::explode(isset($_REQUEST['tags']) ? $_REQUEST['tags'] : $_REQUEST['post']['tags']);
             $hash = md5_file($file);
             // Was an md5 supplied? Does it match the file hash?
             if (isset($_REQUEST['md5'])) {
                 if (strtolower($_REQUEST['md5']) != $hash) {
                     header("HTTP/1.0 409 Conflict");
                     header("X-Danbooru-Errors: md5 mismatch");
                     return;
                 }
             }
             // Upload size checking is now performed in the upload extension
             // It is also currently broken due to some confusion over file variable ($tmp_filename?)
             // Does it exist already?
             $existing = Image::by_hash($hash);
             if (!is_null($existing)) {
                 header("HTTP/1.0 409 Conflict");
                 header("X-Danbooru-Errors: duplicate");
                 $existinglink = make_link("post/view/" . $existing->id);
                 header("X-Danbooru-Location: {$existinglink}");
             }
             // Fire off an event which should process the new file and add it to the db
             $fileinfo = pathinfo($filename);
             $metadata['filename'] = $fileinfo['basename'];
             $metadata['extension'] = $fileinfo['extension'];
             $metadata['tags'] = $posttags;
             $metadata['source'] = $source;
             try {
                 $nevent = new DataUploadEvent($user, $file, $metadata);
                 send_event($nevent);
                 // If it went ok, grab the id for the newly uploaded image and pass it in the header
                 $newimg = Image::by_hash($hash);
                 $newid = make_link("post/view/" . $newimg->id);
                 // Did we POST or GET this call?
                 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                     header("X-Danbooru-Location: {$newid}");
                 } else {
                     header("Location: {$newid}");
                 }
             } catch (UploadException $ex) {
                 // Did something screw up?
                 header("HTTP/1.0 409 Conflict");
                 header("X-Danbooru-Errors: " . $ex->getMessage());
                 return;
             }
         } else {
             header("HTTP/1.0 409 Conflict");
             header("X-Danbooru-Errors: authentication error");
             return;
         }
     }
     /*
     find_posts()
     Find all posts that match the search criteria. Posts will be ordered by id descending.
     Parameters
     * md5: md5 hash to search for (comma delimited)
     * id: id to search for (comma delimited)
     * tags: what tags to search for
     * limit: limit
     * offset: offset
     * after_id: limit results to posts added after this id
     */
     if ($event->get_arg(1) == 'find_posts' || $event->get_arg(1) == 'post' && $event->get_arg(2) == 'index.xml') {
         $this->authenticate_user();
         if (isset($_GET['md5'])) {
             $md5list = explode(",", $_GET['md5']);
             foreach ($md5list as $md5) {
                 $results[] = Image::by_hash($md5);
             }
         } elseif (isset($_GET['id'])) {
             $idlist = explode(",", $_GET['id']);
             foreach ($idlist as $id) {
                 $results[] = Image::by_id($id);
             }
         } else {
             $limit = isset($_GET['limit']) ? int_escape($_GET['limit']) : 100;
             $start = isset($_GET['offset']) ? int_escape($_GET['offset']) : 0;
             $tags = isset($_GET['tags']) ? Tag::explode($_GET['tags']) : array();
             $results = Image::find_images($start, $limit, $tags);
         }
         // Now we have the array $results filled with Image objects
         // Let's display them
         $xml = "<posts>\n";
         foreach ($results as $img) {
             // Sanity check to see if $img is really an image object
             // If it isn't (e.g. someone requested an invalid md5 or id), break out of the this
             if (!is_object($img)) {
                 continue;
             }
             $taglist = $img->get_tag_list();
             $owner = $img->get_owner();
             $xml .= "<post md5=\"{$img->hash}\" rating=\"Questionable\" date=\"{$img->posted}\" is_warehoused=\"false\" file_name=\"{$img->filename}\" tags=\"" . $this->xmlspecialchars($taglist) . "\" source=\"" . $this->xmlspecialchars($img->source) . "\" score=\"0\" id=\"{$img->id}\" author=\"{$owner->name}\"/>\n";
         }
         $xml .= "</posts>";
         $page->set_data($xml);
     }
     /*
     find_tags() Find all tags that match the search criteria.
     Parameters
     * id: A comma delimited list of tag id numbers.
     * name: A comma delimited list of tag names.
     * tags: any typical tag query. See Tag#parse_query for details.
     * after_id: limit results to tags with an id number after after_id. Useful if you only want to refresh
     */
     if ($event->get_arg(1) == 'find_tags') {
         if (isset($_GET['id'])) {
             $idlist = explode(",", $_GET['id']);
             foreach ($idlist as $id) {
                 $sqlresult = $database->execute("SELECT id,tag,count FROM tags WHERE id = ?", array($id));
                 if (!$sqlresult->EOF) {
                     $results[] = array($sqlresult->fields['count'], $sqlresult->fields['tag'], $sqlresult->fields['id']);
                 }
             }
         } elseif (isset($_GET['name'])) {
             $namelist = explode(",", $_GET['name']);
             foreach ($namelist as $name) {
                 $sqlresult = $database->execute("SELECT id,tag,count FROM tags WHERE tag = ?", array($name));
                 if (!$sqlresult->EOF) {
                     $results[] = array($sqlresult->fields['count'], $sqlresult->fields['tag'], $sqlresult->fields['id']);
                 }
             }
         } else {
             $start = isset($_GET['after_id']) ? int_escape($_GET['offset']) : 0;
             $sqlresult = $database->execute("SELECT id,tag,count FROM tags WHERE count > 0 AND id >= ? ORDER BY id DESC", array($start));
             while (!$sqlresult->EOF) {
                 $results[] = array($sqlresult->fields['count'], $sqlresult->fields['tag'], $sqlresult->fields['id']);
                 $sqlresult->MoveNext();
             }
         }
         // Tag results collected, build XML output
         $xml = "<tags>\n";
         foreach ($results as $tag) {
             $xml .= "<tag type=\"0\" count=\"{$tag['0']}\" name=\"" . $this->xmlspecialchars($tag[1]) . "\" id=\"{$tag['2']}\"/>\n";
         }
         $xml .= "</tags>";
         $page->set_data($xml);
     }
     // Hackery for danbooruup 0.3.2 providing the wrong view url. This simply redirects to the proper
     // Shimmie view page
     // Example: danbooruup says the url is http://shimmie/api/danbooru/post/show/123
     // This redirects that to http://shimmie/post/view/123
     if ($event->get_arg(1) == 'post' && $event->get_arg(2) == 'show') {
         $fixedlocation = make_link("post/view/" . $event->get_arg(3));
         header("Location: {$fixedlocation}");
     }
 }
Example #19
0
 /**
  * HERE WE GET THE IMAGES FROM THE TAG ON IMPORT
  * @param int $pool_id
  */
 private function import_posts($pool_id)
 {
     global $page, $config;
     $poolsMaxResults = $config->get_int("poolsMaxImportResults", 1000);
     $images = $images = Image::find_images(0, $poolsMaxResults, Tag::explode($_POST["pool_tag"]));
     $this->theme->pool_result($page, $images, $this->get_pool($pool_id));
 }
Example #20
0
 /**
  * Wrapper for getting a list of posts
  * @param $limit
  * @param $page
  * @param $tags
  */
 protected function postIndex($limit, $page, $tags)
 {
     $start = ($page - 1) * $limit;
     $results = Image::find_images(max($start, 0), min($limit, 100), $tags);
     $posts = array();
     foreach ($results as $img) {
         if (!is_object($img)) {
             continue;
         }
         $posts[] = new _SafeOuroborosImage($img);
     }
     $this->sendData('post', $posts, max($start, 0));
 }
Example #21
0
 public function handle_commands($event)
 {
     global $config, $page, $user;
     if ($event->page_matches("artist")) {
         switch ($event->get_arg(0)) {
             //*************ARTIST SECTION**************
             case "list":
                 $this->get_listing($page, $event);
                 $this->theme->sidebar_options("neutral");
                 break;
             case "new":
                 if (!$user->is_anonymous()) {
                     $this->theme->new_artist_composer();
                 } else {
                     $errMessage = "You must be registered and logged in to create a new artist.";
                     $this->theme->display_error($page, "Error", $errMessage);
                 }
                 break;
             case "new_artist":
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("artist/new"));
                 break;
             case "create":
                 if (!$user->is_anonymous()) {
                     $newArtistID = $this->add_artist();
                     if ($newArtistID == -1) {
                         $errMessage = "Error when entering artist data.";
                         $this->theme->display_error($page, "Error", $errMessage);
                     } else {
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $newArtistID));
                     }
                 } else {
                     $errMessage = "You must be registered and logged in to create a new artist.";
                     $this->theme->display_error($page, "Error", $errMessage);
                 }
                 break;
             case "view":
                 $artistID = $event->get_arg(1);
                 $artist = $this->get_artist($artistID);
                 $aliases = $this->get_alias($artist['id']);
                 $members = $this->get_members($artist['id']);
                 $urls = $this->get_urls($artist['id']);
                 $userIsLogged = !$user->is_anonymous();
                 $userIsAdmin = $user->is_admin();
                 $images = Image::find_images(0, 4, Tag::explode($artist['name']));
                 $this->theme->show_artist($artist, $aliases, $members, $urls, $images, $userIsLogged, $userIsAdmin);
                 if ($userIsLogged) {
                     //$this->theme->show_new_alias_composer($artistID);
                     //$this->theme->show_new_member_composer($artistID);
                     //$this->theme->show_new_url_composer($artistID);
                 }
                 $this->theme->sidebar_options("editor", $artistID, $userIsAdmin);
                 break;
             case "edit":
                 $artistID = $event->get_arg(1);
                 $artist = $this->get_artist($artistID);
                 $aliases = $this->get_alias($artistID);
                 $members = $this->get_members($artistID);
                 $urls = $this->get_urls($artistID);
                 if (!$user->is_anonymous()) {
                     $this->theme->show_artist_editor($artist, $aliases, $members, $urls);
                     $userIsAdmin = $user->is_admin();
                     $this->theme->sidebar_options("editor", $artistID, $userIsAdmin);
                 } else {
                     $errMessage = "You must be registered and logged in to edit an artist.";
                     $this->theme->display_error($page, "Error", $errMessage);
                 }
                 break;
             case "edit_artist":
                 $artistID = $_POST['artist_id'];
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("artist/edit/" . $artistID));
                 break;
             case "edited":
                 $artistID = int_escape($_POST['id']);
                 $this->update_artist();
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("artist/view/" . $artistID));
                 break;
             case "nuke_artist":
                 $artistID = $_POST['artist_id'];
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("artist/nuke/" . $artistID));
                 break;
             case "nuke":
                 $artistID = $event->get_arg(1);
                 $this->delete_artist($artistID);
                 // this will delete the artist, it's alias, it's urls and it's members
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("artist/list"));
                 break;
             case "add_alias":
                 $artistID = $_POST['artist_id'];
                 $this->theme->show_new_alias_composer($artistID);
                 break;
             case "add_member":
                 $artistID = $_POST['artist_id'];
                 $this->theme->show_new_member_composer($artistID);
                 break;
             case "add_url":
                 $artistID = $_POST['artist_id'];
                 $this->theme->show_new_url_composer($artistID);
                 break;
                 //***********ALIAS SECTION ***********************
             //***********ALIAS SECTION ***********************
             case "alias":
                 switch ($event->get_arg(1)) {
                     case "add":
                         $artistID = $_POST['artistID'];
                         $this->add_alias();
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                     case "delete":
                         $aliasID = $event->get_arg(2);
                         $artistID = $this->get_artistID_by_aliasID($aliasID);
                         $this->delete_alias($aliasID);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                     case "edit":
                         $aliasID = int_escape($event->get_arg(2));
                         $alias = $this->get_alias_by_id($aliasID);
                         $this->theme->show_alias_editor($alias);
                         break;
                     case "edited":
                         $this->update_alias();
                         $aliasID = int_escape($_POST['aliasID']);
                         $artistID = $this->get_artistID_by_aliasID($aliasID);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                 }
                 break;
                 // case: alias
                 //**************** URLS SECTION **********************
             //**************** URLS SECTION **********************
             case "url":
                 switch ($event->get_arg(1)) {
                     case "add":
                         $artistID = $_POST['artistID'];
                         $this->add_urls();
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                     case "delete":
                         $urlID = $event->get_arg(2);
                         $artistID = $this->get_artistID_by_urlID($urlID);
                         $this->delete_url($urlID);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                     case "edit":
                         $urlID = int_escape($event->get_arg(2));
                         $url = $this->get_url_by_id($urlID);
                         $this->theme->show_url_editor($url);
                         break;
                     case "edited":
                         $this->update_url();
                         $urlID = int_escape($_POST['urlID']);
                         $artistID = $this->get_artistID_by_urlID($urlID);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                 }
                 break;
                 // case: urls
                 //******************* MEMBERS SECTION *********************
             //******************* MEMBERS SECTION *********************
             case "member":
                 switch ($event->get_arg(1)) {
                     case "add":
                         $artistID = $_POST['artistID'];
                         $this->add_members();
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                     case "delete":
                         $memberID = int_escape($event->get_arg(2));
                         $artistID = $this->get_artistID_by_memberID($memberID);
                         $this->delete_member($memberID);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                     case "edit":
                         $memberID = int_escape($event->get_arg(2));
                         $member = $this->get_member_by_id($memberID);
                         $this->theme->show_member_editor($member);
                         break;
                     case "edited":
                         $this->update_member();
                         $memberID = int_escape($_POST['memberID']);
                         $artistID = $this->get_artistID_by_memberID($memberID);
                         $page->set_mode("redirect");
                         $page->set_redirect(make_link("artist/view/" . $artistID));
                         break;
                 }
                 break;
                 //case: members
         }
     }
 }
Example #22
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 #23
0
 /**
  * @param string|string[] $tags
  * @param string $source
  */
 private function mass_source_edit($tags, $source)
 {
     $tags = Tag::explode($tags);
     $last_id = -1;
     while (true) {
         // make sure we don't look at the same images twice.
         // search returns high-ids first, so we want to look
         // at images with lower IDs than the previous.
         $search_forward = $tags;
         if ($last_id >= 0) {
             $search_forward[] = "id<{$last_id}";
         }
         $images = Image::find_images(0, 100, $search_forward);
         if (count($images) == 0) {
             break;
         }
         foreach ($images as $image) {
             $image->set_source($source);
             $last_id = $image->id;
         }
     }
 }