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'); }
/** * 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); } }
function testShippedAnalyze() { $file = dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'vendors' . DS . 'mime_glob.db'; $skip = $this->skipIf(!file_exists($file), '%s. No shipped glob db.'); if ($skip) { /* Skipping does not silence the error */ $this->expectError(); } $Mime = new MimeGlob($file); $this->assertEqual($Mime->analyze('file.3gp'), array('video/3gpp')); $this->assertEqual($Mime->analyze('file.avi'), array('video/x-msvideo')); $this->assertEqual($Mime->analyze('file.bz2'), array('application/x-bzip')); $this->assertEqual($Mime->analyze('file.mp4'), array('video/mp4')); $this->assertEqual($Mime->analyze('file.css'), array('text/css')); $this->assertEqual($Mime->analyze('file.flac'), array('audio/x-flac')); $this->assertEqual($Mime->analyze('file.swf'), array('application/x-shockwave-flash')); $this->assertEqual($Mime->analyze('file.gif'), array('image/gif')); $this->assertEqual($Mime->analyze('file.gz'), array('application/x-gzip')); $this->assertEqual($Mime->analyze('file.html'), array('text/html')); $this->assertEqual($Mime->analyze('file.mp3'), array('audio/mpeg')); $this->assertEqual($Mime->analyze('file.class'), array('application/x-java')); $this->assertEqual($Mime->analyze('file.js'), array('application/javascript')); $this->assertEqual($Mime->analyze('file.jpg'), array('image/jpeg')); $this->assertEqual($Mime->analyze('file.mpeg'), array('video/mpeg')); $this->assertEqual($Mime->analyze('file.ogg'), array('application/ogg', 'audio/x-vorbis+ogg', 'audio/x-flac+ogg', 'audio/x-speex+ogg', 'video/x-theora+ogg')); $this->assertEqual($Mime->analyze('file.php'), array('application/x-php')); $this->assertEqual($Mime->analyze('file.pdf'), array('application/pdf')); $this->assertEqual($Mime->analyze('file.png'), array('image/png')); $this->assertEqual($Mime->analyze('file.ps'), array('application/postscript')); $this->assertEqual($Mime->analyze('file.po'), array('text/x-gettext-translation')); $this->assertEqual($Mime->analyze('file.pot'), array('application/vnd.ms-powerpoint', 'text/x-gettext-translation-template')); $this->assertEqual($Mime->analyze('file.mo'), array('application/x-gettext-translation')); $this->assertEqual($Mime->analyze('file.rm'), array('application/vnd.rn-realmedia')); $this->assertEqual($Mime->analyze('file.rtf'), array('application/rtf')); $this->assertEqual($Mime->analyze('file.txt'), array('text/plain')); /* Fails with text/plain */ // $this->assertEqual($Mime->analyze('file.doc'), array('application/msword', 'application/msword')); /* This really shouldn't fail */ // $this->assertEqual($Mime->analyze('file.docx'), array('application/vnd.openxmlformats-officedocument.wordprocessingml.document')); $this->assertEqual($Mime->analyze('file.odt'), array('application/vnd.oasis.opendocument.text')); $this->assertEqual($Mime->analyze('file.tar'), array('application/x-tar')); $this->assertEqual($Mime->analyze('file.wav'), array('audio/x-wav')); $this->assertEqual($Mime->analyze('file.xhtml'), array('application/xhtml+xml')); $this->assertEqual($Mime->analyze('file.xml'), array('application/xml')); }