BackupExcluder.php
2 weeks ago
CacheIntegrator.php
2 weeks ago
CloudflareConfigurator.php
2 weeks ago
DeactivationModalLoader.php
2 weeks ago
EnvDetector.php
2 weeks ago
FileLoader.php
2 weeks ago
MediaStatusViewer.php
2 weeks ago
OptionsAccessManager.php
2 weeks ago
PathsGenerator.php
2 weeks ago
RestApiUnlocker.php
2 weeks ago
ServerConfigurator.php
2 weeks ago
SiteHealthDetector.php
2 weeks ago
StatsManager.php
2 weeks ago
TokenValidator.php
2 weeks ago
ViewLoader.php
2 weeks ago
WpCliManager.php
2 weeks ago
TokenValidator.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WebpConverter\Service; |
| 4 | |
| 5 | use WebpConverter\Model\Token; |
| 6 | use WebpConverter\Repository\TokenRepository; |
| 7 | use WebpConverter\WebpConverterConstants; |
| 8 | |
| 9 | /** |
| 10 | * Checks the token status for the PRO version. |
| 11 | */ |
| 12 | class TokenValidator { |
| 13 | |
| 14 | const API_TOKEN_SUCCESS_CODE = 200; |
| 15 | const REQUEST_INFO_OPTION = 'webpc_token_request_info'; |
| 16 | |
| 17 | private TokenRepository $token_repository; |
| 18 | |
| 19 | private Token $token; |
| 20 | |
| 21 | public function __construct( ?TokenRepository $token_repository = null ) { |
| 22 | $this->token_repository = $token_repository ?: new TokenRepository(); |
| 23 | } |
| 24 | |
| 25 | public function validate_token( ?string $token_value = null ): Token { |
| 26 | $this->token = $this->token_repository->get_token( $token_value ); |
| 27 | $status = ( $token_value && $this->check_access_token( $token_value ) ); |
| 28 | |
| 29 | if ( $status ) { |
| 30 | $this->token_repository->save_token( |
| 31 | $this->token |
| 32 | ->set_token_value( $token_value ) |
| 33 | ->set_valid_status( true ) |
| 34 | ); |
| 35 | } else { |
| 36 | $this->token_repository->remove_token(); |
| 37 | } |
| 38 | |
| 39 | return $this->token_repository->get_token( $token_value ); |
| 40 | } |
| 41 | |
| 42 | private function check_access_token( string $token_value ): bool { |
| 43 | $connect = curl_init( sprintf( WebpConverterConstants::API_TOKEN_VALIDATION_URL, $token_value ) ); |
| 44 | if ( ! $connect ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | curl_setopt( $connect, CURLOPT_SSL_VERIFYPEER, false ); |
| 49 | curl_setopt( $connect, CURLOPT_SSL_VERIFYHOST, 0 ); |
| 50 | curl_setopt( $connect, CURLOPT_RETURNTRANSFER, true ); |
| 51 | curl_setopt( $connect, CURLOPT_TIMEOUT, 10 ); |
| 52 | curl_setopt( $connect, CURLOPT_POST, true ); |
| 53 | curl_setopt( |
| 54 | $connect, |
| 55 | CURLOPT_POSTFIELDS, |
| 56 | [ |
| 57 | 'domain_host' => parse_url( get_site_url(), PHP_URL_HOST ), |
| 58 | ] |
| 59 | ); |
| 60 | |
| 61 | $response = curl_exec( $connect ); |
| 62 | $request_info = curl_getinfo( $connect ); |
| 63 | |
| 64 | if ( $request_info['http_code'] !== self::API_TOKEN_SUCCESS_CODE ) { |
| 65 | OptionsAccessManager::update_option( self::REQUEST_INFO_OPTION, $request_info ); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $response_json = ( $response && is_string( $response ) ) ? json_decode( $response, true ) : null; |
| 70 | if ( ! $response_json ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | $this->token->set_images_usage( $response_json[ WebpConverterConstants::API_RESPONSE_VALUE_LIMIT_USAGE ] ); |
| 75 | $this->token->set_images_limit( $response_json[ WebpConverterConstants::API_RESPONSE_VALUE_LIMIT_MAX ] ); |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 |