| 1 |
<?php |
| 2 |
namespace Imagify\Auth; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Class that allows the use of Basic Auth for internal requests. |
| 8 |
* If this doesn’t work automatically, define the constants IMAGIFY_AUTH_USER and IMAGIFY_AUTH_PASSWORD. |
| 9 |
* |
| 10 |
* @since 1.9.5 |
| 11 |
* @author Grégory Viguier |
| 12 |
*/ |
| 13 |
class Basic { |
| 14 |
use \Imagify\Traits\InstanceGetterTrait; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class init: launch hooks. |
| 18 |
* |
| 19 |
* @since 1.9.5 |
| 20 |
* @access public |
| 21 |
* @author Grégory Viguier |
| 22 |
*/ |
| 23 |
public function init() { |
| 24 |
add_filter( 'imagify_background_process_url', [ $this, 'get_auth_url' ] ); |
| 25 |
add_filter( 'imagify_async_job_url', [ $this, 'get_auth_url' ] ); |
| 26 |
add_filter( 'imagify_internal_request_url', [ $this, 'get_auth_url' ] ); |
| 27 |
add_filter( 'cron_request', [ $this, 'cron_request_args' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* If the site uses basic authentication, add the required user and password to the given URL. |
| 32 |
* |
| 33 |
* @since 1.9.5 |
| 34 |
* @access public |
| 35 |
* @author Grégory Viguier |
| 36 |
* |
| 37 |
* @param string $url An URL. |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public function get_auth_url( $url ) { |
| 41 |
if ( ! $url || ! is_string( $url ) ) { |
| 42 |
// Invalid. |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
if ( preg_match( '%.+?//(.+?):(.+?)@%', $url ) ) { |
| 47 |
// Credentials already in the URL. |
| 48 |
return $url; |
| 49 |
} |
| 50 |
|
| 51 |
if ( defined( 'IMAGIFY_AUTH_USER' ) && defined( 'IMAGIFY_AUTH_PASSWORD' ) && IMAGIFY_AUTH_USER && IMAGIFY_AUTH_PASSWORD ) { |
| 52 |
$user = IMAGIFY_AUTH_USER; |
| 53 |
$pass = IMAGIFY_AUTH_PASSWORD; |
| 54 |
} else { |
| 55 |
$auth_type = ! empty( $_SERVER['AUTH_TYPE'] ) ? strtolower( wp_unslash( $_SERVER['AUTH_TYPE'] ) ) : ''; |
| 56 |
|
| 57 |
if ( 'basic' === $auth_type && ! empty( $_SERVER['PHP_AUTH_USER'] ) && ! empty( $_SERVER['PHP_AUTH_PW'] ) ) { |
| 58 |
$user = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) ); |
| 59 |
$pass = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( empty( $user ) || empty( $pass ) ) { |
| 64 |
// No credentials. |
| 65 |
return $url; |
| 66 |
} |
| 67 |
|
| 68 |
return preg_replace( '%^(.+?//)(.+?)$%', '$1' . rawurlencode( $user ) . ':' . rawurlencode( $pass ) . '@$2', $url ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* If the site uses basic authentication, add the required user and password to the given URL. |
| 73 |
* |
| 74 |
* @since 1.9.5 |
| 75 |
* @access public |
| 76 |
* @author Grégory Viguier |
| 77 |
* |
| 78 |
* @param array $args { |
| 79 |
* An array of cron request URL arguments. |
| 80 |
* |
| 81 |
* @type string $url The cron request URL. |
| 82 |
* @type int $key The 22 digit GMT microtime. |
| 83 |
* @type array $args { |
| 84 |
* An array of cron request arguments. |
| 85 |
* |
| 86 |
* @type int $timeout The request timeout in seconds. Default .01 seconds. |
| 87 |
* @type bool $blocking Whether to set blocking for the request. Default false. |
| 88 |
* @type bool $sslverify Whether SSL should be verified for the request. Default false. |
| 89 |
* } |
| 90 |
* } |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function cron_request_args( $args ) { |
| 94 |
if ( ! empty( $args['url'] ) ) { |
| 95 |
$args['url'] = $this->get_auth_url( $args['url'] ); |
| 96 |
} |
| 97 |
|
| 98 |
return $args; |
| 99 |
} |
| 100 |
} |
| 101 |
|