Skip to content

Commit

Permalink
Merge pull request #7 from owncloud/use-utf-8-filenames
Browse files Browse the repository at this point in the history
Use UTF-8 filenames for any browser except Internet Explorer
  • Loading branch information
DeepDiver1975 committed Feb 15, 2016
2 parents 07b940c + 625bc28 commit 859a017
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion src/TarStreamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
79 changes: 78 additions & 1 deletion tests/Streamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -94,7 +172,6 @@ private function assertFolderInTar($folderName)

/**
* @param $folderName
* @param $list
* @return array
*/
private function getElementFromTar($folderName)
Expand Down

0 comments on commit 859a017

Please sign in to comment.