diff --git a/composer.json b/composer.json index b50d3c009..3ebc1d7b6 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ }, "require": { "php": ">=5.3.9", + "ext-fileinfo": "*", "ext-gd": "*", "ext-json": "*", "erusev/parsedown-extra": "^0.7.0", @@ -41,7 +42,6 @@ "symfony/console": "^2.7.5" }, "suggest": { - "ext-fileinfo": "*", "ext-imagick": "*", "ext-ldap": "*", "ext-memcached": "*", diff --git a/core/controllers/components/MimeTypeComponent.php b/core/controllers/components/MimeTypeComponent.php index e7de55c78..b6d377aa1 100644 --- a/core/controllers/components/MimeTypeComponent.php +++ b/core/controllers/components/MimeTypeComponent.php @@ -22,7 +22,7 @@ class MimeTypeComponent extends AppComponent { /** - * Get mime type. + * Get MIME type. * * @param string $filename * @param null|string $alternateName @@ -30,11 +30,11 @@ class MimeTypeComponent extends AppComponent */ public function getType($filename, $alternateName = null) { - if (function_exists('finfo_open') && file_exists($filename)) { + if (file_exists($filename)) { $fileInfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($fileInfo, $filename); finfo_close($fileInfo); - if ($mimeType) { + if ($mimeType !== false) { return $mimeType; } } @@ -55,7 +55,7 @@ public function getType($filename, $alternateName = null) * @param string $filename * @return string */ - public function privFindType($filename) + private function privFindType($filename) { // get base name of the filename provided by user $filename = basename($filename); @@ -72,10 +72,11 @@ public function privFindType($filename) // return mime type for extension if (isset($mimeTypes[$filename])) { return $mimeTypes[$filename]; - // if the extension wasn't found return octet-stream - } else { - return 'application/octet-stream'; + } + + // If the extension was not found, return octet-stream. + return 'application/octet-stream'; } /** @@ -83,7 +84,7 @@ public function privFindType($filename) * * @return array */ - public function privBuildMimeArray() + private function privBuildMimeArray() { return array( '123' => 'application/vnd.lotus-1-2-3', diff --git a/modules/thumbnailcreator/controllers/components/ImagemagickComponent.php b/modules/thumbnailcreator/controllers/components/ImagemagickComponent.php index 10661e8d1..eb71bd188 100644 --- a/modules/thumbnailcreator/controllers/components/ImagemagickComponent.php +++ b/modules/thumbnailcreator/controllers/components/ImagemagickComponent.php @@ -20,179 +20,6 @@ include_once BASE_PATH.'/library/KWUtils.php'; -if (extension_loaded('fileinfo') === false) { - define('FILEINFO_NONE', 0); - define('FILEINFO_SYMLINK', 2); - define('FILEINFO_DEVICES', 8); - define('FILEINFO_CONTINUE', 32); - define('FILEINFO_PRESERVE_ATIME', 128); - define('FILEINFO_RAW', 256); - define('FILEINFO_MIME_TYPE', 16); - define('FILEINFO_MIME_ENCODING', 1024); - define('FILEINFO_MIME', 1040); - - /** - * Return information about a given string. - * - * @param null|int|resource $finfo fileinfo resource - * @param null|string $string content of the file to be checked - * @param int $options fileinfo constant (FILEINFO_NONE | FILEINFO_MIME_TYPE | FILEINFO_MIME_ENCODING | FILEINFO_MIME) - * @param null|resource $context context (not implemented) - * @return false|string a textual description of the given string or false on failure - */ - function finfo_buffer($finfo, $string = null, $options = FILEINFO_NONE, $context = null) - { - if ($finfo !== false && !is_null($string) && is_null($context)) { - if ($options === FILEINFO_NONE && (($finfo & FILEINFO_MIME_TYPE) === FILEINFO_MIME_TYPE || ($finfo & FILEINFO_MIME_ENCODING) === FILEINFO_MIME_ENCODING)) { - $options = $finfo; - } - - if (($options & FILEINFO_MIME_TYPE) === FILEINFO_MIME_TYPE || ($options & FILEINFO_MIME_ENCODING) === FILEINFO_MIME_ENCODING) { - $mimeEncoding = 'binary'; - - if (substr($string, 0, 8) === "\x89PNG\x0d\x0a\x1a\x0a") { - $mimeType = 'image/png'; - } elseif (substr($string, 0, 6) === 'GIF87a' || substr($string, 0, 6) === 'GIF89a') { - $mimeType = 'image/gif'; - } elseif (substr($string, 0, 4) === "MM\x00\x2a" || substr($string, 0, 4) === "II\x2a\x00") { - $mimeType = 'image/tiff'; - } elseif (substr($string, 0, 4) === '8BPS') { - $mimeType = 'image/vnd.adobe.photoshop'; - } elseif (substr($string, 0, 3) === "\xFF\xD8\xFF") { - $mimeType = 'image/jpeg'; - } elseif (substr($string, 0, 2) === 'BM') { - $mimeType = 'image/bmp'; - } elseif (strpos($string, "\x00") !== false) { - $mimeType = 'application/octet-stream'; - } else { - $mimeEncoding = 'utf-8'; - $mimeType = 'text/plain'; - } - - if (($options & FILEINFO_MIME) === FILEINFO_MIME) { - return $mimeType.'; charset='.$mimeEncoding; - } - - if (($options & FILEINFO_MIME_TYPE) === FILEINFO_MIME_TYPE) { - return $mimeType; - } - - return $mimeEncoding; - } - } - - return false; - } - - /** - * Close a given fileinfo resource. - * - * @param null|resource $finfo fileinfo resource - * @return bool true on success or false on failure - */ - function finfo_close($finfo) - { - return $finfo !== false; - } - - /** - * Return information about a given file. - * - * @param null|int|resource $finfo fileinfo resource - * @param null|string $filename name of the file to be checked - * @param int $options fileinfo constant (FILEINFO_NONE | FILEINFO_MIME_TYPE | FILEINFO_MIME_ENCODING | FILEINFO_MIME) - * @param null|resource $context context (partially implemented) - * @return false|string a textual description of the contents of the given file or false on failure - */ - function finfo_file($finfo, $filename = null, $options = FILEINFO_NONE, $context = null) - { - if ($finfo !== false && !is_null($filename)) { - if ($options === FILEINFO_NONE && (($finfo & FILEINFO_MIME_TYPE) === FILEINFO_MIME_TYPE || ($finfo & FILEINFO_MIME_ENCODING) === FILEINFO_MIME_ENCODING)) { - $options = $finfo; - } - - if (($options & FILEINFO_MIME_TYPE) === FILEINFO_MIME_TYPE || ($options & FILEINFO_MIME_ENCODING) === FILEINFO_MIME_ENCODING) { - $mimeType = finfo_buffer($finfo, file_get_contents($filename, false, $context), $options, $context); - - if ($mimeType === false || $mimeType === 'application/octet-stream') { - $extension = strtolower(end(explode('.', basename($filename)))); - - switch ($extension) { - case 'bmp': - $mimeType = 'image/bmp'; - break; - case 'gif': - $mimeType = 'image/gif'; - break; - case 'ico': - $mimeType = 'image/x-icon'; - break; - case 'jpe': - case 'jpeg': - case 'jpg': - $mimeType = 'image/jpeg'; - break; - case 'png': - $mimeType = 'image/png'; - break; - case 'psd': - $mimeType = 'image/vnd.adobe.photoshop'; - break; - case 'tif': - case 'tiff': - $mimeType = 'image/tiff'; - break; - case 'text': - case 'txt': - $mimeType = 'text/plain'; - break; - default: - $mimeType = 'application/octet-stream'; - } - } - - $mimeEncoding = $mimeType === 'text/plain' ? 'utf-8' : 'binary'; - - if (($options & FILEINFO_MIME) === FILEINFO_MIME) { - return $mimeType.'; charset='.$mimeEncoding; - } - - if (($options & FILEINFO_MIME_TYPE) === FILEINFO_MIME_TYPE) { - return $mimeType; - } - - return $mimeEncoding; - } - } - - return false; - } - - /** - * Create a new fileinfo resource. - * - * @param int $options fileinfo constant (FILEINFO_NONE | FILEINFO_MIME_TYPE | FILEINFO_MIME_ENCODING | FILEINFO_MIME) - * @param null|string $magic name of a magic database file (not implemented) - * @return null|int|resource a fileinfo resource on success or false on failure. - */ - function finfo_open($options = FILEINFO_NONE, $magic = null) - { - return is_null($magic) ? $options : false; - } - - /** - * Set the magic configuration options (not implemented). - * - * @param null|int|resource $finfo fileinfo resource - * @param int $options fileinfo constant (FILEINFO_NONE | FILEINFO_MIME_TYPE | FILEINFO_MIME_ENCODING | FILEINFO_MIME) - * @return bool true on success or false on failure - */ - function finfo_set_flags($finfo, $options) - { - return $finfo === $options; - } -} - /** Component used to create thumbnails using phMagick library (on top of ImageMagick) */ class Thumbnailcreator_ImagemagickComponent extends AppComponent {