From ee822245fb80bb17a5b7fabec17b3922d53c1a7f Mon Sep 17 00:00:00 2001 From: Jörn Friedrich Dreyer Date: Tue, 22 Apr 2014 12:22:11 +0200 Subject: backport #6290 to stable5 --- apps/files/download.php | 7 +----- apps/files_trashbin/download.php | 51 --------------------------------------- lib/files.php | 7 +----- lib/public/response.php | 9 +++++++ lib/request.php | 23 ++++++++++++++++++ lib/response.php | 14 +++++++++++ tests/lib/request.php | 52 +++++++++++++++++++++++++++++++++++++++- 7 files changed, 99 insertions(+), 64 deletions(-) delete mode 100644 apps/files_trashbin/download.php diff --git a/apps/files/download.php b/apps/files/download.php index cbf6ab17d79..ee020627eeb 100644 --- a/apps/files/download.php +++ b/apps/files/download.php @@ -40,12 +40,7 @@ if(!\OC\Files\Filesystem::file_exists($filename)) { $ftype=\OC\Files\Filesystem::getMimeType( $filename ); header('Content-Type:'.$ftype); -if ( preg_match( "/MSIE/", $_SERVER["HTTP_USER_AGENT"] ) ) { - header( 'Content-Disposition: attachment; filename="' . rawurlencode( basename($filename) ) . '"' ); -} else { - header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode( basename($filename) ) - . '; filename="' . rawurlencode( basename($filename) ) . '"' ); -} +OCP\Response::setContentDispositionHeader(basename($filename), 'attachment'); OCP\Response::disableCaching(); header('Content-Length: '.\OC\Files\Filesystem::filesize($filename)); diff --git a/apps/files_trashbin/download.php b/apps/files_trashbin/download.php deleted file mode 100644 index 60328e1dddb..00000000000 --- a/apps/files_trashbin/download.php +++ /dev/null @@ -1,51 +0,0 @@ -. -* -*/ - -// Check if we are a user -OCP\User::checkLoggedIn(); - -$filename = $_GET["file"]; - -$view = new OC_FilesystemView('/'.\OCP\User::getUser().'/files_trashbin/files'); - -if(!$view->file_exists($filename)) { - header("HTTP/1.0 404 Not Found"); - $tmpl = new OCP\Template( '', '404', 'guest' ); - $tmpl->assign('file', $filename); - $tmpl->printPage(); - exit; -} - -$ftype=$view->getMimeType( $filename ); - -header('Content-Type:'.$ftype);if ( preg_match( "/MSIE/", $_SERVER["HTTP_USER_AGENT"] ) ) { - header( 'Content-Disposition: attachment; filename="' . rawurlencode( basename($filename) ) . '"' ); -} else { - header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode( basename($filename) ) - . '; filename="' . rawurlencode( basename($filename) ) . '"' ); -} -OCP\Response::disableCaching(); -header('Content-Length: '. $view->filesize($filename)); - -OC_Util::obEnd(); -$view->readfile( $filename ); diff --git a/lib/files.php b/lib/files.php index 534ffb9d470..755758a7ac1 100644 --- a/lib/files.php +++ b/lib/files.php @@ -111,12 +111,7 @@ class OC_Files { } OC_Util::obEnd(); if ($zip or \OC\Files\Filesystem::isReadable($filename)) { - if ( preg_match( "/MSIE/", $_SERVER["HTTP_USER_AGENT"] ) ) { - header( 'Content-Disposition: attachment; filename="' . rawurlencode($name) . '"' ); - } else { - header( 'Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode($name) - . '; filename="' . rawurlencode($name) . '"' ); - } + OC_Response::setContentDispositionHeader($name, 'attachment'); header('Content-Transfer-Encoding: binary'); OC_Response::disableCaching(); if ($zip) { diff --git a/lib/public/response.php b/lib/public/response.php index de0c3f25347..0e51e4d591b 100644 --- a/lib/public/response.php +++ b/lib/public/response.php @@ -54,6 +54,15 @@ class Response { \OC_Response::setLastModifiedHeader( $lastModified ); } + /** + * Sets the content disposition header (with possible workarounds) + * @param string $filename file name + * @param string $type disposition type, either 'attachment' or 'inline' + */ + static public function setContentDispositionHeader( $filename, $type = 'attachment' ) { + \OC_Response::setContentDispositionHeader( $filename, $type ); + } + /** * @brief disable browser caching * @see enableCaching with cache_time = 0 diff --git a/lib/request.php b/lib/request.php index 4c627db3e6f..a9832621125 100755 --- a/lib/request.php +++ b/lib/request.php @@ -7,6 +7,11 @@ */ class OC_Request { + + const USER_AGENT_IE = '/MSIE/'; + // Android Chrome user agent: https://developers.google.com/chrome/mobile/docs/user-agent + const USER_AGENT_ANDROID_MOBILE_CHROME = '#Android.*Chrome/[.0-9]*#'; + const REGEX_LOCALHOST = '/^(127\.0\.0\.1|localhost)(:[0-9]+|)$/'; /** @@ -269,4 +274,22 @@ class OC_Request { return false; } } + + /** + * Checks whether the user agent matches a given regex + * @param string|array $agent agent name or array of agent names + * @return boolean true if at least one of the given agent matches, + * false otherwise + */ + static public function isUserAgent($agent) { + if (!is_array($agent)) { + $agent = array($agent); + } + foreach ($agent as $regex) { + if (preg_match($regex, $_SERVER['HTTP_USER_AGENT'])) { + return true; + } + } + return false; + } } diff --git a/lib/response.php b/lib/response.php index b546ca5fbb0..d250c52765c 100644 --- a/lib/response.php +++ b/lib/response.php @@ -143,6 +143,20 @@ class OC_Response { header('Last-Modified: '.$lastModified); } + /** + * Sets the content disposition header (with possible workarounds) + * @param string $filename file name + * @param string $type disposition type, either 'attachment' or 'inline' + */ + static public function setContentDispositionHeader( $filename, $type = 'attachment' ) { + if (OC_Request::isUserAgent(array(OC_Request::USER_AGENT_IE, OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME))) { + header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' ); + } else { + header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename ) + . '; filename="' . rawurlencode( $filename ) . '"' ); + } + } + /** * @brief Send file as response, checking and setting caching headers * @param $filepath of file to send diff --git a/tests/lib/request.php b/tests/lib/request.php index 2c4cee445be..2d346d08960 100644 --- a/tests/lib/request.php +++ b/tests/lib/request.php @@ -206,4 +206,54 @@ class Test_Request extends PHPUnit_Framework_TestCase { unset($_SERVER['HTTP_HOST']); OC::$CLI = $oldCLI; } -} + + /** + * @dataProvider userAgentProvider + */ + public function testUserAgent($testAgent, $userAgent, $matches) { + $_SERVER['HTTP_USER_AGENT'] = $testAgent; + $this->assertEquals($matches, OC_Request::isUserAgent($userAgent)); + } + + function userAgentProvider() { + return array( + array( + 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)', + OC_Request::USER_AGENT_IE, + true + ), + array( + 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0', + OC_Request::USER_AGENT_IE, + false + ), + array( + 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36', + OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME, + true + ), + array( + 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)', + OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME, + false + ), + // test two values + array( + 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)', + array( + OC_Request::USER_AGENT_IE, + OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME, + ), + true + ), + array( + 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36', + array( + OC_Request::USER_AGENT_IE, + OC_Request::USER_AGENT_ANDROID_MOBILE_CHROME, + ), + true + ), + ); + } +} \ No newline at end of file -- cgit v1.2.3 From 6205c554f178e0efb0844919a7d35112353269c0 Mon Sep 17 00:00:00 2001 From: Jörn Friedrich Dreyer Date: Tue, 22 Apr 2014 13:43:51 +0200 Subject: restore trashbin download.php --- apps/files_trashbin/download.php | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 apps/files_trashbin/download.php diff --git a/apps/files_trashbin/download.php b/apps/files_trashbin/download.php new file mode 100644 index 00000000000..9c9ec436894 --- /dev/null +++ b/apps/files_trashbin/download.php @@ -0,0 +1,46 @@ +. +* +*/ + +// Check if we are a user +OCP\User::checkLoggedIn(); + +$filename = $_GET["file"]; + +$view = new OC_FilesystemView('/'.\OCP\User::getUser().'/files_trashbin/files'); + +if(!$view->file_exists($filename)) { + header("HTTP/1.0 404 Not Found"); + $tmpl = new OCP\Template( '', '404', 'guest' ); + $tmpl->assign('file', $filename); + $tmpl->printPage(); + exit; +} + +$ftype=$view->getMimeType( $filename ); + +OCP\Response::setContentDispositionHeader(basename($filename), 'attachment'); +OCP\Response::disableCaching(); +header('Content-Length: '. $view->filesize($filename)); + +OC_Util::obEnd(); +$view->readfile( $filename ); -- cgit v1.2.3