From 65ae8af2fc353a26b12b835a305fbc934b83613a Mon Sep 17 00:00:00 2001 From: ozhozh Date: Mon, 8 Nov 2010 21:00:29 +0000 Subject: [PATCH] - Stat links comply to SSL preference. Fixes issue 559. - Request (should be) correctly handled on IIS. Fixes issue 558. - Fix possible SSL mismatch between stat page & YOURLS_SITE leading to failing in yourls-loader.php git-svn-id: http://yourls.googlecode.com/svn/trunk@552 12232710-3e20-11de-b438-597f59cd7555 --- includes/functions.php | 60 +++++++++++++++++++++++++++++++++++++++- includes/load-yourls.php | 3 ++ yourls-loader.php | 3 +- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 6d5ab96..a40a9d8 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -267,6 +267,8 @@ function yourls_table_add_row( $keyword, $url, $title = '', $ip, $clicks, $times $shorturl = YOURLS_SITE.'/'.$keyword; $statlink = $shorturl.'+'; + if( yourls_is_ssl() ) + $statlink = str_replace( 'http://', 'https://', $statlink ); if( $title ) { $display_link = "$display_title
$display_url"; @@ -1786,4 +1788,60 @@ function yourls_is_mobile_device() { // Check return str_replace( $mobiles, '', $current ) != $current; -} \ No newline at end of file +} + +// Get request in YOURLS base (eg in 'http://site.com/yourls/abcd' get 'abdc') +function yourls_get_request() { + // Cover all cases: YOURLS_SITE https or not * current URL https or not + $base = str_replace( array( 'https', 'http' ), '', YOURLS_SITE ); + $scheme = yourls_is_ssl() ? 'https' : 'http' ; + + // Extract the 'abdc' from the requested URL + $request = str_replace( + $scheme . $base.'/', + '', + $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] + ); + + return yourls_apply_filter( 'get_request', $request ); +} + +// Fix $_SERVER['REQUEST_URI'] variable for various setups. Stolen from WP. +function wp_fix_request_uri() { + + $default_server_values = array( + 'SERVER_SOFTWARE' => '', + 'REQUEST_URI' => '', + ); + $_SERVER = array_merge( $default_server_values, $_SERVER ); + + // Fix for IIS when running with PHP ISAPI + if ( empty( $_SERVER['REQUEST_URI'] ) || ( php_sapi_name() != 'cgi-fcgi' && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) ) { + + // IIS Mod-Rewrite + if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) { + $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL']; + } + // IIS Isapi_Rewrite + else if ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) { + $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; + } else { + // Use ORIG_PATH_INFO if there is no PATH_INFO + if ( !isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) ) + $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']; + + // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice) + if ( isset( $_SERVER['PATH_INFO'] ) ) { + if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] ) + $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO']; + else + $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO']; + } + + // Append the query string if it exists and isn't null + if ( ! empty( $_SERVER['QUERY_STRING'] ) ) { + $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; + } + } + } +} diff --git a/includes/load-yourls.php b/includes/load-yourls.php index 85a8ebf..cbca077 100644 --- a/includes/load-yourls.php +++ b/includes/load-yourls.php @@ -124,6 +124,9 @@ } } +// Fix REQUEST_URI for IIS +wp_fix_request_uri(); + // Create the YOURLS object $ydb that will contain everything we globally need global $ydb; yourls_db_connect(); diff --git a/yourls-loader.php b/yourls-loader.php index 8082252..1702a27 100644 --- a/yourls-loader.php +++ b/yourls-loader.php @@ -10,8 +10,7 @@ require_once( dirname(__FILE__).'/includes/load-yourls.php' ); // Get request in YOURLS base (eg in 'http://site.com/yourls/abcd' get 'abdc') -$scheme = ( isset($_SERVER["HTTPS"]) ? 'https' : 'http' ); -$request = str_replace( YOURLS_SITE.'/', '', $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); +$request = yourls_get_request(); /** * TODO: think about doing something with $_SERVER['QUERY_STRING']? -- 2.45.0