Commit e26262a5843444ee3b72dd8bda1523829934ad46
1 parent
5471dbfc
Give the MIME functions from PhysicalDocumentManager a temporary home.
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3591 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
106 additions
and
0 deletions
lib/mime.inc.php
0 → 100644
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * This is a temporary location for these functions. | |
| 5 | + */ | |
| 6 | + | |
| 7 | +class KTMime { | |
| 8 | + /** | |
| 9 | + * Get the mime type primary key for a specific mime type | |
| 10 | + * | |
| 11 | + * @param string detected mime type | |
| 12 | + * @param string filename | |
| 13 | + * @return int mime type primary key if found, else default mime type primary key (text/plain) | |
| 14 | + */ | |
| 15 | + function getMimeTypeID($sMimeType, $sFileName) { | |
| 16 | + global $default; | |
| 17 | + $sql = $default->db; | |
| 18 | + $bOfficeDocument = false; | |
| 19 | + | |
| 20 | + // application/msword seems to be set by all Office documents | |
| 21 | + if ($sMimeType == "application/msword") { | |
| 22 | + $bOfficeDocument = true; | |
| 23 | + } | |
| 24 | + | |
| 25 | + if ($bOfficeDocument || (!$sMimeType)) { | |
| 26 | + // check by file extension | |
| 27 | + $sExtension = KTMime::stripAllButExtension($sFileName); | |
| 28 | + $sql->query(array("SELECT id FROM " . $default->mimetypes_table . " WHERE LOWER(filetypes) = ?", $sExtension));/*ok*/ | |
| 29 | + if ($sql->next_record()) { | |
| 30 | + return $sql->f("id"); | |
| 31 | + } | |
| 32 | + } | |
| 33 | + | |
| 34 | + // get the mime type id | |
| 35 | + if (isset($sMimeType)) { | |
| 36 | + $sql->query(array("SELECT id FROM " . $default->mimetypes_table . " WHERE mimetypes = ?", $sMimeType));/*ok*/ | |
| 37 | + if ($sql->next_record()) { | |
| 38 | + return $sql->f("id"); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + //otherwise return the default mime type | |
| 43 | + return KTMime::getDefaultMimeTypeID(); | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Get the default mime type, which is text/plain | |
| 48 | + * | |
| 49 | + * @return int default mime type | |
| 50 | + * | |
| 51 | + */ | |
| 52 | + function getDefaultMimeTypeID() { | |
| 53 | + global $default; | |
| 54 | + $sql = $default->db; | |
| 55 | + $sql->query("SELECT id FROM " . $default->mimetypes_table . " WHERE mimetypes = 'text/plain'");/*ok*/ | |
| 56 | + $sql->next_record(); | |
| 57 | + //get the mime type id | |
| 58 | + return $sql->f("id"); | |
| 59 | + } | |
| 60 | + | |
| 61 | + function getMimeTypeName($iMimeTypeID) { | |
| 62 | + global $default; | |
| 63 | + $sql = $default->db; | |
| 64 | + $sql->query(array("SELECT mimetypes FROM " . $default->mimetypes_table . " WHERE id = ?", $iMimeTypeID));/*ok*/ | |
| 65 | + if ($sql->next_record()) { | |
| 66 | + return $sql->f("mimetypes"); | |
| 67 | + } | |
| 68 | + return "application/octet-stream"; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Try well-defined methods for getting the MIME type for a file on disk. | |
| 73 | + * First try PECL's Fileinfo library, then try mime_content_type() builtin. | |
| 74 | + * If neither are available, returns NULL. | |
| 75 | + * | |
| 76 | + * @param string file on disk | |
| 77 | + * @return string mime time for given filename, or NULL | |
| 78 | + */ | |
| 79 | + function getMimeTypeFromFile($sFileName) { | |
| 80 | + if (extension_loaded('fileinfo')) { | |
| 81 | + $res = finfo_open(FILEINFO_MIME); | |
| 82 | + $sType = finfo_file($res, $sFileName); | |
| 83 | + } | |
| 84 | + | |
| 85 | + if (!$sType && function_exists('mime_content_type')) { | |
| 86 | + $sType = @mime_content_type($sFileName); | |
| 87 | + } | |
| 88 | + | |
| 89 | + if ($sType) { | |
| 90 | + return preg_replace('/;.*$/', '', $sType); | |
| 91 | + } | |
| 92 | + | |
| 93 | + return NULL; | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Strip all but the extension from a file. For instance, input of | |
| 98 | + * 'foo.tif' would return 'tif'. | |
| 99 | + * | |
| 100 | + * @param string filename | |
| 101 | + * @return string extension for given file, without filename itself | |
| 102 | + */ | |
| 103 | + function stripAllButExtension($sFileName) { | |
| 104 | + return strtolower(substr($sFileName, strpos($sFileName, ".")+1, strlen($sFileName) - strpos($sFileName, "."))); | |
| 105 | + } | |
| 106 | +} | ... | ... |