← All changes
|
includes/Core/Integration/ZohoMail/ZohoMailHandler.php
+66
-27
2.0
→
3.3.1
View file →
| @@ -6,21 +6,28 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoMail; |
| 9 | 9 | |
| 10 | +if (!defined('ABSPATH')) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 10 | 14 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 | 15 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 | 16 | use BitCode\BitForm\Core\Util\IpTool; |
| 17 | +use BitCode\BitForm\GlobalHelper; | |
| 13 | 18 | use WP_Error; |
| 14 | 19 | |
| 15 | 20 | /** |
| 16 | 21 | * Provide functionality for ZohoCrm integration |
| 17 | 22 | */ |
| 18 | -class ZohoMailHandler { | |
| 23 | +class ZohoMailHandler | |
| 24 | +{ | |
| 19 | 25 | private $_formID; |
| 20 | 26 | private $_integrationID; |
| 21 | 27 | |
| 22 | - public function __construct($integrationID, $fromID) { | |
| 28 | + public function __construct($integrationID, $fromID) | |
| 29 | + { | |
| 23 | 30 | $this->_formID = $fromID; |
| 24 | 31 | $this->_integrationID = $integrationID; |
| 25 | 32 | } |
| 26 | 33 | |
| @@ -28,13 +35,11 @@ | ||
| 28 | 35 | * Helps to register ajax function's with wp |
| 29 | 36 | * |
| 30 | 37 | * @return null |
| 31 | 38 | */ |
| 32 | - public static function registerAjax() { | |
| 39 | + public static function registerAjax() | |
| 40 | + { | |
| 33 | 41 | add_action('wp_ajax_bitforms_zmail_generate_token', [__CLASS__, 'generateTokens']); |
| 34 | - add_action('wp_ajax_bitforms_zmail_refresh_workspaces', [__CLASS__, 'refreshWorkspacesAjaxHelper']); | |
| 35 | - add_action('wp_ajax_bitforms_zmail_refresh_tables', [__CLASS__, 'refreshTablesAjaxHelper']); | |
| 36 | - add_action('wp_ajax_bitforms_zmail_refresh_table_headers', [__CLASS__, 'refreshTableHeadersAjaxHelper']); | |
| 37 | 42 | } |
| 38 | 43 | |
| 39 | 44 | /** |
| 40 | 45 | * Process ajax request for generate_token |
| @@ -40,13 +45,21 @@ | ||
| 40 | 45 | * Process ajax request for generate_token |
| 41 | 46 | * |
| 42 | 47 | * @return JSON zoho crm api response and status |
| 43 | 48 | */ |
| 44 | - public static function generateTokens() { | |
| 45 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 49 | + public static function generateTokens() | |
| 50 | + { | |
| 51 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 46 | 52 | $authorizationHeader = null; |
| 47 | - $inputJSON = file_get_contents('php://input'); | |
| 48 | - $requestsParams = json_decode($inputJSON); | |
| 53 | + | |
| 54 | + GlobalHelper::requirePostMethod(); | |
| 55 | + | |
| 56 | + try { | |
| 57 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 58 | + } catch (\InvalidArgumentException $e) { | |
| 59 | + wp_send_json_error($e->getMessage(), 400); | |
| 60 | + } | |
| 61 | + | |
| 49 | 62 | if ( |
| 50 | 63 | empty($requestsParams->{'accounts-server'}) |
| 51 | 64 | || empty($requestsParams->dataCenter) |
| 52 | 65 | || empty($requestsParams->clientId) |
| @@ -72,21 +85,44 @@ | ||
| 72 | 85 | 'code' => $requestsParams->code |
| 73 | 86 | ]; |
| 74 | 87 | $apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 75 | 88 | |
| 76 | - $accountIdEndpoint = "http://mail.zoho.{$requestsParams->dataCenter}/api/accounts"; | |
| 89 | + // Validate the token exchange before using the token: reading access_token | |
| 90 | + // off an error response fatals. | |
| 91 | + if (is_wp_error($apiResponse) || !empty($apiResponse->error) || empty($apiResponse->access_token)) { | |
| 92 | + wp_send_json_error( | |
| 93 | + empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, | |
| 94 | + 400 | |
| 95 | + ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + // https, not http: the WP HTTP API drops the Authorization header on a | |
| 99 | + // redirect, so an http:// call reaches Zoho as INVALID_OAUTHTOKEN. | |
| 100 | + $accountIdEndpoint = "https://mail.zoho.{$requestsParams->dataCenter}/api/accounts"; | |
| 77 | 101 | $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$apiResponse->access_token}"; |
| 78 | 102 | $accountResponse = HttpHelper::get($accountIdEndpoint, null, $authorizationHeader); |
| 79 | 103 | |
| 80 | - $apiResponse->accountId = $accountResponse->data[0]->accountId; | |
| 81 | - $apiResponse->accountEmail = $accountResponse->data[0]->primaryEmailAddress; | |
| 104 | + // On success `data` is a list of accounts; on failure it is an object | |
| 105 | + // ({errorCode:...}), so check the shape before reading the account. | |
| 106 | + $account = null; | |
| 107 | + if (!is_wp_error($accountResponse) && isset($accountResponse->data) && is_array($accountResponse->data)) { | |
| 108 | + $account = reset($accountResponse->data); | |
| 109 | + } | |
| 82 | 110 | |
| 83 | - if (is_wp_error($apiResponse) || !empty($apiResponse->error) || is_wp_error($accountResponse) || !empty($accountResponse->errors)) { | |
| 84 | - wp_send_json_error( | |
| 85 | - empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, | |
| 86 | - 400 | |
| 87 | - ); | |
| 111 | + if (empty($account) || empty($account->accountId)) { | |
| 112 | + $reason = __('Could not read the Zoho Mail account for this token', 'bit-form'); | |
| 113 | + if (is_wp_error($accountResponse)) { | |
| 114 | + $reason = $accountResponse->get_error_message(); | |
| 115 | + } elseif (!empty($accountResponse->data->errorCode)) { | |
| 116 | + $reason = $accountResponse->data->errorCode; | |
| 117 | + } elseif (!empty($accountResponse->status->description)) { | |
| 118 | + $reason = $accountResponse->status->description; | |
| 119 | + } | |
| 120 | + wp_send_json_error($reason, 400); | |
| 88 | 121 | } |
| 122 | + | |
| 123 | + $apiResponse->accountId = $account->accountId; | |
| 124 | + $apiResponse->accountEmail = isset($account->primaryEmailAddress) ? $account->primaryEmailAddress : ''; | |
| 89 | 125 | $apiResponse->generates_on = \time(); |
| 90 | 126 | wp_send_json_success($apiResponse, 200); |
| 91 | 127 | } else { |
| 92 | 128 | wp_send_json_error( |
| @@ -101,12 +137,13 @@ | ||
| 101 | 137 | |
| 102 | 138 | /** |
| 103 | 139 | * Helps to refresh zoho crm access_token |
| 104 | 140 | * |
| 105 | - * @param Array $apiData Contains required data for refresh access token | |
| 106 | - * @return JSON $tokenDetails API token details | |
| 141 | + * @param object $apiData Contains required data for refresh access token | |
| 142 | + * @return string|boolean $tokenDetails API token details | |
| 107 | 143 | */ |
| 108 | - protected static function _refreshAccessToken($apiData) { | |
| 144 | + protected static function _refreshAccessToken($apiData) | |
| 145 | + { | |
| 109 | 146 | if ( |
| 110 | 147 | empty($apiData->dataCenter) |
| 111 | 148 | || empty($apiData->clientId) |
| 112 | 149 | || empty($apiData->clientSecret) |
| @@ -136,15 +173,16 @@ | ||
| 136 | 173 | |
| 137 | 174 | /** |
| 138 | 175 | * Save updated access_token to avoid unnecessary token generation |
| 139 | 176 | * |
| 140 | - * @param Integer $fromID ID of Integration related form | |
| 141 | - * @param Integer $integrationID ID of Zoho crm Integration | |
| 142 | - * @param Obeject $tokenDetails refreshed token info | |
| 177 | + * @param integer $fromID ID of Integration related form | |
| 178 | + * @param integer $integrationID ID of Zoho crm Integration | |
| 179 | + * @param object $tokenDetails refreshed token info | |
| 143 | 180 | * |
| 144 | 181 | * @return null |
| 145 | 182 | */ |
| 146 | - protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) { | |
| 183 | + protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) | |
| 184 | + { | |
| 147 | 185 | if (empty($formID) || empty($integrationID)) { |
| 148 | 186 | return; |
| 149 | 187 | } |
| 150 | 188 | |
| @@ -157,12 +195,13 @@ | ||
| 157 | 195 | $newDetails = json_decode($zmailDetails[0]->integration_details); |
| 158 | 196 | |
| 159 | 197 | $newDetails->tokenDetails = $tokenDetails; |
| 160 | 198 | |
| 161 | - $integrationHandler->updateIntegration($integrationID, $zmailDetails[0]->integration_name, 'Zoho Mail', \json_encode($newDetails), 'form'); | |
| 199 | + $integrationHandler->updateIntegration($integrationID, $zmailDetails[0]->integration_name, 'Zoho Mail', wp_json_encode($newDetails), 'form'); | |
| 162 | 200 | } |
| 163 | 201 | |
| 164 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 202 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 203 | + { | |
| 165 | 204 | $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 166 | 205 | |
| 167 | 206 | $tokenDetails = $integrationDetails->tokenDetails; |
| 168 | 207 | |