function testFormat()
 {
     $this->assertNull(MimeMagic::format(true));
     $this->assertNull(MimeMagic::format(5));
     /* Currently arrays aren't validated */
     // $this->assertNull(MimeMagic::format(array('foo' => 'bar')));
     $this->assertNull(MimeMagic::format('does-not-exist.db'));
     $file = $this->TestData->getFile('text-html.snippet.html');
     $this->assertNull(MimeMagic::format($file));
     $file = $this->TestData->getFile('magic.apache.snippet.db');
     $this->assertEqual(MimeMagic::format($file), 'Apache Module mod_mime_magic');
     $file = $this->TestData->getFile('magic.freedesktop.snippet.db');
     $this->assertEqual(MimeMagic::format($file), 'Freedesktop Shared MIME-info Database');
 }
Esempio n. 2
0
 /**
  * Will load a file from various sources
  *
  * Supported formats:
  * - Freedesktop Shared MIME-info Database
  * - Apache Module mod_mime_magic
  * - PHP file containing variables formatted like: $data[0] = array(item, item, item, ...)
  *
  * @param mixed $file An absolute path to a magic file in apache, freedesktop
  * 	or a filename (without .php) of a file in the configs/ dir in CakePHP format
  * @return mixed A format string or null if format could not be determined
  * @access private
  * @link http://httpd.apache.org/docs/2.2/en/mod/mod_mime_magic.html
  * @link http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.13.html
  */
 function __read($db)
 {
     $format = MimeMagic::format($db);
     if ($format === 'Array') {
         foreach ($db as $item) {
             $this->register($item);
         }
     } elseif ($format === 'PHP') {
         include $db;
         foreach ($data as $item) {
             $this->register($item);
         }
     } elseif ($format === 'Freedesktop Shared MIME-info Database') {
         $sectionRegex = '^\\[(\\d{1,3}):([-\\w.\\+]+\\/[-\\w.\\+]+)\\]$';
         $itemRegex = '^(\\d*)\\>+(\\d+)=+([^&~\\+]{2})([^&~\\+]+)&?([^~\\+]*)~?(\\d*)\\+?(\\d*).*$';
         $File = new File($db);
         $File->open('rb');
         $File->offset(12);
         while (!feof($File->handle)) {
             $line = '';
             if (!isset($chars)) {
                 $chars = array(0 => $File->read(1), 1 => $File->read(1));
             } else {
                 $chars = array(0 => $chars[1], 1 => null);
             }
             while (!feof($File->handle) && !($chars[0] === "\n" && (ctype_digit($chars[1]) || $chars[1] === '>' || $chars[1] === '['))) {
                 $line .= $chars[0];
                 $chars = array(0 => $chars[1], 1 => $File->read(1));
             }
             if (preg_match('/' . $sectionRegex . '/', $line, $matches)) {
                 $section = array('priority' => $matches[1], 'mime_type' => $matches[2]);
             } elseif (preg_match('/' . $itemRegex . '/', $line, $matches)) {
                 $indent = empty($matches[1]) ? 0 : intval($matches[1]);
                 $wordSize = empty($matches[6]) ? 1 : intval($matches[6]);
                 $item = array('offset' => intval($matches[2]), 'value_length' => current(unpack('n', $matches[3])), 'value' => $this->__formatValue($matches[4], $wordSize), 'mask' => empty($matches[5]) ? null : $this->__formatValue($matches[5], $wordSize), 'range_length' => empty($matches[7]) ? 1 : intval($matches[7]), 'mime_type' => $section['mime_type']);
                 $this->register($item, $indent, $section['priority']);
             }
         }
     } elseif ($format === 'Apache Module mod_mime_magic') {
         $itemRegex = '^(\\>*)(\\d+)\\t+(\\S+)\\t+([\\S^\\040]+)\\t*([-\\w.\\+]+\\/[-\\w.\\+]+)*\\t*(\\S*)$';
         $File = new File($db);
         $File->open('rb');
         while (!feof($File->handle)) {
             $line = trim(fgets($File->handle));
             if (empty($line) || $line[0] === '#') {
                 continue;
             }
             $line = preg_replace('/(?!\\B)\\040+/', "\t", $line);
             if (!preg_match('/' . $itemRegex . '/', $line, $matches)) {
                 continue;
             }
             $item = array('offset' => intval($matches[2]), 'value' => $this->__formatValue($matches[4], $matches[3], true), 'mask' => null, 'range_length' => 0, 'mime_type' => empty($matches[5]) ? null : $matches[5], 'encoding' => empty($matches[6]) ? null : $matches[6]);
             $item['value_length'] = strlen($item['value']);
             $this->register($item, strlen($matches[1]), 80);
         }
     } else {
         trigger_error('MimeGlob::read - Unknown db format', E_USER_WARNING);
     }
 }