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