PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/download_helper.php

https://gitlab.com/nicolerey/PPLI-payroll
PHP | 167 lines | 77 code | 17 blank | 73 comment | 19 complexity | afa3819e1a27a808c8d38b8a10425333 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Download Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link http://codeigniter.com/user_guide/helpers/download_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('force_download'))
  50. {
  51. /**
  52. * Force Download
  53. *
  54. * Generates headers that force a download to happen
  55. *
  56. * @param string filename
  57. * @param mixed the data to be downloaded
  58. * @param bool whether to try and send the actual file MIME type
  59. * @return void
  60. */
  61. function force_download($filename = '', $data = '', $set_mime = FALSE)
  62. {
  63. if ($filename === '' OR $data === '')
  64. {
  65. return;
  66. }
  67. elseif ($data === NULL)
  68. {
  69. if (@is_file($filename) && ($filesize = @filesize($filename)) !== FALSE)
  70. {
  71. $filepath = $filename;
  72. $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename));
  73. $filename = end($filename);
  74. }
  75. else
  76. {
  77. return;
  78. }
  79. }
  80. else
  81. {
  82. $filesize = strlen($data);
  83. }
  84. // Set the default MIME type to send
  85. $mime = 'application/octet-stream';
  86. $x = explode('.', $filename);
  87. $extension = end($x);
  88. if ($set_mime === TRUE)
  89. {
  90. if (count($x) === 1 OR $extension === '')
  91. {
  92. /* If we're going to detect the MIME type,
  93. * we'll need a file extension.
  94. */
  95. return;
  96. }
  97. // Load the mime types
  98. $mimes =& get_mimes();
  99. // Only change the default MIME if we can find one
  100. if (isset($mimes[$extension]))
  101. {
  102. $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension];
  103. }
  104. }
  105. /* It was reported that browsers on Android 2.1 (and possibly older as well)
  106. * need to have the filename extension upper-cased in order to be able to
  107. * download it.
  108. *
  109. * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/
  110. */
  111. if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT']))
  112. {
  113. $x[count($x) - 1] = strtoupper($extension);
  114. $filename = implode('.', $x);
  115. }
  116. if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE)
  117. {
  118. return;
  119. }
  120. // Clean output buffer
  121. if (ob_get_level() !== 0 && @ob_end_clean() === FALSE)
  122. {
  123. @ob_clean();
  124. }
  125. // Generate the server headers
  126. header('Content-Type: '.$mime);
  127. header('Content-Disposition: attachment; filename="'.$filename.'"');
  128. header('Expires: 0');
  129. header('Content-Transfer-Encoding: binary');
  130. header('Content-Length: '.$filesize);
  131. // Internet Explorer-specific headers
  132. if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
  133. {
  134. header('Cache-Control: no-cache, no-store, must-revalidate');
  135. }
  136. header('Pragma: no-cache');
  137. // If we have raw data - just dump it
  138. if ($data !== NULL)
  139. {
  140. exit($data);
  141. }
  142. // Flush 1MB chunks of data
  143. while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE)
  144. {
  145. echo $data;
  146. }
  147. fclose($fp);
  148. exit;
  149. }
  150. }