Subversion Repositories fileformats

Rev

Rev 5 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * File Type Detection for PHP
  5.  * Copyright 2020 - 2021 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  *    Revision 2021-05-21
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License");
  10.  * you may not use this file except in compliance with the License.
  11.  * You may obtain a copy of the License at
  12.  *
  13.  *     http://www.apache.org/licenses/LICENSE-2.0
  14.  *
  15.  * Unless required by applicable law or agreed to in writing, software
  16.  * distributed under the License is distributed on an "AS IS" BASIS,
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18.  * See the License for the specific language governing permissions and
  19.  * limitations under the License.
  20.  */
  21.  
  22. class VtsFileTypeDetect {
  23.  
  24.         public static function getMimeType($filename) {
  25.                 $mime_types = array();
  26.  
  27.                 include __DIR__ . '/mimetype_lookup.inc.php';
  28.  
  29.                 /** @phpstan-ignore-next-line */
  30.                 foreach ($mime_types as $ext => $mime) {
  31.                         if (strtoupper(substr($filename, -strlen($ext)-1)) == strtoupper('.'.$ext)) {
  32.                                 return $mime;
  33.                         }
  34.                 }
  35.  
  36.                 return false;
  37.         }
  38.  
  39.         public static function getDescription($file, $filenames=array(__DIR__.'/filetypes.local', __DIR__.'/filetypes.conf')) {
  40.                 // TODO: Make it multi-lang
  41.  
  42.                 $inis = array();
  43.                 foreach ($filenames as $num => $filename) {
  44.                         $inis[$num] = !file_exists($filename) ? array() : parse_ini_file($filename, true, INI_SCANNER_RAW);
  45.                         if (!isset($inis[$num]['OidHeader']))     $inis[$num]['OidHeader']     = array();
  46.                         if (!isset($inis[$num]['GuidHeader']))    $inis[$num]['GuidHeader']    = array();
  47.                         if (!isset($inis[$num]['FileExtension'])) $inis[$num]['FileExtension'] = array();
  48.                         if (!isset($inis[$num]['MimeType']))      $inis[$num]['MimeType']      = array();
  49.                 }
  50.  
  51.                 if (is_readable($file)) {
  52.                         $h = fopen($file, 'r');
  53.                         $line = trim(fgets($h, 128));
  54.                         if ((substr($line,0,1) == '[') && ($line[strlen($line)-1] == ']')) {
  55.                                 $line = substr($line, 1, strlen($line)-2);
  56.                                 foreach ($inis as $ini) {
  57.                                         if (isset($ini['OidHeader'][$line]))  return $ini['OidHeader'][$line];
  58.                                 }
  59.                                 foreach ($inis as $ini) {
  60.                                         if (isset($ini['GuidHeader'][$line])) return $ini['GuidHeader'][$line];
  61.                                 }
  62.                         }
  63.                         fclose($h);
  64.                 }
  65.  
  66.                 foreach ($inis as $ini) {
  67.                         foreach ($ini['FileExtension'] as $ext => $name) {
  68.                                 if (strtoupper(substr($file, -strlen($ext)-1)) == strtoupper('.'.$ext)) {
  69.                                         return $name;
  70.                                 }
  71.                         }
  72.                 }
  73.  
  74.                 $mime = false;
  75.                 if (function_exists('mime_content_type')) {
  76.                         $mime = @mime_content_type($file);
  77.                 }
  78.                 if (!$mime) {
  79.                         $mime = self::getMimeType($file);
  80.                 }
  81.                 if ($mime) {
  82.                         foreach ($inis as $ini) {
  83.                                 if (isset($ini['MimeType'][$mime]))  return $ini['MimeType'][$mime];
  84.                         }
  85.                 }
  86.  
  87.                 foreach ($inis as $ini) {
  88.                         if (isset($ini['Static']['LngUnknown'])) {
  89.                                 return $ini['Static']['LngUnknown'];
  90.                         }
  91.                 }
  92.                 return 'Unknown';
  93.         }
  94.  
  95. }
  96.