Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

guesser.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 4.13 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\mimetype;
 015   
 016  class guesser
 017  {
 018      /**
 019      * @const Default priority for mimetype guessers
 020      */
 021      const PRIORITY_DEFAULT = 0;
 022   
 023      /**
 024      * @var array guessers
 025      */
 026      protected $guessers;
 027   
 028      /**
 029      * Construct a mimetype guesser object
 030      *
 031      * @param array $mimetype_guessers Mimetype guesser service collection
 032      */
 033      public function __construct($mimetype_guessers)
 034      {
 035          $this->register_guessers($mimetype_guessers);
 036      }
 037   
 038      /**
 039      * Register MimeTypeGuessers and sort them by priority
 040      *
 041      * @param array $mimetype_guessers Mimetype guesser service collection
 042      *
 043      * @throws \LogicException If incorrect or not mimetype guessers have
 044      *            been supplied to class
 045      */
 046      protected function register_guessers($mimetype_guessers)
 047      {
 048          foreach ($mimetype_guessers as $guesser)
 049          {
 050              $is_supported = (method_exists($guesser, 'is_supported')) ? 'is_supported' : '';
 051              $is_supported = (method_exists($guesser, 'isSupported')) ? 'isSupported' : $is_supported;
 052   
 053              if (empty($is_supported))
 054              {
 055                  throw new \LogicException('Incorrect mimetype guesser supplied.');
 056              }
 057   
 058              if ($guesser->$is_supported())
 059              {
 060                  $this->guessers[] = $guesser;
 061              }
 062          }
 063   
 064          if (empty($this->guessers))
 065          {
 066              throw new \LogicException('No mimetype guesser supplied.');
 067          }
 068   
 069          // Sort guessers by priority
 070          usort($this->guessers, array($this, 'sort_priority'));
 071      }
 072   
 073      /**
 074      * Sort the priority of supplied guessers
 075      * This is a compare function for usort. A guesser with higher priority
 076      * should be used first and vice versa. usort() orders the array values
 077      * from low to high depending on what the comparison function returns
 078      * to it. Return value should be smaller than 0 if value a is smaller
 079      * than value b. This has been reversed in the comparision function in
 080      * order to sort the guessers from high to low.
 081      * Method has been set to public in order to allow proper testing.
 082      *
 083      * @param object $guesser_a Mimetype guesser a
 084      * @param object $guesser_b Mimetype guesser b
 085      *
 086      * @return int     If both guessers have the same priority 0, bigger
 087      *        than 0 if first guesser has lower priority, and lower
 088      *        than 0 if first guesser has higher priority
 089      */
 090      public function sort_priority($guesser_a, $guesser_b)
 091      {
 092          $priority_a = (int) (method_exists($guesser_a, 'get_priority')) ? $guesser_a->get_priority() : self::PRIORITY_DEFAULT;
 093          $priority_b = (int) (method_exists($guesser_b, 'get_priority')) ? $guesser_b->get_priority() : self::PRIORITY_DEFAULT;
 094   
 095          return $priority_b - $priority_a;
 096      }
 097   
 098      /**
 099      * 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  }
 157