[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/ -> functions-http.php (summary)

Functions that relate to HTTP requests On functions using the 3rd party library Requests: Their goal here is to provide convenient wrapper functions to the Requests library. There are 2 types of functions for each METHOD, where METHOD is 'get' or 'post' (implement more as needed) - yourls_http_METHOD() : Return a complete Response object (with ->body, ->headers, ->status_code, etc...) or a simple string (error message) - yourls_http_METHOD_body() : Return a string (response body) or null if there was an error

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

Defines 24 functions

  yourls_http_get()
  yourls_http_get_body()
  yourls_http_post()
  yourls_http_post_body()
  yourls_http_get_proxy()
  yourls_http_get_proxy_bypass_host()
  yourls_http_default_options()
  yourls_send_through_proxy()
  yourls_resolve_host()
  yourls_ip_is_local()
  yourls_host_is_local()
  yourls_restrict_remote_title_fetch()
  yourls_http_options_no_local_redirect()
  yourls_http_abort_local_redirect()
  yourls_http_request()
  yourls_http_user_agent()
  yourls_check_core_version()
  yourls_validate_core_version_response()
  yourls_get_version_from_zipball_url()
  yourls_is_valid_github_repo_url()
  yourls_validate_core_version_response_keys()
  yourls_maybe_check_core_version()
  yourls_skip_version_check()
  yourls_can_http_over_ssl()

Functions
Functions that are not part of a class:

yourls_http_get( $url, $headers = array()   X-Ref
Perform a GET request, return response object or error string message

Notable object properties: body, headers, status_code

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

yourls_http_get_body( $url, $headers = array()   X-Ref
Perform a GET request, return body or null if there was an error

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

yourls_http_post( $url, $headers = array()   X-Ref
Perform a POST request, return response object

Notable object properties: body, headers, status_code

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

yourls_http_post_body( $url, $headers = array()   X-Ref
Perform a POST request, return body

Wrapper for yourls_http_request()

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

yourls_http_get_proxy()   X-Ref
Get proxy information

since: 1.7.1
return: mixed false if no proxy is defined, or string like '10.0.0.201:3128' or array like ('10.0.0.201:3128', 'username', 'password')

yourls_http_get_proxy_bypass_host()   X-Ref
Get list of hosts that should bypass the proxy

since: 1.7.1
return: mixed false if no host defined, or string like "example.com, *.mycorp.com"

yourls_http_default_options()   X-Ref
Default HTTP requests options for YOURLS

For a list of all available options, see function request() in /includes/Requests/Requests.php

since: 1.7
return: array Options

yourls_send_through_proxy( $url )   X-Ref
Whether URL should be sent through the proxy server.

Concept stolen from WordPress. The idea is to allow some URLs, including localhost and the YOURLS install itself,
to be requested directly and bypassing any defined proxy.

since: 1.7
param: string $url URL to check
return: bool true to request through proxy, false to request directly

yourls_resolve_host(string $host)   X-Ref
Resolve a host name to a list of IP addresses

Returns every A and AAAA record found for $host, or an empty array if the host cannot be
resolved. Does not check the addresses in any way, see yourls_host_is_local() for this.

since: 1.10.5
param: string $host Host name to resolve (no brackets around IPv6 literals)
return: array       Array of IP addresses as strings, empty array if resolution failed

yourls_ip_is_local(string $ip)   X-Ref
Check if an IP address is not a public one (loopback, private, reserved or link-local)

IPv6 addresses that embed an IPv4 one are checked on the IPv4 they wrap, since this is where the
traffic ends up. PHP considers all of these public on its own.

Anything that is not a valid IP is considered non-public.

since: 1.10.5
param: string $ip IP address, v4 or v6
return: bool      true if the address is not public, or not an IP at all

yourls_host_is_local(string $host)   X-Ref
Check if a host points to a non-public address (loopback, private, reserved or link-local)

Accepts either a host name or an IP literal. A host name is resolved first, and considered
local as soon as one of its addresses is not public. A host that cannot be resolved is
considered local too.

Known limitation: this does not protect against DNS rebinding (attacker controlling a DNS server
with 0s TTL refresh, where evil-url.com could point to 1.2.3.4 (public) and then the next second point
to 10.0.0.1 (private). Let's consider this a low risk, and not worth the complexity of a DNS cache with TTL awareness.

since: 1.10.5
param: string $host Host name or IP address. IPv6 literals can be bracketed or not.
return: bool        true if the host is not a public address or cannot be resolved

yourls_restrict_remote_title_fetch()   X-Ref
Check if the destination of a remote title fetch must be restricted to public addresses

We want to avoid the situation where a public install of YOURLS is used to fetch titles from internal hosts,
and potentially leak information about them (SSRF and port scan / service discovery)
On a private install, the user is authenticated and (hopefully) trusted.

Public install can be: YOURLS_PRIVATE set to false, or having a public interface on top of a regular private
install. Checking constant YOURLS_USER covers both cases at once.

A one-liner plugin disables the filtering entirely (say, public install on a private network):
// Disable the restriction on remote title fetches (allow internal hosts)
yourls_add_filter( 'restrict_remote_title_fetch', 'yourls_return_false' );

since: 1.10.5
return: bool  true if the fetch destination must be restricted to public addresses

yourls_http_options_no_local_redirect()   X-Ref
HTTP request options that make a request fail when it is redirected to a non-public host

Redirects are still followed: dropping them would break 'http -> https', 'example.com ->
www.example.com', URL shorteners, or any legit 30x redirect. Instead, every hop
is checked before it is requested.

Meant to be merged into the $options of a single yourls_http_*() call, not to be added to
yourls_http_default_options() -- other requests (core version check, plugins) are not
triggered by an untrusted party.

since: 1.10.5
return: array  Options to pass to yourls_http_get()

yourls_http_abort_local_redirect(string $location)   X-Ref
Callback on the 'requests.before_redirect' hook: abort if the redirect target is not public

The exception thrown is a \WpOrg\Requests\Exception and not a plain \Exception, because this
is what yourls_http_request() catches -- anything else would escape and fatal.

since: 1.10.5
param: string $location URL the request is about to be redirected to
return: void

yourls_http_request( $type, $url, $headers, $data, $options )   X-Ref
Perform a HTTP request, return response object

since: 1.7
param: string $type HTTP request type (GET, POST)
param: string $url URL to request
param: array $headers Extra headers to send with the request
param: array $data Data to send either as a query string for GET requests, or in the body for POST requests
param: array $options Options for the request (see /includes/Requests/Requests.php:request())
return: object WpOrg\Requests\Response object

yourls_http_user_agent()   X-Ref
Return funky user agent string

since: 1.5
return: string UA string

yourls_check_core_version()   X-Ref
Check api.yourls.org if there's a newer version of YOURLS

This function collects various stats to help us improve YOURLS. See the blog post about it:
http://blog.yourls.org/2014/01/on-yourls-1-7-and-api-yourls-org/
Results of requests sent to api.yourls.org are stored in option 'core_version_checks' and is an object
with the following properties:
- failed_attempts : number of consecutive failed attempts
- last_attempt    : time() of last attempt
- last_result     : content retrieved from api.yourls.org during previous check
- version_checked : installed YOURLS version that was last checked

since: 1.7
return: mixed JSON data if api.yourls.org successfully requested, false otherwise

yourls_validate_core_version_response($json)   X-Ref
Make sure response from api.yourls.org is valid

1) we should get a json object with two following properties:
'latest' => a string representing a YOURLS version number, eg '1.2.3'
'zipurl' => a string for a zip package URL, from github, eg 'https://api.github.com/repos/YOURLS/YOURLS/zipball/1.2.3'
2) 'latest' and version extracted from 'zipurl' should match
3) the object should not contain any other key

since: 1.7.7
param: object $json  JSON object to check
return: bool   true if seems legit, false otherwise

yourls_get_version_from_zipball_url($zipurl)   X-Ref
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

yourls_is_valid_github_repo_url($url)   X-Ref
Check if URL is from YOURLS/YOURLS repo on github

since: 1.8.3
param: string $url  URL to check
return: bool

yourls_validate_core_version_response_keys($json)   X-Ref
Check if object has only expected keys 'latest' and 'zipurl' containing strings

since: 1.8.3
param: object $json
return: bool

yourls_maybe_check_core_version()   X-Ref
Determine if we want to check for a newer YOURLS version (and check if applicable)

Currently checks are performed every 24h and only when someone is visiting an admin page.
In the future (1.8?) maybe check with cronjob emulation instead.

since: 1.7
return: bool true if a check was needed and successfully performed, false otherwise

yourls_skip_version_check()   X-Ref
Check if user setting for skipping version check is set

since: 1.8.2
return: bool

yourls_can_http_over_ssl()   X-Ref
Check if server can perform HTTPS requests, return bool

since: 1.7.1
return: bool whether the server can perform HTTP requests over SSL



Generated: Mon Nov 9 05:10:31 2026 Cross-referenced by PHPXref 0.7.1