1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation\File\MimeType;
13:
14: use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
15: use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
16:
17: 18: 19: 20: 21:
22: class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
23: {
24: private $cmd;
25:
26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: public function __construct($cmd = 'file -b --mime %s 2>/dev/null')
37: {
38: $this->cmd = $cmd;
39: }
40:
41: 42: 43: 44: 45:
46: public static function isSupported()
47: {
48: return '\\' !== DIRECTORY_SEPARATOR && function_exists('passthru') && function_exists('escapeshellarg');
49: }
50:
51: 52: 53:
54: public function guess($path)
55: {
56: if (!is_file($path)) {
57: throw new FileNotFoundException($path);
58: }
59:
60: if (!is_readable($path)) {
61: throw new AccessDeniedException($path);
62: }
63:
64: if (!self::isSupported()) {
65: return;
66: }
67:
68: ob_start();
69:
70:
71: passthru(sprintf($this->cmd, escapeshellarg($path)), $return);
72: if ($return > 0) {
73: ob_end_clean();
74:
75: return;
76: }
77:
78: $type = trim(ob_get_clean());
79:
80: if (!preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match)) {
81:
82: return;
83: }
84:
85: return $match[1];
86: }
87: }
88: