| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The class maps settings and options from imageproxy. |
| 5 |
* |
| 6 |
* @package \Optml\Inc |
| 7 |
* @author Optimole <friends@optimole.com> |
| 8 |
*/ |
| 9 |
abstract class Optml_Resource { |
| 10 |
|
| 11 |
use Optml_Validator; |
| 12 |
use Optml_Normalizer; |
| 13 |
|
| 14 |
/** |
| 15 |
* Signature size. |
| 16 |
*/ |
| 17 |
const SIGNATURE_SIZE = 8; |
| 18 |
|
| 19 |
/** |
| 20 |
* Source image url. |
| 21 |
* |
| 22 |
* @var string Source image. |
| 23 |
*/ |
| 24 |
protected $source_url = ''; |
| 25 |
|
| 26 |
/** |
| 27 |
* Cache Buster passed from Optimole Service. |
| 28 |
* |
| 29 |
* @var string Active Cache Buster |
| 30 |
*/ |
| 31 |
protected $cache_buster = ''; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Optml_Resource constructor. |
| 37 |
* |
| 38 |
* @param string $url Source image url. |
| 39 |
* @param string $cache_buster The cache buster. |
| 40 |
* |
| 41 |
* @throws \InvalidArgumentException In case that the url is not provided. |
| 42 |
*/ |
| 43 |
public function __construct( $url = '', $cache_buster = '' ) { |
| 44 |
if ( empty( $url ) ) { |
| 45 |
throw new \InvalidArgumentException( 'Optimole resource builder requires the source url to optimize.' ); // @codeCoverageIgnore |
| 46 |
} |
| 47 |
$this->set_defaults(); |
| 48 |
$this->source_url = apply_filters( 'optml_processed_url', $url ); |
| 49 |
|
| 50 |
$this->cache_buster = $cache_buster; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Set defaults for resource transformations. |
| 55 |
*/ |
| 56 |
abstract protected function set_defaults(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Return transformed url. |
| 60 |
* |
| 61 |
* @param array $params Either will be signed or not. |
| 62 |
* |
| 63 |
* @return string Transformed image url. |
| 64 |
*/ |
| 65 |
abstract public function get_url( $params = [] ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Method to generate tokens and cache them for further requests. |
| 69 |
* |
| 70 |
* @param string $source The source string to tokenize. |
| 71 |
* @param int $size The size of bites for the generated token. |
| 72 |
* |
| 73 |
* @return string |
| 74 |
*/ |
| 75 |
private function get_token_from_cache( $source, $size = 8 ) { |
| 76 |
$key = crc32( $source ); |
| 77 |
$cache_token = wp_cache_get( $key, 'optml_cache_tokens' ); |
| 78 |
if ( $cache_token === false ) { |
| 79 |
$cache_token = $this->get_signature( $source, $size ); |
| 80 |
wp_cache_add( $key, $cache_token, 'optml_cache_tokens', DAY_IN_SECONDS ); |
| 81 |
} |
| 82 |
return $cache_token; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get the token for the domain. |
| 87 |
* |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
public function get_domain_token() { |
| 91 |
$parts = parse_url( $this->source_url ); |
| 92 |
$domain = isset( $parts['host'] ) ? str_replace( 'www.', '', $parts['host'] ) : ''; |
| 93 |
return $this->get_token_from_cache( $domain, 5 ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Return a the cache buster, defaults to the url token used previously. |
| 98 |
* |
| 99 |
* @return string|bool |
| 100 |
*/ |
| 101 |
public function get_cache_buster() { |
| 102 |
if ( $this->cache_buster !== '' ) { |
| 103 |
return $this->cache_buster; |
| 104 |
} |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Return the url signature. |
| 110 |
* |
| 111 |
* @param string $string The path from url. |
| 112 |
* |
| 113 |
* @return bool|string |
| 114 |
*/ |
| 115 |
public function get_signature( $string = '', $size = self::SIGNATURE_SIZE ) { |
| 116 |
|
| 117 |
$binary = hash_hmac( 'sha256', Optml_Config::$secret . $string, Optml_Config::$key, true ); |
| 118 |
$binary = pack( 'A' . $size, $binary ); |
| 119 |
$signature = rtrim( strtr( base64_encode( $binary ), '+/', '-_' ), '=' ); |
| 120 |
|
| 121 |
return $signature; |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|