query("DELETE FROM `$table` WHERE `keyword` = '$keyword';");
yourls_do_action( 'delete_link', $keyword, $delete );
return $delete;
}
// SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting
function yourls_insert_link_in_db( $url, $keyword, $title = '' ) {
global $ydb;
$url = addslashes( yourls_sanitize_url( $url ) );
$keyword = addslashes( yourls_sanitize_keyword( $keyword ) );
$title = addslashes( yourls_sanitize_title( $title ) );
$table = YOURLS_DB_TABLE_URL;
$timestamp = date('Y-m-d H:i:s');
$ip = yourls_get_IP();
$insert = $ydb->query("INSERT INTO `$table` (`keyword`, `url`, `title`, `timestamp`, `ip`, `clicks`) VALUES('$keyword', '$url', '$title', '$timestamp', '$ip', 0);");
yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $title, $timestamp, $ip );
return (bool)$insert;
}
// Check if a URL already exists in the DB. Return NULL (doesn't exist) or an object with URL informations.
function yourls_url_exists( $url ) {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_url_exists', false, $url );
if ( false !== $pre )
return $pre;
global $ydb;
$table = YOURLS_DB_TABLE_URL;
$strip_url = stripslashes($url);
$url_exists = $ydb->get_row("SELECT * FROM `$table` WHERE `url` = '".$strip_url."';");
return yourls_apply_filter( 'url_exists', $url_exists, $url );
}
// Add a new link in the DB, either with custom keyword, or find one
function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
global $ydb;
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_add_new_link', false, $url, $keyword, $title );
if ( false !== $pre )
return $pre;
if ( !$url || $url == 'http://' || $url == 'https://' ) {
$return['status'] = 'fail';
$return['code'] = 'error:nourl';
$return['message'] = 'Missing URL input';
$return['errorCode'] = '400';
return yourls_apply_filter( 'add_new_link_fail_nourl', $return, $url, $keyword, $title );
}
// Prevent DB flood
$ip = yourls_get_IP();
yourls_check_IP_flood( $ip );
// Prevent internal redirection loops: cannot shorten a shortened URL
$url = yourls_escape( yourls_sanitize_url($url) );
if( preg_match( '!^'.YOURLS_SITE.'/!', $url ) ) {
if( yourls_is_shorturl( $url ) ) {
$return['status'] = 'fail';
$return['code'] = 'error:noloop';
$return['message'] = 'URL is a short URL';
$return['errorCode'] = '400';
return yourls_apply_filter( 'add_new_link_fail_noloop', $return, $url, $keyword, $title );
}
}
yourls_do_action( 'pre_add_new_link', $url, $keyword, $title );
$strip_url = stripslashes($url);
$return = array();
// duplicates allowed or new URL => store it
if( yourls_allow_duplicate_longurls() || !yourls_url_exists( $url ) ) {
if( isset( $title ) && !empty( $title ) ) {
$title = yourls_sanitize_title( $title );
} else {
$title = yourls_get_remote_title( $url );
}
$title = yourls_apply_filter( 'add_new_title', $title, $url, $keyword );
// Custom keyword provided
if ( $keyword ) {
yourls_do_action( 'add_new_link_custom_keyword', $url, $keyword, $title );
$keyword = yourls_escape( yourls_sanitize_string($keyword) );
$keyword = yourls_apply_filter( 'custom_keyword', $keyword, $url, $title );
if ( !yourls_keyword_is_free($keyword) ) {
// This shorturl either reserved or taken already
$return['status'] = 'fail';
$return['code'] = 'error:keyword';
$return['message'] = 'Short URL '.$keyword.' already exists in database or is reserved';
} else {
// all clear, store !
yourls_insert_link_in_db( $url, $keyword, $title );
$return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => date('Y-m-d H:i:s'), 'ip' => $ip );
$return['status'] = 'success';
$return['message'] = yourls_trim_long_string( $strip_url ).' added to database';
$return['title'] = $title;
$return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
$return['shorturl'] = YOURLS_SITE .'/'. $keyword;
}
// Create random keyword
} else {
yourls_do_action( 'add_new_link_create_keyword', $url, $keyword, $title );
$timestamp = date('Y-m-d H:i:s');
$id = yourls_get_next_decimal();
$ok = false;
do {
$keyword = yourls_int2string( $id );
$keyword = yourls_apply_filter( 'random_keyword', $keyword, $url, $title );
$free = yourls_keyword_is_free($keyword);
$add_url = @yourls_insert_link_in_db( $url, $keyword, $title );
$ok = ($free && $add_url);
if ( $ok === false && $add_url === 1 ) {
// we stored something, but shouldn't have (ie reserved id)
$delete = yourls_delete_link_by_keyword( $keyword );
$return['extra_info'] .= '(deleted '.$keyword.')';
} else {
// everything ok, populate needed vars
$return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip );
$return['status'] = 'success';
$return['message'] = yourls_trim_long_string( $strip_url ).' added to database';
$return['title'] = $title;
$return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
$return['shorturl'] = YOURLS_SITE .'/'. $keyword;
}
$id++;
} while (!$ok);
@yourls_update_next_decimal($id);
}
// URL was already stored
} else {
yourls_do_action( 'add_new_link_already_stored', $url, $keyword, $title );
$return['status'] = 'fail';
$return['code'] = 'error:url';
$return['url'] = array( 'keyword' => $url_exists->keyword, 'url' => $strip_url, 'title' => $url_exists->title, 'date' => $url_exists->timestamp, 'ip' => $url_exists->ip, 'clicks' => $url_exists->clicks );
$return['message'] = yourls_trim_long_string( $strip_url ).' already exists in database';
$return['title'] = $url_exists->title;
$return['shorturl'] = YOURLS_SITE .'/'. $url_exists->keyword;
}
yourls_do_action( 'post_add_new_link', $url, $keyword, $title );
$return['statusCode'] = 200; // regardless of result, this is still a valid request
return yourls_apply_filter( 'add_new_link', $return, $url, $keyword, $title );
}
// Edit a link
function yourls_edit_link( $url, $keyword, $newkeyword='', $title='' ) {
global $ydb;
$table = YOURLS_DB_TABLE_URL;
$url = yourls_escape(yourls_sanitize_url($url));
$keyword = yourls_escape(yourls_sanitize_string( $keyword ));
$title = yourls_escape(yourls_sanitize_title( $title ));
$newkeyword = yourls_escape(yourls_sanitize_string( $newkeyword ));
$strip_url = stripslashes($url);
$strip_title = stripslashes($title);
$old_url = $ydb->get_var("SELECT `url` FROM `$table` WHERE `keyword` = '$keyword';");
// Check if new URL is not here already
if ( $old_url != $url && !yourls_allow_duplicate_longurls() ) {
$new_url_already_there = intval($ydb->get_var("SELECT COUNT(keyword) FROM `$table` WHERE `url` = '$strip_url';"));
} else {
$new_url_already_there = false;
}
// Check if the new keyword is not here already
if ( $newkeyword != $keyword ) {
$keyword_is_ok = yourls_keyword_is_free( $newkeyword );
} else {
$keyword_is_ok = true;
}
yourls_do_action( 'pre_edit_link', $url, $keyword, $newkeyword, $new_url_already_there, $keyword_is_ok );
// All clear, update
if ( ( !$new_url_already_there || yourls_allow_duplicate_longurls() ) && $keyword_is_ok ) {
$update_url = $ydb->query("UPDATE `$table` SET `url` = '$url', `keyword` = '$newkeyword', `title` = '$title' WHERE `keyword` = '$keyword';");
if( $update_url ) {
$return['url'] = array( 'keyword' => $newkeyword, 'shorturl' => YOURLS_SITE.'/'.$newkeyword, 'url' => $strip_url, 'display_url' => yourls_trim_long_string( $strip_url ), 'title' => $strip_title, 'display_title' => yourls_trim_long_string( $strip_title ) );
$return['status'] = 'success';
$return['message'] = 'Link updated in database';
} else {
$return['status'] = 'fail';
$return['message'] = 'Error updating '. yourls_trim_long_string( $strip_url ).' (Short URL: '.$keyword.') to database';
}
// Nope
} else {
$return['status'] = 'fail';
$return['message'] = 'URL or keyword already exists in database';
}
return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title, $new_url_already_there, $keyword_is_ok );
}
// Update a title link (no checks for duplicates etc..)
function yourls_edit_link_title( $keyword, $title ) {
global $ydb;
$keyword = yourls_escape( yourls_sanitize_keyword( $keyword ) );
$title = yourls_escape( yourls_sanitize_title( $title ) );
$table = YOURLS_DB_TABLE_URL;
$update = $ydb->query("UPDATE `$table` SET `title` = '$title' WHERE `keyword` = '$keyword';");
return $update;
}
// Check if keyword id is free (ie not already taken, and not reserved). Return bool.
function yourls_keyword_is_free( $keyword ) {
$free = true;
if ( yourls_keyword_is_reserved( $keyword ) or yourls_keyword_is_taken( $keyword ) )
$free = false;
return yourls_apply_filter( 'keyword_is_free', $free, $keyword );
}
// Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
function yourls_keyword_is_taken( $keyword ) {
global $ydb;
$keyword = yourls_sanitize_keyword( $keyword );
$taken = false;
$table = YOURLS_DB_TABLE_URL;
$already_exists = $ydb->get_var("SELECT COUNT(`keyword`) FROM `$table` WHERE `keyword` = '$keyword';");
if ( $already_exists )
$taken = true;
return yourls_apply_filter( 'keyword_is_taken', $taken, $keyword );
}
// Connect to DB
function yourls_db_connect() {
global $ydb;
if (!defined('YOURLS_DB_USER')
or !defined('YOURLS_DB_PASS')
or !defined('YOURLS_DB_NAME')
or !defined('YOURLS_DB_HOST')
or !class_exists('ezSQL_mysql')
) yourls_die ('DB config missing, or could not find DB class', 'Fatal error', 503);
// Are we standalone or in the WordPress environment?
if ( class_exists('wpdb') ) {
$ydb = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
} else {
$ydb = new ezSQL_mysql(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
}
if ( $ydb->last_error )
yourls_die( $ydb->last_error, 'Fatal error', 503 );
if ( defined('YOURLS_DEBUG') && YOURLS_DEBUG === true )
$ydb->show_errors = true;
return $ydb;
}
// Return XML output.
function yourls_xml_encode($array) {
require_once(YOURLS_INC.'/functions-xml.php');
$converter= new yourls_array2xml;
return $converter->array2xml($array);
}
// Return array of all informations associated with keyword. Returns false if keyword not found. Set optional $use_cache to false to force fetching from DB
function yourls_get_keyword_infos( $keyword, $use_cache = true ) {
global $ydb;
$keyword = yourls_sanitize_string( $keyword );
yourls_do_action( 'pre_get_keyword', $keyword, $use_cache );
if( isset( $ydb->infos[$keyword] ) && $use_cache == true ) {
return yourls_apply_filter( 'get_keyword_infos', $ydb->infos[$keyword], $keyword );
}
yourls_do_action( 'get_keyword_not_cached', $keyword );
$table = YOURLS_DB_TABLE_URL;
$infos = $ydb->get_row("SELECT * FROM `$table` WHERE `keyword` = '$keyword'");
if( $infos ) {
$infos = (array)$infos;
$ydb->infos[$keyword] = $infos;
} else {
$ydb->infos[$keyword] = false;
}
return yourls_apply_filter( 'get_keyword_infos', $ydb->infos[$keyword], $keyword );
}
// Return (string) selected information associated with a keyword. Optional $notfound = string default message if nothing found
function yourls_get_keyword_info( $keyword, $field, $notfound = false ) {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_get_keyword_info', false, $keyword, $field, $notfound );
if ( false !== $pre )
return $pre;
$keyword = yourls_sanitize_string( $keyword );
$infos = yourls_get_keyword_infos( $keyword );
$return = $notfound;
if ( isset($infos[$field]) && $infos[$field] !== false )
$return = $infos[$field];
return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound );
}
// Return title associated with keyword. Optional $notfound = string default message if nothing found
function yourls_get_keyword_title( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'title', $notfound );
}
// Return long URL associated with keyword. Optional $notfound = string default message if nothing found
function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'url', $notfound );
}
// Return number of clicks on a keyword. Optional $notfound = string default message if nothing found
function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'clicks', $notfound );
}
// Return IP that added a keyword. Optional $notfound = string default message if nothing found
function yourls_get_keyword_IP( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'ip', $notfound );
}
// Return timestamp associated with a keyword. Optional $notfound = string default message if nothing found
function yourls_get_keyword_timestamp( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'timestamp', $notfound );
}
// Update click count on a short URL. Return 0/1 for error/success.
function yourls_update_clicks( $keyword, $clicks = false ) {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_update_clicks', false, $keyword, $clicks );
if ( false !== $pre )
return $pre;
global $ydb;
$keyword = yourls_sanitize_string( $keyword );
$table = YOURLS_DB_TABLE_URL;
if ( $clicks !== false && is_int( $clicks ) && $clicks >= 0 )
$update = $ydb->query("UPDATE `$table` SET `clicks` = $clicks WHERE `keyword` = '$keyword'");
else
$update = $ydb->query("UPDATE `$table` SET `clicks` = clicks + 1 WHERE `keyword` = '$keyword'");
yourls_do_action( 'update_clicks', $keyword, $update, $clicks );
return $update;
}
// Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
global $ydb;
switch( $filter ) {
case 'bottom':
$sort_by = 'clicks';
$sort_order = 'asc';
break;
case 'last':
$sort_by = 'timestamp';
$sort_order = 'desc';
break;
case 'rand':
case 'random':
$sort_by = 'RAND()';
$sort_order = '';
break;
case 'top':
default:
$sort_by = 'clicks';
$sort_order = 'desc';
break;
}
// Fetch links
$limit = intval( $limit );
$start = intval( $start );
if ( $limit > 0 ) {
$table_url = YOURLS_DB_TABLE_URL;
$results = $ydb->get_results("SELECT * FROM `$table_url` WHERE 1=1 ORDER BY `$sort_by` $sort_order LIMIT $start, $limit;");
$return = array();
$i = 1;
foreach ( (array)$results as $res) {
$return['links']['link_'.$i++] = array(
'shorturl' => YOURLS_SITE .'/'. $res->keyword,
'url' => $res->url,
'title' => $res->title,
'timestamp'=> $res->timestamp,
'ip' => $res->ip,
'clicks' => $res->clicks,
);
}
}
$return['stats'] = yourls_get_db_stats();
$return['statusCode'] = 200;
return yourls_apply_filter( 'get_stats', $return, $filter, $limit, $start );
}
// Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
function yourls_get_link_stats( $shorturl ) {
global $ydb;
$table_url = YOURLS_DB_TABLE_URL;
$res = $ydb->get_row("SELECT * FROM `$table_url` WHERE keyword = '$shorturl';");
$return = array();
if( !$res ) {
// non existent link
$return = array(
'statusCode' => 404,
'message' => 'Error: short URL not found',
);
} else {
$return = array(
'statusCode' => 200,
'message' => 'success',
'link' => array(
'shorturl' => YOURLS_SITE .'/'. $res->keyword,
'url' => $res->url,
'title' => $res->title,
'timestamp'=> $res->timestamp,
'ip' => $res->ip,
'clicks' => $res->clicks,
)
);
}
return yourls_apply_filter( 'get_link_stats', $return, $shorturl );
}
// Return array for API stat requests
function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
$return = yourls_get_stats( $filter, $limit, $start );
$return['simple'] = 'Need either XML or JSON format for stats';
$return['message'] = 'success';
return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start );
}
// Return array for counts of shorturls and clicks
function yourls_api_db_stats() {
$return = yourls_get_db_stats();
$return['simple'] = 'Need either XML or JSON format for stats';
$return['message'] = 'success';
return yourls_apply_filter( 'api_db_stats', $return );
}
// Return array for API stat requests
function yourls_api_url_stats($shorturl) {
$keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
$keyword = yourls_sanitize_string( $keyword );
$return = yourls_get_link_stats( $keyword );
$return['simple'] = 'Need either XML or JSON format for stats';
return yourls_apply_filter( 'api_url_stats', $return, $shorturl );
}
// Expand short url to long url
function yourls_api_expand( $shorturl ) {
$keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
$keyword = yourls_sanitize_string( $keyword );
$longurl = yourls_get_keyword_longurl( $keyword );
if( $longurl ) {
$return = array(
'keyword' => $keyword,
'shorturl' => YOURLS_SITE . "/$keyword",
'longurl' => $longurl,
'simple' => $longurl,
'message' => 'success',
'statusCode' => 200,
);
} else {
$return = array(
'keyword' => $keyword,
'simple' => 'not found',
'message' => 'Error: short URL not found',
'errorCode' => 404,
);
}
return yourls_apply_filter( 'api_expand', $return, $shorturl );
}
// Get total number of URLs and sum of clicks. Input: optional "AND WHERE" clause. Returns array
function yourls_get_db_stats( $where = '' ) {
global $ydb;
$table_url = YOURLS_DB_TABLE_URL;
$totals = $ydb->get_row("SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM `$table_url` WHERE 1=1 $where");
$return = array( 'total_links' => $totals->count, 'total_clicks' => $totals->sum );
return yourls_apply_filter( 'get_db_stats', $return, $where );
}
// Return API result. Dies after this
function yourls_api_output( $mode, $return ) {
if( isset( $return['simple'] ) ) {
$simple = $return['simple'];
unset( $return['simple'] );
}
yourls_do_action( 'pre_api_output', $mode, $return );
switch ( $mode ) {
case 'jsonp':
header('Content-type: application/javascript');
echo $return['callback'] . '(' . json_encode($return) . ')';
break;
case 'json':
header('Content-type: application/json');
echo json_encode($return);
break;
case 'xml':
header('Content-type: application/xml');
echo yourls_xml_encode($return);
break;
case 'simple':
default:
if( isset( $simple ) )
echo $simple;
break;
}
yourls_do_action( 'api_output', $mode, $return );
die();
}
// Get number of SQL queries performed
function yourls_get_num_queries() {
global $ydb;
return yourls_apply_filter( 'get_num_queries', $ydb->num_queries );
}
// Returns a sanitized a user agent string. Given what I found on http://www.user-agents.org/ it should be OK.
function yourls_get_user_agent() {
if ( !isset( $_SERVER['HTTP_USER_AGENT'] ) )
return '-';
$ua = strip_tags( html_entity_decode( $_SERVER['HTTP_USER_AGENT'] ));
$ua = preg_replace('![^0-9a-zA-Z\':., /{}\(\)\[\]\+@&\!\?;_\-=~\*\#]!', '', $ua );
return yourls_apply_filter( 'get_user_agent', substr( $ua, 0, 254 ) );
}
// Redirect to another page
function yourls_redirect( $location, $code = 301 ) {
yourls_do_action( 'pre_redirect', $location, $code );
$location = yourls_apply_filter( 'redirect', $location, $code );
// Redirect, either properly if possible, or via Javascript otherwise
if( !headers_sent() ) {
yourls_status_header( $code );
header( "Location: $location" );
} else {
yourls_redirect_javascript( $location );
}
die();
}
// Set HTTP status header
function yourls_status_header( $code = 200 ) {
if( headers_sent() )
return;
$protocol = $_SERVER["SERVER_PROTOCOL"];
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
$protocol = 'HTTP/1.0';
$code = intval( $code );
$desc = yourls_get_HTTP_status( $code );
@header ("$protocol $code $desc"); // This causes problems on IIS and some FastCGI setups
yourls_do_action( 'status_header', $code );
}
// Redirect to another page using Javascript. Set optional (bool)$dontwait to false to force manual redirection (make sure a message has been read by user)
function yourls_redirect_javascript( $location, $dontwait = true ) {
yourls_do_action( 'pre_redirect_javascript', $location, $dontwait );
$location = yourls_apply_filter( 'redirect_javascript', $location, $dontwait );
if( $dontwait ) {
echo <<
Things should not last very long, thank you for your patience and please excuse the inconvenience'; yourls_die( $message, $title , 503 ); } } // Toggle maintenance mode function yourls_maintenance_mode( $maintenance = true ) { yourls_update_option( 'maintenance_mode', (bool)$maintenance ); } // Quick UA check for mobile devices. Return boolean. function yourls_is_mobile_device() { // Strings searched $mobiles = array( 'android', 'blackberry', 'blazer', 'compal', 'elaine', 'fennec', 'hiptop', 'iemobile', 'iphone', 'ipod', 'ipad', 'iris', 'kindle', 'opera mobi', 'opera mini', 'palm', 'phone', 'pocket', 'psp', 'symbian', 'treo', 'wap', 'windows ce', 'windows phone' ); // Current user-agent $current = strtolower( $_SERVER['HTTP_USER_AGENT'] ); // Check and return $is_mobile = ( str_replace( $mobiles, '', $current ) != $current ); return yourls_apply_filter( 'is_mobile_device', $is_mobile ); } // Get request in YOURLS base (eg in 'http://site.com/yourls/abcd' get 'abdc') function yourls_get_request() { // Allow plugins to short-circuit the whole function $pre = yourls_apply_filter( 'shunt_get_request', false ); if ( false !== $pre ) return $pre; yourls_do_action( 'pre_get_request' ); // Ignore protocol, www. prefix and extract keyword $base = str_replace( array( 'https://', 'http://', 'https://www.', 'http://www.' ), '', YOURLS_SITE ); $request = str_replace( $base.'/', '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); // Strip query string from keyword $request = current( explode( '?', $request ) ); return yourls_apply_filter( 'get_request', $request ); } // Change protocol to match current scheme used (http or https) function yourls_match_current_protocol( $url, $normal = 'http', $ssl = 'https' ) { if( yourls_is_ssl() ) $url = str_replace( $normal, $ssl, $url ); return yourls_apply_filter( 'match_current_protocol', $url ); } // Fix $_SERVER['REQUEST_URI'] variable for various setups. Stolen from WP. function yourls_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']; } } } } // Shutdown function, runs just before PHP shuts down execution. Stolen from WP function yourls_shutdown() { yourls_do_action( 'shutdown' ); }