Mirror of the main YOURLS application https://yourls.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.8KB

  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. // Handle inexistent root robots.txt requests and exit
  9. if ( '/robots.txt' == $_SERVER['REQUEST_URI'] ) {
  10. header( 'Content-Type: text/plain; charset=utf-8' );
  11. echo "User-agent: *\n";
  12. echo "Disallow:\n";
  13. exit;
  14. }
  15. // Load YOURLS
  16. require_once __DIR__ . '/includes/load-yourls.php';
  17. // Get request in YOURLS base (eg in 'http://sho.rt/yourls/abcd' get 'abdc')
  18. // At this point, $request is NOT sanitized.
  19. $request = yourls_get_request();
  20. // Now load required template and exit
  21. yourls_do_action( 'pre_load_template', $request );
  22. // Let's look at the request : what we want to catch here is "anything", or "anything+" / "anything+all" (stat page)
  23. preg_match( "@^(.+?)(\+(all)?)?/?$@", $request, $matches );
  24. $keyword = isset($matches[1]) ? $matches[1] : null; // 'anything' whatever the request is (keyword, bookmarklet URL...)
  25. $stats = isset($matches[2]) ? $matches[2] : null; // null, or '+' if request is 'anything+', '+all' if request is 'anything+all'
  26. $stats_all = isset($matches[3]) ? $matches[3] : null; // null, or 'all' if request is 'anything+all'
  27. // if request has a scheme (eg scheme://uri) : "Prefix-n-Shorten" sends to bookmarklet (doesn't work on Windows)
  28. if ( yourls_get_protocol($keyword) ) {
  29. $url = yourls_sanitize_url_safe($keyword);
  30. $parse = yourls_get_protocol_slashes_and_rest( $url, [ 'up', 'us', 'ur' ] );
  31. yourls_do_action( 'load_template_redirect_admin', $url );
  32. $parse = array_map( 'rawurlencode', $parse );
  33. yourls_do_action( 'pre_redirect_bookmarklet', $url );
  34. // Redirect to /admin/index.php?up=<url protocol>&us=<url slashes>&ur=<url rest>
  35. yourls_redirect( yourls_add_query_arg( $parse , yourls_admin_url( 'index.php' ) ), 302 );
  36. exit;
  37. }
  38. // if request is an existing short URL keyword ('abc') or stat page ('abc+') or an existing page :
  39. if ( yourls_keyword_is_taken($keyword) or yourls_is_page($keyword) ) {
  40. // we have a short URL or a page
  41. if( $keyword && !$stats ) {
  42. yourls_do_action( 'load_template_go', $keyword );
  43. require_once( YOURLS_ABSPATH.'/yourls-go.php' );
  44. exit;
  45. }
  46. // we have a stat page
  47. if( $keyword && $stats ) {
  48. $aggregate = $stats_all && yourls_allow_duplicate_longurls();
  49. yourls_do_action( 'load_template_infos', $keyword );
  50. require_once( YOURLS_ABSPATH.'/yourls-infos.php' );
  51. exit;
  52. }
  53. }
  54. // Past this point this is a request the loader could not understand : not a valid shorturl, not a bookmarklet
  55. yourls_do_action( 'redirect_keyword_not_found', $keyword );
  56. yourls_do_action( 'loader_failed', $request );
  57. yourls_redirect( YOURLS_SITE, 302 );
  58. exit;