| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-adobesign.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Model_E2pdf_AdobeSign extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
protected $api = null; |
| 17 |
protected $provider = array( |
| 18 |
'api_access_point' => false, |
| 19 |
'web_access_point' => false, |
| 20 |
'access_token' => false, |
| 21 |
'refresh_token' => false, |
| 22 |
'code' => false, |
| 23 |
'client_id' => false, |
| 24 |
'client_secret' => false, |
| 25 |
); |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
|
| 29 |
parent::__construct(); |
| 30 |
|
| 31 |
$this->provider = array( |
| 32 |
'api_access_point' => get_option('e2pdf_adobesign_api_access_point'), |
| 33 |
'web_access_point' => get_option('e2pdf_adobesign_web_access_point'), |
| 34 |
'access_token' => get_transient('e2pdf_adobesign_access_token'), |
| 35 |
'refresh_token' => get_option('e2pdf_adobesign_refresh_token'), |
| 36 |
'code' => get_option('e2pdf_adobesign_code'), |
| 37 |
'client_id' => get_option('e2pdf_adobesign_client_id'), |
| 38 |
'client_secret' => get_option('e2pdf_adobesign_client_secret'), |
| 39 |
); |
| 40 |
|
| 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) { |
| 55 |
set_transient('e2pdf_adobesign_access_token', $access_token, 1800); |
| 56 |
set_transient('e2pdf_adobesign_refresh_token', $this->provider['refresh_token'], 2592000); |
| 57 |
$this->provider['access_token'] = $access_token; |
| 58 |
} |
| 59 |
$this->flush(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// get code |
| 64 |
public function get_code() { |
| 65 |
$response = array(); |
| 66 |
if ($this->provider['client_id'] && $this->provider['client_secret']) { |
| 67 |
$scopes = array(); |
| 68 |
$options = Model_E2pdf_Options::get_options(false, array('adobesign_group')); |
| 69 |
foreach ($options as $group_key => $group) { |
| 70 |
foreach ($group['options'] as $option_key => $option_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']; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
$data = array( |
| 77 |
'response_type' => 'code', |
| 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), |
| 89 |
); |
| 90 |
$response['redirect'] = sprintf('https://secure.%s.echosign.com/public/oauth/v2', get_option('e2pdf_adobesign_region')) . '?' . urldecode(http_build_query($data)); |
| 91 |
} |
| 92 |
return $response; |
| 93 |
} |
| 94 |
|
| 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(); |
| 116 |
|
| 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']; |
| 126 |
} else { |
| 127 |
$response['error'] = __('Something went wrong!', 'e2pdf'); |
| 128 |
} |
| 129 |
return $response; |
| 130 |
} |
| 131 |
|
| 132 |
// request |
| 133 |
public function request($key = false) { |
| 134 |
|
| 135 |
if ($this->api && isset($this->api->action) && ($this->provider['api_access_point'] || $this->api->action == 'oauth/v2/token')) { |
| 136 |
|
| 137 |
$response = array(); |
| 138 |
$headers = array(); |
| 139 |
$data = array(); |
| 140 |
$json_data = '[]'; |
| 141 |
|
| 142 |
if (!empty($this->api->data)) { |
| 143 |
$data = array_merge($data, $this->api->data); |
| 144 |
} |
| 145 |
|
| 146 |
if ($this->api->action != 'oauth/v2/refresh' && $this->api->action != 'oauth/v2/token') { |
| 147 |
if ($this->provider['access_token']) { |
| 148 |
$headers[] = 'Authorization: Bearear'; |
| 149 |
$headers[] = 'Access-Token: ' . $this->provider['access_token']; |
| 150 |
} else { |
| 151 |
$response['error'] = __('Access Token is not set or Incorrect', 'e2pdf'); |
| 152 |
return $response; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if ($this->api->action == 'oauth/v2/token') { |
| 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'; |
| 161 |
} elseif ($this->api->action == 'api/rest/v5/transientDocuments') { |
| 162 |
$headers[] = 'Content-Type: multipart/form-data'; |
| 163 |
} else { |
| 164 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
| 165 |
$json_data = json_encode($data); |
| 166 |
$headers[] = 'Content-Type: application/json'; |
| 167 |
$headers[] = 'Content-Length: ' . strlen($json_data); |
| 168 |
} |
| 169 |
|
| 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 |
| 174 |
$ch = curl_init($request_url); |
| 175 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 176 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 177 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 178 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 179 |
if (!ini_get('safe_mode') && !ini_get('open_basedir')) { |
| 180 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 181 |
} |
| 182 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
| 183 |
|
| 184 |
if (isset($this->api->method) && $this->api->method) { |
| 185 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->api->method); |
| 186 |
} |
| 187 |
|
| 188 |
if ($this->api->action == 'oauth/v2/refresh' || $this->api->action == 'oauth/v2/token') { |
| 189 |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
| 190 |
} elseif ($this->api->action == 'api/rest/v5/transientDocuments') { |
| 191 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 192 |
} else { |
| 193 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); |
| 194 |
} |
| 195 |
|
| 196 |
$json = curl_exec($ch); |
| 197 |
$curl_errno = curl_errno($ch); |
| 198 |
$curl_error = curl_error($ch); |
| 199 |
curl_close($ch); |
| 200 |
// phpcs:enable |
| 201 |
|
| 202 |
if ($curl_errno > 0) { |
| 203 |
$response['error'] = "[{$curl_errno}] {$curl_error}"; |
| 204 |
} else { |
| 205 |
$result = json_decode($json, true); |
| 206 |
$response = $result; |
| 207 |
if (empty($response)) { |
| 208 |
$response['error'] = __('Something went wrong!', 'e2pdf'); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if ($key) { |
| 213 |
if (isset($response[$key])) { |
| 214 |
return $response[$key]; |
| 215 |
} else { |
| 216 |
return false; |
| 217 |
} |
| 218 |
} else { |
| 219 |
return $response; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
// flush |
| 227 |
public function flush() { |
| 228 |
$this->api = null; |
| 229 |
} |
| 230 |
|
| 231 |
// set |
| 232 |
public function set($key, $value = false) { |
| 233 |
if (!$this->api) { |
| 234 |
$this->api = new stdClass(); |
| 235 |
} |
| 236 |
if (is_array($key)) { |
| 237 |
foreach ($key as $attr => $value) { |
| 238 |
$this->api->$attr = $value; |
| 239 |
} |
| 240 |
} else { |
| 241 |
$this->api->$key = $value; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|