8 * Determine the allowed character set in short URLs
11 function yourls_get_shorturl_charset() {
12 static $charset = null;
13 if( $charset !== null )
16 if( !defined('YOURLS_URL_CONVERT') ) {
17 $charset = '0123456789abcdefghijklmnopqrstuvwxyz';
19 switch( YOURLS_URL_CONVERT ) {
21 $charset = '0123456789abcdefghijklmnopqrstuvwxyz';
24 case 64: // just because some people get this wrong in their config.php
25 $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
30 $charset = yourls_apply_filter( 'get_shorturl_charset', $charset );
35 * Make an optimized regexp pattern from a string of characters
38 function yourls_make_regexp_pattern( $string ) {
39 $pattern = preg_quote( $string, '-' ); // add - as an escaped characters -- this is fixed in PHP 5.3
40 // TODO: replace char sequences by smart sequences such as 0-9, a-z, A-Z ... ?
45 * Is a URL a short URL? Accept either 'http://sho.rt/abc' or 'abc'
48 function yourls_is_shorturl( $shorturl ) {
49 // TODO: make sure this function evolves with the feature set.
53 // Is $shorturl a URL (http://sho.rt/abc) or a keyword (abc) ?
54 if( yourls_get_protocol( $shorturl ) ) {
55 $keyword = yourls_get_relative_url( $shorturl );
60 // Check if it's a valid && used keyword
61 if( $keyword && $keyword == yourls_sanitize_string( $keyword ) && yourls_keyword_is_taken( $keyword ) ) {
65 return yourls_apply_filter( 'is_shorturl', $is_short, $shorturl );
69 * Check to see if a given keyword is reserved (ie reserved URL or an existing page). Returns bool
72 function yourls_keyword_is_reserved( $keyword ) {
73 global $yourls_reserved_URL;
74 $keyword = yourls_sanitize_keyword( $keyword );
77 if ( in_array( $keyword, $yourls_reserved_URL)
78 or file_exists( YOURLS_ABSPATH ."/pages/$keyword.php" )
79 or is_dir( YOURLS_ABSPATH ."/$keyword" )
83 return yourls_apply_filter( 'keyword_is_reserved', $reserved, $keyword );
87 * Function: Get client IP Address. Returns a DB safe string.
90 function yourls_get_IP() {
93 // Precedence: if set, X-Forwarded-For > HTTP_X_FORWARDED_FOR > HTTP_CLIENT_IP > HTTP_VIA > REMOTE_ADDR
94 $headers = array( 'X-Forwarded-For', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_VIA', 'REMOTE_ADDR' );
95 foreach( $headers as $header ) {
96 if ( !empty( $_SERVER[ $header ] ) ) {
97 $ip = $_SERVER[ $header ];
102 // headers can contain multiple IPs (X-Forwarded-For = client, proxy1, proxy2). Take first one.
103 if ( strpos( $ip, ',' ) !== false )
104 $ip = substr( $ip, 0, strpos( $ip, ',' ) );
106 return yourls_apply_filter( 'get_IP', yourls_sanitize_ip( $ip ) );
110 * Get next id a new link will have if no custom keyword provided
113 function yourls_get_next_decimal() {
114 return yourls_apply_filter( 'get_next_decimal', (int)yourls_get_option( 'next_id' ) );
118 * Update id for next link with no custom keyword
121 function yourls_update_next_decimal( $int = '' ) {
122 $int = ( $int == '' ) ? yourls_get_next_decimal() + 1 : (int)$int ;
123 $update = yourls_update_option( 'next_id', $int );
124 yourls_do_action( 'update_next_decimal', $int, $update );
129 * Delete a link in the DB
132 function yourls_delete_link_by_keyword( $keyword ) {
135 $table = YOURLS_DB_TABLE_URL;
136 $keyword = yourls_sanitize_string( $keyword );
137 $delete = $ydb->query("DELETE FROM `$table` WHERE `keyword` = '$keyword';");
138 yourls_do_action( 'delete_link', $keyword, $delete );
143 * SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting
146 function yourls_insert_link_in_db( $url, $keyword, $title = '' ) {
149 $url = yourls_escape( yourls_sanitize_url( $url ) );
150 $keyword = yourls_escape( yourls_sanitize_keyword( $keyword ) );
151 $title = yourls_escape( yourls_sanitize_title( $title ) );
153 $table = YOURLS_DB_TABLE_URL;
154 $timestamp = date('Y-m-d H:i:s');
155 $ip = yourls_get_IP();
156 $insert = $ydb->query("INSERT INTO `$table` (`keyword`, `url`, `title`, `timestamp`, `ip`, `clicks`) VALUES('$keyword', '$url', '$title', '$timestamp', '$ip', 0);");
158 yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $title, $timestamp, $ip );
160 return (bool)$insert;
164 * Check if a URL already exists in the DB. Return NULL (doesn't exist) or an object with URL informations.
167 function yourls_url_exists( $url ) {
168 // Allow plugins to short-circuit the whole function
169 $pre = yourls_apply_filter( 'shunt_url_exists', false, $url );
170 if ( false !== $pre )
174 $table = YOURLS_DB_TABLE_URL;
175 $strip_url = stripslashes($url);
176 $url_exists = $ydb->get_row("SELECT * FROM `$table` WHERE `url` = '".$strip_url."';");
178 return yourls_apply_filter( 'url_exists', $url_exists, $url );
182 * Add a new link in the DB, either with custom keyword, or find one
185 function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
186 // Allow plugins to short-circuit the whole function
187 $pre = yourls_apply_filter( 'shunt_add_new_link', false, $url, $keyword, $title );
188 if ( false !== $pre )
191 $url = yourls_encodeURI( $url );
192 $url = yourls_escape( yourls_sanitize_url( $url ) );
193 if ( !$url || $url == 'http://' || $url == 'https://' ) {
194 $return['status'] = 'fail';
195 $return['code'] = 'error:nourl';
196 $return['message'] = yourls__( 'Missing or malformed URL' );
197 $return['errorCode'] = '400';
198 return yourls_apply_filter( 'add_new_link_fail_nourl', $return, $url, $keyword, $title );
202 $ip = yourls_get_IP();
203 yourls_check_IP_flood( $ip );
205 // Prevent internal redirection loops: cannot shorten a shortened URL
206 if( yourls_get_relative_url( $url ) ) {
207 if( yourls_is_shorturl( $url ) ) {
208 $return['status'] = 'fail';
209 $return['code'] = 'error:noloop';
210 $return['message'] = yourls__( 'URL is a short URL' );
211 $return['errorCode'] = '400';
212 return yourls_apply_filter( 'add_new_link_fail_noloop', $return, $url, $keyword, $title );
216 yourls_do_action( 'pre_add_new_link', $url, $keyword, $title );
218 $strip_url = stripslashes( $url );
221 // duplicates allowed or new URL => store it
222 if( yourls_allow_duplicate_longurls() || !( $url_exists = yourls_url_exists( $url ) ) ) {
224 if( isset( $title ) && !empty( $title ) ) {
225 $title = yourls_sanitize_title( $title );
227 $title = yourls_get_remote_title( $url );
229 $title = yourls_apply_filter( 'add_new_title', $title, $url, $keyword );
231 // Custom keyword provided
234 yourls_do_action( 'add_new_link_custom_keyword', $url, $keyword, $title );
236 $keyword = yourls_escape( yourls_sanitize_string( $keyword ) );
237 $keyword = yourls_apply_filter( 'custom_keyword', $keyword, $url, $title );
238 if ( !yourls_keyword_is_free( $keyword ) ) {
239 // This shorturl either reserved or taken already
240 $return['status'] = 'fail';
241 $return['code'] = 'error:keyword';
242 $return['message'] = yourls_s( 'Short URL %s already exists in database or is reserved', $keyword );
244 // all clear, store !
245 yourls_insert_link_in_db( $url, $keyword, $title );
246 $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => date('Y-m-d H:i:s'), 'ip' => $ip );
247 $return['status'] = 'success';
248 $return['message'] = /* //translators: eg "http://someurl/ added to DB" */ yourls_s( '%s added to database', yourls_trim_long_string( $strip_url ) );
249 $return['title'] = $title;
250 $return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
251 $return['shorturl'] = YOURLS_SITE .'/'. $keyword;
254 // Create random keyword
257 yourls_do_action( 'add_new_link_create_keyword', $url, $keyword, $title );
259 $timestamp = date( 'Y-m-d H:i:s' );
260 $id = yourls_get_next_decimal();
263 $keyword = yourls_int2string( $id );
264 $keyword = yourls_apply_filter( 'random_keyword', $keyword, $url, $title );
265 if ( yourls_keyword_is_free($keyword) ) {
266 if( @yourls_insert_link_in_db( $url, $keyword, $title ) ){
267 // everything ok, populate needed vars
268 $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip );
269 $return['status'] = 'success';
270 $return['message'] = /* //translators: eg "http://someurl/ added to DB" */ yourls_s( '%s added to database', yourls_trim_long_string( $strip_url ) );
271 $return['title'] = $title;
272 $return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
273 $return['shorturl'] = YOURLS_SITE .'/'. $keyword;
275 // database error, couldnt store result
276 $return['status'] = 'fail';
277 $return['code'] = 'error:db';
278 $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 );
279 $return['message'] = /* //translators: eg "Error saving url to database" */ yourls_s( 'Error saving url to database' );
280 $return['title'] = /* //translators: eg "Error saving url to database" */ yourls_s( 'Error saving url to database' );
286 @yourls_update_next_decimal( $id );
289 // URL was already stored
292 yourls_do_action( 'add_new_link_already_stored', $url, $keyword, $title );
294 $return['status'] = 'fail';
295 $return['code'] = 'error:url';
296 $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 );
297 $return['message'] = /* //translators: eg "http://someurl/ already exists" */ yourls_s( '%s already exists in database', yourls_trim_long_string( $strip_url ) );
298 $return['title'] = $url_exists->title;
299 $return['shorturl'] = YOURLS_SITE .'/'. $url_exists->keyword;
302 yourls_do_action( 'post_add_new_link', $url, $keyword, $title );
304 $return['statusCode'] = 200; // regardless of result, this is still a valid request
305 return yourls_apply_filter( 'add_new_link', $return, $url, $keyword, $title );
313 function yourls_edit_link( $url, $keyword, $newkeyword='', $title='' ) {
316 $table = YOURLS_DB_TABLE_URL;
317 $url = yourls_escape (yourls_sanitize_url( $url ) );
318 $keyword = yourls_escape( yourls_sanitize_string( $keyword ) );
319 $title = yourls_escape( yourls_sanitize_title( $title ) );
320 $newkeyword = yourls_escape( yourls_sanitize_string( $newkeyword ) );
321 $strip_url = stripslashes( $url );
322 $strip_title = stripslashes( $title );
323 $old_url = $ydb->get_var( "SELECT `url` FROM `$table` WHERE `keyword` = '$keyword';" );
325 // Check if new URL is not here already
326 if ( $old_url != $url && !yourls_allow_duplicate_longurls() ) {
327 $new_url_already_there = intval($ydb->get_var("SELECT COUNT(keyword) FROM `$table` WHERE `url` = '$strip_url';"));
329 $new_url_already_there = false;
332 // Check if the new keyword is not here already
333 if ( $newkeyword != $keyword ) {
334 $keyword_is_ok = yourls_keyword_is_free( $newkeyword );
336 $keyword_is_ok = true;
339 yourls_do_action( 'pre_edit_link', $url, $keyword, $newkeyword, $new_url_already_there, $keyword_is_ok );
342 if ( ( !$new_url_already_there || yourls_allow_duplicate_longurls() ) && $keyword_is_ok ) {
343 $update_url = $ydb->query( "UPDATE `$table` SET `url` = '$url', `keyword` = '$newkeyword', `title` = '$title' WHERE `keyword` = '$keyword';" );
345 $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 ) );
346 $return['status'] = 'success';
347 $return['message'] = yourls__( 'Link updated in database' );
349 $return['status'] = 'fail';
350 $return['message'] = /* //translators: "Error updating http://someurl/ (Shorturl: http://sho.rt/blah)" */ yourls_s( 'Error updating %s (Short URL: %s)', yourls_trim_long_string( $strip_url ), $keyword ) ;
355 $return['status'] = 'fail';
356 $return['message'] = yourls__( 'URL or keyword already exists in database' );
359 return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title, $new_url_already_there, $keyword_is_ok );
363 * Update a title link (no checks for duplicates etc..)
366 function yourls_edit_link_title( $keyword, $title ) {
369 $keyword = yourls_escape( yourls_sanitize_keyword( $keyword ) );
370 $title = yourls_escape( yourls_sanitize_title( $title ) );
372 $table = YOURLS_DB_TABLE_URL;
373 $update = $ydb->query("UPDATE `$table` SET `title` = '$title' WHERE `keyword` = '$keyword';");
380 * Check if keyword id is free (ie not already taken, and not reserved). Return bool.
383 function yourls_keyword_is_free( $keyword ) {
385 if ( yourls_keyword_is_reserved( $keyword ) or yourls_keyword_is_taken( $keyword ) )
388 return yourls_apply_filter( 'keyword_is_free', $free, $keyword );
392 * Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
395 function yourls_keyword_is_taken( $keyword ) {
397 // Allow plugins to short-circuit the whole function
398 $pre = yourls_apply_filter( 'shunt_keyword_is_taken', false, $keyword );
399 if ( false !== $pre )
403 $keyword = yourls_sanitize_keyword( $keyword );
405 $table = YOURLS_DB_TABLE_URL;
406 $already_exists = $ydb->get_var( "SELECT COUNT(`keyword`) FROM `$table` WHERE `keyword` = '$keyword';" );
407 if ( $already_exists )
410 return yourls_apply_filter( 'keyword_is_taken', $taken, $keyword );
418 function yourls_db_connect() {
421 if ( !defined( 'YOURLS_DB_USER' )
422 or !defined( 'YOURLS_DB_PASS' )
423 or !defined( 'YOURLS_DB_NAME' )
424 or !defined( 'YOURLS_DB_HOST' )
425 ) yourls_die ( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );
427 // Are we standalone or in the WordPress environment?
428 if ( class_exists( 'wpdb', false ) ) {
429 /* TODO: should we deprecate this? Follow WP dev in that area */
430 $ydb = new wpdb( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
432 yourls_set_DB_driver();
435 // Check if connection attempt raised an error. It seems that only PDO does, though.
436 if ( $ydb->last_error )
437 yourls_die( $ydb->last_error, yourls__( 'Fatal error' ), 503 );
439 if ( defined( 'YOURLS_DEBUG' ) && YOURLS_DEBUG === true )
440 $ydb->show_errors = true;
449 function yourls_xml_encode( $array ) {
450 require_once( YOURLS_INC.'/functions-xml.php' );
451 $converter= new yourls_array2xml;
452 return $converter->array2xml( $array );
456 * Return array of all information associated with keyword. Returns false if keyword not found. Set optional $use_cache to false to force fetching from DB
459 function yourls_get_keyword_infos( $keyword, $use_cache = true ) {
461 $keyword = yourls_sanitize_string( $keyword );
463 yourls_do_action( 'pre_get_keyword', $keyword, $use_cache );
465 if( isset( $ydb->infos[$keyword] ) && $use_cache == true ) {
466 return yourls_apply_filter( 'get_keyword_infos', $ydb->infos[$keyword], $keyword );
469 yourls_do_action( 'get_keyword_not_cached', $keyword );
471 $table = YOURLS_DB_TABLE_URL;
472 $infos = $ydb->get_row( "SELECT * FROM `$table` WHERE `keyword` = '$keyword'" );
475 $infos = (array)$infos;
476 $ydb->infos[ $keyword ] = $infos;
478 $ydb->infos[ $keyword ] = false;
481 return yourls_apply_filter( 'get_keyword_infos', $ydb->infos[$keyword], $keyword );
485 * Return (string) selected information associated with a keyword. Optional $notfound = string default message if nothing found
488 function yourls_get_keyword_info( $keyword, $field, $notfound = false ) {
490 // Allow plugins to short-circuit the whole function
491 $pre = yourls_apply_filter( 'shunt_get_keyword_info', false, $keyword, $field, $notfound );
492 if ( false !== $pre )
495 $keyword = yourls_sanitize_string( $keyword );
496 $infos = yourls_get_keyword_infos( $keyword );
499 if ( isset( $infos[ $field ] ) && $infos[ $field ] !== false )
500 $return = $infos[ $field ];
502 return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound );
506 * Return title associated with keyword. Optional $notfound = string default message if nothing found
509 function yourls_get_keyword_title( $keyword, $notfound = false ) {
510 return yourls_get_keyword_info( $keyword, 'title', $notfound );
514 * Return long URL associated with keyword. Optional $notfound = string default message if nothing found
517 function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
518 return yourls_get_keyword_info( $keyword, 'url', $notfound );
522 * Return number of clicks on a keyword. Optional $notfound = string default message if nothing found
525 function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
526 return yourls_get_keyword_info( $keyword, 'clicks', $notfound );
530 * Return IP that added a keyword. Optional $notfound = string default message if nothing found
533 function yourls_get_keyword_IP( $keyword, $notfound = false ) {
534 return yourls_get_keyword_info( $keyword, 'ip', $notfound );
538 * Return timestamp associated with a keyword. Optional $notfound = string default message if nothing found
541 function yourls_get_keyword_timestamp( $keyword, $notfound = false ) {
542 return yourls_get_keyword_info( $keyword, 'timestamp', $notfound );
546 * Update click count on a short URL. Return 0/1 for error/success.
549 function yourls_update_clicks( $keyword, $clicks = false ) {
550 // Allow plugins to short-circuit the whole function
551 $pre = yourls_apply_filter( 'shunt_update_clicks', false, $keyword, $clicks );
552 if ( false !== $pre )
556 $keyword = yourls_sanitize_string( $keyword );
557 $table = YOURLS_DB_TABLE_URL;
558 if ( $clicks !== false && is_int( $clicks ) && $clicks >= 0 )
559 $update = $ydb->query( "UPDATE `$table` SET `clicks` = $clicks WHERE `keyword` = '$keyword'" );
561 $update = $ydb->query( "UPDATE `$table` SET `clicks` = clicks + 1 WHERE `keyword` = '$keyword'" );
563 yourls_do_action( 'update_clicks', $keyword, $update, $clicks );
568 * Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
571 function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
580 $sort_by = 'timestamp';
581 $sort_order = 'desc';
591 $sort_order = 'desc';
596 $limit = intval( $limit );
597 $start = intval( $start );
600 $table_url = YOURLS_DB_TABLE_URL;
601 $results = $ydb->get_results( "SELECT * FROM `$table_url` WHERE 1=1 ORDER BY `$sort_by` $sort_order LIMIT $start, $limit;" );
606 foreach ( (array)$results as $res ) {
607 $return['links']['link_'.$i++] = array(
608 'shorturl' => YOURLS_SITE .'/'. $res->keyword,
610 'title' => $res->title,
611 'timestamp'=> $res->timestamp,
613 'clicks' => $res->clicks,
618 $return['stats'] = yourls_get_db_stats();
620 $return['statusCode'] = 200;
622 return yourls_apply_filter( 'get_stats', $return, $filter, $limit, $start );
626 * Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
629 function yourls_get_link_stats( $shorturl ) {
632 $table_url = YOURLS_DB_TABLE_URL;
633 $res = $ydb->get_row( "SELECT * FROM `$table_url` WHERE keyword = '$shorturl';" );
640 'message' => 'Error: short URL not found',
645 'message' => 'success',
647 'shorturl' => YOURLS_SITE .'/'. $res->keyword,
649 'title' => $res->title,
650 'timestamp'=> $res->timestamp,
652 'clicks' => $res->clicks,
657 return yourls_apply_filter( 'get_link_stats', $return, $shorturl );
661 * Get total number of URLs and sum of clicks. Input: optional "AND WHERE" clause. Returns array
664 function yourls_get_db_stats( $where = '' ) {
666 $table_url = YOURLS_DB_TABLE_URL;
668 $totals = $ydb->get_row( "SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM `$table_url` WHERE 1=1 $where" );
669 $return = array( 'total_links' => $totals->count, 'total_clicks' => $totals->sum );
671 return yourls_apply_filter( 'get_db_stats', $return, $where );
675 * Get number of SQL queries performed
678 function yourls_get_num_queries() {
681 return yourls_apply_filter( 'get_num_queries', $ydb->num_queries );
685 * Returns a sanitized a user agent string. Given what I found on http://www.user-agents.org/ it should be OK.
688 function yourls_get_user_agent() {
689 if ( !isset( $_SERVER['HTTP_USER_AGENT'] ) )
692 $ua = strip_tags( html_entity_decode( $_SERVER['HTTP_USER_AGENT'] ));
693 $ua = preg_replace('![^0-9a-zA-Z\':., /{}\(\)\[\]\+@&\!\?;_\-=~\*\#]!', '', $ua );
695 return yourls_apply_filter( 'get_user_agent', substr( $ua, 0, 254 ) );
699 * Redirect to another page
702 function yourls_redirect( $location, $code = 301 ) {
703 yourls_do_action( 'pre_redirect', $location, $code );
704 $location = yourls_apply_filter( 'redirect_location', $location, $code );
705 $code = yourls_apply_filter( 'redirect_code', $code, $location );
706 // Redirect, either properly if possible, or via Javascript otherwise
707 if( !headers_sent() ) {
708 yourls_status_header( $code );
709 header( "Location: $location" );
711 yourls_redirect_javascript( $location );
717 * Set HTTP status header
720 function yourls_status_header( $code = 200 ) {
724 $protocol = $_SERVER["SERVER_PROTOCOL"];
725 if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
726 $protocol = 'HTTP/1.0';
728 $code = intval( $code );
729 $desc = yourls_get_HTTP_status( $code );
731 @header ("$protocol $code $desc"); // This causes problems on IIS and some FastCGI setups
732 yourls_do_action( 'status_header', $code );
736 * 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)
739 function yourls_redirect_javascript( $location, $dontwait = true ) {
740 yourls_do_action( 'pre_redirect_javascript', $location, $dontwait );
741 $location = yourls_apply_filter( 'redirect_javascript', $location, $dontwait );
743 $message = yourls_s( 'if you are not redirected after 10 seconds, please <a href="%s">click here</a>', $location );
745 <script type="text/javascript">
746 window.location="$location";
748 <small>($message)</small>
751 echo '<p>' . yourls_s( 'Please <a href="%s">click here</a>', $location ) . '</p>';
753 yourls_do_action( 'post_redirect_javascript', $location );
757 * Return a HTTP status code
760 function yourls_get_HTTP_status( $code ) {
761 $code = intval( $code );
762 $headers_desc = array(
764 101 => 'Switching Protocols',
770 203 => 'Non-Authoritative Information',
772 205 => 'Reset Content',
773 206 => 'Partial Content',
774 207 => 'Multi-Status',
777 300 => 'Multiple Choices',
778 301 => 'Moved Permanently',
781 304 => 'Not Modified',
784 307 => 'Temporary Redirect',
786 400 => 'Bad Request',
787 401 => 'Unauthorized',
788 402 => 'Payment Required',
791 405 => 'Method Not Allowed',
792 406 => 'Not Acceptable',
793 407 => 'Proxy Authentication Required',
794 408 => 'Request Timeout',
797 411 => 'Length Required',
798 412 => 'Precondition Failed',
799 413 => 'Request Entity Too Large',
800 414 => 'Request-URI Too Long',
801 415 => 'Unsupported Media Type',
802 416 => 'Requested Range Not Satisfiable',
803 417 => 'Expectation Failed',
804 422 => 'Unprocessable Entity',
806 424 => 'Failed Dependency',
807 426 => 'Upgrade Required',
809 500 => 'Internal Server Error',
810 501 => 'Not Implemented',
811 502 => 'Bad Gateway',
812 503 => 'Service Unavailable',
813 504 => 'Gateway Timeout',
814 505 => 'HTTP Version Not Supported',
815 506 => 'Variant Also Negotiates',
816 507 => 'Insufficient Storage',
817 510 => 'Not Extended'
820 if ( isset( $headers_desc[$code] ) )
821 return $headers_desc[$code];
828 * Log a redirect (for stats)
831 function yourls_log_redirect( $keyword ) {
832 // Allow plugins to short-circuit the whole function
833 $pre = yourls_apply_filter( 'shunt_log_redirect', false, $keyword );
834 if ( false !== $pre )
837 if ( !yourls_do_log_redirect() )
841 $table = YOURLS_DB_TABLE_LOG;
843 $keyword = yourls_sanitize_string( $keyword );
844 $referrer = ( isset( $_SERVER['HTTP_REFERER'] ) ? yourls_sanitize_url( $_SERVER['HTTP_REFERER'] ) : 'direct' );
845 $ua = yourls_get_user_agent();
846 $ip = yourls_get_IP();
847 $location = yourls_geo_ip_to_countrycode( $ip );
849 return $ydb->query( "INSERT INTO `$table` (click_time, shorturl, referrer, user_agent, ip_address, country_code) VALUES (NOW(), '$keyword', '$referrer', '$ua', '$ip', '$location')" );
853 * Check if we want to not log redirects (for stats)
856 function yourls_do_log_redirect() {
857 return ( !defined( 'YOURLS_NOSTATS' ) || YOURLS_NOSTATS != true );
861 * Converts an IP to a 2 letter country code, using GeoIP database if available in includes/geo/
864 function yourls_geo_ip_to_countrycode( $ip = '', $default = '' ) {
865 // Allow plugins to short-circuit the Geo IP API
866 $location = yourls_apply_filter( 'shunt_geo_ip_to_countrycode', false, $ip, $default ); // at this point $ip can be '', check if your plugin hooks in here
867 if ( false !== $location )
870 if ( !file_exists( YOURLS_INC.'/geo/GeoIP.dat') || !file_exists( YOURLS_INC.'/geo/geoip.inc') )
874 $ip = yourls_get_IP();
876 require_once( YOURLS_INC.'/geo/geoip.inc') ;
877 $gi = geoip_open( YOURLS_INC.'/geo/GeoIP.dat', GEOIP_STANDARD);
878 $location = geoip_country_code_by_addr($gi, $ip);
881 return yourls_apply_filter( 'geo_ip_to_countrycode', $location, $ip, $default );
885 * Converts a 2 letter country code to long name (ie AU -> Australia)
888 function yourls_geo_countrycode_to_countryname( $code ) {
889 // Allow plugins to short-circuit the Geo IP API
890 $country = yourls_apply_filter( 'shunt_geo_countrycode_to_countryname', false, $code );
891 if ( false !== $country )
894 // Load the Geo class if not already done
895 if( !class_exists( 'GeoIP', false ) ) {
896 $temp = yourls_geo_ip_to_countrycode( '127.0.0.1' );
899 if( class_exists( 'GeoIP', false ) ) {
901 $id = $geo->GEOIP_COUNTRY_CODE_TO_NUMBER[ $code ];
902 $long = $geo->GEOIP_COUNTRY_NAMES[ $id ];
910 * Return flag URL from 2 letter country code
913 function yourls_geo_get_flag( $code ) {
914 if( file_exists( YOURLS_INC.'/geo/flags/flag_'.strtolower($code).'.gif' ) ) {
915 $img = yourls_match_current_protocol( YOURLS_SITE.'/includes/geo/flags/flag_'.( strtolower( $code ) ).'.gif' );
919 return yourls_apply_filter( 'geo_get_flag', $img, $code );
924 * Check if an upgrade is needed
927 function yourls_upgrade_is_needed() {
928 // check YOURLS_DB_VERSION exist && match values stored in YOURLS_DB_TABLE_OPTIONS
929 list( $currentver, $currentsql ) = yourls_get_current_version_from_sql();
930 if( $currentsql < YOURLS_DB_VERSION )
937 * Get current version & db version as stored in the options DB. Prior to 1.4 there's no option table.
940 function yourls_get_current_version_from_sql() {
941 $currentver = yourls_get_option( 'version' );
942 $currentsql = yourls_get_option( 'db_version' );
944 // Values if version is 1.3
950 return array( $currentver, $currentsql);
954 * Read an option from DB (or from cache if available). Return value or $default if not found
957 function yourls_get_option( $option_name, $default = false ) {
960 // Allow plugins to short-circuit options
961 $pre = yourls_apply_filter( 'shunt_option_'.$option_name, false );
962 if ( false !== $pre )
965 // If option not cached already, get its value from the DB
966 if ( !isset( $ydb->option[$option_name] ) ) {
967 $table = YOURLS_DB_TABLE_OPTIONS;
968 $option_name = yourls_escape( $option_name );
969 $row = $ydb->get_row( "SELECT `option_value` FROM `$table` WHERE `option_name` = '$option_name' LIMIT 1" );
970 if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
971 $value = $row->option_value;
972 } else { // option does not exist, so we must cache its non-existence
975 $ydb->option[ $option_name ] = yourls_maybe_unserialize( $value );
978 return yourls_apply_filter( 'get_option_'.$option_name, $ydb->option[$option_name] );
982 * Read all options from DB at once
985 function yourls_get_all_options() {
988 // Allow plugins to short-circuit all options. (Note: regular plugins are loaded after all options)
989 $pre = yourls_apply_filter( 'shunt_all_options', false );
990 if ( false !== $pre )
993 $table = YOURLS_DB_TABLE_OPTIONS;
995 $allopt = $ydb->get_results( "SELECT `option_name`, `option_value` FROM `$table` WHERE 1=1" );
997 foreach( (array)$allopt as $option ) {
998 $ydb->option[ $option->option_name ] = yourls_maybe_unserialize( $option->option_value );
1001 if( property_exists( $ydb, 'option' ) ) {
1002 $ydb->option = yourls_apply_filter( 'get_all_options', $ydb->option );
1003 $ydb->installed = true;
1005 // Zero option found: assume YOURLS is not installed
1006 $ydb->installed = false;
1011 * Update (add if doesn't exist) an option to DB
1014 function yourls_update_option( $option_name, $newvalue ) {
1016 $table = YOURLS_DB_TABLE_OPTIONS;
1018 $safe_option_name = yourls_escape( $option_name );
1020 $oldvalue = yourls_get_option( $safe_option_name );
1022 // If the new and old values are the same, no need to update.
1023 if ( $newvalue === $oldvalue )
1026 if ( false === $oldvalue ) {
1027 yourls_add_option( $option_name, $newvalue );
1031 $_newvalue = yourls_escape( yourls_maybe_serialize( $newvalue ) );
1033 yourls_do_action( 'update_option', $option_name, $oldvalue, $newvalue );
1035 $ydb->query( "UPDATE `$table` SET `option_value` = '$_newvalue' WHERE `option_name` = '$option_name'" );
1037 if ( $ydb->rows_affected == 1 ) {
1038 $ydb->option[ $option_name ] = $newvalue;
1045 * Add an option to the DB
1048 function yourls_add_option( $name, $value = '' ) {
1050 $table = YOURLS_DB_TABLE_OPTIONS;
1051 $safe_name = yourls_escape( $name );
1053 // Make sure the option doesn't already exist
1054 if ( false !== yourls_get_option( $safe_name ) )
1057 $_value = yourls_escape( yourls_maybe_serialize( $value ) );
1059 yourls_do_action( 'add_option', $safe_name, $_value );
1061 $ydb->query( "INSERT INTO `$table` (`option_name`, `option_value`) VALUES ('$name', '$_value')" );
1062 $ydb->option[ $name ] = $value;
1068 * Delete an option from the DB
1071 function yourls_delete_option( $name ) {
1073 $table = YOURLS_DB_TABLE_OPTIONS;
1074 $name = yourls_escape( $name );
1076 // Get the ID, if no ID then return
1077 $option = $ydb->get_row( "SELECT option_id FROM `$table` WHERE `option_name` = '$name'" );
1078 if ( is_null( $option ) || !$option->option_id )
1081 yourls_do_action( 'delete_option', $name );
1083 $ydb->query( "DELETE FROM `$table` WHERE `option_name` = '$name'" );
1090 * Serialize data if needed. Stolen from WordPress
1093 function yourls_maybe_serialize( $data ) {
1094 if ( is_array( $data ) || is_object( $data ) )
1095 return serialize( $data );
1097 if ( yourls_is_serialized( $data ) )
1098 return serialize( $data );
1104 * Check value to find if it was serialized. Stolen from WordPress
1107 function yourls_is_serialized( $data ) {
1108 // if it isn't a string, it isn't serialized
1109 if ( !is_string( $data ) )
1111 $data = trim( $data );
1112 if ( 'N;' == $data )
1114 if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
1116 switch ( $badions[1] ) {
1120 if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
1126 if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
1134 * Unserialize value only if it was serialized. Stolen from WP
1137 function yourls_maybe_unserialize( $original ) {
1138 if ( yourls_is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
1139 return @unserialize( $original );
1144 * Determine if the current page is private
1147 function yourls_is_private() {
1150 if ( defined('YOURLS_PRIVATE') && YOURLS_PRIVATE == true ) {
1152 // Allow overruling for particular pages:
1155 if( yourls_is_API() ) {
1156 if( !defined('YOURLS_PRIVATE_API') || YOURLS_PRIVATE_API != false )
1160 } elseif( yourls_is_infos() ) {
1161 if( !defined('YOURLS_PRIVATE_INFOS') || YOURLS_PRIVATE_INFOS !== false )
1171 return yourls_apply_filter( 'is_private', $private );
1175 * Show login form if required
1178 function yourls_maybe_require_auth() {
1179 if( yourls_is_private() ) {
1180 yourls_do_action( 'require_auth' );
1181 require_once( YOURLS_INC.'/auth.php' );
1183 yourls_do_action( 'require_no_auth' );
1188 * Allow several short URLs for the same long URL ?
1191 function yourls_allow_duplicate_longurls() {
1192 // special treatment if API to check for WordPress plugin requests
1193 if( yourls_is_API() ) {
1194 if ( isset($_REQUEST['source']) && $_REQUEST['source'] == 'plugin' )
1197 return ( defined( 'YOURLS_UNIQUE_URLS' ) && YOURLS_UNIQUE_URLS == false );
1201 * Return list of all shorturls associated to the same long URL. Returns NULL or array of keywords.
1204 function yourls_get_duplicate_keywords( $longurl ) {
1205 if( !yourls_allow_duplicate_longurls() )
1209 $longurl = yourls_escape( yourls_sanitize_url($longurl) );
1210 $table = YOURLS_DB_TABLE_URL;
1212 $return = $ydb->get_col( "SELECT `keyword` FROM `$table` WHERE `url` = '$longurl'" );
1213 return yourls_apply_filter( 'get_duplicate_keywords', $return, $longurl );
1217 * Check if an IP shortens URL too fast to prevent DB flood. Return true, or die.
1220 function yourls_check_IP_flood( $ip = '' ) {
1222 // Allow plugins to short-circuit the whole function
1223 $pre = yourls_apply_filter( 'shunt_check_IP_flood', false, $ip );
1224 if ( false !== $pre )
1227 yourls_do_action( 'pre_check_ip_flood', $ip ); // at this point $ip can be '', check it if your plugin hooks in here
1230 ( defined('YOURLS_FLOOD_DELAY_SECONDS') && YOURLS_FLOOD_DELAY_SECONDS === 0 ) ||
1231 !defined('YOURLS_FLOOD_DELAY_SECONDS')
1235 $ip = ( $ip ? yourls_sanitize_ip( $ip ) : yourls_get_IP() );
1237 // Don't throttle whitelist IPs
1238 if( defined( 'YOURLS_FLOOD_IP_WHITELIST' ) && YOURLS_FLOOD_IP_WHITELIST ) {
1239 $whitelist_ips = explode( ',', YOURLS_FLOOD_IP_WHITELIST );
1240 foreach( (array)$whitelist_ips as $whitelist_ip ) {
1241 $whitelist_ip = trim( $whitelist_ip );
1242 if ( $whitelist_ip == $ip )
1247 // Don't throttle logged in users
1248 if( yourls_is_private() ) {
1249 if( yourls_is_valid_user() === true )
1253 yourls_do_action( 'check_ip_flood', $ip );
1256 $table = YOURLS_DB_TABLE_URL;
1258 $lasttime = $ydb->get_var( "SELECT `timestamp` FROM $table WHERE `ip` = '$ip' ORDER BY `timestamp` DESC LIMIT 1" );
1261 $then = date( 'U', strtotime( $lasttime ) );
1262 if( ( $now - $then ) <= YOURLS_FLOOD_DELAY_SECONDS ) {
1264 yourls_do_action( 'ip_flood', $ip, $now - $then );
1265 yourls_die( yourls__( 'Too many URLs added too fast. Slow down please.' ), yourls__( 'Forbidden' ), 403 );
1273 * Check if YOURLS is installing
1278 function yourls_is_installing() {
1279 $installing = defined( 'YOURLS_INSTALLING' ) && YOURLS_INSTALLING == true;
1280 return yourls_apply_filter( 'is_installing', $installing );
1284 * Check if YOURLS is upgrading
1289 function yourls_is_upgrading() {
1290 $upgrading = defined( 'YOURLS_UPGRADING' ) && YOURLS_UPGRADING == true;
1291 return yourls_apply_filter( 'is_upgrading', $upgrading );
1296 * Check if YOURLS is installed
1298 * Checks property $ydb->installed that is created by yourls_get_all_options()
1300 * See inline comment for updating from 1.3 or prior.
1303 function yourls_is_installed() {
1305 $is_installed = ( property_exists( $ydb, 'installed' ) && $ydb->installed == true );
1306 return yourls_apply_filter( 'is_installed', $is_installed );
1308 /* Note: this test won't work on YOURLS 1.3 or older (Aug 2009...)
1309 Should someone complain that they cannot upgrade directly from
1310 1.3 to 1.7: first, laugh at them, then ask them to install 1.6 first.
1315 * Generate random string of (int)$length length and type $type (see function for details)
1318 function yourls_rnd_string ( $length = 5, $type = 0, $charlist = '' ) {
1320 $length = intval( $length );
1322 // define possible characters
1325 // custom char list, or comply to charset as defined in config
1327 $possible = $charlist ? $charlist : yourls_get_shorturl_charset() ;
1330 // no vowels to make no offending word, no 0/1/o/l to avoid confusion between letters & digits. Perfect for passwords.
1332 $possible = "23456789bcdfghjkmnpqrstvwxyz";
1335 // Same, with lower + upper
1337 $possible = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKMNPQRSTVWXYZ";
1340 // all letters, lowercase
1342 $possible = "abcdefghijklmnopqrstuvwxyz";
1345 // all letters, lowercase + uppercase
1347 $possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1350 // all digits & letters lowercase
1352 $possible = "0123456789abcdefghijklmnopqrstuvwxyz";
1355 // all digits & letters lowercase + uppercase
1357 $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
1363 while ($i < $length) {
1364 $str .= substr( $possible, mt_rand( 0, strlen( $possible )-1 ), 1 );
1368 return yourls_apply_filter( 'rnd_string', $str, $length, $type, $charlist );
1372 * Return salted string
1375 function yourls_salt( $string ) {
1376 $salt = defined('YOURLS_COOKIEKEY') ? YOURLS_COOKIEKEY : md5(__FILE__) ;
1377 return yourls_apply_filter( 'yourls_salt', md5 ($string . $salt), $string );
1381 * Add a query var to a URL and return URL. Completely stolen from WP.
1383 * Works with one of these parameter patterns:
1384 * array( 'var' => 'value' )
1385 * array( 'var' => 'value' ), $url
1387 * 'var', 'value', $url
1388 * If $url omitted, uses $_SERVER['REQUEST_URI']
1391 function yourls_add_query_arg() {
1393 if ( is_array( func_get_arg(0) ) ) {
1394 if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
1395 $uri = $_SERVER['REQUEST_URI'];
1397 $uri = @func_get_arg( 1 );
1399 if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
1400 $uri = $_SERVER['REQUEST_URI'];
1402 $uri = @func_get_arg( 2 );
1405 $uri = str_replace( '&', '&', $uri );
1408 if ( $frag = strstr( $uri, '#' ) )
1409 $uri = substr( $uri, 0, -strlen( $frag ) );
1413 if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
1414 $protocol = $matches[0];
1415 $uri = substr( $uri, strlen( $protocol ) );
1420 if ( strpos( $uri, '?' ) !== false ) {
1421 $parts = explode( '?', $uri, 2 );
1422 if ( 1 == count( $parts ) ) {
1426 $base = $parts[0] . '?';
1429 } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
1437 parse_str( $query, $qs );
1438 $qs = yourls_urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
1439 if ( is_array( func_get_arg( 0 ) ) ) {
1440 $kayvees = func_get_arg( 0 );
1441 $qs = array_merge( $qs, $kayvees );
1443 $qs[func_get_arg( 0 )] = func_get_arg( 1 );
1446 foreach ( (array) $qs as $k => $v ) {
1451 $ret = http_build_query( $qs );
1452 $ret = trim( $ret, '?' );
1453 $ret = preg_replace( '#=(&|$)#', '$1', $ret );
1454 $ret = $protocol . $base . $ret . $frag;
1455 $ret = rtrim( $ret, '?' );
1460 * Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg()
1463 function yourls_urlencode_deep( $value ) {
1464 $value = is_array( $value ) ? array_map( 'yourls_urlencode_deep', $value ) : urlencode( $value );
1469 * Remove arg from query. Opposite of yourls_add_query_arg. Stolen from WP.
1472 function yourls_remove_query_arg( $key, $query = false ) {
1473 if ( is_array( $key ) ) { // removing multiple keys
1474 foreach ( $key as $k )
1475 $query = yourls_add_query_arg( $k, false, $query );
1478 return yourls_add_query_arg( $key, false, $query );
1482 * Return a time-dependent string for nonce creation
1485 function yourls_tick() {
1486 return ceil( time() / YOURLS_NONCE_LIFE );
1490 * Create a time limited, action limited and user limited token
1493 function yourls_create_nonce( $action, $user = false ) {
1494 if( false == $user )
1495 $user = defined( 'YOURLS_USER' ) ? YOURLS_USER : '-1';
1496 $tick = yourls_tick();
1497 return substr( yourls_salt($tick . $action . $user), 0, 10 );
1501 * Create a nonce field for inclusion into a form
1504 function yourls_nonce_field( $action, $name = 'nonce', $user = false, $echo = true ) {
1505 $field = '<input type="hidden" id="'.$name.'" name="'.$name.'" value="'.yourls_create_nonce( $action, $user ).'" />';
1512 * Add a nonce to a URL. If URL omitted, adds nonce to current URL
1515 function yourls_nonce_url( $action, $url = false, $name = 'nonce', $user = false ) {
1516 $nonce = yourls_create_nonce( $action, $user );
1517 return yourls_add_query_arg( $name, $nonce, $url );
1521 * Check validity of a nonce (ie time span, user and action match).
1523 * Returns true if valid, dies otherwise (yourls_die() or die($return) if defined)
1524 * if $nonce is false or unspecified, it will use $_REQUEST['nonce']
1527 function yourls_verify_nonce( $action, $nonce = false, $user = false, $return = '' ) {
1529 if( false == $user )
1530 $user = defined( 'YOURLS_USER' ) ? YOURLS_USER : '-1';
1532 // get current nonce value
1533 if( false == $nonce && isset( $_REQUEST['nonce'] ) )
1534 $nonce = $_REQUEST['nonce'];
1536 // what nonce should be
1537 $valid = yourls_create_nonce( $action, $user );
1539 if( $nonce == $valid ) {
1544 yourls_die( yourls__( 'Unauthorized action or expired link' ), yourls__( 'Error' ), 403 );
1549 * Converts keyword into short link (prepend with YOURLS base URL)
1552 function yourls_link( $keyword = '' ) {
1553 $link = YOURLS_SITE . '/' . yourls_sanitize_keyword( $keyword );
1554 return yourls_apply_filter( 'yourls_link', $link, $keyword );
1558 * Converts keyword into stat link (prepend with YOURLS base URL, append +)
1561 function yourls_statlink( $keyword = '' ) {
1562 $link = YOURLS_SITE . '/' . yourls_sanitize_keyword( $keyword ) . '+';
1563 if( yourls_is_ssl() )
1564 $link = str_replace( 'http://', 'https://', $link );
1565 return yourls_apply_filter( 'yourls_statlink', $link, $keyword );
1569 * Check if we're in API mode. Returns bool
1572 function yourls_is_API() {
1573 if ( defined( 'YOURLS_API' ) && YOURLS_API == true )
1579 * Check if we're in Ajax mode. Returns bool
1582 function yourls_is_Ajax() {
1583 if ( defined( 'YOURLS_AJAX' ) && YOURLS_AJAX == true )
1589 * Check if we're in GO mode (yourls-go.php). Returns bool
1592 function yourls_is_GO() {
1593 if ( defined( 'YOURLS_GO' ) && YOURLS_GO == true )
1599 * Check if we're displaying stats infos (yourls-infos.php). Returns bool
1602 function yourls_is_infos() {
1603 if ( defined( 'YOURLS_INFOS' ) && YOURLS_INFOS == true )
1609 * Check if we'll need interface display function (ie not API or redirection)
1612 function yourls_has_interface() {
1613 if( yourls_is_API() or yourls_is_GO() )
1619 * Check if we're in the admin area. Returns bool
1622 function yourls_is_admin() {
1623 if ( defined( 'YOURLS_ADMIN' ) && YOURLS_ADMIN == true )
1629 * Check if the server seems to be running on Windows. Not exactly sure how reliable this is.
1632 function yourls_is_windows() {
1633 return defined( 'DIRECTORY_SEPARATOR' ) && DIRECTORY_SEPARATOR == '\\';
1637 * Check if SSL is required. Returns bool.
1640 function yourls_needs_ssl() {
1641 if ( defined('YOURLS_ADMIN_SSL') && YOURLS_ADMIN_SSL == true )
1647 * Return admin link, with SSL preference if applicable.
1650 function yourls_admin_url( $page = '' ) {
1651 $admin = YOURLS_SITE . '/admin/' . $page;
1652 if( yourls_is_ssl() or yourls_needs_ssl() )
1653 $admin = str_replace('http://', 'https://', $admin);
1654 return yourls_apply_filter( 'admin_url', $admin, $page );
1658 * Return YOURLS_SITE or URL under YOURLS setup, with SSL preference
1661 function yourls_site_url( $echo = true, $url = '' ) {
1662 $url = yourls_get_relative_url( $url );
1663 $url = trim( YOURLS_SITE . '/' . $url, '/' );
1665 // Do not enforce (checking yourls_need_ssl() ) but check current usage so it won't force SSL on non-admin pages
1666 if( yourls_is_ssl() )
1667 $url = str_replace( 'http://', 'https://', $url );
1668 $url = yourls_apply_filter( 'site_url', $url );
1675 * Check if SSL is used, returns bool. Stolen from WP.
1678 function yourls_is_ssl() {
1680 if ( isset( $_SERVER['HTTPS'] ) ) {
1681 if ( 'on' == strtolower( $_SERVER['HTTPS'] ) )
1683 if ( '1' == $_SERVER['HTTPS'] )
1685 } elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
1688 return yourls_apply_filter( 'is_ssl', $is_ssl );
1693 * Get a remote page <title>, return a string (either title or url)
1696 function yourls_get_remote_title( $url ) {
1697 // Allow plugins to short-circuit the whole function
1698 $pre = yourls_apply_filter( 'shunt_get_remote_title', false, $url );
1699 if ( false !== $pre )
1702 require_once( YOURLS_INC.'/functions-http.php' );
1704 $url = yourls_sanitize_url( $url );
1706 $title = $charset = false;
1708 $content = yourls_get_remote_content( $url );
1710 // If false, return url as title.
1711 // Todo: improve this with temporary title when shorturl_meta available?
1712 if( false === $content )
1715 if( $content !== false ) {
1717 if ( preg_match('/<title>(.*?)<\/title>/is', $content, $found ) ) {
1723 // <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
1724 if ( preg_match('/<meta[^>]*?charset=([^>]*?)\/?>/is', $content, $found ) ) {
1725 $charset = trim($found[1], '"\' ');
1730 // if title not found, guess if returned content was actually an error message
1731 if( $title == false && strpos( $content, 'Error' ) === 0 ) {
1735 if( $title == false )
1739 if( !yourls_seems_utf8( $title ) )
1740 $title = utf8_encode( $title );
1743 // Charset conversion. We use @ to remove warnings (mb_ functions are easily bitching about illegal chars)
1744 if( function_exists( 'mb_convert_encoding' ) ) {
1746 $title = @mb_convert_encoding( $title, 'UTF-8', $charset );
1748 $title = @mb_convert_encoding( $title, 'UTF-8' );
1752 // Remove HTML entities
1753 $title = html_entity_decode( $title, ENT_QUOTES, 'UTF-8' );
1755 // Strip out evil things
1756 $title = yourls_sanitize_title( $title );
1758 return yourls_apply_filter( 'get_remote_title', $title, $url );
1762 * Quick UA check for mobile devices. Return boolean.
1765 function yourls_is_mobile_device() {
1768 'android', 'blackberry', 'blazer',
1769 'compal', 'elaine', 'fennec', 'hiptop',
1770 'iemobile', 'iphone', 'ipod', 'ipad',
1771 'iris', 'kindle', 'opera mobi', 'opera mini',
1772 'palm', 'phone', 'pocket', 'psp', 'symbian',
1773 'treo', 'wap', 'windows ce', 'windows phone'
1776 // Current user-agent
1777 $current = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1780 $is_mobile = ( str_replace( $mobiles, '', $current ) != $current );
1781 return yourls_apply_filter( 'is_mobile_device', $is_mobile );
1785 * Get request in YOURLS base (eg in 'http://site.com/yourls/abcd' get 'abdc')
1788 function yourls_get_request() {
1789 // Allow plugins to short-circuit the whole function
1790 $pre = yourls_apply_filter( 'shunt_get_request', false );
1791 if ( false !== $pre )
1794 static $request = null;
1796 yourls_do_action( 'pre_get_request', $request );
1798 if( $request !== null )
1801 // Ignore protocol & www. prefix
1802 $root = str_replace( array( 'https://', 'http://', 'https://www.', 'http://www.' ), '', YOURLS_SITE );
1803 // Case insensitive comparison of the YOURLS root to match both http://Sho.rt/blah and http://sho.rt/blah
1804 $request = preg_replace( "!$root/!i", '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 1 );
1806 // Unless request looks like a full URL (ie request is a simple keyword) strip query string
1807 if( !preg_match( "@^[a-zA-Z]+://.+@", $request ) ) {
1808 $request = current( explode( '?', $request ) );
1811 return yourls_apply_filter( 'get_request', $request );
1815 * Change protocol to match current scheme used (http or https)
1818 function yourls_match_current_protocol( $url, $normal = 'http', $ssl = 'https' ) {
1819 if( yourls_is_ssl() )
1820 $url = str_replace( $normal, $ssl, $url );
1821 return yourls_apply_filter( 'match_current_protocol', $url );
1825 * Fix $_SERVER['REQUEST_URI'] variable for various setups. Stolen from WP.
1828 function yourls_fix_request_uri() {
1830 $default_server_values = array(
1831 'SERVER_SOFTWARE' => '',
1832 'REQUEST_URI' => '',
1834 $_SERVER = array_merge( $default_server_values, $_SERVER );
1836 // Fix for IIS when running with PHP ISAPI
1837 if ( empty( $_SERVER['REQUEST_URI'] ) || ( php_sapi_name() != 'cgi-fcgi' && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) ) {
1840 if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
1841 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
1843 // IIS Isapi_Rewrite
1844 else if ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) {
1845 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
1847 // Use ORIG_PATH_INFO if there is no PATH_INFO
1848 if ( !isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) )
1849 $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
1851 // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
1852 if ( isset( $_SERVER['PATH_INFO'] ) ) {
1853 if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
1854 $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
1856 $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
1859 // Append the query string if it exists and isn't null
1860 if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
1861 $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
1868 * Shutdown function, runs just before PHP shuts down execution. Stolen from WP
1871 function yourls_shutdown() {
1872 yourls_do_action( 'shutdown' );
1876 * Auto detect custom favicon in /user directory, fallback to YOURLS favicon, and echo/return its URL
1879 function yourls_favicon( $echo = true ) {
1880 static $favicon = null;
1881 if( $favicon !== null )
1885 // search for favicon.(gif|ico|png|jpg|svg)
1886 foreach( array( 'gif', 'ico', 'png', 'jpg', 'svg' ) as $ext ) {
1887 if( file_exists( YOURLS_USERDIR. '/favicon.' . $ext ) ) {
1888 $custom = 'favicon.' . $ext;
1894 $favicon = yourls_site_url( false, YOURLS_USERURL . '/' . $custom );
1896 $favicon = yourls_site_url( false ) . '/images/favicon.gif';
1904 * Check for maintenance mode. If yes, die. See yourls_maintenance_mode(). Stolen from WP.
1907 function yourls_check_maintenance_mode() {
1909 $file = YOURLS_ABSPATH . '/.maintenance' ;
1910 if ( !file_exists( $file ) || yourls_is_upgrading() || yourls_is_installing() )
1913 global $maintenance_start;
1916 // If the $maintenance_start timestamp is older than 10 minutes, don't die.
1917 if ( ( time() - $maintenance_start ) >= 600 )
1920 // Use any /user/maintenance.php file
1921 if( file_exists( YOURLS_USERDIR.'/maintenance.php' ) ) {
1922 include( YOURLS_USERDIR.'/maintenance.php' );
1926 // https://www.youtube.com/watch?v=Xw-m4jEY-Ns
1927 $title = yourls__( 'Service temporarily unavailable' );
1928 $message = yourls__( 'Our service is currently undergoing scheduled maintenance.' ) . "</p>\n<p>" .
1929 yourls__( 'Things should not last very long, thank you for your patience and please excuse the inconvenience' );
1930 yourls_die( $message, $title , 503 );
1935 * Return current admin page, or null if not an admin page
1937 * @return mixed string if admin page, null if not an admin page
1940 function yourls_current_admin_page() {
1941 if( yourls_is_admin() ) {
1942 $current = substr( yourls_get_request(), 6 );
1943 if( $current === false )
1944 $current = 'index.php'; // if current page is http://sho.rt/admin/ instead of http://sho.rt/admin/index.php
1952 * Check if a URL protocol is allowed
1954 * Checks a URL against a list of whitelisted protocols. Protocols must be defined with
1955 * their complete scheme name, ie 'stuff:' or 'stuff://' (for instance, 'mailto:' is a valid
1956 * protocol, 'mailto://' isn't, and 'http:' with no double slashed isn't either
1960 * @param string $url URL to be check
1961 * @param array $protocols Optional. Array of protocols, defaults to global $yourls_allowedprotocols
1962 * @return boolean true if protocol allowed, false otherwise
1964 function yourls_is_allowed_protocol( $url, $protocols = array() ) {
1965 if( ! $protocols ) {
1966 global $yourls_allowedprotocols;
1967 $protocols = $yourls_allowedprotocols;
1970 $protocol = yourls_get_protocol( $url );
1971 return yourls_apply_filter( 'is_allowed_protocol', in_array( $protocol, $protocols ), $url, $protocols );
1975 * Get protocol from a URL (eg mailto:, http:// ...)
1979 * @param string $url URL to be check
1980 * @return string Protocol, with slash slash if applicable. Empty string if no protocol
1982 function yourls_get_protocol( $url ) {
1983 preg_match( '!^[a-zA-Z0-9\+\.-]+:(//)?!', $url, $matches );
1985 http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax
1986 The scheme name consists of a sequence of characters beginning with a letter and followed by any
1987 combination of letters, digits, plus ("+"), period ("."), or hyphen ("-"). Although schemes are
1988 case-insensitive, the canonical form is lowercase and documents that specify schemes must do so
1989 with lowercase letters. It is followed by a colon (":").
1991 $protocol = ( isset( $matches[0] ) ? $matches[0] : '' );
1992 return yourls_apply_filter( 'get_protocol', $protocol, $url );
1996 * Get relative URL (eg 'abc' from 'http://sho.rt/abc')
1998 * Treat indifferently http & https. If a URL isn't relative to the YOURLS install, return it as is
1999 * or return empty string if $strict is true
2002 * @param string $url URL to relativize
2003 * @param bool $strict if true and if URL isn't relative to YOURLS install, return empty string
2004 * @return string URL
2006 function yourls_get_relative_url( $url, $strict = true ) {
2007 $url = yourls_sanitize_url( $url );
2009 // Remove protocols to make it easier
2010 $noproto_url = str_replace( 'https:', 'http:', $url );
2011 $noproto_site = str_replace( 'https:', 'http:', YOURLS_SITE );
2013 // Trim URL from YOURLS root URL : if no modification made, URL wasn't relative
2014 $_url = str_replace( $noproto_site . '/', '', $noproto_url );
2015 if( $_url == $noproto_url )
2016 $_url = ( $strict ? '' : $url );
2018 return yourls_apply_filter( 'get_relative_url', $_url, $url );
2022 * Marks a function as deprecated and informs when it has been used. Stolen from WP.
2024 * There is a hook deprecated_function that will be called that can be used
2025 * to get the backtrace up to what file and function called the deprecated
2028 * The current behavior is to trigger a user error if YOURLS_DEBUG is true.
2030 * This function is to be used in every function that is deprecated.
2033 * @uses yourls_do_action() Calls 'deprecated_function' and passes the function name, what to use instead,
2034 * and the version the function was deprecated in.
2035 * @uses yourls_apply_filters() Calls 'deprecated_function_trigger_error' and expects boolean value of true to do
2036 * trigger or false to not trigger error.
2038 * @param string $function The function that was called
2039 * @param string $version The version of WordPress that deprecated the function
2040 * @param string $replacement Optional. The function that should have been called
2042 function yourls_deprecated_function( $function, $version, $replacement = null ) {
2044 yourls_do_action( 'deprecated_function', $function, $replacement, $version );
2046 // Allow plugin to filter the output error trigger
2047 if ( YOURLS_DEBUG && yourls_apply_filters( 'deprecated_function_trigger_error', true ) ) {
2048 if ( ! is_null( $replacement ) )
2049 trigger_error( sprintf( yourls__('%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $function, $version, $replacement ) );
2051 trigger_error( sprintf( yourls__('%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $function, $version ) );
2056 * Return the value if not an empty string
2058 * Used with array_filter(), to remove empty keys but not keys with value 0 or false
2061 * @param mixed $val Value to test against ''
2062 * @return bool True if not an empty string
2064 function yourls_return_if_not_empty_string( $val ) {
2065 return( $val !== '' );