From e65c0ed123c013c44e89e1cc2bf5c79879a1bbbc Mon Sep 17 00:00:00 2001 From: Jamie Snape Date: Mon, 7 Dec 2015 11:09:57 -0500 Subject: [PATCH 1/2] Always use fileinfo as it is now available on App Engine --- composer.json | 2 +- .../components/MimeTypeComponent.php | 17 +- .../components/ImagemagickComponent.php | 173 ------------------ 3 files changed, 10 insertions(+), 182 deletions(-) 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 { From 2a54a8a7e649f8e25e5920a38a423549a407ba73 Mon Sep 17 00:00:00 2001 From: Jamie Snape Date: Tue, 8 Dec 2015 11:30:41 -0500 Subject: [PATCH 2/2] Applied fixes from StyleCI --- core/controllers/components/DownloadBitstreamComponent.php | 6 +++--- core/controllers/components/MimeTypeComponent.php | 1 - library/KWUtils.php | 4 ++-- modules/oai/library/oai/oaidp-util.php | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/core/controllers/components/DownloadBitstreamComponent.php b/core/controllers/components/DownloadBitstreamComponent.php index a04f2b36f..6af87614a 100644 --- a/core/controllers/components/DownloadBitstreamComponent.php +++ b/core/controllers/components/DownloadBitstreamComponent.php @@ -165,10 +165,10 @@ function env($key) { if ($key == 'HTTPS') { if (isset($_SERVER['HTTPS'])) { - return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'); + return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'; } - return (strpos(env('SCRIPT_URI'), 'https://') === 0); + return strpos(env('SCRIPT_URI'), 'https://') === 0; } if ($key == 'SCRIPT_NAME') { @@ -217,7 +217,7 @@ function env($key) return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME')); break; case 'CGI_MODE': - return (PHP_SAPI === 'cgi'); + return PHP_SAPI === 'cgi'; break; case 'HTTP_BASE': $host = env('HTTP_HOST'); diff --git a/core/controllers/components/MimeTypeComponent.php b/core/controllers/components/MimeTypeComponent.php index b6d377aa1..41208311a 100644 --- a/core/controllers/components/MimeTypeComponent.php +++ b/core/controllers/components/MimeTypeComponent.php @@ -72,7 +72,6 @@ private function privFindType($filename) // return mime type for extension if (isset($mimeTypes[$filename])) { return $mimeTypes[$filename]; - } // If the extension was not found, return octet-stream. diff --git a/library/KWUtils.php b/library/KWUtils.php index a23aff372..0e5b9e767 100644 --- a/library/KWUtils.php +++ b/library/KWUtils.php @@ -84,7 +84,7 @@ public static function createSubDirectories($baseDirectory, $subDirectories, $mo */ public static function isWindows() { - return (strtolower(substr(PHP_OS, 0, 3)) == 'win'); + return strtolower(substr(PHP_OS, 0, 3)) == 'win'; } /** @@ -162,7 +162,7 @@ public static function exec($command, &$output = null, $chdir = '', &$return_val */ public static function isLinux() { - return (strtolower(substr(PHP_OS, 0, 5)) == 'linux'); + return strtolower(substr(PHP_OS, 0, 5)) == 'linux'; } /** diff --git a/modules/oai/library/oai/oaidp-util.php b/modules/oai/library/oai/oaidp-util.php index 838c0086b..b44fda162 100644 --- a/modules/oai/library/oai/oaidp-util.php +++ b/modules/oai/library/oai/oaidp-util.php @@ -22,7 +22,7 @@ function get_token() { list($usec, $sec) = explode(' ', microtime()); - return ((int) ($usec * 1000) + (int) ($sec * 1000)); + return (int) ($usec * 1000) + (int) ($sec * 1000); } function oai_error($code, $argument = '', $value = '') @@ -429,7 +429,7 @@ function php_is_at_least($version) function is_valid_uri($url) { - return ((bool) preg_match("'^[^:]+:(?://)?(?:[a-z_0-9-]+[\.]{1})*(?:[a-z_0-9-]+\.)[a-z]{2,3}.*$'i", $url)); + return (bool) preg_match("'^[^:]+:(?://)?(?:[a-z_0-9-]+[\.]{1})*(?:[a-z_0-9-]+\.)[a-z]{2,3}.*$'i", $url); } function metadataHeader($prefix)