Example #1
0
 function testFormat()
 {
     $this->assertNull(MimeGlob::format(true));
     $this->assertNull(MimeGlob::format(5));
     //		$this->assertNull(MimeGlob::format(array('foo' => 'bar')));
     $this->assertNull(MimeGlob::format('does-not-exist.db'));
     $file = $this->TestData->getFile('glob.apache.snippet.db');
     $this->assertEqual(MimeGlob::format($file), 'Apache Module mod_mime');
     $file = $this->TestData->getFile('glob.freedesktop.snippet.db');
     $this->assertEqual(MimeGlob::format($file), 'Freedesktop Shared MIME-info Database');
 }
Example #2
0
 /**
  * Will load a file from various sources
  *
  * Supported formats:
  * - Freedesktop Shared MIME-info Database
  * - Apache Module mod_mime
  * - PHP file containing variables formatted like: $data[0] = array(item, item, item, ...)
  *
  * @param mixed $file An absolute path to a glob 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.html
  * @link http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.13.html
  */
 function __read($db)
 {
     $format = MimeGlob::format($db);
     if ($format === 'Array') {
         foreach ($db as $item) {
             $this->register($item);
         }
     } elseif ($format === 'PHP') {
         include $db;
         foreach ($config as $item) {
             $this->register($item);
         }
     } elseif ($format === 'Freedesktop Shared MIME-info Database') {
         $File =& new File($db);
         $File->open('rb');
         while (!feof($File->handle)) {
             $line = trim(fgets($File->handle));
             if (empty($line) || $line[0] === '#') {
                 continue;
             }
             $line = explode(':', $line);
             if (count($line) > 2) {
                 $priority = array_shift($line);
             } else {
                 $priority = null;
             }
             if (!preg_match('/(\\*\\.)?[a-zA-Z0-9\\.]+$|/', $line[1])) {
                 continue;
             }
             $this->register(array('mime_type' => array_shift($line), 'pattern' => str_replace('*.', null, array_shift($line)), 'priority' => $priority));
         }
     } elseif ($format === 'Apache Module mod_mime') {
         $File = new File($db);
         $File->open('rb');
         while (!feof($File->handle)) {
             $line = trim(fgets($File->handle));
             if (empty($line) || $line[0] === '#') {
                 continue;
             }
             $line = preg_split('/\\s+/', $line);
             $this->register(array('mime_type' => array_shift($line), 'pattern' => $line));
         }
     } else {
         trigger_error('MimeGlob::read - Unknown db format', E_USER_WARNING);
     }
 }