]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-loader.php
Add plain API format
[Github/YOURLS.git] / yourls-loader.php
1 <?php
2 // Handle inexistent root favicon requests and exit
3 if ( '/favicon.ico' == $_SERVER['REQUEST_URI'] ) {
4         header( 'Content-Type: image/gif' );
5         echo base64_decode( "R0lGODlhEAAQAJECAAAAzFZWzP///wAAACH5BAEAAAIALAAAAAAQABAAAAIplI+py+0PUQAgSGoNQFt0LWTVOE6GuX1H6onTVHaW2tEHnJ1YxPc+UwAAOw==" );
6         exit;
7 }
8
9 // Handle inexistent root robots.txt requests and exit
10 if ( '/robots.txt' == $_SERVER['REQUEST_URI'] ) {
11         header( 'Content-Type: text/plain; charset=utf-8' );
12         echo "User-agent: *\n";
13         echo "Disallow:\n";
14         exit;
15 }
16
17 // Start YOURLS
18 require_once( dirname( __FILE__ ) . '/includes/load-yourls.php' );
19
20 // Get request in YOURLS base (eg in 'http://sho.rt/yourls/abcd' get 'abdc')
21 $request = yourls_get_request();
22
23 // Make valid regexp pattern from authorized charset in keywords
24 $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );
25
26 // Now load required template and exit
27
28 yourls_do_action( 'pre_load_template', $request );
29
30 // At this point, $request is not sanitized. Sanitize in loaded template.
31
32 // Redirection:
33 if( preg_match( "@^([$pattern]+)/?$@", $request, $matches ) ) {
34         $keyword = isset( $matches[1] ) ? $matches[1] : '';
35         $keyword = yourls_sanitize_keyword( $keyword );
36         yourls_do_action( 'load_template_go', $keyword );
37         require_once( YOURLS_ABSPATH.'/yourls-go.php' );
38         exit;
39 }
40
41 // Stats:
42 if( preg_match( "@^([$pattern]+)\+(all)?/?$@", $request, $matches ) ) {
43         $keyword = isset( $matches[1] ) ? $matches[1] : '';
44         $keyword = yourls_sanitize_keyword( $keyword );
45         $aggregate = isset( $matches[2] ) ? (bool)$matches[2] && yourls_allow_duplicate_longurls() : false;
46         yourls_do_action( 'load_template_infos', $keyword );
47         require_once( YOURLS_ABSPATH.'/yourls-infos.php' );
48         exit;
49 }
50
51 // Prefix-n-Shorten sends to bookmarklet (doesn't work on Windows)
52 if( preg_match( "@^[a-zA-Z]+://.+@", $request, $matches ) ) {
53         $url = yourls_sanitize_url( $matches[0] );
54         if( $parse = yourls_get_protocol_slashes_and_rest( $url, array( 'up', 'us', 'ur' ) ) ) {
55                 yourls_do_action( 'load_template_redirect_admin', $url );
56                 $parse = array_map( 'rawurlencode', $parse );
57                 // Redirect to /admin/index.php?up=<url protocol>&us=<url slashes>&ur=<url rest>
58                 yourls_redirect( yourls_add_query_arg( $parse , yourls_admin_url( 'index.php' ) ), 302 );
59                 exit;
60         }
61 }
62
63 // Past this point this is a request the loader could not understand
64 yourls_do_action( 'loader_failed', $request );
65 yourls_redirect( YOURLS_SITE, 302 );
66 exit;