Merge pull request #3943 from owncloud/encode_uri_component

Add path encoding to public API.
This commit is contained in:
VicDeo 2013-07-09 08:13:50 -07:00
commit 43755a292b
8 changed files with 48 additions and 16 deletions

View File

@ -7,8 +7,7 @@
<?php endif;?>
<?php for($i=0; $i<count($_["breadcrumb"]); $i++):
$crumb = $_["breadcrumb"][$i];
$dir = str_replace('+', '%20', urlencode($crumb["dir"]));
$dir = str_replace('%2F', '/', $dir); ?>
$dir = \OCP\Util::encodePath($crumb["dir"]); ?>
<div class="crumb <?php if($i == count($_["breadcrumb"])-1) p('last');?> svg"
data-dir='<?php p($dir);?>'>
<a href="<?php p($_['baseURL'].$dir); ?>"><?php p($crumb["name"]); ?></a>

View File

@ -17,10 +17,8 @@ $totalsize = 0; ?>
// the older the file, the brighter the shade of grey; days*14
$relative_date_color = round((time()-$file['mtime'])/60/60/24*14);
if($relative_date_color>160) $relative_date_color = 160;
$name = rawurlencode($file['name']);
$name = str_replace('%2F', '/', $name);
$directory = rawurlencode($file['directory']);
$directory = str_replace('%2F', '/', $directory); ?>
$name = \OCP\Util::encodePath($file['name']);
$directory = \OCP\Util::encodePath($file['directory']); ?>
<tr data-id="<?php p($file['fileid']); ?>"
data-file="<?php p($name);?>"
data-type="<?php ($file['type'] == 'dir')?p('dir'):p('file')?>"

View File

@ -101,12 +101,15 @@ $breadcrumbNav->assign('home', OCP\Util::linkTo('files', 'index.php'));
$list = new OCP\Template('files_trashbin', 'part.list', '');
$list->assign('files', $files);
$list->assign('baseURL', OCP\Util::linkTo('files_trashbin', 'index.php'). '?dir='.$dir);
$list->assign('downloadURL', OCP\Util::linkTo('files_trashbin', 'download.php') . '?file='.$dir);
$encodedDir = \OCP\Util::encodePath($dir);
$list->assign('baseURL', OCP\Util::linkTo('files_trashbin', 'index.php'). '?dir='.$encodedDir);
$list->assign('downloadURL', OCP\Util::linkTo('files_trashbin', 'download.php') . '?file='.$encodedDir);
$list->assign('disableSharing', true);
$list->assign('dirlisting', $dirlisting);
$tmpl->assign('dirlisting', $dirlisting);
$list->assign('disableDownloadActions', true);
$tmpl->assign('dirlisting', $dirlisting);
$tmpl->assign('breadcrumb', $breadcrumbNav->fetchPage());
$tmpl->assign('fileList', $list->fetchPage());
$tmpl->assign('files', $files);

View File

@ -11,8 +11,7 @@
<?php endif;?>
<?php for($i=0; $i<count($_["breadcrumb"]); $i++):
$crumb = $_["breadcrumb"][$i];
$dir = str_replace('+', '%20', urlencode($crumb["dir"]));
$dir = str_replace('%2F', '/', $dir); ?>
$dir = \OCP\Util::encodePath($crumb["dir"]); ?>
<div class="crumb <?php if($i == count($_["breadcrumb"])-1) p('last');?> svg"
data-dir='<?php p($dir);?>'>
<a href="<?php p($_['baseURL'].$dir); ?>"><?php p($crumb["name"]); ?></a>

View File

@ -4,10 +4,8 @@
// the older the file, the brighter the shade of grey; days*14
$relative_date_color = round((time()-$file['date'])/60/60/24*14);
if($relative_date_color>200) $relative_date_color = 200;
$name = str_replace('+', '%20', urlencode($file['name']));
$name = str_replace('%2F', '/', $name);
$directory = str_replace('+', '%20', urlencode($file['directory']));
$directory = str_replace('%2F', '/', $directory); ?>
$name = \OCP\Util::encodePath($file['name']);
$directory = \OCP\Util::encodePath($file['directory']); ?>
<tr data-filename="<?php p($file['name']);?>"
data-type="<?php ($file['type'] == 'dir')?p('dir'):p('file')?>"
data-mime="<?php p($file['mimetype'])?>"

View File

@ -356,6 +356,20 @@ class Util {
return(\OC_Util::sanitizeHTML($value));
}
/**
* @brief Public function to encode url parameters
*
* This function is used to encode path to file before output.
* Encoding is done according to RFC 3986 with one exception:
* Character '/' is preserved as is.
*
* @param string $component part of URI to encode
* @return string
*/
public static function encodePath($component) {
return(\OC_Util::encodePath($component));
}
/**
* @brief Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
*

View File

@ -540,6 +540,21 @@ class OC_Util {
return $value;
}
/**
* @brief Public function to encode url parameters
*
* This function is used to encode path to file before output.
* Encoding is done according to RFC 3986 with one exception:
* Character '/' is preserved as is.
*
* @param string $component part of URI to encode
* @return string
*/
public static function encodePath($component) {
$encoded = rawurlencode($component);
$encoded = str_replace('%2F', '/', $encoded);
return $encoded;
}
/**
* Check if the htaccess file is working by creating a test file in the data directory and trying to access via http

View File

@ -38,6 +38,12 @@ class Test_Util extends PHPUnit_Framework_TestCase {
$this->assertEquals("This is an harmless string.", $result);
}
function testEncodePath(){
$component = '/§#@test%&^ä/-child';
$result = OC_Util::encodePath($component);
$this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
}
function testGenerate_random_bytes() {
$result = strlen(OC_Util::generate_random_bytes(59));
$this->assertEquals(59, $result);