3 * Functions that relate to HTTP requests
8 * On functions using the 3rd party library Requests:
10 * The goal here is to provide convenient wrapper functions to the Requests library. There are
11 * 2 types of functions for each METHOD, where METHOD is 'get' or 'post' (implement more as needed)
12 * - yourls_http_METHOD() :
13 * Return a complete Response object (with ->body, ->headers, ->status_code, etc...) or
14 * a simple string (error message)
15 * - yourls_http_METHOD_body() :
16 * Return a string (response body) or null if there was an error
22 * Perform a GET request, return response object or error string message
24 * Notable object properties: body, headers, status_code
27 * @see yourls_http_request
28 * @return mixed Response object, or error string
30 function yourls_http_get( $url, $headers = array(), $data = array(), $options = array() ) {
31 return yourls_http_request( 'GET', $url, $headers, $data, $options );
35 * Perform a GET request, return body or null if there was an error
38 * @see yourls_http_request
39 * @return mixed String (page body) or null if error
41 function yourls_http_get_body( $url, $headers = array(), $data = array(), $options = array() ) {
42 $return = yourls_http_get( $url, $headers, $data, $options );
43 return isset( $return->body ) ? $return->body : null;
47 * Perform a POST request, return response object
49 * Notable object properties: body, headers, status_code
52 * @see yourls_http_request
53 * @return mixed Response object, or error string
55 function yourls_http_post( $url, $headers = array(), $data = array(), $options = array() ) {
56 return yourls_http_request( 'POST', $url, $headers, $data, $options );
60 * Perform a POST request, return body
62 * Wrapper for yourls_http_request()
65 * @see yourls_http_request
66 * @return mixed String (page body) or null if error
68 function yourls_http_post_body( $url, $headers = array(), $data = array(), $options = array() ) {
69 $return = yourls_http_post( $url, $headers, $data, $options );
70 return isset( $return->body ) ? $return->body : null;
74 * Default HTTP requests options for YOURLS
76 * For a list of all available options, see function request() in /includes/Requests/Requests.php
79 * @uses YOURLS_PROXY_USERNAME
80 * @uses YOURLS_PROXY_PASSWORD
82 * @return array Options
84 function yourls_http_default_options() {
86 'timeout' => yourls_apply_filter( 'http_default_options_timeout', 3 ),
87 'useragent' => yourls_http_user_agent(),
88 'follow_redirects' => true,
92 if( defined( 'YOURLS_PROXY' ) ) {
93 if( defined( 'YOURLS_PROXY_USERNAME' ) && defined( 'YOURLS_PROXY_PASSWORD' ) ) {
94 $options['proxy'] = array( YOURLS_PROXY, YOURLS_PROXY_USERNAME, YOURLS_PROXY_PASSWORD );
96 $options['proxy'] = YOURLS_PROXY;
100 return yourls_apply_filter( 'http_default_options', $options );
104 * Whether URL should be sent through the proxy server.
106 * Concept stolen from WordPress. The idea is to allow some URLs, including localhost and the YOURLS install itself,
107 * to be requested directly and bypassing any defined proxy.
110 * @uses YOURLS_PROXY_BYPASS_HOSTS
112 * @param string $url URL to check
113 * @return bool true to request through proxy, false to request directly
115 function yourls_send_through_proxy( $url ) {
117 // Allow plugins to short-circuit the whole function
118 $pre = yourls_apply_filter( 'shunt_send_through_proxy', null, $url );
122 $check = @parse_url( $url );
124 // Malformed URL, can not process, but this could mean ssl, so let through anyway.
125 if ( $check === false )
128 // Self and loopback URLs are considered local (':' is parse_url() host on '::1')
129 $home = parse_url( YOURLS_SITE );
130 $local = array( 'localhost', '127.0.0.1', '127.1', '[::1]', ':', $home['host'] );
132 if( in_array( $check['host'], $local ) )
135 if ( !defined( 'YOURLS_PROXY_BYPASS_HOSTS' ) )
138 // Check YOURLS_PROXY_BYPASS_HOSTS
139 static $bypass_hosts;
140 static $wildcard_regex = false;
141 if ( null == $bypass_hosts ) {
142 $bypass_hosts = preg_split( '|,\s*|', YOURLS_PROXY_BYPASS_HOSTS );
144 if ( false !== strpos( YOURLS_PROXY_BYPASS_HOSTS, '*' ) ) {
145 $wildcard_regex = array();
146 foreach ( $bypass_hosts as $host )
147 $wildcard_regex[] = str_replace( '\*', '.+', preg_quote( $host, '/' ) );
148 $wildcard_regex = '/^(' . implode( '|', $wildcard_regex ) . ')$/i';
152 if ( !empty( $wildcard_regex ) )
153 return !preg_match( $wildcard_regex, $check['host'] );
155 return !in_array( $check['host'], $bypass_hosts );
159 * Perform a HTTP request, return response object
162 * @param string $var Stuff
163 * @return string Result
165 function yourls_http_request( $type, $url, $headers, $data, $options ) {
166 yourls_http_load_library();
168 $options = array_merge( yourls_http_default_options(), $options );
170 if( defined( 'YOURLS_PROXY' ) && !yourls_send_through_proxy( $url ) )
171 unset( $options['proxy'] );
174 $result = Requests::request( $url, $headers, $data, $type, $options );
175 } catch( Requests_Exception $e ) {
176 $result = yourls_debug_log( $e->getMessage() . ' (' . $type . ' on ' . $url . ')' );
183 * Check if Requests class is defined, include Requests library if need be
185 * All HTTP functions should perform that check prior to any operation. This is to avoid
186 * include()-ing all the Requests files on every YOURLS instance disregarding whether needed or not.
190 function yourls_http_load_library() {
191 if ( !class_exists( 'Requests', false ) ) {
192 require_once dirname( __FILE__ ) . '/Requests/Requests.php';
193 Requests::register_autoloader();
198 * Deprecated. Get remote content via a GET request using best transport available
199 * Returns $content (might be an error message) or false if no transport available
202 function yourls_get_remote_content( $url, $maxlen = 4096, $timeout = 5 ) {
203 yourls_deprecated_function( __FUNCTION__, '1.7', 'yourls_http_get' );
204 return yourls_http_get_body( $url );
208 * Return funky user agent string
211 function yourls_http_user_agent() {
212 return yourls_apply_filter( 'http_user_agent', 'YOURLS v'.YOURLS_VERSION.' +http://yourls.org/ (running on '.YOURLS_SITE.')' );
216 * Check api.yourls.org if there's a newer version of YOURLS
218 * This function collects various stats to help us improve YOURLS. See https://gist.github.com/ozh/5518761
219 * Results of requests sent to api.yourls.org are stored in option 'core_version_checks' and is an object
220 * with the following properties:
221 * - failed_attempts : number of consecutive failed attempts
222 * - last_attempt : time() of last attempt
223 * - last_result : content retrieved from api.yourls.org during previous check
224 * - version_checked : installed YOURLS version that was last checked
227 * @return boolean True if api.yourls.org successfully requested, false otherwise
229 function yourls_check_core_version() {
231 if( defined( 'YOURLS_NO_VERSION_CHECK' ) && YOURLS_NO_VERSION_CHECK )
234 global $ydb, $yourls_user_passwords;
236 $checks = yourls_get_option( 'core_version_checks' );
238 // Invalidate check data when YOURLS version changes
239 if ( is_object( $checks ) && YOURLS_VERSION != $checks->version_checked ) {
243 if( !is_object( $checks ) ) {
244 $checks = new stdClass;
245 $checks->failed_attempts = 0;
246 $checks->last_attempt = 0;
247 $checks->last_result = '';
248 $checks->version_checked = YOURLS_VERSION;
251 // Config file location ('u' for '/user' or 'i' for '/includes')
252 $conf_loc = str_replace( YOURLS_ABSPATH, '', YOURLS_CONFIGFILE );
253 $conf_loc = str_replace( '/config.php', '', $conf_loc );
254 $conf_loc = ( $conf_loc == '/user' ? 'u' : 'i' );
256 // The collection of stuff to report
258 'md5' => md5( YOURLS_SITE . YOURLS_ABSPATH ), // globally unique site identifier
260 'failed_attempts' => $checks->failed_attempts,
261 'yourls_site' => YOURLS_SITE,
262 'yourls_version' => YOURLS_VERSION,
263 'php_version' => phpversion(),
264 'mysql_version' => $ydb->mysql_version(),
265 'locale' => yourls_get_locale(),
267 'db_driver' => YOURLS_DB_DRIVER,
268 'db_ext_pdo' => extension_loaded( 'pdo_mysql' ) ? 1 : 0,
269 'db_ext_mysql' => extension_loaded( 'mysql' ) ? 1 : 0,
270 'db_ext_mysqli' => extension_loaded( 'mysqli' ) ? 1 : 0,
271 'ext_curl' => extension_loaded( 'curl' ) ? 1 : 0,
273 'num_users' => count( $yourls_user_passwords ),
274 'config_location' => $conf_loc,
275 'yourls_private' => YOURLS_PRIVATE ? 1 : 0,
276 'yourls_unique' => YOURLS_UNIQUE_URLS ? 1 : 0,
277 'yourls_url_convert' => YOURLS_URL_CONVERT,
278 'num_active_plugins' => yourls_has_active_plugins(),
281 $stuff = yourls_apply_filter( 'version_check_stuff', $stuff );
284 $checks->last_attempt = time();
285 $url = 'https://api.yourls.org/core/version/1.0/';
286 $req = yourls_http_post( $url, array(), $stuff );
288 // Unexpected results ?
289 if( is_string( $req ) or !$req->success ) {
290 $checks->failed_attempts = $checks->failed_attempts + 1;
291 $checks->last_result = '';
292 yourls_update_option( 'core_version_checks', $checks );
297 $json = json_decode( trim( $req->body ) );
299 if( isset( $json->latest ) && isset( $json->zipurl ) ) {
300 // All went OK - mark this down
301 $checks->failed_attempts = 0;
302 $checks->last_result = $json;
303 $checks->version_checked = YOURLS_VERSION;
304 yourls_update_option( 'core_version_checks', $checks );
309 // Request returned actual result, but not what we expected
315 * Determine if we need to check for a newer YOURLS version
320 * @param unknown_type $a TODO
321 * @return unknown TODO
323 function yourls_maybe_check_core_version() {
326 - we're viewing an admin page
327 AND one of these cases:
328 - last_result not set
329 - failed_attempts = 0 && last_attempt > 24h ( 24 * 3600 > ( time() - $check->last_attempt )
330 - failed_attempts > 0 && last_attempt > 2h
331 - version_checked != YOURLS_VERSION
333 In the future, maybe check with cronjob emulation instead of limiting to when viewing an admin page