2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2010-01-04 12:41:04 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-01-11 00:55:05 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\{InputInterface,InputArgument};
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2019-08-16 14:18:14 +00:00
|
|
|
require_once "config.php";
|
|
|
|
require_once "events.php";
|
2007-07-17 18:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Index extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var IndexTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2019-08-16 14:18:14 +00:00
|
|
|
$config->set_default_int(IndexConfig::IMAGES, 24);
|
|
|
|
$config->set_default_bool(IndexConfig::TIPS, true);
|
|
|
|
$config->set_default_string(IndexConfig::ORDER, "id DESC");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-07-10 22:33:49 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-12-14 16:33:21 +00:00
|
|
|
global $cache, $config, $page, $user;
|
2024-02-11 11:34:09 +00:00
|
|
|
if (
|
|
|
|
$event->page_matches("post/list", paged: true)
|
|
|
|
|| $event->page_matches("post/list/{search}", paged: true)
|
|
|
|
) {
|
2024-02-09 16:36:57 +00:00
|
|
|
if ($event->get_GET('search')) {
|
2023-08-18 12:38:55 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2024-02-09 16:36:57 +00:00
|
|
|
$page->set_redirect(search_link(Tag::explode($event->get_GET('search'), false)));
|
2019-05-28 16:59:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-07-19 11:46:43 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
$search_terms = Tag::explode($event->get_arg('search', ""), false);
|
2019-05-28 16:59:38 +00:00
|
|
|
$count_search_terms = count($search_terms);
|
2024-02-11 11:34:09 +00:00
|
|
|
$page_number = $event->get_iarg('page_num', 1);
|
|
|
|
$page_size = $config->get_int(IndexConfig::IMAGES);
|
2015-12-04 11:38:44 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
try {
|
2020-02-04 22:44:27 +00:00
|
|
|
$fast_page_limit = 500;
|
|
|
|
|
2020-02-08 11:55:06 +00:00
|
|
|
$ua = $_SERVER["HTTP_USER_AGENT"] ?? "No UA";
|
2020-02-06 03:10:30 +00:00
|
|
|
if (
|
2020-02-06 12:49:11 +00:00
|
|
|
SPEED_HAX
|
|
|
|
&& (
|
2020-10-25 19:31:58 +00:00
|
|
|
str_contains($ua, "Googlebot")
|
|
|
|
|| str_contains($ua, "YandexBot")
|
|
|
|
|| str_contains($ua, "bingbot")
|
|
|
|
|| str_contains($ua, "msnbot")
|
2020-02-06 12:49:11 +00:00
|
|
|
)
|
2020-02-06 03:10:30 +00:00
|
|
|
&& (
|
|
|
|
$count_search_terms > 1
|
|
|
|
|| ($count_search_terms == 1 && $search_terms[0][0] == "-")
|
|
|
|
)
|
2020-02-06 02:59:44 +00:00
|
|
|
) {
|
2020-02-06 12:49:11 +00:00
|
|
|
// bots love searching for weird combinations of tags...
|
2020-02-06 03:11:21 +00:00
|
|
|
$fast_page_limit = 10;
|
2020-02-06 02:59:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 22:44:27 +00:00
|
|
|
if (SPEED_HAX && $page_number > $fast_page_limit && !$user->can("big_search")) {
|
|
|
|
$this->theme->display_error(
|
|
|
|
404,
|
|
|
|
"Search limit hit",
|
|
|
|
"Only $fast_page_limit pages of results are searchable - " .
|
|
|
|
"if you want to find older results, use more specific search terms"
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-14 16:33:21 +00:00
|
|
|
$total_pages = (int)ceil(Search::count_images($search_terms) / $config->get_int(IndexConfig::IMAGES));
|
2020-02-04 22:44:27 +00:00
|
|
|
if (SPEED_HAX && $total_pages > $fast_page_limit && !$user->can("big_search")) {
|
|
|
|
$total_pages = $fast_page_limit;
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:08:59 +00:00
|
|
|
$images = null;
|
2019-07-07 18:23:48 +00:00
|
|
|
if (SPEED_HAX) {
|
2019-07-19 09:24:17 +00:00
|
|
|
if ($count_search_terms === 0 && ($page_number < 10)) {
|
2019-07-07 18:23:48 +00:00
|
|
|
// extra caching for the first few post/list pages
|
2023-12-14 17:06:54 +00:00
|
|
|
$images = cache_get_or_set(
|
|
|
|
"post-list:$page_number",
|
|
|
|
fn () => Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms),
|
|
|
|
60
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-07-07 18:23:48 +00:00
|
|
|
}
|
2024-01-15 13:08:59 +00:00
|
|
|
if (is_null($images)) {
|
2023-12-14 16:33:21 +00:00
|
|
|
$images = Search::find_images(($page_number - 1) * $page_size, $page_size, $search_terms);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2021-11-10 19:33:51 +00:00
|
|
|
} catch (PermissionDeniedException $pde) {
|
|
|
|
$this->theme->display_error(403, "Permission denied", $pde->error);
|
|
|
|
$total_pages = 0;
|
|
|
|
$images = [];
|
2019-05-28 16:59:38 +00:00
|
|
|
} catch (SearchTermParseException $stpe) {
|
2021-11-10 19:33:51 +00:00
|
|
|
$this->theme->display_error(400, "Malformed search query", $stpe->error);
|
2019-05-28 16:59:38 +00:00
|
|
|
$total_pages = 0;
|
|
|
|
$images = [];
|
|
|
|
}
|
2007-07-17 18:07:18 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$count_images = count($images);
|
2015-12-04 11:38:44 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($count_search_terms === 0 && $count_images === 0 && $page_number === 1) {
|
|
|
|
$this->theme->display_intro($page);
|
|
|
|
send_event(new PostListBuildingEvent($search_terms));
|
|
|
|
} elseif ($count_search_terms > 0 && $count_images === 1 && $page_number === 1) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link('post/view/'.$images[0]->id));
|
|
|
|
} else {
|
2023-02-04 20:50:26 +00:00
|
|
|
$plbe = send_event(new PostListBuildingEvent($search_terms));
|
2009-04-22 05:41:49 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Index Options");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->position = 20;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label("Show ");
|
2019-08-16 14:18:14 +00:00
|
|
|
$sb->add_int_option(IndexConfig::IMAGES);
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_label(" images on the post list");
|
|
|
|
}
|
2008-02-06 17:24:08 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageNavBuilding(PageNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
$event->add_nav_link("posts", new Link('post/list'), "Posts", NavLink::is_active(["post","view"]), 20);
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->parent == "posts") {
|
2019-08-02 19:54:48 +00:00
|
|
|
$event->add_nav_link("posts_all", new Link('post/list'), "All");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onHelpPageBuilding(HelpPageBuildingEvent $event): void
|
2019-08-02 20:05:49 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($event->key === HelpPages::SEARCH) {
|
2023-07-10 18:04:23 +00:00
|
|
|
$event->add_block(new Block("General", $this->theme->get_help_html()), 0);
|
2019-08-02 20:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onCliGen(CliGenEvent $event): void
|
2019-10-04 19:48:59 +00:00
|
|
|
{
|
2024-01-11 00:55:05 +00:00
|
|
|
$event->app->register('search')
|
|
|
|
->addArgument('query', InputArgument::REQUIRED)
|
|
|
|
->setDescription('Search the database and print results')
|
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output): int {
|
|
|
|
$query = Tag::explode($input->getArgument('query'));
|
|
|
|
$items = Search::find_images(limit: 1000, tags: $query);
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$output->writeln($item->hash);
|
|
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
|
|
});
|
2019-10-04 19:48:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSearchTermParse(SearchTermParseEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-26 16:38:26 +00:00
|
|
|
if (is_null($event->term)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 13:19:35 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$matches = [];
|
|
|
|
// check for tags first as tag based searches are more common.
|
|
|
|
if (preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
|
|
|
$count = $matches[2];
|
|
|
|
$event->add_querylet(
|
|
|
|
new Querylet("EXISTS (
|
2015-12-04 11:38:44 +00:00
|
|
|
SELECT 1
|
|
|
|
FROM image_tags it
|
|
|
|
LEFT JOIN tags t ON it.tag_id = t.id
|
|
|
|
WHERE images.id = it.image_id
|
|
|
|
GROUP BY image_id
|
|
|
|
HAVING COUNT(*) $cmp $count
|
|
|
|
)")
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
} elseif (preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = preg_replace('/^:/', '=', $matches[1]);
|
2023-11-11 21:49:12 +00:00
|
|
|
$args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])];
|
2020-10-25 12:32:10 +00:00
|
|
|
$event->add_querylet(new Querylet("width / :width{$event->id} $cmp height / :height{$event->id}", $args));
|
2024-01-04 16:52:30 +00:00
|
|
|
} elseif (preg_match("/^filesize([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+[kmg]?b?)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
|
|
|
$val = parse_shorthand_int($matches[2]);
|
|
|
|
$event->add_querylet(new Querylet("images.filesize $cmp :val{$event->id}", ["val{$event->id}" => $val]));
|
|
|
|
} elseif (preg_match("/^id=([\d,]+)$/i", $event->term, $matches)) {
|
|
|
|
$val = array_map(fn ($x) => int_escape($x), explode(",", $matches[1]));
|
|
|
|
$set = implode(",", $val);
|
|
|
|
$event->add_querylet(new Querylet("images.id IN ($set)"));
|
|
|
|
} elseif (preg_match("/^id([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
|
|
|
$val = int_escape($matches[2]);
|
|
|
|
$event->add_querylet(new Querylet("images.id $cmp :val{$event->id}", ["val{$event->id}" => $val]));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (preg_match("/^(hash|md5)[=|:]([0-9a-fA-F]*)$/i", $event->term, $matches)) {
|
|
|
|
$hash = strtolower($matches[2]);
|
|
|
|
$event->add_querylet(new Querylet('images.hash = :hash', ["hash" => $hash]));
|
|
|
|
} elseif (preg_match("/^(phash)[=|:]([0-9a-fA-F]*)$/i", $event->term, $matches)) {
|
|
|
|
$phash = strtolower($matches[2]);
|
|
|
|
$event->add_querylet(new Querylet('images.phash = :phash', ["phash" => $phash]));
|
2020-10-08 22:07:53 +00:00
|
|
|
} elseif (preg_match("/^(filename|name)[=|:](.+)$/i", $event->term, $matches)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$filename = strtolower($matches[2]);
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("lower(images.filename) LIKE :filename{$event->id}", ["filename{$event->id}" => "%$filename%"]));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (preg_match("/^(source)[=|:](.*)$/i", $event->term, $matches)) {
|
|
|
|
$source = strtolower($matches[2]);
|
2014-01-14 06:27:12 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if (preg_match("/^(any|none)$/i", $source)) {
|
|
|
|
$not = ($source == "any" ? "NOT" : "");
|
|
|
|
$event->add_querylet(new Querylet("images.source IS $not NULL"));
|
|
|
|
} else {
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet('images.source LIKE :src', ["src" => "%$source%"]));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
} elseif (preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/i", $event->term, $matches)) {
|
2019-08-02 20:05:49 +00:00
|
|
|
// TODO Make this able to search = without needing a time component.
|
2019-05-28 16:59:38 +00:00
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
|
|
|
$val = $matches[2];
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("images.posted $cmp :posted{$event->id}", ["posted{$event->id}" => $val]));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
2023-11-11 21:49:12 +00:00
|
|
|
$args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])];
|
2020-10-25 12:32:10 +00:00
|
|
|
$event->add_querylet(new Querylet("width $cmp :width{$event->id} AND height $cmp :height{$event->id}", $args));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("width $cmp :width{$event->id}", ["width{$event->id}" => int_escape($matches[2])]));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("height $cmp :height{$event->id}", ["height{$event->id}" => int_escape($matches[2])]));
|
2020-10-26 13:45:25 +00:00
|
|
|
} elseif (preg_match("/^length([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(.+)$/i", $event->term, $matches)) {
|
|
|
|
$value = parse_to_milliseconds($matches[2]);
|
|
|
|
$cmp = ltrim($matches[1], ":") ?: "=";
|
2023-11-11 21:49:12 +00:00
|
|
|
$event->add_querylet(new Querylet("length $cmp :length{$event->id}", ["length{$event->id}" => $value]));
|
2020-02-09 00:32:38 +00:00
|
|
|
} elseif (preg_match("/^order[=|:](id|width|height|length|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$ord = strtolower($matches[1]);
|
|
|
|
$default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC";
|
|
|
|
$sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column;
|
2020-10-25 12:55:13 +00:00
|
|
|
$event->order = "images.$ord $sort";
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif (preg_match("/^order[=|:]random[_]([0-9]{1,4})$/i", $event->term, $matches)) {
|
2020-05-13 12:03:49 +00:00
|
|
|
// requires a seed to avoid duplicates
|
|
|
|
// since the tag can't be changed during the parseevent, we instead generate the seed during submit using js
|
2019-05-28 16:59:38 +00:00
|
|
|
$seed = $matches[1];
|
2020-10-25 12:55:13 +00:00
|
|
|
$event->order = "RAND($seed)";
|
2020-05-13 12:03:49 +00:00
|
|
|
} elseif (preg_match("/^order[=|:]dailyshuffle$/i", $event->term, $matches)) {
|
|
|
|
// will use today's date as seed, thus allowing for a dynamic randomized list without outside intervention.
|
|
|
|
// This way the list will change every day, giving a more dynamic feel to the imageboard.
|
|
|
|
// recommended to change homepage to "post/list/order:dailyshuffle/1"
|
|
|
|
$seed = date("Ymd");
|
2020-10-25 12:55:13 +00:00
|
|
|
$event->order = "RAND($seed)";
|
2020-05-13 12:03:49 +00:00
|
|
|
}
|
2023-05-25 10:26:31 +00:00
|
|
|
|
|
|
|
// If we've reached this far, and nobody else has done anything with this term, then treat it as a tag
|
2023-06-27 16:45:35 +00:00
|
|
|
if ($event->order === null && $event->img_conditions == [] && $event->tag_conditions == []) {
|
2023-05-25 10:26:31 +00:00
|
|
|
$event->add_tag_condition(new TagCondition($event->term, $event->positive));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
// we want to turn a search term into a TagCondition only if nobody did anything else with that term
|
|
|
|
return 95;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|