[Pkg-owncloud-commits] [php-sabredav] 06/23: Created URLUtil class to encode uri's.

David Prévot taffit at moszumanska.debian.org
Sat Nov 30 15:44:00 UTC 2013


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to tag version-1.0.12
in repository php-sabredav.

commit 8b61e1952dbdefe53eb75c2d2836fe6983237f58
Author: Evert Pot <evert at rooftopsolutions.nl>
Date:   Fri Mar 26 15:12:18 2010 +0900

    Created URLUtil class to encode uri's.
    
    Updates Issue 26.
---
 lib/Sabre/DAV/Property/Response.php |   7 +--
 lib/Sabre/DAV/Server.php            |   2 +-
 lib/Sabre/DAV/URLUtil.php           | 111 ++++++++++++++++++++++++++++++++++++
 tests/Sabre/DAV/URLUtilTest.php     |  52 +++++++++++++++++
 4 files changed, 165 insertions(+), 7 deletions(-)

diff --git a/lib/Sabre/DAV/Property/Response.php b/lib/Sabre/DAV/Property/Response.php
index 77bdcaf..2a9698b 100644
--- a/lib/Sabre/DAV/Property/Response.php
+++ b/lib/Sabre/DAV/Property/Response.php
@@ -82,12 +82,7 @@ class Sabre_DAV_Property_Response extends Sabre_DAV_Property  {
         $xresponse = $document->createElementNS('DAV:','d:response');
         $dom->appendChild($xresponse); 
 
-        $uri = explode('/',trim($this->href,'/'));
-
-        // Decoding the uri part-by-part, for instance to make sure we got spaces, and not %20
-        foreach($uri as $k=>$item) $uri[$k] = rawurlencode($item);
-
-        $uri = implode('/',$uri);
+        $uri = Sabre_DAV_URLUtil::encodePath($this->href);
 
         // TODO: we need a better way to do this
         if ($uri!='' && isset($properties[200]['{DAV:}resourcetype']) && $properties[200]['{DAV:}resourcetype']->getValue()=='{DAV:}collection') $uri .='/';
diff --git a/lib/Sabre/DAV/Server.php b/lib/Sabre/DAV/Server.php
index fde3875..56ff5a1 100644
--- a/lib/Sabre/DAV/Server.php
+++ b/lib/Sabre/DAV/Server.php
@@ -750,7 +750,7 @@ class Sabre_DAV_Server {
 
         if (strpos($uri,$this->baseUri)===0) {
 
-            return trim(urldecode(substr($uri,strlen($this->baseUri))),'/');
+            return trim(Sabre_DAV_URLUtil::decodePath(substr($uri,strlen($this->baseUri))),'/');
 
         } else {
 
diff --git a/lib/Sabre/DAV/URLUtil.php b/lib/Sabre/DAV/URLUtil.php
new file mode 100644
index 0000000..60740e7
--- /dev/null
+++ b/lib/Sabre/DAV/URLUtil.php
@@ -0,0 +1,111 @@
+<?php
+
+/**
+ * URL utilities 
+ * 
+ * @package Sabre
+ * @subpackage DAV
+ * @copyright Copyright (C) 2007-2010 Rooftop Solutions. All rights reserved.
+ * @author Evert Pot (http://www.rooftopsolutions.nl/) 
+ */
+
+/**
+ * URL utility class
+ *
+ * This class provides methods to deal with encoding and decoding url (percent encoded) strings.
+ *
+ * It was not possible to use PHP's built-in methods for this, because some clients don't like
+ * encoding of certain characters.
+ *
+ * Specifically, it was found that GVFS (gnome's webdav client) does not like encoding of ( and
+ * ). Since these are reserved, but don't have a reserved meaning in url, these characters are
+ * kept as-is.
+ */
+class Sabre_DAV_URLUtil {
+
+    /**
+     * Encodes the path of a url.
+     *
+     * slashes (/) are treated as path-separators.
+     * 
+     * @param string $path 
+     * @return string 
+     */
+    static function encodePath($path) {
+
+        $path = explode('/',$path);
+        return implode('/',array_map(array('Sabre_DAV_URLUtil','encodePathSegment'), $path));
+
+    }
+
+    /**
+     * Encodes a 1 segment of a path
+     *
+     * Slashes are considered part of the name, and are encoded as %2f
+     * 
+     * @param string $pathSegment 
+     * @return string 
+     */
+    static function encodePathSegment($pathSegment) {
+
+        $newStr = '';
+        for($i=0;$i<strlen($pathSegment);$i++) {
+            $c = ord($pathSegment[$i]);
+
+            if(
+                
+                /* Unreserved chacaters */
+
+                ($c>=0x41 /* A */ && $c<=0x5a /* Z */) ||
+                ($c>=0x61 /* a */ && $c<=0x7a /* z */) ||
+                ($c>=0x30 /* 0 */ && $c<=0x39 /* 9 */) ||
+                $c===0x5f /* _ */ ||
+                $c===0x2d /* - */ ||
+                $c===0x2e /* . */ ||
+                $c===0x7E /* ~ */ ||
+
+                /* Reserved, but no reserved purpose */
+                $c===0x28 /* ( */ ||
+                $c===0x29 /* ) */
+
+            ) { 
+                $newStr.=$pathSegment[$i];
+            } else {
+                $newStr.='%' . str_pad(dechex($c), 2, '0', STR_PAD_LEFT);
+            }
+                
+        }
+        return $newStr;
+
+    }
+
+    /**
+     * Decodes a url-encoded path
+     *
+     * It should be noted that this method just uses php's urldecode.
+     * 
+     * @param string $path 
+     * @return string 
+     */
+    static function decodePath($path) {
+
+        return urldecode($path);
+
+    }
+
+    /**
+     * Decodes a url-encoded path segment
+     *
+     * It should be noted that this method just uses php's urldecode.
+     * 
+     * @param string $path 
+     * @return string 
+     */
+    static function decodePathSegment($path) {
+
+        return urldecode($path);
+
+    }
+
+
+}
diff --git a/tests/Sabre/DAV/URLUtilTest.php b/tests/Sabre/DAV/URLUtilTest.php
new file mode 100644
index 0000000..b0557d7
--- /dev/null
+++ b/tests/Sabre/DAV/URLUtilTest.php
@@ -0,0 +1,52 @@
+<?php
+
+class Sabre_DAV_URLUtilTest extends PHPUnit_Framework_TestCase{
+
+    function testEncodePath() {
+
+        $str = '';
+        for($i=0;$i<128;$i++) $str.=chr($i);
+
+        $newStr = Sabre_DAV_URLUtil::encodePath($str);
+
+        $this->assertEquals(
+            '%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f'.
+            '%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f'.
+            '%20%21%22%23%24%25%26%27()%2a%2b%2c-./'.
+            '0123456789%3a%3b%3c%3d%3e%3f'.
+            '%40ABCDEFGHIJKLMNO' .
+            'PQRSTUVWXYZ%5b%5c%5d%5e_' .
+            '%60abcdefghijklmno' .
+            'pqrstuvwxyz%7b%7c%7d~%7f',
+            $newStr);
+
+        $this->assertEquals($str,Sabre_DAV_URLUtil::decodePath($newStr));
+
+    }
+
+    function testEncodePathSegment() {
+
+        $str = '';
+        for($i=0;$i<128;$i++) $str.=chr($i);
+
+        $newStr = Sabre_DAV_URLUtil::encodePathSegment($str);
+
+        // Note: almost exactly the same as the last test, with the
+        // exception of the encoding of / (ascii code 2f)
+        $this->assertEquals(
+            '%00%01%02%03%04%05%06%07%08%09%0a%0b%0c%0d%0e%0f'.
+            '%10%11%12%13%14%15%16%17%18%19%1a%1b%1c%1d%1e%1f'.
+            '%20%21%22%23%24%25%26%27()%2a%2b%2c-.%2f'.
+            '0123456789%3a%3b%3c%3d%3e%3f'.
+            '%40ABCDEFGHIJKLMNO' .
+            'PQRSTUVWXYZ%5b%5c%5d%5e_' .
+            '%60abcdefghijklmno' .
+            'pqrstuvwxyz%7b%7c%7d~%7f',
+            $newStr);
+
+        $this->assertEquals($str,Sabre_DAV_URLUtil::decodePathSegment($newStr));
+
+    }
+
+
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list