[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/mimetype/ -> guesser.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\mimetype;
  15  
  16  class guesser
  17  {
  18      /**
  19      * @const Default priority for mimetype guessers
  20      */
  21      const PRIORITY_DEFAULT = 0;
  22  
  23      /**
  24      * @var array guessers
  25      */
  26      protected $guessers;
  27  
  28      /**
  29      * Construct a mimetype guesser object
  30      *
  31      * @param array $mimetype_guessers Mimetype guesser service collection
  32      */
  33  	public function __construct($mimetype_guessers)
  34      {
  35          $this->register_guessers($mimetype_guessers);
  36      }
  37  
  38      /**
  39      * Register MimeTypeGuessers and sort them by priority
  40      *
  41      * @param array $mimetype_guessers Mimetype guesser service collection
  42      *
  43      * @throws \LogicException If incorrect or not mimetype guessers have
  44      *            been supplied to class
  45      */
  46  	protected function register_guessers($mimetype_guessers)
  47      {
  48          foreach ($mimetype_guessers as $guesser)
  49          {
  50              $is_supported = (method_exists($guesser, 'is_supported')) ? 'is_supported' : '';
  51              $is_supported = (method_exists($guesser, 'isSupported')) ? 'isSupported' : $is_supported;
  52  
  53              if (empty($is_supported))
  54              {
  55                  throw new \LogicException('Incorrect mimetype guesser supplied.');
  56              }
  57  
  58              if ($guesser->$is_supported())
  59              {
  60                  $this->guessers[] = $guesser;
  61              }
  62          }
  63  
  64          if (empty($this->guessers))
  65          {
  66              throw new \LogicException('No mimetype guesser supplied.');
  67          }
  68  
  69          // Sort guessers by priority
  70          usort($this->guessers, array($this, 'sort_priority'));
  71      }
  72  
  73      /**
  74      * Sort the priority of supplied guessers
  75      * This is a compare function for usort. A guesser with higher priority
  76      * should be used first and vice versa. usort() orders the array values
  77      * from low to high depending on what the comparison function returns
  78      * to it. Return value should be smaller than 0 if value a is smaller
  79      * than value b. This has been reversed in the comparision function in
  80      * order to sort the guessers from high to low.
  81      * Method has been set to public in order to allow proper testing.
  82      *
  83      * @param object $guesser_a Mimetype guesser a
  84      * @param object $guesser_b Mimetype guesser b
  85      *
  86      * @return int     If both guessers have the same priority 0, bigger
  87      *        than 0 if first guesser has lower priority, and lower
  88      *        than 0 if first guesser has higher priority
  89      */
  90  	public function sort_priority($guesser_a, $guesser_b)
  91      {
  92          $priority_a = (int) (method_exists($guesser_a, 'get_priority')) ? $guesser_a->get_priority() : self::PRIORITY_DEFAULT;
  93          $priority_b = (int) (method_exists($guesser_b, 'get_priority')) ? $guesser_b->get_priority() : self::PRIORITY_DEFAULT;
  94  
  95          return $priority_b - $priority_a;
  96      }
  97  
  98      /**
  99      * Guess mimetype of supplied file
 100      *
 101      * @param string $file Path to file
 102      * @param string $file_name The real file name
 103      *
 104      * @return string Guess for mimetype of file
 105      */
 106  	public function guess($file, $file_name = '')
 107      {
 108          if (!is_file($file))
 109          {
 110              return false;
 111          }
 112  
 113          if (!is_readable($file))
 114          {
 115              return false;
 116          }
 117  
 118          $mimetype = 'application/octet-stream';
 119  
 120          foreach ($this->guessers as $guesser)
 121          {
 122              $mimetype_guess = $guesser->guess($file, $file_name);
 123  
 124              $mimetype = $this->choose_mime_type($mimetype, $mimetype_guess);
 125          }
 126          // Return any mimetype if we got a result or the fallback value
 127          return $mimetype;
 128      }
 129  
 130      /**
 131       * Choose the best mime type based on the current mime type and the guess
 132       * If a guesser returns nulls or application/octet-stream, we will keep
 133       * the current guess. Guesses with a slash inside them will be favored over
 134       * already existing ones. However, any guess that will pass the first check
 135       * will always overwrite the default application/octet-stream.
 136       *
 137       * @param    string    $mime_type    The current mime type
 138       * @param    string    $guess        The current mime type guess
 139       *
 140       * @return string The best mime type based on current mime type and guess
 141       */
 142  	public function choose_mime_type($mime_type, $guess)
 143      {
 144          if ($guess === null || $guess == 'application/octet-stream')
 145          {
 146              return $mime_type;
 147          }
 148  
 149          if ($mime_type == 'application/octet-stream' || strpos($guess, '/') !== false)
 150          {
 151              $mime_type = $guess;
 152          }
 153  
 154          return $mime_type;
 155      }
 156  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1