PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.02.02
E2Pdf – Export Pdf Tool for WordPress v1.02.02
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 1.08.00 1.08.06 All 72 releases
e2pdf / classes / model / e2pdf-adobesign.php

e2pdf-adobesign.php in E2Pdf – Export Pdf Tool for WordPress 1.02.02, at classes/model/e2pdf-adobesign.php

276 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
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
11 */
12 if (!defined('ABSPATH')) {
13 die('Access denied.');
14 }
15
16 class Model_E2pdf_AdobeSign extends Model_E2pdf_Model {
17
18 const DEFAULT_CURL_TIMEOUT = 300;
19
20 protected $oauth_url = 'https://secure.%s.echosign.com/';
21 protected $api = null;
22 protected $provider = array(
23 'api_url' => false,
24 'access_token' => false,
25 'refresh_token' => false,
26 'code' => false,
27 'client_id' => false,
28 'client_secret' => false
29 );
30
31 function __construct() {
32 parent::__construct();
33
34 $this->provider = array(
35 'api_url' => get_option('e2pdf_adobesign_api_access_point'),
36 'access_token' => get_transient('e2pdf_adobesign_access_token'),
37 'refresh_token' => get_option('e2pdf_adobesign_refresh_token'),
38 'code' => get_option('e2pdf_adobesign_code'),
39 'client_id' => get_option('e2pdf_adobesign_client_id'),
40 'client_secret' => get_option('e2pdf_adobesign_client_secret')
41 );
42
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')) {
60 set_transient('e2pdf_adobesign_access_token', $access_token, 1800);
61 set_transient('e2pdf_adobesign_refresh_token', 'updated', 2592000);
62 $this->provider['access_token'] = $access_token;
63 }
64 $this->flush();
65 } else {
66 set_transient('e2pdf_adobesign_refresh_token', 'unavailable', 2592000);
67 }
68 }
69
70 /**
71 * Request code for refresh_token request
72 *
73 * @return array
74 */
75 public function request_code() {
76 $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";
93 $scopes = array();
94 $options = Model_E2pdf_Options::get_options(false, array('adobesign_group'));
95
96 foreach ($options as $group_key => $group) {
97 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'];
100 }
101 }
102 }
103 $data = array(
104 'redirect_uri' => urlencode($this->helper->get_url(array('page' => 'e2pdf-settings', 'action' => 'adobesign'))),
105 'response_type' => 'code',
106 'client_id' => get_option('e2pdf_adobesign_client_id'),
107 'scope' => implode("+", $scopes),
108 );
109 $location .= "?" . urldecode(http_build_query($data));
110 $response['redirect'] = $location;
111 }
112 return $response;
113 }
114
115 /**
116 * Request refresh_token request
117 *
118 * @return array
119 */
120 public function request_refresh_token() {
121
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');
131 } 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 }
155 }
156 return $response;
157 }
158
159 /**
160 * Request to Adobe Sign API
161 *
162 * @return array
163 */
164 public function request($key = false) {
165 if ($this->api->action && $this->provider['api_url']) {
166
167 $response = array();
168 $headers = array();
169 $data = array();
170 $json_data = "[]";
171
172 if (!empty($this->api->data)) {
173 $data = array_merge($data, $this->api->data);
174 }
175
176 if ($this->api->action != 'oauth/refresh' && $this->api->action != 'oauth/token') {
177 if ($this->provider['access_token']) {
178 $headers[] = 'Authorization: Bearear';
179 $headers[] = 'Access-Token: ' . $this->provider['access_token'];
180 } else {
181 $response['error'] = __("Access Token is not set or Incorrect", "e2pdf");
182 return $response;
183 }
184 }
185
186 if ($this->api->action == 'oauth/refresh' || $this->api->action == 'oauth/token') {
187 $headers[] = 'Content-Type: application/x-www-form-urlencoded';
188 } elseif ($this->api->action == 'api/rest/v5/transientDocuments') {
189 $headers[] = 'Content-Type: multipart/form-data';
190 } else {
191 $json_data = json_encode($data);
192 $headers[] = 'Content-Type: application/json';
193 $headers[] = 'Content-Length: ' . strlen($json_data);
194 }
195
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');
198 $ch = curl_init($request_url);
199 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
200 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
201 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
202 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
203 if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
204 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
205 }
206 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $e2pdf_connection_timeout);
207
208 if ($this->api->method) {
209 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->api->method);
210 }
211
212 if ($this->api->action == 'oauth/refresh' || $this->api->action == 'oauth/token') {
213 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
214 } elseif ($this->api->action == 'api/rest/v5/transientDocuments') {
215 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
216 } else {
217 curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
218 }
219
220 $json = curl_exec($ch);
221 $curl_errno = curl_errno($ch);
222 $curl_error = curl_error($ch);
223 curl_close($ch);
224
225 if ($curl_errno > 0) {
226 $response['error'] = "[{$curl_errno}] {$curl_error}";
227 } else {
228 $result = json_decode($json, true);
229 $response = $result;
230 if (empty($response)) {
231 $response['error'] = __("Something went wrong!", "e2pdf");
232 }
233 }
234
235 if ($key) {
236 if (isset($response[$key])) {
237 return $response[$key];
238 } else {
239 return false;
240 }
241 } else {
242 return $response;
243 }
244 }
245
246 return false;
247 }
248
249 /**
250 * Remove API Request options
251 */
252 public function flush() {
253 $this->api = null;
254 }
255
256 /**
257 * Set options for API Request
258 *
259 * @param string $key - Key
260 * @param mixed $value - Value
261 */
262 public function set($key, $value = false) {
263 if (!$this->api) {
264 $this->api = new stdClass();
265 }
266 if (is_array($key)) {
267 foreach ($key as $attr => $value) {
268 $this->api->$attr = $value;
269 }
270 } else {
271 $this->api->$key = $value;
272 }
273 }
274
275 }
276