]>
Commit | Line | Data |
---|---|---|
4188f38a | 1 | <?php |
2 | if(!is_callable("http_build_url")){ | |
3 | define('HTTP_URL_REPLACE', 1); // Replace every part of the first URL when there's one of the second URL | |
4 | define('HTTP_URL_JOIN_PATH', 2); // Join relative paths | |
5 | define('HTTP_URL_JOIN_QUERY', 4); // Join query strings | |
6 | define('HTTP_URL_STRIP_USER', 8); // Strip any user authentication information | |
7 | define('HTTP_URL_STRIP_PASS', 16); // Strip any password authentication information | |
8 | define('HTTP_URL_STRIP_AUTH', 32); // Strip any authentication information | |
9 | define('HTTP_URL_STRIP_PORT', 64); // Strip explicit port numbers | |
10 | define('HTTP_URL_STRIP_PATH', 128); // Strip complete path | |
11 | define('HTTP_URL_STRIP_QUERY', 256); // Strip query string | |
12 | define('HTTP_URL_STRIP_FRAGMENT', 512); // Strip any fragments (#identifier) | |
13 | define('HTTP_URL_STRIP_ALL', 1024); // Strip anything but scheme and host | |
14 | ||
15 | // Build an URL | |
16 | // The parts of the second URL will be merged into the first according to the flags argument. | |
17 | // | |
18 | // @param mixed (Part(s) of) an URL in form of a string or associative array like parse_url() returns | |
19 | // @param mixed Same as the first argument | |
20 | // @param int A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default | |
21 | // @param array If set, it will be filled with the parts of the composed url like parse_url() would return | |
22 | function http_build_url($url, $parts = array (), $flags = HTTP_URL_REPLACE, &$new_url = false) { | |
23 | $keys = array ( | |
24 | 'user', | |
25 | 'pass', | |
26 | 'port', | |
27 | 'path', | |
28 | 'query', | |
29 | 'fragment' | |
30 | ); | |
31 | ||
32 | // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs | |
33 | if ($flags & HTTP_URL_STRIP_ALL) { | |
34 | $flags |= HTTP_URL_STRIP_USER; | |
35 | $flags |= HTTP_URL_STRIP_PASS; | |
36 | $flags |= HTTP_URL_STRIP_PORT; | |
37 | $flags |= HTTP_URL_STRIP_PATH; | |
38 | $flags |= HTTP_URL_STRIP_QUERY; | |
39 | $flags |= HTTP_URL_STRIP_FRAGMENT; | |
40 | } | |
41 | // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS | |
42 | else if ($flags & HTTP_URL_STRIP_AUTH) { | |
43 | $flags |= HTTP_URL_STRIP_USER; | |
44 | $flags |= HTTP_URL_STRIP_PASS; | |
45 | } | |
46 | ||
47 | // Parse the original URL | |
48 | $parse_url = parse_url($url); | |
49 | ||
50 | // Scheme and Host are always replaced | |
51 | if (isset($parts['scheme'])) | |
52 | $parse_url['scheme'] = $parts['scheme']; | |
53 | ||
54 | if (isset($parts['host'])) | |
55 | $parse_url['host'] = $parts['host']; | |
56 | ||
57 | // (If applicable) Replace the original URL with it's new parts | |
58 | if ($flags & HTTP_URL_REPLACE) { | |
59 | foreach ($keys as $key) { | |
60 | if (isset($parts[$key])) | |
61 | $parse_url[$key] = $parts[$key]; | |
62 | } | |
63 | } else { | |
64 | // Join the original URL path with the new path | |
65 | if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) { | |
66 | if (isset($parse_url['path'])) | |
67 | $parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/'); | |
68 | else | |
69 | $parse_url['path'] = $parts['path']; | |
70 | } | |
71 | ||
72 | // Join the original query string with the new query string | |
73 | if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) { | |
74 | if (isset($parse_url['query'])) | |
75 | $parse_url['query'] .= '&' . $parts['query']; | |
76 | else | |
77 | $parse_url['query'] = $parts['query']; | |
78 | } | |
79 | } | |
80 | ||
81 | // Strips all the applicable sections of the URL | |
82 | // Note: Scheme and Host are never stripped | |
83 | foreach ($keys as $key) { | |
84 | if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key))) | |
85 | unset($parse_url[$key]); | |
86 | } | |
87 | ||
88 | $new_url = $parse_url; | |
89 | ||
90 | return ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '') . ((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') . '@' : '') | |
91 | . ((isset($parse_url['host'])) ? $parse_url['host'] : '') . ((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '') . ((isset($parse_url['path'])) ? $parse_url['path'] : '') | |
92 | . ((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '') . ((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : ''); | |
93 | } | |
94 | } |