cvs: pear /MIME_Type Type.php example.php
| From: | Christian Weiske | Date: | Sun, 25 Mar 2007 06:40:22 +0000 |
| Subject: | cvs: pear /MIME_Type Type.php example.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-45415@lists.php.net to get a copy of this message | ||
cweiske Sun Mar 25 06:40:22 2007 UTC
Modified files:
/pear/MIME_Type Type.php example.php
Log:
- Implementing bug #3719: Use several methods when detecting mime type
- Make example.php check for errors when detecting.
http://cvs.php.net/viewvc.cgi/pear/MIME_Type/Type.php?r1=1.3&r2=1.4&diff_format=u
Index: pear/MIME_Type/Type.php
diff -u pear/MIME_Type/Type.php:1.3 pear/MIME_Type/Type.php:1.4
--- pear/MIME_Type/Type.php:1.3 Sun Mar 18 17:17:47 2007
+++ pear/MIME_Type/Type.php Sun Mar 25 06:40:22 2007
@@ -16,7 +16,7 @@
// | Authors: Ian Eure <ieure@php.net> |
// +----------------------------------------------------------------------+
//
-// $Id: Type.php,v 1.3 2007/03/18 17:17:47 cweiske Exp $
+// $Id: Type.php,v 1.4 2007/03/25 06:40:22 cweiske Exp $
require_once 'PEAR.php';
@@ -37,21 +37,21 @@
* @var string
*/
var $media = '';
-
+
/**
* The MIME media sub-type
*
* @var string
*/
var $subType = '';
-
+
/**
* Optional MIME parameters
*
* @var array
*/
var $parameters = array();
-
+
/**
* List of valid media types
*
@@ -138,7 +138,7 @@
}
return $params;
}
-
+
/**
* Strip paramaters from a MIME type string
@@ -266,17 +266,18 @@
* @param string $card Wildcard to check against
* @param string $type MIME type to check
* @return boolean true if there was a match, false otherwise
+ * @static
*/
function wildcardMatch($card, $type)
{
if (!MIME_Type::isWildcard($card)) {
return false;
}
-
+
if ($card == '*/*') {
return true;
}
-
+
if (MIME_Type::getMedia($card) ==
MIME_Type::getMedia($type)) {
return true;
@@ -320,6 +321,10 @@
*
* This function may be called staticly.
*
+ * @internal Tries to use fileinfo extension at first. If that
+ * does not work, mime_magic is used. If this is also not available
+ * or does not succeed, "file" command is tried.
+ *
* @param string $file Path to the file to get the type of
* @param bool $params Append MIME parameters if true
* @return string $file's MIME-type on success, PEAR_Error otherwise
@@ -328,15 +333,57 @@
*/
function autoDetect($file, $params = false)
{
- @include_once 'System/Command.php';
+ // Sanity checks
+ if (!file_exists($file)) {
+ return PEAR::raiseError("File \"$file\" doesn't exist");
+ }
+
+ if (!is_readable($file)) {
+ return PEAR::raiseError("File \"$file\" is not readable");
+ }
+
+ if (function_exists('finfo_file')) {
+ $finfo = finfo_open(FILEINFO_MIME);
+ $type = finfo_file($finfo, $file);
+ finfo_close($finfo);
+ if ($type !== false && $type !== '') {
+ return MIME_Type::_handleDetection($type, $params);
+ }
+ }
+
if (function_exists('mime_content_type')) {
$type = mime_content_type($file);
- } else if (class_exists('System_Command')) {
- $type = MIME_Type::_fileAutoDetect($file);
- } else {
- return PEAR::raiseError("Sorry, can't autodetect; you need the mime_magic
extension or System_Command and 'file' installed to use this function.");
+ if ($type !== false && $type !== '') {
+ return MIME_Type::_handleDetection($type, $params);
+ }
+ }
+
+ @include_once 'System/Command.php';
+ if (class_exists('System_Command')) {
+ return MIME_Type::_handleDetection(
+ MIME_Type::_fileAutoDetect($file),
+ $params
+ );
}
+
+ return PEAR::raiseError(
+ "Sorry, can't autodetect; you need the fileinfo or"
+ . " mime_magic extension"
+ . " or System_Command and 'file' installed"
+ . " to use this function."
+ );
+ }
+
+ /**
+ * Handles a detected MIME type and modifies it if necessary.
+ *
+ * @param string $file Path to the file to get the type of
+ * @param bool $params Append MIME parameters if true
+ * @return string $file's MIME-type on success, PEAR_Error otherwise
+ */
+ function _handleDetection($type, $params)
+ {
// _fileAutoDetect() may have returned an error.
if (PEAR::isError($type)) {
return $type;
@@ -367,16 +414,7 @@
*/
function _fileAutoDetect($file)
{
- // Sanity checks
- if (!file_exists($file)) {
- return PEAR::raiseError("File \"$file\" doesn't exist");
- }
-
- if (!is_readable($file)) {
- return PEAR::raiseError("File \"$file\" is not readable");
- }
-
- $cmd = new System_Command;
+ $cmd = new System_Command();
// Make sure we have the 'file' command.
http://cvs.php.net/viewvc.cgi/pear/MIME_Type/example.php?r1=1.1&r2=1.2&diff_format=u
Index: pear/MIME_Type/example.php
diff -u pear/MIME_Type/example.php:1.1 pear/MIME_Type/example.php:1.2
--- pear/MIME_Type/example.php:1.1 Wed Jun 16 08:46:19 2004
+++ pear/MIME_Type/example.php Sun Mar 25 06:40:22 2007
@@ -23,6 +23,11 @@
$file = '@doc_dir@/@package@/example.php';
print "\nChecking type of: $file\n";
-print MIME_Type::autoDetect($file)."\n";
+$type = MIME_Type::autoDetect($file);
+if (PEAR::isError($type)) {
+ print 'Error: ' . $type->getMessage() . "\n";
+} else {
+ print $type . "\n";
+}
?>