本文整理汇总了PHP中MimeMagic类的典型用法代码示例。如果您正苦于以下问题:PHP MimeMagic类的具体用法?PHP MimeMagic怎么用?PHP MimeMagic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MimeMagic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: MimeMagicInit
/**
* Hook: MimeMagicInit
*
* @param MimeMagic $mimeMagic
* @param object $addToList Callback function
* @return bool Always true
*/
public static function MimeMagicInit($mimeMagic)
{
static $extraTypes = 'model/x-pdb pdb';
static $extraInfo = 'model/x-pdb [MODEL]';
$mimeMagic->addExtraTypes($extraTypes);
$mimeMagic->addExtraInfo($extraInfo);
return true;
}
示例2: execute
/**
* This is called upon loading the special page. It should write output to the page with $wgOut.
*
* @param string $par The portion of the URI after Special:StaticDocServer/
*/
public function execute($par)
{
#TODO: switch to $this->getOuput() and $this->getRequest() when we upgrade MW
global $wgOut, $wgRequest;
$wgOut->disable();
$found = FALSE;
list($productName, $versionName, $path) = explode('/', $par, 3);
if (substr($par, -1, 1) == '/') {
$par .= 'index.html';
}
// Validate parameters are set
if (isset($productName) && isset($versionName) && PonyDocsProduct::GetProductByShortName($productName) && PonyDocsProductVersion::GetVersionByName($productName, $versionName)) {
$filename = PONYDOCS_STATIC_DIR . "/{$par}";
if (file_exists($filename)) {
$found = TRUE;
}
}
if (!$found) {
$wgRequest->response()->header("HTTP/1.1 404 Not Found");
echo "<html>\n";
echo "<head><title>Not Found</title></head>\n";
echo "<body>\n";
echo "<h1>Bad Request</h1>\n";
echo "<div>The documentation you have requested does not exist.</div>\n";
echo "</body>\n";
echo "</html>\n";
} else {
$mimeMagic = MimeMagic::singleton();
$pathParts = pathinfo($filename);
/* get mime-type for a specific file */
header('Content-type: ' . $mimeMagic->guessTypesForExtension($pathParts['extension']));
readfile($filename);
}
}
示例3: showFile
function showFile()
{
$i = strpos($this->mBasePath, '/');
$i = $i === false ? 0 : $i + 1;
$filename = substr($this->mBasePath, $i);
$i = strpos($filename, '.');
if ($i !== false) {
$ext = substr($filename, $i + 1);
$mime = MimeMagic::singleton();
$ctype = $mime->guessTypesForExtension($ext);
} else {
$ctype = 'application/octet-stream';
}
$contents = $this->svn->getFile($this->mBasePath, $this->mRev == 'HEAD' ? null : $this->mRev);
if ($this->mAction == 'raw' || $this->mAction == 'download') {
global $wgOut;
$wgOut->disable();
if ($this->mAction == 'raw') {
header('Content-Type: ' . $ctype);
} else {
header('Content-Type: application/octet-stream');
}
header('Content-Length: ' . strlen($contents));
print $contents;
return;
}
if (substr($ctype, 0, 5) == 'text/') {
return Xml::element('pre', array('style' => 'overflow: auto;'), $contents);
} else {
return wfMsgHTML('codebrowse-binary-file');
}
}
示例4: getMimeType
function getMimeType()
{
if (!isset($this->mime)) {
$magic = MimeMagic::singleton();
$this->mime = $magic->guessMimeType($this->path);
}
return $this->mime;
}
示例5: dataFile
/**
* Utility function: Get a new file object for a file on disk but not actually in db.
*
* File must be in the path returned by getFilePath()
* @param string $name File name
* @param string $type MIME type [optional]
* @return UnregisteredLocalFile
*/
protected function dataFile($name, $type = null)
{
if (!$type) {
// Autodetect by file extension for the lazy.
$magic = MimeMagic::singleton();
$parts = explode($name, '.');
$type = $magic->guessTypesForExtension($parts[count($parts) - 1]);
}
return new UnregisteredLocalFile(false, $this->repo, "mwstore://localtesting/data/{$name}", $type);
}
示例6: _displayFile
private function _displayFile()
{
$registry = Zend_Registry::getInstance();
// Print the contents of the file
$file = $registry->configuration->testcase->dir . '/' . $this->getRequest()->getParam(2);
if ($stream = fopen($file, 'r')) {
// Disable layout
$layout = Zend_Layout::getMvcInstance();
$layout->disableLayout();
/// @todo: output headers and file information
$mm = new MimeMagic();
/// @todo: fix in mimemagic instead
header('Content-type: ' . $mm->get(realpath($file)));
echo stream_get_contents($stream);
fclose($stream);
} else {
throw new Exception("Attempted to view a non-existing file \"{$file}\".");
}
}
示例7: testFormat
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');
}
示例8: wfGetType
/** */
function wfGetType($filename, $safe = true)
{
global $wgTrivialMimeDetection;
$ext = strrchr($filename, '.');
$ext = $ext === false ? '' : strtolower(substr($ext, 1));
# trivial detection by file extension,
# used for thumbnails (thumb.php)
if ($wgTrivialMimeDetection) {
switch ($ext) {
case 'gif':
return 'image/gif';
case 'png':
return 'image/png';
case 'jpg':
return 'image/jpeg';
case 'jpeg':
return 'image/jpeg';
}
return 'unknown/unknown';
}
$magic = MimeMagic::singleton();
// Use the extension only, rather than magic numbers, to avoid opening
// up vulnerabilities due to uploads of files with allowed extensions
// but disallowed types.
$type = $magic->guessTypesForExtension($ext);
/**
* Double-check some security settings that were done on upload but might
* have changed since.
*/
if ($safe) {
global $wgFileBlacklist, $wgCheckFileExtensions, $wgStrictFileExtensions, $wgFileExtensions, $wgVerifyMimeType, $wgMimeTypeBlacklist, $wgRequest;
$form = new UploadForm($wgRequest);
list($partName, $extList) = $form->splitExtensions($filename);
if ($form->checkFileExtensionList($extList, $wgFileBlacklist)) {
return 'unknown/unknown';
}
if ($wgCheckFileExtensions && $wgStrictFileExtensions && !$form->checkFileExtensionList($extList, $wgFileExtensions)) {
return 'unknown/unknown';
}
if ($wgVerifyMimeType && in_array(strtolower($type), $wgMimeTypeBlacklist)) {
return 'unknown/unknown';
}
}
return $type;
}
示例9: setType
function setType( $image ) {
$this->mimetype = $image->getMimeType();
$this->mediatype = $image->getMediaType();
#FIXME: ugly hack! do this on upload!
if ( $this->mimetype == 'unknown/unknown' ) {
$mime = MimeMagic::singleton();
$ext = preg_replace('!^.*\.([^.]+)$!', '\1', $image->getName());
$this->mimetype = $mime->guessTypesForExtension($ext);
$this->mediatype = $mime->getMediaType(null, $this->mimetype);
}
if ( preg_match('!/(x-)?ogg$!', $this->mimetype) ) {
if ( $this->mediatype == MEDIATYPE_AUDIO ) $this->mimetype = 'audio/ogg';
elseif ( $this->mediatype == MEDIATYPE_VIDEO ) $this->mimetype = 'video/ogg';
else $this->mimetype = 'application/ogg';
}
}
示例10: wfGetType
/** */
function wfGetType($filename)
{
global $wgTrivialMimeDetection;
# trivial detection by file extension,
# used for thumbnails (thumb.php)
if ($wgTrivialMimeDetection) {
$ext = strtolower(strrchr($filename, '.'));
switch ($ext) {
case '.gif':
return 'image/gif';
case '.png':
return 'image/png';
case '.jpg':
return 'image/jpeg';
case '.jpeg':
return 'image/jpeg';
}
return 'unknown/unknown';
} else {
$magic =& MimeMagic::singleton();
return $magic->guessMimeType($filename);
//full fancy mime detection
}
}
示例11: MimeMagic
/**
* Get an instance of this class
* @return MimeMagic
*/
public static function &singleton()
{
if (!isset(self::$instance)) {
self::$instance = new MimeMagic();
}
return self::$instance;
}
示例12: getMediaType
function getMediaType()
{
$magic = MimeMagic::singleton();
return $magic->getMediaType(null, $this->getMimeType());
}
示例13: getPropsFromPath
/**
* Get an associative array containing information about a file in the local filesystem.
*
* @param string $path Absolute local filesystem path
* @param mixed $ext The file extension, or true to extract it from the filename.
* Set it to false to ignore the extension.
*/
static function getPropsFromPath($path, $ext = true)
{
wfProfileIn(__METHOD__);
wfDebug(__METHOD__ . ": Getting file info for {$path}\n");
$info = array('fileExists' => file_exists($path) && !is_dir($path));
$gis = false;
if ($info['fileExists']) {
$magic = MimeMagic::singleton();
$info['mime'] = $magic->guessMimeType($path, $ext);
list($info['major_mime'], $info['minor_mime']) = self::splitMime($info['mime']);
$info['media_type'] = $magic->getMediaType($path, $info['mime']);
# Get size in bytes
$info['size'] = filesize($path);
# Height, width and metadata
$handler = MediaHandler::getHandler($info['mime']);
if ($handler) {
$tempImage = (object) array();
$info['metadata'] = $handler->getMetadata($tempImage, $path);
$gis = $handler->getImageSize($tempImage, $path, $info['metadata']);
} else {
$gis = false;
$info['metadata'] = '';
}
$info['sha1'] = self::sha1Base36($path);
wfDebug(__METHOD__ . ": {$path} loaded, {$info['size']} bytes, {$info['mime']}.\n");
} else {
$info['mime'] = NULL;
$info['media_type'] = MEDIATYPE_UNKNOWN;
$info['metadata'] = '';
$info['sha1'] = '';
wfDebug(__METHOD__ . ": {$path} NOT FOUND!\n");
}
if ($gis) {
# NOTE: $gis[2] contains a code for the image type. This is no longer used.
$info['width'] = $gis[0];
$info['height'] = $gis[1];
if (isset($gis['bits'])) {
$info['bits'] = $gis['bits'];
} else {
$info['bits'] = 0;
}
} else {
$info['width'] = 0;
$info['height'] = 0;
$info['bits'] = 0;
}
wfProfileOut(__METHOD__);
return $info;
}
示例14: __read
/**
* 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);
}
}
示例15: getThumbType
function getThumbType($ext, $mime)
{
global $wgDjvuOutputExtension;
static $mime;
if (!isset($mime)) {
$magic = MimeMagic::singleton();
$mime = $magic->guessTypesForExtension($wgDjvuOutputExtension);
}
return array($wgDjvuOutputExtension, $mime);
}