PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.7
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Auth / Basic.php

Basic.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.7, at classes/Auth/Basic.php

104 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Auth;
3
4 use Imagify\Traits\InstanceGetterTrait;
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 final class Basic {
14 use 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 $user = '';
52 $pass = '';
53
54 if ( defined( 'IMAGIFY_AUTH_USER' ) && defined( 'IMAGIFY_AUTH_PASSWORD' ) && IMAGIFY_AUTH_USER && IMAGIFY_AUTH_PASSWORD ) {
55 $user = IMAGIFY_AUTH_USER;
56 $pass = IMAGIFY_AUTH_PASSWORD;
57 } else {
58 $auth_type = ! empty( $_SERVER['AUTH_TYPE'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['AUTH_TYPE'] ) ) ) : '';
59
60 if ( 'basic' === $auth_type && ! empty( $_SERVER['PHP_AUTH_USER'] ) && ! empty( $_SERVER['PHP_AUTH_PW'] ) ) {
61 $user = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) );
62 $pass = sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
63 }
64 }
65
66 if ( empty( $user ) ) {
67 // No credentials.
68 return $url;
69 }
70
71 return preg_replace( '%^(.+?//)(.+?)$%', '$1' . rawurlencode( $user ) . ':' . rawurlencode( $pass ) . '@$2', $url );
72 }
73
74 /**
75 * If the site uses basic authentication, add the required user and password to the given URL.
76 *
77 * @since 1.9.5
78 * @access public
79 * @author Grégory Viguier
80 *
81 * @param array $args {
82 * An array of cron request URL arguments.
83 *
84 * @type string $url The cron request URL.
85 * @type int $key The 22 digit GMT microtime.
86 * @type array $args {
87 * An array of cron request arguments.
88 *
89 * @type int $timeout The request timeout in seconds. Default .01 seconds.
90 * @type bool $blocking Whether to set blocking for the request. Default false.
91 * @type bool $sslverify Whether SSL should be verified for the request. Default false.
92 * }
93 * }
94 * @return array
95 */
96 public function cron_request_args( $args ) {
97 if ( ! empty( $args['url'] ) ) {
98 $args['url'] = $this->get_auth_url( $args['url'] );
99 }
100
101 return $args;
102 }
103 }
104