Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wordpres.../src/config
File: oauth-client.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Config;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception;
[5] Fix | Delete
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception;
[6] Fix | Delete
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception;
[7] Fix | Delete
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Failed_Storage_Exception;
[8] Fix | Delete
use Yoast\WP\SEO\Helpers\Options_Helper;
[9] Fix | Delete
use Yoast\WP\SEO\Values\OAuth\OAuth_Token;
[10] Fix | Delete
use YoastSEO_Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException;
[11] Fix | Delete
use YoastSEO_Vendor\League\OAuth2\Client\Provider\GenericProvider;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Class OAuth_Client
[15] Fix | Delete
*/
[16] Fix | Delete
abstract class OAuth_Client {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* The option's key.
[20] Fix | Delete
*
[21] Fix | Delete
* @var string
[22] Fix | Delete
*/
[23] Fix | Delete
protected $token_option = null;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The provider.
[27] Fix | Delete
*
[28] Fix | Delete
* @var Wincher_PKCE_Provider|GenericProvider
[29] Fix | Delete
*/
[30] Fix | Delete
protected $provider;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* The options helper.
[34] Fix | Delete
*
[35] Fix | Delete
* @var Options_Helper
[36] Fix | Delete
*/
[37] Fix | Delete
protected $options_helper;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* The token.
[41] Fix | Delete
*
[42] Fix | Delete
* @var OAuth_Token|null
[43] Fix | Delete
*/
[44] Fix | Delete
protected $token = null;
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* OAuth_Client constructor.
[48] Fix | Delete
*
[49] Fix | Delete
* @param string $token_option The option's name to save the token as.
[50] Fix | Delete
* @param Wincher_PKCE_Provider|GenericProvider $provider The provider.
[51] Fix | Delete
* @param Options_Helper $options_helper The Options_Helper instance.
[52] Fix | Delete
*
[53] Fix | Delete
* @throws Empty_Property_Exception Exception thrown if a token property is empty.
[54] Fix | Delete
*/
[55] Fix | Delete
public function __construct(
[56] Fix | Delete
$token_option,
[57] Fix | Delete
$provider,
[58] Fix | Delete
Options_Helper $options_helper
[59] Fix | Delete
) {
[60] Fix | Delete
$this->provider = $provider;
[61] Fix | Delete
$this->token_option = $token_option;
[62] Fix | Delete
$this->options_helper = $options_helper;
[63] Fix | Delete
[64] Fix | Delete
$tokens = $this->options_helper->get( $this->token_option );
[65] Fix | Delete
[66] Fix | Delete
if ( ! empty( $tokens ) ) {
[67] Fix | Delete
$this->token = new OAuth_Token(
[68] Fix | Delete
$tokens['access_token'],
[69] Fix | Delete
$tokens['refresh_token'],
[70] Fix | Delete
$tokens['expires'],
[71] Fix | Delete
$tokens['has_expired'],
[72] Fix | Delete
$tokens['created_at'],
[73] Fix | Delete
( $tokens['error_count'] ?? 0 )
[74] Fix | Delete
);
[75] Fix | Delete
}
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Requests the access token and refresh token based on the passed code.
[80] Fix | Delete
*
[81] Fix | Delete
* @param string $code The code to send.
[82] Fix | Delete
*
[83] Fix | Delete
* @return OAuth_Token The requested tokens.
[84] Fix | Delete
*
[85] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[86] Fix | Delete
*/
[87] Fix | Delete
public function request_tokens( $code ) {
[88] Fix | Delete
try {
[89] Fix | Delete
$response = $this->provider
[90] Fix | Delete
->getAccessToken(
[91] Fix | Delete
'authorization_code',
[92] Fix | Delete
[
[93] Fix | Delete
'code' => $code,
[94] Fix | Delete
]
[95] Fix | Delete
);
[96] Fix | Delete
[97] Fix | Delete
$token = OAuth_Token::from_response( $response );
[98] Fix | Delete
[99] Fix | Delete
return $this->store_token( $token );
[100] Fix | Delete
} catch ( Exception $exception ) {
[101] Fix | Delete
throw new Authentication_Failed_Exception( $exception );
[102] Fix | Delete
}
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Performs an authenticated GET request to the desired URL.
[107] Fix | Delete
*
[108] Fix | Delete
* @param string $url The URL to send the request to.
[109] Fix | Delete
* @param array $options The options to pass along to the request.
[110] Fix | Delete
*
[111] Fix | Delete
* @return mixed The parsed API response.
[112] Fix | Delete
*
[113] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[114] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[115] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[116] Fix | Delete
*/
[117] Fix | Delete
public function get( $url, $options = [] ) {
[118] Fix | Delete
return $this->do_request( 'GET', $url, $options );
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* Performs an authenticated POST request to the desired URL.
[123] Fix | Delete
*
[124] Fix | Delete
* @param string $url The URL to send the request to.
[125] Fix | Delete
* @param mixed $body The data to send along in the request's body.
[126] Fix | Delete
* @param array $options The options to pass along to the request.
[127] Fix | Delete
*
[128] Fix | Delete
* @return mixed The parsed API response.
[129] Fix | Delete
*
[130] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[131] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[132] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[133] Fix | Delete
*/
[134] Fix | Delete
public function post( $url, $body, $options = [] ) {
[135] Fix | Delete
$options['body'] = $body;
[136] Fix | Delete
[137] Fix | Delete
return $this->do_request( 'POST', $url, $options );
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
/**
[141] Fix | Delete
* Performs an authenticated DELETE request to the desired URL.
[142] Fix | Delete
*
[143] Fix | Delete
* @param string $url The URL to send the request to.
[144] Fix | Delete
* @param array $options The options to pass along to the request.
[145] Fix | Delete
*
[146] Fix | Delete
* @return mixed The parsed API response.
[147] Fix | Delete
*
[148] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[149] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[150] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[151] Fix | Delete
*/
[152] Fix | Delete
public function delete( $url, $options = [] ) {
[153] Fix | Delete
return $this->do_request( 'DELETE', $url, $options );
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
/**
[157] Fix | Delete
* Determines whether there are valid tokens available.
[158] Fix | Delete
*
[159] Fix | Delete
* @return bool Whether there are valid tokens.
[160] Fix | Delete
*/
[161] Fix | Delete
public function has_valid_tokens() {
[162] Fix | Delete
return ! empty( $this->token ) && $this->token->has_expired() === false;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/**
[166] Fix | Delete
* Gets the stored tokens and refreshes them if they've expired.
[167] Fix | Delete
*
[168] Fix | Delete
* @return OAuth_Token The stored tokens.
[169] Fix | Delete
*
[170] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[171] Fix | Delete
*/
[172] Fix | Delete
public function get_tokens() {
[173] Fix | Delete
if ( empty( $this->token ) ) {
[174] Fix | Delete
throw new Empty_Token_Exception();
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
if ( $this->token->has_expired() ) {
[178] Fix | Delete
$this->token = $this->refresh_tokens( $this->token );
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
return $this->token;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* Stores the passed token.
[186] Fix | Delete
*
[187] Fix | Delete
* @param OAuth_Token $token The token to store.
[188] Fix | Delete
*
[189] Fix | Delete
* @return OAuth_Token The stored token.
[190] Fix | Delete
*
[191] Fix | Delete
* @throws Failed_Storage_Exception Exception thrown if storing of the token fails.
[192] Fix | Delete
*/
[193] Fix | Delete
public function store_token( OAuth_Token $token ) {
[194] Fix | Delete
$saved = $this->options_helper->set( $this->token_option, $token->to_array() );
[195] Fix | Delete
[196] Fix | Delete
if ( $saved === false ) {
[197] Fix | Delete
throw new Failed_Storage_Exception();
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $token;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Clears the stored token from storage.
[205] Fix | Delete
*
[206] Fix | Delete
* @return bool The stored token.
[207] Fix | Delete
*
[208] Fix | Delete
* @throws Failed_Storage_Exception Exception thrown if clearing of the token fails.
[209] Fix | Delete
*/
[210] Fix | Delete
public function clear_token() {
[211] Fix | Delete
$saved = $this->options_helper->set( $this->token_option, [] );
[212] Fix | Delete
[213] Fix | Delete
if ( $saved === false ) {
[214] Fix | Delete
throw new Failed_Storage_Exception();
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
return true;
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Performs the specified request.
[222] Fix | Delete
*
[223] Fix | Delete
* @param string $method The HTTP method to use.
[224] Fix | Delete
* @param string $url The URL to send the request to.
[225] Fix | Delete
* @param array $options The options to pass along to the request.
[226] Fix | Delete
*
[227] Fix | Delete
* @return mixed The parsed API response.
[228] Fix | Delete
*
[229] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[230] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[231] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[232] Fix | Delete
*/
[233] Fix | Delete
protected function do_request( $method, $url, array $options ) {
[234] Fix | Delete
$defaults = [
[235] Fix | Delete
'headers' => $this->provider->getHeaders( $this->get_tokens()->access_token ),
[236] Fix | Delete
];
[237] Fix | Delete
[238] Fix | Delete
$options = \array_merge_recursive( $defaults, $options );
[239] Fix | Delete
[240] Fix | Delete
if ( \array_key_exists( 'params', $options ) ) {
[241] Fix | Delete
$url .= '?' . \http_build_query( $options['params'] );
[242] Fix | Delete
unset( $options['params'] );
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
$request = $this->provider
[246] Fix | Delete
->getAuthenticatedRequest( $method, $url, null, $options );
[247] Fix | Delete
[248] Fix | Delete
return $this->provider->getParsedResponse( $request );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/**
[252] Fix | Delete
* Refreshes the outdated tokens.
[253] Fix | Delete
*
[254] Fix | Delete
* @param OAuth_Token $tokens The outdated tokens.
[255] Fix | Delete
*
[256] Fix | Delete
* @return OAuth_Token The refreshed tokens.
[257] Fix | Delete
*
[258] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[259] Fix | Delete
*/
[260] Fix | Delete
protected function refresh_tokens( OAuth_Token $tokens ) {
[261] Fix | Delete
// We do this dance with transients since we need to make sure we don't
[262] Fix | Delete
// delete valid tokens because of a race condition when two calls are
[263] Fix | Delete
// made simultaneously to this function and refresh token rotation is
[264] Fix | Delete
// turned on in the OAuth server. This is not 100% safe, but should at
[265] Fix | Delete
// least be much better than not having any lock at all.
[266] Fix | Delete
$lock_name = \sprintf( 'lock:%s', $this->token_option );
[267] Fix | Delete
$can_lock = \get_transient( $lock_name ) === false;
[268] Fix | Delete
$has_lock = $can_lock && \set_transient( $lock_name, true, 30 );
[269] Fix | Delete
[270] Fix | Delete
try {
[271] Fix | Delete
$new_tokens = $this->provider->getAccessToken(
[272] Fix | Delete
'refresh_token',
[273] Fix | Delete
[
[274] Fix | Delete
'refresh_token' => $tokens->refresh_token,
[275] Fix | Delete
]
[276] Fix | Delete
);
[277] Fix | Delete
[278] Fix | Delete
$token_obj = OAuth_Token::from_response( $new_tokens );
[279] Fix | Delete
[280] Fix | Delete
return $this->store_token( $token_obj );
[281] Fix | Delete
} catch ( Exception $exception ) {
[282] Fix | Delete
// If we tried to refresh but the refresh token is invalid, delete
[283] Fix | Delete
// the tokens so that we don't try again. Only do this if we got the
[284] Fix | Delete
// lock at the beginning of the call.
[285] Fix | Delete
if ( $has_lock && $exception->getMessage() === 'invalid_grant' ) {
[286] Fix | Delete
try {
[287] Fix | Delete
// To protect from race conditions, only do this if we've
[288] Fix | Delete
// seen an error before with the same token.
[289] Fix | Delete
if ( $tokens->error_count >= 1 ) {
[290] Fix | Delete
$this->clear_token();
[291] Fix | Delete
}
[292] Fix | Delete
else {
[293] Fix | Delete
$tokens->error_count += 1;
[294] Fix | Delete
$this->store_token( $tokens );
[295] Fix | Delete
}
[296] Fix | Delete
} catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
[297] Fix | Delete
// Pass through.
[298] Fix | Delete
}
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
throw new Authentication_Failed_Exception( $exception );
[302] Fix | Delete
} finally {
[303] Fix | Delete
\delete_transient( $lock_name );
[304] Fix | Delete
}
[305] Fix | Delete
}
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function