2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2008-10-24 01:12:09 +00:00
|
|
|
* Methods to display or download any type of file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view
|
|
|
|
* @since CakePHP(tm) v 1.2.0.5714
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-12-01 14:50:27 +00:00
|
|
|
App::import('View', 'View', false);
|
|
|
|
|
|
|
|
class MediaView extends View {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param object $controller
|
|
|
|
*/
|
2010-04-28 02:42:33 +00:00
|
|
|
function __construct(&$controller) {
|
2009-12-01 14:50:27 +00:00
|
|
|
parent::__construct($controller);
|
2010-10-03 21:10:54 +00:00
|
|
|
if (is_object($controller) && isset($controller->response)) {
|
|
|
|
$this->response = $controller->response;
|
|
|
|
} else {
|
|
|
|
App::import('Core', 'CakeRequest');
|
|
|
|
$this->response = new CakeResponse;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-24 01:12:09 +00:00
|
|
|
* Display or download the given file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
function render() {
|
2008-10-24 01:12:09 +00:00
|
|
|
$name = $download = $extension = $id = $modified = $path = $size = $cache = $mimeType = null;
|
2008-05-30 11:40:08 +00:00
|
|
|
extract($this->viewVars, EXTR_OVERWRITE);
|
|
|
|
|
|
|
|
if ($size) {
|
2008-10-24 01:12:09 +00:00
|
|
|
$id = $id . '_' . $size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($path)) {
|
|
|
|
$path = $path . $id;
|
|
|
|
} else {
|
|
|
|
$path = APP . $path . $id;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-19 21:10:13 +00:00
|
|
|
|
2010-10-03 23:30:27 +00:00
|
|
|
if (!is_file($path)) {
|
|
|
|
if (Configure::read('debug')) {
|
|
|
|
throw new NotFoundException(sprintf('The requested file %s was not found', $path));
|
|
|
|
}
|
2010-10-03 21:10:54 +00:00
|
|
|
throw new NotFoundException('The requested file was not found');
|
2009-02-27 16:37:37 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (is_null($name)) {
|
|
|
|
$name = $id;
|
|
|
|
}
|
|
|
|
|
2008-10-24 01:12:09 +00:00
|
|
|
if (is_array($mimeType)) {
|
2010-10-03 21:10:54 +00:00
|
|
|
$this->response->type($mimeType);
|
2008-10-24 01:12:09 +00:00
|
|
|
}
|
2009-03-19 21:10:13 +00:00
|
|
|
|
2010-10-03 21:10:54 +00:00
|
|
|
if (isset($extension) && $this->response->type($extension) && $this->_isActive()) {
|
2008-10-24 01:12:09 +00:00
|
|
|
$chunkSize = 8192;
|
2008-05-30 11:40:08 +00:00
|
|
|
$buffer = '';
|
|
|
|
$fileSize = @filesize($path);
|
|
|
|
$handle = fopen($path, 'rb');
|
|
|
|
|
|
|
|
if ($handle === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 06:37:35 +00:00
|
|
|
if (!empty($modified)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$modified = gmdate('D, d M Y H:i:s', strtotime($modified, time())) . ' GMT';
|
|
|
|
} else {
|
2008-10-24 11:25:19 +00:00
|
|
|
$modified = gmdate('D, d M Y H:i:s') . ' GMT';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($download) {
|
2009-02-17 18:23:44 +00:00
|
|
|
$contentTypes = array('application/octet-stream');
|
2008-05-30 11:40:08 +00:00
|
|
|
$agent = env('HTTP_USER_AGENT');
|
|
|
|
|
2009-02-17 18:23:44 +00:00
|
|
|
if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
|
|
|
|
$contentTypes[0] = 'application/octetstream';
|
|
|
|
} else if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
|
|
|
|
$contentTypes[0] = 'application/force-download';
|
2010-03-17 15:17:03 +00:00
|
|
|
array_merge($contentTypes, array(
|
2009-02-17 18:23:44 +00:00
|
|
|
'application/octet-stream',
|
|
|
|
'application/download'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
foreach($contentTypes as $contentType) {
|
2009-03-19 21:10:13 +00:00
|
|
|
$this->_header('Content-Type: ' . $contentType);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-03-19 21:10:13 +00:00
|
|
|
$this->_header(array(
|
|
|
|
'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
|
|
|
|
'Expires: 0',
|
|
|
|
'Accept-Ranges: bytes',
|
|
|
|
'Cache-Control: private' => false,
|
|
|
|
'Pragma: private'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$httpRange = env('HTTP_RANGE');
|
|
|
|
if (isset($httpRange)) {
|
2008-10-24 01:12:09 +00:00
|
|
|
list($toss, $range) = explode('=', $httpRange);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$size = $fileSize - 1;
|
|
|
|
$length = $fileSize - $range;
|
|
|
|
|
2009-03-19 21:10:13 +00:00
|
|
|
$this->_header(array(
|
|
|
|
'HTTP/1.1 206 Partial Content',
|
|
|
|
'Content-Length: ' . $length,
|
|
|
|
'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
fseek($handle, $range);
|
|
|
|
} else {
|
2009-03-19 21:10:13 +00:00
|
|
|
$this->_header('Content-Length: ' . $fileSize);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-03 21:10:54 +00:00
|
|
|
|
2008-09-18 06:37:35 +00:00
|
|
|
if ($cache) {
|
2010-10-03 21:10:54 +00:00
|
|
|
$this->response->cache(time(), $cache);
|
2008-09-18 06:37:35 +00:00
|
|
|
} else {
|
2010-10-03 21:10:54 +00:00
|
|
|
$this->response->header(array(
|
|
|
|
'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
|
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
|
|
|
|
'Pragma' => 'no-cache'
|
|
|
|
));
|
2008-09-18 06:37:35 +00:00
|
|
|
}
|
2010-10-03 21:10:54 +00:00
|
|
|
|
|
|
|
$this->response->type($extension);
|
|
|
|
$this->respose->header(array(
|
|
|
|
'Last-Modified' => $modified,
|
|
|
|
'Content-Length' => $fileSize
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-10-03 21:10:54 +00:00
|
|
|
$this->response->send();
|
2010-04-13 20:04:13 +00:00
|
|
|
$this->_clearBuffer();
|
2010-10-03 21:10:54 +00:00
|
|
|
$this->_sendFile($handle);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-04-13 20:04:13 +00:00
|
|
|
return;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-03 21:10:54 +00:00
|
|
|
protected function _sendFile($handle) {
|
|
|
|
while (!feof($handle)) {
|
|
|
|
if (!$this->_isActive()) {
|
|
|
|
fclose($handle);
|
|
|
|
return false;
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2010-10-03 21:10:54 +00:00
|
|
|
set_time_limit(0);
|
|
|
|
$buffer = fread($handle, $chunkSize);
|
|
|
|
echo $buffer;
|
|
|
|
$this->_flushBuffer();
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2010-10-03 21:10:54 +00:00
|
|
|
fclose($handle);
|
2009-03-19 21:10:13 +00:00
|
|
|
}
|
2010-04-13 20:04:13 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-13 21:22:39 +00:00
|
|
|
* Returns true if connection is still active
|
2010-04-13 20:04:13 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-10-03 21:10:54 +00:00
|
|
|
protected function _isActive() {
|
2010-04-13 20:04:13 +00:00
|
|
|
return connection_status() == 0 && !connection_aborted();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the contents of the topmost output buffer and discards them
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-10-03 21:10:54 +00:00
|
|
|
protected function _clearBuffer() {
|
2010-04-13 20:04:13 +00:00
|
|
|
return @ob_end_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flushes the contents of the output buffer
|
|
|
|
*/
|
2010-10-03 21:10:54 +00:00
|
|
|
protected function _flushBuffer() {
|
2010-04-13 20:04:13 +00:00
|
|
|
@flush();
|
|
|
|
@ob_flush();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|