PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/compat/mime_content_type.php

http://snowcms.googlecode.com/
PHP | 121 lines | 58 code | 14 blank | 49 comment | 10 complexity | 8934cfa4e4ec8270a2885919f45cf495 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////
  3. // SnowCMS v2.0 //
  4. // By the SnowCMS Team //
  5. // www.snowcms.com //
  6. // Released under the Microsoft Reciprocal License //
  7. // www.opensource.org/licenses/ms-rl.html //
  8. ////////////////////////////////////////////////////////////////////////////
  9. // //
  10. // SnowCMS originally pawned by soren121 started in early 2008 //
  11. // //
  12. ////////////////////////////////////////////////////////////////////////////
  13. // //
  14. // SnowCMS v2.0 began in November 2009 //
  15. // //
  16. ////////////////////////////////////////////////////////////////////////////
  17. // File version: SnowCMS 2.0 //
  18. ////////////////////////////////////////////////////////////////////////////
  19. if(!defined('INSNOW'))
  20. {
  21. die('Nice try...');
  22. }
  23. // Title: MIME Content-Type
  24. if(!function_exists('mime_content_type'))
  25. {
  26. /*
  27. Function: mime_content_type
  28. Attempts to detect the content type of the specified file.
  29. Parameters:
  30. string $filename - The name of the file.
  31. Returns:
  32. string - Returns the MIME content type of the file.
  33. Note:
  34. This function attempts to detect the type of the file according to
  35. a text file containing extensions and content types, so it won't
  36. be as accurate as it could be.
  37. However, if the system detects that the Fileinfo extension for PHP
  38. is installed, then that will be used instead of the crappy method.
  39. */
  40. function mime_content_type($filename)
  41. {
  42. // Is the Fileinfo extension installed in your PHP setup? Even better!
  43. if(function_exists('finfo_file'))
  44. {
  45. $ff = finfo_open(FILEINFO_MIME, settings()->get('finfo_magic_file', 'string', substr(PHP_OS, 0, 3) == 'WIN' ? 'C:\Program Files\PHP\magic' : '/usr/share/misc/file/magic.mgc'));
  46. $mime_type = finfo_file($ff, $location);
  47. finfo_close($ff);
  48. // Alright, got it!
  49. return $mime_type;
  50. }
  51. // Get the extension of the file. Maybe.
  52. elseif(strpos($filename, '.') !== false)
  53. {
  54. // The extension is SHA-1'd ;)
  55. $tmp = explode('.', $filename);
  56. $extension = sha1(strtolower(array_pop($tmp)));
  57. $fp = fopen(dirname(__FILE__). '/mime.db', 'rb');
  58. fseek($fp, 0, SEEK_END);
  59. $filesize = ftell($fp);
  60. fseek($fp, 0);
  61. // How many possibilities are there?
  62. $total = $filesize / (double)295;
  63. // Now let's get to searching!
  64. $min = 0;
  65. $max = $total - 1;
  66. $searches = 0;
  67. do
  68. {
  69. $mid = ceil(($min + $max) / 2);
  70. fseek($fp, $mid * 295);
  71. $current = fread($fp, 40);
  72. // Did we find it?
  73. if($current == $extension)
  74. {
  75. // Yup, we did!
  76. break;
  77. }
  78. // But don't give up!
  79. elseif($extension > $current)
  80. {
  81. $min = $mid + 1;
  82. }
  83. else
  84. {
  85. $max = $mid - 1;
  86. }
  87. $searches++;
  88. }
  89. while($current != $extension && $min <= $max);
  90. // Was it found?
  91. if($current == $extension)
  92. {
  93. // Yup, so read the mime type and return it!
  94. list(, $mime_type) = unpack('a255', fread($fp, 255));
  95. return $mime_type;
  96. }
  97. }
  98. // Just return a generic content type.
  99. return 'application/octet-stream';
  100. }
  101. }
  102. ?>