]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - yourls-go.php
Add plain API format
[Github/YOURLS.git] / yourls-go.php
1 <?php
2 define( 'YOURLS_GO', true );
3 require_once( dirname( __FILE__ ) . '/includes/load-yourls.php' );
4
5 // Variables should be defined in yourls-loader.php, if not try GET request (old behavior of yourls-go.php)
6 if( !isset( $keyword ) && isset( $_GET['id'] ) )
7         $keyword = $_GET['id'];
8 $keyword = yourls_sanitize_string( $keyword );
9
10 // First possible exit:
11 if ( !isset( $keyword ) ) {
12         yourls_do_action( 'redirect_no_keyword' );
13         yourls_redirect( YOURLS_SITE, 301 );
14 }
15
16 // Get URL From Database
17 $url = yourls_get_keyword_longurl( $keyword );
18
19 // URL found
20 if( !empty( $url ) ) {
21         yourls_do_action( 'redirect_shorturl', $url, $keyword );
22
23         // Update click count in main table
24         $update_clicks = yourls_update_clicks( $keyword );
25
26         // Update detailed log for stats
27         $log_redirect = yourls_log_redirect( $keyword );
28         
29         yourls_redirect( $url, 301 );
30
31 // URL not found. Either reserved, or page, or doesn't exist
32 } else {
33
34         // Do we have a page?
35         if ( file_exists( YOURLS_PAGEDIR . "/$keyword.php" ) ) {
36                 yourls_page( $keyword );
37
38         // Either reserved id, or no such id
39         } else {
40                 yourls_do_action( 'redirect_keyword_not_found', $keyword );
41                 
42                 yourls_redirect( YOURLS_SITE, 302 ); // no 404 to tell browser this might change, and also to not pollute logs
43         }
44 }
45 exit();