Mime type handling overhaul

Changed mime type map to deal with the reality that certain file types have multiple extensions and/or multiple mime types, as well as constants supporting all of the data. Created new functions using the updated mime type map to resolve mime types and extensions. Updated various items around the project that determine mime/extension to take advantage of the new functions.
This commit is contained in:
Matthew Barbour 2020-05-28 10:05:20 -05:00 committed by Shish
parent 16c58e266b
commit 63b2601e67
41 changed files with 575 additions and 270 deletions

View file

@ -53,7 +53,7 @@ class BaseThemelet
$h_tip = html_escape($image->get_tooltip());
$h_tags = html_escape(strtolower($image->get_tag_list()));
$extArr = array_flip(['swf', 'svg', 'mp3']); //List of thumbless filetypes
$extArr = array_flip([EXTENSION_FLASH, EXTENSION_SVG, EXTENSION_MP3]); //List of thumbless filetypes
if (!isset($extArr[$image->ext])) {
$tsize = get_thumbnail_size($image->width, $image->height);
} else {

View file

@ -275,7 +275,7 @@ abstract class FormatterExtension extends Extension
*/
abstract class DataHandlerExtension extends Extension
{
protected $SUPPORTED_EXT = [];
protected $SUPPORTED_MIME = [];
protected function move_upload_to_archive(DataUploadEvent $event)
{
@ -403,7 +403,7 @@ abstract class DataHandlerExtension extends Extension
$image->hash = $metadata['hash'];
$image->filename = (($pos = strpos($metadata['filename'], '?')) !== false) ? substr($metadata['filename'], 0, $pos) : $metadata['filename'];
if ($config->get_bool("upload_use_mime")) {
$image->ext = get_extension(getMimeType($filename));
$image->ext = get_extension_for_file($filename);
} else {
$image->ext = (($pos = strpos($metadata['extension'], '?')) !== false) ? substr($metadata['extension'], 0, $pos) : $metadata['extension'];
}
@ -419,15 +419,20 @@ abstract class DataHandlerExtension extends Extension
protected function supported_ext(string $ext): bool
{
return in_array(strtolower($ext), $this->SUPPORTED_EXT);
return in_array(get_mime_for_extension($ext), $this->SUPPORTED_MIME);
}
public static function get_all_supported_exts(): array
{
$arr = [];
foreach (getSubclassesOf("DataHandlerExtension") as $handler) {
$arr = array_merge($arr, (new $handler())->SUPPORTED_EXT);
$handler = (new $handler());
foreach($handler->SUPPORTED_MIME as $mime) {
$arr = array_merge($arr, get_all_extension_for_mime($mime));
}
}
$arr = array_unique($arr);
return $arr;
}
}

409
core/filetypes.php Normal file
View file

@ -0,0 +1,409 @@
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* MIME types and extension information and resolvers *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
class MimeTypeMap {
}
const EXTENSION_ANI = 'ani';
const EXTENSION_ASC = 'asc';
const EXTENSION_ASF = 'asf';
const EXTENSION_AVI = 'avi';
const EXTENSION_BMP = 'bmp';
const EXTENSION_BZIP = 'bz';
const EXTENSION_BZIP2 = 'bz2';
const EXTENSION_CBR = 'cbr';
const EXTENSION_CBZ = 'cbz';
const EXTENSION_CBT = 'cbt';
const EXTENSION_CBA = 'cbA';
const EXTENSION_CB7 = 'cb7';
const EXTENSION_CSS = 'css';
const EXTENSION_CSV = 'csv';
const EXTENSION_CUR = 'cur';
const EXTENSION_FLASH = 'swf';
const EXTENSION_FLASH_VIDEO = 'flv';
const EXTENSION_GIF = 'gif';
const EXTENSION_GZIP = 'gz';
const EXTENSION_HTML = 'html';
const EXTENSION_HTM = 'htm';
const EXTENSION_ICO = 'ico';
const EXTENSION_JFIF = 'jfif';
const EXTENSION_JFI = 'jfi';
const EXTENSION_JPEG = 'jpeg';
const EXTENSION_JPG = 'jpg';
const EXTENSION_JS = 'js';
const EXTENSION_JSON = 'json';
const EXTENSION_MKV = 'mkv';
const EXTENSION_MP3 = 'mp3';
const EXTENSION_MP4 = 'mp4';
const EXTENSION_M4V = 'm4v';
const EXTENSION_M4A = 'm4a';
const EXTENSION_MPEG = 'mpeg';
const EXTENSION_MPG = 'mpg';
const EXTENSION_OGG = 'ogg';
const EXTENSION_OGG_VIDEO = 'ogv';
const EXTENSION_OGG_AUDIO = 'oga';
const EXTENSION_PDF = 'pdf';
const EXTENSION_PHP = 'php';
const EXTENSION_PHP5 = 'php5';
const EXTENSION_PNG = 'png';
const EXTENSION_PSD = 'psd';
const EXTENSION_MOV = 'mov';
const EXTENSION_RSS = 'rss';
const EXTENSION_SVG = 'svg';
const EXTENSION_TAR = 'tar';
const EXTENSION_TEXT = 'txt';
const EXTENSION_TIFF = 'tiff';
const EXTENSION_TIF = 'tif';
const EXTENSION_WAV = 'wav';
const EXTENSION_WEBM = 'webm';
const EXTENSION_WEBP = 'webp';
const EXTENSION_WMA = 'wma';
const EXTENSION_WMV = 'wmv';
const EXTENSION_XML = 'xml';
const EXTENSION_XSL = 'xsl';
const EXTENSION_ZIP = 'zip';
// Couldn't find a mimetype for ani, so made one up based on it being a riff container
const MIME_TYPE_ANI = 'application/riff+ani';
const MIME_TYPE_ASF = 'video/x-ms-asf';
const MIME_TYPE_AVI = 'video/x-msvideo';
// Went with mime types from http://fileformats.archiveteam.org/wiki/Comic_Book_Archive
const MIME_TYPE_COMIC_ZIP = 'application/vnd.comicbook+zip';
const MIME_TYPE_COMIC_RAR = 'application/vnd.comicbook-rar';
const MIME_TYPE_BMP = 'image/x-ms-bmp';
const MIME_TYPE_BZIP = 'application/x-bzip';
const MIME_TYPE_BZIP2 = 'application/x-bzip2';
const MIME_TYPE_CSS = 'text/css';
const MIME_TYPE_CSV = 'text/csv';
const MIME_TYPE_FLASH = 'application/x-shockwave-flash';
const MIME_TYPE_FLASH_VIDEO = 'video/x-flv';
const MIME_TYPE_GIF = 'image/gif';
const MIME_TYPE_GZIP = 'application/x-gzip';
const MIME_TYPE_HTML = 'text/html';
const MIME_TYPE_ICO = 'image/x-icon';
const MIME_TYPE_JPEG = 'image/jpeg';
const MIME_TYPE_JS = 'text/javascript';
const MIME_TYPE_JSON = 'application/json';
const MIME_TYPE_MKV = 'video/x-matroska';
const MIME_TYPE_MP3 = 'audio/mpeg';
const MIME_TYPE_MP4_AUDIO = 'audio/mp4';
const MIME_TYPE_MP4_VIDEO = 'video/mp4';
const MIME_TYPE_MPEG = 'video/mpeg';
const MIME_TYPE_OCTET_STREAM = 'application/octet-stream';
const MIME_TYPE_OGG = 'application/ogg';
const MIME_TYPE_OGG_VIDEO = 'video/ogg';
const MIME_TYPE_OGG_AUDIO = 'audio/ogg';
const MIME_TYPE_PDF = 'application/pdf';
const MIME_TYPE_PHP = 'text/x-php';
const MIME_TYPE_PNG = 'image/png';
const MIME_TYPE_PSD = 'image/vnd.adobe.photoshop';
const MIME_TYPE_QUICKTIME = 'video/quicktime';
const MIME_TYPE_RSS = 'application/rss+xml';
const MIME_TYPE_SVG = 'image/svg+xml';
const MIME_TYPE_TAR = 'application/x-tar';
const MIME_TYPE_TEXT = 'text/plain';
const MIME_TYPE_TIFF = 'image/tiff';
const MIME_TYPE_WAV = 'audio/x-wav';
const MIME_TYPE_WEBM = 'video/webm';
const MIME_TYPE_WEBP = 'image/webp';
const MIME_TYPE_WIN_BITMAP = 'image/x-win-bitmap';
const MIME_TYPE_XML = 'text/xml';
const MIME_TYPE_XML_APPLICATION = 'application/xml';
const MIME_TYPE_XSL = 'application/xsl+xml';
const MIME_TYPE_ZIP = 'application/zip';
const MIME_TYPE_MAP_EXT = 'ext';
const MIME_TYPE_MAP_MIME = 'mime';
// Mime type map. Each entry in the MIME_TYPE_ARRAY represents a kind of file, identified by the "correct" mimetype as the key.
// The value for each entry is a map of twokeys, ext and mime.
// ext's value is an array of all of the extensions that the file type can use, with the "correct" one being first.
// mime's value is an array of all mime types that the file type is known to use, with the current "correct" one being first.
const MIME_TYPE_MAP = [
MIME_TYPE_ANI => [
MIME_TYPE_MAP_EXT => [EXTENSION_ANI],
MIME_TYPE_MAP_MIME => [MIME_TYPE_ANI],
],
MIME_TYPE_AVI => [
MIME_TYPE_MAP_EXT => [EXTENSION_AVI],
MIME_TYPE_MAP_MIME => [MIME_TYPE_AVI,'video/avi','video/msvideo'],
],
MIME_TYPE_ASF => [
MIME_TYPE_MAP_EXT => [EXTENSION_ASF,EXTENSION_WMA,EXTENSION_WMV],
MIME_TYPE_MAP_MIME => [MIME_TYPE_ASF,'audio/x-ms-wma','video/x-ms-wmv'],
],
MIME_TYPE_BMP => [
MIME_TYPE_MAP_EXT => [EXTENSION_BMP],
MIME_TYPE_MAP_MIME => [MIME_TYPE_BMP],
],
MIME_TYPE_BZIP => [
MIME_TYPE_MAP_EXT => [EXTENSION_BZIP],
MIME_TYPE_MAP_MIME => [MIME_TYPE_BZIP],
],
MIME_TYPE_BZIP2 => [
MIME_TYPE_MAP_EXT => [EXTENSION_BZIP2],
MIME_TYPE_MAP_MIME => [MIME_TYPE_BZIP2],
],
MIME_TYPE_COMIC_ZIP => [
MIME_TYPE_MAP_EXT => [EXTENSION_CBZ],
MIME_TYPE_MAP_MIME => [MIME_TYPE_COMIC_ZIP],
],
MIME_TYPE_CSS => [
MIME_TYPE_MAP_EXT => [EXTENSION_CSS],
MIME_TYPE_MAP_MIME => [MIME_TYPE_CSS],
],
MIME_TYPE_CSV => [
MIME_TYPE_MAP_EXT => [EXTENSION_CSV],
MIME_TYPE_MAP_MIME => [MIME_TYPE_CSV],
],
MIME_TYPE_FLASH => [
MIME_TYPE_MAP_EXT => [EXTENSION_FLASH],
MIME_TYPE_MAP_MIME => [MIME_TYPE_FLASH],
],
MIME_TYPE_FLASH_VIDEO => [
MIME_TYPE_MAP_EXT => [EXTENSION_FLASH_VIDEO],
MIME_TYPE_MAP_MIME => [MIME_TYPE_FLASH_VIDEO,'video/flv'],
],
MIME_TYPE_GIF => [
MIME_TYPE_MAP_EXT => [EXTENSION_GIF],
MIME_TYPE_MAP_MIME => [MIME_TYPE_GIF],
],
MIME_TYPE_GZIP => [
MIME_TYPE_MAP_EXT => [EXTENSION_GZIP],
MIME_TYPE_MAP_MIME => [MIME_TYPE_TAR],
],
MIME_TYPE_HTML => [
MIME_TYPE_MAP_EXT => [EXTENSION_HTM, EXTENSION_HTML],
MIME_TYPE_MAP_MIME => [MIME_TYPE_HTML],
],
MIME_TYPE_ICO => [
MIME_TYPE_MAP_EXT => [EXTENSION_ICO, EXTENSION_CUR],
MIME_TYPE_MAP_MIME => [MIME_TYPE_ICO, MIME_TYPE_WIN_BITMAP],
],
MIME_TYPE_JPEG => [
MIME_TYPE_MAP_EXT => [EXTENSION_JPG, EXTENSION_JPEG, EXTENSION_JFIF, EXTENSION_JFI],
MIME_TYPE_MAP_MIME => [MIME_TYPE_JPEG],
],
MIME_TYPE_JS => [
MIME_TYPE_MAP_EXT => [EXTENSION_JS],
MIME_TYPE_MAP_MIME => [MIME_TYPE_JS],
],
MIME_TYPE_JSON => [
MIME_TYPE_MAP_EXT => [EXTENSION_JSON],
MIME_TYPE_MAP_MIME => [MIME_TYPE_JSON],
],
MIME_TYPE_MKV => [
MIME_TYPE_MAP_EXT => [EXTENSION_MKV],
MIME_TYPE_MAP_MIME => [MIME_TYPE_MKV],
],
MIME_TYPE_MP3 => [
MIME_TYPE_MAP_EXT => [EXTENSION_MP3],
MIME_TYPE_MAP_MIME => [MIME_TYPE_MP3],
],
MIME_TYPE_MP4_AUDIO => [
MIME_TYPE_MAP_EXT => [EXTENSION_M4A],
MIME_TYPE_MAP_MIME => [MIME_TYPE_MP4_AUDIO,"audio/m4a"],
],
MIME_TYPE_MP4_VIDEO => [
MIME_TYPE_MAP_EXT => [EXTENSION_MP4,EXTENSION_M4V],
MIME_TYPE_MAP_MIME => [MIME_TYPE_MP4_VIDEO,'video/x-m4v'],
],
MIME_TYPE_MPEG => [
MIME_TYPE_MAP_EXT => [EXTENSION_MPG,EXTENSION_MPEG],
MIME_TYPE_MAP_MIME => [MIME_TYPE_MPEG],
],
MIME_TYPE_PDF => [
MIME_TYPE_MAP_EXT => [EXTENSION_PDF],
MIME_TYPE_MAP_MIME => [MIME_TYPE_PDF],
],
MIME_TYPE_PHP => [
MIME_TYPE_MAP_EXT => [EXTENSION_PHP,EXTENSION_PHP5],
MIME_TYPE_MAP_MIME => [MIME_TYPE_PHP],
],
MIME_TYPE_PNG => [
MIME_TYPE_MAP_EXT => [EXTENSION_PNG],
MIME_TYPE_MAP_MIME => [MIME_TYPE_PNG],
],
MIME_TYPE_PSD => [
MIME_TYPE_MAP_EXT => [EXTENSION_PSD],
MIME_TYPE_MAP_MIME => [MIME_TYPE_PSD],
],
MIME_TYPE_OGG_AUDIO => [
MIME_TYPE_MAP_EXT => [EXTENSION_OGG_AUDIO,EXTENSION_OGG],
MIME_TYPE_MAP_MIME => [MIME_TYPE_OGG_AUDIO,MIME_TYPE_OGG],
],
MIME_TYPE_OGG_VIDEO => [
MIME_TYPE_MAP_EXT => [EXTENSION_OGG_VIDEO],
MIME_TYPE_MAP_MIME => [MIME_TYPE_OGG_VIDEO],
],
MIME_TYPE_QUICKTIME => [
MIME_TYPE_MAP_EXT => [EXTENSION_MOV],
MIME_TYPE_MAP_MIME => [MIME_TYPE_QUICKTIME],
],
MIME_TYPE_RSS => [
MIME_TYPE_MAP_EXT => [EXTENSION_RSS],
MIME_TYPE_MAP_MIME => [MIME_TYPE_RSS],
],
MIME_TYPE_SVG => [
MIME_TYPE_MAP_EXT => [EXTENSION_SVG],
MIME_TYPE_MAP_MIME => [MIME_TYPE_SVG],
],
MIME_TYPE_TAR => [
MIME_TYPE_MAP_EXT => [EXTENSION_TAR],
MIME_TYPE_MAP_MIME => [MIME_TYPE_TAR],
],
MIME_TYPE_TEXT => [
MIME_TYPE_MAP_EXT => [EXTENSION_TEXT, EXTENSION_ASC],
MIME_TYPE_MAP_MIME => [MIME_TYPE_TEXT],
],
MIME_TYPE_TIFF => [
MIME_TYPE_MAP_EXT => [EXTENSION_TIF,EXTENSION_TIFF],
MIME_TYPE_MAP_MIME => [MIME_TYPE_TIFF],
],
MIME_TYPE_WAV => [
MIME_TYPE_MAP_EXT => [EXTENSION_WAV],
MIME_TYPE_MAP_MIME => [MIME_TYPE_WAV],
],
MIME_TYPE_WEBM => [
MIME_TYPE_MAP_EXT => [EXTENSION_WEBM],
MIME_TYPE_MAP_MIME => [MIME_TYPE_WEBM],
],
MIME_TYPE_WEBP => [
MIME_TYPE_MAP_EXT => [EXTENSION_WEBP],
MIME_TYPE_MAP_MIME => [MIME_TYPE_WEBP],
],
MIME_TYPE_XML => [
MIME_TYPE_MAP_EXT => [EXTENSION_XML],
MIME_TYPE_MAP_MIME => [MIME_TYPE_XML,MIME_TYPE_XML_APPLICATION],
],
MIME_TYPE_XSL => [
MIME_TYPE_MAP_EXT => [EXTENSION_XSL],
MIME_TYPE_MAP_MIME => [MIME_TYPE_XSL],
],
MIME_TYPE_ZIP => [
MIME_TYPE_MAP_EXT => [EXTENSION_ZIP],
MIME_TYPE_MAP_MIME => [MIME_TYPE_ZIP],
],
];
/**
* Returns the mimetype that matches the provided extension.
*/
function get_mime_for_extension(string $ext): ?string
{
$ext = strtolower($ext);
foreach (MIME_TYPE_MAP as $key=>$value) {
if(in_array($ext,$value[MIME_TYPE_MAP_EXT])) {
return $key;
}
}
return null;
}
/**
* Returns the mimetype for the specified file, trying file inspection methods before falling back on extension-based detection.
* @param String $file
* @param String $ext The files extension, for if the current filename somehow lacks the extension
* @return String The extension that was found.
*/
function get_mime(string $file, string $ext=""): string
{
if(!file_exists($file)) {
throw new SCoreException("File not found: ".$file);
}
$type = false;
if (extension_loaded('fileinfo')) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
try {
$type = finfo_file($finfo, $file);
} finally {
finfo_close($finfo);
}
} elseif (function_exists('mime_content_type')) {
// If anyone is still using mime_content_type()
$type = trim(mime_content_type($file));
}
if($type===false || empty($type)) {
// Checking by extension is our last resort
if($ext==null||strlen($ext) == 0)
$ext = pathinfo($file, PATHINFO_EXTENSION);
$type = get_mime_for_extension($ext);
}
if ($type !== false && strlen($type) > 0) {
return $type;
}
return MIME_TYPE_OCTET_STREAM;
}
/**
* Returns the file extension associated with the specified mimetype.
*/
function get_extension(?string $mime_type): ?string
{
if (empty($mime_type)) {
return null;
}
if($mime_type==MIME_TYPE_OCTET_STREAM) {
return null;
}
foreach (MIME_TYPE_MAP as $key=>$value) {
if(in_array($mime_type,$value[MIME_TYPE_MAP_MIME])) {
return $value[MIME_TYPE_MAP_EXT][0];
}
}
return null;
}
/**
* Returns all of the file extensions associated with the specified mimetype.
*/
function get_all_extension_for_mime(?string $mime_type): array
{
$output = [];
if (empty($mime_type)) {
return $output;
}
foreach (MIME_TYPE_MAP as $key=>$value) {
if(in_array($mime_type,$value[MIME_TYPE_MAP_MIME])) {
$output = array_merge($output, $value[MIME_TYPE_MAP_EXT]);
}
}
return $output;
}
/**
* Gets an the extension defined in MIME_TYPE_MAP for a file.
*
* @param String $file_path
* @return String The extension that was found, or null if one can not be found.
*/
function get_extension_for_file(String $file_path): ?String
{
$mime = get_mime($file_path);
if (!empty($mime)) {
if($mime==MIME_TYPE_OCTET_STREAM) {
return null;
} else {
$ext = get_extension($mime);
}
if (!empty($ext)) {
return $ext;
}
}
return null;
}

View file

@ -570,7 +570,7 @@ class Image
*/
public function get_mime_type(): string
{
return getMimeType($this->get_image_filename(), $this->get_ext());
return get_mime($this->get_image_filename(), $this->get_ext());
}
/**

View file

@ -142,7 +142,7 @@ function create_scaled_image(string $inname, string $outname, array $tsize, stri
}
$output_format = $config->get_string(ImageConfig::THUMB_TYPE);
if ($output_format=="webp") {
if ($output_format==EXTENSION_WEBP) {
$output_format = Media::WEBP_LOSSY;
}

View file

@ -116,8 +116,6 @@ class Tag
for ($i = 0; $i < count($tags1); $i++) {
if ($tags1[$i]!==$tags2[$i]) {
var_dump($tags1);
var_dump($tags2);
return false;
}
}

View file

@ -3,6 +3,9 @@
* Things which should be in the core API *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
require_once "filetypes.php";
/**
* Return the unique elements of an array, case insensitively
*/
@ -253,99 +256,6 @@ if (!function_exists('mb_strlen')) {
}
}
const MIME_TYPE_MAP = [
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'ico' => 'image/x-icon',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
'svg' => 'image/svg+xml',
'pdf' => 'application/pdf',
'zip' => 'application/zip',
'gz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'bz' => 'application/x-bzip',
'bz2' => 'application/x-bzip2',
'txt' => 'text/plain',
'asc' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'css' => 'text/css',
'js' => 'text/javascript',
'xml' => 'text/xml',
'xsl' => 'application/xsl+xml',
'ogg' => 'application/ogg',
'mp3' => 'audio/mpeg',
'wav' => 'audio/x-wav',
'avi' => 'video/x-msvideo',
'mpg' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'mov' => 'video/quicktime',
'php' => 'text/x-php',
'mp4' => 'video/mp4',
'ogv' => 'video/ogg',
'webm' => 'video/webm',
'webp' => 'image/webp',
'bmp' =>'image/x-ms-bmp',
'psd' => 'image/vnd.adobe.photoshop',
'mkv' => 'video/x-matroska'
];
/**
* Get MIME type for file
*
* The contents of this function are taken from the __getMimeType() function
* from the "Amazon S3 PHP class" which is Copyright (c) 2008, Donovan Schönknecht
* and released under the 'Simplified BSD License'.
*/
function getMimeType(string $file, string $ext=""): string
{
// Static extension lookup
$ext = strtolower($ext);
if (array_key_exists($ext, MIME_TYPE_MAP)) {
return MIME_TYPE_MAP[$ext];
}
$type = false;
// Fileinfo documentation says fileinfo_open() will use the
// MAGIC env var for the magic file
if (extension_loaded('fileinfo') && isset($_ENV['MAGIC']) &&
($finfo = finfo_open(FILEINFO_MIME, $_ENV['MAGIC'])) !== false) {
if (($type = finfo_file($finfo, $file)) !== false) {
// Remove the charset and grab the last content-type
$type = explode(' ', str_replace('; charset=', ';charset=', $type));
$type = array_pop($type);
$type = explode(';', $type);
$type = trim(array_shift($type));
}
finfo_close($finfo);
// If anyone is still using mime_content_type()
} elseif (function_exists('mime_content_type')) {
$type = trim(mime_content_type($file));
}
if ($type !== false && strlen($type) > 0) {
return $type;
}
return 'application/octet-stream';
}
function get_extension(?string $mime_type): ?string
{
if (empty($mime_type)) {
return null;
}
$ext = array_search($mime_type, MIME_TYPE_MAP);
return ($ext ? $ext : null);
}
/** @noinspection PhpUnhandledExceptionInspection */
function getSubclassesOf(string $parent)
{
@ -860,3 +770,4 @@ function stringer($s)
}
return (string)$s;
}

View file

@ -96,7 +96,7 @@ class AliasEditor extends Extension
$this->theme->display_aliases($t->table($t->query()), $t->paginator());
} elseif ($event->get_arg(0) == "export") {
$page->set_mode(PageMode::DATA);
$page->set_type("text/csv");
$page->set_type(MIME_TYPE_CSV);
$page->set_filename("aliases.csv");
$page->set_data($this->get_alias_csv($database));
} elseif ($event->get_arg(0) == "import") {

View file

@ -98,7 +98,7 @@ class AutoTagger extends Extension
$this->theme->display_auto_tagtable($t->table($t->query()), $t->paginator());
} elseif ($event->get_arg(0) == "export") {
$page->set_mode(PageMode::DATA);
$page->set_type("text/csv");
$page->set_type(MIME_TYPE_CSV);
$page->set_filename("auto_tag.csv");
$page->set_data($this->get_auto_tag_csv($database));
} elseif ($event->get_arg(0) == "import") {

View file

@ -20,7 +20,7 @@ class AutoComplete extends Extension
}
$page->set_mode(PageMode::DATA);
$page->set_type("application/json");
$page->set_type(MIME_TYPE_JSON);
$s = strtolower($_GET["s"]);
if (

View file

@ -42,7 +42,7 @@ class BrowserSearch extends Extension
// And now to send it to the browser
$page->set_mode(PageMode::DATA);
$page->set_type("text/xml");
$page->set_type(MIME_TYPE_XML);
$page->set_data($xml);
} elseif ($event->page_matches("browser_search")) {
$suggestions = $config->get_string("search_suggestions_results_order");

View file

@ -10,13 +10,13 @@ class DanbooruApi extends Extension
if ($event->page_matches("api/danbooru/add_post") || $event->page_matches("api/danbooru/post/create.xml")) {
// No XML data is returned from this function
$page->set_type("text/plain");
$page->set_type(MIME_TYPE_TEXT);
$this->api_add_post();
} elseif ($event->page_matches("api/danbooru/find_posts") || $event->page_matches("api/danbooru/post/index.xml")) {
$page->set_type("application/xml");
$page->set_type(MIME_TYPE_XML_APPLICATION);
$page->set_data($this->api_find_posts());
} elseif ($event->page_matches("api/danbooru/find_tags")) {
$page->set_type("application/xml");
$page->set_type(MIME_TYPE_XML_APPLICATION);
$page->set_data($this->api_find_tags());
}

View file

@ -2,7 +2,7 @@
class ArchiveFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_EXT = ["zip"];
protected $SUPPORTED_MIME = [MIME_TYPE_ZIP];
public function onInitExt(InitExtEvent $event)
{

View file

@ -2,7 +2,7 @@
class CBZFileHandler extends DataHandlerExtension
{
public $SUPPORTED_EXT = ["cbz"];
public $SUPPORTED_MIME = [MIME_TYPE_COMIC_ZIP];
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
@ -27,7 +27,7 @@ class CBZFileHandler extends DataHandlerExtension
$cover,
warehouse_path(Image::THUMBNAIL_DIR, $hash),
get_thumbnail_max_size_scaled(),
get_extension(getMimeType($cover)),
get_extension(get_mime($cover)),
null
);
return true;

View file

@ -2,7 +2,7 @@
class FlashFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_EXT = ["swf"];
protected $SUPPORTED_MIME = [MIME_TYPE_FLASH];
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{

View file

@ -2,7 +2,7 @@
class IcoFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_EXT = ["ico", "ani", "cur"];
protected $SUPPORTED_MIME = [MIME_TYPE_ICO, MIME_TYPE_ANI];
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{

View file

@ -2,7 +2,7 @@
class MP3FileHandler extends DataHandlerExtension
{
protected $SUPPORTED_EXT = ["mp3"];
protected $SUPPORTED_MIME = [MIME_TYPE_MP3];
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
@ -23,6 +23,6 @@ class MP3FileHandler extends DataHandlerExtension
protected function check_contents(string $tmpname): bool
{
return getMimeType($tmpname) == 'audio/mpeg';
return get_mime($tmpname) === MIME_TYPE_MP3;
}
}

View file

@ -2,13 +2,13 @@
class PixelFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_EXT = ["jpg", "jpeg", "gif", "png", "webp"];
protected $SUPPORTED_MIME = [MIME_TYPE_JPEG, MIME_TYPE_GIF, MIME_TYPE_PNG, MIME_TYPE_WEBP];
protected function media_check_properties(MediaCheckPropertiesEvent $event): void
{
if (in_array($event->ext, Media::LOSSLESS_FORMATS)) {
$event->image->lossless = true;
} elseif ($event->ext=="webp") {
} elseif ($event->ext==EXTENSION_WEBP) {
$event->image->lossless = Media::is_lossless_webp($event->file_name);
}
@ -17,10 +17,10 @@ class PixelFileHandler extends DataHandlerExtension
}
$event->image->audio = false;
switch ($event->ext) {
case "gif":
case EXTENSION_GIF:
$event->image->video = Media::is_animated_gif($event->file_name);
break;
case "webp":
case EXTENSION_WEBP:
$event->image->video = Media::is_animated_webp($event->file_name);
break;
default:

View file

@ -3,7 +3,7 @@ use enshrined\svgSanitize\Sanitizer;
class SVGFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_EXT = ["svg"];
protected $SUPPORTED_MIME = [MIME_TYPE_SVG];
/** @var SVGFileHandlerTheme */
protected $theme;
@ -16,7 +16,7 @@ class SVGFileHandler extends DataHandlerExtension
$image = Image::by_id($id);
$hash = $image->hash;
$page->set_type("image/svg+xml");
$page->set_type(MIME_TYPE_SVG);
$page->set_mode(PageMode::DATA);
$sanitizer = new Sanitizer();
@ -67,7 +67,7 @@ class SVGFileHandler extends DataHandlerExtension
protected function check_contents(string $file): bool
{
if (getMimeType($file)!="image/svg+xml") {
if (get_mime($file)!==MIME_TYPE_SVG) {
return false;
}

View file

@ -3,13 +3,12 @@
class VideoFileHandler extends DataHandlerExtension
{
protected $SUPPORTED_MIME = [
'video/webm',
'video/mp4',
'video/ogg',
'video/flv',
'video/x-flv'
MIME_TYPE_WEBM,
MIME_TYPE_MP4_VIDEO,
MIME_TYPE_OGG_VIDEO,
MIME_TYPE_FLASH_VIDEO,
];
protected $SUPPORTED_EXT = ["flv", "mp4", "m4v", "ogv", "webm"];
protected $SUPPORTED_EXT = [EXTENSION_FLASH_VIDEO, EXTENSION_MP4, EXTENSION_M4V, EXTENSION_OGG_VIDEO, EXTENSION_WEBM];
public function onInitExt(InitExtEvent $event)
{
@ -87,6 +86,6 @@ class VideoFileHandler extends DataHandlerExtension
protected function check_contents(string $tmpname): bool
{
return in_array(getMimeType($tmpname), $this->SUPPORTED_MIME);
return in_array(get_mime($tmpname), $this->SUPPORTED_MIME);
}
}

View file

@ -2,6 +2,9 @@
class VideoFileHandlerTheme extends Themelet
{
const SUPPORTED_TYPES = [MIME_TYPE_MP4_VIDEO, MIME_TYPE_OGG_VIDEO, MIME_TYPE_WEBM, MIME_TYPE_FLASH_VIDEO];
public function display_image(Page $page, Image $image)
{
global $config;
@ -16,10 +19,11 @@ class VideoFileHandlerTheme extends Themelet
$html = "Video not playing? <a href='$ilink'>Click here</a> to download the file.<br/>";
//Browser media format support: https://developer.mozilla.org/en-US/docs/Web/HTML/Supported_media_formats
$supportedExts = ['mp4' => 'video/mp4', 'm4v' => 'video/mp4', 'ogv' => 'video/ogg', 'webm' => 'video/webm', 'flv' => 'video/flv'];
if (array_key_exists($ext, $supportedExts)) {
$mime = get_mime_for_extension($ext);
if (in_array($mime, self::SUPPORTED_TYPES)) {
//FLV isn't supported by <video>, but it should always fallback to the flash-based method.
if ($ext == "webm") {
if ($mime == MIME_TYPE_WEBM) {
//Several browsers still lack WebM support sadly: https://caniuse.com/#feat=webm
$html .= "<!--[if IE]><p>To view webm files with IE, please <a href='https://tools.google.com/dlpage/webmmf/' target='_blank'>download this plugin</a>.</p><![endif]-->";
}
@ -40,7 +44,7 @@ class VideoFileHandlerTheme extends Themelet
<img alt='thumb' src=\"{$thumb_url}\" />
</object>";
if ($ext == "flv") {
if ($mime == MIME_TYPE_FLASH_VIDEO) {
//FLV doesn't support <video>.
$html .= $html_fallback;
} else {
@ -50,7 +54,7 @@ class VideoFileHandlerTheme extends Themelet
$html .= "
<video controls class='shm-main-image' id='main_image' alt='main image' poster='$thumb_url' {$autoplay} {$loop}
style='max-width: 100%'>
<source src='{$ilink}' type='{$supportedExts[$ext]}'>
<source src='{$ilink}' type='{$mime}'>
<!-- If browser doesn't support filetype, fallback to flash -->
{$html_fallback}
@ -60,7 +64,7 @@ class VideoFileHandlerTheme extends Themelet
}
} else {
//This should never happen, but just in case let's have a fallback..
$html = "Video type '$ext' not recognised";
$html = "Video type '$mime' not recognised";
}
$page->add_block(new Block("Video", $html, "main", 10));
}

View file

@ -21,8 +21,8 @@ class ImageIO extends Extension
];
const THUMBNAIL_TYPES = [
'JPEG' => "jpg",
'WEBP (Not IE/Safari compatible)' => "webp"
'JPEG' => EXTENSION_JPG,
'WEBP (Not IE/Safari compatible)' => EXTENSION_WEBP
];
public function onInitExt(InitExtEvent $event)
@ -32,7 +32,7 @@ class ImageIO extends Extension
$config->set_default_int(ImageConfig::THUMB_HEIGHT, 192);
$config->set_default_int(ImageConfig::THUMB_SCALING, 100);
$config->set_default_int(ImageConfig::THUMB_QUALITY, 75);
$config->set_default_string(ImageConfig::THUMB_TYPE, 'jpg');
$config->set_default_string(ImageConfig::THUMB_TYPE, EXTENSION_JPG);
if (function_exists(self::EXIF_READ_FUNCTION)) {
$config->set_default_bool(ImageConfig::SHOW_META, false);
@ -271,11 +271,7 @@ class ImageIO extends Extension
if (!is_null($image)) {
if ($type == "thumb") {
$ext = $config->get_string(ImageConfig::THUMB_TYPE);
if (array_key_exists($ext, MIME_TYPE_MAP)) {
$page->set_type(MIME_TYPE_MAP[$ext]);
} else {
$page->set_type("image/jpeg");
}
$page->set_type(get_mime_for_extension($ext));
$file = $image->get_thumb_filename();
} else {

View file

@ -26,26 +26,26 @@ class Media extends Extension
const LOSSLESS_FORMATS = [
self::WEBP_LOSSLESS,
"png",
"psd",
"bmp",
"ico",
"cur",
"ani",
"gif"
EXTENSION_PNG,
EXTENSION_PSD,
EXTENSION_BMP,
EXTENSION_ICO,
EXTENSION_CUR,
EXTENSION_ANI,
EXTENSION_GIF
];
const ALPHA_FORMATS = [
self::WEBP_LOSSLESS,
self::WEBP_LOSSY,
"webp",
"png",
EXTENSION_WEBP,
EXTENSION_PNG,
];
const FORMAT_ALIASES = [
"tif" => "tiff",
"jpeg" => "jpg",
EXTENSION_TIF => EXTENSION_TIFF,
EXTENSION_JPEG => EXTENSION_JPG,
];
@ -363,7 +363,7 @@ class Media extends Extension
$codec = "mjpeg";
$quality = $config->get_int(ImageConfig::THUMB_QUALITY);
if ($config->get_string(ImageConfig::THUMB_TYPE) == "webp") {
if ($config->get_string(ImageConfig::THUMB_TYPE) == EXTENSION_WEBP) {
$codec = "libwebp";
} else {
// mjpeg quality ranges from 2-31, with 2 being the best quality.
@ -436,7 +436,7 @@ class Media extends Extension
switch ($format) {
case self::WEBP_LOSSLESS:
case self::WEBP_LOSSY:
return "webp";
return EXTENSION_WEBP;
default:
return $format;
}
@ -445,7 +445,7 @@ class Media extends Extension
// private static function image_save_imagick(Imagick $image, string $path, string $format, int $output_quality = 80, bool $minimize)
// {
// switch ($format) {
// case "png":
// case EXTENSION_PNG:
// $result = $image->setOption('png:compression-level', 9);
// if ($result !== true) {
// throw new GraphicsException("Could not set png compression option");
@ -555,7 +555,7 @@ class Media extends Extension
return true;
}
switch ($format) {
case "webp":
case EXTENSION_WEBP:
return self::is_lossless_webp($filename);
break;
}
@ -586,7 +586,7 @@ class Media extends Extension
$output_type = $input_type;
}
if ($output_type=="webp" && self::is_lossless($input_path, $input_type)) {
if ($output_type==EXTENSION_WEBP && self::is_lossless($input_path, $input_type)) {
$output_type = self::WEBP_LOSSLESS;
}
@ -612,7 +612,7 @@ class Media extends Extension
case Media::WEBP_LOSSLESS:
$args .= '-define webp:lossless=true';
break;
case "png":
case EXTENSION_PNG:
$args .= '-define png:compression-level=9';
break;
}
@ -668,19 +668,19 @@ class Media extends Extension
/* If not specified, output to the same format as the original image */
switch ($info[2]) {
case IMAGETYPE_GIF:
$output_type = "gif";
$output_type = EXTENSION_GIF;
break;
case IMAGETYPE_JPEG:
$output_type = "jpeg";
$output_type = EXTENSION_JPEG;
break;
case IMAGETYPE_PNG:
$output_type = "png";
$output_type = EXTENSION_PNG;
break;
case IMAGETYPE_WEBP:
$output_type = "webp";
$output_type = EXTENSION_WEBP;
break;
case IMAGETYPE_BMP:
$output_type = "bmp";
$output_type = EXTENSION_BMP;
break;
default:
throw new MediaException("Failed to save the new image - Unsupported image type.");
@ -776,21 +776,21 @@ class Media extends Extension
}
switch ($output_type) {
case "bmp":
case EXTENSION_BMP:
$result = imagebmp($image_resized, $output_filename, true);
break;
case "webp":
case EXTENSION_WEBP:
case Media::WEBP_LOSSY:
$result = imagewebp($image_resized, $output_filename, $output_quality);
break;
case "jpg":
case "jpeg":
case EXTENSION_JPG:
case EXTENSION_JPEG:
$result = imagejpeg($image_resized, $output_filename, $output_quality);
break;
case "png":
case EXTENSION_PNG:
$result = imagepng($image_resized, $output_filename, 9);
break;
case "gif":
case EXTENSION_GIF:
$result = imagegif($image_resized, $output_filename);
break;
default:
@ -904,7 +904,7 @@ class Media extends Extension
*/
public static function normalize_format(string $format, ?bool $lossless = null): ?string
{
if ($format == "webp") {
if ($format == EXTENSION_WEBP) {
if ($lossless === true) {
$format = Media::WEBP_LOSSLESS;
} else {

View file

@ -15,63 +15,63 @@ abstract class MediaEngine
];
public const OUTPUT_SUPPORT = [
MediaEngine::GD => [
"gif",
"jpg",
"png",
"webp",
EXTENSION_GIF,
EXTENSION_JPG,
EXTENSION_PNG,
EXTENSION_WEBP,
Media::WEBP_LOSSY,
],
MediaEngine::IMAGICK => [
"gif",
"jpg",
"png",
"webp",
EXTENSION_GIF,
EXTENSION_JPG,
EXTENSION_PNG,
EXTENSION_WEBP,
Media::WEBP_LOSSY,
Media::WEBP_LOSSLESS,
],
MediaEngine::FFMPEG => [
"jpg",
"webp",
"png",
EXTENSION_JPG,
EXTENSION_WEBP,
EXTENSION_PNG,
],
MediaEngine::STATIC => [
"jpg",
EXTENSION_JPG,
],
];
public const INPUT_SUPPORT = [
MediaEngine::GD => [
"bmp",
"gif",
"jpg",
"png",
"webp",
EXTENSION_BMP,
EXTENSION_GIF,
EXTENSION_JPG,
EXTENSION_PNG,
EXTENSION_WEBP,
Media::WEBP_LOSSY,
Media::WEBP_LOSSLESS,
],
MediaEngine::IMAGICK => [
"bmp",
"gif",
"jpg",
"png",
"psd",
"tiff",
"webp",
EXTENSION_BMP,
EXTENSION_GIF,
EXTENSION_JPG,
EXTENSION_PNG,
EXTENSION_PSD,
EXTENSION_TIFF,
EXTENSION_WEBP,
Media::WEBP_LOSSY,
Media::WEBP_LOSSLESS,
"ico",
EXTENSION_ICO,
],
MediaEngine::FFMPEG => [
"avi",
"mkv",
"webm",
"mp4",
"mov",
"flv",
EXTENSION_AVI,
EXTENSION_MKV,
EXTENSION_WEBM,
EXTENSION_MP4,
EXTENSION_MOV,
EXTENSION_FLASH_VIDEO,
],
MediaEngine::STATIC => [
"jpg",
"gif",
"png",
EXTENSION_JPG,
EXTENSION_GIF,
EXTENSION_PNG,
],
];
}

View file

@ -35,7 +35,7 @@ class RandomImage extends Extension
send_event(new DisplayingImageEvent($image));
} elseif ($action === "widget") {
$page->set_mode(PageMode::DATA);
$page->set_type("text/html");
$page->set_type(MIME_TYPE_HTML);
$page->set_data($this->theme->build_thumb_html($image));
}
}

View file

@ -84,7 +84,7 @@ class ResizeImage extends Extension
$height = $config->get_int(ResizeConfig::DEFAULT_HEIGHT);
}
$isanigif = 0;
if ($image_obj->ext == "gif") {
if ($image_obj->ext == EXTENSION_GIF) {
$image_filename = warehouse_path(Image::IMAGE_DIR, $image_obj->hash);
if (($fh = @fopen($image_filename, 'rb'))) {
//check if gif is animated (via https://www.php.net/manual/en/function.imagecreatefromgif.php#104473)

View file

@ -15,7 +15,7 @@ class RotateImage extends Extension
/** @var RotateImageTheme */
protected $theme;
const SUPPORTED_EXT = ["jpg","jpeg","png","gif","webp"];
const SUPPORTED_MIME = [MIME_TYPE_JPEG, MIME_TYPE_PNG, MIME_TYPE_GIF, MIME_TYPE_WEBP];
public function onInitExt(InitExtEvent $event)
{
@ -28,7 +28,7 @@ class RotateImage extends Extension
{
global $user, $config;
if ($user->can(Permissions::EDIT_FILES) && $config->get_bool("rotate_enabled")
&& in_array($event->image->ext, self::SUPPORTED_EXT)) {
&& in_array(get_mime_for_extension($event->image->ext), self::SUPPORTED_MIME)) {
/* Add a link to rotate the image */
$event->add_part($this->theme->get_rotate_html($event->image->id));
}

View file

@ -16,7 +16,7 @@ class RSSComments extends Extension
global $config, $database, $page;
if ($event->page_matches("rss/comments")) {
$page->set_mode(PageMode::DATA);
$page->set_type("application/rss+xml");
$page->set_type(MIME_TYPE_RSS);
$comments = $database->get_all("
SELECT

View file

@ -9,7 +9,7 @@ class RSSCommentsTest extends ShimmiePHPUnitTestCase
send_event(new CommentPostingEvent($image_id, $user, "ASDFASDF"));
$this->get_page('rss/comments');
//$this->assert_mime("application/rss+xml");
//$this->assert_mime(MIME_TYPE_RSS);
$this->assert_no_content("Exception");
$this->assert_content("ASDFASDF");
}

View file

@ -43,7 +43,7 @@ class RSSImages extends Extension
global $page;
global $config;
$page->set_mode(PageMode::DATA);
$page->set_type("application/rss+xml");
$page->set_type(MIME_TYPE_RSS);
$data = "";
foreach ($images as $image) {

View file

@ -8,26 +8,26 @@ class RSSImagesTest extends ShimmiePHPUnitTestCase
$this->log_out();
$this->get_page('rss/images');
//$this->assert_mime("application/rss+xml");
//$this->assert_mime(MIME_TYPE_RSS);
$this->assert_no_content("Exception");
$this->get_page('rss/images/1');
//$this->assert_mime("application/rss+xml");
//$this->assert_mime(MIME_TYPE_RSS);
$this->assert_no_content("Exception");
# FIXME: test that the image is actually found
$this->get_page('rss/images/computer/1');
//$this->assert_mime("application/rss+xml");
//$this->assert_mime(MIME_TYPE_RSS);
$this->assert_no_content("Exception");
# valid tag, invalid page
$this->get_page('rss/images/computer/2');
//$this->assert_mime("application/rss+xml");
//$this->assert_mime(MIME_TYPE_RSS);
$this->assert_no_content("Exception");
# not found
$this->get_page('rss/images/waffle/2');
//$this->assert_mime("application/rss+xml");
//$this->assert_mime(MIME_TYPE_RSS);
$this->assert_no_content("Exception");
}
}

View file

@ -36,7 +36,7 @@ class ShimmieApi extends Extension
if ($event->page_matches("api/shimmie")) {
$page->set_mode(PageMode::DATA);
$page->set_type("text/plain");
$page->set_type(MIME_TYPE_TEXT);
if ($event->page_matches("api/shimmie/get_tags")) {
if ($event->count_args() > 0) {

View file

@ -155,7 +155,7 @@ class XMLSitemap extends Extension
// Generate new sitemap
file_put_contents($this->sitemap_filepath, $xml);
$page->set_mode(PageMode::DATA);
$page->set_type("application/xml");
$page->set_type(MIME_TYPE_XML_APPLICATION);
$page->set_data($xml);
}
@ -191,7 +191,7 @@ class XMLSitemap extends Extension
$xml = file_get_contents($this->sitemap_filepath);
$page->set_mode(PageMode::DATA);
$page->set_type("application/xml");
$page->set_type(MIME_TYPE_XML_APPLICATION);
$page->set_data($xml);
}
}

View file

@ -21,15 +21,8 @@ class StaticFiles extends Extension
$page->add_http_header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 600) . ' GMT');
$page->set_mode(PageMode::DATA);
$page->set_data(file_get_contents($filename));
if (endsWith($filename, ".ico")) {
$page->set_type("image/x-icon");
}
if (endsWith($filename, ".png")) {
$page->set_type("image/png");
}
if (endsWith($filename, ".txt")) {
$page->set_type("text/plain");
}
$page->set_type(get_mime($filename));
}
}
}

View file

@ -77,7 +77,7 @@ class TagList extends Extension
}
$page->set_mode(PageMode::DATA);
$page->set_type("text/plain");
$page->set_type(MIME_TYPE_TEXT);
$page->set_data(implode("\n", $res));
}
}

View file

@ -30,7 +30,7 @@ class TaggerXML extends Extension
"</tags>";
$page->set_mode(PageMode::DATA);
$page->set_type("text/xml");
$page->set_type(MIME_TYPE_XML);
$page->set_data($xml);
}
}

View file

@ -17,20 +17,20 @@ class TranscodeImage extends Extension
const ACTION_BULK_TRANSCODE = "bulk_transcode";
const INPUT_FORMATS = [
"BMP" => "bmp",
"GIF" => "gif",
"ICO" => "ico",
"JPG" => "jpg",
"PNG" => "png",
"PSD" => "psd",
"TIFF" => "tiff",
"WEBP" => "webp",
"BMP" => EXTENSION_BMP,
"GIF" => EXTENSION_GIF,
"ICO" => EXTENSION_ICO,
"JPG" => EXTENSION_JPG,
"PNG" => EXTENSION_PNG,
"PSD" => EXTENSION_PSD,
"TIFF" => EXTENSION_TIFF,
"WEBP" => EXTENSION_WEBP,
];
const OUTPUT_FORMATS = [
"" => "",
"JPEG (lossy)" => "jpg",
"PNG (lossless)" => "png",
"JPEG (lossy)" => EXTENSION_JPG,
"PNG (lossless)" => EXTENSION_PNG,
"WEBP (lossy)" => Media::WEBP_LOSSY,
"WEBP (lossless)" => Media::WEBP_LOSSLESS,
];
@ -102,7 +102,7 @@ class TranscodeImage extends Extension
$ext = Media::normalize_format($ext);
if ($event->type=="gif"&&Media::is_animated_gif($event->tmpname)) {
if ($event->type==EXTENSION_GIF&&Media::is_animated_gif($event->tmpname)) {
return;
}
@ -314,14 +314,14 @@ class TranscodeImage extends Extension
try {
$result = false;
switch ($target_format) {
case "webp":
case EXTENSION_WEBP:
case Media::WEBP_LOSSY:
$result = imagewebp($image, $tmp_name, $q);
break;
case "png":
case EXTENSION_PNG:
$result = imagepng($image, $tmp_name, 9);
break;
case "jpg":
case EXTENSION_JPG:
// In case of alpha channels
$width = imagesx($image);
$height = imagesy($image);
@ -376,7 +376,7 @@ class TranscodeImage extends Extension
case Media::WEBP_LOSSY:
$args .= '';
break;
case "png":
case EXTENSION_PNG:
$args .= '-define png:compression-level=9';
break;
default:
@ -387,7 +387,7 @@ class TranscodeImage extends Extension
$source_type = "";
switch ($source_format) {
case "ico":
case EXTENSION_ICO:
$source_type = "ico:";
}

View file

@ -67,7 +67,7 @@ class Update extends Extension
log_info("update", "Attempting to download Shimmie commit: ".$commitSHA);
if ($headers = transload($url, $filename)) {
if (($headers['Content-Type'] !== "application/zip") || ((int) $headers['Content-Length'] !== filesize($filename))) {
if (($headers['Content-Type'] !== MIME_TYPE_ZIP) || ((int) $headers['Content-Length'] !== filesize($filename))) {
unlink("./data/update_{$commitSHA}.zip");
log_warning("update", "Download failed: not zip / not same size as remote file.");
return false;

View file

@ -45,14 +45,18 @@ class DataUploadEvent extends Event
$this->set_tmpname($tmpname);
if ($config->get_bool("upload_use_mime")) {
$this->set_type(get_extension(getMimeType($tmpname)));
} else {
$filetype = get_extension_for_file($tmpname);
}
if(empty($filetype)) {
if (array_key_exists('extension', $metadata) && !empty($metadata['extension'])) {
$this->type = strtolower($metadata['extension']);
$filetype = strtolower($metadata['extension']);
} else {
throw new UploadException("Could not determine extension for file " . $metadata["filename"]);
}
}
$this->set_type($filetype);
}
public function set_type(String $type)

View file

@ -123,22 +123,8 @@ class UploadTheme extends Themelet
$html .= ' (Drag &amp; drop onto your bookmarks toolbar, then click when looking at an image)';
// Bookmarklet checks if shimmie supports ext. If not, won't upload to site/shows alert saying not supported.
$supported_ext = "jpg jpeg gif png webp";
if (class_exists("FlashFileHandler")) {
$supported_ext .= " swf";
}
if (class_exists("ICOFileHandler")) {
$supported_ext .= " ico ani cur";
}
if (class_exists("MP3FileHandler")) {
$supported_ext .= " mp3";
}
if (class_exists("SVGFileHandler")) {
$supported_ext .= " svg";
}
if (class_exists("VideoFileHandler")) {
$supported_ext .= " flv mp4 ogv webm m4v";
}
$supported_ext = join(" ", DataHandlerExtension::get_all_supported_exts());
$title = "Booru to " . $config->get_string(SetupConfig::TITLE);
// CA=0: Ask to use current or new tags | CA=1: Always use current tags | CA=2: Always use new tags
$html .= '<p><a href="javascript:

View file

@ -19,7 +19,7 @@ class Themelet extends BaseThemelet
$h_ext = strtolower($image->ext);
// If file is flash or svg then sets thumbnail to max size.
if ($image->ext === 'swf' || $image->ext === 'svg') {
if ($image->ext === EXTENSION_FLASH || $image->ext === EXTENSION_SVG) {
$tsize = get_thumbnail_size($config->get_int('thumb_width'), $config->get_int('thumb_height'));
} else {
$tsize = get_thumbnail_size($image->width, $image->height);