[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/MIME/ -> Magic.php (source)

   1  <?php
   2  /**
   3   * The MIME_Magic:: class provides an interface to determine a
   4   * MIME type for various content, if it provided with different
   5   * levels of information.
   6   *
   7   * $Horde: framework/MIME/MIME/Magic.php,v 1.52.8.12 2006/01/01 21:28:24 jan Exp $
   8   *
   9   * Copyright 1999-2006 Anil Madhavapeddy <anil@recoil.org>
  10   * Copyright 2002-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
  11   *
  12   * See the enclosed file COPYING for license information (LGPL). If you
  13   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  14   *
  15   * @author  Anil Madhavapeddy <anil@recoil.org>
  16   * @author  Michael Slusarz <slusarz@bigworm.colorado.edu>
  17   * @since   Horde 1.3
  18   * @package Horde_MIME
  19   */
  20  class MIME_Magic {
  21  
  22      /**
  23       * Returns a copy of the MIME extension map.
  24       *
  25       * @access private
  26       *
  27       * @return array  The MIME extension map.
  28       */
  29      function &_getMimeExtensionMap()
  30      {
  31          static $mime_extension_map;
  32  
  33          if (!isset($mime_extension_map)) {
  34              require dirname(__FILE__) . '/mime.mapping.php';
  35          }
  36  
  37          return $mime_extension_map;
  38      }
  39  
  40      /**
  41       * Returns a copy of the MIME magic file.
  42       *
  43       * @access private
  44       *
  45       * @return array  The MIME magic file.
  46       */
  47      function &_getMimeMagicFile()
  48      {
  49          static $mime_magic;
  50  
  51          if (!isset($mime_magic)) {
  52              require dirname(__FILE__) . '/mime.magic.php';
  53          }
  54  
  55          return $mime_magic;
  56      }
  57  
  58      /**
  59       * Attempt to convert a file extension to a MIME type, based
  60       * on the global Horde and application specific config files.
  61       *
  62       * If we cannot map the file extension to a specific type, then
  63       * we fall back to a custom MIME handler 'x-extension/$ext', which
  64       * can be used as a normal MIME type internally throughout Horde.
  65       *
  66       * @param string $ext  The file extension to be mapped to a MIME type.
  67       *
  68       * @return string  The MIME type of the file extension.
  69       */
  70      function extToMIME($ext)
  71      {
  72          if (empty($ext)) {
  73             return 'application/octet-stream';
  74          } else {
  75              $ext = String::lower($ext);
  76              $map = &MIME_Magic::_getMimeExtensionMap();
  77              $pos = 0;
  78              while (!isset($map[$ext]) && $pos !== false) {
  79                  $pos = strpos($ext, '.');
  80                  if ($pos !== false) {
  81                      $ext = substr($ext, $pos + 1);
  82                  }
  83              }
  84  
  85              if (isset($map[$ext])) {
  86                  return $map[$ext];
  87              } else {
  88                  return 'x-extension/' . $ext;
  89              }
  90          }
  91      }
  92  
  93      /**
  94       * Attempt to convert a filename to a MIME type, based on the global Horde
  95       * and application specific config files.
  96       *
  97       * @param string $filename  The filename to be mapped to a MIME type.
  98       * @param boolean $unknown  How should unknown extensions be handled? If
  99       *                          true, will return 'x-extension/*' types.  If
 100       *                          false, will return 'application/octet-stream'.
 101       *
 102       * @return string  The MIME type of the filename.
 103       */
 104      function filenameToMIME($filename, $unknown = true)
 105      {
 106          $pos = strlen($filename) + 1;
 107          $type = '';
 108  
 109          $map = &MIME_Magic::_getMimeExtensionMap();
 110          for ($i = 0;
 111               $i <= $map['__MAXPERIOD__'] &&
 112                   strrpos(substr($filename, 0, $pos - 1), '.') !== false;
 113               $i++) {
 114              $pos = strrpos(substr($filename, 0, $pos - 1), '.') + 1;
 115          }
 116          $type = MIME_Magic::extToMIME(substr($filename, $pos));
 117  
 118          if (empty($type) ||
 119              (!$unknown && (strpos($type, 'x-extension') !== false))) {
 120              return 'application/octet-stream';
 121          } else {
 122              return $type;
 123          }
 124      }
 125  
 126      /**
 127       * Attempt to convert a MIME type to a file extension, based
 128       * on the global Horde and application specific config files.
 129       *
 130       * If we cannot map the type to a file extension, we return false.
 131       *
 132       * @param string $type  The MIME type to be mapped to a file extension.
 133       *
 134       * @return string  The file extension of the MIME type.
 135       */
 136      function MIMEToExt($type)
 137      {
 138          if (empty($type)) {
 139              return false;
 140          }
 141  
 142          $key = array_search($type, MIME_Magic::_getMimeExtensionMap());
 143          if ($key === false) {
 144              list($major, $minor) = explode('/', $type);
 145              if ($major == 'x-extension') {
 146                  return $minor;
 147              }
 148              if (strpos($minor, 'x-') === 0) {
 149                  return substr($minor, 2);
 150              }
 151              return false;
 152          } else {
 153              return $key;
 154          }
 155      }
 156  
 157      /**
 158       * Uses variants of the UNIX "file" command to attempt to determine the
 159       * MIME type of an unknown file.
 160       *
 161       * @param string $path      The path to the file to analyze.
 162       * @param string $magic_db  Path to the mime magic database.
 163       *
 164       * @return string  The MIME type of the file.  Returns false if the file
 165       *                 type isn't recognized or an error happened.
 166       */
 167      function analyzeFile($path, $magic_db = null)
 168      {
 169          /* If the PHP Mimetype extension is available, use that. */
 170          if (Util::extensionExists('fileinfo')) {
 171              if (empty($magic_db)) {
 172                  $res = @finfo_open(FILEINFO_MIME);
 173              } else {
 174                  $res = @finfo_open(FILEINFO_MIME, $magic_db);
 175              }
 176              if ($res) {
 177                  $type = finfo_file($res, $path);
 178                  finfo_close($res);
 179  
 180                  /* Remove any additional information. */
 181                  foreach (array(';', ',', '\\0') as $separator) {
 182                      $pos = strpos($type, $separator);
 183                      if ($pos !== false) {
 184                          $type = rtrim(substr($type, 0, $pos));
 185                      }
 186                  }
 187  
 188                  if (preg_match('|^[a-z0-9]+/[.-a-z0-9]+$|i', $type)) {
 189                      return $type;
 190                  }
 191              }
 192          }
 193  
 194          if (Util::extensionExists('mime_magic')) {
 195              return trim(mime_content_type($path));
 196          } else {
 197              /* Use a built-in magic file. */
 198              $mime_magic = &MIME_Magic::_getMimeMagicFile();
 199              if (!($fp = @fopen($path, 'rb'))) {
 200                  return false;
 201              }
 202              foreach ($mime_magic as $offset => $odata) {
 203                  foreach ($odata as $length => $ldata) {
 204                      @fseek($fp, $offset, SEEK_SET);
 205                      $lookup = @fread($fp, $length);
 206                      if (!empty($ldata[$lookup])) {
 207                          fclose($fp);
 208                          return $ldata[$lookup];
 209                      }
 210                  }
 211              }
 212              fclose($fp);
 213          }
 214  
 215          return false;
 216      }
 217  
 218  
 219      /**
 220       * Uses variants of the UNIX "file" command to attempt to determine the
 221       * MIME type of an unknown byte stream.
 222       *
 223       * @param string $data      The file data to analyze.
 224       * @param string $magic_db  Path to the mime magic database.
 225       *
 226       * @return string  The MIME type of the file.  Returns false if the file
 227       *                 type isn't recognized or an error happened.
 228       */
 229      function analyzeData($data, $magic_db = null)
 230      {
 231          /* If the PHP Mimetype extension is available, use that. */
 232          if (Util::extensionExists('fileinfo')) {
 233              if (empty($magic_db)) {
 234                  $res = @finfo_open(FILEINFO_MIME);
 235              } else {
 236                  $res = @finfo_open(FILEINFO_MIME, $magic_db);
 237              }
 238              if (!$res) {
 239                  return false;
 240              }
 241  
 242              $type = finfo_buffer($res, $data);
 243              finfo_close($res);
 244  
 245              /* Remove any additional information. */
 246              $pos = strpos($type, ';');
 247              if ($pos !== false) {
 248                  $type = rtrim(substr($type, 0, $pos));
 249              }
 250              $pos = strpos($type, ',');
 251              if ($pos !== false) {
 252                  $type = rtrim(substr($type, 0, $pos));
 253              }
 254              return $type;
 255          }
 256  
 257          /* Use a built-in magic file. */
 258          $mime_magic = &MIME_Magic::_getMimeMagicFile();
 259          foreach ($mime_magic as $offset => $odata) {
 260              foreach ($odata as $length => $ldata) {
 261                  $lookup = substr($data, $offset, $length);
 262                  if (!empty($ldata[$lookup])) {
 263                      return $ldata[$lookup];
 264                  }
 265              }
 266          }
 267  
 268          return false;
 269      }
 270  
 271  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7