]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/http_build_url/http_build_url.php
Update jakeasmith/http_build_url
[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                                         // Workaround for trailing slashes
96                                         $url['path'] .= 'a';
97                                         $url['path'] = rtrim(
98                                                         str_replace(basename($url['path']), '', $url['path']),
99                                                         '/'
100                                                 ) . '/' . ltrim($parts['path'], '/');
101                                 } else {
102                                         $url['path'] = $parts['path'];
103                                 }
104                         }
105
106                         if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
107                                 if (isset($url['query'])) {
108                                         parse_str($url['query'], $url_query);
109                                         parse_str($parts['query'], $parts_query);
110
111                                         $url['query'] = http_build_query(
112                                                 array_replace_recursive(
113                                                         $url_query,
114                                                         $parts_query
115                                                 )
116                                         );
117                                 } else {
118                                         $url['query'] = $parts['query'];
119                                 }
120                         }
121                 }
122
123                 if (isset($url['path']) && $url['path'] !== '' && substr($url['path'], 0, 1) !== '/') {
124                         $url['path'] = '/' . $url['path'];
125                 }
126
127                 foreach ($keys as $key) {
128                         $strip = 'HTTP_URL_STRIP_' . strtoupper($key);
129                         if ($flags & constant($strip)) {
130                                 unset($url[$key]);
131                         }
132                 }
133
134                 $parsed_string = '';
135
136                 if (!empty($url['scheme'])) {
137                         $parsed_string .= $url['scheme'] . '://';
138                 }
139
140                 if (!empty($url['user'])) {
141                         $parsed_string .= $url['user'];
142
143                         if (isset($url['pass'])) {
144                                 $parsed_string .= ':' . $url['pass'];
145                         }
146
147                         $parsed_string .= '@';
148                 }
149
150                 if (!empty($url['host'])) {
151                         $parsed_string .= $url['host'];
152                 }
153
154                 if (!empty($url['port'])) {
155                         $parsed_string .= ':' . $url['port'];
156                 }
157
158                 if (!empty($url['path'])) {
159                         $parsed_string .= $url['path'];
160                 }
161
162                 if (!empty($url['query'])) {
163                         $parsed_string .= '?' . $url['query'];
164                 }
165
166                 if (!empty($url['fragment'])) {
167                         $parsed_string .= '#' . $url['fragment'];
168                 }
169
170                 $new_url = $url;
171
172                 return $parsed_string;
173         }
174 }