]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/http_build_url/http_build_url.php
Improve lowercasing of scheme & domain
[Github/YOURLS.git] / includes / http_build_url / http_build_url.php
1 <?php
2
3 /**
4  * URL constants as defined in the PHP Manual under "Constants usable with
5  * http_build_url()".
6  *
7  * @see http://us2.php.net/manual/en/http.constants.php#http.constants.url
8  */
9 if (!defined('HTTP_URL_REPLACE')) {
10         define('HTTP_URL_REPLACE', 1);
11 }
12 if (!defined('HTTP_URL_JOIN_PATH')) {
13         define('HTTP_URL_JOIN_PATH', 2);
14 }
15 if (!defined('HTTP_URL_JOIN_QUERY')) {
16         define('HTTP_URL_JOIN_QUERY', 4);
17 }
18 if (!defined('HTTP_URL_STRIP_USER')) {
19         define('HTTP_URL_STRIP_USER', 8);
20 }
21 if (!defined('HTTP_URL_STRIP_PASS')) {
22         define('HTTP_URL_STRIP_PASS', 16);
23 }
24 if (!defined('HTTP_URL_STRIP_AUTH')) {
25         define('HTTP_URL_STRIP_AUTH', 32);
26 }
27 if (!defined('HTTP_URL_STRIP_PORT')) {
28         define('HTTP_URL_STRIP_PORT', 64);
29 }
30 if (!defined('HTTP_URL_STRIP_PATH')) {
31         define('HTTP_URL_STRIP_PATH', 128);
32 }
33 if (!defined('HTTP_URL_STRIP_QUERY')) {
34         define('HTTP_URL_STRIP_QUERY', 256);
35 }
36 if (!defined('HTTP_URL_STRIP_FRAGMENT')) {
37         define('HTTP_URL_STRIP_FRAGMENT', 512);
38 }
39 if (!defined('HTTP_URL_STRIP_ALL')) {
40         define('HTTP_URL_STRIP_ALL', 1024);
41 }
42
43 if (!function_exists('http_build_url')) {
44
45         /**
46          * Build a URL.
47          *
48          * The parts of the second URL will be merged into the first according to
49          * the flags argument.
50          *
51          * @param mixed $url     (part(s) of) an URL in form of a string or
52          *                       associative array like parse_url() returns
53          * @param mixed $parts   same as the first argument
54          * @param int   $flags   a bitmask of binary or'ed HTTP_URL constants;
55          *                       HTTP_URL_REPLACE is the default
56          * @param array $new_url if set, it will be filled with the parts of the
57          *                       composed url like parse_url() would return
58          * @return string
59          */
60         function http_build_url($url, $parts = array(), $flags = HTTP_URL_REPLACE, &$new_url = array())
61         {
62                 is_array($url) || $url = parse_url($url);
63                 is_array($parts) || $parts = parse_url($parts);
64
65                 isset($url['query']) && is_string($url['query']) || $url['query'] = null;
66                 isset($parts['query']) && is_string($parts['query']) || $parts['query'] = null;
67
68                 $keys = array('user', 'pass', 'port', 'path', 'query', 'fragment');
69
70                 // HTTP_URL_STRIP_ALL and HTTP_URL_STRIP_AUTH cover several other flags.
71                 if ($flags & HTTP_URL_STRIP_ALL) {
72                         $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS
73                                 | HTTP_URL_STRIP_PORT | HTTP_URL_STRIP_PATH
74                                 | HTTP_URL_STRIP_QUERY | HTTP_URL_STRIP_FRAGMENT;
75                 } elseif ($flags & HTTP_URL_STRIP_AUTH) {
76                         $flags |= HTTP_URL_STRIP_USER | HTTP_URL_STRIP_PASS;
77                 }
78
79                 // Schema and host are alwasy replaced
80                 foreach (array('scheme', 'host') as $part) {
81                         if (isset($parts[$part])) {
82                                 $url[$part] = $parts[$part];
83                         }
84                 }
85
86                 if ($flags & HTTP_URL_REPLACE) {
87                         foreach ($keys as $key) {
88                                 if (isset($parts[$key])) {
89                                         $url[$key] = $parts[$key];
90                                 }
91                         }
92                 } else {
93                         if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) {
94                                 if (isset($url['path']) && substr($parts['path'], 0, 1) !== '/') {
95                                         $url['path'] = rtrim(
96                                                         str_replace(basename($url['path']), '', $url['path']),
97                                                         '/'
98                                                 ) . '/' . ltrim($parts['path'], '/');
99                                 } else {
100                                         $url['path'] = $parts['path'];
101                                 }
102                         }
103
104                         if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
105                                 if (isset($url['query'])) {
106                                         parse_str($url['query'], $url_query);
107                                         parse_str($parts['query'], $parts_query);
108
109                                         $url['query'] = http_build_query(
110                                                 array_replace_recursive(
111                                                         $url_query,
112                                                         $parts_query
113                                                 )
114                                         );
115                                 } else {
116                                         $url['query'] = $parts['query'];
117                                 }
118                         }
119                 }
120
121                 if (isset($url['path']) && substr($url['path'], 0, 1) !== '/') {
122                         $url['path'] = '/' . $url['path'];
123                 }
124
125                 foreach ($keys as $key) {
126                         $strip = 'HTTP_URL_STRIP_' . strtoupper($key);
127                         if ($flags & constant($strip)) {
128                                 unset($url[$key]);
129                         }
130                 }
131
132                 $parsed_string = '';
133
134                 if (isset($url['scheme'])) {
135                         $parsed_string .= $url['scheme'] . '://';
136                 }
137
138                 if (isset($url['user'])) {
139                         $parsed_string .= $url['user'];
140
141                         if (isset($url['pass'])) {
142                                 $parsed_string .= ':' . $url['pass'];
143                         }
144
145                         $parsed_string .= '@';
146                 }
147
148                 if (isset($url['host'])) {
149                         $parsed_string .= $url['host'];
150                 }
151
152                 if (isset($url['port'])) {
153                         $parsed_string .= ':' . $url['port'];
154                 }
155
156                 if (!empty($url['path'])) {
157                         $parsed_string .= $url['path'];
158                 } else {
159                         $parsed_string .= '/';
160                 }
161
162                 if (isset($url['query'])) {
163                         $parsed_string .= '?' . $url['query'];
164                 }
165
166                 if (isset($url['fragment'])) {
167                         $parsed_string .= '#' . $url['fragment'];
168                 }
169
170                 $new_url = $url;
171
172                 return $parsed_string;
173         }
174 }