From 8015df70221beb197c366c823937cd515c8ea6cb Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Wed, 4 Nov 2015 17:49:57 +0100 Subject: [PATCH 1/2] Use UTF-8 filenames for any browser except Internet Explorer Otherwise archives such as `1 (234).tar` would be downloaded as `1 %28234%29.tar`. Ref https://github.com/owncloud/core/issues/19862 --- src/TarStreamer.php | 9 +++++- tests/Streamer.php | 79 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/TarStreamer.php b/src/TarStreamer.php index 7d167cb..0aab245 100644 --- a/src/TarStreamer.php +++ b/src/TarStreamer.php @@ -57,11 +57,18 @@ public function sendHeaders($archiveName = 'archive.tar', $contentType = 'applic 'Accept-Ranges' => 'bytes', 'Connection' => 'Keep-Alive', 'Content-Type' => $contentType, - 'Content-Disposition' => 'attachment; filename="' . $encodedArchiveName . '";', 'Cache-Control' => 'public, must-revalidate', 'Content-Transfer-Encoding' => 'binary', ]; + // Use UTF-8 filenames when not using Internet Explorer + if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') > 0) { + header('Content-Disposition: attachment; filename="' . rawurlencode($archiveName) . '"'); + } else { + header('Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode($archiveName) + . '; filename="' . rawurlencode($archiveName) . '"'); + } + foreach ($headers as $key => $value){ header("$key: $value"); } diff --git a/tests/Streamer.php b/tests/Streamer.php index caea3aa..4bd3148 100644 --- a/tests/Streamer.php +++ b/tests/Streamer.php @@ -71,6 +71,84 @@ public function providesNameAndData() ]; } + /** + * @return array array(filename, mimetype), expectedMimetype, expectedFilename, $description, $browser + */ + public function providerSendHeadersOK() { + return array( + // Regular browsers + array( + array(), + 'application/x-tar', + 'archive.tar', + 'default headers', + 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', + 'Content-Disposition: attachment; filename*=UTF-8\'\'archive.tar; filename="archive.tar"', + ), + array( + array( + 'file.tar', + 'application/octet-stream', + ), + 'application/octet-stream', + 'file.tar', + 'specific headers', + 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', + 'Content-Disposition: attachment; filename*=UTF-8\'\'file.tar; filename="file.tar"', + ), + // Internet Explorer + array( + array(), + 'application/x-tar', + 'archive.tar', + 'default headers', + 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko', + 'Content-Disposition: attachment; filename="archive.tar"', + ), + array( + array( + 'file.tar', + 'application/octet-stream', + ), + 'application/octet-stream', + 'file.tar', + 'specific headers', + 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko', + 'Content-Disposition: attachment; filename="file.tar"', + ), + ); + } + + /** + * @dataProvider providerSendHeadersOK + * @preserveGlobalState disabled + * @runInSeparateProcess + * + * @param array $arguments + * @param string $expectedMimetype + * @param string $expectedFilename + * @param string $description + * @param string $browser + * @param string $expectedDisposition + */ + public function testSendHeadersOKWithRegularBrowser(array $arguments, + $expectedMimetype, + $expectedFilename, + $description, + $browser, + $expectedDisposition) { + $_SERVER['HTTP_USER_AGENT'] = $browser; + call_user_func_array(array($this->streamer, "sendHeaders"), $arguments); + $headers = xdebug_get_headers(); + $this->assertContains('Pragma: public', $headers); + $this->assertContains('Expires: 0', $headers); + $this->assertContains('Accept-Ranges: bytes', $headers); + $this->assertContains('Connection: Keep-Alive', $headers); + $this->assertContains('Content-Transfer-Encoding: binary', $headers); + $this->assertContains('Content-Type: ' . $expectedMimetype, $headers); + $this->assertContains($expectedDisposition, $headers); + } + private function assertFileInTar($file) { $elem = $this->getElementFromTar($file); @@ -94,7 +172,6 @@ private function assertFolderInTar($folderName) /** * @param $folderName - * @param $list * @return array */ private function getElementFromTar($folderName) From 625bc285753eb0ea90f527ea8ceebcbb37af9723 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Wed, 4 Nov 2015 22:21:06 +0100 Subject: [PATCH 2/2] Install XDebug on PHP 7 --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 081521d..3330ada 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,9 @@ branches: only: - master +before_install: + - if [[ $TRAVIS_PHP_VERSION = '7.0' ]]; then pecl install xdebug; fi; + install: - composer install --dev --no-interaction