Zoho Grant Token: ' . htmlspecialchars($_REQUEST['code']); exit; } protected static function initOptions() { static $initialized; if ( true === $initialized ) { return; } self::$client_id = get_option( 'fqp_zoho_client_id' ); self::$client_secret = get_option( 'fqp_zoho_client_secret' ); self::$authorized_redirect_uri = get_option( 'fqp_zoho_authorized_redirect_uri' ); self::$user_email = get_option( 'fqp_zoho_user_email' ); self::$grant_token = get_option( 'fqp_zoho_grant_token' ); $initialized = true; } public static function getTokenPersistencePath() { return __DIR__ . '/zoho_oauthtokens'; } public static function getApplicationLogFilePath() { return __DIR__ . '/zoho_logs'; } public static function initZCRMRestClient() { static $initialized; if ( true === $initialized ) { return true; } self::initOptions(); try { $accounts_url = 'https://accounts.zoho.com'; // (false !== strpos($_SERVER['HTTP_HOST'], '.dev.orange35.com')) ? 'https://accounts.zoho.com' : 'https://accounts.zoho.eu'; $apiBaseUrl = 'www.zohoapis.com'; // (false !== strpos($_SERVER['HTTP_HOST'], '.dev.orange35.com')) ? 'www.zohoapis.com' : 'www.zohoapis.eu' $configuration = [ 'client_id' => self::$client_id, // mandatory 'client_secret' => self::$client_secret, // mandatory 'redirect_uri' => self::$authorized_redirect_uri, // mandatory 'currentUserEmail' => self::$user_email, // mandatory 'accounts_url' => $accounts_url, 'token_persistence_path' => self::getTokenPersistencePath(), 'applicationLogFilePath' => self::getApplicationLogFilePath(), 'sandbox' => false, 'apiBaseUrl' => $apiBaseUrl, // mandatory in case the user is not in the "com" domain 'apiVersion' => 'v2', // mandatory in case the user is not in the "com" domain 'access_type' => 'offline', 'persistence_handler_class' => '\zcrmsdk\oauth\persistence\ZohoOAuthPersistenceHandler', //'db_port' => '3306', //'db_username' => 'root', //'db_password' => '', ]; ZCRMRestClient::initialize( $configuration ); } catch (Exception $e) { self::$errors[] = $e->getMessage(); return false; } $initialized = true; return true; } public static function authorize() { self::initOptions(); self::$errors = []; if ( ! self::$client_id ) { self::$errors[] = fqp__( 'ZOHO Client ID is required' ); } if ( ! self::$client_secret ) { self::$errors[] = fqp__( 'ZOHO Client Secret is required' ); } if ( ! self::$user_email ) { self::$errors[] = fqp__( 'ZOHO User Email is required' ); } if ( ! self::$authorized_redirect_uri ) { self::$errors[] = fqp__( 'Aauthorized Redirect URI is required' ); } if ( ! self::$grant_token ) { self::$errors[] = fqp__( 'ZOHO Grant Token is required' ); } if ( self::$errors ) { return false; } if ( ! self::initZCRMRestClient() ) { return false; } try { /** @var ZohoOAuthClient $oAuthClient */ $oAuthClient = ZohoOAuth::getClientInstance(); // Generate access and refresh token from grant token $oAuthClient->generateAccessToken( self::$grant_token ); } catch (ZohoOAuthException $e) { self::$errors[] = '
' . $e->getMessage() . ''; return false; } return true; } public static function revoke_token() { if ( ! self::initZCRMRestClient() ) { return false; } $persistence = ZohoOAuth::getPersistenceHandlerInstance(); try { $tokens = $persistence->getOAuthTokens( self::$user_email ); $post = [ 'token' => $tokens->getRefreshToken(), ]; $post = array_filter( $post ); $options = [ CURLOPT_POST => 1, CURLOPT_HEADER => 0, CURLOPT_URL => ZohoOAuth::getRevokeTokenURL(), CURLOPT_FRESH_CONNECT => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FORBID_REUSE => 1, CURLOPT_TIMEOUT => 4, CURLOPT_POSTFIELDS => http_build_query( $post ), CURLOPT_SSL_VERIFYPEER => false, ]; $ch = curl_init(); curl_setopt_array( $ch, $options ); if( ! $result = curl_exec( $ch ) ) { trigger_error( curl_error( $ch ) ); } curl_close( $ch ); $tokens_file = self::getTokenPersistencePath() . '/zcrm_oauthtokens.txt'; if ( $fh = fopen( $tokens_file, 'r+' ) ) { ftruncate($fh, 0); fclose($fh); } } catch (Exception $e) { self::$errors[] = '
' . $e->getMessage() . ''; return false; } return true; } }