6 * This class provides methods to deal with encoding and decoding url (percent encoded) strings.
8 * It was not possible to use PHP's built-in methods for this, because some clients don't like
9 * encoding of certain characters.
11 * Specifically, it was found that GVFS (gnome's webdav client) does not like encoding of ( and
12 * ). Since these are reserved, but don't have a reserved meaning in url, these characters are
17 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
18 * @author Evert Pot (http://www.rooftopsolutions.nl/)
19 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
21 class Sabre_DAV_URLUtil {
24 * Encodes the path of a url.
26 * slashes (/) are treated as path-separators.
31 static function encodePath($path) {
33 return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/])/',function($match) {
35 return '%'.sprintf('%02x',ord($match[0]));
42 * Encodes a 1 segment of a path
44 * Slashes are considered part of the name, and are encoded as %2f
46 * @param string $pathSegment
49 static function encodePathSegment($pathSegment) {
51 return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)])/',function($match) {
53 return '%'.sprintf('%02x',ord($match[0]));
59 * Decodes a url-encoded path
64 static function decodePath($path) {
66 return self::decodePathSegment($path);
71 * Decodes a url-encoded path segment
76 static function decodePathSegment($path) {
78 $path = rawurldecode($path);
79 $encoding = mb_detect_encoding($path, array('UTF-8','ISO-8859-1'));
84 $path = utf8_encode($path);
93 * Returns the 'dirname' and 'basename' for a path.
95 * The reason there is a custom function for this purpose, is because
96 * basename() is locale aware (behaviour changes if C locale or a UTF-8 locale is used)
97 * and we need a method that just operates on UTF-8 characters.
99 * In addition basename and dirname are platform aware, and will treat backslash (\) as a
100 * directory separator on windows.
102 * This method returns the 2 components as an array.
104 * If there is no dirname, it will return an empty string. Any / appearing at the end of the
105 * string is stripped off.
107 * @param string $path
110 static function splitPath($path) {
113 if(preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u',$path,$matches)) {
114 return array($matches[1],$matches[2]);
116 return array(null,null);