static protected $_map = null;
/**
- * The MIME magic file.
- *
- * @var array
- */
- static protected $_magic = null;
-
- /**
* Returns a copy of the MIME extension map.
*
* @return array The MIME extension map.
}
/**
- * Uses variants of the UNIX "file" command to attempt to determine the
- * MIME type of an unknown file.
+ * Attempt to determine the MIME type of an unknown file.
*
* @param string $path The path to the file to analyze.
* @param string $magic_db Path to the mime magic database.
+ * @param array $opts Additional options:
+ * <pre>
+ * 'nostrip' - (boolean) Don't strip parameter information from MIME
+ * type string.
+ * DEFAULT: false
+ * </pre>
*
- * @return string The MIME type of the file. Returns false if the file
- * type isn't recognized or an error happened.
+ * @return mixed The MIME type of the file. Returns false if the file
+ * type can not be determined.
*/
- static public function analyzeFile($path, $magic_db = null)
+ static public function analyzeFile($path, $magic_db = null,
+ $opts = array())
{
if (Horde_Util::extensionExists('fileinfo')) {
$res = empty($magic_db)
- ? @finfo_open(FILEINFO_MIME)
- : @finfo_open(FILEINFO_MIME, $magic_db);
+ ? finfo_open(FILEINFO_MIME)
+ : finfo_open(FILEINFO_MIME, $magic_db);
if ($res) {
- $type = finfo_file($res, $path);
+ $type = trim(finfo_file($res, $path));
finfo_close($res);
/* Remove any additional information. */
- foreach (array(';', ',', '\\0') as $separator) {
- if (($pos = strpos($type, $separator)) !== false) {
- $type = rtrim(substr($type, 0, $pos));
+ if (empty($opts['nostrip'])) {
+ foreach (array(';', ',', '\\0') as $separator) {
+ if (($pos = strpos($type, $separator)) !== false) {
+ $type = rtrim(substr($type, 0, $pos));
+ }
}
- }
- if (preg_match('|^[a-z0-9]+/[.-a-z0-9]+$|i', $type)) {
+ if (preg_match('|^[a-z0-9]+/[.-a-z0-9]+$|i', $type)) {
+ return $type;
+ }
+ } else {
return $type;
}
}
}
/**
- * Uses variants of the UNIX "file" command to attempt to determine the
- * MIME type of an unknown byte stream.
+ * Attempt to determine the MIME type of an unknown byte stream.
*
* @param string $data The file data to analyze.
* @param string $magic_db Path to the mime magic database.
+ * @param array $opts Additional options:
+ * <pre>
+ * 'nostrip' - (boolean) Don't strip parameter information from MIME
+ * type string.
+ * DEFAULT: false
+ * </pre>
*
- * @return string The MIME type of the file. Returns false if the file
- * type isn't recognized or an error happened.
+ * @return mixed The MIME type of the file. Returns false if the file
+ * type can not be determined.
*/
- static public function analyzeData($data, $magic_db = null)
+ static public function analyzeData($data, $magic_db = null,
+ $opts = array())
{
/* If the PHP Mimetype extension is available, use that. */
if (Horde_Util::extensionExists('fileinfo')) {
return false;
}
- $type = finfo_buffer($res, $data);
+ $type = trim(finfo_buffer($res, $data));
finfo_close($res);
/* Remove any additional information. */
- if (($pos = strpos($type, ';')) !== false) {
- $type = rtrim(substr($type, 0, $pos));
- }
+ if (empty($opts['nostrip'])) {
+ if (($pos = strpos($type, ';')) !== false) {
+ $type = rtrim(substr($type, 0, $pos));
+ }
- if (($pos = strpos($type, ',')) !== false) {
- $type = rtrim(substr($type, 0, $pos));
+ if (($pos = strpos($type, ',')) !== false) {
+ $type = rtrim(substr($type, 0, $pos));
+ }
}
return $type;
return false;
}
+
}