2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class RegenThumb extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var RegenThumbTheme */
|
|
|
|
protected $theme;
|
|
|
|
|
|
|
|
public function regenerate_thumbnail($image, $force = true): bool
|
2019-06-05 23:03:22 +00:00
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache;
|
2019-06-09 18:22:48 +00:00
|
|
|
$event = new ThumbnailGenerationEvent($image->hash, $image->ext, $force);
|
|
|
|
send_event($event);
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->delete("thumb-block:{$image->id}");
|
2019-06-09 18:22:48 +00:00
|
|
|
return $event->generated;
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2019-10-02 10:23:57 +00:00
|
|
|
global $page, $user;
|
2007-06-30 01:19:11 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($event->page_matches("regen_thumb/one") && $user->can(Permissions::DELETE_IMAGE) && isset($_POST['image_id'])) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$image = Image::by_id(int_escape($_POST['image_id']));
|
2019-06-05 23:03:22 +00:00
|
|
|
|
|
|
|
$this->regenerate_thumbnail($image);
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->display_results($page, $image);
|
|
|
|
}
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($event->page_matches("regen_thumb/mass") && $user->can(Permissions::DELETE_IMAGE) && isset($_POST['tags'])) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$tags = Tag::explode(strtolower($_POST['tags']), false);
|
|
|
|
$images = Image::find_images(0, 10000, $tags);
|
2017-06-01 19:44:17 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
foreach ($images as $image) {
|
2019-06-05 23:03:22 +00:00
|
|
|
$this->regenerate_thumbnail($image);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2017-06-01 19:44:17 +00:00
|
|
|
|
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/list"));
|
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
|
|
|
|
{
|
|
|
|
global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::DELETE_IMAGE)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_part($this->theme->get_buttons_html($event->image->id));
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 19:44:17 +00:00
|
|
|
|
2019-06-05 23:03:22 +00:00
|
|
|
// public function onPostListBuilding(PostListBuildingEvent $event)
|
|
|
|
// {
|
|
|
|
// global $user;
|
2019-07-09 14:10:21 +00:00
|
|
|
// if ($user->can(UserAbilities::DELETE_IMAGE) && !empty($event->search_terms)) {
|
2019-06-05 23:03:22 +00:00
|
|
|
// $event->add_control($this->theme->mtr_html(Tag::implode($event->search_terms)));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2019-06-05 23:03:22 +00:00
|
|
|
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::DELETE_IMAGE)) {
|
2019-09-29 13:30:55 +00:00
|
|
|
$event->add_action("bulk_regen", "Regen Thumbnails", "", "", $this->theme->bulk_html());
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onBulkAction(BulkActionEvent $event)
|
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page, $user;
|
2019-06-05 23:03:22 +00:00
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
switch ($event->action) {
|
2019-06-12 22:44:25 +00:00
|
|
|
case "bulk_regen":
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::DELETE_IMAGE)) {
|
2019-06-09 18:22:48 +00:00
|
|
|
$force = true;
|
2019-06-14 12:47:50 +00:00
|
|
|
if (isset($_POST["bulk_regen_thumb_missing_only"])
|
|
|
|
&&$_POST["bulk_regen_thumb_missing_only"]=="true") {
|
2019-06-09 18:22:48 +00:00
|
|
|
$force=false;
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-12 22:44:25 +00:00
|
|
|
$total = 0;
|
2019-07-05 15:24:46 +00:00
|
|
|
foreach ($event->items as $image) {
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($this->regenerate_thumbnail($image, $force)) {
|
2019-06-12 22:44:25 +00:00
|
|
|
$total++;
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Regenerated thumbnails for $total items");
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAdminBuilding(AdminBuildingEvent $event)
|
|
|
|
{
|
|
|
|
$this->theme->display_admin_block();
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
public function onAdminAction(AdminActionEvent $event)
|
|
|
|
{
|
2019-12-15 19:47:18 +00:00
|
|
|
global $page;
|
2019-06-14 12:47:50 +00:00
|
|
|
switch ($event->action) {
|
2019-06-09 18:22:48 +00:00
|
|
|
case "regen_thumbs":
|
|
|
|
$event->redirect = true;
|
|
|
|
$force = false;
|
2019-06-14 12:47:50 +00:00
|
|
|
if (isset($_POST["regen_thumb_force"])&&$_POST["regen_thumb_force"]=="true") {
|
2019-06-09 18:22:48 +00:00
|
|
|
$force=true;
|
|
|
|
}
|
|
|
|
$limit = 1000;
|
2019-06-14 12:47:50 +00:00
|
|
|
if (isset($_POST["regen_thumb_limit"])&&is_numeric($_POST["regen_thumb_limit"])) {
|
2019-06-09 18:22:48 +00:00
|
|
|
$limit=intval($_POST["regen_thumb_limit"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = "";
|
2019-06-14 12:47:50 +00:00
|
|
|
if (isset($_POST["regen_thumb_limit"])) {
|
2019-06-09 18:22:48 +00:00
|
|
|
$type = $_POST["regen_thumb_type"];
|
|
|
|
}
|
|
|
|
$images = $this->get_images($type);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ($images as $image) {
|
2019-06-14 12:47:50 +00:00
|
|
|
if (!$force) {
|
2019-06-15 16:18:52 +00:00
|
|
|
$path = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"], false);
|
2019-06-14 12:47:50 +00:00
|
|
|
if (file_exists($path)) {
|
2019-06-09 18:22:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$event = new ThumbnailGenerationEvent($image["hash"], $image["ext"], $force);
|
|
|
|
send_event($event);
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($event->generated) {
|
2019-06-09 18:22:48 +00:00
|
|
|
$i++;
|
|
|
|
}
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($i>=$limit) {
|
2019-06-09 18:22:48 +00:00
|
|
|
break;
|
2019-06-05 23:03:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Re-generated $i thumbnails");
|
2019-06-05 23:03:22 +00:00
|
|
|
break;
|
2019-06-09 18:22:48 +00:00
|
|
|
case "delete_thumbs":
|
|
|
|
$event->redirect = true;
|
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
if (isset($_POST["delete_thumb_type"])&&$_POST["delete_thumb_type"]!="") {
|
2019-06-09 18:22:48 +00:00
|
|
|
$images = $this->get_images($_POST["delete_thumb_type"]);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-06-09 18:22:48 +00:00
|
|
|
$i = 0;
|
|
|
|
foreach ($images as $image) {
|
2019-06-15 16:18:52 +00:00
|
|
|
$outname = warehouse_path(Image::THUMBNAIL_DIR, $image["hash"]);
|
2019-06-14 12:47:50 +00:00
|
|
|
if (file_exists($outname)) {
|
2019-06-09 18:22:48 +00:00
|
|
|
unlink($outname);
|
|
|
|
$i++;
|
2019-06-14 12:47:50 +00:00
|
|
|
}
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Deleted $i thumbnails for ".$_POST["delete_thumb_type"]." images");
|
2019-06-09 18:22:48 +00:00
|
|
|
} else {
|
|
|
|
$dir = "data/thumbs/";
|
|
|
|
$this->remove_dir_recursively($dir);
|
2019-12-15 19:47:18 +00:00
|
|
|
$page->flash("Deleted all thumbnails");
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
public function get_images(String $ext = null)
|
2019-06-09 18:22:48 +00:00
|
|
|
{
|
|
|
|
global $database;
|
|
|
|
|
|
|
|
$query = "SELECT hash, ext FROM images";
|
|
|
|
$args = [];
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($ext!=null&&$ext!="") {
|
2019-06-09 18:22:48 +00:00
|
|
|
$query .= " WHERE ext = :ext";
|
|
|
|
$args["ext"] = $ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $database->get_all($query, $args);
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:47:50 +00:00
|
|
|
public function remove_dir_recursively($dir)
|
2019-06-09 18:22:48 +00:00
|
|
|
{
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
$objects = scandir($dir);
|
|
|
|
foreach ($objects as $object) {
|
2019-06-14 12:47:50 +00:00
|
|
|
if ($object != "." && $object != "..") {
|
2019-06-09 18:22:48 +00:00
|
|
|
if (filetype($dir."/".$object) == "dir") {
|
2019-06-14 12:47:50 +00:00
|
|
|
$this->remove_dir_recursively($dir."/".$object);
|
2019-06-09 18:22:48 +00:00
|
|
|
} else {
|
2019-06-14 12:47:50 +00:00
|
|
|
unlink($dir."/".$object);
|
2019-06-09 18:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reset($objects);
|
|
|
|
rmdir($dir);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|