Merge pull request #756 from sanmadjack/pull

Pull
This commit is contained in:
Shish 2020-10-08 23:33:09 +01:00 committed by GitHub
commit 547c6d98da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 10 deletions

View File

@ -258,6 +258,20 @@ class Database
return $res;
}
/**
* Execute an SQL query and return the the first column => the second column as an iterable object.
*/
public function get_pairs_iterable(string $query, array $args = []): Generator
{
$_start = microtime(true);
$stmt = $this->execute($query, $args);
$this->count_time("get_pairs_iterable", $_start, $query, $args);
foreach ($stmt as $row) {
yield $row[0] => $row[1];
}
}
/**
* Execute an SQL query and return a single value, or null.
*/

View File

@ -61,7 +61,7 @@ abstract class Extension
return 50;
}
public static function determine_enabled_extensions()
public static function determine_enabled_extensions(): void
{
self::$enabled_extensions = [];
foreach (array_merge(
@ -138,6 +138,7 @@ abstract class ExtensionInfo
public $license;
public $version;
public $dependencies = [];
public $conflicts = [];
public $visibility;
public $description;
public $documentation;
@ -193,6 +194,13 @@ abstract class ExtensionInfo
if (!empty($this->db_support) && !in_array($database->get_driver_name(), $this->db_support)) {
$this->support_info .= "Database not supported. ";
}
if (!empty($this->conflicts)) {
$intersects = array_intersect($this->conflicts, Extension::get_enabled_extensions());
if (!empty($intersects)) {
$this->support_info .= "Conflicts with other extension(s): " . join(", ", $intersects);
}
}
// Additional checks here as needed
$this->supported = empty($this->support_info);

View File

@ -187,3 +187,27 @@ function create_scaled_image(string $inname, string $outname, array $tsize, stri
true
));
}
function redirect_to_next_image(Image $image): void
{
global $page;
if (isset($_GET['search'])) {
$search_terms = Tag::explode(Tag::decaret($_GET['search']));
$query = "search=" . url_escape($_GET['search']);
} else {
$search_terms = [];
$query = null;
}
$target_image = $image->get_next($search_terms);
if ($target_image == null) {
$redirect_target = referer_or(make_link("post/list"), ['post/view']);
} else {
$redirect_target = make_link("post/view/{$target_image->id}", null, $query);
}
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect($redirect_target);
}

View File

@ -23,4 +23,8 @@ abstract class ImageConfig
const COLLISION_MERGE = 'merge';
const COLLISION_ERROR = 'error';
const ON_DELETE = 'image_on_delete';
const ON_DELETE_NEXT = 'next';
const ON_DELETE_LIST = 'list';
}

View File

@ -15,6 +15,11 @@ class ImageIO extends Extension
'Merge'=>ImageConfig::COLLISION_MERGE
];
const ON_DELETE_OPTIONS = [
'Return to post list'=>ImageConfig::ON_DELETE_LIST,
'Go to next image'=>ImageConfig::ON_DELETE_NEXT
];
const EXIF_READ_FUNCTION = "exif_read_data";
const THUMBNAIL_ENGINES = [
@ -70,16 +75,22 @@ class ImageIO extends Extension
public function onPageRequest(PageRequestEvent $event)
{
global $config;
if ($event->page_matches("image/delete")) {
global $page, $user;
if ($user->can(Permissions::DELETE_IMAGE) && isset($_POST['image_id']) && $user->check_auth_token()) {
$image = Image::by_id(int_escape($_POST['image_id']));
if ($image) {
send_event(new ImageDeletionEvent($image));
if ($config->get_string(ImageConfig::ON_DELETE)===ImageConfig::ON_DELETE_NEXT) {
redirect_to_next_image($image);
} else {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link("post/list"), ['post/view']));
}
}
}
} elseif ($event->page_matches("image/replace")) {
global $page, $user;
if ($user->can(Permissions::REPLACE_IMAGE) && isset($_POST['image_id']) && $user->check_auth_token()) {
@ -254,6 +265,7 @@ class ImageIO extends Extension
$sb->add_text_option(ImageConfig::TIP, "Image tooltip", true);
$sb->add_text_option(ImageConfig::INFO, "Image info", true);
$sb->add_choice_option(ImageConfig::UPLOAD_COLLISION_HANDLER, self::COLLISION_OPTIONS, "Upload collision handler", true);
$sb->add_choice_option(ImageConfig::ON_DELETE, self::ON_DELETE_OPTIONS, "On Delete", true);
if (function_exists(self::EXIF_READ_FUNCTION)) {
$sb->add_bool_option(ImageConfig::SHOW_META, "Show metadata", true);
}

View File

@ -208,7 +208,7 @@ class Index extends Extension
} 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]));
} elseif (preg_match("/^(filename|name)[=|:]([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
} elseif (preg_match("/^(filename|name)[=|:](.+)$/i", $event->term, $matches)) {
$filename = strtolower($matches[2]);
$event->add_querylet(new Querylet("lower(images.filename) LIKE :filename{$this->stpen}", ["filename{$this->stpen}"=>"%$filename%"]));
} elseif (preg_match("/^(source)[=|:](.*)$/i", $event->term, $matches)) {

View File

@ -581,7 +581,7 @@ class Media extends Extension
$resize_suffix .= "\!";
}
$args = "";
$args = " -auto-orient ";
$resize_arg = "-resize";
if ($minimize) {
$args .= "-strip ";
@ -611,8 +611,8 @@ class Media extends Extension
case Media::RESIZE_TYPE_FIT_BLUR:
$blur_size = max(ceil(max($new_width, $new_height) / 25), 5);
$args .= "${file_arg} ".
"\( -clone 0 -resize ${new_width}x${new_height}\^ -background ${bg} -flatten -gravity center -fill black -colorize 50% -extent ${new_width}x${new_height} -blur 0x${blur_size} \) ".
"\( -clone 0 -resize ${new_width}x${new_height} \) ".
"\( -clone 0 -auto-orient -resize ${new_width}x${new_height}\^ -background ${bg} -flatten -gravity center -fill black -colorize 50% -extent ${new_width}x${new_height} -blur 0x${blur_size} \) ".
"\( -clone 0 -auto-orient -resize ${new_width}x${new_height} \) ".
"-delete 0 -gravity center -compose over -composite";
break;
}

View File

@ -131,15 +131,17 @@ class MimeType
if (($fh = @fopen($image_filename, 'rb'))) {
try {
//check if gif is animated (via https://www.php.net/manual/en/function.imagecreatefromgif.php#104473)
$chunk = false;
while (!feof($fh) && $is_anim_gif < 2) {
$chunk = fread($fh, 1024 * 100);
$is_anim_gif += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk, $matches);
$chunk = ($chunk ? substr($chunk, -20) : "") . fread($fh, 1024 * 100); //read 100kb at a time
$is_anim_gif += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}
} finally {
@fclose($fh);
}
}
return ($is_anim_gif == 0);
return ($is_anim_gif >=2);
}