| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * E2pdf Adobe Sign Model | |
| 5 | - * | |
| 6 | - * @copyright Copyright 2017 https://e2pdf.com | |
| 7 | - * @license GPL v2 | |
| 8 | - * @version 1 | |
| 9 | - * @link https://e2pdf.com | |
| 10 | - * @since 1.02.00 | |
| 4 | + * File: /model/e2pdf-adobesign.php | |
| 5 | + * | |
| 6 | + * @package E2Pdf | |
| 7 | + * @license GPLv3 | |
| 8 | + * @link https://e2pdf.com | |
| 11 | 9 | */ |
| 12 | 10 | if (!defined('ABSPATH')) { |
| 13 | 11 | die('Access denied.'); |
| 14 | 12 | } |
| @@ -14,186 +12,166 @@ | ||
| 14 | 12 | } |
| 15 | 13 | |
| 16 | 14 | class Model_E2pdf_AdobeSign extends Model_E2pdf_Model { |
| 17 | 15 | |
| 18 | - protected $oauth_url = 'https://secure.%s.echosign.com/'; | |
| 19 | 16 | protected $api = null; |
| 20 | 17 | protected $provider = array( |
| 21 | - 'api_url' => false, | |
| 18 | + 'api_access_point' => false, | |
| 19 | + 'web_access_point' => false, | |
| 22 | 20 | 'access_token' => false, |
| 23 | 21 | 'refresh_token' => false, |
| 24 | 22 | 'code' => false, |
| 25 | 23 | 'client_id' => false, |
| 26 | - 'client_secret' => false | |
| 24 | + 'client_secret' => false, | |
| 27 | 25 | ); |
| 28 | 26 | |
| 29 | - function __construct() { | |
| 27 | + public function __construct() { | |
| 28 | + | |
| 30 | 29 | parent::__construct(); |
| 31 | 30 | |
| 32 | 31 | $this->provider = array( |
| 33 | - 'api_url' => get_option('e2pdf_adobesign_api_access_point'), | |
| 32 | + 'api_access_point' => get_option('e2pdf_adobesign_api_access_point'), | |
| 33 | + 'web_access_point' => get_option('e2pdf_adobesign_web_access_point'), | |
| 34 | 34 | 'access_token' => get_transient('e2pdf_adobesign_access_token'), |
| 35 | 35 | 'refresh_token' => get_option('e2pdf_adobesign_refresh_token'), |
| 36 | 36 | 'code' => get_option('e2pdf_adobesign_code'), |
| 37 | 37 | 'client_id' => get_option('e2pdf_adobesign_client_id'), |
| 38 | - 'client_secret' => get_option('e2pdf_adobesign_client_secret') | |
| 38 | + 'client_secret' => get_option('e2pdf_adobesign_client_secret'), | |
| 39 | 39 | ); |
| 40 | 40 | |
| 41 | - //refresh access_token | |
| 42 | - if (!$this->provider['access_token'] && | |
| 43 | - $this->provider['api_url'] && | |
| 44 | - $this->provider['client_id'] && | |
| 45 | - $this->provider['client_secret'] && | |
| 46 | - $this->provider['refresh_token'] | |
| 47 | - ) { | |
| 48 | - $this->set(array( | |
| 49 | - 'action' => 'oauth/refresh', | |
| 50 | - 'data' => array( | |
| 51 | - 'grant_type' => 'refresh_token', | |
| 52 | - 'client_id' => $this->provider['client_id'], | |
| 53 | - 'client_secret' => $this->provider['client_secret'], | |
| 54 | - 'refresh_token' => $this->provider['refresh_token'], | |
| 55 | - ), | |
| 56 | - )); | |
| 57 | - if ($access_token = $this->request('access_token')) { | |
| 41 | + if (!$this->provider['access_token'] && $this->provider['refresh_token']) { | |
| 42 | + $this->set( | |
| 43 | + array( | |
| 44 | + 'action' => 'oauth/v2/refresh', | |
| 45 | + 'data' => array( | |
| 46 | + 'grant_type' => 'refresh_token', | |
| 47 | + 'client_id' => $this->provider['client_id'], | |
| 48 | + 'client_secret' => $this->provider['client_secret'], | |
| 49 | + 'refresh_token' => $this->provider['refresh_token'], | |
| 50 | + ), | |
| 51 | + ) | |
| 52 | + ); | |
| 53 | + $access_token = $this->request('access_token'); | |
| 54 | + if ($access_token) { | |
| 58 | 55 | set_transient('e2pdf_adobesign_access_token', $access_token, 1800); |
| 59 | - set_transient('e2pdf_adobesign_refresh_token', 'updated', 2592000); | |
| 56 | + set_transient('e2pdf_adobesign_refresh_token', $this->provider['refresh_token'], 2592000); | |
| 60 | 57 | $this->provider['access_token'] = $access_token; |
| 61 | 58 | } |
| 62 | 59 | $this->flush(); |
| 63 | - } else { | |
| 64 | - set_transient('e2pdf_adobesign_refresh_token', 'unavailable', 2592000); | |
| 65 | 60 | } |
| 66 | 61 | } |
| 67 | 62 | |
| 68 | - /** | |
| 69 | - * Request code for refresh_token request | |
| 70 | - * | |
| 71 | - * @return array | |
| 72 | - */ | |
| 73 | - public function request_code() { | |
| 63 | + // get code | |
| 64 | + public function get_code() { | |
| 74 | 65 | $response = array(); |
| 75 | - if (!get_option('e2pdf_adobesign_region') && !$this->provider['client_id'] && !$this->provider['client_secret']) { | |
| 76 | - set_transient('e2pdf_adobesign_access_token', false); | |
| 77 | - set_transient('e2pdf_adobesign_refresh_token', 'unavailable', 2592000); | |
| 78 | - update_option('e2pdf_adobesign_code', false); | |
| 79 | - update_option('e2pdf_adobesign_api_access_point', false); | |
| 80 | - update_option('e2pdf_adobesign_refresh_token', false); | |
| 81 | - } elseif (!get_option('e2pdf_adobesign_region')) { | |
| 82 | - $response['error'] = __('Region is required', 'e2pdf'); | |
| 83 | - } elseif (!$this->provider['client_id']) { | |
| 84 | - $response['error'] = __('Cliend ID is required', 'e2pdf'); | |
| 85 | - } elseif (!$this->provider['client_secret']) { | |
| 86 | - $response['error'] = __('Client Secret is required', 'e2pdf'); | |
| 87 | - } else { | |
| 88 | - $this->oauth_url = sprintf($this->oauth_url, get_option('e2pdf_adobesign_region')); | |
| 89 | - | |
| 90 | - $location = $this->oauth_url . "public/oauth"; | |
| 66 | + if ($this->provider['client_id'] && $this->provider['client_secret']) { | |
| 91 | 67 | $scopes = array(); |
| 92 | 68 | $options = Model_E2pdf_Options::get_options(false, array('adobesign_group')); |
| 93 | - | |
| 94 | 69 | foreach ($options as $group_key => $group) { |
| 95 | 70 | foreach ($group['options'] as $option_key => $option_value) { |
| 96 | - if (substr($option_value['key'], 0, 21) === "e2pdf_adobesign_scope" && $option_value['value']) { | |
| 97 | - $scopes[] = substr($option_value['key'], 22) . ":" . $option_value['value']; | |
| 71 | + if (substr($option_value['key'], 0, 21) === 'e2pdf_adobesign_scope' && $option_value['value']) { | |
| 72 | + $scopes[] = substr($option_value['key'], 22) . ':' . $option_value['value']; | |
| 98 | 73 | } |
| 99 | 74 | } |
| 100 | 75 | } |
| 101 | 76 | $data = array( |
| 102 | - 'redirect_uri' => urlencode($this->helper->get_url(array('page' => 'e2pdf-settings', 'action' => 'adobesign'))), | |
| 103 | 77 | 'response_type' => 'code', |
| 104 | - 'client_id' => get_option('e2pdf_adobesign_client_id'), | |
| 105 | - 'scope' => implode("+", $scopes), | |
| 78 | + 'client_id' => $this->provider['client_id'], | |
| 79 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode | |
| 80 | + 'redirect_uri' => urlencode( | |
| 81 | + get_option('e2pdf_adobe_api_version') == '1' ? site_url('/e2pdf-rpc/v1/adobe/auth?api_key=' . get_option('e2pdf_adobe_api_key')) : $this->helper->get_url( | |
| 82 | + array( | |
| 83 | + 'page' => 'e2pdf-settings', | |
| 84 | + 'action' => 'adobesign', | |
| 85 | + ) | |
| 86 | + ), | |
| 87 | + ), | |
| 88 | + 'scope' => implode('+', $scopes), | |
| 106 | 89 | ); |
| 107 | - $location .= "?" . urldecode(http_build_query($data)); | |
| 108 | - $response['redirect'] = $location; | |
| 90 | + $response['redirect'] = sprintf('https://secure.%s.echosign.com/public/oauth/v2', get_option('e2pdf_adobesign_region')) . '?' . urldecode(http_build_query($data)); | |
| 109 | 91 | } |
| 110 | 92 | return $response; |
| 111 | 93 | } |
| 112 | 94 | |
| 113 | - /** | |
| 114 | - * Request refresh_token request | |
| 115 | - * | |
| 116 | - * @return array | |
| 117 | - */ | |
| 118 | - public function request_refresh_token() { | |
| 95 | + // get token | |
| 96 | + public function get_token() { | |
| 97 | + $response = array(); | |
| 98 | + $this->set( | |
| 99 | + array( | |
| 100 | + 'action' => 'oauth/v2/token', | |
| 101 | + 'data' => array( | |
| 102 | + 'grant_type' => 'authorization_code', | |
| 103 | + 'redirect_uri' => get_option('e2pdf_adobe_api_version') == '1' ? site_url('/e2pdf-rpc/v1/adobe/auth?api_key=' . get_option('e2pdf_adobe_api_key')) : $this->helper->get_url( | |
| 104 | + array( | |
| 105 | + 'page' => 'e2pdf-settings', | |
| 106 | + 'action' => 'adobesign', | |
| 107 | + ) | |
| 108 | + ), | |
| 109 | + 'client_id' => $this->provider['client_id'], | |
| 110 | + 'client_secret' => $this->provider['client_secret'], | |
| 111 | + 'code' => $this->provider['code'], | |
| 112 | + ), | |
| 113 | + ) | |
| 114 | + ); | |
| 115 | + $request = $this->request(); | |
| 119 | 116 | |
| 120 | - $response = array(); | |
| 121 | - if (!$this->provider['api_url']) { | |
| 122 | - $response['error'] = __('API Url is not set or Incorrect', 'e2pdf'); | |
| 123 | - } elseif (!$this->provider['code']) { | |
| 124 | - $response['error'] = __('Code is not set or Incorrect', 'e2pdf'); | |
| 125 | - } elseif (!$this->provider['client_id']) { | |
| 126 | - $response['error'] = __('Cliend ID is required', 'e2pdf'); | |
| 127 | - } elseif (!$this->provider['client_secret']) { | |
| 128 | - $response['error'] = __('Client Secret is required', 'e2pdf'); | |
| 117 | + $this->flush(); | |
| 118 | + if (isset($request['access_token']) && isset($request['refresh_token'])) { | |
| 119 | + update_option('e2pdf_adobesign_refresh_token', $request['refresh_token']); | |
| 120 | + update_option('e2pdf_adobesign_api_access_point', $request['api_access_point']); | |
| 121 | + update_option('e2pdf_adobesign_web_access_point', $request['web_access_point']); | |
| 122 | + set_transient('e2pdf_adobesign_access_token', $request['access_token'], 1800); | |
| 123 | + set_transient('e2pdf_adobesign_refresh_token', $request['refresh_token'], 2592000); | |
| 124 | + } elseif (isset($request['error'])) { | |
| 125 | + $response['error'] = $request['error']; | |
| 129 | 126 | } else { |
| 130 | - | |
| 131 | - $this->set(array( | |
| 132 | - 'action' => 'oauth/token', | |
| 133 | - 'data' => array( | |
| 134 | - 'grant_type' => 'authorization_code', | |
| 135 | - 'redirect_uri' => $this->helper->get_url(array('page' => 'e2pdf-settings', 'action' => 'adobesign')), | |
| 136 | - 'client_id' => $this->provider['client_id'], | |
| 137 | - 'client_secret' => $this->provider['client_secret'], | |
| 138 | - 'code' => $this->provider['code'], | |
| 139 | - ), | |
| 140 | - )); | |
| 141 | - | |
| 142 | - $request = $this->request(); | |
| 143 | - $this->flush(); | |
| 144 | - if (isset($request['access_token']) && isset($request['refresh_token'])) { | |
| 145 | - set_transient('e2pdf_adobesign_access_token', $request['access_token'], 1800); | |
| 146 | - set_transient('e2pdf_adobesign_refresh_token', 'updated', 2592000); | |
| 147 | - update_option('e2pdf_adobesign_refresh_token', $request['refresh_token']); | |
| 148 | - } elseif (isset($request['error'])) { | |
| 149 | - $response['error'] = $request['error']; | |
| 150 | - } else { | |
| 151 | - $response['error'] = $response['error'] = __("Something went wrong!", "e2pdf"); | |
| 152 | - } | |
| 127 | + $response['error'] = __('Something went wrong!', 'e2pdf'); | |
| 153 | 128 | } |
| 154 | 129 | return $response; |
| 155 | 130 | } |
| 156 | 131 | |
| 157 | - /** | |
| 158 | - * Request to Adobe Sign API | |
| 159 | - * | |
| 160 | - * @return array | |
| 161 | - */ | |
| 132 | + // request | |
| 162 | 133 | public function request($key = false) { |
| 163 | - if ($this->api->action && $this->provider['api_url']) { | |
| 164 | 134 | |
| 135 | + if ($this->api && isset($this->api->action) && ($this->provider['api_access_point'] || $this->api->action == 'oauth/v2/token')) { | |
| 136 | + | |
| 165 | 137 | $response = array(); |
| 166 | 138 | $headers = array(); |
| 167 | 139 | $data = array(); |
| 168 | - $json_data = "[]"; | |
| 140 | + $json_data = '[]'; | |
| 169 | 141 | |
| 170 | 142 | if (!empty($this->api->data)) { |
| 171 | 143 | $data = array_merge($data, $this->api->data); |
| 172 | 144 | } |
| 173 | 145 | |
| 174 | - if ($this->api->action != 'oauth/refresh' && $this->api->action != 'oauth/token') { | |
| 146 | + if ($this->api->action != 'oauth/v2/refresh' && $this->api->action != 'oauth/v2/token') { | |
| 175 | 147 | if ($this->provider['access_token']) { |
| 176 | 148 | $headers[] = 'Authorization: Bearear'; |
| 177 | 149 | $headers[] = 'Access-Token: ' . $this->provider['access_token']; |
| 178 | 150 | } else { |
| 179 | - $response['error'] = __("Access Token is not set or Incorrect", "e2pdf"); | |
| 151 | + $response['error'] = __('Access Token is not set or Incorrect', 'e2pdf'); | |
| 180 | 152 | return $response; |
| 181 | 153 | } |
| 182 | 154 | } |
| 183 | 155 | |
| 184 | - if ($this->api->action == 'oauth/refresh' || $this->api->action == 'oauth/token') { | |
| 156 | + if ($this->api->action == 'oauth/v2/token') { | |
| 185 | 157 | $headers[] = 'Content-Type: application/x-www-form-urlencoded'; |
| 158 | + $this->provider['api_access_point'] = 'https://api.echosign.com/'; | |
| 159 | + } elseif ($this->api->action == 'oauth/v2/refresh') { | |
| 160 | + $headers[] = 'Content-Type: application/x-www-form-urlencoded'; | |
| 186 | 161 | } elseif ($this->api->action == 'api/rest/v5/transientDocuments') { |
| 187 | 162 | $headers[] = 'Content-Type: multipart/form-data'; |
| 188 | 163 | } else { |
| 164 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode | |
| 189 | 165 | $json_data = json_encode($data); |
| 190 | 166 | $headers[] = 'Content-Type: application/json'; |
| 191 | 167 | $headers[] = 'Content-Length: ' . strlen($json_data); |
| 192 | 168 | } |
| 193 | 169 | |
| 194 | - $request_url = $this->provider['api_url'] . $this->api->action; | |
| 195 | - $timeout = get_option('e2pdf_connection_timeout') === false ? 300 : get_option('e2pdf_connection_timeout'); | |
| 170 | + $request_url = $this->provider['api_access_point'] . $this->api->action; | |
| 171 | + $timeout = get_option('e2pdf_connection_timeout', '300'); | |
| 172 | + | |
| 173 | + // phpcs:disable WordPress.WP.AlternativeFunctions | |
| 196 | 174 | $ch = curl_init($request_url); |
| 197 | 175 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 198 | 176 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 199 | 177 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| @@ -202,13 +180,13 @@ | ||
| 202 | 180 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 203 | 181 | } |
| 204 | 182 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
| 205 | 183 | |
| 206 | - if ($this->api->method) { | |
| 184 | + if (isset($this->api->method) && $this->api->method) { | |
| 207 | 185 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->api->method); |
| 208 | 186 | } |
| 209 | 187 | |
| 210 | - if ($this->api->action == 'oauth/refresh' || $this->api->action == 'oauth/token') { | |
| 188 | + if ($this->api->action == 'oauth/v2/refresh' || $this->api->action == 'oauth/v2/token') { | |
| 211 | 189 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
| 212 | 190 | } elseif ($this->api->action == 'api/rest/v5/transientDocuments') { |
| 213 | 191 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 214 | 192 | } else { |
| @@ -218,8 +196,9 @@ | ||
| 218 | 196 | $json = curl_exec($ch); |
| 219 | 197 | $curl_errno = curl_errno($ch); |
| 220 | 198 | $curl_error = curl_error($ch); |
| 221 | 199 | curl_close($ch); |
| 200 | + // phpcs:enable | |
| 222 | 201 | |
| 223 | 202 | if ($curl_errno > 0) { |
| 224 | 203 | $response['error'] = "[{$curl_errno}] {$curl_error}"; |
| 225 | 204 | } else { |
| @@ -225,9 +204,9 @@ | ||
| 225 | 204 | } else { |
| 226 | 205 | $result = json_decode($json, true); |
| 227 | 206 | $response = $result; |
| 228 | 207 | if (empty($response)) { |
| 229 | - $response['error'] = __("Something went wrong!", "e2pdf"); | |
| 208 | + $response['error'] = __('Something went wrong!', 'e2pdf'); | |
| 230 | 209 | } |
| 231 | 210 | } |
| 232 | 211 | |
| 233 | 212 | if ($key) { |
| @@ -243,21 +222,14 @@ | ||
| 243 | 222 | |
| 244 | 223 | return false; |
| 245 | 224 | } |
| 246 | 225 | |
| 247 | - /** | |
| 248 | - * Remove API Request options | |
| 249 | - */ | |
| 226 | + // flush | |
| 250 | 227 | public function flush() { |
| 251 | 228 | $this->api = null; |
| 252 | 229 | } |
| 253 | 230 | |
| 254 | - /** | |
| 255 | - * Set options for API Request | |
| 256 | - * | |
| 257 | - * @param string $key - Key | |
| 258 | - * @param mixed $value - Value | |
| 259 | - */ | |
| 231 | + // set | |
| 260 | 232 | public function set($key, $value = false) { |
| 261 | 233 | if (!$this->api) { |
| 262 | 234 | $this->api = new stdClass(); |
| 263 | 235 | } |
| @@ -268,6 +240,5 @@ | ||
| 268 | 240 | } else { |
| 269 | 241 | $this->api->$key = $value; |
| 270 | 242 | } |
| 271 | 243 | } |
| 272 | - | |
| 273 | 244 | } |