[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/ -> functions.php (summary)

(no description)

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

Defines 52 functions

  yourls_make_regexp_pattern()
  yourls_get_IP()
  yourls_get_next_decimal()
  yourls_update_next_decimal()
  yourls_xml_encode()
  yourls_update_clicks()
  yourls_get_stats()
  yourls_get_db_stats()
  yourls_get_user_agent()
  yourls_get_referrer()
  yourls_redirect()
  yourls_redirect_shorturl()
  yourls_robots_tag_header()
  yourls_no_cache_headers()
  yourls_no_frame_header()
  yourls_content_type_header()
  yourls_status_header()
  yourls_redirect_javascript()
  yourls_get_HTTP_status()
  yourls_log_redirect()
  yourls_do_log_redirect()
  yourls_upgrade_is_needed()
  yourls_get_current_version_from_sql()
  yourls_is_private()
  yourls_allow_duplicate_longurls()
  yourls_check_IP_flood()
  yourls_is_installing()
  yourls_is_upgrading()
  yourls_is_installed()
  yourls_set_installed()
  yourls_rnd_string()
  yourls_is_API()
  yourls_is_Ajax()
  yourls_is_GO()
  yourls_is_infos()
  yourls_is_admin()
  yourls_is_windows()
  yourls_needs_ssl()
  yourls_is_ssl()
  yourls_get_remote_title()
  yourls_is_mobile_device()
  yourls_get_request()
  yourls_fix_request_uri()
  yourls_check_maintenance_mode()
  yourls_is_allowed_protocol()
  yourls_get_protocol()
  yourls_get_relative_url()
  yourls_deprecated_function()
  yourls_get_protocol_slashes_and_rest()
  yourls_set_url_scheme()
  yourls_tell_if_new_version()
  yourls_include_file_sandbox()

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

param: string $string
return: 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
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.

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

param: array $array
return: string

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

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

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

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

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)

param: array $where See comment above
return: array

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
param: string $location      URL to redirect to
param: int    $code          HTTP status code to send
return: int                  1 for header redirection, 2 for js redirection, 3 otherwise (CLI)

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 an X-Robots-Tag header to control indexing of a page.

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

yourls_robots_tag_header()   X-Ref
Send an X-Robots-Tag header. See #3486

since: 1.9.2
return: void

yourls_no_cache_headers()   X-Ref
Send headers to explicitly 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
param: string $type content type ('text/html', 'application/json', ...)
return: bool whether header was sent

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

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

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)

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

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

param: int $code
return: string

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
param: string $keyword short URL keyword
return: mixed Result of the INSERT query (1 on success)

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.

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

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
param: bool $bool whether YOURLS is installed or not
return: void

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

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

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.

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

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
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' )
return: string               request relative to YOURLS base (eg 'abdc')

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

We also strip $_COOKIE from $_REQUEST to allow our lazy using $_REQUEST without 3rd party cookie interfering.
See #3383 for explanation.

since: 1.5.1
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
param: string $url URL to be check
param: array $protocols Optional. Array of protocols, defaults to global $yourls_allowedprotocols
return: bool true if protocol allowed, false otherwise

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
param: string $url URL to be check
return: string Protocol, with slash slash if applicable. Empty string if no protocol

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
param: string $url URL to relativize
param: bool $strict if true and if URL isn't relative to YOURLS install, return empty string
return: string URL

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
function.

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
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

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:[email protected]?subject=hey' :
array( 'protocol' => 'mailto:', 'slashes' => '', 'rest' => '[email protected]?subject=hey' )

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

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

yourls_set_url_scheme( $url, $scheme = '' )   X-Ref
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

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

yourls_include_file_sandbox($file)   X-Ref
File include sandbox

Attempt to include a PHP file, fail with an error message if the file isn't valid PHP code.
This function does not check first if the file exists : depending on use case, you may check first.

since: 1.9.2
param: string $file filename (full path)
return: string|bool  string if error, true if success



Generated: Fri Apr 18 05:10:03 2025 Cross-referenced by PHPXref 0.7.1