This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/rule34/main.php

159 lines
5.5 KiB
PHP
Raw Normal View History

2020-01-26 13:19:35 +00:00
<?php declare(strict_types=1);
2018-07-19 18:55:28 +00:00
use function MicroHTML\TR;
use function MicroHTML\TH;
use function MicroHTML\TD;
use function MicroHTML\A;
if ( // kill these glitched requests immediately
2020-01-26 16:43:41 +00:00
!empty($_SERVER["REQUEST_URI"])
&& str_contains(@$_SERVER["REQUEST_URI"], "/http")
&& str_contains(@$_SERVER["REQUEST_URI"], "paheal.net")
) {
die("No");
}
class Rule34 extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var Rule34Theme */
protected $theme;
public function onImageDeletion(ImageDeletionEvent $event)
{
global $database;
2020-10-03 12:54:27 +00:00
$database->notify("shm_image_bans", $event->image->hash);
}
public function onImageInfoSet(ImageInfoSetEvent $event)
{
global $cache;
$cache->delete("thumb-block:{$event->image->id}");
}
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event)
{
global $config;
$image_link = $config->get_string(ImageConfig::ILINK);
$url0 = $event->image->parse_link_template($image_link, 0);
$url1 = $event->image->parse_link_template($image_link, 1);
$html = (string)TR(
TH("Links"),
TD(
2020-10-26 15:23:41 +00:00
A(["href"=>$url0], "File Only"),
" (",
A(["href"=>$url1], "Backup Server"),
")"
)
);
$event->add_part($html, 90);
}
public function onAdminBuilding(AdminBuildingEvent $event)
{
global $page;
$html = make_form(make_link("admin/cache_purge"), "POST");
$html .= "<textarea type='text' name='hash' placeholder='Enter image URL or hash' cols='80' rows='5'></textarea>";
$html .= "<br><input type='submit' value='Purge from caches'>";
$html .= "</form>\n";
$page->add_block(new Block("Cache Purger", $html));
}
public function onUserPageBuilding(UserPageBuildingEvent $event)
{
global $database, $user, $config;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::CHANGE_SETTING) && $config->get_bool('r34_comic_integration')) {
2019-11-27 11:22:46 +00:00
$current_state = bool_escape($database->get_one("SELECT comic_admin FROM users WHERE id=:id", ['id'=>$event->display_user->id]));
$this->theme->show_comic_changer($event->display_user, $current_state);
}
}
public function onThumbnailGeneration(ThumbnailGenerationEvent $event)
{
global $database, $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::MANAGE_ADMINTOOLS)) {
2021-02-12 20:07:44 +00:00
#$database->notify("shm_image_bans", $event->hash);
}
}
public function onCommand(CommandEvent $event)
{
2020-02-09 00:32:53 +00:00
global $cache;
if ($event->cmd == "wipe-thumb-cache") {
foreach (Image::find_images_iterable(0, null, Tag::explode($event->args[0])) as $image) {
print($image->id . "\n");
$cache->delete("thumb-block:{$image->id}");
}
}
}
2019-08-26 23:09:35 +00:00
public function onSourceSet(SourceSetEvent $event)
{
// Maybe check for 404?
if (empty($event->source)) {
return;
}
if (!preg_match("/^(https?:\/\/)?[a-zA-Z0-9\.\-]+(\/.*)?$/", $event->source)) {
throw new SCoreException("Invalid source URL");
}
}
public function onPageRequest(PageRequestEvent $event)
{
global $database, $page, $user;
$database->set_timeout(DATABASE_TIMEOUT+15000); // deleting users can take a while
if (function_exists("sd_notify_watchdog")) {
sd_notify_watchdog();
}
if ($event->page_matches("rule34/comic_admin")) {
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::CHANGE_SETTING) && $user->check_auth_token()) {
$input = validate_input([
'user_id' => 'user_id,exists',
'is_admin' => 'bool',
]);
$database->execute(
2019-11-27 11:22:46 +00:00
'UPDATE users SET comic_admin=:is_admin WHERE id=:id',
['is_admin'=>$input['is_admin'] ? 't' : 'f', 'id'=>$input['user_id']]
);
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
2020-03-27 19:41:34 +00:00
$page->set_redirect(referer_or(make_link()));
}
}
if ($event->page_matches("tnc_agreed")) {
setcookie("ui-tnc-agreed", "true", 0, "/");
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
2020-03-27 19:41:34 +00:00
$page->set_redirect(referer_or("/"));
}
if ($event->page_matches("admin/cache_purge")) {
2019-07-09 14:10:21 +00:00
if (!$user->can(Permissions::MANAGE_ADMINTOOLS)) {
$this->theme->display_permission_denied();
} else {
if ($user->check_auth_token()) {
$all = $_POST["hash"];
$matches = [];
if (preg_match_all("/([a-fA-F0-9]{32})/", $all, $matches)) {
$matches = $matches[0];
foreach ($matches as $hash) {
$page->flash("Cleaning {$hash}");
if (strlen($hash) != 32) {
continue;
}
log_info("admin", "Cleaning {$hash}");
@unlink(warehouse_path(Image::IMAGE_DIR, $hash));
@unlink(warehouse_path(Image::THUMBNAIL_DIR, $hash));
2020-10-03 12:54:27 +00:00
$database->notify("shm_image_bans", $hash);
}
}
}
2019-06-19 01:58:28 +00:00
$page->set_mode(PageMode::REDIRECT);
2019-06-14 12:16:58 +00:00
$page->set_redirect(make_link("admin"));
}
}
}
2018-07-19 18:55:28 +00:00
}