Upload files to 'includes'

This commit is contained in:
Sophia Atkinson 2022-10-30 14:11:56 -07:00
parent 5e82f002b9
commit bd403ce047
19 changed files with 532 additions and 245 deletions

View File

@ -4,6 +4,7 @@
* Connect to DB
*
* @since 1.0
* @return \YOURLS\Database\YDB
*/
function yourls_db_connect() {
global $ydb;
@ -101,6 +102,7 @@ function yourls_get_db() {
*
* @since 1.7.10
* @param mixed $db Either a \YOURLS\Database\YDB instance, or anything. If null, the function will unset $ydb
* @return void
*/
function yourls_set_db($db) {
global $ydb;

View File

@ -163,8 +163,12 @@ function yourls_api_output( $mode, $output, $send_headers = true, $echo = true )
/**
* Return array for API stat requests
*
* @param string $filter either "top", "bottom" , "rand" or "last"
* @param int $limit maximum number of links to return
* @param int $start offset
* @return array
*/
function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
function yourls_api_stats($filter = 'top', $limit = 10, $start = 0 ) {
$return = yourls_get_stats( $filter, $limit, $start );
$return['simple'] = 'Need either XML or JSON format for stats';
$return['message'] = 'success';
@ -174,6 +178,7 @@ function yourls_api_stats( $filter = 'top', $limit = 10, $start = 0 ) {
/**
* Return array for counts of shorturls and clicks
*
* @return array
*/
function yourls_api_db_stats() {
$return = array(
@ -189,6 +194,8 @@ function yourls_api_db_stats() {
/**
* Return array for API stat requests
*
* @param string $shorturl Short URL to check
* @return array
*/
function yourls_api_url_stats( $shorturl ) {
$keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
@ -202,6 +209,8 @@ function yourls_api_url_stats( $shorturl ) {
/**
* Expand short url to long url
*
* @param string $shorturl Short URL to expand
* @return array
*/
function yourls_api_expand( $shorturl ) {
$keyword = str_replace( yourls_get_yourls_site() . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'

View File

@ -7,6 +7,7 @@
/**
* Show login form if required
*
* @return void
*/
function yourls_maybe_require_auth() {
if( yourls_is_private() ) {
@ -37,7 +38,7 @@ function yourls_is_valid_user() {
// The logout nonce is associated to fake user 'logout' since at this point we don't know the real user
yourls_verify_nonce('admin_logout', $_REQUEST['nonce'], 'logout');
yourls_do_action( 'logout' );
yourls_store_cookie( null );
yourls_store_cookie( '' );
return yourls__( 'Logged out successfully' );
}
@ -124,6 +125,7 @@ function yourls_is_valid_user() {
/**
* Check auth against list of login=>pwd. Sets user if applicable, returns bool
*
* @return bool true if login/pwd pair is valid (and sets user if applicable), false otherwise
*/
function yourls_check_username_password() {
global $yourls_user_passwords;
@ -143,8 +145,11 @@ function yourls_check_username_password() {
/**
* Check a submitted password sent in plain text against stored password which can be a salted hash
*
* @param string $user
* @param string $submitted_password
* @return bool
*/
function yourls_check_password_hash( $user, $submitted_password ) {
function yourls_check_password_hash($user, $submitted_password ) {
global $yourls_user_passwords;
if( !isset( $yourls_user_passwords[ $user ] ) )
@ -173,11 +178,15 @@ function yourls_check_password_hash( $user, $submitted_password ) {
* @return true|string if overwrite was successful, an error message otherwise
*/
function yourls_hash_passwords_now( $config_file ) {
if( !is_readable( $config_file ) )
return 'cannot read file'; // not sure that can actually happen...
if( !is_readable( $config_file ) ) {
yourls_debug_log( 'Cannot hash passwords: cannot read file ' . $config_file );
return 'cannot read file'; // not sure that can actually happen...
}
if( !is_writable( $config_file ) )
if( !is_writable( $config_file ) ) {
yourls_debug_log( 'Cannot hash passwords: cannot write file ' . $config_file );
return 'cannot write file';
}
$yourls_user_passwords = [];
// Include file to read value of $yourls_user_passwords
@ -188,11 +197,16 @@ function yourls_hash_passwords_now( $config_file ) {
error_reporting( $errlevel );
$configdata = file_get_contents( $config_file );
if( $configdata == false )
return 'could not read file';
if( $configdata == false ) {
yourls_debug_log('Cannot hash passwords: file_get_contents() false with ' . $config_file);
return 'could not read file';
}
$to_hash = 0; // keep track of number of passwords that need hashing
foreach ( $yourls_user_passwords as $user => $password ) {
// avoid "deprecated" warning when password is null -- see test case in tests/data/auth/preg_replace_problem.php
$password ??= '';
if ( !yourls_has_phpass_password( $user ) && !yourls_has_md5_password( $user ) ) {
$to_hash++;
$hash = yourls_phpass_hash( $password );
@ -211,8 +225,10 @@ function yourls_hash_passwords_now( $config_file ) {
}
}
if( $to_hash == 0 )
return 0; // There was no password to encrypt
if( $to_hash == 0 ) {
yourls_debug_log('Cannot hash passwords: no password found in ' . $config_file);
return 'no password found';
}
$success = file_put_contents( $config_file, $configdata );
if ( $success === FALSE ) {
@ -320,6 +336,7 @@ function yourls_has_phpass_password( $user ) {
/**
* Check auth against encrypted COOKIE data. Sets user if applicable, returns bool
*
* @return bool true if authenticated, false otherwise
*/
function yourls_check_auth_cookie() {
global $yourls_user_passwords;
@ -406,6 +423,8 @@ function yourls_check_signature() {
/**
* Generate secret signature hash
*
* @param false|string $username Username to generate signature for, or false to use current user
* @return string Signature
*/
function yourls_auth_signature( $username = false ) {
if( !$username && defined('YOURLS_USER') ) {
@ -417,6 +436,8 @@ function yourls_auth_signature( $username = false ) {
/**
* Check if timestamp is not too old
*
* @param int $time Timestamp to check
* @return bool True if timestamp is valid
*/
function yourls_check_timestamp( $time ) {
$now = time();
@ -427,9 +448,10 @@ function yourls_check_timestamp( $time ) {
/**
* Store new cookie. No $user will delete the cookie.
*
* @param mixed $user String, user login, or null to delete cookie
* @param string $user User login, or empty string to delete cookie
* @return void
*/
function yourls_store_cookie( $user = null ) {
function yourls_store_cookie( $user = '' ) {
// No user will delete the cookie with a cookie time from the past
if( !$user ) {
@ -463,7 +485,6 @@ function yourls_store_cookie( $user = null ) {
*
* @see https://github.com/GoogleChromeLabs/samesite-examples/blob/master/php.md
* @see https://stackoverflow.com/a/59654832/36850
* @see https://3v4l.org/uKEtH for compat tests
* @see https://www.php.net/manual/en/function.setcookie.php
*
* @since 1.7.7
@ -479,24 +500,21 @@ function yourls_store_cookie( $user = null ) {
function yourls_setcookie($name, $value, $expire, $path, $domain, $secure, $httponly) {
$samesite = yourls_apply_filter('setcookie_samesite', 'Lax' );
if (PHP_VERSION_ID < 70300) {
return(setcookie($name, $value, $expire, "$path; samesite=$samesite", $domain, $secure, $httponly));
}
else {
return(setcookie($name, $value, array(
'expires' => $expire,
'path' => $path,
'domain' => $domain,
'samesite' => $samesite,
'secure' => $secure,
'httponly' => $httponly,
)));
}
return(setcookie($name, $value, array(
'expires' => $expire,
'path' => $path,
'domain' => $domain,
'samesite' => $samesite,
'secure' => $secure,
'httponly' => $httponly,
)));
}
/**
* Set user name
*
* @param string $user Username
* @return void
*/
function yourls_set_user( $user ) {
if( !defined( 'YOURLS_USER' ) )
@ -554,7 +572,7 @@ function yourls_cookie_name() {
* @return string cookie value
*/
function yourls_cookie_value( $user ) {
return yourls_apply_filter( 'set_cookie_value', yourls_salt( $user ), $user );
return yourls_apply_filter( 'set_cookie_value', yourls_salt( $user ?? '' ), $user );
}
/**
@ -562,6 +580,7 @@ function yourls_cookie_value( $user ) {
*
* Actually, this returns a float: ceil rounds up a value but is of type float, see https://www.php.net/ceil
*
* @return float
*/
function yourls_tick() {
return ceil( time() / yourls_get_nonce_life() );
@ -598,8 +617,11 @@ function yourls_hmac_algo() {
/**
* Create a time limited, action limited and user limited token
*
* @param string $action Action to create nonce for
* @param false|string $user Optional user string, false for current user
* @return string Nonce token
*/
function yourls_create_nonce( $action, $user = false ) {
function yourls_create_nonce($action, $user = false ) {
if( false === $user ) {
$user = defined('YOURLS_USER') ? YOURLS_USER : '-1';
}
@ -610,10 +632,15 @@ function yourls_create_nonce( $action, $user = false ) {
}
/**
* Create a nonce field for inclusion into a form
* Echoes or returns a nonce field for inclusion into a form
*
* @param string $action Action to create nonce for
* @param string $name Optional name of nonce field -- defaults to 'nonce'
* @param false|string $user Optional user string, false if unspecified
* @param bool $echo True to echo, false to return nonce field
* @return string Nonce field
*/
function yourls_nonce_field( $action, $name = 'nonce', $user = false, $echo = true ) {
function yourls_nonce_field($action, $name = 'nonce', $user = false, $echo = true ) {
$field = '<input type="hidden" id="'.$name.'" name="'.$name.'" value="'.yourls_create_nonce( $action, $user ).'" />';
if( $echo )
echo $field."\n";
@ -623,8 +650,13 @@ function yourls_nonce_field( $action, $name = 'nonce', $user = false, $echo = tr
/**
* Add a nonce to a URL. If URL omitted, adds nonce to current URL
*
* @param string $action Action to create nonce for
* @param string $url Optional URL to add nonce to -- defaults to current URL
* @param string $name Optional name of nonce field -- defaults to 'nonce'
* @param false|string $user Optional user string, false if unspecified
* @return string URL with nonce added
*/
function yourls_nonce_url( $action, $url = false, $name = 'nonce', $user = false ) {
function yourls_nonce_url($action, $url = false, $name = 'nonce', $user = false ) {
$nonce = yourls_create_nonce( $action, $user );
return yourls_add_query_arg( $name, $nonce, $url );
}
@ -632,11 +664,16 @@ function yourls_nonce_url( $action, $url = false, $name = 'nonce', $user = false
/**
* Check validity of a nonce (ie time span, user and action match).
*
* Returns true if valid, dies otherwise (yourls_die() or die($return) if defined)
* if $nonce is false or unspecified, it will use $_REQUEST['nonce']
* Returns true if valid, dies otherwise (yourls_die() or die($return) if defined).
* If $nonce is false or unspecified, it will use $_REQUEST['nonce']
*
* @param string $action
* @param false|string $nonce Optional, string: nonce value, or false to use $_REQUEST['nonce']
* @param false|string $user Optional, string user, false for current user
* @param string $return Optional, string: message to die with if nonce is invalid
* @return bool|void True if valid, dies otherwise
*/
function yourls_verify_nonce( $action, $nonce = false, $user = false, $return = '' ) {
function yourls_verify_nonce($action, $nonce = false, $user = false, $return = '' ) {
// Get user
if( false === $user ) {
$user = defined('YOURLS_USER') ? YOURLS_USER : '-1';
@ -668,7 +705,7 @@ function yourls_verify_nonce( $action, $nonce = false, $user = false, $return =
* Check if YOURLS_USER comes from environment variables
*
* @since 1.8.2
* @return bool true if YOURLS_USER and YOURLS_PASSWORD are defined as environment variables
* @return bool true if YOURLS_USER and YOURLS_PASSWORD are defined as environment variables
*/
function yourls_is_user_from_env() {
return yourls_apply_filter('is_user_from_env', getenv('YOURLS_USER') && getenv('YOURLS_PASSWORD'));

View File

@ -34,6 +34,7 @@ function yourls_get_debug_log() {
/**
* Get number of SQL queries performed
*
* @return int
*/
function yourls_get_num_queries() {
return yourls_apply_filter( 'get_num_queries', yourls_get_db()->get_num_queries() );
@ -44,6 +45,7 @@ function yourls_get_num_queries() {
*
* @since 1.7.3
* @param bool $bool Debug on or off
* @return void
*/
function yourls_debug_mode( $bool ) {
// log queries if true

View File

@ -5,10 +5,61 @@
*
* Note to devs: when deprecating a function, move it here. Then check all the places
* in core that might be using it, including core plugins.
*
* Usage : yourls_deprecated_function( 'function_name', 'version', 'replacement' );
* Output: "{function_name} is deprecated since version {version}! Use {replacement} instead."
*
* Usage : yourls_deprecated_function( 'function_name', 'version' );
* Output: "{function_name} is deprecated since version {version} with no alternative available."
*
* @see yourls_deprecated_function()
*/
// @codeCoverageIgnoreStart
/**
* Return current admin page, or null if not an admin page. Was not used anywhere.
*
* @return mixed string if admin page, null if not an admin page
* @since 1.6
* @deprecated 1.9.1
*/
function yourls_current_admin_page() {
yourls_deprecated_function( __FUNCTION__, '1.9.1' );
if( yourls_is_admin() ) {
$current = substr( yourls_get_request(), 6 );
if( $current === false )
$current = 'index.php'; // if current page is http://sho.rt/admin/ instead of http://sho.rt/admin/index.php
return $current;
}
return null;
}
/**
* PHP emulation of JS's encodeURI
*
* @link https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
* @deprecated 1.9.1
* @param string $url
* @return string
*/
function yourls_encodeURI($url) {
yourls_deprecated_function( __FUNCTION__, '1.9.1', '' );
// Decode URL all the way
$result = yourls_rawurldecode_while_encoded( $url );
// Encode once
$result = strtr( rawurlencode( $result ), array (
'%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':', '%40' => '@',
'%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$', '%21' => '!', '%2A' => '*',
'%27' => '\'', '%28' => '(', '%29' => ')', '%23' => '#',
) );
// @TODO:
// Known limit: this will most likely break IDN URLs such as http://www.académie-française.fr/
// To fully support IDN URLs, advocate use of a plugin.
return yourls_apply_filter( 'encodeURI', $result, $url );
}
/**
* Check if a file is a plugin file
*

View File

@ -7,15 +7,18 @@
/**
* Convert an integer (1337) to a string (3jk).
*
* @param int $num Number to convert
* @param string $chars Characters to use for conversion
* @return string Converted number
*/
function yourls_int2string( $num, $chars = null ) {
function yourls_int2string($num, $chars = null) {
if( $chars == null )
$chars = yourls_get_shorturl_charset();
$string = '';
$len = strlen( $chars );
while( $num >= $len ) {
$mod = bcmod( $num, $len );
$num = bcdiv( $num, $len );
$mod = bcmod( (string)$num, (string)$len );
$num = bcdiv( (string)$num, (string)$len );
$string = $chars[ $mod ] . $string;
}
$string = $chars[ intval( $num ) ] . $string;
@ -26,8 +29,11 @@ function yourls_int2string( $num, $chars = null ) {
/**
* Convert a string (3jk) to an integer (1337)
*
* @param string $string String to convert
* @param string $chars Characters to use for conversion
* @return string Number (as a string)
*/
function yourls_string2int( $string, $chars = null ) {
function yourls_string2int($string, $chars = null) {
if( $chars == null )
$chars = yourls_get_shorturl_charset();
$integer = 0;
@ -36,7 +42,7 @@ function yourls_string2int( $string, $chars = null ) {
$inputlen = strlen( $string );
for ($i = 0; $i < $inputlen; $i++) {
$index = strpos( $chars, $string[$i] );
$integer = bcadd( $integer, bcmul( $index, bcpow( $baselen, $i ) ) );
$integer = bcadd( (string)$integer, bcmul( (string)$index, bcpow( (string)$baselen, (string)$i ) ) );
}
return yourls_apply_filter( 'string2int', $integer, $string, $chars );
@ -48,7 +54,6 @@ function yourls_string2int( $string, $chars = null ) {
* @since 1.8.3
* @param string $prefix Optional prefix
* @return string The unique string
*
*/
function yourls_unique_element_id($prefix = 'yid') {
static $id_counter = 0;
@ -139,8 +144,11 @@ function yourls_sanitize_url_safe( $unsafe_url, $protocols = array() ) {
*
* Stolen from WP's _deep_replace
*
* @param string|array $search Needle, or array of needles.
* @param string $subject Haystack.
* @return string The string with the replaced values.
*/
function yourls_deep_replace( $search, $subject ){
function yourls_deep_replace($search, $subject ){
$found = true;
while($found) {
$found = false;
@ -158,24 +166,31 @@ function yourls_deep_replace( $search, $subject ){
/**
* Make sure an integer is a valid integer (PHP's intval() limits to too small numbers)
*
* @param int $int Integer to check
* @return string Integer as a string
*/
function yourls_sanitize_int( $int ) {
function yourls_sanitize_int($int ) {
return ( substr( preg_replace( '/[^0-9]/', '', strval( $int ) ), 0, 20 ) );
}
/**
* Sanitize an IP address
* No check on validity, just return a sanitized string
*
* @param string $ip IP address
* @return string IP address
*/
function yourls_sanitize_ip( $ip ) {
function yourls_sanitize_ip($ip ) {
return preg_replace( '/[^0-9a-fA-F:., ]/', '', $ip );
}
/**
* Make sure a date is m(m)/d(d)/yyyy, return false otherwise
*
* @param string $date Date to check
* @return false|mixed Date in format m(m)/d(d)/yyyy or false if invalid
*/
function yourls_sanitize_date( $date ) {
function yourls_sanitize_date($date ) {
if( !preg_match( '!^\d{1,2}/\d{1,2}/\d{4}$!' , $date ) ) {
return false;
}
@ -185,18 +200,24 @@ function yourls_sanitize_date( $date ) {
/**
* Sanitize a date for SQL search. Return false if malformed input.
*
* @param string $date Date
* @return false|string String in Y-m-d format for SQL search or false if malformed input
*/
function yourls_sanitize_date_for_sql( $date ) {
function yourls_sanitize_date_for_sql($date) {
if( !yourls_sanitize_date( $date ) )
return false;
return date( 'Y-m-d', strtotime( $date ) );
}
/**
* Return trimmed string
* Return trimmed string, optionally append '[...]' if string is too long
*
* @param string $string String to trim
* @param int $length Maximum length of string
* @param string $append String to append if trimmed
* @return string Trimmed string
*/
function yourls_trim_long_string( $string, $length = 60, $append = '[...]' ) {
function yourls_trim_long_string($string, $length = 60, $append = '[...]') {
$newstring = $string;
if ( mb_strlen( $newstring ) > $length ) {
$newstring = mb_substr( $newstring, 0, $length - mb_strlen( $append ), 'UTF-8' ) . $append;
@ -225,8 +246,10 @@ function yourls_sanitize_version( $version ) {
/**
* Sanitize a filename (no Win32 stuff)
*
* @param string $file File name
* @return string|null Sanitized file name (or null if it's just backslashes, ok...)
*/
function yourls_sanitize_filename( $file ) {
function yourls_sanitize_filename($file) {
$file = str_replace( '\\', '/', $file ); // sanitize for Win32 installs
$file = preg_replace( '|/+|' ,'/', $file ); // remove any duplicate slash
return $file;
@ -235,8 +258,10 @@ function yourls_sanitize_filename( $file ) {
/**
* Check if a string seems to be UTF-8. Stolen from WP.
*
* @param string $str String to check
* @return bool Whether string seems valid UTF-8
*/
function yourls_seems_utf8( $str ) {
function yourls_seems_utf8($str) {
$length = strlen( $str );
for ( $i=0; $i < $length; $i++ ) {
$c = ord( $str[ $i ] );
@ -417,6 +442,8 @@ function yourls_specialchars_decode( $string, $quote_style = ENT_NOQUOTES ) {
$others = array( '&lt;' => '<', '&#060;' => '<', '&gt;' => '>', '&#062;' => '>', '&amp;' => '&', '&#038;' => '&', '&#x26;' => '&' );
$others_preg = array( '/&#0*60;/' => '&#060;', '/&#0*62;/' => '&#062;', '/&#0*38;/' => '&#038;', '/&#x0*26;/i' => '&#x26;' );
$translation = $translation_preg = [];
if ( $quote_style === ENT_QUOTES ) {
$translation = array_merge( $single, $double, $others );
$translation_preg = array_merge( $single_preg, $double_preg, $others_preg );
@ -654,40 +681,16 @@ function yourls_esc_textarea( $text ) {
return yourls_apply_filter( 'esc_textarea', $safe_text, $text );
}
/**
* PHP emulation of JS's encodeURI
*
* @link https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURI
* @param $url
* @return string
*/
function yourls_encodeURI( $url ) {
// Decode URL all the way
$result = yourls_rawurldecode_while_encoded( $url );
// Encode once
$result = strtr( rawurlencode( $result ), array (
'%3B' => ';', '%2C' => ',', '%2F' => '/', '%3F' => '?', '%3A' => ':', '%40' => '@',
'%26' => '&', '%3D' => '=', '%2B' => '+', '%24' => '$', '%21' => '!', '%2A' => '*',
'%27' => '\'', '%28' => '(', '%29' => ')', '%23' => '#',
) );
// @TODO:
// Known limit: this will most likely break IDN URLs such as http://www.académie-française.fr/
// To fully support IDN URLs, advocate use of a plugin.
return yourls_apply_filter( 'encodeURI', $result, $url );
}
/**
* Adds backslashes before letters and before a number at the start of a string. Stolen from WP.
*
* @since 1.6
*
* @param string $string Value to which backslashes will be added.
* @return string String with backslashes inserted.
*/
function yourls_backslashit($string) {
$string = preg_replace('/^([0-9])/', '\\\\\\\\\1', $string);
$string = preg_replace('/([a-z])/i', '\\\\\1', $string);
$string = preg_replace('/^([0-9])/', '\\\\\\\\\1', (string)$string);
$string = preg_replace('/([a-z])/i', '\\\\\1', (string)$string);
return $string;
}
@ -745,7 +748,7 @@ function yourls_make_bookmarklet( $code ) {
*/
function yourls_get_timestamp( $timestamp ) {
$offset = yourls_get_time_offset();
$timestamp_offset = $timestamp + ($offset * 3600);
$timestamp_offset = (int)$timestamp + ($offset * 3600);
return yourls_apply_filter( 'get_timestamp', $timestamp_offset, $timestamp, $offset );
}
@ -793,4 +796,3 @@ function yourls_get_date_format( $format ) {
function yourls_get_time_format( $format ) {
return yourls_apply_filter( 'get_time_format', (string)$format );
}

View File

@ -3,6 +3,7 @@
/**
* Display <h1> header and logo
*
* @return void
*/
function yourls_html_logo() {
yourls_do_action( 'pre_html_logo' );
@ -22,6 +23,7 @@ function yourls_html_logo() {
*
* @param string $context Context of the page (stats, index, infos, ...)
* @param string $title HTML title of the page
* @return void
*/
function yourls_html_head( $context = 'index', $title = '' ) {
@ -172,6 +174,7 @@ function yourls_html_footer($can_query = true) {
*
* @param string $url URL to prefill the input with
* @param string $keyword Keyword to prefill the input with
* @return void
*/
function yourls_html_addnew( $url = '', $keyword = '' ) {
$pre = yourls_apply_filter( 'shunt_html_addnew', false, $url, $keyword );
@ -205,7 +208,7 @@ function yourls_html_addnew( $url = '', $keyword = '' ) {
* The $param array is defined in /admin/index.php, check the yourls_html_tfooter() call
*
* @param array $params Array of all required parameters
* @return string Result
* @return void
*/
function yourls_html_tfooter( $params = array() ) {
// Manually extract all parameters from the array. We prefer doing it this way, over using extract(),
@ -310,7 +313,7 @@ function yourls_html_tfooter( $params = array() ) {
<?php
// Remove empty keys from the $params array so it doesn't clutter the pagination links
$params = array_filter( $params, 'yourls_return_if_not_empty_string' ); // remove empty keys
$params = array_filter( $params, function($val){ return $val !== '';} ); // remove keys with empty values
if( isset( $search_text ) ) {
$params['search'] = $search_text;
@ -384,9 +387,18 @@ function yourls_html_select( $name, $options, $selected = '', $display = false,
return $html;
}
/**
* Display the Quick Share box
*
* @param string $longurl Long URL
* @param string $shorturl Short URL
* @param string $title Title
* @param string $text Text to display
* @param string $shortlink_title Optional replacement for 'Your short link'
* @param string $share_title Optional replacement for 'Quick Share'
* @param bool $hidden Optional. Hide the box by default (with css "display:none")
* @return void
*/
function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlink_title = '', $share_title = '', $hidden = false ) {
if ( $shortlink_title == '' )
@ -460,6 +472,11 @@ function yourls_share_box( $longurl, $shorturl, $title = '', $text='', $shortlin
/**
* Die die die
*
* @see https://www.youtube.com/watch?v=zSiKETBjARk
* @param string $message
* @param string $title
* @param int $header_code
* @return void
*/
function yourls_die( $message = '', $title = '', $header_code = 200 ) {
yourls_do_action( 'pre_yourls_die', $message, $title, $header_code );
@ -522,7 +539,13 @@ RETURN;
/**
* Return an "Add" row for the main table
*
* @return string HTML of the edit row
* @param string $keyword Keyword (short URL)
* @param string $url URL (long URL)
* @param string $title Title
* @param string $ip IP
* @param string|int $clicks Number of clicks
* @param string $timestamp Timestamp
* @return string HTML of the row
*/
function yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp ) {
$keyword = yourls_sanitize_keyword($keyword);
@ -614,7 +637,7 @@ function yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp
),
'clicks' => array(
'template' => '%clicks%',
'clicks' => yourls_number_format_i18n( $clicks, 0, '', '' ),
'clicks' => yourls_number_format_i18n( $clicks, 0 ),
),
'actions' => array(
'template' => '%actions% <input type="hidden" id="keyword_%id%" value="%keyword%"/>',
@ -641,6 +664,7 @@ function yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp
/**
* Echo the main table head
*
* @return void
*/
function yourls_table_head() {
$start = '<table id="main_table" class="tblSorter" cellpadding="0" cellspacing="1"><thead><tr>'."\n";
@ -665,6 +689,7 @@ function yourls_table_head() {
/**
* Echo the tbody start tag
*
* @return void
*/
function yourls_table_tbody_start() {
echo yourls_apply_filter( 'table_tbody_start', '<tbody>' );
@ -673,6 +698,7 @@ function yourls_table_tbody_start() {
/**
* Echo the tbody end tag
*
* @return void
*/
function yourls_table_tbody_end() {
echo yourls_apply_filter( 'table_tbody_end', '</tbody>' );
@ -681,27 +707,36 @@ function yourls_table_tbody_end() {
/**
* Echo the table start tag
*
* @return void
*/
function yourls_table_end() {
echo yourls_apply_filter( 'table_end', '</table></main>' );
}
/**
* Echo HTML tag for a link
*
*/
function yourls_html_link( $href, $title = '', $element = '' ) {
if( !$title )
$title = $href;
* @param string $href URL to link to
* @param string $anchor Anchor text
* @param string $element Element id
* @return void
*/
function yourls_html_link( $href, $anchor = '', $element = '' ) {
if( !$anchor )
$anchor = $href;
if( $element )
$element = sprintf( 'id="%s"', yourls_esc_attr( $element ) );
$link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $title ) );
$link = sprintf( '<a href="%s" %s>%s</a>', yourls_esc_url( $href ), $element, yourls_esc_html( $anchor ) );
echo yourls_apply_filter( 'html_link', $link );
}
/**
* Display the login screen. Nothing past this point.
*
* @param string $error_msg Optional error message to display
* @return void
*/
function yourls_login_screen( $error_msg = '' ) {
yourls_html_head( 'login' );
@ -744,12 +779,13 @@ function yourls_login_screen( $error_msg = '' ) {
die();
}
/**
* Display the admin menu
*
* @return void
*/
function yourls_html_menu() {
// Build menu links
if( defined( 'YOURLS_USER' ) ) {
// Create a logout link with a nonce associated to fake user 'logout' : the user is not yet defined
@ -828,6 +864,9 @@ function yourls_html_menu() {
/**
* Wrapper function to display admin notices
*
* @param string $message Message to display
* @param string $style Message style (default: 'notice')
* @return void
*/
function yourls_add_notice( $message, $style = 'notice' ) {
// Escape single quotes in $message to avoid breaking the anonymous function
@ -838,6 +877,9 @@ function yourls_add_notice( $message, $style = 'notice' ) {
/**
* Return a formatted notice
*
* @param string $message Message to display
* @param string $style CSS class to use for the notice
* @return string HTML of the notice
*/
function yourls_notice_box( $message, $style = 'notice' ) {
return <<<HTML
@ -848,13 +890,14 @@ HTML;
}
/**
* Display a page
* Display a page
*
* Includes content of a PHP file from the YOURLS_PAGEDIR directory, as if it
* were a standard short URL (ie http://sho.rt/$page)
* Includes content of a PHP file from the YOURLS_PAGEDIR directory, as if it
* were a standard short URL (ie http://sho.rt/$page)
*
* @since 1.0
* @param $page PHP file to display
* @since 1.0
* @param string $page PHP file to display
* @return void
*/
function yourls_page( $page ) {
if( !yourls_is_page($page)) {
@ -873,6 +916,7 @@ function yourls_page( $page ) {
* information for the page. Stolen from WP.
*
* @since 1.6
* @return void
*/
function yourls_html_language_attributes() {
$attributes = array();
@ -899,6 +943,7 @@ function yourls_html_language_attributes() {
* Output translated strings used by the Javascript calendar
*
* @since 1.6
* @return void
*/
function yourls_l10n_calendar_strings() {
echo "\n<script>\n";
@ -919,8 +964,9 @@ function yourls_l10n_calendar_strings() {
*
* @since 1.7
* @param string $compare_with Optional, YOURLS version to compare to
* @return void
*/
function yourls_new_core_version_notice($compare_with = false) {
function yourls_new_core_version_notice($compare_with = null) {
$compare_with = $compare_with ?: YOURLS_VERSION;
$checks = yourls_get_option( 'core_version_checks' );
@ -971,7 +1017,7 @@ function yourls_set_html_context($context) {
* @return string
*/
function yourls_get_html_context() {
yourls_get_db()->get_html_context();
return yourls_get_db()->get_html_context();
}
/**

View File

@ -24,6 +24,10 @@ use WpOrg\Requests\Requests;
*
* @since 1.7
* @see yourls_http_request
* @param string $url URL to request
* @param array $headers HTTP headers to send
* @param array $data GET data
* @param array $options Options to pass to Requests
* @return mixed Response object, or error string
*/
function yourls_http_get( $url, $headers = array(), $data = array(), $options = array() ) {
@ -35,6 +39,10 @@ function yourls_http_get( $url, $headers = array(), $data = array(), $options =
*
* @since 1.7
* @see yourls_http_request
* @param string $url URL to request
* @param array $headers HTTP headers to send
* @param array $data GET data
* @param array $options Options to pass to Requests
* @return mixed String (page body) or null if error
*/
function yourls_http_get_body( $url, $headers = array(), $data = array(), $options = array() ) {
@ -49,6 +57,10 @@ function yourls_http_get_body( $url, $headers = array(), $data = array(), $optio
*
* @since 1.7
* @see yourls_http_request
* @param string $url URL to request
* @param array $headers HTTP headers to send
* @param array $data POST data
* @param array $options Options to pass to Requests
* @return mixed Response object, or error string
*/
function yourls_http_post( $url, $headers = array(), $data = array(), $options = array() ) {
@ -62,6 +74,10 @@ function yourls_http_post( $url, $headers = array(), $data = array(), $options =
*
* @since 1.7
* @see yourls_http_request
* @param string $url URL to request
* @param array $headers HTTP headers to send
* @param array $data POST data
* @param array $options Options to pass to Requests
* @return mixed String (page body) or null if error
*/
function yourls_http_post_body( $url, $headers = array(), $data = array(), $options = array() ) {
@ -360,8 +376,8 @@ function yourls_check_core_version() {
* 3) the object should not contain any other key
*
* @since 1.7.7
* @param $json JSON object to check
* @return bool true if seems legit, false otherwise
* @param object $json JSON object to check
* @return bool true if seems legit, false otherwise
*/
function yourls_validate_core_version_response($json) {
return (
@ -375,13 +391,14 @@ function yourls_validate_core_version_response($json) {
/**
* Get version number from Github zipball URL (last part of URL, really)
*
* @since 1.8.3
* @param string $zipurl eg 'https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3'
* @return string
*/
function yourls_get_version_from_zipball_url($zipurl) {
$version = '';
$parts = explode('/', parse_url(yourls_sanitize_url($zipurl), PHP_URL_PATH));
$parts = explode('/', parse_url(yourls_sanitize_url($zipurl), PHP_URL_PATH) ?? '');
// expect at least 1 slash in path, return last part
if( count($parts) > 1 ) {
$version = end($parts);
@ -391,11 +408,15 @@ function yourls_get_version_from_zipball_url($zipurl) {
/**
* Check if URL is from YOURLS/YOURLS repo on github
*
* @since 1.8.3
* @param string $url URL to check
* @return bool
*/
function yourls_is_valid_github_repo_url($url) {
$url = yourls_sanitize_url($url);
return (
join('.',array_slice(explode('.',parse_url($url, PHP_URL_HOST)), -2, 2)) === 'github.com'
join('.',array_slice(explode('.', parse_url($url, PHP_URL_HOST) ?? ''), -2, 2)) === 'github.com'
// explodes on '.' (['api','github','com']) and keeps the last two elements
// to make sure domain is either github.com or one of its subdomain (api.github.com for instance)
// TODO: keep an eye on Github API to make sure it doesn't change some day to another domain (githubapi.com, ...)
@ -406,6 +427,7 @@ function yourls_is_valid_github_repo_url($url) {
/**
* Check if object has only expected keys 'latest' and 'zipurl' containing strings
*
* @since 1.8.3
* @param object $json
* @return bool

View File

@ -3,8 +3,11 @@
/**
* Echoes an image tag of Google Charts map from sorted array of 'country_code' => 'number of visits' (sort by DESC)
*
* @param array $countries Array of 'country_code' => 'number of visits'
* @param string $id Optional HTML element ID
* @return void
*/
function yourls_stats_countries_map( $countries, $id = null ) {
function yourls_stats_countries_map($countries, $id = null) {
yourls_do_action( 'pre_stats_countries_map' );
@ -29,11 +32,17 @@ function yourls_stats_countries_map( $countries, $id = null ) {
echo yourls_apply_filter( 'stats_countries_map', $map, $countries, $options, $id );
}
/**
* Echoes an image tag of Google Charts pie from sorted array of 'data' => 'value' (sort by DESC). Optional $limit = (integer) limit list of X first countries, sorted by most visits
*
* @param array $data Array of 'data' => 'value'
* @param int $limit Optional limit list of X first countries
* @param $size Optional size of the image
* @param $id Optional HTML element ID
* @return void
*/
function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $id = null ) {
function yourls_stats_pie($data, $limit = 10, $size = '340x220', $id = null) {
yourls_do_action( 'pre_stats_pie' );
@ -80,11 +89,14 @@ function yourls_stats_pie( $data, $limit = 10, $size = '340x220', $id = null ) {
echo yourls_apply_filter( 'stats_pie', $pie, $data, $limit, $size, $options, $id );
}
/**
* Build a list of all daily values between d1/m1/y1 to d2/m2/y2.
*
* @param array $dates
* @return array[] Array of arrays of days, months, years
*/
function yourls_build_list_of_days( $dates ) {
function yourls_build_list_of_days($dates) {
/* Say we have an array like:
$dates = array (
2009 => array (
@ -153,13 +165,17 @@ function yourls_build_list_of_days( $dates ) {
);
}
/**
* Echoes an image tag of Google Charts line graph from array of values (eg 'number of clicks').
*
* $legend1_list & legend2_list are values used for the 2 x-axis labels. $id is an HTML/JS id
*
* @param array $values Array of values (eg 'number of clicks')
* @param string $id HTML element id
* @return void
*/
function yourls_stats_line( $values, $id = null ) {
function yourls_stats_line($values, $id = null) {
yourls_do_action( 'pre_stats_line' );
@ -195,20 +211,27 @@ function yourls_stats_line( $values, $id = null ) {
echo yourls_apply_filter( 'stats_line', $lineChart, $values, $options, $id );
}
/**
* Return the number of days in a month. From php.net, used if PHP built without calendar functions
* Return the number of days in a month. From php.net.
*
* @param int $month
* @param int $year
* @return int
*/
function yourls_days_in_month( $month, $year ) {
function yourls_days_in_month($month, $year) {
// calculate number of days in a month
return $month == 2 ? ( $year % 4 ? 28 : ( $year % 100 ? 29 : ( $year % 400 ? 28 : 29 ) ) ) : ( ( $month - 1 ) % 7 % 2 ? 30 : 31 );
}
/**
* Get max value from date array of 'Aug 12, 2012' = '1337'
*
* @param array $list_of_days
* @return array
*/
function yourls_stats_get_best_day( $list_of_days ) {
function yourls_stats_get_best_day($list_of_days) {
$max = max( $list_of_days );
foreach( $list_of_days as $k=>$v ) {
if ( $v == $max )
@ -219,8 +242,11 @@ function yourls_stats_get_best_day( $list_of_days ) {
/**
* Return domain of a URL
*
* @param string $url
* @param bool $include_scheme
* @return string
*/
function yourls_get_domain( $url, $include_scheme = false ) {
function yourls_get_domain($url, $include_scheme = false) {
$parse = @parse_url( $url ); // Hiding ugly stuff coming from malformed referrer URLs
// Get host & scheme. Fall back to path if not found.
@ -236,19 +262,24 @@ function yourls_get_domain( $url, $include_scheme = false ) {
return $host;
}
/**
* Return favicon URL
*
* @param string $url
* @return string
*/
function yourls_get_favicon_url( $url ) {
function yourls_get_favicon_url($url) {
return yourls_match_current_protocol( '//www.google.com/s2/favicons?domain=' . yourls_get_domain( $url, false ) );
}
/**
* Scale array of data from 0 to 100 max
*
* @param array $data
* @return array
*/
function yourls_scale_data( $data ) {
function yourls_scale_data($data ) {
$max = max( $data );
if( $max > 100 ) {
foreach( $data as $k=>$v ) {
@ -258,11 +289,19 @@ function yourls_scale_data( $data ) {
return $data;
}
/**
* Tweak granularity of array $array: keep only $grain values. This make less accurate but less messy graphs when too much values. See http://code.google.com/apis/chart/formats.html#granularity
* Tweak granularity of array $array: keep only $grain values.
*
* This make less accurate but less messy graphs when too much values.
* See https://developers.google.com/chart/image/docs/gallery/line_charts?hl=en#data-granularity
*
* @param array $array
* @param int $grain
* @param bool $preserve_max
* @return array
*/
function yourls_array_granularity( $array, $grain = 100, $preserve_max = true ) {
function yourls_array_granularity($array, $grain = 100, $preserve_max = true) {
if ( count( $array ) > $grain ) {
$max = max( $array );
$step = intval( count( $array ) / $grain );
@ -286,8 +325,10 @@ function yourls_array_granularity( $array, $grain = 100, $preserve_max = true )
/**
* Transform data array to data table for Google API
*
* @param array $data
* @return string
*/
function yourls_google_array_to_data_table( $data ){
function yourls_google_array_to_data_table($data){
$str = "var data = google.visualization.arrayToDataTable([\n";
foreach( $data as $label => $values ){
if( !is_array( $values ) ) {
@ -310,8 +351,13 @@ function yourls_google_array_to_data_table( $data ){
/**
* Return javascript code that will display the Google Chart
*
* @param string $graph_type
* @param string $data
* @param var $options
* @param string $id
* @return string
*/
function yourls_google_viz_code( $graph_type, $data, $options, $id ) {
function yourls_google_viz_code($graph_type, $data, $options, $id ) {
$function_name = 'yourls_graph' . $id;
$code = "\n<script id=\"$function_name\" type=\"text/javascript\">\n";
$code .= "function $function_name() { \n";
@ -336,4 +382,3 @@ function yourls_google_viz_code( $graph_type, $data, $options, $id ) {
return $code;
}

View File

@ -13,6 +13,7 @@ function yourls_check_PDO() {
/**
* Check if server has MySQL 5.0+
*
* @return bool
*/
function yourls_check_database_version() {
return ( version_compare( '5.0', yourls_get_database_version() ) <= 0 );
@ -40,6 +41,7 @@ function yourls_get_database_version() {
* As of 1.8 we advertise YOURLS as being 7.4+ but it should work on 7.2 (although untested)
* so we don't want to strictly enforce a limitation that may not be necessary.
*
* @return bool
*/
function yourls_check_php_version() {
return version_compare( PHP_VERSION, '7.2.0', '>=' );
@ -48,6 +50,7 @@ function yourls_check_php_version() {
/**
* Check if server is an Apache
*
* @return bool
*/
function yourls_is_apache() {
if( !array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) )
@ -61,6 +64,7 @@ function yourls_is_apache() {
/**
* Check if server is running IIS
*
* @return bool
*/
function yourls_is_iis() {
return ( array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) ? ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false ) : false );
@ -70,6 +74,7 @@ function yourls_is_iis() {
/**
* Create .htaccess or web.config. Returns boolean
*
* @return bool
*/
function yourls_create_htaccess() {
$host = parse_url( yourls_get_yourls_site() );
@ -319,6 +324,8 @@ function yourls_insert_sample_links() {
/**
* Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
*
* @param bool $maintenance True to enable, false to disable
* @return bool True on success, false on failure
*/
function yourls_maintenance_mode( $maintenance = true ) {
@ -342,4 +349,3 @@ function yourls_maintenance_mode( $maintenance = true ) {
return @unlink($file);
}
}

View File

@ -39,17 +39,17 @@
* - $yourls_allowedentitynames is used internally in KSES functions to sanitize HTML entities
* - $yourls_allowedprotocols is used in various parts of YOURLS, not just in KSES, albeit being defined here
* Two globals are not defined and unused at this moment: $yourls_allowedtags_all and $yourls_allowedtags
* The code for these vars is here and ready for any future use
* The code for these vars is here and ready for any future use
*/
// Populate after plugins have loaded to allow user defined values
yourls_add_action( 'plugins_loaded', 'yourls_kses_init' );
/**
* Init KSES globals if not already defined (by a plugin)
*
* @since 1.6
*
* @return void
*/
function yourls_kses_init() {
global $yourls_allowedentitynames, $yourls_allowedprotocols;
@ -57,13 +57,13 @@ function yourls_kses_init() {
if( ! $yourls_allowedentitynames ) {
$yourls_allowedentitynames = yourls_apply_filter( 'kses_allowed_entities', yourls_kses_allowed_entities() );
}
if( ! $yourls_allowedprotocols ) {
$yourls_allowedprotocols = yourls_apply_filter( 'kses_allowed_protocols', yourls_kses_allowed_protocols() );
}
}
/** See NOTE ABOUT GLOBALS **
if( ! $yourls_allowedtags_all ) {
$yourls_allowedtags_all = yourls_kses_allowed_tags_all();
$yourls_allowedtags_all = array_map( '_yourls_add_global_attributes', $yourls_allowedtags_all );
@ -72,7 +72,7 @@ function yourls_kses_init() {
// User defined: let's sanitize
$yourls_allowedtags_all = yourls_kses_array_lc( $yourls_allowedtags_all );
}
if( ! $yourls_allowedtags ) {
$yourls_allowedtags = yourls_kses_allowed_tags();
$yourls_allowedtags = array_map( '_yourls_add_global_attributes', $yourls_allowedtags );
@ -84,7 +84,7 @@ function yourls_kses_init() {
/**/
}
/**
* Kses global for all allowable HTML tags.
*
@ -411,7 +411,7 @@ function yourls_kses_allowed_tags_all() {
'var' => array(),
);
}
/**
* Kses global for default allowable HTML tags. TODO: trim down to necessary only.
*
@ -522,39 +522,39 @@ function yourls_kses_allowed_protocols() {
'feed:', 'feed://',
'mailto:',
'news:', 'nntp://',
// Old school bearded geek
'gopher://', 'telnet://', 'finger://',
'nntp://', 'worldwind://',
// Dev
'ssh://', 'svn://', 'svn+ssh://', 'git://', 'cvs://',
'apt:',
'market://', // Google Play
'view-source:',
// P2P
'ed2k://', 'magnet:', 'udp://',
// Streaming stuff
'mms://', 'lastfm://', 'spotify:', 'rtsp://',
// Text & voice
'aim:', 'facetime://', 'gtalk:', 'xmpp:',
'irc://', 'ircs://', 'mumble://',
'irc://', 'ircs://', 'mumble://',
'callto:', 'skype:', 'sip:',
'teamspeak://', 'tel:', 'ventrilo://', 'xfire:',
'teamspeak://', 'tel:', 'ventrilo://', 'xfire:',
'ymsgr:', 'tg://', 'whatsapp://',
// Misc
'steam:', 'steam://',
'bitcoin:',
'ldap://', 'ldaps://',
// Purposedly removed for security
/*
'about:', 'chrome://', 'chrome-extension://',
'javascript:',
'javascript:',
'data:',
*/
);

View File

@ -32,7 +32,7 @@ use POMO\Translations\NOOPTranslations;
* @uses yourls_apply_filter() Calls 'get_locale' hook on locale value.
* @uses $yourls_locale Gets the locale stored in the global.
*
* @return string The locale of the blog or from the 'get_locale' hook.
* @return string The locale of the YOURLS instance
*/
function yourls_get_locale() {
global $yourls_locale;
@ -116,8 +116,7 @@ function yourls__( $text, $domain = 'default' ) {
* @see sprintf()
* @since 1.6
*
* @param string $pattern Text to translate
* @param string $arg1, $arg2... Optional: sprintf tokens, and translation domain
* @param mixed ...$pattern Text to translate, then $arg1: optional sprintf tokens, and $arg2: translation domain
* @return string Translated text
*/
function yourls_s( $pattern ) {
@ -158,9 +157,8 @@ function yourls_s( $pattern ) {
* @see sprintf()
* @since 1.6
*
* @param string $pattern Text to translate
* @param string $arg1, $arg2... Optional: sprintf tokens, and translation domain
* @return string Translated text
* @param string ...$pattern Text to translate, then optional sprintf tokens, and optional translation domain
* @return void Translated text
*/
function yourls_se( $pattern ) {
echo yourls_s( func_get_args() );
@ -207,6 +205,7 @@ function yourls_esc_html__( $text, $domain = 'default' ) {
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
* @return void
*/
function yourls_e( $text, $domain = 'default' ) {
echo yourls_translate( $text, $domain );
@ -221,6 +220,7 @@ function yourls_e( $text, $domain = 'default' ) {
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
* @return void
*/
function yourls_esc_attr_e( $text, $domain = 'default' ) {
echo yourls_esc_attr( yourls_translate( $text, $domain ) );
@ -235,6 +235,7 @@ function yourls_esc_attr_e( $text, $domain = 'default' ) {
*
* @param string $text Text to translate
* @param string $domain Optional. Domain to retrieve the translated text
* @return void
*/
function yourls_esc_html_e( $text, $domain = 'default' ) {
echo yourls_esc_html( yourls_translate( $text, $domain ) );
@ -269,7 +270,7 @@ function yourls_x( $text, $context, $domain = 'default' ) {
* @param string $text Text to translate
* @param string $context Context information for the translators
* @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated context string
* @return void Echoes translated context string
*/
function yourls_xe( $text, $context, $domain = 'default' ) {
echo yourls_x( $text, $context, $domain );
@ -347,6 +348,12 @@ function yourls_n( $single, $plural, $number, $domain = 'default' ) {
* @see yourls_n()
* @see yourls_x()
*
* @param string $single The text that will be used if $number is 1
* @param string $plural The text that will be used if $number is not 1
* @param int $number The number to compare against to use either $single or $plural
* @param string $context Context information for the translators
* @param string $domain Optional. The domain identifier the text should be retrieved in
* @return string Either $single or $plural translated text
*/
function yourls_nx($single, $plural, $number, $context, $domain = 'default') {
$translations = yourls_get_translations_for_domain( $domain );
@ -391,6 +398,12 @@ function yourls_n_noop( $singular, $plural, $domain = null ) {
*
* @since 1.6
* @see yourls_n_noop()
*
* @param string $singular Single form to be i18ned
* @param string $plural Plural form to be i18ned
* @param string $context Context information for the translators
* @param string $domain Optional. The domain identifier the text will be retrieved in
* @return array array($singular, $plural)
*/
function yourls_nx_noop( $singular, $plural, $context, $domain = null ) {
return array(
@ -510,6 +523,8 @@ function yourls_load_default_textdomain() {
if( !empty( $yourls_locale ) )
return yourls_load_textdomain( 'default', YOURLS_LANG_DIR . "/$yourls_locale.mo" );
return false;
}
/**
@ -548,6 +563,8 @@ function yourls_is_textdomain_loaded( $domain ) {
* file and this function properly translates them back.
*
* @since 1.6
* @param string $name The role name
* @return string Translated role name
*/
function yourls_translate_user_role( $name ) {
return yourls_translate_with_context( $name, 'User role' );
@ -605,6 +622,9 @@ function yourls_number_format_i18n( $number, $decimals = 0 ) {
* @return string The date, translated if locale specifies it.
*/
function yourls_date_i18n( $dateformatstring, $timestamp = false ) {
/**
* @var YOURLS_Locale_Formats $yourls_locale_formats
*/
global $yourls_locale_formats;
if( !isset( $yourls_locale_formats ) )
$yourls_locale_formats = new YOURLS_Locale_Formats();
@ -743,6 +763,7 @@ class YOURLS_Locale_Formats {
*
* @since 1.6
* @access private
* @return void
*/
function init() {
// The Weekdays
@ -845,7 +866,7 @@ class YOURLS_Locale_Formats {
* @since 1.6
* @access public
*
* @param int $weekday_number 0 for Sunday through 6 Saturday
* @param int|string $weekday_number 0 for Sunday through 6 Saturday
* @return string Full translated weekday
*/
function get_weekday( $weekday_number ) {
@ -945,6 +966,7 @@ class YOURLS_Locale_Formats {
* @access private
*
* @since 1.6
* @return void
*/
function register_globals() {
$GLOBALS['weekday'] = $this->weekday;

View File

@ -93,6 +93,8 @@ function yourls_add_query_arg() {
/**
* Navigates through an array and encodes the values to be used in a URL. Stolen from WP, used in yourls_add_query_arg()
*
* @param array|string $value The array or string to be encoded.
* @return array|string
*/
function yourls_urlencode_deep( $value ) {
$value = is_array( $value ) ? array_map( 'yourls_urlencode_deep', $value ) : urlencode( $value );
@ -147,6 +149,8 @@ function yourls_link( $keyword = '', $stats = false ) {
*
* This function does not make sure the keyword matches an actual short URL
*
* @param string $keyword Short URL keyword
* @return string Short URL stat link
*/
function yourls_statlink( $keyword = '' ) {
$link = yourls_link( $keyword, true );
@ -156,6 +160,8 @@ function yourls_statlink( $keyword = '' ) {
/**
* Return admin link, with SSL preference if applicable.
*
* @param string $page Page name, eg "index.php"
* @return string
*/
function yourls_admin_url( $page = '' ) {
$admin = yourls_get_yourls_site() . '/admin/' . $page;
@ -168,8 +174,11 @@ function yourls_admin_url( $page = '' ) {
/**
* Return YOURLS_SITE or URL under YOURLS setup, with SSL preference
*
* @param bool $echo Echo if true, or return if false
* @param string $url
* @return string
*/
function yourls_site_url( $echo = true, $url = '' ) {
function yourls_site_url($echo = true, $url = '' ) {
$url = yourls_get_relative_url( $url );
$url = trim( yourls_get_yourls_site() . '/' . $url, '/' );
@ -255,7 +264,7 @@ function yourls_get_yourls_favicon_url( $echo = true ) {
if( $custom ) {
$favicon = yourls_site_url( false, YOURLS_USERURL . '/' . $custom );
} else {
$favicon = yourls_site_url( false ) . '/images/favicon.ico';
$favicon = yourls_site_url( false ) . '/images/favicon.svg';
}
$favicon = yourls_apply_filter('get_favicon_url', $favicon);

View File

@ -36,6 +36,7 @@ function yourls_get_option( $option_name, $default = false ) {
* a check for DB server reachability has been performed
*
* @since 1.4
* @return void
*/
function yourls_get_all_options() {
// Allow plugins to short-circuit all options. (Note: regular plugins are loaded after all options)

View File

@ -55,7 +55,7 @@
* )
* )
*
* @var array
* @var array $yourls_filters
*/
if ( !isset( $yourls_filters ) ) {
$yourls_filters = [];
@ -65,7 +65,7 @@ if ( !isset( $yourls_filters ) ) {
* This global var will collect 'done' actions with the following structure:
* $yourls_actions['hook'] => number of time this action was done
*
* @var array
* @var array $yourls_actions
*/
if ( !isset( $yourls_actions ) ) {
$yourls_actions = [];
@ -79,7 +79,7 @@ if ( !isset( $yourls_actions ) ) {
*
* @link https://docs.yourls.org/development/plugins.html
* @param string $hook the name of the YOURLS element to be filtered or YOURLS action to be triggered
* @param callback $function_name the name of the function that is to be called.
* @param callable $function_name the name of the function that is to be called.
* @param int $priority optional. Used to specify the order in which the functions associated with a
* particular action are executed (default=10, lower=earlier execution, and functions
* with the same priority are executed in the order in which they were added to the
@ -88,11 +88,12 @@ if ( !isset( $yourls_actions ) ) {
* provided).
* @param string $type
* @global array $yourls_filters Storage for all of the filters
* @return void
*/
function yourls_add_filter( $hook, $function_name, $priority = 10, $accepted_args = NULL, $type = 'filter' ) {
global $yourls_filters;
// At this point, we cannot check if the function exists, as it may well be defined later (which is OK)
$id = yourls_filter_unique_id( $hook, $function_name, $priority );
$id = yourls_filter_unique_id($function_name);
$yourls_filters[ $hook ][ $priority ][ $id ] = [
'function' => $function_name,
@ -114,11 +115,12 @@ function yourls_add_filter( $hook, $function_name, $priority = 10, $accepted_arg
*
* @link https://docs.yourls.org/development/plugins.html
* @param string $hook The name of the action to which the $function_to_add is hooked.
* @param callback $function_name The name of the function you wish to be called.
* @param callable $function_name The name of the function you wish to be called.
* @param int $priority Optional. Used to specify the order in which the functions associated with a particular action
* are executed (default: 10). Lower numbers correspond with earlier execution, and functions
* with the same priority are executed in the order in which they were added to the action.
* @param int $accepted_args Optional. The number of arguments the function accept (default 1).
* @return void
*/
function yourls_add_action( $hook, $function_name, $priority = 10, $accepted_args = 1 ) {
yourls_add_filter( $hook, $function_name, $priority, $accepted_args, 'action' );
@ -142,14 +144,11 @@ function yourls_add_action( $hook, $function_name, $priority = 10, $accepted_arg
* yourls_add_filter('my_hook_test', $my_callback_function);
*
* @link https://docs.yourls.org/development/hooks.html
* @param string $hook Hook to which the function is attached
* @param string|array $function Used for creating unique id
* @param int|bool $priority Used in counting how many hooks were applied. If === false and $function is an object reference,
* we return the unique id only if it already has one, false otherwise.
* @param string|array|object $function The callable used in a filter or action.
* @return string unique ID for usage as array key
*/
function yourls_filter_unique_id( $hook, $function, $priority ) {
// If function then just skip all of the tests and not overwrite the following.
function yourls_filter_unique_id($function) {
// If given a string (function name)
if ( is_string( $function ) ) {
return $function;
}
@ -167,20 +166,12 @@ function yourls_filter_unique_id( $hook, $function, $priority ) {
return spl_object_hash( $function[0] ).$function[1];
}
// Static Calling
if ( is_string( $function[0] ) ) {
return $function[0].'::'.$function[1];
}
/**
* There is no other possible case as of PHP 7.2-8.0 callables. Still, we're leaving the final
* `if` block (which could be remove to simply `return $function[0].'::'.$function[1]`) for readability
* and understanding the logic.
*/
// Last case, static Calling : $function[0] is a string (Class Name) and $function[1] is a string (Method Name)
return $function[0].'::'.$function[1];
}
/**
* Performs a filtering operation on a YOURLS element or event.
* Performs a filtering operation on a value or an event.
*
* Typical use:
*
@ -192,12 +183,12 @@ function yourls_filter_unique_id( $hook, $function, $priority ) {
* yourls_apply_filter( 'yourls_event' );
* (see yourls_do_action() )
*
* Returns an element which may have been filtered by a filter.
* Returns a value which may have been modified by a filter.
*
* @global array $yourls_filters storage for all of the filters
* @param string $hook the name of the YOURLS element or action
* @param mixed $value the value of the element before filtering
* @param bool $is_action true if the function is called by yourls_do_action()
* @param true|mixed $is_action true if the function is called by yourls_do_action() - otherwise may be the second parameter of an arbitrary number of parameters
* @return mixed
*/
function yourls_apply_filter( $hook, $value = '', $is_action = false ) {
@ -222,6 +213,7 @@ function yourls_apply_filter( $hook, $value = '', $is_action = false ) {
reset( $yourls_filters[ $hook ] );
do {
foreach ( (array)current( $yourls_filters[ $hook ] ) as $the_ ) {
$_value = '';
if ( !is_null($the_[ 'function' ]) ) {
$args[ 1 ] = $value;
$count = $the_[ 'accepted_args' ];
@ -247,6 +239,7 @@ function yourls_apply_filter( $hook, $value = '', $is_action = false ) {
*
* @param string $hook the name of the YOURLS action
* @param mixed $arg action arguments
* @return void
*/
function yourls_do_action( $hook, $arg = '' ) {
global $yourls_actions, $yourls_filters;
@ -306,6 +299,7 @@ function yourls_did_action( $hook ) {
* @param string $type Either 'action' or 'filter'
* @param string $hook The hook name, eg 'plugins_loaded'
* @param mixed $args Variable-length argument lists that were passed to the action or filter
* @return void
*/
function yourls_call_all_hooks($type, $hook, ...$args) {
global $yourls_filters;
@ -340,14 +334,14 @@ function yourls_call_all_hooks($type, $hook, ...$args) {
*
* @global array $yourls_filters storage for all of the filters
* @param string $hook The filter hook to which the function to be removed is hooked.
* @param callback $function_to_remove The name of the function which should be removed.
* @param callable $function_to_remove The name of the function which should be removed.
* @param int $priority optional. The priority of the function (default: 10).
* @return bool Whether the function was registered as a filter before it was removed.
*/
function yourls_remove_filter( $hook, $function_to_remove, $priority = 10 ) {
global $yourls_filters;
$function_to_remove = yourls_filter_unique_id( $hook, $function_to_remove, $priority );
$function_to_remove = yourls_filter_unique_id($function_to_remove);
$remove = isset( $yourls_filters[ $hook ][ $priority ][ $function_to_remove ] );
@ -435,15 +429,14 @@ function yourls_get_filters($hook) {
function yourls_get_actions($hook) {
return yourls_get_filters($hook);
}
/**
* Check if any filter has been registered for a hook.
*
* @since 1.5
* @global array $yourls_filters storage for all of the filters
* @param string $hook The name of the filter hook.
* @param callable|false $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
* @return int|bool Optionally returns the priority on that hook for the specified function.
* @global array $yourls_filters storage for all of the filters
*/
function yourls_has_filter( $hook, $function_to_check = false ) {
global $yourls_filters;
@ -453,7 +446,7 @@ function yourls_has_filter( $hook, $function_to_check = false ) {
return $has;
}
if ( !$idx = yourls_filter_unique_id( $hook, $function_to_check, false ) ) {
if ( !$idx = yourls_filter_unique_id($function_to_check) ) {
return false;
}
@ -465,6 +458,7 @@ function yourls_has_filter( $hook, $function_to_check = false ) {
return false;
}
/**
* Check if any action has been registered for a hook.
*
@ -786,6 +780,7 @@ function yourls_list_plugin_admin_pages() {
* @param string $slug
* @param string $title
* @param callable $function
* @return void
*/
function yourls_register_plugin_page( $slug, $title, $function ) {
yourls_get_db()->add_plugin_page( $slug, $title, $function );
@ -796,6 +791,7 @@ function yourls_register_plugin_page( $slug, $title, $function ) {
*
* @since 1.5
* @param string $plugin_page
* @return void
*/
function yourls_plugin_admin_page( $plugin_page ) {
// Check the plugin page is actually registered
@ -866,6 +862,7 @@ function yourls_plugins_sort_callback( $plugin_a, $plugin_b ) {
*
* @codeCoverageIgnore
* @since 1.5.1
* @return void
*/
function yourls_shutdown() {
yourls_do_action( 'shutdown' );

View File

@ -84,6 +84,7 @@ function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
yourls_trim_long_string($url), preg_replace('!https?://!', '', yourls_get_yourls_site()) . '/'. $url_exists->keyword );
$return['title'] = $url_exists->title;
$return['shorturl'] = yourls_link($url_exists->keyword);
$return['errorCode'] = $return['statusCode'] = '400'; // 400 Bad Request
return yourls_apply_filter( 'add_new_link_already_stored_filter', $return, $url, $keyword, $title );
}
@ -168,11 +169,6 @@ function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
* @return string Acceptable charset for short URLS keywords
*/
function yourls_get_shorturl_charset() {
static $charset = null;
if ( $charset !== null ) {
return $charset;
}
if ( defined( 'YOURLS_URL_CONVERT' ) && in_array( YOURLS_URL_CONVERT, [ 62, 64 ] ) ) {
$charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
@ -233,6 +229,8 @@ function yourls_keyword_is_reserved( $keyword ) {
/**
* Delete a link in the DB
*
* @param string $keyword Short URL keyword
* @return int Number of links deleted
*/
function yourls_delete_link_by_keyword( $keyword ) {
// Allow plugins to short-circuit the whole function
@ -251,8 +249,12 @@ function yourls_delete_link_by_keyword( $keyword ) {
/**
* SQL query to insert a new link in the DB. Returns boolean for success or failure of the inserting
*
* @param string $url
* @param string $keyword
* @param string $title
* @return bool true if insert succeeded, false if failed
*/
function yourls_insert_link_in_db( $url, $keyword, $title = '' ) {
function yourls_insert_link_in_db($url, $keyword, $title = '' ) {
$url = yourls_sanitize_url($url);
$keyword = yourls_sanitize_keyword($keyword);
$title = yourls_sanitize_title($title);
@ -304,8 +306,13 @@ function yourls_long_url_exists( $url ) {
/**
* Edit a link
*
* @param string $url
* @param string $keyword
* @param string $newkeyword
* @param string $title
* @return array Result of the edit and link information if successful
*/
function yourls_edit_link( $url, $keyword, $newkeyword='', $title='' ) {
function yourls_edit_link($url, $keyword, $newkeyword='', $title='' ) {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_edit_link', null, $keyword, $url, $keyword, $newkeyword, $title );
if ( null !== $pre )
@ -375,6 +382,9 @@ function yourls_edit_link( $url, $keyword, $newkeyword='', $title='' ) {
/**
* Update a title link (no checks for duplicates etc..)
*
* @param string $keyword
* @param string $title
* @return int number of rows updated
*/
function yourls_edit_link_title( $keyword, $title ) {
// Allow plugins to short-circuit the whole function
@ -487,10 +497,14 @@ function yourls_get_keyword_infos( $keyword, $use_cache = true ) {
}
/**
* Return (string) selected information associated with a keyword. Optional $notfound = string default message if nothing found
* Return information associated with a keyword (eg clicks, URL, title...). Optional $notfound = string default message if nothing found
*
* @param string $keyword Short URL keyword
* @param string $field Field to return (eg 'url', 'title', 'ip', 'clicks', 'timestamp', 'keyword')
* @param false|string $notfound Optional string to return if keyword not found
* @return mixed|string
*/
function yourls_get_keyword_info( $keyword, $field, $notfound = false ) {
function yourls_get_keyword_info($keyword, $field, $notfound = false ) {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_get_keyword_info', false, $keyword, $field, $notfound );
@ -510,6 +524,9 @@ function yourls_get_keyword_info( $keyword, $field, $notfound = false ) {
/**
* Return title associated with keyword. Optional $notfound = string default message if nothing found
*
* @param string $keyword Short URL keyword
* @param false|string $notfound Optional string to return if keyword not found
* @return mixed|string
*/
function yourls_get_keyword_title( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'title', $notfound );
@ -518,6 +535,9 @@ function yourls_get_keyword_title( $keyword, $notfound = false ) {
/**
* Return long URL associated with keyword. Optional $notfound = string default message if nothing found
*
* @param string $keyword Short URL keyword
* @param false|string $notfound Optional string to return if keyword not found
* @return mixed|string
*/
function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'url', $notfound );
@ -526,6 +546,9 @@ function yourls_get_keyword_longurl( $keyword, $notfound = false ) {
/**
* Return number of clicks on a keyword. Optional $notfound = string default message if nothing found
*
* @param string $keyword Short URL keyword
* @param false|string $notfound Optional string to return if keyword not found
* @return mixed|string
*/
function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'clicks', $notfound );
@ -534,6 +557,9 @@ function yourls_get_keyword_clicks( $keyword, $notfound = false ) {
/**
* Return IP that added a keyword. Optional $notfound = string default message if nothing found
*
* @param string $keyword Short URL keyword
* @param false|string $notfound Optional string to return if keyword not found
* @return mixed|string
*/
function yourls_get_keyword_IP( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'ip', $notfound );
@ -542,6 +568,9 @@ function yourls_get_keyword_IP( $keyword, $notfound = false ) {
/**
* Return timestamp associated with a keyword. Optional $notfound = string default message if nothing found
*
* @param string $keyword Short URL keyword
* @param false|string $notfound Optional string to return if keyword not found
* @return mixed|string
*/
function yourls_get_keyword_timestamp( $keyword, $notfound = false ) {
return yourls_get_keyword_info( $keyword, 'timestamp', $notfound );

View File

@ -7,8 +7,14 @@
* rather than using the YOURLS version number, eg yourls_update_to_18(). This is to allow having
* multiple SQL update during the dev cycle of the same Y0URLS version
*
* @param string|int $step
* @param string $oldver
* @param string $newver
* @param string|int $oldsql
* @param string|int $newsql
* @return void
*/
function yourls_upgrade( $step, $oldver, $newver, $oldsql, $newsql ) {
function yourls_upgrade($step, $oldver, $newver, $oldsql, $newsql ) {
/**
* Sanitize input. Two notes :
@ -206,8 +212,8 @@ function yourls_upgrade_to_143( ) {
*/
function yourls_upgrade_to_141( ) {
// Kill old cookies from 1.3 and prior
setcookie('yourls_username', null, time() - 3600 );
setcookie('yourls_password', null, time() - 3600 );
setcookie('yourls_username', '', time() - 3600 );
setcookie('yourls_password', '', time() - 3600 );
// alter table URL
yourls_alter_url_table_to_141();
// recreate the htaccess file if needed
@ -445,4 +451,3 @@ function yourls_clean_htaccess_for_14() {
return $result;
}

View File

@ -6,7 +6,8 @@
/**
* Make an optimized regexp pattern from a string of characters
* @param $string
*
* @param string $string
* @return string
*/
function yourls_make_regexp_pattern( $string ) {
@ -16,7 +17,8 @@ function yourls_make_regexp_pattern( $string ) {
}
/**
* Function: Get client IP Address. Returns a DB safe string.
* Get client IP Address. Returns a DB safe string.
*
* @return string
*/
function yourls_get_IP() {
@ -52,17 +54,17 @@ function yourls_get_next_decimal() {
* Update id for next link with no custom keyword
*
* Note: this function relies upon yourls_update_option(), which will return either true or false
* depending if there has been an actual MySQL query updating the DB.
* depending upon if there has been an actual MySQL query updating the DB.
* In other words, this function may return false yet this would not mean it has functionally failed
* In other words I'm not sure we really need this function to return something :face_with_eyes_looking_up:
* In other words I'm not sure if we really need this function to return something :face_with_eyes_looking_up:
* See issue 2621 for more on this.
*
* @since 1.0
* @param integer $int id for next link
* @return bool true or false depending on if there has been an actual MySQL query. See note above.
* @param integer $int id for next link
* @return bool true or false depending on if there has been an actual MySQL query. See note above.
*/
function yourls_update_next_decimal( $int = '' ) {
$int = ( $int == '' ) ? yourls_get_next_decimal() + 1 : (int)$int ;
function yourls_update_next_decimal( $int = 0 ) {
$int = ( $int == 0 ) ? yourls_get_next_decimal() + 1 : (int)$int ;
$update = yourls_update_option( 'next_id', $int );
yourls_do_action( 'update_next_decimal', $int, $update );
return $update;
@ -70,18 +72,20 @@ function yourls_update_next_decimal( $int = '' ) {
/**
* Return XML output.
*
* @param array $array
* @return string
*/
function yourls_xml_encode( $array ) {
return (\Spatie\ArrayToXml\ArrayToXml::convert($array));
return (\Spatie\ArrayToXml\ArrayToXml::convert($array, '', true, 'UTF-8'));
}
/**
* Update click count on a short URL. Return 0/1 for error/success.
*
* @param string $keyword
* @param bool $clicks
* @return mixed|string
* @param false|int $clicks
* @return int 0 or 1 for error/success
*/
function yourls_update_clicks( $keyword, $clicks = false ) {
// Allow plugins to short-circuit the whole function
@ -112,11 +116,16 @@ function yourls_update_clicks( $keyword, $clicks = false ) {
return $result;
}
/**
* Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return
*
* @param string $filter 'bottom', 'last', 'rand' or 'top'
* @param int $limit Number of links to return
* @param int $start Offset to start from
* @return array Array of links
*/
function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
function yourls_get_stats($filter = 'top', $limit = 10, $start = 0) {
switch( $filter ) {
case 'bottom':
$sort_by = '`clicks`';
@ -175,7 +184,7 @@ function yourls_get_stats( $filter = 'top', $limit = 10, $start = 0 ) {
* $where['sql'] will concatenate SQL clauses: $where['sql'] = ' AND something = :value AND otherthing < :othervalue';
* $where['binds'] will hold the (name => value) placeholder pairs: $where['binds'] = array('value' => $value, 'othervalue' => $value2)
*
* @param $where array See comment above
* @param array $where See comment above
* @return array
*/
function yourls_get_db_stats( $where = [ 'sql' => '', 'binds' => [] ] ) {
@ -190,6 +199,7 @@ function yourls_get_db_stats( $where = [ 'sql' => '', 'binds' => [] ] ) {
/**
* Returns a sanitized a user agent string. Given what I found on http://www.user-agents.org/ it should be OK.
*
* @return string
*/
function yourls_get_user_agent() {
$ua = '-';
@ -260,6 +270,7 @@ function yourls_redirect( $location, $code = 301 ) {
* @since 1.7.3
* @param string $url
* @param string $keyword
* @return void
*/
function yourls_redirect_shorturl($url, $keyword) {
yourls_do_action( 'redirect_shorturl', $url, $keyword );
@ -364,6 +375,7 @@ function yourls_status_header( $code = 200 ) {
*
* @param string $location
* @param bool $dontwait
* @return void
*/
function yourls_redirect_javascript( $location, $dontwait = true ) {
yourls_do_action( 'pre_redirect_javascript', $location, $dontwait );
@ -385,6 +397,7 @@ REDIR;
/**
* Return an HTTP status code
*
* @param int $code
* @return string
*/
@ -448,7 +461,7 @@ function yourls_get_HTTP_status( $code ) {
510 => 'Not Extended'
];
return isset( $headers_desc[ $code ] ) ? $headers_desc[ $code ] : '';
return $headers_desc[$code] ?? '';
}
/**
@ -496,6 +509,7 @@ function yourls_log_redirect( $keyword ) {
/**
* Check if we want to not log redirects (for stats)
*
* @return bool
*/
function yourls_do_log_redirect() {
return ( !defined( 'YOURLS_NOSTATS' ) || YOURLS_NOSTATS != true );
@ -503,6 +517,7 @@ function yourls_do_log_redirect() {
/**
* Check if an upgrade is needed
*
* @return bool
*/
function yourls_upgrade_is_needed() {
@ -522,6 +537,7 @@ function yourls_upgrade_is_needed() {
/**
* Get current version & db version as stored in the options DB. Prior to 1.4 there's no option table.
*
* @return array
*/
function yourls_get_current_version_from_sql() {
@ -542,6 +558,7 @@ function yourls_get_current_version_from_sql() {
/**
* Determine if the current page is private
*
* @return bool
*/
function yourls_is_private() {
$private = defined( 'YOURLS_PRIVATE' ) && YOURLS_PRIVATE;
@ -566,6 +583,7 @@ function yourls_is_private() {
/**
* Allow several short URLs for the same long URL ?
*
* @return bool
*/
function yourls_allow_duplicate_longurls() {
@ -579,6 +597,7 @@ function yourls_allow_duplicate_longurls() {
/**
* Check if an IP shortens URL too fast to prevent DB flood. Return true, or die.
*
* @param string $ip
* @return bool|mixed|string
*/
@ -735,6 +754,7 @@ function yourls_rnd_string ( $length = 5, $type = 0, $charlist = '' ) {
/**
* Check if we're in API mode.
*
* @return bool
*/
function yourls_is_API() {
@ -743,6 +763,7 @@ function yourls_is_API() {
/**
* Check if we're in Ajax mode.
*
* @return bool
*/
function yourls_is_Ajax() {
@ -751,6 +772,7 @@ function yourls_is_Ajax() {
/**
* Check if we're in GO mode (yourls-go.php).
*
* @return bool
*/
function yourls_is_GO() {
@ -760,14 +782,16 @@ function yourls_is_GO() {
/**
* Check if we're displaying stats infos (yourls-infos.php). Returns bool
*
* @return bool
*/
function yourls_is_infos() {
return (bool)yourls_apply_filter( 'is_infos', defined( 'YOURLS_INFOS' ) && YOURLS_INFOS );
}
/**
* Check if we're in the admin area. Returns bool
* Check if we're in the admin area. Returns bool. Does not relate with user rights.
*
* @return bool
*/
function yourls_is_admin() {
return (bool)yourls_apply_filter( 'is_admin', defined( 'YOURLS_ADMIN' ) && YOURLS_ADMIN );
@ -776,6 +800,7 @@ function yourls_is_admin() {
/**
* Check if the server seems to be running on Windows. Not exactly sure how reliable this is.
*
* @return bool
*/
function yourls_is_windows() {
return defined( 'DIRECTORY_SEPARATOR' ) && DIRECTORY_SEPARATOR == '\\';
@ -783,6 +808,7 @@ function yourls_is_windows() {
/**
* Check if SSL is required.
*
* @return bool
*/
function yourls_needs_ssl() {
@ -791,6 +817,7 @@ function yourls_needs_ssl() {
/**
* Check if SSL is used. Stolen from WP.
*
* @return bool
*/
function yourls_is_ssl() {
@ -902,6 +929,7 @@ function yourls_get_remote_title( $url ) {
/**
* Quick UA check for mobile devices.
*
* @return bool
*/
function yourls_is_mobile_device() {
@ -935,7 +963,7 @@ function yourls_is_mobile_device() {
* @param string $uri Optional, page requested (default to $_SERVER['REQUEST_URI'] eg '/yourls/abcd' )
* @return string request relative to YOURLS base (eg 'abdc')
*/
function yourls_get_request($yourls_site = false, $uri = false) {
function yourls_get_request($yourls_site = '', $uri = '') {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_get_request', false );
if ( false !== $pre ) {
@ -945,10 +973,10 @@ function yourls_get_request($yourls_site = false, $uri = false) {
yourls_do_action( 'pre_get_request', $yourls_site, $uri );
// Default values
if ( false === $yourls_site ) {
if ( '' === $yourls_site ) {
$yourls_site = yourls_get_yourls_site();
}
if ( false === $uri ) {
if ( '' === $uri ) {
$uri = $_SERVER[ 'REQUEST_URI' ];
}
@ -986,6 +1014,7 @@ function yourls_get_request($yourls_site = false, $uri = false) {
/**
* Fix $_SERVER['REQUEST_URI'] variable for various setups. Stolen from WP.
*
* @return void
*/
function yourls_fix_request_uri() {
@ -1033,19 +1062,21 @@ function yourls_fix_request_uri() {
/**
* Check for maintenance mode. If yes, die. See yourls_maintenance_mode(). Stolen from WP.
*
* @return void
*/
function yourls_check_maintenance_mode() {
$file = YOURLS_ABSPATH . '/.maintenance' ;
if ( !file_exists( $file ) || yourls_is_upgrading() || yourls_is_installing() )
return;
if ( !file_exists( $file ) || yourls_is_upgrading() || yourls_is_installing() ) {
return;
}
global $maintenance_start;
include_once( $file );
// If the $maintenance_start timestamp is older than 10 minutes, don't die.
if ( ( time() - $maintenance_start ) >= 600 )
return;
if ( ( time() - $maintenance_start ) >= 600 ) {
return;
}
// Use any /user/maintenance.php file
if( file_exists( YOURLS_USERDIR.'/maintenance.php' ) ) {
@ -1053,29 +1084,11 @@ function yourls_check_maintenance_mode() {
die();
}
// https://www.youtube.com/watch?v=Xw-m4jEY-Ns
// Or use the default messages
$title = yourls__( 'Service temporarily unavailable' );
$message = yourls__( 'Our service is currently undergoing scheduled maintenance.' ) . "</p>\n<p>" .
yourls__( 'Things should not last very long, thank you for your patience and please excuse the inconvenience' );
yourls_die( $message, $title , 503 );
}
/**
* Return current admin page, or null if not an admin page
*
* @return mixed string if admin page, null if not an admin page
* @since 1.6
*/
function yourls_current_admin_page() {
if( yourls_is_admin() ) {
$current = substr( yourls_get_request(), 6 );
if( $current === false )
$current = 'index.php'; // if current page is http://sho.rt/admin/ instead of http://sho.rt/admin/index.php
return $current;
}
return null;
}
/**
@ -1155,7 +1168,7 @@ function yourls_get_relative_url( $url, $strict = true ) {
}
/**
* Marks a function as deprecated and informs when it has been used. Stolen from WP.
* Marks a function as deprecated and informs that it has been used. Stolen from WP.
*
* There is a hook deprecated_function that will be called that can be used
* to get the backtrace up to what file and function called the deprecated
@ -1174,6 +1187,7 @@ function yourls_get_relative_url( $url, $strict = true ) {
* @param string $function The function that was called
* @param string $version The version of WordPress that deprecated the function
* @param string $replacement Optional. The function that should have been called
* @return void
*/
function yourls_deprecated_function( $function, $version, $replacement = null ) {
@ -1188,19 +1202,6 @@ function yourls_deprecated_function( $function, $version, $replacement = null )
}
}
/**
* Return the value if not an empty string
*
* Used with array_filter(), to remove empty keys but not keys with value 0 or false
*
* @since 1.6
* @param mixed $val Value to test against ''
* @return bool True if not an empty string
*/
function yourls_return_if_not_empty_string( $val ) {
return ( $val !== '' );
}
/**
* Explode a URL in an array of ( 'protocol' , 'slashes if any', 'rest of the URL' )
*
@ -1243,14 +1244,14 @@ function yourls_get_protocol_slashes_and_rest( $url, $array = [ 'protocol', 'sla
}
/**
* Set URL scheme (to HTTP or HTTPS)
* Set URL scheme (HTTP or HTTPS) to a URL
*
* @since 1.7.1
* @param string $url URL
* @param string $scheme scheme, either 'http' or 'https'
* @return string URL with chosen scheme
*/
function yourls_set_url_scheme( $url, $scheme = false ) {
function yourls_set_url_scheme( $url, $scheme = '' ) {
if ( in_array( $scheme, [ 'http', 'https' ] ) ) {
$url = preg_replace( '!^[a-zA-Z0-9+.-]+://!', $scheme.'://', $url );
}
@ -1260,10 +1261,11 @@ function yourls_set_url_scheme( $url, $scheme = false ) {
/**
* Tell if there is a new YOURLS version
*
* This function checks, if needed, if there's a new version of YOURLS and, if applicable, display
* This function checks, if needed, if there's a new version of YOURLS and, if applicable, displays
* an update notice.
*
* @since 1.7.3
* @return void
*/
function yourls_tell_if_new_version() {
yourls_debug_log( 'Check for new version: '.( yourls_maybe_check_core_version() ? 'yes' : 'no' ) );

View File

@ -9,7 +9,7 @@
* MAJOR.MINOR.PATCH-SOMETHING (1.8.1-donotuse)
*
*/
define( 'YOURLS_VERSION', '1.9' );
define( 'YOURLS_VERSION', '1.9.1' );
/**
* YOURLS DB version. Increments when changes are made to the DB schema, to trigger a DB update