PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/download.php

http://github.com/gallery/gallery3
PHP | 135 lines | 64 code | 19 blank | 52 comment | 4 complexity | 3c4f97da041e714b511f8845b8bb045e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Download helper class.
  4. *
  5. * @package Kohana
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2009 Kohana Team
  8. * @license http://kohanaphp.com/license
  9. */
  10. class download_Core {
  11. /**
  12. * Send headers necessary to invoke a "Save As" dialog
  13. *
  14. * @link http://support.microsoft.com/kb/260519
  15. * @link http://greenbytes.de/tech/tc2231/
  16. *
  17. * @param string file name
  18. * @return string file name as it was sent
  19. */
  20. public static function dialog($filename)
  21. {
  22. $filename = basename($filename);
  23. header('Content-Disposition: attachment; filename="'.$filename.'"');
  24. return $filename;
  25. }
  26. /**
  27. * Send the contents of a file or a data string with the proper MIME type and exit.
  28. *
  29. * @uses exit()
  30. * @uses Kohana::close_buffers()
  31. *
  32. * @param string a file path or file name
  33. * @param string optional data to send
  34. * @return void
  35. */
  36. public static function send($filename, $data = NULL)
  37. {
  38. if ($data === NULL)
  39. {
  40. $filepath = realpath($filename);
  41. $filename = basename($filepath);
  42. $filesize = filesize($filepath);
  43. }
  44. else
  45. {
  46. $filename = basename($filename);
  47. $filesize = strlen($data);
  48. }
  49. // Retrieve MIME type by extension
  50. $mime = Kohana::config('mimes.'.strtolower(substr(strrchr($filename, '.'), 1)));
  51. $mime = empty($mime) ? 'application/octet-stream' : $mime[0];
  52. // Close output buffers
  53. Kohana::close_buffers(FALSE);
  54. // Clear any output
  55. Event::add('system.display', create_function('', 'Kohana::$output = "";'));
  56. // Send headers
  57. header("Content-Type: $mime");
  58. header('Content-Length: '.sprintf('%d', $filesize));
  59. header('Content-Transfer-Encoding: binary');
  60. // Send data
  61. if ($data === NULL)
  62. {
  63. $handle = fopen($filepath, 'rb');
  64. fpassthru($handle);
  65. fclose($handle);
  66. }
  67. else
  68. {
  69. echo $data;
  70. }
  71. exit;
  72. }
  73. /**
  74. * Force the download of a file by the user's browser by preventing any
  75. * caching. Contains a workaround for Internet Explorer.
  76. *
  77. * @link http://support.microsoft.com/kb/316431
  78. * @link http://support.microsoft.com/kb/812935
  79. *
  80. * @uses download::dialog()
  81. * @uses download::send()
  82. *
  83. * @param string a file path or file name
  84. * @param mixed data to be sent if the filename does not exist
  85. * @param string suggested filename to display in the download
  86. * @return void
  87. */
  88. public static function force($filename = NULL, $data = NULL, $nicename = NULL)
  89. {
  90. download::dialog(empty($nicename) ? $filename : $nicename);
  91. // Prevent caching
  92. header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
  93. if (request::user_agent('browser') === 'Internet Explorer' AND request::user_agent('version') <= '6.0')
  94. {
  95. // HTTP 1.0
  96. header('Pragma:');
  97. // HTTP 1.1 with IE extensions
  98. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  99. }
  100. else
  101. {
  102. // HTTP 1.0
  103. header('Pragma: no-cache');
  104. // HTTP 1.1
  105. header('Cache-Control: no-cache, max-age=0');
  106. }
  107. if (is_file($filename))
  108. {
  109. download::send($filename);
  110. }
  111. else
  112. {
  113. download::send($filename, $data);
  114. }
  115. }
  116. } // End download