[ Index ]

PHP Cross Reference of YOURLS




/includes/ -> functions.php (summary)

(no description)

File Size: 1273 lines (41 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 50 functions


Functions that are not part of a class:

yourls_make_regexp_pattern( $string )   X-Ref
Make an optimized regexp pattern from a string of characters

return: string
param: string $string

yourls_get_IP()   X-Ref
Get client IP Address. Returns a DB safe string.

return: string

yourls_get_next_decimal()   X-Ref
Get next id a new link will have if no custom keyword provided

since: 1.0
return: int            id of next link

yourls_update_next_decimal( $int = 0 )   X-Ref
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 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 if we really need this function to return something :face_with_eyes_looking_up:
See issue 2621 for more on this.

since: 1.0
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

yourls_xml_encode( $array )   X-Ref
Return XML output.

return: string
param: array $array

yourls_update_clicks( $keyword, $clicks = false )   X-Ref
Update click count on a short URL. Return 0/1 for error/success.

return: int 0 or 1 for error/success
param: string $keyword
param: false|int $clicks

yourls_get_stats($filter = 'top', $limit = 10, $start = 0)   X-Ref
Return array of stats. (string)$filter is 'bottom', 'last', 'rand' or 'top'. (int)$limit is the number of links to return

return: array          Array of links
param: string $filter  'bottom', 'last', 'rand' or 'top'
param: int $limit      Number of links to return
param: int $start      Offset to start from

yourls_get_db_stats( $where = [ 'sql' => '', 'binds' => [] ] )   X-Ref
Get total number of URLs and sum of clicks. Input: optional "AND WHERE" clause. Returns array

The $where parameter will contain additional SQL arguments:
$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)

return: array
param: array $where See comment above

yourls_get_user_agent()   X-Ref
Returns a sanitized a user agent string. Given what I found on http://www.user-agents.org/ it should be OK.

return: string

yourls_get_referrer()   X-Ref
Returns the sanitized referrer submitted by the browser.

return: string               HTTP Referrer or 'direct' if no referrer was provided

yourls_redirect( $location, $code = 301 )   X-Ref
Redirect to another page

YOURLS redirection, either to internal or external URLs. If headers have not been sent, redirection
is achieved with PHP's header(). If headers have been sent already and we're not in a command line
client, redirection occurs with Javascript.

Note: yourls_redirect() does not exit automatically, and should almost always be followed by a call to exit()
to prevent the script from continuing.

since: 1.4
return: int                  1 for header redirection, 2 for js redirection, 3 otherwise (CLI)
param: string $location      URL to redirect to
param: int    $code          HTTP status code to send

yourls_redirect_shorturl($url, $keyword)   X-Ref
Redirect to an existing short URL

Redirect client to an existing short URL (no check performed) and execute misc tasks: update
clicks for short URL, update logs, and send a nocache header to prevent bots indexing short
URLS (see #2202)

since: 1.7.3
return: void
param: string $url
param: string $keyword

yourls_no_cache_headers()   X-Ref
Send headers to explicitely tell browser not to cache content or redirection

since: 1.7.10
return: void

yourls_no_frame_header()   X-Ref
Send header to prevent display within a frame from another site (avoid clickjacking)

This header makes it impossible for an external site to display YOURLS admin within a frame,
which allows for clickjacking.
See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
This said, the whole function is shuntable : legit uses of iframes should be still possible.

since: 1.8.1
return: void|mixed

yourls_content_type_header( $type )   X-Ref
Send a filterable content type header

since: 1.7
return: bool whether header was sent
param: string $type content type ('text/html', 'application/json', ...)

yourls_status_header( $code = 200 )   X-Ref
Set HTTP status header

since: 1.4
return: bool      whether header was sent
param: int $code  status header code

yourls_redirect_javascript( $location, $dontwait = true )   X-Ref
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)

return: void
param: string $location
param: bool   $dontwait

yourls_get_HTTP_status( $code )   X-Ref
Return an HTTP status code

return: string
param: int $code

yourls_log_redirect( $keyword )   X-Ref
Log a redirect (for stats)

This function does not check for the existence of a valid keyword, in order to save a query. Make sure the keyword
exists before calling it.

since: 1.4
return: mixed Result of the INSERT query (1 on success)
param: string $keyword short URL keyword

yourls_do_log_redirect()   X-Ref
Check if we want to not log redirects (for stats)

return: bool

yourls_upgrade_is_needed()   X-Ref
Check if an upgrade is needed

return: bool

yourls_get_current_version_from_sql()   X-Ref
Get current version & db version as stored in the options DB. Prior to 1.4 there's no option table.

return: array

yourls_is_private()   X-Ref
Determine if the current page is private

return: bool

yourls_allow_duplicate_longurls()   X-Ref
Allow several short URLs for the same long URL ?

return: bool

yourls_check_IP_flood( $ip = '' )   X-Ref
Check if an IP shortens URL too fast to prevent DB flood. Return true, or die.

return: bool|mixed|string
param: string $ip

yourls_is_installing()   X-Ref
Check if YOURLS is installing

since: 1.6
return: bool

yourls_is_upgrading()   X-Ref
Check if YOURLS is upgrading

since: 1.6
return: bool

yourls_is_installed()   X-Ref
Check if YOURLS is installed

Checks property $ydb->installed that is created by yourls_get_all_options()

See inline comment for updating from 1.3 or prior.

return: bool

yourls_set_installed( $bool )   X-Ref
Set installed state

since: 1.7.3
return: void
param: bool $bool whether YOURLS is installed or not

yourls_rnd_string( $length = 5, $type = 0, $charlist = '' )   X-Ref
Generate random string of (int)$length length and type $type (see function for details)

return: mixed|string
param: int    $length
param: int    $type
param: string $charlist

yourls_is_API()   X-Ref
Check if we're in API mode.

return: bool

yourls_is_Ajax()   X-Ref
Check if we're in Ajax mode.

return: bool

yourls_is_GO()   X-Ref
Check if we're in GO mode (yourls-go.php).

return: bool

yourls_is_infos()   X-Ref
Check if we're displaying stats infos (yourls-infos.php). Returns bool

return: bool

yourls_is_admin()   X-Ref
Check if we're in the admin area. Returns bool. Does not relate with user rights.

return: bool

yourls_is_windows()   X-Ref
Check if the server seems to be running on Windows. Not exactly sure how reliable this is.

return: bool

yourls_needs_ssl()   X-Ref
Check if SSL is required.

return: bool

yourls_is_ssl()   X-Ref
Check if SSL is used. Stolen from WP.

return: bool

yourls_get_remote_title( $url )   X-Ref
Get a remote page title

This function returns a string: either the page title as defined in HTML, or the URL if not found
The function tries to convert funky characters found in titles to UTF8, from the detected charset.
Charset in use is guessed from HTML meta tag, or if not found, from server's 'content-type' response.

return: string Title (sanitized) or the URL if no title found
param: string $url URL

yourls_is_mobile_device()   X-Ref
Quick UA check for mobile devices.

return: bool

yourls_get_request($yourls_site = '', $uri = '')   X-Ref
Get request in YOURLS base (eg in 'http://sho.rt/yourls/abcd' get 'abdc')

With no parameter passed, this function will guess current page and consider
it is the requested page.
For testing purposes, parameters can be passed.

since: 1.5
return: string               request relative to YOURLS base (eg 'abdc')
param: string $yourls_site   Optional, YOURLS installation URL (default to constant YOURLS_SITE)
param: string $uri           Optional, page requested (default to $_SERVER['REQUEST_URI'] eg '/yourls/abcd' )

yourls_fix_request_uri()   X-Ref
Fix $_SERVER['REQUEST_URI'] variable for various setups. Stolen from WP.

return: void

yourls_check_maintenance_mode()   X-Ref
Check for maintenance mode. If yes, die. See yourls_maintenance_mode(). Stolen from WP.

return: void

yourls_is_allowed_protocol( $url, $protocols = [] )   X-Ref
Check if a URL protocol is allowed

Checks a URL against a list of whitelisted protocols. Protocols must be defined with
their complete scheme name, ie 'stuff:' or 'stuff://' (for instance, 'mailto:' is a valid
protocol, 'mailto://' isn't, and 'http:' with no double slashed isn't either

since: 1.6
return: bool true if protocol allowed, false otherwise
param: string $url URL to be check
param: array $protocols Optional. Array of protocols, defaults to global $yourls_allowedprotocols

yourls_get_protocol( $url )   X-Ref
Get protocol from a URL (eg mailto:, http:// ...)

What we liberally call a "protocol" in YOURLS is the scheme name + colon + double slashes if present of a URI. Examples:
"something://blah" -> "something://"
"something:blah"   -> "something:"
"something:/blah"  -> "something:"

Unit Tests for this function are located in tests/format/urls.php

since: 1.6
return: string Protocol, with slash slash if applicable. Empty string if no protocol
param: string $url URL to be check

yourls_get_relative_url( $url, $strict = true )   X-Ref
Get relative URL (eg 'abc' from 'http://sho.rt/abc')

Treat indifferently http & https. If a URL isn't relative to the YOURLS install, return it as is
or return empty string if $strict is true

since: 1.6
return: string URL
param: string $url URL to relativize
param: bool $strict if true and if URL isn't relative to YOURLS install, return empty string

yourls_deprecated_function( $function, $version, $replacement = null )   X-Ref
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

The current behavior is to trigger a user error if YOURLS_DEBUG is true.

This function is to be used in every function that is deprecated.

since: 1.6
return: void
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

yourls_get_protocol_slashes_and_rest( $url, $array = [ 'protocol', 'slashes', 'rest' ] )   X-Ref
Explode a URL in an array of ( 'protocol' , 'slashes if any', 'rest of the URL' )

Some hosts trip up when a query string contains 'http://' - see http://git.io/j1FlJg
The idea is that instead of passing the whole URL to a bookmarklet, eg index.php?u=http://blah.com,
we pass it by pieces to fool the server, eg index.php?proto=http:&slashes=//&rest=blah.com

Known limitation: this won't work if the rest of the URL itself contains 'http://', for example
if rest = blah.com/file.php?url=http://foo.com

Sample returns:

with 'mailto:jsmith@example.com?subject=hey' :
array( 'protocol' => 'mailto:', 'slashes' => '', 'rest' => 'jsmith@example.com?subject=hey' )

with 'http://example.com/blah.html' :
array( 'protocol' => 'http:', 'slashes' => '//', 'rest' => 'example.com/blah.html' )

since: 1.7
return: array|false false if no protocol found, array of ('protocol' , 'slashes', 'rest') otherwise
param: string $url URL to be parsed
param: array $array Optional, array of key names to be used in returned array

yourls_set_url_scheme( $url, $scheme = '' )   X-Ref
Set URL scheme (HTTP or HTTPS) to a URL

since: 1.7.1
return: string URL with chosen scheme
param: string $url    URL
param: string $scheme scheme, either 'http' or 'https'

yourls_tell_if_new_version()   X-Ref
Tell if there is a new YOURLS version

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

Generated: Wed Sep 28 05:10:02 2022 Cross-referenced by PHPXref 0.7.1