summaryrefslogtreecommitdiff
path: root/system/helpers/download.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/helpers/download.php')
-rw-r--r--system/helpers/download.php148
1 files changed, 90 insertions, 58 deletions
diff --git a/system/helpers/download.php b/system/helpers/download.php
index 49fed42c..58a7ab94 100644
--- a/system/helpers/download.php
+++ b/system/helpers/download.php
@@ -2,104 +2,136 @@
/**
* Download helper class.
*
- * $Id: download.php 3769 2008-12-15 00:48:56Z zombor $
+ * $Id: download.php 4679 2009-11-10 01:45:52Z isaiah $
*
* @package Core
* @author Kohana Team
- * @copyright (c) 2007-2008 Kohana Team
- * @license http://kohanaphp.com/license.html
+ * @copyright (c) 2007-2009 Kohana Team
+ * @license http://kohanaphp.com/license
*/
class download_Core {
/**
- * Force a download of a file to the user's browser. This function is
- * binary-safe and will work with any MIME type that Kohana is aware of.
+ * Send headers necessary to invoke a "Save As" dialog
+ *
+ * @link http://support.microsoft.com/kb/260519
+ * @link http://greenbytes.de/tech/tc2231/
+ *
+ * @param string file name
+ * @return string file name as it was sent
+ */
+ public static function dialog($filename)
+ {
+ $filename = basename($filename);
+
+ header('Content-Disposition: attachment; filename="'.$filename.'"');
+
+ return $filename;
+ }
+
+ /**
+ * Send the contents of a file or a data string with the proper MIME type and exit.
+ *
+ * @uses exit()
+ * @uses Kohana::close_buffers()
*
* @param string a file path or file name
- * @param mixed data to be sent if the filename does not exist
- * @param string suggested filename to display in the download
+ * @param string optional data to send
* @return void
*/
- public static function force($filename = NULL, $data = NULL, $nicename = NULL)
+ public static function send($filename, $data = NULL)
{
- if (empty($filename))
- return FALSE;
-
- if (is_file($filename))
+ if ($data === NULL)
{
- // Get the real path
- $filepath = str_replace('\\', '/', realpath($filename));
+ $filepath = realpath($filename);
- // Set filesize
+ $filename = basename($filepath);
$filesize = filesize($filepath);
-
- // Get filename
- $filename = substr(strrchr('/'.$filepath, '/'), 1);
-
- // Get extension
- $extension = strtolower(substr(strrchr($filepath, '.'), 1));
}
else
{
- // Get filesize
+ $filename = basename($filename);
$filesize = strlen($data);
+ }
- // Make sure the filename does not have directory info
- $filename = substr(strrchr('/'.$filename, '/'), 1);
+ // Retrieve MIME type by extension
+ $mime = Kohana::config('mimes.'.strtolower(substr(strrchr($filename, '.'), 1)));
+ $mime = empty($mime) ? 'application/octet-stream' : $mime[0];
- // Get extension
- $extension = strtolower(substr(strrchr($filename, '.'), 1));
- }
+ // Close output buffers
+ Kohana::close_buffers(FALSE);
+
+ // Clear any output
+ Event::add('system.display', create_function('', 'Kohana::$output = "";'));
- // Get the mime type of the file
- $mime = Kohana::config('mimes.'.$extension);
+ // Send headers
+ header("Content-Type: $mime");
+ header('Content-Length: '.sprintf('%d', $filesize));
+ header('Content-Transfer-Encoding: binary');
- if (empty($mime))
+ // Send data
+ if ($data === NULL)
{
- // Set a default mime if none was found
- $mime = array('application/octet-stream');
+ $handle = fopen($filepath, 'rb');
+
+ fpassthru($handle);
+ fclose($handle);
+ }
+ else
+ {
+ echo $data;
}
- // Generate the server headers
- header('Content-Type: '.$mime[0]);
- header('Content-Disposition: attachment; filename="'.(empty($nicename) ? $filename : $nicename).'"');
- header('Content-Transfer-Encoding: binary');
- header('Content-Length: '.sprintf('%d', $filesize));
+ exit;
+ }
+
+ /**
+ * Force the download of a file by the user's browser by preventing any
+ * caching. Contains a workaround for Internet Explorer.
+ *
+ * @link http://support.microsoft.com/kb/316431
+ * @link http://support.microsoft.com/kb/812935
+ *
+ * @uses download::dialog()
+ * @uses download::send()
+ *
+ * @param string a file path or file name
+ * @param mixed data to be sent if the filename does not exist
+ * @param string suggested filename to display in the download
+ * @return void
+ */
+ public static function force($filename = NULL, $data = NULL, $nicename = NULL)
+ {
+ download::dialog(empty($nicename) ? $filename : $nicename);
- // More caching prevention
- header('Expires: 0');
+ // Prevent caching
+ header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
- if (Kohana::user_agent('browser') === 'Internet Explorer')
+ if (request::user_agent('browser') === 'Internet Explorer' AND request::user_agent('version') <= '6.0')
{
- // Send IE headers
+ // HTTP 1.0
+ header('Pragma:');
+
+ // HTTP 1.1 with IE extensions
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Pragma: public');
}
else
{
- // Send normal headers
+ // HTTP 1.0
header('Pragma: no-cache');
- }
- // Clear the output buffer
- Kohana::close_buffers(FALSE);
+ // HTTP 1.1
+ header('Cache-Control: no-cache, max-age=0');
+ }
- if (isset($filepath))
+ if (is_file($filename))
{
- // Open the file
- $handle = fopen($filepath, 'rb');
-
- // Send the file data
- fpassthru($handle);
-
- // Close the file
- fclose($handle);
+ download::send($filename);
}
else
{
- // Send the file data
- echo $data;
+ download::send($filename, $data);
}
}
-} // End download \ No newline at end of file
+} // End download