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/wp-conte.../plugins/embedpre.../EmbedPre.../ThirdPar.../Googleca...
File: GoogleClient.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* Exception with a description field.
[3] Fix | Delete
**/
[4] Fix | Delete
class EmbedPress_GoogleClient_RequestException extends Exception {
[5] Fix | Delete
[6] Fix | Delete
private $description;
[7] Fix | Delete
[8] Fix | Delete
function __construct($message, $code = 0, $description = '') {
[9] Fix | Delete
parent::__construct($message, $code );
[10] Fix | Delete
$this->description = $description;
[11] Fix | Delete
[12] Fix | Delete
}
[13] Fix | Delete
public function getDescription() {
[14] Fix | Delete
return $this->description;
[15] Fix | Delete
}
[16] Fix | Delete
[17] Fix | Delete
}
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Class that can do an HTTP request.
[21] Fix | Delete
**/
[22] Fix | Delete
class EmbedPress_GoogleClient_Request {
[23] Fix | Delete
[24] Fix | Delete
// See for example: https://developers.google.com/drive/v3/web/handle-errors
[25] Fix | Delete
private static $HTTP_CODES = [
[26] Fix | Delete
400 => 'Bad request. User error.',
[27] Fix | Delete
401 => 'Invalid Credentials. Invalid authorization header. The access token you\'re using is either expired or invalid.',
[28] Fix | Delete
403 => 'Rate Limit Exceeded.',
[29] Fix | Delete
404 => 'Not found. The specified resource was not found.',
[30] Fix | Delete
500 => 'Backend error.'
[31] Fix | Delete
];
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Does a HTTP request
[35] Fix | Delete
*
[36] Fix | Delete
* @param string $url URL to request
[37] Fix | Delete
* @param array $params Parameters to add to the URL or request body
[38] Fix | Delete
* @param string $method HTTP method to use
[39] Fix | Delete
* @param array $headers Headers for this request
[40] Fix | Delete
*
[41] Fix | Delete
* @return array JSON result decoded as an array
[42] Fix | Delete
*
[43] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[44] Fix | Delete
**/
[45] Fix | Delete
public static function doRequest($url, $params = [], $method = 'GET', $headers = []) {
[46] Fix | Delete
[47] Fix | Delete
$args = [];
[48] Fix | Delete
if (!empty($headers)) {
[49] Fix | Delete
$args['headers'] = $headers;
[50] Fix | Delete
}
[51] Fix | Delete
[52] Fix | Delete
$args['timeout'] = 10; // default is 5 seconds.
[53] Fix | Delete
[54] Fix | Delete
switch ($method) {
[55] Fix | Delete
case 'GET':
[56] Fix | Delete
if (!empty($params)) {
[57] Fix | Delete
$url .= '?' . http_build_query($params); // urlencoded is done for you
[58] Fix | Delete
//$url = add_query_arg($params, $url); // wp variant, but I think you still have to urlencoded it
[59] Fix | Delete
}
[60] Fix | Delete
$result = wp_remote_get($url, $args);
[61] Fix | Delete
break;
[62] Fix | Delete
case 'POST':
[63] Fix | Delete
if (!empty($params)) {
[64] Fix | Delete
$args['body'] = $params; // TODO: Do we have to urlencoded these manually?
[65] Fix | Delete
}
[66] Fix | Delete
$result = wp_remote_post($url, $args);
[67] Fix | Delete
break;
[68] Fix | Delete
default:
[69] Fix | Delete
throw new EmbedPress_GoogleClient_RequestException('Unknown request method.');
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
if (empty($result)) {
[73] Fix | Delete
throw new EmbedPress_GoogleClient_RequestException('Request failed.');
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
if (is_wp_error($result)) {
[77] Fix | Delete
throw new EmbedPress_GoogleClient_RequestException($result->get_error_message());
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
$decodedResult = json_decode(wp_remote_retrieve_body($result), true);
[81] Fix | Delete
if (is_null($decodedResult)) {
[82] Fix | Delete
throw new EmbedPress_GoogleClient_RequestException('Response is invalid JSON.', 0, $result);
[83] Fix | Delete
}
[84] Fix | Delete
if (!empty($decodedResult['error'])) {
[85] Fix | Delete
$exCode = 0;
[86] Fix | Delete
$exMessage = 'Something went wrong.';
[87] Fix | Delete
$exDescription = '';
[88] Fix | Delete
if (is_array($decodedResult['error'])) {
[89] Fix | Delete
if (!empty($decodedResult['error']['message'])) {
[90] Fix | Delete
$exMessage = $decodedResult['error']['message'];
[91] Fix | Delete
}
[92] Fix | Delete
if (!empty($decodedResult['error']['code']) && preg_match("/^\d+$/", $decodedResult['error']['code'])) {
[93] Fix | Delete
$exCode = $decodedResult['error']['code'];
[94] Fix | Delete
}
[95] Fix | Delete
} else {
[96] Fix | Delete
if (!empty($decodedResult['error_description'])) {
[97] Fix | Delete
$exMessage = $decodedResult['error_description'];
[98] Fix | Delete
}
[99] Fix | Delete
if (!empty($decodedResult['code']) && preg_match("/^\d+$/", $decodedResult['code'])) {
[100] Fix | Delete
$exCode = $decodedResult['code'];
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
if (!empty(self::$HTTP_CODES[$exCode])) {
[104] Fix | Delete
$exDescription = self::$HTTP_CODES[$exCode];
[105] Fix | Delete
}
[106] Fix | Delete
throw new EmbedPress_GoogleClient_RequestException($exMessage, $exCode, $exDescription);
[107] Fix | Delete
}
[108] Fix | Delete
return $decodedResult;
[109] Fix | Delete
}
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* The Google OAuth client class.
[114] Fix | Delete
**/
[115] Fix | Delete
class EmbedPress_GoogleClient {
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* Array of client_secret that can be downloaded from the Google api console at:
[119] Fix | Delete
* https://console.developers.google.com
[120] Fix | Delete
**/
[121] Fix | Delete
private $clientInfo;
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* @var array The access token info as an array that is returned by the Google API.
[125] Fix | Delete
**/
[126] Fix | Delete
private $accessTokenInfo;
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* @var string The refreshed
[130] Fix | Delete
**/
[131] Fix | Delete
private $refreshToken;
[132] Fix | Delete
[133] Fix | Delete
private $scope;
[134] Fix | Delete
private $redirectUri;
[135] Fix | Delete
[136] Fix | Delete
const GOOGLE_AUTH_URI = 'https://accounts.google.com/o/oauth2/v2/auth';
[137] Fix | Delete
const GOOGLE_REFRESH_URI = 'https://www.googleapis.com/oauth2/v4/token';
[138] Fix | Delete
const GOOGLE_CODE_URI = 'https://www.googleapis.com/oauth2/v4/token';
[139] Fix | Delete
const GOOGLE_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke';
[140] Fix | Delete
[141] Fix | Delete
// Gets called whenever we receive a new access and optionally refresh token.
[142] Fix | Delete
// So mostly after authorize phase (with refresh token) or after refresh token (only access token).
[143] Fix | Delete
private $tokenCallback;
[144] Fix | Delete
/**
[145] Fix | Delete
* @var null|mixed
[146] Fix | Delete
*/
[147] Fix | Delete
private $getAccessTokenInfo;
[148] Fix | Delete
[149] Fix | Delete
function __construct($clientInfo, $accessTokenInfo = null, $refreshToken = null, $tokenCallback = null) {
[150] Fix | Delete
$this->clientInfo = $clientInfo;
[151] Fix | Delete
$this->getAccessTokenInfo = $accessTokenInfo;
[152] Fix | Delete
$this->refreshToken = $refreshToken;
[153] Fix | Delete
$this->tokenCallback = $tokenCallback;
[154] Fix | Delete
$this->scope = null;
[155] Fix | Delete
$this->redirectUri = null;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
public function setAccessTokenInfo($accessTokenInfo) {
[159] Fix | Delete
$this->accessTokenInfo = $accessTokenInfo;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
public function getAccessTokenInfo() {
[163] Fix | Delete
return $this->accessTokenInfo;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
// Helper
[167] Fix | Delete
public function getAccessToken() {
[168] Fix | Delete
return $this->accessTokenInfo['access_token'];
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
public function setRefreshToken($refreshToken) {
[172] Fix | Delete
$this->refreshToken = $refreshToken;
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
public function getRefreshToken() {
[176] Fix | Delete
return $this->refreshToken;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
public function setTokenCallback($tokenCallback) {
[180] Fix | Delete
$this->tokenCallback = $tokenCallback;
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
public function setScope($scope) {
[184] Fix | Delete
$this->scope = $scope;
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* @throws Exception
[189] Fix | Delete
*/
[190] Fix | Delete
public function setRedirectUri($redirectUri) {
[191] Fix | Delete
if (!in_array($redirectUri, $this->clientInfo['web']['redirect_uris'])) {
[192] Fix | Delete
throw new Exception('Redirect Uri does not exist in client info. Add it first to the project and download the JSON again.');
[193] Fix | Delete
}
[194] Fix | Delete
$this->redirectUri = $redirectUri;
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
public function isAccessTokenExpired() {
[198] Fix | Delete
// Add 30 seconds, so we refresh them on time.
[199] Fix | Delete
return $this->accessTokenInfo['expire_time'] + 30 < time();
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
private function updateTokens($response) {
[203] Fix | Delete
// Compute when access token expires and add it to the info array.
[204] Fix | Delete
$response['expire_time'] = time() + $response['expires_in'];
[205] Fix | Delete
// Only after authorize we get a refresh token, so make sure to save it!
[206] Fix | Delete
// If you loose it, you have to revoke permission and authorize again.
[207] Fix | Delete
$this->setAccessTokenInfo($response);
[208] Fix | Delete
$refreshToken = !empty($response['refresh_token']) ? $response['refresh_token'] : null;
[209] Fix | Delete
if (!empty($refreshToken)) {
[210] Fix | Delete
$this->setRefreshToken($refreshToken);
[211] Fix | Delete
}
[212] Fix | Delete
call_user_func($this->tokenCallback, $response, $refreshToken);
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[217] Fix | Delete
* @throws Exception
[218] Fix | Delete
*/
[219] Fix | Delete
public function handleCodeRedirect($state = '') {
[220] Fix | Delete
if (!empty($_GET['error'])) {
[221] Fix | Delete
throw new Exception($_GET['error']);
[222] Fix | Delete
}
[223] Fix | Delete
if (empty($_GET['code'])) {
[224] Fix | Delete
throw new Exception('Code missing');
[225] Fix | Delete
}
[226] Fix | Delete
$getState = !empty($_GET['state']) ? $_GET['state'] : '';
[227] Fix | Delete
if ($getState !== $state) {
[228] Fix | Delete
throw new Exception("State mismatch.");
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
$result = EmbedPress_GoogleClient_Request::doRequest(self::GOOGLE_CODE_URI, [
[232] Fix | Delete
'code' => $_GET['code'],
[233] Fix | Delete
'client_id' => $this->clientInfo['web']['client_id'],
[234] Fix | Delete
'client_secret' => $this->clientInfo['web']['client_secret'],
[235] Fix | Delete
'redirect_uri' => $this->redirectUri,
[236] Fix | Delete
'grant_type' => 'authorization_code'
[237] Fix | Delete
], 'POST', [
[238] Fix | Delete
'Content-Type' => 'application/x-www-form-urlencoded'
[239] Fix | Delete
]);
[240] Fix | Delete
[241] Fix | Delete
$this->updateTokens($result);
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
public function authorize($state = '') {
[245] Fix | Delete
$params = [
[246] Fix | Delete
'client_id' => $this->clientInfo['web']['client_id'],
[247] Fix | Delete
'scope' => $this->scope,
[248] Fix | Delete
'access_type' => 'offline',
[249] Fix | Delete
'include_granted_scopes' => 'true',
[250] Fix | Delete
'state' => $state,
[251] Fix | Delete
'redirect_uri' => $this->redirectUri,
[252] Fix | Delete
'response_type' => 'code'
[253] Fix | Delete
];
[254] Fix | Delete
$url = self::GOOGLE_AUTH_URI . '?' . http_build_query($params);
[255] Fix | Delete
header('Location: ' . $url);
[256] Fix | Delete
exit;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
/**
[260] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[261] Fix | Delete
*/
[262] Fix | Delete
public function refreshAccessToken() {
[263] Fix | Delete
$result = EmbedPress_GoogleClient_Request::doRequest(
[264] Fix | Delete
self::GOOGLE_REFRESH_URI,
[265] Fix | Delete
[
[266] Fix | Delete
'client_id' => $this->clientInfo['web']['client_id'],
[267] Fix | Delete
'client_secret' => $this->clientInfo['web']['client_secret'],
[268] Fix | Delete
'refresh_token' => $this->refreshToken,
[269] Fix | Delete
'grant_type' => 'refresh_token'
[270] Fix | Delete
],
[271] Fix | Delete
'POST',
[272] Fix | Delete
[
[273] Fix | Delete
'Content-Type' => 'application/x-www-form-urlencoded'
[274] Fix | Delete
]
[275] Fix | Delete
);
[276] Fix | Delete
$this->updateTokens($result);
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[281] Fix | Delete
* @throws Exception
[282] Fix | Delete
*/
[283] Fix | Delete
public function revoke() {
[284] Fix | Delete
// Can be done with access and refresh token,
[285] Fix | Delete
// but as access tokens expire more frequent, first take refresh token.
[286] Fix | Delete
// It can take some time before the revoke is processed.
[287] Fix | Delete
$token = $this->getRefreshToken();
[288] Fix | Delete
if (empty($token)) {
[289] Fix | Delete
$token = $this->getAccessToken();
[290] Fix | Delete
}
[291] Fix | Delete
if (empty($token)) {
[292] Fix | Delete
throw new Exception('No access and refresh token.');
[293] Fix | Delete
}
[294] Fix | Delete
EmbedPress_GoogleClient_Request::doRequest(
[295] Fix | Delete
self::GOOGLE_REVOKE_URI, ['token' => $token]);
[296] Fix | Delete
// TODO: do we have 200 status code???
[297] Fix | Delete
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
// https://developers.google.com/google-apps/calendar/v3/reference/events/list
[303] Fix | Delete
/**
[304] Fix | Delete
* Class that can communicate with the Google Calendar API.
[305] Fix | Delete
**/
[306] Fix | Delete
class EmbedPress_GoogleCalendarClient {
[307] Fix | Delete
[308] Fix | Delete
const GOOGLE_CALENDAR_EVENTS_URI = 'https://www.googleapis.com/calendar/v3/calendars/$calendarId/events';
[309] Fix | Delete
const GOOGLE_CALENDARLIST_URI = 'https://www.googleapis.com/calendar/v3/users/me/calendarList';
[310] Fix | Delete
const GOOGLE_COLORLIST_URI = 'https://www.googleapis.com/calendar/v3/colors';
[311] Fix | Delete
[312] Fix | Delete
private $googleClient;
[313] Fix | Delete
[314] Fix | Delete
function __construct($client) {
[315] Fix | Delete
$this->googleClient = $client;
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[320] Fix | Delete
*/
[321] Fix | Delete
public function getEvents($calendarId, $params) {
[322] Fix | Delete
$url = str_replace('$calendarId', urlencode($calendarId), self::GOOGLE_CALENDAR_EVENTS_URI);
[323] Fix | Delete
// https://developers.google.com/google-apps/calendar/performance#partial-response
[324] Fix | Delete
$params['fields'] = "items(summary,description,start,end,htmlLink,creator,location,attendees,attachments,colorId)";
[325] Fix | Delete
$result = EmbedPress_GoogleClient_Request::doRequest(
[326] Fix | Delete
$url,
[327] Fix | Delete
$params,
[328] Fix | Delete
'GET',
[329] Fix | Delete
[
[330] Fix | Delete
'Authorization' => 'Bearer ' . $this->googleClient->getAccessToken(),
[331] Fix | Delete
]
[332] Fix | Delete
);
[333] Fix | Delete
[334] Fix | Delete
return !empty($result['items']) ? $result['items'] : [];
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
/**
[338] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[339] Fix | Delete
*/
[340] Fix | Delete
public function getEventsPublic($calendarId, $params, $apiKey, $referer) {
[341] Fix | Delete
$url = str_replace('$calendarId', urlencode($calendarId), self::GOOGLE_CALENDAR_EVENTS_URI);
[342] Fix | Delete
// https://developers.google.com/google-apps/calendar/performance#partial-response
[343] Fix | Delete
$params['fields'] = "items(summary,description,start,end,htmlLink,creator,location,attendees,attachments,colorId)";
[344] Fix | Delete
$params['key'] = $apiKey;
[345] Fix | Delete
$result = EmbedPress_GoogleClient_Request::doRequest(
[346] Fix | Delete
$url,
[347] Fix | Delete
$params,
[348] Fix | Delete
'GET',
[349] Fix | Delete
[
[350] Fix | Delete
'Referer' => $referer
[351] Fix | Delete
]
[352] Fix | Delete
);
[353] Fix | Delete
[354] Fix | Delete
return !empty($result['items']) ? $result['items'] : [];
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
/**
[358] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[359] Fix | Delete
*/
[360] Fix | Delete
public function getPrimaryEvents($params) {
[361] Fix | Delete
return $this->getEvents('primary', $params);
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
/**
[365] Fix | Delete
* @return string JSON with items as key each item is calendarListEntry
[366] Fix | Delete
* calendarListEntry:
[367] Fix | Delete
* 'id' => string 'ehqelgh6hq4juqhjd79g4b5qkk@group.calendar.google.com' ==> use this for event list
[368] Fix | Delete
* 'summary' => string 'Vacationers'
[369] Fix | Delete
* 'description' => string 'Agenda voor de vakantierooster'
[370] Fix | Delete
* 'backgroundColor' => string '#cd74e6'
[371] Fix | Delete
* 'foregroundColor' => string '#000000'
[372] Fix | Delete
* 'selected' => boolean true ==> alleen aanwezig als geselecteerd! Geeft aan of de gebruiker deze calendat in de Google ui aan heeft gezet
[373] Fix | Delete
* 'primary' => boolean true ==> alleen aanwezig bij primary calendar!
[374] Fix | Delete
* 'accessRole' => we can get events only for 'owner' items, so we only query these.
[375] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[376] Fix | Delete
*/
[377] Fix | Delete
public function getCalendarList() {
[378] Fix | Delete
$result = EmbedPress_GoogleClient_Request::doRequest(
[379] Fix | Delete
self::GOOGLE_CALENDARLIST_URI,
[380] Fix | Delete
[
[381] Fix | Delete
'minAccessRole' => 'reader' // if 'owner', then you don't see calendars like national holidays.
[382] Fix | Delete
],
[383] Fix | Delete
'GET',
[384] Fix | Delete
[
[385] Fix | Delete
'Authorization' => 'Bearer ' . $this->googleClient->getAccessToken()
[386] Fix | Delete
]
[387] Fix | Delete
);
[388] Fix | Delete
[389] Fix | Delete
return !empty($result['items']) ? $result['items'] : [];
[390] Fix | Delete
}
[391] Fix | Delete
[392] Fix | Delete
/**
[393] Fix | Delete
* @throws EmbedPress_GoogleClient_RequestException
[394] Fix | Delete
*/
[395] Fix | Delete
public function getColorList() {
[396] Fix | Delete
$result = EmbedPress_GoogleClient_Request::doRequest(
[397] Fix | Delete
self::GOOGLE_COLORLIST_URI,
[398] Fix | Delete
null,
[399] Fix | Delete
'GET',
[400] Fix | Delete
[
[401] Fix | Delete
'Authorization' => 'Bearer ' . $this->googleClient->getAccessToken()
[402] Fix | Delete
]
[403] Fix | Delete
);
[404] Fix | Delete
$calendar = !empty($result['calendar']) ? $result['calendar'] : [];
[405] Fix | Delete
$event = !empty($result['event']) ? $result['event'] : [];
[406] Fix | Delete
return [
[407] Fix | Delete
'calendar' => $calendar,
[408] Fix | Delete
'event' => $event
[409] Fix | Delete
];
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
}
[413] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function