7 // Determine the allowed character set in short URLs
\r
8 function yourls_get_shorturl_charset() {
\r
9 static $charset = null;
\r
10 if( $charset !== null )
\r
13 if( !defined('YOURLS_URL_CONVERT') ) {
\r
14 $charset = '0123456789abcdefghijklmnopqrstuvwxyz';
\r
16 switch( YOURLS_URL_CONVERT ) {
\r
18 $charset = '0123456789abcdefghijklmnopqrstuvwxyz';
\r
21 case 64: // just because some people get this wrong in their config.php
\r
22 $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
\r
27 $charset = yourls_apply_filter( 'get_shorturl_charset', $charset );
\r
31 // function to convert an integer (1337) to a string (3jk).
\r
32 function yourls_int2string( $num, $chars = null ) {
\r
33 if( $chars == null )
\r
34 $chars = yourls_get_shorturl_charset();
\r
36 $len = strlen( $chars );
\r
37 while( $num >= $len ) {
\r
38 $mod = bcmod( $num, $len );
\r
39 $num = bcdiv( $num, $len );
\r
40 $string = $chars[$mod] . $string;
\r
42 $string = $chars[$num] . $string;
\r
44 return yourls_apply_filter( 'int2string', $string, $num, $chars );
\r
47 // function to convert a string (3jk) to an integer (1337)
\r
48 function yourls_string2int( $string, $chars = null ) {
\r
49 if( $chars == null )
\r
50 $chars = yourls_get_shorturl_charset();
\r
52 $string = strrev( $string );
\r
53 $baselen = strlen( $chars );
\r
54 $inputlen = strlen( $string );
\r
55 for ($i = 0; $i < $inputlen; $i++) {
\r
56 $index = strpos( $chars, $string[$i] );
\r
57 $integer = bcadd( $integer, bcmul( $index, bcpow( $baselen, $i ) ) );
\r
60 return yourls_apply_filter( 'string2int', $integer, $string, $chars );
\r
63 // return a unique(ish) hash for a string to be used as a valid HTML id
\r
64 function yourls_string2htmlid( $string ) {
\r
65 return yourls_apply_filter( 'string2htmlid', 'y'.abs( crc32( $string ) ) );
\r
68 // Make sure a link keyword (ie "1fv" as in "site.com/1fv") is valid.
\r
69 function yourls_sanitize_string( $string ) {
\r
70 // make a regexp pattern with the shorturl charset, and remove everything but this
\r
71 $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );
\r
72 $valid = substr(preg_replace('![^'.$pattern.']!', '', $string ), 0, 199);
\r
74 return yourls_apply_filter( 'sanitize_string', $valid, $string );
\r
77 // Make an optimized regexp pattern from a string of characters
\r
78 function yourls_make_regexp_pattern( $string ) {
\r
79 $pattern = preg_quote( $string, '-' ); // add - as an escaped characters -- this is fixed in PHP 5.3
\r
80 // TODO: replace char sequences by smart sequences such as 0-9, a-z, A-Z ... ?
\r
84 // Alias function. I was always getting it wrong.
\r
85 function yourls_sanitize_keyword( $keyword ) {
\r
86 return yourls_sanitize_string( $keyword );
\r
89 // Sanitize a page title. No HTML per W3C http://www.w3.org/TR/html401/struct/global.html#h-7.4.2
\r
90 function yourls_sanitize_title( $title ) {
\r
91 // TODO: make stronger Implement KSES?
\r
92 $title = strip_tags( $title );
\r
93 // Remove extra white space
\r
94 $title = preg_replace( "/\s+/", ' ', trim( $title ) );
\r
98 // Is an URL a short URL?
\r
99 function yourls_is_shorturl( $shorturl ) {
\r
100 // TODO: make sure this function evolves with the feature set.
\r
103 $keyword = preg_replace( '!^'.YOURLS_SITE.'/!', '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
\r
104 if( $keyword && $keyword == yourls_sanitize_string( $keyword ) && yourls_keyword_is_taken( $keyword ) ) {
\r
108 return yourls_apply_filter( 'is_shorturl', $is_short, $shorturl );
\r
111 // A few sanity checks on the URL
\r
112 function yourls_sanitize_url($url) {
\r
113 // make sure there's only one 'http://' at the beginning (prevents pasting a URL right after the default 'http://')
\r
114 $url = str_replace('http://http://', 'http://', $url);
\r
116 // make sure there's a protocol, add http:// if not
\r
117 if ( !preg_match('!^([a-zA-Z]+://)!', $url ) )
\r
118 $url = 'http://'.$url;
\r
120 $url = yourls_clean_url($url);
\r
122 return substr( $url, 0, 1999 );
\r
125 // Function to filter all invalid characters from a URL. Stolen from WP's clean_url()
\r
126 function yourls_clean_url( $url ) {
\r
127 $url = preg_replace('|[^a-z0-9-~+_.?\[\]\^#=!&;,/:%@$\|*\'"()\\x80-\\xff]|i', '', $url );
\r
128 $strip = array('%0d', '%0a', '%0D', '%0A');
\r
129 $url = yourls_deep_replace($strip, $url);
\r
130 $url = str_replace(';//', '://', $url);
\r
131 $url = str_replace('&', '&', $url); // Revert & not to break query strings
\r
136 // Perform a replacement while a string is found, eg $subject = '%0%0%0DDD', $search ='%0D' -> $result =''
\r
137 // Stolen from WP's _deep_replace
\r
138 function yourls_deep_replace($search, $subject){
\r
142 foreach( (array) $search as $val ) {
\r
143 while(strpos($subject, $val) !== false) {
\r
145 $subject = str_replace($val, '', $subject);
\r
153 // Make sure an integer is a valid integer (PHP's intval() limits to too small numbers)
\r
154 // TODO FIXME FFS: unused ?
\r
155 function yourls_sanitize_int($in) {
\r
156 return ( substr(preg_replace('/[^0-9]/', '', strval($in) ), 0, 20) );
\r
159 // Make sure a integer is safe
\r
160 // Note: this is not checking for integers, since integers on 32bits system are way too limited
\r
161 // TODO: find a way to validate as integer
\r
162 function yourls_intval($in) {
\r
163 return yourls_escape($in);
\r
167 function yourls_escape( $in ) {
\r
168 return mysql_real_escape_string($in);
\r
171 // Check to see if a given keyword is reserved (ie reserved URL or an existing page)
\r
173 function yourls_keyword_is_reserved( $keyword ) {
\r
174 global $yourls_reserved_URL;
\r
175 $keyword = yourls_sanitize_keyword( $keyword );
\r
178 if ( in_array( $keyword, $yourls_reserved_URL)
\r
179 or file_exists( YOURLS_ABSPATH ."/pages/$keyword.php" )
\r
180 or is_dir( YOURLS_ABSPATH ."/$keyword" )
\r
184 return yourls_apply_filter( 'keyword_is_reserved', $reserved, $keyword );
\r
187 // Function: Get IP Address. Returns a DB safe string.
\r
188 function yourls_get_IP() {
\r
189 if( !empty( $_SERVER['REMOTE_ADDR'] ) ) {
\r
190 $ip = $_SERVER['REMOTE_ADDR'];
\r
192 if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
\r
193 $ip = $_SERVER['HTTP_CLIENT_IP'];
\r
194 } else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
\r
195 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
\r
196 } else if(!empty($_SERVER['HTTP_VIA '])) {
\r
197 $ip = $_SERVER['HTTP_VIA '];
\r
201 return yourls_apply_filter( 'get_IP', yourls_sanitize_ip( $ip ) );
\r
204 // Sanitize an IP address
\r
205 function yourls_sanitize_ip( $ip ) {
\r
206 return preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
\r
209 // Make sure a date is m(m)/d(d)/yyyy, return false otherwise
\r
210 function yourls_sanitize_date( $date ) {
\r
211 if( !preg_match( '!^\d{1,2}/\d{1,2}/\d{4}$!' , $date ) ) {
\r
217 // Sanitize a date for SQL search. Return false if malformed input.
\r
218 function yourls_sanitize_date_for_sql( $date ) {
\r
219 if( !yourls_sanitize_date( $date ) )
\r
221 return date('Y-m-d', strtotime( $date ) );
\r
224 // Get next id a new link will have if no custom keyword provided
\r
225 function yourls_get_next_decimal() {
\r
226 return yourls_apply_filter( 'get_next_decimal', (int)yourls_get_option( 'next_id' ) );
\r
229 // Update id for next link with no custom keyword
\r
230 function yourls_update_next_decimal( $int = '' ) {
\r
231 $int = ( $int == '' ) ? yourls_get_next_decimal() + 1 : (int)$int ;
\r
232 $update = yourls_update_option( 'next_id', $int );
\r
233 yourls_do_action( 'update_next_decimal', $int, $update );
\r
237 // Delete a link in the DB
\r
238 function yourls_delete_link_by_keyword( $keyword ) {
\r
241 $table = YOURLS_DB_TABLE_URL;
\r
242 $keyword = yourls_sanitize_string( $keyword );
\r
243 $delete = $ydb->query("DELETE FROM `$table` WHERE `keyword` = '$keyword';");
\r
244 yourls_do_action( 'delete_link', $keyword, $delete );
\r
248 // SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting
\r
249 function yourls_insert_link_in_db( $url, $keyword, $title = '' ) {
\r
252 $url = addslashes( yourls_sanitize_url( $url ) );
\r
253 $keyword = addslashes( yourls_sanitize_keyword( $keyword ) );
\r
254 $title = addslashes( yourls_sanitize_title( $title ) );
\r
256 $table = YOURLS_DB_TABLE_URL;
\r
257 $timestamp = date('Y-m-d H:i:s');
\r
258 $ip = yourls_get_IP();
\r
259 $insert = $ydb->query("INSERT INTO `$table` VALUES('$keyword', '$url', '$title', '$timestamp', '$ip', 0);");
\r
261 yourls_do_action( 'insert_link', (bool)$insert, $url, $keyword, $title, $timestamp, $ip );
\r
263 return (bool)$insert;
\r
266 // Add a new link in the DB, either with custom keyword, or find one
\r
267 function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
\r
270 // Allow plugins to short-circuit the whole function
\r
271 $pre = yourls_apply_filter( 'shunt_add_new_link', false, $url, $keyword, $title );
\r
272 if ( false !== $pre )
\r
275 if ( !$url || $url == 'http://' || $url == 'https://' ) {
\r
276 $return['status'] = 'fail';
\r
277 $return['code'] = 'error:nourl';
\r
278 $return['message'] = 'Missing URL input';
\r
279 $return['errorCode'] = '400';
\r
280 return yourls_apply_filter( 'add_new_link_fail_nourl', $return, $url, $keyword, $title );
\r
283 // Prevent DB flood
\r
284 $ip = yourls_get_IP();
\r
285 yourls_check_IP_flood( $ip );
\r
287 // Prevent internal redirection loops: cannot shorten a shortened URL
\r
288 $url = yourls_escape( yourls_sanitize_url($url) );
\r
289 if( preg_match( '!^'.YOURLS_SITE.'/!', $url ) ) {
\r
290 if( yourls_is_shorturl( $url ) ) {
\r
291 $return['status'] = 'fail';
\r
292 $return['code'] = 'error:noloop';
\r
293 $return['message'] = 'URL is a short URL';
\r
294 $return['errorCode'] = '400';
\r
295 return yourls_apply_filter( 'add_new_link_fail_noloop', $return, $url, $keyword, $title );
\r
299 yourls_do_action( 'pre_add_new_link', $url, $keyword, $title );
\r
301 $table = YOURLS_DB_TABLE_URL;
\r
302 $strip_url = stripslashes($url);
\r
303 $url_exists = $ydb->get_row("SELECT * FROM `$table` WHERE `url` = '".$strip_url."';");
\r
306 // New URL : store it -- or: URL exists, but duplicates allowed
\r
307 if( !$url_exists || yourls_allow_duplicate_longurls() ) {
\r
309 if( isset( $title ) && !empty( $title ) ) {
\r
310 $title = yourls_sanitize_title( $title );
\r
312 $title = yourls_get_remote_title( $url );
\r
314 $title = yourls_apply_filter( 'add_new_title', $title, $url, $keyword );
\r
316 // Custom keyword provided
\r
319 yourls_do_action( 'add_new_link_custom_keyword', $url, $keyword, $title );
\r
321 $keyword = yourls_escape( yourls_sanitize_string($keyword) );
\r
322 $keyword = yourls_apply_filter( 'custom_keyword', $keyword, $url, $title );
\r
323 if ( !yourls_keyword_is_free($keyword) ) {
\r
324 // This shorturl either reserved or taken already
\r
325 $return['status'] = 'fail';
\r
326 $return['code'] = 'error:keyword';
\r
327 $return['message'] = 'Short URL '.$keyword.' already exists in database or is reserved';
\r
329 // all clear, store !
\r
330 yourls_insert_link_in_db( $url, $keyword, $title );
\r
331 $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => date('Y-m-d H:i:s'), 'ip' => $ip );
\r
332 $return['status'] = 'success';
\r
333 $return['message'] = yourls_trim_long_string( $strip_url ).' added to database';
\r
334 $return['title'] = $title;
\r
335 $return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
\r
336 $return['shorturl'] = YOURLS_SITE .'/'. $keyword;
\r
339 // Create random keyword
\r
342 yourls_do_action( 'add_new_link_create_keyword', $url, $keyword, $title );
\r
344 $timestamp = date('Y-m-d H:i:s');
\r
345 $id = yourls_get_next_decimal();
\r
348 $keyword = yourls_int2string( $id );
\r
349 $keyword = yourls_apply_filter( 'random_keyword', $keyword, $url, $title );
\r
350 $free = yourls_keyword_is_free($keyword);
\r
351 $add_url = @yourls_insert_link_in_db( $url, $keyword, $title );
\r
352 $ok = ($free && $add_url);
\r
353 if ( $ok === false && $add_url === 1 ) {
\r
354 // we stored something, but shouldn't have (ie reserved id)
\r
355 $delete = yourls_delete_link_by_keyword( $keyword );
\r
356 $return['extra_info'] .= '(deleted '.$keyword.')';
\r
358 // everything ok, populate needed vars
\r
359 $return['url'] = array('keyword' => $keyword, 'url' => $strip_url, 'title' => $title, 'date' => $timestamp, 'ip' => $ip );
\r
360 $return['status'] = 'success';
\r
361 $return['message'] = yourls_trim_long_string( $strip_url ).' added to database';
\r
362 $return['title'] = $title;
\r
363 $return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
\r
364 $return['shorturl'] = YOURLS_SITE .'/'. $keyword;
\r
368 @yourls_update_next_decimal($id);
\r
371 // URL was already stored
\r
374 yourls_do_action( 'add_new_link_already_stored', $url, $keyword, $title );
\r
376 $return['status'] = 'fail';
\r
377 $return['code'] = 'error:url';
\r
378 $return['url'] = array( 'keyword' => $keyword, 'url' => $strip_url, 'title' => $url_exists->title, 'date' => $url_exists->timestamp, 'ip' => $url_exists->ip, 'clicks' => $url_exists->clicks );
\r
379 $return['message'] = yourls_trim_long_string( $strip_url ).' already exists in database';
\r
380 $return['title'] = $url_exists->title;
\r
381 $return['shorturl'] = YOURLS_SITE .'/'. $url_exists->keyword;
\r
384 yourls_do_action( 'post_add_new_link', $url, $keyword, $title );
\r
386 $return['statusCode'] = 200; // regardless of result, this is still a valid request
\r
387 return yourls_apply_filter( 'add_new_link', $return, $url, $keyword, $title );
\r
392 function yourls_edit_link( $url, $keyword, $newkeyword='', $title='' ) {
\r
395 $table = YOURLS_DB_TABLE_URL;
\r
396 $url = yourls_escape(yourls_sanitize_url($url));
\r
397 $keyword = yourls_escape(yourls_sanitize_string( $keyword ));
\r
398 $title = yourls_escape(yourls_sanitize_title( $title ));
\r
399 $newkeyword = yourls_escape(yourls_sanitize_string( $newkeyword ));
\r
400 $strip_url = stripslashes($url);
\r
401 $strip_title = stripslashes($title);
\r
402 $old_url = $ydb->get_var("SELECT `url` FROM `$table` WHERE `keyword` = '$keyword';");
\r
404 // Check if new URL is not here already
\r
405 if ( $old_url != $url && !yourls_allow_duplicate_longurls() ) {
\r
406 $new_url_already_there = intval($ydb->get_var("SELECT COUNT(keyword) FROM `$table` WHERE `url` = '$strip_url';"));
\r
408 $new_url_already_there = false;
\r
411 // Check if the new keyword is not here already
\r
412 if ( $newkeyword != $keyword ) {
\r
413 $keyword_is_ok = yourls_keyword_is_free( $newkeyword );
\r
415 $keyword_is_ok = true;
\r
418 yourls_do_action( 'pre_edit_link', $url, $keyword, $newkeyword, $new_url_already_there, $keyword_is_ok );
\r
420 // All clear, update
\r
421 if ( ( !$new_url_already_there || yourls_allow_duplicate_longurls() ) && $keyword_is_ok ) {
\r
422 $update_url = $ydb->query("UPDATE `$table` SET `url` = '$url', `keyword` = '$newkeyword', `title` = '$title' WHERE `keyword` = '$keyword';");
\r
423 if( $update_url ) {
\r
424 $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 ) );
\r
425 $return['status'] = 'success';
\r
426 $return['message'] = 'Link updated in database';
\r
428 $return['status'] = 'fail';
\r
429 $return['message'] = 'Error updating '. yourls_trim_long_string( $strip_url ).' (Short URL: '.$keyword.') to database';
\r
434 $return['status'] = 'fail';
\r
435 $return['message'] = 'URL or keyword already exists in database';
\r
438 return yourls_apply_filter( 'edit_link', $return, $url, $keyword, $newkeyword, $title, $new_url_already_there, $keyword_is_ok );
\r
441 // Update a title link (no checks for duplicates etc..)
\r
442 function yourls_edit_link_title( $keyword, $title ) {
\r
445 $keyword = yourls_escape( yourls_sanitize_keyword( $keyword ) );
\r
446 $title = yourls_escape( yourls_sanitize_title( $title ) );
\r
448 $table = YOURLS_DB_TABLE_URL;
\r
449 $update = $ydb->query("UPDATE `$table` SET `title` = '$title' WHERE `keyword` = '$keyword';");
\r
455 // Check if keyword id is free (ie not already taken, and not reserved). Return bool.
\r
456 function yourls_keyword_is_free( $keyword ) {
\r
458 if ( yourls_keyword_is_reserved( $keyword ) or yourls_keyword_is_taken( $keyword ) )
\r
461 return yourls_apply_filter( 'keyword_is_free', $free, $keyword );
\r
464 // Check if a keyword is taken (ie there is already a short URL with this id). Return bool.
\r
465 function yourls_keyword_is_taken( $keyword ) {
\r
467 $keyword = yourls_sanitize_keyword( $keyword );
\r
469 $table = YOURLS_DB_TABLE_URL;
\r
470 $already_exists = $ydb->get_var("SELECT COUNT(`keyword`) FROM `$table` WHERE `keyword` = '$keyword';");
\r
471 if ( $already_exists )
\r
474 return yourls_apply_filter( 'keyword_is_taken', $taken, $keyword );
\r
479 function yourls_page( $page ) {
\r
480 $include = YOURLS_ABSPATH . "/pages/$page.php";
\r
481 if (!file_exists($include)) {
\r
482 yourls_die("Page '$page' not found", 'Not found', 404);
\r
484 yourls_do_action( 'pre_page', $page );
\r
486 yourls_do_action( 'post_page', $page );
\r
491 function yourls_db_connect() {
\r
494 if (!defined('YOURLS_DB_USER')
\r
495 or !defined('YOURLS_DB_PASS')
\r
496 or !defined('YOURLS_DB_NAME')
\r
497 or !defined('YOURLS_DB_HOST')
\r
498 or !class_exists('ezSQL_mysql')
\r
499 ) yourls_die ('DB config missing, or could not find DB class', 'Fatal error', 503);
\r
501 // Are we standalone or in the WordPress environment?
\r
502 if ( class_exists('wpdb') ) {
\r
503 $ydb = new wpdb(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
\r
505 $ydb = new ezSQL_mysql(YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST);
\r
507 if ( $ydb->last_error )
\r
508 yourls_die( $ydb->last_error, 'Fatal error', 503 );
\r
510 if ( defined('YOURLS_DEBUG') && YOURLS_DEBUG === true )
\r
511 $ydb->show_errors = true;
\r
516 // Return XML output.
\r
517 function yourls_xml_encode($array) {
\r
518 require_once(YOURLS_INC.'/functions-xml.php');
\r
519 $converter= new yourls_array2xml;
\r
520 return $converter->array2xml($array);
\r
523 // 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
\r
524 function yourls_get_keyword_infos( $keyword, $use_cache = true ) {
\r
526 $keyword = yourls_sanitize_string( $keyword );
\r
528 yourls_do_action( 'pre_get_keyword', $keyword, $use_cache );
\r
530 if( isset( $ydb->infos[$keyword] ) && $use_cache == true ) {
\r
531 return yourls_apply_filter( 'get_keyword_infos', $ydb->infos[$keyword], $keyword );
\r
534 yourls_do_action( 'get_keyword_not_cached', $keyword );
\r
536 $table = YOURLS_DB_TABLE_URL;
\r
537 $infos = $ydb->get_row("SELECT * FROM `$table` WHERE `keyword` = '$keyword'");
\r
540 $infos = (array)$infos;
\r
541 $ydb->infos[$keyword] = $infos;
\r
543 $ydb->infos[$keyword] = false;
\r
546 return yourls_apply_filter( 'get_keyword_infos', $ydb->infos[$keyword], $keyword );
\r
549 // Return (string) selected information associated with a keyword. Optional $notfound = string default message if nothing found
\r
550 function yourls_get_keyword_info( $keyword, $field, $notfound = false ) {
\r
552 // Allow plugins to short-circuit the whole function
\r
553 $pre = yourls_apply_filter( 'shunt_get_keyword_info', false, $keyword, $field, $notfound );
\r
554 if ( false !== $pre )
\r
557 $keyword = yourls_sanitize_string( $keyword );
\r
558 $infos = yourls_get_keyword_infos( $keyword );
\r
560 $return = $notfound;
\r
561 if ( isset($infos[$field]) && $infos[$field] !== false )
\r
562 $return = $infos[$field];
\r
564 return yourls_apply_filter( 'get_keyword_info', $return, $keyword, $field, $notfound );
\r
567 // Return title associated with keyword. Optional $notfound = string default message if nothing found
\r
568 function yourls_get_keyword_title( $keyword, $notfound = false ) {
\r
569 return yourls_get_keyword_info( $keyword, 'title', $notfound );
\r
572 // Return long URL associated with keyword. Optional $notfound = string default message if nothing found
\r
573 function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
\r
574 return yourls_get_keyword_info( $keyword, 'url', $notfound );
\r
577 // Return number of clicks on a keyword. Optional $notfound = string default message if nothing found
\r
578 function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
\r
579 return yourls_get_keyword_info( $keyword, 'clicks', $notfound );
\r
582 // Return IP that added a keyword. Optional $notfound = string default message if nothing found
\r
583 function yourls_get_keyword_IP( $keyword, $notfound = false ) {
\r
584 return yourls_get_keyword_info( $keyword, 'ip', $notfound );
\r
587 // Return timestamp associated with a keyword. Optional $notfound = string default message if nothing found
\r
588 function yourls_get_keyword_timestamp( $keyword, $notfound = false ) {
\r
589 return yourls_get_keyword_info( $keyword, 'timestamp', $notfound );
\r
592 // Update click count on a short URL. Return 0/1 for error/success.
\r
593 function yourls_update_clicks( $keyword ) {
\r
594 // Allow plugins to short-circuit the whole function
\r
595 $pre = yourls_apply_filter( 'shunt_update_clicks', false, $keyword );
\r
596 if ( false !== $pre )
\r
600 $keyword = yourls_sanitize_string( $keyword );
\r
601 $table = YOURLS_DB_TABLE_URL;
\r
602 $update = $ydb->query("UPDATE `$table` SET `clicks` = clicks + 1 WHERE `keyword` = '$keyword'");
\r
603 yourls_do_action( 'update_clicks', $keyword, $update );
\r
607 // Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
\r
608 function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
\r
611 switch( $filter ) {
\r
613 $sort_by = 'clicks';
\r
614 $sort_order = 'asc';
\r
617 $sort_by = 'timestamp';
\r
618 $sort_order = 'desc';
\r
622 $sort_by = 'RAND()';
\r
627 $sort_by = 'clicks';
\r
628 $sort_order = 'desc';
\r
633 $limit = intval( $limit );
\r
634 $start = intval( $start );
\r
635 if ( $limit > 0 ) {
\r
637 $table_url = YOURLS_DB_TABLE_URL;
\r
638 $results = $ydb->get_results("SELECT * FROM `$table_url` WHERE 1=1 ORDER BY `$sort_by` $sort_order LIMIT $start, $limit;");
\r
643 foreach ( (array)$results as $res) {
\r
644 $return['links']['link_'.$i++] = array(
\r
645 'shorturl' => YOURLS_SITE .'/'. $res->keyword,
\r
646 'url' => $res->url,
\r
647 'title' => $res->title,
\r
648 'timestamp'=> $res->timestamp,
\r
650 'clicks' => $res->clicks,
\r
655 $return['stats'] = yourls_get_db_stats();
\r
657 $return['statusCode'] = 200;
\r
659 return yourls_apply_filter( 'get_stats', $return, $filter, $limit, $start );
\r
662 // Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
\r
663 function yourls_get_link_stats( $shorturl ) {
\r
666 $table_url = YOURLS_DB_TABLE_URL;
\r
667 $res = $ydb->get_row("SELECT * FROM `$table_url` WHERE keyword = '$shorturl';");
\r
671 // non existent link
\r
673 'statusCode' => 404,
\r
674 'message' => 'Error: short URL not found',
\r
678 'statusCode' => 200,
\r
679 'message' => 'success',
\r
681 'shorturl' => YOURLS_SITE .'/'. $res->keyword,
\r
682 'url' => $res->url,
\r
683 'title' => $res->title,
\r
684 'timestamp'=> $res->timestamp,
\r
686 'clicks' => $res->clicks,
\r
691 return yourls_apply_filter( 'get_link_stats', $return, $shorturl );
\r
694 // Return array for API stat requests
\r
695 function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
\r
696 $return = yourls_get_stats( $filter, $limit, $start );
\r
697 $return['simple'] = 'Need either XML or JSON format for stats';
\r
698 $return['message'] = 'success';
\r
699 return yourls_apply_filter( 'api_stats', $return, $filter, $limit, $start );
\r
702 // Return array for counts of shorturls and clicks
\r
703 function yourls_api_db_stats() {
\r
704 $return = yourls_get_db_stats();
\r
705 $return['simple'] = 'Need either XML or JSON format for stats';
\r
706 $return['message'] = 'success';
\r
707 return yourls_apply_filter( 'api_db_stats', $return );
\r
710 // Return array for API stat requests
\r
711 function yourls_api_url_stats($shorturl) {
\r
712 $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
\r
713 $keyword = yourls_sanitize_string( $keyword );
\r
715 $return = yourls_get_link_stats( $keyword );
\r
716 $return['simple'] = 'Need either XML or JSON format for stats';
\r
717 return yourls_apply_filter( 'api_url_stats', $return, $shorturl );
\r
720 // Expand short url to long url
\r
721 function yourls_api_expand( $shorturl ) {
\r
722 $keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
\r
723 $keyword = yourls_sanitize_string( $keyword );
\r
725 $longurl = yourls_get_keyword_longurl( $keyword );
\r
729 'keyword' => $keyword,
\r
730 'shorturl' => YOURLS_SITE . "/$keyword",
\r
731 'longurl' => $longurl,
\r
732 'simple' => $longurl,
\r
733 'message' => 'success',
\r
734 'statusCode' => 200,
\r
738 'keyword' => $keyword,
\r
739 'simple' => 'not found',
\r
740 'message' => 'Error: short URL not found',
\r
741 'errorCode' => 404,
\r
745 return yourls_apply_filter( 'api_expand', $return, $shorturl );
\r
749 // Get total number of URLs and sum of clicks. Input: optional "AND WHERE" clause. Returns array
\r
750 function yourls_get_db_stats( $where = '' ) {
\r
752 $table_url = YOURLS_DB_TABLE_URL;
\r
754 $totals = $ydb->get_row("SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM `$table_url` WHERE 1=1 $where");
\r
755 $return = array( 'total_links' => $totals->count, 'total_clicks' => $totals->sum );
\r
757 return yourls_apply_filter( 'get_db_stats', $return, $where );
\r
760 // Return API result. Dies after this
\r
761 function yourls_api_output( $mode, $return ) {
\r
762 if( isset( $return['simple'] ) ) {
\r
763 $simple = $return['simple'];
\r
764 unset( $return['simple'] );
\r
767 yourls_do_action( 'pre_api_output', $mode, $return );
\r
771 header('Content-type: application/json');
\r
772 echo json_encode($return);
\r
776 header('Content-type: application/xml');
\r
777 echo yourls_xml_encode($return);
\r
782 if( isset( $simple ) )
\r
787 yourls_do_action( 'api_output', $mode, $return );
\r
792 // Get number of SQL queries performed
\r
793 function yourls_get_num_queries() {
\r
796 return yourls_apply_filter( 'get_num_queries', $ydb->num_queries );
\r
799 // Returns a sanitized a user agent string. Given what I found on http://www.user-agents.org/ it should be OK.
\r
800 function yourls_get_user_agent() {
\r
801 if ( !isset( $_SERVER['HTTP_USER_AGENT'] ) )
\r
804 $ua = strip_tags( html_entity_decode( $_SERVER['HTTP_USER_AGENT'] ));
\r
805 $ua = preg_replace('![^0-9a-zA-Z\':., /{}\(\)\[\]\+@&\!\?;_\-=~\*\#]!', '', $ua );
\r
807 return yourls_apply_filter( 'get_user_agent', substr( $ua, 0, 254 ) );
\r
810 // Redirect to another page
\r
811 function yourls_redirect( $location, $code = 301 ) {
\r
812 yourls_do_action( 'pre_redirect', $location, $code );
\r
813 $location = yourls_apply_filter( 'redirect', $location, $code );
\r
814 // Redirect, either properly if possible, or via Javascript otherwise
\r
815 if( !headers_sent() ) {
\r
816 yourls_status_header( $code );
\r
817 header( "Location: $location" );
\r
819 yourls_redirect_javascript( $location );
\r
824 // Set HTTP status header
\r
825 function yourls_status_header( $code = 200 ) {
\r
826 if( headers_sent() )
\r
829 $protocol = $_SERVER["SERVER_PROTOCOL"];
\r
830 if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
\r
831 $protocol = 'HTTP/1.0';
\r
833 $code = intval( $code );
\r
834 $desc = yourls_get_HTTP_status( $code );
\r
836 @header ("$protocol $code $desc"); // This causes problems on IIS and some FastCGI setups
\r
837 yourls_do_action( 'status_header', $code );
\r
840 // 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)
\r
841 function yourls_redirect_javascript( $location, $dontwait = true ) {
\r
842 yourls_do_action( 'pre_redirect_javascript', $location, $dontwait );
\r
843 $location = yourls_apply_filter( 'redirect_javascript', $location, $dontwait );
\r
846 <script type="text/javascript">
\r
847 window.location="$location";
\r
849 <small>(if you are not redirected after 10 seconds, please <a href="$location">click here</a>)</small>
\r
853 <p>Please <a href="$location">click here</a></p>
\r
856 yourls_do_action( 'post_redirect_javascript', $location );
\r
859 // Return a HTTP status code
\r
860 function yourls_get_HTTP_status( $code ) {
\r
861 $code = intval( $code );
\r
862 $headers_desc = array(
\r
864 101 => 'Switching Protocols',
\r
865 102 => 'Processing',
\r
870 203 => 'Non-Authoritative Information',
\r
871 204 => 'No Content',
\r
872 205 => 'Reset Content',
\r
873 206 => 'Partial Content',
\r
874 207 => 'Multi-Status',
\r
877 300 => 'Multiple Choices',
\r
878 301 => 'Moved Permanently',
\r
880 303 => 'See Other',
\r
881 304 => 'Not Modified',
\r
882 305 => 'Use Proxy',
\r
884 307 => 'Temporary Redirect',
\r
886 400 => 'Bad Request',
\r
887 401 => 'Unauthorized',
\r
888 402 => 'Payment Required',
\r
889 403 => 'Forbidden',
\r
890 404 => 'Not Found',
\r
891 405 => 'Method Not Allowed',
\r
892 406 => 'Not Acceptable',
\r
893 407 => 'Proxy Authentication Required',
\r
894 408 => 'Request Timeout',
\r
897 411 => 'Length Required',
\r
898 412 => 'Precondition Failed',
\r
899 413 => 'Request Entity Too Large',
\r
900 414 => 'Request-URI Too Long',
\r
901 415 => 'Unsupported Media Type',
\r
902 416 => 'Requested Range Not Satisfiable',
\r
903 417 => 'Expectation Failed',
\r
904 422 => 'Unprocessable Entity',
\r
906 424 => 'Failed Dependency',
\r
907 426 => 'Upgrade Required',
\r
909 500 => 'Internal Server Error',
\r
910 501 => 'Not Implemented',
\r
911 502 => 'Bad Gateway',
\r
912 503 => 'Service Unavailable',
\r
913 504 => 'Gateway Timeout',
\r
914 505 => 'HTTP Version Not Supported',
\r
915 506 => 'Variant Also Negotiates',
\r
916 507 => 'Insufficient Storage',
\r
917 510 => 'Not Extended'
\r
920 if ( isset( $headers_desc[$code] ) )
\r
921 return $headers_desc[$code];
\r
927 // Log a redirect (for stats)
\r
928 function yourls_log_redirect( $keyword ) {
\r
929 // Allow plugins to short-circuit the whole function
\r
930 $pre = yourls_apply_filter( 'shunt_log_redirect', false, $keyword );
\r
931 if ( false !== $pre )
\r
934 if ( !yourls_do_log_redirect() )
\r
938 $table = YOURLS_DB_TABLE_LOG;
\r
940 $keyword = yourls_sanitize_string( $keyword );
\r
941 $referrer = ( isset( $_SERVER['HTTP_REFERER'] ) ? yourls_sanitize_url( $_SERVER['HTTP_REFERER'] ) : 'direct' );
\r
942 $ua = yourls_get_user_agent();
\r
943 $ip = yourls_get_IP();
\r
944 $location = yourls_geo_ip_to_countrycode( $ip );
\r
946 return $ydb->query( "INSERT INTO `$table` (click_time, shorturl, referrer, user_agent, ip_address, country_code) VALUES (NOW(), '$keyword', '$referrer', '$ua', '$ip', '$location')" );
\r
949 // Check if we want to not log redirects (for stats)
\r
950 function yourls_do_log_redirect() {
\r
951 return ( !defined('YOURLS_NOSTATS') || YOURLS_NOSTATS != true );
\r
954 // Converts an IP to a 2 letter country code, using GeoIP database if available in includes/geo/
\r
955 function yourls_geo_ip_to_countrycode( $ip = '', $default = '' ) {
\r
956 // Allow plugins to short-circuit the Geo IP API
\r
957 $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
\r
958 if ( false !== $location )
\r
961 if ( !file_exists( YOURLS_INC.'/geo/GeoIP.dat') || !file_exists( YOURLS_INC.'/geo/geoip.inc') )
\r
965 $ip = yourls_get_IP();
\r
967 require_once( YOURLS_INC.'/geo/geoip.inc') ;
\r
968 $gi = geoip_open( YOURLS_INC.'/geo/GeoIP.dat', GEOIP_STANDARD);
\r
969 $location = geoip_country_code_by_addr($gi, $ip);
\r
972 return yourls_apply_filter( 'geo_ip_to_countrycode', $location, $ip, $default );
\r
975 // Converts a 2 letter country code to long name (ie AU -> Australia)
\r
976 function yourls_geo_countrycode_to_countryname( $code ) {
\r
977 // Allow plugins to short-circuit the Geo IP API
\r
978 $country = yourls_apply_filter( 'shunt_geo_countrycode_to_countryname', false, $code );
\r
979 if ( false !== $country )
\r
982 // Load the Geo class if not already done
\r
983 if( !class_exists('GeoIP') ) {
\r
984 $temp = yourls_geo_ip_to_countrycode('127.0.0.1');
\r
987 if( class_exists('GeoIP') ) {
\r
989 $id = $geo->GEOIP_COUNTRY_CODE_TO_NUMBER[$code];
\r
990 $long = $geo->GEOIP_COUNTRY_NAMES[$id];
\r
997 // Return flag URL from 2 letter country code
\r
998 function yourls_geo_get_flag( $code ) {
\r
999 if( file_exists( YOURLS_INC.'/geo/flags/flag_'.strtolower($code).'.gif' ) ) {
\r
1000 $img = yourls_match_current_protocol( YOURLS_SITE.'/includes/geo/flags/flag_'.(strtolower($code)).'.gif' );
\r
1004 return yourls_apply_filter( 'geo_get_flag', $img, $code );
\r
1008 // Check if an upgrade is needed
\r
1009 function yourls_upgrade_is_needed() {
\r
1010 // check YOURLS_DB_VERSION exist && match values stored in YOURLS_DB_TABLE_OPTIONS
\r
1011 list( $currentver, $currentsql ) = yourls_get_current_version_from_sql();
\r
1012 if( $currentsql < YOURLS_DB_VERSION )
\r
1018 // Get current version & db version as stored in the options DB. Prior to 1.4 there's no option table.
\r
1019 function yourls_get_current_version_from_sql() {
\r
1020 $currentver = yourls_get_option( 'version' );
\r
1021 $currentsql = yourls_get_option( 'db_version' );
\r
1023 // Values if version is 1.3
\r
1024 if( !$currentver )
\r
1025 $currentver = '1.3';
\r
1026 if( !$currentsql )
\r
1027 $currentsql = '100';
\r
1029 return array( $currentver, $currentsql);
\r
1032 // Read an option from DB (or from cache if available). Return value or $default if not found
\r
1033 function yourls_get_option( $option_name, $default = false ) {
\r
1036 // Allow plugins to short-circuit options
\r
1037 $pre = yourls_apply_filter( 'shunt_option_'.$option_name, false );
\r
1038 if ( false !== $pre )
\r
1041 if ( !isset( $ydb->option[$option_name] ) ) {
\r
1042 $table = YOURLS_DB_TABLE_OPTIONS;
\r
1043 $option_name = yourls_escape( $option_name );
\r
1044 $row = $ydb->get_row( "SELECT `option_value` FROM `$table` WHERE `option_name` = '$option_name' LIMIT 1" );
\r
1045 if ( is_object( $row) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
\r
1046 $value = $row->option_value;
\r
1047 } else { // option does not exist, so we must cache its non-existence
\r
1048 $value = $default;
\r
1050 $ydb->option[$option_name] = yourls_maybe_unserialize( $value );
\r
1053 return yourls_apply_filter( 'get_option_'.$option_name, $ydb->option[$option_name] );
\r
1056 // Read all options from DB at once
\r
1057 function yourls_get_all_options() {
\r
1059 $table = YOURLS_DB_TABLE_OPTIONS;
\r
1061 $allopt = $ydb->get_results("SELECT `option_name`, `option_value` FROM `$table` WHERE 1=1");
\r
1063 foreach( (array)$allopt as $option ) {
\r
1064 $ydb->option[$option->option_name] = yourls_maybe_unserialize( $option->option_value );
\r
1068 // Update (add if doesn't exist) an option to DB
\r
1069 function yourls_update_option( $option_name, $newvalue ) {
\r
1071 $table = YOURLS_DB_TABLE_OPTIONS;
\r
1073 $safe_option_name = yourls_escape( $option_name );
\r
1075 $oldvalue = yourls_get_option( $safe_option_name );
\r
1077 // If the new and old values are the same, no need to update.
\r
1078 if ( $newvalue === $oldvalue )
\r
1081 if ( false === $oldvalue ) {
\r
1082 yourls_add_option( $option_name, $newvalue );
\r
1086 $_newvalue = yourls_escape( yourls_maybe_serialize( $newvalue ) );
\r
1088 yourls_do_action( 'update_option', $option_name, $oldvalue, $newvalue );
\r
1090 $ydb->query( "UPDATE `$table` SET `option_value` = '$_newvalue' WHERE `option_name` = '$option_name'");
\r
1092 if ( $ydb->rows_affected == 1 ) {
\r
1093 $ydb->option[$option_name] = $newvalue;
\r
1099 // Add an option to the DB
\r
1100 function yourls_add_option( $name, $value = '' ) {
\r
1102 $table = YOURLS_DB_TABLE_OPTIONS;
\r
1103 $safe_name = yourls_escape( $name );
\r
1105 // Make sure the option doesn't already exist
\r
1106 if ( false !== yourls_get_option( $safe_name ) )
\r
1109 $_value = yourls_escape( yourls_maybe_serialize( $value ) );
\r
1111 yourls_do_action( 'add_option', $safe_name, $_value );
\r
1113 $ydb->query( "INSERT INTO `$table` (`option_name`, `option_value`) VALUES ('$name', '$_value')" );
\r
1114 $ydb->option[$name] = $value;
\r
1119 // Delete an option from the DB
\r
1120 function yourls_delete_option( $name ) {
\r
1122 $table = YOURLS_DB_TABLE_OPTIONS;
\r
1123 $name = yourls_escape( $name );
\r
1125 // Get the ID, if no ID then return
\r
1126 $option = $ydb->get_row( "SELECT option_id FROM `$table` WHERE `option_name` = '$name'" );
\r
1127 if ( is_null($option) || !$option->option_id )
\r
1130 yourls_do_action( 'delete_option', $option_name );
\r
1132 $ydb->query( "DELETE FROM `$table` WHERE `option_name` = '$name'" );
\r
1138 // Serialize data if needed. Stolen from WordPress
\r
1139 function yourls_maybe_serialize( $data ) {
\r
1140 if ( is_array( $data ) || is_object( $data ) )
\r
1141 return serialize( $data );
\r
1143 if ( yourls_is_serialized( $data ) )
\r
1144 return serialize( $data );
\r
1149 // Check value to find if it was serialized. Stolen from WordPress
\r
1150 function yourls_is_serialized( $data ) {
\r
1151 // if it isn't a string, it isn't serialized
\r
1152 if ( !is_string( $data ) )
\r
1154 $data = trim( $data );
\r
1155 if ( 'N;' == $data )
\r
1157 if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
\r
1159 switch ( $badions[1] ) {
\r
1163 if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
\r
1169 if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
\r
1176 // Unserialize value only if it was serialized. Stolen from WP
\r
1177 function yourls_maybe_unserialize( $original ) {
\r
1178 if ( yourls_is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
\r
1179 return @unserialize( $original );
\r
1183 // Determine if the current page is private
\r
1184 function yourls_is_private() {
\r
1187 if ( defined('YOURLS_PRIVATE') && YOURLS_PRIVATE == true ) {
\r
1189 // Allow overruling for particular pages:
\r
1192 if( yourls_is_API() ) {
\r
1193 if( !defined('YOURLS_PRIVATE_API') || YOURLS_PRIVATE_API != false )
\r
1197 } elseif( yourls_is_infos() ) {
\r
1198 if( !defined('YOURLS_PRIVATE_INFOS') || YOURLS_PRIVATE_INFOS !== false )
\r
1208 return yourls_apply_filter( 'is_private', $private );
\r
1211 // Show login form if required
\r
1212 function yourls_maybe_require_auth() {
\r
1213 if( yourls_is_private() )
\r
1214 require_once( YOURLS_INC.'/auth.php' );
\r
1217 // Return word or words if more than one
\r
1218 function yourls_plural( $word, $count=1 ) {
\r
1219 return $word . ($count > 1 ? 's' : '');
\r
1222 // Return trimmed string
\r
1223 function yourls_trim_long_string( $string, $length = 60, $append = '[...]' ) {
\r
1224 $newstring = $string;
\r
1225 if( function_exists('mb_substr') ) {
\r
1226 if ( mb_strlen( $newstring ) > $length ) {
\r
1227 $newstring = mb_substr( $newstring, 0, $length - mb_strlen( $append ), 'UTF-8' ) . $append;
\r
1230 if ( strlen( $newstring ) > $length ) {
\r
1231 $newstring = substr( $newstring, 0, $length - strlen( $append ) ) . $append;
\r
1234 return yourls_apply_filter( 'trim_long_string', $newstring, $string, $length, $append );
\r
1237 // Allow several short URLs for the same long URL ?
\r
1238 function yourls_allow_duplicate_longurls() {
\r
1239 // special treatment if API to check for WordPress plugin requests
\r
1240 if( yourls_is_API() ) {
\r
1241 if ( isset($_REQUEST['source']) && $_REQUEST['source'] == 'plugin' )
\r
1244 return ( defined( 'YOURLS_UNIQUE_URLS' ) && YOURLS_UNIQUE_URLS == false );
\r
1247 // Return list of all shorturls associated to the same long URL. Returns NULL or array of keywords.
\r
1248 function yourls_get_duplicate_keywords( $longurl ) {
\r
1249 if( !yourls_allow_duplicate_longurls() )
\r
1253 $longurl = yourls_escape( yourls_sanitize_url($longurl) );
\r
1254 $table = YOURLS_DB_TABLE_URL;
\r
1256 $return = $ydb->get_col( "SELECT `keyword` FROM `$table` WHERE `url` = '$longurl'" );
\r
1257 return yourls_apply_filter( 'get_duplicate_keywords', $return, $longurl );
\r
1260 // Check if an IP shortens URL too fast to prevent DB flood. Return true, or die.
\r
1261 function yourls_check_IP_flood( $ip = '' ) {
\r
1263 // Allow plugins to short-circuit the whole function
\r
1264 $pre = yourls_apply_filter( 'shunt_check_IP_flood', false, $ip );
\r
1265 if ( false !== $pre )
\r
1268 yourls_do_action( 'pre_check_ip_flood', $ip ); // at this point $ip can be '', check it if your plugin hooks in here
\r
1271 ( defined('YOURLS_FLOOD_DELAY_SECONDS') && YOURLS_FLOOD_DELAY_SECONDS === 0 ) ||
\r
1272 !defined('YOURLS_FLOOD_DELAY_SECONDS')
\r
1276 $ip = ( $ip ? yourls_sanitize_ip( $ip ) : yourls_get_IP() );
\r
1278 // Don't throttle whitelist IPs
\r
1279 if( defined('YOURLS_FLOOD_IP_WHITELIST' && YOURLS_FLOOD_IP_WHITELIST ) ) {
\r
1280 $whitelist_ips = explode( ',', YOURLS_FLOOD_IP_WHITELIST );
\r
1281 foreach( (array)$whitelist_ips as $whitelist_ip ) {
\r
1282 $whitelist_ip = trim( $whitelist_ip );
\r
1283 if ( $whitelist_ip == $ip )
\r
1288 // Don't throttle logged in users
\r
1289 if( yourls_is_private() ) {
\r
1290 if( yourls_is_valid_user() === true )
\r
1294 yourls_do_action( 'check_ip_flood', $ip );
\r
1297 $table = YOURLS_DB_TABLE_URL;
\r
1299 $lasttime = $ydb->get_var( "SELECT `timestamp` FROM $table WHERE `ip` = '$ip' ORDER BY `timestamp` DESC LIMIT 1" );
\r
1301 $now = date( 'U' );
\r
1302 $then = date( 'U', strtotime( $lasttime ) );
\r
1303 if( ( $now - $then ) <= YOURLS_FLOOD_DELAY_SECONDS ) {
\r
1305 yourls_do_action( 'ip_flood', $ip, $now - $then );
\r
1306 yourls_die( 'Too many URLs added too fast. Slow down please.', 'Forbidden', 403 );
\r
1313 // Check if YOURLS is installed
\r
1314 function yourls_is_installed() {
\r
1315 static $is_installed = false;
\r
1316 if ( $is_installed === false ) {
\r
1317 $check_14 = $check_13 = false;
\r
1319 if( defined('YOURLS_DB_TABLE_NEXTDEC') )
\r
1320 $check_13 = $ydb->get_var('SELECT `next_id` FROM '.YOURLS_DB_TABLE_NEXTDEC);
\r
1321 $check_14 = yourls_get_option( 'version' );
\r
1322 $is_installed = $check_13 || $check_14;
\r
1324 return yourls_apply_filter( 'is_installed', $is_installed );
\r
1327 // Generate random string of (int)$length length and type $type (see function for details)
\r
1328 function yourls_rnd_string ( $length = 5, $type = 0, $charlist = '' ) {
\r
1330 $length = intval( $length );
\r
1332 // define possible characters
\r
1333 switch ( $type ) {
\r
1335 // custom char list, or comply to charset as defined in config
\r
1337 $possible = $charlist ? $charlist : yourls_get_shorturl_charset() ;
\r
1340 // no vowels to make no offending word, no 0/1/o/l to avoid confusion between letters & digits. Perfect for passwords.
\r
1342 $possible = "23456789bcdfghjkmnpqrstvwxyz";
\r
1345 // Same, with lower + upper
\r
1347 $possible = "23456789bcdfghjkmnpqrstvwxyzBCDFGHJKMNPQRSTVWXYZ";
\r
1350 // all letters, lowercase
\r
1352 $possible = "abcdefghijklmnopqrstuvwxyz";
\r
1355 // all letters, lowercase + uppercase
\r
1357 $possible = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
\r
1360 // all digits & letters lowercase
\r
1362 $possible = "0123456789abcdefghijklmnopqrstuvwxyz";
\r
1365 // all digits & letters lowercase + uppercase
\r
1367 $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
\r
1373 while ($i < $length) {
\r
1374 $str .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
\r
1378 return yourls_apply_filter( 'rnd_string', $str, $length, $type, $charlist );
\r
1381 // Return salted string
\r
1382 function yourls_salt( $string ) {
\r
1383 $salt = defined('YOURLS_COOKIEKEY') ? YOURLS_COOKIEKEY : md5(__FILE__) ;
\r
1384 return yourls_apply_filter( 'yourls_salt', md5 ($string . $salt), $string );
\r
1387 // Add a query var to a URL and return URL. Completely stolen from WP.
\r
1388 // Works with one of these parameter patterns:
\r
1389 // array( 'var' => 'value' )
\r
1390 // array( 'var' => 'value' ), $url
\r
1392 // 'var', 'value', $url
\r
1393 // If $url ommited, uses $_SERVER['REQUEST_URI']
\r
1394 function yourls_add_query_arg() {
\r
1396 if ( is_array( func_get_arg(0) ) ) {
\r
1397 if ( @func_num_args() < 2 || false === @func_get_arg( 1 ) )
\r
1398 $uri = $_SERVER['REQUEST_URI'];
\r
1400 $uri = @func_get_arg( 1 );
\r
1402 if ( @func_num_args() < 3 || false === @func_get_arg( 2 ) )
\r
1403 $uri = $_SERVER['REQUEST_URI'];
\r
1405 $uri = @func_get_arg( 2 );
\r
1408 $uri = str_replace( '&', '&', $uri );
\r
1411 if ( $frag = strstr( $uri, '#' ) )
\r
1412 $uri = substr( $uri, 0, -strlen( $frag ) );
\r
1416 if ( preg_match( '|^https?://|i', $uri, $matches ) ) {
\r
1417 $protocol = $matches[0];
\r
1418 $uri = substr( $uri, strlen( $protocol ) );
\r
1423 if ( strpos( $uri, '?' ) !== false ) {
\r
1424 $parts = explode( '?', $uri, 2 );
\r
1425 if ( 1 == count( $parts ) ) {
\r
1427 $query = $parts[0];
\r
1429 $base = $parts[0] . '?';
\r
1430 $query = $parts[1];
\r
1432 } elseif ( !empty( $protocol ) || strpos( $uri, '=' ) === false ) {
\r
1433 $base = $uri . '?';
\r
1440 parse_str( $query, $qs );
\r
1441 $qs = yourls_urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string
\r
1442 if ( is_array( func_get_arg( 0 ) ) ) {
\r
1443 $kayvees = func_get_arg( 0 );
\r
1444 $qs = array_merge( $qs, $kayvees );
\r
1446 $qs[func_get_arg( 0 )] = func_get_arg( 1 );
\r
1449 foreach ( (array) $qs as $k => $v ) {
\r
1450 if ( $v === false )
\r
1454 $ret = http_build_query( $qs );
\r
1455 $ret = trim( $ret, '?' );
\r
1456 $ret = preg_replace( '#=(&|$)#', '$1', $ret );
\r
1457 $ret = $protocol . $base . $ret . $frag;
\r
1458 $ret = rtrim( $ret, '?' );
\r
1462 // Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg()
\r
1463 function yourls_urlencode_deep($value) {
\r
1464 $value = is_array($value) ? array_map('yourls_urlencode_deep', $value) : urlencode($value);
\r
1468 // Remove arg from query. Opposite of yourls_add_query_arg. Stolen from WP.
\r
1469 function yourls_remove_query_arg( $key, $query = false ) {
\r
1470 if ( is_array( $key ) ) { // removing multiple keys
\r
1471 foreach ( $key as $k )
\r
1472 $query = add_query_arg( $k, false, $query );
\r
1475 return add_query_arg( $key, false, $query );
\r
1478 // Return a time-dependent string for nonce creation
\r
1479 function yourls_tick() {
\r
1480 return ceil( time() / YOURLS_NONCE_LIFE );
\r
1483 // Create a time limited, action limited and user limited token
\r
1484 function yourls_create_nonce( $action, $user = false ) {
\r
1485 if( false == $user )
\r
1486 $user = defined('YOURLS_USER') ? YOURLS_USER : '-1';
\r
1487 $tick = yourls_tick();
\r
1488 return substr( yourls_salt($tick . $action . $user), 0, 10 );
\r
1491 // Create a nonce field for inclusion into a form
\r
1492 function yourls_nonce_field( $action, $name = 'nonce', $user = false, $echo = true ) {
\r
1493 $field = '<input type="hidden" id="'.$name.'" name="'.$name.'" value="'.yourls_create_nonce( $action, $user ).'" />';
\r
1499 // Add a nonce to a URL. If URL omitted, adds nonce to current URL
\r
1500 function yourls_nonce_url( $action, $url = false, $name = 'nonce', $user = false ) {
\r
1501 $nonce = yourls_create_nonce( $action, $user );
\r
1502 return yourls_add_query_arg( $name, $nonce, $url );
\r
1505 // Check validity of a nonce (ie time span, user and action match).
\r
1506 // Returns true if valid, dies otherwise (yourls_die() or die($return) if defined)
\r
1507 // if $nonce is false or unspecified, it will use $_REQUEST['nonce']
\r
1508 function yourls_verify_nonce( $action, $nonce = false, $user = false, $return = '' ) {
\r
1510 if( false == $user )
\r
1511 $user = defined('YOURLS_USER') ? YOURLS_USER : '-1';
\r
1513 // get current nonce value
\r
1514 if( false == $nonce && isset( $_REQUEST['nonce'] ) )
\r
1515 $nonce = $_REQUEST['nonce'];
\r
1517 // what nonce should be
\r
1518 $valid = yourls_create_nonce( $action, $user );
\r
1520 if( $nonce == $valid ) {
\r
1525 yourls_die( 'Unauthorized action or expired link', 'Error', 403 );
\r
1529 // Sanitize a version number (1.4.1-whatever -> 1.4.1)
\r
1530 function yourls_sanitize_version( $ver ) {
\r
1531 return preg_replace( '/[^0-9.]/', '', $ver );
\r
1534 // Converts keyword into short link (prepend with YOURLS base URL)
\r
1535 function yourls_link( $keyword = '' ) {
\r
1536 $link = YOURLS_SITE . '/' . yourls_sanitize_keyword( $keyword );
\r
1537 return yourls_apply_filter( 'yourls_link', $link, $keyword );
\r
1540 // Converts keyword into stat link (prepend with YOURLS base URL, append +)
\r
1541 function yourls_statlink( $keyword = '' ) {
\r
1542 $link = YOURLS_SITE . '/' . yourls_sanitize_keyword( $keyword ) . '+';
\r
1543 return yourls_apply_filter( 'yourls_statlink', $link, $keyword );
\r
1546 // Check if we're in API mode. Returns bool
\r
1547 function yourls_is_API() {
\r
1548 if ( defined('YOURLS_API') && YOURLS_API == true )
\r
1553 // Check if we're in Ajax mode. Returns bool
\r
1554 function yourls_is_Ajax() {
\r
1555 if ( defined('YOURLS_AJAX') && YOURLS_AJAX == true )
\r
1560 // Check if we're in GO mode (yourls-go.php). Returns bool
\r
1561 function yourls_is_GO() {
\r
1562 if ( defined('YOURLS_GO') && YOURLS_GO == true )
\r
1567 // Check if we're displaying stats infos (yourls-infos.php). Returns bool
\r
1568 function yourls_is_infos() {
\r
1569 if ( defined('YOURLS_INFOS') && YOURLS_INFOS == true )
\r
1574 // Check if we'll need interface display function (ie not API or redirection)
\r
1575 function yourls_has_interface() {
\r
1576 if( yourls_is_API() or yourls_is_GO() )
\r
1581 // Check if we're in the admin area. Returns bool
\r
1582 function yourls_is_admin() {
\r
1583 if ( defined('YOURLS_ADMIN') && YOURLS_ADMIN == true )
\r
1588 // Check if the server seems to be running on Windows. Not exactly sure how reliable this is.
\r
1589 function yourls_is_windows() {
\r
1590 return defined( 'DIRECTORY_SEPARATOR' ) && DIRECTORY_SEPARATOR == '\\';
\r
1593 // Check if SSL is required. Returns bool.
\r
1594 function yourls_needs_ssl() {
\r
1595 if ( defined('YOURLS_ADMIN_SSL') && YOURLS_ADMIN_SSL == true )
\r
1600 // Return admin link, with SSL preference if applicable.
\r
1601 function yourls_admin_url( $page = '' ) {
\r
1602 $admin = YOURLS_SITE . '/admin/' . $page;
\r
1603 if( yourls_is_ssl() or yourls_needs_ssl() )
\r
1604 $admin = str_replace('http://', 'https://', $admin);
\r
1605 return yourls_apply_filter( 'admin_url', $admin, $page );
\r
1608 // Return YOURLS_SITE, with SSL preference
\r
1609 function yourls_site_url( $echo = true ) {
\r
1610 $site = YOURLS_SITE;
\r
1611 // Do not enforce (checking yourls_need_ssl() ) but check current usage so it won't force SSL on non-admin pages
\r
1612 if( yourls_is_ssl() )
\r
1613 $site = str_replace( 'http://', 'https://', $site );
\r
1614 $site = yourls_apply_filter( 'site_url', $site );
\r
1620 // Check if SSL is used, returns bool. Stolen from WP.
\r
1621 function yourls_is_ssl() {
\r
1623 if ( isset($_SERVER['HTTPS']) ) {
\r
1624 if ( 'on' == strtolower($_SERVER['HTTPS']) )
\r
1626 if ( '1' == $_SERVER['HTTPS'] )
\r
1628 } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
\r
1631 return yourls_apply_filter( 'is_ssl', $is_ssl );
\r
1635 // Get a remote page <title>, return a string (either title or url)
\r
1636 function yourls_get_remote_title( $url ) {
\r
1637 // Allow plugins to short-circuit the whole function
\r
1638 $pre = yourls_apply_filter( 'shunt_get_remote_title', false, $url );
\r
1639 if ( false !== $pre )
\r
1642 require_once( YOURLS_INC.'/functions-http.php' );
\r
1644 $url = yourls_sanitize_url( $url );
\r
1646 $title = $charset = false;
\r
1648 $content = yourls_get_remote_content( $url );
\r
1650 // If false, return url as title.
\r
1651 // Todo: improve this with temporary title when shorturl_meta available?
\r
1652 if( false === $content )
\r
1655 if( $content !== false ) {
\r
1656 // look for <title>
\r
1657 if ( preg_match('/<title>(.*?)<\/title>/is', $content, $found ) ) {
\r
1658 $title = $found[1];
\r
1662 // look for charset
\r
1663 // <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
\r
1664 if ( preg_match('/<meta[^>]*?charset=([^>]*?)\/?>/is', $content, $found ) ) {
\r
1665 $charset = trim($found[1], '"\' ');
\r
1670 // if title not found, guess if returned content was actually an error message
\r
1671 if( $title == false && strpos( $content, 'Error' ) === 0 ) {
\r
1672 $title = $content;
\r
1675 if( $title == false )
\r
1679 if( !yourls_seems_utf8( $title ) )
\r
1680 $title = utf8_encode( $title );
\r
1683 // Charset conversion. We use @ to remove warnings (mb_ functions are easily bitching about illegal chars)
\r
1684 if( function_exists( 'mb_convert_encoding' ) ) {
\r
1686 $title = @mb_convert_encoding( $title, 'UTF-8', $charset );
\r
1688 $title = @mb_convert_encoding( $title, 'UTF-8' );
\r
1692 // Remove HTML entities
\r
1693 $title = html_entity_decode( $title, ENT_QUOTES, 'UTF-8' );
\r
1695 // Strip out evil things
\r
1696 $title = yourls_sanitize_title( $title );
\r
1698 return yourls_apply_filter( 'get_remote_title', $title, $url );
\r
1701 // Sanitize a filename (no Win32 stuff)
\r
1702 function yourls_sanitize_filename( $file ) {
\r
1703 $file = str_replace( '\\', '/', $file ); // sanitize for Win32 installs
\r
1704 $file = preg_replace( '|/+|' ,'/', $file ); // remove any duplicate slash
\r
1708 // Check for maintenance mode that will shortcut everything
\r
1709 function yourls_check_maintenance_mode() {
\r
1711 // TODO: all cases that always display the sites (is_admin but not is_ajax?)
\r
1715 // first case: /user/maintenance.php file
\r
1716 if( file_exists( YOURLS_USERDIR.'/maintenance.php' ) ) {
\r
1717 include( YOURLS_USERDIR.'/maintenance.php' );
\r
1721 // second case: option in DB
\r
1722 if( yourls_get_option( 'maintenance_mode' ) !== false ) {
\r
1723 require_once( YOURLS_INC.'/functions-html.php' );
\r
1724 $title = 'Service temporarily unavailable';
\r
1725 $message = 'Our service is currently undergoing scheduled maintenance.</p>
\r
1726 <p>Things should not last very long, thank you for your patience and please excuse the inconvenience';
\r
1727 yourls_die( $message, $title , 503 );
\r
1732 // Toggle maintenance mode
\r
1733 function yourls_maintenance_mode( $maintenance = true ) {
\r
1734 yourls_update_option( 'maintenance_mode', (bool)$maintenance );
\r
1737 // Check if a string seems to be UTF-8. Stolen from WP.
\r
1738 function yourls_seems_utf8($str) {
\r
1739 $length = strlen($str);
\r
1740 for ($i=0; $i < $length; $i++) {
\r
1741 $c = ord($str[$i]);
\r
1742 if ($c < 0x80) $n = 0; # 0bbbbbbb
\r
1743 elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
\r
1744 elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
\r
1745 elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
\r
1746 elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
\r
1747 elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
\r
1748 else return false; # Does not match any model
\r
1749 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
\r
1750 if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
\r
1757 // Quick UA check for mobile devices. Return boolean.
\r
1758 function yourls_is_mobile_device() {
\r
1759 // Strings searched
\r
1761 'android', 'blackberry', 'blazer',
\r
1762 'compal', 'elaine', 'fennec', 'hiptop',
\r
1763 'iemobile', 'iphone', 'ipod', 'ipad',
\r
1764 'iris', 'kindle', 'opera mobi', 'opera mini',
\r
1765 'palm', 'phone', 'pocket', 'psp', 'symbian',
\r
1766 'treo', 'wap', 'windows ce', 'windows phone'
\r
1769 // Current user-agent
\r
1770 $current = strtolower( $_SERVER['HTTP_USER_AGENT'] );
\r
1772 // Check and return
\r
1773 $is_mobile = ( str_replace( $mobiles, '', $current ) != $current );
\r
1774 return yourls_apply_filter( 'is_mobile_device', $is_mobile );
\r
1777 // Get request in YOURLS base (eg in 'http://site.com/yourls/abcd' get 'abdc')
\r
1778 function yourls_get_request() {
\r
1779 // Allow plugins to short-circuit the whole function
\r
1780 $pre = yourls_apply_filter( 'shunt_get_request', false );
\r
1781 if ( false !== $pre )
\r
1784 yourls_do_action( 'pre_get_request' );
\r
1786 // Ignore protocol and extract keyword
\r
1787 $base = str_replace( array( 'https://', 'http://' ), '', YOURLS_SITE );
\r
1788 $request = str_replace( $base.'/', '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
\r
1790 return yourls_apply_filter( 'get_request', $request );
\r
1793 // Change protocol to match current scheme used (http or https)
\r
1794 function yourls_match_current_protocol( $url, $normal = 'http', $ssl = 'https' ) {
\r
1795 if( yourls_is_ssl() )
\r
1796 $url = str_replace( $normal, $ssl, $url );
\r
1800 // Fix $_SERVER['REQUEST_URI'] variable for various setups. Stolen from WP.
\r
1801 function yourls_fix_request_uri() {
\r
1803 $default_server_values = array(
\r
1804 'SERVER_SOFTWARE' => '',
\r
1805 'REQUEST_URI' => '',
\r
1807 $_SERVER = array_merge( $default_server_values, $_SERVER );
\r
1809 // Fix for IIS when running with PHP ISAPI
\r
1810 if ( empty( $_SERVER['REQUEST_URI'] ) || ( php_sapi_name() != 'cgi-fcgi' && preg_match( '/^Microsoft-IIS\//', $_SERVER['SERVER_SOFTWARE'] ) ) ) {
\r
1812 // IIS Mod-Rewrite
\r
1813 if ( isset( $_SERVER['HTTP_X_ORIGINAL_URL'] ) ) {
\r
1814 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
\r
1816 // IIS Isapi_Rewrite
\r
1817 else if ( isset( $_SERVER['HTTP_X_REWRITE_URL'] ) ) {
\r
1818 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
\r
1820 // Use ORIG_PATH_INFO if there is no PATH_INFO
\r
1821 if ( !isset( $_SERVER['PATH_INFO'] ) && isset( $_SERVER['ORIG_PATH_INFO'] ) )
\r
1822 $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
\r
1824 // Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
\r
1825 if ( isset( $_SERVER['PATH_INFO'] ) ) {
\r
1826 if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
\r
1827 $_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
\r
1829 $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
\r
1832 // Append the query string if it exists and isn't null
\r
1833 if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
\r
1834 $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
\r