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
webp-converter-for-media / src / Service / TokenValidator.php
webp-converter-for-media / src / Service Last commit date
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