| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /model/e2pdf-api.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_Api extends Model_E2pdf_Model { |
| 15 |
|
| 16 |
protected $api; |
| 17 |
|
| 18 |
// request |
| 19 |
public function request($key = false, $api_server = false) { |
| 20 |
if ($this->api->action) { |
| 21 |
|
| 22 |
// fix for upgrade via dashboard -> updates |
| 23 |
if (method_exists($this->helper, 'set_time_limit')) { |
| 24 |
$this->helper->set_time_limit((int) get_option('e2pdf_request_timeout', '420')); |
| 25 |
} |
| 26 |
|
| 27 |
// fix for upgrade via dashboard -> updates |
| 28 |
if ($this->api->action == 'update/info' && !method_exists($this->helper, 'get_site_url')) { |
| 29 |
return []; |
| 30 |
} |
| 31 |
|
| 32 |
$api_protocol = get_option('e2pdf_api_protocol', '0'); |
| 33 |
$api_processor = get_option('e2pdf_debug', '0') && get_option('e2pdf_processor', '0') ? get_option('e2pdf_processor', '0') : '0'; |
| 34 |
if ($api_processor == '2') { |
| 35 |
$api_version = '1.16.19'; |
| 36 |
} else { |
| 37 |
$api_version = '1.32.14'; |
| 38 |
} |
| 39 |
|
| 40 |
$data = [ |
| 41 |
'api_url' => $this->helper->get_site_url(), |
| 42 |
'api_license_key' => $this->get_license(), |
| 43 |
'api_processor' => apply_filters('e2pdf_api_processor', $api_processor), |
| 44 |
'api_version' => apply_filters('e2pdf_api_version', $api_version), |
| 45 |
'api_protocol' => $api_protocol, |
| 46 |
]; |
| 47 |
if (!$api_server) { |
| 48 |
$api_server = $this->get_api_server(); |
| 49 |
} |
| 50 |
$request_url = 'https://' . $api_server . '/' . $this->api->action; |
| 51 |
|
| 52 |
// phpcs:disable WordPress.WP.AlternativeFunctions |
| 53 |
$ch = apply_filters('e2pdf_api_connection', curl_init($request_url)); |
| 54 |
curl_setopt($ch, CURLOPT_USERAGENT, $this->helper->get_site_url()); |
| 55 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 56 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 57 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 58 |
if (!ini_get('safe_mode') && !ini_get('open_basedir')) { |
| 59 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 60 |
} |
| 61 |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) get_option('e2pdf_connection_timeout', '300')); |
| 62 |
|
| 63 |
if (defined('E2PDF_API_PROXY')) { |
| 64 |
curl_setopt($ch, CURLOPT_PROXY, E2PDF_API_PROXY); |
| 65 |
} |
| 66 |
if (defined('E2PDF_API_PROXYPORT')) { |
| 67 |
curl_setopt($ch, CURLOPT_PROXYPORT, E2PDF_API_PROXYPORT); |
| 68 |
} |
| 69 |
if (defined('E2PDF_API_PROXYUSERPWD')) { |
| 70 |
curl_setopt($ch, CURLOPT_PROXYUSERPWD, E2PDF_API_PROXYUSERPWD); |
| 71 |
} |
| 72 |
if (defined('E2PDF_API_PROXYTYPE')) { |
| 73 |
curl_setopt($ch, CURLOPT_PROXYTYPE, E2PDF_API_PROXYTYPE); |
| 74 |
} |
| 75 |
if (defined('E2PDF_API_PROXYAUTH')) { |
| 76 |
curl_setopt($ch, CURLOPT_PROXYAUTH, E2PDF_API_PROXYAUTH); |
| 77 |
} |
| 78 |
if (class_exists('CURLFile')) { |
| 79 |
if (defined('CURLOPT_SAFE_UPLOAD')) { |
| 80 |
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); |
| 81 |
} |
| 82 |
} else { |
| 83 |
if (defined('CURLOPT_SAFE_UPLOAD')) { |
| 84 |
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (!empty($this->api->data)) { |
| 89 |
$data = array_merge($data, $this->api->data); |
| 90 |
} |
| 91 |
if ($this->api->action === 'template/upload2') { |
| 92 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
| 93 |
} else { |
| 94 |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
| 95 |
} |
| 96 |
|
| 97 |
$json = curl_exec($ch); |
| 98 |
$curl_errno = curl_errno($ch); |
| 99 |
$curl_error = curl_error($ch); |
| 100 |
$curl_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
| 101 |
$curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 102 |
curl_close($ch); |
| 103 |
// phpcs:enable |
| 104 |
|
| 105 |
if ($curl_errno > 0) { |
| 106 |
if ($this->get_api_server() === $this->get_api_server($api_server)) { |
| 107 |
$response['error'] = '[' . $curl_errno . '] ' . $curl_error; |
| 108 |
} else { |
| 109 |
return $this->request($key, $this->get_api_server($api_server)); |
| 110 |
} |
| 111 |
} elseif ($curl_code < 200 || $curl_code >= 300) { |
| 112 |
if ($this->get_api_server() === $this->get_api_server($api_server)) { |
| 113 |
$response['error'] = '[' . $curl_code . ']'; |
| 114 |
} else { |
| 115 |
return $this->request($key, $this->get_api_server($api_server)); |
| 116 |
} |
| 117 |
} else { |
| 118 |
if ($api_protocol == '1' && strpos($curl_type, 'application/octet-stream') !== false) { |
| 119 |
$response['file'] = $json; |
| 120 |
} else { |
| 121 |
$result = json_decode($json, true); |
| 122 |
$response = isset($result['response']) ? $result['response'] : null; |
| 123 |
if (!empty($response['file'])) { |
| 124 |
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 125 |
$response['file'] = base64_decode($response['file']); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (($this->api->action === 'common/activate' || $this->api->action === 'license/update' || $this->api->action === 'license/request') && !empty($response['license_key'])) { |
| 130 |
update_option('e2pdf_license', $response['license_key']); |
| 131 |
} |
| 132 |
if ($this->api->action === 'common/activate' && !empty($response['e2pdf_api']) && get_option('e2pdf_api') === false) { |
| 133 |
if ($response['e2pdf_api'] === 'api3.e2pdf.com') { |
| 134 |
update_option('e2pdf_api', 'api3.e2pdf.com'); |
| 135 |
} else { |
| 136 |
update_option('e2pdf_api', 'api.e2pdf.com'); |
| 137 |
} |
| 138 |
} |
| 139 |
if (empty($response)) { |
| 140 |
if ($this->get_api_server() === $this->get_api_server($api_server)) { |
| 141 |
$response['error'] = __('Something went wrong!', 'e2pdf'); |
| 142 |
} else { |
| 143 |
return $this->request($key, $this->get_api_server($api_server)); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if ($key) { |
| 149 |
if (isset($response[$key])) { |
| 150 |
return $response[$key]; |
| 151 |
} else { |
| 152 |
return false; |
| 153 |
} |
| 154 |
} else { |
| 155 |
return $response; |
| 156 |
} |
| 157 |
} |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
public function get_api_server($api_server = false) { |
| 162 |
if (!$api_server) { |
| 163 |
if (defined('E2PDF_API_SERVER')) { |
| 164 |
$api_server = E2PDF_API_SERVER; |
| 165 |
} else { |
| 166 |
$api_server = apply_filters('e2pdf_api', get_option('e2pdf_api', 'api.e2pdf.com')); |
| 167 |
} |
| 168 |
return $api_server; |
| 169 |
} |
| 170 |
switch ($api_server) { |
| 171 |
case 'api.e2pdf.com': |
| 172 |
$api_server = 'api2.e2pdf.com'; |
| 173 |
break; |
| 174 |
case 'api2.e2pdf.com': |
| 175 |
$api_server = 'api3.e2pdf.com'; |
| 176 |
break; |
| 177 |
case 'api3.e2pdf.com': |
| 178 |
$api_server = 'api4.e2pdf.com'; |
| 179 |
break; |
| 180 |
case 'api4.e2pdf.com': |
| 181 |
$api_server = 'api.e2pdf.com'; |
| 182 |
break; |
| 183 |
default: |
| 184 |
break; |
| 185 |
} |
| 186 |
return $api_server; |
| 187 |
} |
| 188 |
|
| 189 |
// flush |
| 190 |
public function flush() { |
| 191 |
$this->api = null; |
| 192 |
} |
| 193 |
|
| 194 |
// set |
| 195 |
public function set($key, $value = false) { |
| 196 |
if (!$this->api) { |
| 197 |
$this->api = new stdClass(); |
| 198 |
} |
| 199 |
if (is_array($key)) { |
| 200 |
foreach ($key as $attr => $value) { |
| 201 |
$this->api->$attr = $value; |
| 202 |
} |
| 203 |
} else { |
| 204 |
$this->api->$key = $value; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// get license |
| 209 |
public function get_license() { |
| 210 |
return get_option('e2pdf_license', false); |
| 211 |
} |
| 212 |
} |
| 213 |
|