diff options
author | Marc Alexander <admin@m-a-styles.de> | 2013-10-23 18:34:35 +0200 |
---|---|---|
committer | Marc Alexander <admin@m-a-styles.de> | 2013-10-23 18:34:35 +0200 |
commit | 36d314e032757fc8d86f8f0daddc596e0e95ca8e (patch) | |
tree | 87f3fefcfccf462f5702dff3dd646f6552aecb5d /phpBB/phpbb | |
parent | 789d49359510f10c68be61eaa56d77b3ab428328 (diff) | |
download | forums-36d314e032757fc8d86f8f0daddc596e0e95ca8e.tar forums-36d314e032757fc8d86f8f0daddc596e0e95ca8e.tar.gz forums-36d314e032757fc8d86f8f0daddc596e0e95ca8e.tar.bz2 forums-36d314e032757fc8d86f8f0daddc596e0e95ca8e.tar.xz forums-36d314e032757fc8d86f8f0daddc596e0e95ca8e.zip |
[ticket/11912] Add phpbb mimetype guesser
Mimetype guesser will be used as front-end file for mimetype guessing.
PHPBB3-11912
Diffstat (limited to 'phpBB/phpbb')
-rw-r--r-- | phpBB/phpbb/mimetype/guesser.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/phpBB/phpbb/mimetype/guesser.php b/phpBB/phpbb/mimetype/guesser.php new file mode 100644 index 0000000000..5ae094cd63 --- /dev/null +++ b/phpBB/phpbb/mimetype/guesser.php @@ -0,0 +1,105 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\mimetype; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* @package mimetype +*/ + +class guesser +{ + /** + * @var mimetype guessers + */ + protected $guessers; + + /** + * Construct a mimetype guesser object + * + * @param array $mimetype_guessers Mimetype guesser service collection + */ + public function __construct($mimetype_guessers) + { + $this->register_guessers($mimetype_guessers); + } + + /** + * Register MimeTypeGuessers + * + * @param array $mimetype_guessers Mimetype guesser service collection + * + * @throws \LogicException If incorrect or not mimetype guessers have + * been supplied to class + */ + protected function register_guessers($mimetype_guessers) + { + foreach ($mimetype_guessers as $guesser) + { + $is_supported = (method_exists($guesser, 'is_supported')) ? 'is_supported' : ''; + $is_supported = (method_exists($guesser, 'isSupported')) ? 'isSupported' : $is_supported; + + if (empty($is_supported)) + { + throw new \LogicException('Incorrect mimetype guesser supplied.'); + } + + if ($guesser->$is_supported()) + { + $this->guessers[] = $guesser; + } + } + + if (empty($this->guessers)) + { + throw new \LogicException('No mimetype guesser supplied.'); + } + } + + /** + * Guess mimetype of supplied file + * + * @param string $file Path to file + * + * @return string Guess for mimetype of file + */ + public function guess($file) + { + if (!is_file($file)) + { + return false; + } + + if (!is_readable($file)) + { + return false; + } + + foreach ($this->guessers as $guesser) + { + $mimetype = $guesser->guess($file); + + // Try to guess something that is not the fallback application/octet-stream + if ($mimetype !== null && $mimetype !== 'application/octet-stream') + { + return $mimetype; + } + } + // Return any mimetype if we got a result or the fallback value + return (!empty($mimetype)) ? $mimetype : 'application/octet-stream'; + } +} |