diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2014-04-23 15:58:36 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2014-04-23 15:58:36 +0200 |
commit | c6a8efeaf2853bf646ff46cfeeb5c921be09b7f9 (patch) | |
tree | bf6fce9dc94f329520b35b7a039afd22ec9200e2 | |
parent | 5327b303e32b79792de8d84135db29cf724cd5fb (diff) | |
parent | 6205c554f178e0efb0844919a7d35112353269c0 (diff) | |
download | nextcloud-server-c6a8efeaf2853bf646ff46cfeeb5c921be09b7f9.tar.gz nextcloud-server-c6a8efeaf2853bf646ff46cfeeb5c921be09b7f9.zip |
Merge pull request #8298 from owncloud/backport_6290_to_stable5
Backport #6290 to stable5
-rw-r--r-- | apps/files/download.php | 7 | ||||
-rw-r--r-- | apps/files_trashbin/download.php | 7 | ||||
-rw-r--r-- | lib/files.php | 7 | ||||
-rw-r--r-- | lib/public/response.php | 9 | ||||
-rwxr-xr-x | lib/request.php | 23 | ||||
-rw-r--r-- | lib/response.php | 14 | ||||
-rw-r--r-- | tests/lib/request.php | 52 |
7 files changed, 100 insertions, 19 deletions
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 index 60328e1dddb..9c9ec436894 100644 --- a/apps/files_trashbin/download.php +++ b/apps/files_trashbin/download.php @@ -38,12 +38,7 @@ if(!$view->file_exists($filename)) { $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::setContentDispositionHeader(basename($filename), 'attachment'); OCP\Response::disableCaching(); header('Content-Length: '. $view->filesize($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 @@ -55,6 +55,15 @@ class Response { } /** + * 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 @@ -144,6 +144,20 @@ class OC_Response { } /** + * 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 |