Welcome to mirror list, hosted at ThFree Co, Russian Federation.

functions-links.php « includes - github.com/YOURLS/YOURLS.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4114e73eb1c7257b51c411afdedec1976ac9a1db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/*
 * Functions relative to how YOURLS handle some links
 *
 */

/**
 * Add a query var to a URL and return URL. Completely stolen from WP.
 *
 * Works with one of these parameter patterns:
 *     array( 'var' => 'value' )
 *     array( 'var' => 'value' ), $url
 *     'var', 'value'
 *     'var', 'value', $url
 * If $url omitted, uses $_SERVER['REQUEST_URI']
 *
 * The result of this function call is a URL : it should be escaped before being printed as HTML
 *
 * @since 1.5
 * @param string|array $param1 Either newkey or an associative_array.
 * @param string       $param2 Either newvalue or oldquery or URI.
 * @param string       $param3 Optional. Old query or URI.
 * @return string New URL query string.
 */
function yourls_add_query_arg() {
    $ret = '';
    if ( is_array( func_get_arg(0) ) ) {
        if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
            $uri = $_SERVER['REQUEST_URI'];
        else
            $uri = @func_get_arg( 1 );
    } else {
        if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
            $uri = $_SERVER['REQUEST_URI'];
        else
            $uri = @func_get_arg( 2 );
    }

    $uri = str_replace( '&amp;', '&', $uri );


    if ( $frag = strstr( $uri, '#' ) )
        $uri = substr( $uri, 0, -strlen( $frag ) );
    else
        $frag = '';

    if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
        $protocol = $matches[0];
        $uri = substr( $uri, strlen( $protocol ) );
    } else {
        $protocol = '';
    }

    if ( strpos( $uri, '?' ) !== false ) {
        $parts = explode( '?', $uri, 2 );
        if ( 1 == count( $parts ) ) {
            $base = '?';
            $query = $parts[0];
        } else {
            $base = $parts[0] . '?';
            $query = $parts[1];
        }
    } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
        $base = $uri . '?';
        $query = '';
    } else {
        $base = '';
        $query = $uri;
    }

    parse_str( $query, $qs );
    $qs = yourls_urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
    if ( is_array( func_get_arg( 0 ) ) ) {
        $kayvees = func_get_arg( 0 );
        $qs = array_merge( $qs, $kayvees );
    } else {
        $qs[func_get_arg( 0 )] = func_get_arg( 1 );
    }

    foreach ( (array) $qs as $k => $v ) {
        if ( $v === false )
            unset( $qs[$k] );
    }

    $ret = http_build_query( $qs );
    $ret = trim( $ret, '?' );
    $ret = preg_replace( '#=(&|$)#', '$1', $ret );
    $ret = $protocol . $base . $ret . $frag;
    $ret = rtrim( $ret, '?' );
    return $ret;
}

/**
 * Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg()
 *
 * @param array|string $value The array or string to be encoded.
 * @return array|string
 */
function yourls_urlencode_deep( $value ) {
    $value = is_array( $value ) ? array_map( 'yourls_urlencode_deep', $value ) : urlencode( $value );
    return $value;
}

/**
 * Remove arg from query. Opposite of yourls_add_query_arg. Stolen from WP.
 *
 * The result of this function call is a URL : it should be escaped before being printed as HTML
 *
 * @since 1.5
 * @param string|array $key   Query key or keys to remove.
 * @param bool|string  $query Optional. When false uses the $_SERVER value. Default false.
 * @return string New URL query string.
 */
function yourls_remove_query_arg( $key, $query = false ) {
    if ( is_array( $key ) ) { // removing multiple keys
        foreach ( $key as $k )
            $query = yourls_add_query_arg( $k, false, $query );
        return $query;
    }
    return yourls_add_query_arg( $key, false, $query );
}

/**
 * Converts keyword into short link (prepend with YOURLS base URL) or stat link (sho.rt/abc+)
 *
 * This function does not check for a valid keyword.
 * The resulting link is normalized to allow for IDN translation to UTF8
 *
 * @param  string $keyword  Short URL keyword
 * @param  bool   $stats    Optional, true to return a stat link (eg sho.rt/abc+)
 * @return string           Short URL, or keyword stat URL
 */
function yourls_link( $keyword = '', $stats = false ) {
    $keyword = yourls_sanitize_keyword($keyword);
    if( $stats  === true ) {
        $keyword = $keyword . '+';
    }
    $link    = yourls_normalize_uri( yourls_get_yourls_site() . '/' . $keyword );

    if( yourls_is_ssl() ) {
        $link = yourls_set_url_scheme( $link, 'https' );
    }

    return yourls_apply_filter( 'yourls_link', $link, $keyword );
}

/**
 * Converts keyword into stat link (prepend with YOURLS base URL, append +)
 *
 * This function does not make sure the keyword matches an actual short URL
 *
 * @param  string $keyword  Short URL keyword
 * @return string           Short URL stat link
 */
function yourls_statlink( $keyword = '' ) {
    $link = yourls_link( $keyword, true );
    return yourls_apply_filter( 'yourls_statlink', $link, $keyword );
}

/**
 * Return admin link, with SSL preference if applicable.
 *
 * @param string $page  Page name, eg "index.php"
 * @return string
 */
function yourls_admin_url( $page = '' ) {
    $admin = yourls_get_yourls_site() . '/admin/' . $page;
    if( yourls_is_ssl() or yourls_needs_ssl() ) {
        $admin = yourls_set_url_scheme( $admin, 'https' );
    }
    return yourls_apply_filter( 'admin_url', $admin, $page );
}

/**
 * Return YOURLS_SITE or URL under YOURLS setup, with SSL preference
 *
 * @param bool $echo  Echo if true, or return if false
 * @param $url
 * @return string
 */
function yourls_site_url($echo = true, $url = '' ) {
    $url = yourls_get_relative_url( $url );
    $url = trim( yourls_get_yourls_site() . '/' . $url, '/' );

    // Do not enforce (checking yourls_need_ssl() ) but check current usage so it won't force SSL on non-admin pages
    if( yourls_is_ssl() ) {
        $url = yourls_set_url_scheme( $url, 'https' );
    }
    $url = yourls_apply_filter( 'site_url', $url );
    if( $echo ) {
        echo $url;
    }
    return $url;
}

/**
 *  Get YOURLS_SITE value, trimmed and filtered
 *
 *  In addition of being filtered for plugins to hack this, this function is mostly here
 *  to help people entering "sho.rt/" instead of "sho.rt" in their config
 *
 *  @since 1.7.7
 *  @return string  YOURLS_SITE, trimmed and filtered
 */
function yourls_get_yourls_site() {
    return yourls_apply_filter('get_yourls_site', trim(YOURLS_SITE, '/'));
}

/**
 * Change protocol of a URL to HTTPS if we are currently on HTTPS
 *
 * This function is used to avoid insert 'http://' images or scripts in a page when it's served through HTTPS,
 * to avoid "mixed content" errors.
 * So:
 *   - if you are on http://sho.rt/, 'http://something' and 'https://something' are left untouched.
 *   - if you are on https:/sho.rt/, 'http://something' is changed to 'https://something'
 *
 * So, arguably, this function is poorly named. It should be something like yourls_match_current_protocol_if_we_re_on_https
 *
 * @since 1.5.1
 * @param string $url        a URL
 * @param string $normal     Optional, the standard scheme (defaults to 'http://')
 * @param string $ssl        Optional, the SSL scheme (defaults to 'https://')
 * @return string            the modified URL, if applicable
 */
function yourls_match_current_protocol( $url, $normal = 'http://', $ssl = 'https://' ) {
    // we're only doing something if we're currently serving through SSL and the input URL begins with 'http://' or 'https://'
    if( yourls_is_ssl() && in_array( yourls_get_protocol($url), array('http://', 'https://') ) ) {
        $url = str_replace( $normal, $ssl, $url );
    }

    return yourls_apply_filter( 'match_current_protocol', $url );
}

/**
 * Auto detect custom favicon in /user directory, fallback to YOURLS favicon, and echo/return its URL
 *
 * This function supersedes function yourls_favicon(), deprecated in 1.7.10, with a better naming.
 *
 * @since 1.7.10
 * @param  bool $echo   true to echo, false to silently return
 * @return string       favicon URL
 *
 */
function yourls_get_yourls_favicon_url( $echo = true ) {
    static $favicon = null;

    if( $favicon !== null ) {
        if( $echo ) {
            echo $favicon;
        }
        return $favicon;
    }

    $custom = null;
    // search for favicon.(gif|ico|png|jpg|svg)
    foreach( array( 'gif', 'ico', 'png', 'jpg', 'svg' ) as $ext ) {
        if( file_exists( YOURLS_USERDIR. '/favicon.' . $ext ) ) {
            $custom = 'favicon.' . $ext;
            break;
        }
    }

    if( $custom ) {
        $favicon = yourls_site_url( false, YOURLS_USERURL . '/' . $custom );
    } else {
        $favicon = yourls_site_url( false ) . '/images/favicon.svg';
    }

    $favicon = yourls_apply_filter('get_favicon_url', $favicon);

    if( $echo ) {
        echo $favicon;
    }
    return $favicon;
}