4
4
5
5
class CSSUrlUtils {
6
6
7
- static function loadURL ($ sURL ) {
7
+ /**
8
+ * Requests the contents of an URL
9
+ *
10
+ * @param string $sURL the URL to fetch
11
+ * @returns array an array in the form:
12
+ * 'charset' => the charset of the response as specified by the
13
+ * HTTP Content-Type header, if specified
14
+ * 'response' => the response body
15
+ **/
16
+ static public function loadURL ($ sURL ) {
8
17
$ rCurl = curl_init ();
9
18
curl_setopt ($ rCurl , CURLOPT_URL , $ sURL );
10
19
//curl_setopt($rCurl, CURLOPT_HEADER, true);
@@ -21,7 +30,7 @@ static function loadURL($sURL) {
21
30
'response ' => $ mResponse
22
31
);
23
32
if ($ aInfos ['content_type ' ]) {
24
- if (preg_match ('/charset=([a-zA-Z0-9-]*)/ ' , $ aInfos ['content_type ' ], $ aMatches )) {
33
+ if (preg_match ('/charset=([a-zA-Z0-9-]*)/ ' , $ aInfos ['content_type ' ], $ aMatches )) {
25
34
$ aResult ['charset ' ] = $ aMatches [0 ];
26
35
}
27
36
}
@@ -31,12 +40,11 @@ static function loadURL($sURL) {
31
40
/**
32
41
* CSSUrlUtils::joinPaths( string $head, string $tail [, string $...] )
33
42
*
34
- * @param $head string the head component of the path
35
- * @param $tail string at least one path component
36
- * @returns string the resulting path
43
+ * @param string $head the head component of the path
44
+ * @param string $tail at least one path component
45
+ * @returns string the resulting path
37
46
**/
38
- static function joinPaths ()
39
- {
47
+ static public function joinPaths () {
40
48
$ num_args = func_num_args ();
41
49
if ($ num_args < 1 ) return '' ;
42
50
$ args = func_get_args ();
@@ -54,12 +62,10 @@ static function joinPaths()
54
62
/**
55
63
* Returns boolean based on whether given path is absolute or not.
56
64
*
57
- * @static
58
- * @access public
59
65
* @param string $path Given path
60
- * @return boolean True if the path is absolute, false if it is not
66
+ * @return boolean True if the path is absolute, false if it is not
61
67
*/
62
- static function isAbsPath ($ sPath ) {
68
+ static public function isAbsPath ($ sPath ) {
63
69
if (preg_match ('#(?:/| \\\)\.\.(?=/|$)# ' , $ sPath )) {
64
70
return false ;
65
71
}
@@ -69,8 +75,70 @@ static function isAbsPath($sPath) {
69
75
return ($ sPath [0 ] == '/ ' ) || ($ sPath [0 ] == '~ ' );
70
76
}
71
77
72
- static function isAbsURL ($ sPath )
78
+ /**
79
+ * Tests if an URL is absolute
80
+ *
81
+ * @param string $sURL
82
+ * @return boolean
83
+ **/
84
+ static public function isAbsURL ($ sURL ) {
85
+ return preg_match ('#^(http|https|ftp)://# ' , $ sURL );
86
+ }
87
+
88
+ /**
89
+ * Returns the parent path of an URL or path
90
+ *
91
+ * @param string $sURL an URL
92
+ * @returns string an URL
93
+ **/
94
+ static public function dirname ($ sURL ) {
95
+ $ aURL = parse_url ($ sURL );
96
+ if (isset ($ aURL ['path ' ])) {
97
+ $ sPath = dirname ($ aURL ['path ' ]);
98
+ if ($ sPath == '/ ' ) {
99
+ unset($ aURL ['path ' ]);
100
+ } else {
101
+ $ aURL ['path ' ] = $ sPath ;
102
+ }
103
+ }
104
+ return self ::buildURL ($ aURL );
105
+ }
106
+
107
+ /**
108
+ * Builds an URL from an array of URL parts
109
+ *
110
+ * @param array $aURL URL parts in the format returned by parse_url
111
+ * @return string the builded URL
112
+ * @see http://php.net/manual/function.parse-url.php
113
+ **/
114
+ static public function buildURL (array $ aURL )
73
115
{
74
- return preg_match ('#^(http|https|ftp)://# ' , $ sPath );
116
+ if (isset ($ aURL ['scheme ' ])) {
117
+ $ sURL .= $ aURL ['scheme ' ] . ':// ' ;
118
+ }
119
+ if (isset ($ aURL ['user ' ])) {
120
+ $ sURL .= $ aURL ['user ' ];
121
+ if (isset ($ aURL ['pass ' ])) {
122
+ $ sURL .= ': ' . $ aURL ['pass ' ];
123
+ }
124
+ $ sURL .= '@ ' ;
125
+ }
126
+ if (isset ($ aURL ['host ' ])) {
127
+ $ sURL .= $ aURL ['host ' ];
128
+ }
129
+ if (isset ($ aURL ['port ' ])) {
130
+ $ sURL .= ': ' . $ aURL ['port ' ];
131
+ }
132
+ if (isset ($ aURL ['path ' ])) {
133
+ if (strpos ($ aURL ['path ' ], '/ ' ) !== 0 ) $ sURL .= '/ ' ;
134
+ $ sURL .= $ aURL ['path ' ];
135
+ }
136
+ if (isset ($ aURL ['query ' ])) {
137
+ $ sURL .= '? ' . $ aURL ['query ' ];
138
+ }
139
+ if (isset ($ aURL ['fragment ' ])) {
140
+ $ sURL .= '# ' . $ aURL ['fragment ' ];
141
+ }
142
+ return $ sURL ;
75
143
}
76
144
}
0 commit comments