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