PageRenderTime 1259ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Share/Lib.FileMime.php

https://github.com/CircleCode/dynacase-core
PHP | 331 lines | 266 code | 21 blank | 44 comment | 62 complexity | 7685b288b9d09600191a78a19d738145 MD5 | raw file
  1. <?php
  2. /*
  3. * @author Anakeen
  4. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  5. * @package FDL
  6. */
  7. /**
  8. * Assosiate icon with mime type
  9. *
  10. * @author Anakeen
  11. * @version $Id: Lib.FileMime.php,v 1.9 2008/05/06 08:20:43 marc Exp $
  12. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License
  13. * @package FDL
  14. * @subpackage CORE
  15. */
  16. /**
  17. */
  18. function getIconMimeArray()
  19. {
  20. return array(
  21. "application/msword" => "mime-wordprocessing",
  22. "application/ogg" => "audio-x-generic",
  23. "application/pdf" => "mime-pdf",
  24. "application/postscript" => "mime-wordprocessing",
  25. "application/rtf" => "mime-wordprocessing",
  26. "application/vnd.lotus-1-2-3" => "mime-spreadsheet",
  27. "application/vnd.ms-excel" => "mime-spreadsheet",
  28. "application/vnd.ms-powerpoint" => "mime-presentation",
  29. "application/vnd.oasis.opendocument.presentation" => "mime-presentation",
  30. "application/vnd.oasis.opendocument.spreadsheet" => "mime-spreadsheet",
  31. "application/vnd.oasis.opendocument.text" => "mime-wordprocessing",
  32. "application/vnd.oasis.opendocument.graphics" => "mime-vectorgfx",
  33. "application/vnd.stardivision.calc" => "mime-spreadsheet",
  34. "application/vnd.stardivision.impress" => "mime-presentation",
  35. "application/vnd.stardivision.writer" => "mime-wordprocessing",
  36. "application/vnd.sun.xml.calc" => "mime-spreadsheet",
  37. "application/vnd.sun.xml.impress" => "mime-presentation",
  38. "application/vnd.sun.xml.writer" => "mime-wordprocessing",
  39. "application/vnd.sun.xml.writer.global" => "mime-wordprocessing",
  40. "application/vnd.sun.xml.draw" => "mime-vectorgfx",
  41. "application/wordperfect" => "mime-wordprocessing",
  42. "application/x-7z-compressed" => "mime-tgz",
  43. "application/x-abiword" => "mime-wordprocessing",
  44. "application/x-applix-spreadsheet" => "mime-spreadsheet",
  45. "application/x-applix-word" => "mime-wordprocessing",
  46. "application/x-archive" => "mime-tar",
  47. "application/x-arj" => "mime-tgz",
  48. "application/x-bzip-compressed-tar" => "mime-tgz",
  49. "application/x-bzip" => "mime-tgz",
  50. "application/x-compressed-tar" => "mime-tgz",
  51. "application/x-compress" => "mime-tgz",
  52. "application/x-cpio-compressed" => "mime-tgz",
  53. "application/x-cpio" => "mime-tgz",
  54. "application/x-deb" => "mime-tgz",
  55. "application/x-dvi" => "mime-wordprocessing",
  56. "application/x-executable" => "application/x-executable",
  57. "application/x-font-afm" => "font-x-generic",
  58. "application/x-font-bdf" => "font-x-generic",
  59. "application/x-font-linux-psf" => "font-x-generic",
  60. "application/x-font-pcf" => "font-x-generic",
  61. "application/x-font-sunos-news" => "font-x-generic",
  62. "application/x-font-ttf" => "font-x-generic",
  63. "application/x-gnumeric" => "mime-spreadsheet",
  64. "application/x-gzip" => "mime-tgz",
  65. "application/xhtml+xml" => "text-html",
  66. "application/x-killustrator" => "mime-vectorgfx",
  67. "application/x-kpresenter" => "mime-presentation",
  68. "application/x-kspread" => "mime-spreadsheet",
  69. "application/x-kword" => "mime-wordprocessing",
  70. "application/x-ms-dos-executable" => "application/x-executable",
  71. "application/x-perl" => "mime-shellscript",
  72. "application/x-php" => "text-html",
  73. "application/x-python-bytecode" => "mime-shellscript",
  74. "application/x-rar" => "mime-tgz",
  75. "application/x-rpm" => "mime-rpm",
  76. "application/x-shellscript" => "mime-shellscript",
  77. "application/x-shockwave-flash" => "video-x-generic",
  78. "application/x-tar" => "mime-tar",
  79. "application/x-tarz" => "mime-tgz",
  80. "application/x-tex" => "mime-tex",
  81. "application/x-zip" => "mime-zip",
  82. "application/zip" => "mime-zip",
  83. "application" => "mime-zip",
  84. "audio" => "mime-sound",
  85. "image" => "mime-image2",
  86. "video" => "mime-video",
  87. "image/jpeg" => "mime-image",
  88. "text/html" => "mime-html",
  89. "text/xml" => "mime-xml",
  90. "text/x-c++" => "mime-source_cpp",
  91. "text/x-c" => "mime-source_c",
  92. "text/x-c-header" => "mime-source_h",
  93. "text/x-php" => "mime-source_php",
  94. "text" => "mime-txt",
  95. "text/vnd.wap.wml" => "mime-html",
  96. "text/x-csh" => "mime-shellscript",
  97. "text/x-python" => "mime-shellscript",
  98. "text/x-sh" => "mime-shellscript",
  99. "text/x-vcalendar" => "mime-vcalendar",
  100. "text/x-vcard" => "mime-address-book",
  101. "text/x-zsh" => "mime-shellscript",
  102. "text/x-makefile" => "mime-make",
  103. "x-font-afm" => "font-x-generic"
  104. );
  105. }
  106. function getIconMimeFile($sysmime)
  107. {
  108. $icon = "";
  109. if ($sysmime) {
  110. $tmime = explode(";", $sysmime);
  111. $mime = trim($tmime[0]);
  112. $mimeIcon = getIconMimeArray();
  113. if (isset($mimeIcon[$mime])) {
  114. $icon = $mimeIcon[$mime] . ".png";
  115. } else {
  116. $p = strpos($mime, '/');
  117. $mime = substr($mime, 0, $p);
  118. if (isset($mimeIcon[$mime])) {
  119. $icon = $mimeIcon[$mime] . ".png";
  120. }
  121. }
  122. }
  123. return $icon;
  124. }
  125. /**
  126. * return system file mime
  127. * @param string $f filename
  128. * @param string $fn basename of file (can be different of real path)
  129. * return string mime like text/html
  130. */
  131. function getSysMimeFile($f, $fn = "")
  132. {
  133. if (!file_exists($f)) return false;
  134. $ret = getMimeFile($fn, 'sys');
  135. if ($ret !== false) {
  136. return $ret;
  137. }
  138. $sys = trim(shell_exec(sprintf("file --mime -b %s", escapeshellarg($f))));
  139. $txt = getTextMimeFile($f);
  140. $sys = strtok($sys, " ;\n\t"); // only first part
  141. // correct errors of file function
  142. if (preg_match('/Makefile/', $fn)) return 'text/x-makefile';
  143. if (preg_match('/ASCII C\+\+/', $txt)) {
  144. if (preg_match('/\.h$/', $fn)) return 'text/x-c-header';
  145. return 'text/x-c++';
  146. }
  147. if (preg_match('/PHP script/', $txt)) return 'text/x-php';
  148. // open office archive
  149. if (preg_match('/zip$/', $sys) || (preg_match('/octet-stream$/', $sys))) {
  150. if (preg_match('/OpenOffice/', $txt)) {
  151. if (preg_match('/\.sxw$/', $fn)) return 'application/vnd.sun.xml.writer';
  152. if (preg_match('/\.sxc$/', $fn)) return 'application/vnd.sun.xml.calc';
  153. if (preg_match('/\.sxi$/', $fn)) return 'application/vnd.sun.xml.impress';
  154. if (preg_match('/\.sxd$/', $fn)) return 'application/vnd.sun.xml.draw';
  155. if (preg_match('/\.sxg$/', $fn)) return 'application/vnd.sun.xml.writer.global';
  156. return 'application/vnd.sun.xml.writer';
  157. }
  158. if (preg_match('/OpenDocument/', $txt)) {
  159. if (preg_match('/\.odp$/', $fn)) return 'application/vnd.oasis.opendocument.presentation';
  160. if (preg_match('/\.odt$/', $fn)) return 'application/vnd.oasis.opendocument.text';
  161. if (preg_match('/\.ods$/', $fn)) return 'application/vnd.oasis.opendocument.spreadsheet';
  162. if (preg_match('/\.odg$/', $fn)) return 'application/vnd.oasis.opendocument.graphics';
  163. return 'application/vnd.oasis.opendocument.text';
  164. }
  165. if (preg_match('/\.sxw$/', $fn)) return 'application/vnd.sun.xml.writer';
  166. if (preg_match('/\.sxc$/', $fn)) return 'application/vnd.sun.xml.calc';
  167. if (preg_match('/\.sxi$/', $fn)) return 'application/vnd.sun.xml.impress';
  168. if (preg_match('/\.sxd$/', $fn)) return 'application/vnd.sun.xml.draw';
  169. if (preg_match('/\.sxg$/', $fn)) return 'application/vnd.sun.xml.writer.global';
  170. if (preg_match('/\.odp$/', $fn)) return 'application/vnd.oasis.opendocument.presentation';
  171. if (preg_match('/\.odt$/', $fn)) return 'application/vnd.oasis.opendocument.text';
  172. if (preg_match('/\.ods$/', $fn)) return 'application/vnd.oasis.opendocument.spreadsheet';
  173. if (preg_match('/\.odg$/', $fn)) return 'application/vnd.oasis.opendocument.graphics';
  174. }
  175. if (!$sys) {
  176. // try with text only
  177. if ($txt) {
  178. if (preg_match('/^XML/', $txt)) return 'text/xml';
  179. }
  180. if (preg_match('/\.xls$/', $fn)) return 'application/vnd.ms-excel';
  181. if (preg_match('/\.doc$/', $fn)) return 'application/msword';
  182. if (preg_match('/\.ppt$/', $fn)) return 'application/vnd.ms-powerpoint';
  183. }
  184. if ($sys == 'application/msword') { // for old sys mime info
  185. if (preg_match('/\.xls$/', $fn)) return 'application/vnd.ms-excel';
  186. if (preg_match('/\.ppt$/', $fn)) return 'application/vnd.ms-powerpoint';
  187. }
  188. return strtok($sys, " ;\n\t");
  189. }
  190. function getTextMimeFile($f, $fn = '')
  191. {
  192. $ret = getMimeFile($fn, 'text');
  193. if ($ret !== false) {
  194. return $ret;
  195. }
  196. $txt = trim(shell_exec(sprintf("file -b %s", escapeshellarg($f))));
  197. if ($txt == 'data') {
  198. if (preg_match('/\.ods$/', $f)) $txt = 'OpenDocument Spreadsheet';
  199. else if (preg_match('/\.odt$/', $f)) $txt = 'OpenDocument Text';
  200. else if (preg_match('/\.odp$/', $f)) $txt = 'OpenDocument Presentation';
  201. else if (preg_match('/\.odg$/', $f)) $txt = 'OpenDocument Drawing';
  202. }
  203. if (!$txt) return " ";
  204. return $txt;
  205. }
  206. /**
  207. * get current extension from system mime
  208. */
  209. function getExtension($smime)
  210. {
  211. include_once ("MimeExtension.php");
  212. $exts = getExtensions();
  213. $ext = $exts[$smime];
  214. if ($ext) {
  215. return strtok($ext, " \n\t");
  216. }
  217. }
  218. /**
  219. * get extension from file name
  220. */
  221. function getFileExtension($filename)
  222. {
  223. $ext = false;
  224. if ($filename && strrpos($filename, '.') > 0) $ext = substr($filename, strrpos($filename, '.') + 1);
  225. return $ext;
  226. }
  227. /**
  228. * get MIME type/text from mime.conf and mime-user.conf files
  229. */
  230. function getMimeFile($filename, $type = 'sys')
  231. {
  232. $conf_user = loadUserMimeConf();
  233. $conf_global = loadMimeConf();
  234. $conf = array_merge($conf_user, $conf_global);
  235. foreach ($conf as $rule) {
  236. $ext = $rule['ext'];
  237. if (preg_match("/\.\Q$ext\E$/i", $filename)) {
  238. return $rule[$type];
  239. }
  240. }
  241. return false;
  242. }
  243. /**
  244. * load mime-user.conf XML file into PHP array
  245. */
  246. function loadUserMimeConf()
  247. {
  248. include_once ('WHAT/Lib.Prefix.php');
  249. $rules = array();
  250. $conf_file = DEFAULT_PUBDIR . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "mime-user.conf";
  251. if (!file_exists($conf_file) || !is_readable($conf_file)) {
  252. return $rules;
  253. }
  254. $xml = simplexml_load_file($conf_file);
  255. if ($xml === false) {
  256. error_log(__FUNCTION__ . " " . sprintf("Could not load user MIME config '%s'.", $conf_file));
  257. return $rules;
  258. }
  259. foreach ($xml->mime as $mimeNode) {
  260. $rule = array();
  261. foreach (array(
  262. 'ext',
  263. 'sys',
  264. 'text'
  265. ) as $attrName) {
  266. $rule[$attrName] = (string)$mimeNode[$attrName];
  267. }
  268. array_push($rules, $rule);
  269. }
  270. return $rules;
  271. }
  272. /**
  273. * load mime.conf XML file into PHP array
  274. */
  275. function loadMimeConf()
  276. {
  277. include_once ('WHAT/Lib.Prefix.php');
  278. $rules = array();
  279. $conf_file = DEFAULT_PUBDIR . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "mime.conf";
  280. $xml = simplexml_load_file($conf_file);
  281. if ($xml === false) {
  282. error_log(__FUNCTION__ . " " . sprintf("Could not load MIME config '%s'.", $conf_file));
  283. return $rules;
  284. }
  285. foreach ($xml->mime as $mimeNode) {
  286. $rule = array();
  287. foreach (array(
  288. 'ext',
  289. 'sys',
  290. 'text'
  291. ) as $attrName) {
  292. $rule[$attrName] = (string)$mimeNode[$attrName];
  293. }
  294. array_push($rules, $rule);
  295. }
  296. return $rules;
  297. }
  298. /**
  299. * get number of pages from pdf file
  300. * @return int
  301. */
  302. function getPdfNumberOfPages($file)
  303. {
  304. $nbpages = 0;
  305. if (file_exists($file)) {
  306. $nbpages = intval(trim(shell_exec(sprintf('grep -c "/Type[[:space:]]*/Page\>" %s', escapeshellarg($file)))));
  307. }
  308. return $nbpages;
  309. }
  310. ?>