]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-loader.php
Tweak internal redirects: use 302 instead of 301 to help prevent browser cache proble...
[Github/YOURLS.git] / yourls-loader.php
1 <?php\r
2 // Handle inexistant root favicon requests and exit\r
3 if ( '/favicon.ico' == $_SERVER['REQUEST_URI'] ) {\r
4         header('Content-Type: image/gif');\r
5         echo base64_decode("R0lGODlhEAAQAJECAAAAzFZWzP///wAAACH5BAEAAAIALAAAAAAQABAAAAIplI+py+0PUQAgSGoNQFt0LWTVOE6GuX1H6onTVHaW2tEHnJ1YxPc+UwAAOw==");\r
6         exit;\r
7 }\r
8 \r
9 // Start YOURLS\r
10 require_once( dirname(__FILE__).'/includes/load-yourls.php' );\r
11 \r
12 // Get request in YOURLS base (eg in 'http://site.com/yourls/abcd' get 'abdc')\r
13 $scheme = ( isset($_SERVER["HTTPS"]) ? 'https' : 'http' );\r
14 $request = str_replace( YOURLS_SITE.'/', '', $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );\r
15 \r
16 /**\r
17  * TODO: think about doing something with $_SERVER['QUERY_STRING']?\r
18  * Like sho.rt/keyword?something\r
19  * if so:\r
20  *     - deal with logout requests early\r
21  *     - handle differently short urls (sho.rt/keyword?something) and prefix-n-shorten (sho.rt/http://bleh/?s=1)\r
22  */\r
23 \r
24 // Make valid regexp pattern from authorized charset in keywords\r
25 $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );\r
26 \r
27 // Now load required template and exit\r
28 \r
29 yourls_do_action( 'pre_load_template', $request );\r
30 \r
31 // At this point, $request is not sanitized. Sanitize in loaded template.\r
32 \r
33 // Redirection:\r
34 if( preg_match( "@^([$pattern]+)/?$@", $request, $matches ) ) {\r
35         $keyword   = isset( $matches[1] ) ? $matches[1] : '';\r
36         $keyword = yourls_sanitize_keyword( $keyword );\r
37         yourls_do_action( 'load_template_go', $keyword );\r
38         include( YOURLS_ABSPATH.'/yourls-go.php' );\r
39         exit;\r
40 }\r
41 \r
42 // Stats:\r
43 if( preg_match( "@^([$pattern]+)\+(all)?/?$@", $request, $matches ) ) {\r
44         $keyword   = isset( $matches[1] ) ? $matches[1] : '';\r
45         $keyword = yourls_sanitize_keyword( $keyword );\r
46         $aggregate = isset( $matches[2] ) ? (bool)$matches[2] && yourls_allow_duplicate_longurls() : false;\r
47         yourls_do_action( 'load_template_infos', $keyword );\r
48         include( YOURLS_ABSPATH.'/yourls-infos.php' );\r
49         exit;\r
50 }\r
51 \r
52 // Prefix-n-Shorten sends to bookmarklet (doesn't work on Windows)\r
53 if( preg_match( "@^[a-zA-Z]+://.+@", $request, $matches ) ) {\r
54         $url = yourls_sanitize_url( $matches[0] );\r
55         yourls_do_action( 'load_template_redirect_admin', $url );\r
56         yourls_redirect( yourls_admin_url('index.php').'?u='.rawurlencode( $url ), 302 );\r
57         exit;\r
58 }\r
59 \r
60 // Past this point this is a request the loader could not understand\r
61 yourls_do_action( 'loader_failed', $request );\r
62 yourls_redirect( YOURLS_SITE, 302 );\r
63 exit;