1 <?php 2 /** 3 * MIME detection code. 4 * 5 * @todo Maybe we could try to use fileinfo module if loaded 6 */ 7 8 declare(strict_types=1); 9 10 namespace PhpMyAdmin; 11 12 use function chr; 13 use function mb_strlen; 14 use function mb_substr; 15 use function substr; 16 17 /** 18 * PhpMyAdmin\Mime class; 19 */ 20 class Mime 21 { 22 /** 23 * Tries to detect MIME type of content. 24 * 25 * @param string $test First few bytes of content to use for detection 26 * 27 * @return string 28 */ 29 public static function detect(&$test) 30 { 31 $len = mb_strlen($test); 32 if ($len >= 2 && $test[0] == chr(0xff) && $test[1] == chr(0xd8)) { 33 return 'image/jpeg'; 34 } 35 if ($len >= 3 && substr($test, 0, 3) === 'GIF') { 36 return 'image/gif'; 37 } 38 if ($len >= 4 && mb_substr($test, 0, 4) == "\x89PNG") { 39 return 'image/png'; 40 } 41 42 return 'application/octet-stream'; 43 } 44 }