]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/URLUtil.php
privacy_image_cache: checking if the cached file really is an image
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / URLUtil.php
1 <?php
2
3 /**
4  * URL utility class
5  *
6  * This class provides methods to deal with encoding and decoding url (percent encoded) strings.
7  *
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.
10  *
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
13  * kept as-is.
14  *
15  * @package Sabre
16  * @subpackage DAV
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
20  */
21 class Sabre_DAV_URLUtil {
22
23     /**
24      * Encodes the path of a url.
25      *
26      * slashes (/) are treated as path-separators.
27      *
28      * @param string $path
29      * @return string
30      */
31     static function encodePath($path) {
32
33         return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)\/])/',function($match) {
34
35             return '%'.sprintf('%02x',ord($match[0]));
36
37         }, $path);
38
39     }
40
41     /**
42      * Encodes a 1 segment of a path
43      *
44      * Slashes are considered part of the name, and are encoded as %2f
45      *
46      * @param string $pathSegment
47      * @return string
48      */
49     static function encodePathSegment($pathSegment) {
50
51         return preg_replace_callback('/([^A-Za-z0-9_\-\.~\(\)])/',function($match) {
52
53             return '%'.sprintf('%02x',ord($match[0]));
54
55         }, $pathSegment);
56     }
57
58     /**
59      * Decodes a url-encoded path
60      *
61      * @param string $path
62      * @return string
63      */
64     static function decodePath($path) {
65
66         return self::decodePathSegment($path);
67
68     }
69
70     /**
71      * Decodes a url-encoded path segment
72      *
73      * @param string $path
74      * @return string
75      */
76     static function decodePathSegment($path) {
77
78         $path = rawurldecode($path);
79         $encoding = mb_detect_encoding($path, array('UTF-8','ISO-8859-1'));
80
81         switch($encoding) {
82
83             case 'ISO-8859-1' :
84                 $path = utf8_encode($path);
85
86         }
87
88         return $path;
89
90     }
91
92     /**
93      * Returns the 'dirname' and 'basename' for a path.
94      *
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.
98      *
99      * In addition basename and dirname are platform aware, and will treat backslash (\) as a
100      * directory separator on windows.
101      *
102      * This method returns the 2 components as an array.
103      *
104      * If there is no dirname, it will return an empty string. Any / appearing at the end of the
105      * string is stripped off.
106      *
107      * @param string $path
108      * @return array
109      */
110     static function splitPath($path) {
111
112         $matches = array();
113         if(preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u',$path,$matches)) {
114             return array($matches[1],$matches[2]);
115         } else {
116             return array(null,null);
117         }
118
119     }
120
121 }