1
|
|
|
<?php |
2
|
|
|
/* |
|
|
|
|
3
|
|
|
* Functions relative to how YOURLS handle some links |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Add a query var to a URL and return URL. Completely stolen from WP. |
9
|
|
|
* |
10
|
|
|
* Works with one of these parameter patterns: |
11
|
|
|
* array( 'var' => 'value' ) |
12
|
|
|
* array( 'var' => 'value' ), $url |
13
|
|
|
* 'var', 'value' |
14
|
|
|
* 'var', 'value', $url |
15
|
|
|
* If $url omitted, uses $_SERVER['REQUEST_URI'] |
16
|
|
|
* |
17
|
|
|
* The result of this function call is a URL : it should be escaped before being printed as HTML |
18
|
|
|
* |
19
|
|
|
* @since 1.5 |
20
|
|
|
* @param string|array $param1 Either newkey or an associative_array. |
21
|
|
|
* @param string $param2 Either newvalue or oldquery or URI. |
22
|
|
|
* @param string $param3 Optional. Old query or URI. |
23
|
|
|
* @return string New URL query string. |
24
|
|
|
*/ |
25
|
|
|
function yourls_add_query_arg() { |
26
|
2 |
|
$ret = ''; |
|
|
|
|
27
|
2 |
|
if ( is_array( func_get_arg(0) ) ) { |
28
|
1 |
|
if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) ) |
29
|
|
|
$uri = $_SERVER['REQUEST_URI']; |
30
|
|
|
else |
31
|
1 |
|
$uri = @func_get_arg( 1 ); |
32
|
|
|
} else { |
33
|
2 |
|
if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) ) |
34
|
|
|
$uri = $_SERVER['REQUEST_URI']; |
35
|
|
|
else |
36
|
2 |
|
$uri = @func_get_arg( 2 ); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$uri = str_replace( '&', '&', $uri ); |
40
|
|
|
|
41
|
|
|
|
42
|
2 |
|
if ( $frag = strstr( $uri, '#' ) ) |
|
|
|
|
43
|
|
|
$uri = substr( $uri, 0, -strlen( $frag ) ); |
44
|
|
|
else |
45
|
2 |
|
$frag = ''; |
46
|
|
|
|
47
|
2 |
|
if ( preg_match( '|^https?://|i', $uri, $matches ) ) { |
48
|
1 |
|
$protocol = $matches[0]; |
49
|
1 |
|
$uri = substr( $uri, strlen( $protocol ) ); |
50
|
|
|
} else { |
51
|
1 |
|
$protocol = ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
if ( strpos( $uri, '?' ) !== false ) { |
55
|
1 |
|
$parts = explode( '?', $uri, 2 ); |
56
|
1 |
|
if ( 1 == count( $parts ) ) { |
|
|
|
|
57
|
|
|
$base = '?'; |
58
|
|
|
$query = $parts[0]; |
59
|
|
|
} else { |
60
|
1 |
|
$base = $parts[0] . '?'; |
61
|
1 |
|
$query = $parts[1]; |
62
|
|
|
} |
63
|
2 |
|
} elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) { |
64
|
2 |
|
$base = $uri . '?'; |
65
|
2 |
|
$query = ''; |
66
|
|
|
} else { |
67
|
|
|
$base = ''; |
68
|
|
|
$query = $uri; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
parse_str( $query, $qs ); |
72
|
2 |
|
$qs = yourls_urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string |
|
|
|
|
73
|
2 |
|
if ( is_array( func_get_arg( 0 ) ) ) { |
74
|
1 |
|
$kayvees = func_get_arg( 0 ); |
75
|
1 |
|
$qs = array_merge( $qs, $kayvees ); |
|
|
|
|
76
|
|
|
} else { |
77
|
2 |
|
$qs[func_get_arg( 0 )] = func_get_arg( 1 ); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
foreach ( (array) $qs as $k => $v ) { |
81
|
2 |
|
if ( $v === false ) |
82
|
|
|
unset( $qs[$k] ); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$ret = http_build_query( $qs ); |
86
|
2 |
|
$ret = trim( $ret, '?' ); |
87
|
2 |
|
$ret = preg_replace( '#=(&|$)#', '$1', $ret ); |
88
|
2 |
|
$ret = $protocol . $base . $ret . $frag; |
89
|
2 |
|
$ret = rtrim( $ret, '?' ); |
90
|
2 |
|
return $ret; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg() |
|
|
|
|
95
|
|
|
* |
96
|
|
|
*/ |
97
|
|
|
function yourls_urlencode_deep( $value ) { |
98
|
2 |
|
$value = is_array( $value ) ? array_map( 'yourls_urlencode_deep', $value ) : urlencode( $value ); |
|
|
|
|
99
|
2 |
|
return $value; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Remove arg from query. Opposite of yourls_add_query_arg. Stolen from WP. |
104
|
|
|
* |
105
|
|
|
* The result of this function call is a URL : it should be escaped before being printed as HTML |
106
|
|
|
* |
107
|
|
|
* @since 1.5 |
108
|
|
|
* @param string|array $key Query key or keys to remove. |
109
|
|
|
* @param bool|string $query Optional. When false uses the $_SERVER value. Default false. |
110
|
|
|
* @return string New URL query string. |
111
|
|
|
*/ |
112
|
|
|
function yourls_remove_query_arg( $key, $query = false ) { |
113
|
|
|
if ( is_array( $key ) ) { // removing multiple keys |
114
|
|
|
foreach ( $key as $k ) |
115
|
|
|
$query = yourls_add_query_arg( $k, false, $query ); |
116
|
|
|
return $query; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
return yourls_add_query_arg( $key, false, $query ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Converts keyword into short link (prepend with YOURLS base URL) or stat link (sho.rt/abc+) |
123
|
|
|
* |
124
|
|
|
* This function does not check for a valid keyword. |
125
|
|
|
* The resulting link is normalized to allow for IDN translation to UTF8 |
126
|
|
|
* |
127
|
|
|
* @param string $keyword Short URL keyword |
128
|
|
|
* @param bool $stats Optional, true to return a stat link (eg sho.rt/abc+) |
129
|
|
|
* @return string Short URL, or keyword stat URL |
130
|
|
|
*/ |
131
|
|
|
function yourls_link( $keyword = '', $stats = false ) { |
132
|
9 |
|
$keyword = yourls_sanitize_keyword($keyword); |
133
|
9 |
|
if( $stats === true ) { |
134
|
3 |
|
$keyword = $keyword . '+'; |
135
|
|
|
} |
136
|
9 |
|
$link = yourls_normalize_uri( yourls_get_yourls_site() . '/' . $keyword ); |
137
|
|
|
|
138
|
9 |
|
if( yourls_is_ssl() ) { |
139
|
|
|
$link = yourls_set_url_scheme( $link, 'https' ); |
140
|
|
|
} |
141
|
|
|
|
142
|
9 |
|
return yourls_apply_filter( 'yourls_link', $link, $keyword ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Converts keyword into stat link (prepend with YOURLS base URL, append +) |
147
|
|
|
* |
148
|
|
|
* This function does not make sure the keyword matches an actual short URL |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
|
|
function yourls_statlink( $keyword = '' ) { |
152
|
3 |
|
$link = yourls_link( $keyword, true ); |
153
|
3 |
|
return yourls_apply_filter( 'yourls_statlink', $link, $keyword ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return admin link, with SSL preference if applicable. |
158
|
|
|
* |
159
|
|
|
*/ |
160
|
|
|
function yourls_admin_url( $page = '' ) { |
161
|
3 |
|
$admin = yourls_get_yourls_site() . '/admin/' . $page; |
162
|
3 |
|
if( yourls_is_ssl() or yourls_needs_ssl() ) { |
|
|
|
|
163
|
|
|
$admin = yourls_set_url_scheme( $admin, 'https' ); |
164
|
|
|
} |
165
|
3 |
|
return yourls_apply_filter( 'admin_url', $admin, $page ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return YOURLS_SITE or URL under YOURLS setup, with SSL preference |
170
|
|
|
* |
171
|
|
|
*/ |
172
|
|
|
function yourls_site_url( $echo = true, $url = '' ) { |
173
|
1 |
|
$url = yourls_get_relative_url( $url ); |
174
|
1 |
|
$url = trim( yourls_get_yourls_site() . '/' . $url, '/' ); |
175
|
|
|
|
176
|
|
|
// Do not enforce (checking yourls_need_ssl() ) but check current usage so it won't force SSL on non-admin pages |
|
|
|
|
177
|
1 |
|
if( yourls_is_ssl() ) { |
178
|
|
|
$url = yourls_set_url_scheme( $url, 'https' ); |
179
|
|
|
} |
180
|
1 |
|
$url = yourls_apply_filter( 'site_url', $url ); |
181
|
1 |
|
if( $echo ) { |
182
|
1 |
|
echo $url; |
183
|
|
|
} |
184
|
1 |
|
return $url; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get YOURLS_SITE value, trimmed and filtered |
189
|
|
|
* |
190
|
|
|
* In addition of being filtered for plugins to hack this, this function is mostly here |
191
|
|
|
* to help people entering "sho.rt/" instead of "sho.rt" in their config |
192
|
|
|
* |
193
|
|
|
* @since 1.7.7 |
194
|
|
|
* @return string YOURLS_SITE, trimmed and filtered |
195
|
|
|
*/ |
196
|
|
|
function yourls_get_yourls_site() { |
197
|
66 |
|
return yourls_apply_filter('get_yourls_site', trim(YOURLS_SITE, '/')); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Change protocol of a URL to HTTPS if we are currently on HTTPS |
202
|
|
|
* |
203
|
|
|
* This function is used to avoid insert 'http://' images or scripts in a page when it's served through HTTPS, |
|
|
|
|
204
|
|
|
* to avoid "mixed content" errors. |
205
|
|
|
* So: |
206
|
|
|
* - if you are on http://sho.rt/, 'http://something' and 'https://something' are left untouched. |
207
|
|
|
* - if you are on https:/sho.rt/, 'http://something' is changed to 'https://something' |
208
|
|
|
* |
209
|
|
|
* So, arguably, this function is poorly named. It should be something like yourls_match_current_protocol_if_we_re_on_https |
|
|
|
|
210
|
|
|
* |
211
|
|
|
* @since 1.5.1 |
212
|
|
|
* @param string $url a URL |
213
|
|
|
* @param string $normal Optional, the standard scheme (defaults to 'http://') |
214
|
|
|
* @param string $ssl Optional, the SSL scheme (defaults to 'https://') |
215
|
|
|
* @return string the modified URL, if applicable |
216
|
|
|
*/ |
217
|
|
|
function yourls_match_current_protocol( $url, $normal = 'http://', $ssl = 'https://' ) { |
218
|
|
|
// we're only doing something if we're currently serving through SSL and the input URL begins with 'http://' or 'https://' |
|
|
|
|
219
|
21 |
|
if( yourls_is_ssl() && in_array( yourls_get_protocol($url), array('http://', 'https://') ) ) { |
220
|
6 |
|
$url = str_replace( $normal, $ssl, $url ); |
221
|
|
|
} |
222
|
|
|
|
223
|
21 |
|
return yourls_apply_filter( 'match_current_protocol', $url ); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Auto detect custom favicon in /user directory, fallback to YOURLS favicon, and echo/return its URL |
|
|
|
|
228
|
|
|
* |
229
|
|
|
* This function supersedes function yourls_favicon(), deprecated in 1.7.10, with a better naming. |
230
|
|
|
* |
231
|
|
|
* @since 1.7.10 |
232
|
|
|
* @param bool $echo true to echo, false to silently return |
233
|
|
|
* @return string favicon URL |
234
|
|
|
* |
235
|
|
|
*/ |
236
|
|
|
function yourls_get_yourls_favicon_url( $echo = true ) { |
237
|
1 |
|
static $favicon = null; |
238
|
|
|
|
239
|
1 |
|
if( $favicon !== null ) { |
240
|
|
|
if( $echo ) { |
241
|
|
|
echo $favicon; |
242
|
|
|
} |
243
|
|
|
return $favicon; |
244
|
|
|
} |
245
|
|
|
|
246
|
1 |
|
$custom = null; |
247
|
|
|
// search for favicon.(gif|ico|png|jpg|svg) |
|
|
|
|
248
|
1 |
|
foreach( array( 'gif', 'ico', 'png', 'jpg', 'svg' ) as $ext ) { |
249
|
1 |
|
if( file_exists( YOURLS_USERDIR. '/favicon.' . $ext ) ) { |
250
|
|
|
$custom = 'favicon.' . $ext; |
251
|
|
|
break; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
1 |
|
if( $custom ) { |
256
|
|
|
$favicon = yourls_site_url( false, YOURLS_USERURL . '/' . $custom ); |
257
|
|
|
} else { |
258
|
1 |
|
$favicon = yourls_site_url( false ) . '/images/favicon.gif'; |
259
|
|
|
} |
260
|
|
|
|
261
|
1 |
|
$favicon = yourls_apply_filter('get_favicon_url', $favicon); |
262
|
|
|
|
263
|
1 |
|
if( $echo ) { |
264
|
1 |
|
echo $favicon; |
265
|
|
|
} |
266
|
1 |
|
return $favicon; |
267
|
|
|
} |
268
|
|
|
|