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-api.php +153 -86 1.01.01 → 1.32.53 View file →
@@ -1,14 +1,12 @@
1 1 <?php
2 2
3 3 /**
4 - * E2pdf Api Model
5 - *
6 - * @copyright Copyright 2017 https://e2pdf.com
7 - * @license GPL v2
8 - * @version 1
9 - * @link https://e2pdf.com
10 - * @since 0.00.01
4 + * File: /model/e2pdf-api.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,37 +12,54 @@
14 12 }
15 13
16 14 class Model_E2pdf_Api extends Model_E2pdf_Model {
17 15
18 - protected $api = NULL;
16 + protected $api;
19 17
20 - const API_URL = 'https://api.e2pdf.com/';
21 - const DEFAULT_CURL_TIMEOUT = 300;
22 -
23 - function __construct() {
18 + public function __construct() {
24 19 parent::__construct();
20 + $this->api = new stdClass();
25 21 }
26 22
27 - /**
28 - * Submit API Request to Remote Server
29 - *
30 - * @param string $key - Return Value by Key
31 - *
32 - * @return mixed
33 - */
34 - public function request($key = false) {
35 - if ($this->api->action) {
23 + // request
24 + public function request($key = false, $api_server = false) {
36 25
37 - $data = array(
38 - 'api_url' => site_url(),
26 + $api_action = $this->get('action');
27 + if ($api_action) {
28 +
29 + // fix for upgrade via dashboard -> updates
30 + if (method_exists($this->helper, 'set_time_limit')) {
31 + $this->helper->set_time_limit((int) get_option('e2pdf_request_timeout', '420'));
32 + }
33 +
34 + // fix for upgrade via dashboard -> updates
35 + if ($api_action == 'update/info' && !method_exists($this->helper, 'get_site_url')) {
36 + return [];
37 + }
38 +
39 + $api_protocol = get_option('e2pdf_api_protocol', '0');
40 + $api_processor = get_option('e2pdf_debug', '0') && get_option('e2pdf_processor', '0') ? get_option('e2pdf_processor', '0') : '0';
41 + if ($api_processor == '2') {
42 + $api_version = '1.16.19';
43 + } else {
44 + $api_version = '1.32.52';
45 + }
46 +
47 + $data = [
48 + 'api_url' => $this->helper->get_site_url(),
39 49 'api_license_key' => $this->get_license(),
40 - 'api_processor' => get_option('e2pdf_processor') ? get_option('e2pdf_processor') : 0,
41 - );
42 - $request_url = self::API_URL . $this->api->action;
50 + 'api_processor' => apply_filters('e2pdf_api_processor', $api_processor),
51 + 'api_version' => apply_filters('e2pdf_api_version', $api_version),
52 + 'api_protocol' => $api_protocol,
53 + ];
54 + if (!$api_server) {
55 + $api_server = $this->get_api_server();
56 + }
57 + $request_url = 'https://' . $api_server . '/' . $api_action;
43 58
44 - $e2pdf_connection_timeout = get_option('e2pdf_connection_timeout') === false ? self::DEFAULT_CURL_TIMEOUT : get_option('e2pdf_connection_timeout');
45 -
46 - $ch = curl_init($request_url);
59 + // phpcs:disable WordPress.WP.AlternativeFunctions
60 + $ch = apply_filters('e2pdf_api_connection', curl_init($request_url));
61 + curl_setopt($ch, CURLOPT_USERAGENT, $this->helper->get_site_url());
47 62 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
48 63 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
49 64 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50 65 if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
@@ -49,49 +64,83 @@
49 64 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50 65 if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
51 66 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
52 67 }
53 - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $e2pdf_connection_timeout);
68 + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) get_option('e2pdf_connection_timeout', '300'));
54 69
55 - if (!empty($this->api->data)) {
56 - $data = array_merge($data, $this->api->data);
70 + if (defined('E2PDF_API_PROXY')) {
71 + curl_setopt($ch, CURLOPT_PROXY, E2PDF_API_PROXY);
57 72 }
73 + if (defined('E2PDF_API_PROXYPORT')) {
74 + curl_setopt($ch, CURLOPT_PROXYPORT, E2PDF_API_PROXYPORT);
75 + }
76 + if (defined('E2PDF_API_PROXYUSERPWD')) {
77 + curl_setopt($ch, CURLOPT_PROXYUSERPWD, E2PDF_API_PROXYUSERPWD);
78 + }
79 + if (defined('E2PDF_API_PROXYTYPE')) {
80 + curl_setopt($ch, CURLOPT_PROXYTYPE, E2PDF_API_PROXYTYPE);
81 + }
82 + if (defined('E2PDF_API_PROXYAUTH')) {
83 + curl_setopt($ch, CURLOPT_PROXYAUTH, E2PDF_API_PROXYAUTH);
84 + }
85 + if (class_exists('CURLFile')) {
86 + if (defined('CURLOPT_SAFE_UPLOAD')) {
87 + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
88 + }
89 + } else {
90 + if (defined('CURLOPT_SAFE_UPLOAD')) {
91 + curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
92 + }
93 + }
58 94
59 - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
95 + if (!empty($this->get('data'))) {
96 + $data = array_merge($data, $this->get('data'));
97 + }
98 + if ($api_action === 'template/upload2') {
99 + curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
100 + } else {
101 + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
102 + }
60 103
61 104 $json = curl_exec($ch);
62 105 $curl_errno = curl_errno($ch);
63 106 $curl_error = curl_error($ch);
64 - curl_close($ch);
107 + $curl_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
108 + $curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
109 + if (PHP_VERSION_ID < 80000) {
110 + curl_close($ch);
111 + }
112 + // phpcs:enable
65 113
66 114 if ($curl_errno > 0) {
67 - $response['error'] = "[{$curl_errno}] {$curl_error}";
115 + if ($this->get_api_server() === $this->get_api_server($api_server)) {
116 + $response['error'] = '[' . $curl_errno . '] ' . $curl_error;
117 + } else {
118 + return $this->request($key, $this->get_api_server($api_server));
119 + }
120 + } elseif ($curl_code < 200 || $curl_code >= 300) {
121 + if ($this->get_api_server() === $this->get_api_server($api_server)) {
122 + $response['error'] = '[' . $curl_code . ']';
123 + } else {
124 + return $this->request($key, $this->get_api_server($api_server));
125 + }
68 126 } else {
69 -
70 - if (($this->api->action === 'template/build' || $this->api->action === 'template/parse') && get_option('e2pdf_developer')) {
71 - $model_e2pdf_options = new Model_E2pdf_Options();
72 - if (in_array($this->helper->get_ip(), $model_e2pdf_options->get_option('e2pdf_developer_ips'))) {
73 - $data = array(
74 - 'request_url' => $request_url,
75 - 'data' => $data,
76 - 'response' => $json
77 - );
78 - var_dump("<PRE>");
79 - var_dump($data);
80 - die();
127 + if ($api_protocol == '1' && strpos($curl_type, 'application/octet-stream') !== false) {
128 + $response['file'] = $json;
129 + } else {
130 + $result = json_decode($json, true);
131 + $response = isset($result['response']) ? $result['response'] : null;
132 + if (!empty($response['file'])) {
133 + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
134 + $response['file'] = base64_decode($response['file']);
81 135 }
82 136 }
83 -
84 - $result = json_decode($json, true);
85 - $response = $result['response'];
86 -
87 -
88 - if (($this->api->action === 'common/activate' || $this->api->action === 'license/update') && isset($response['license_key'])) {
89 - update_option('e2pdf_license', $response['license_key']);
90 - }
91 -
92 137 if (empty($response)) {
93 - $response['error'] = __("Something went wrong!", "e2pdf");
138 + if ($this->get_api_server() === $this->get_api_server($api_server)) {
139 + $response['error'] = __('Something went wrong!', 'e2pdf');
140 + } else {
141 + return $this->request($key, $this->get_api_server($api_server));
142 + }
94 143 }
95 144 }
96 145
97 146 if ($key) {
@@ -100,8 +149,22 @@
100 149 } else {
101 150 return false;
102 151 }
103 152 } else {
153 + if ($api_action == 'template/build' || ($api_action == 'license/info' && $this->get('reload') === false)) {
154 + if (isset($response['error']) && $response['error'] == 'Site Url Not Found. Please try to "Reactivate" plugin.') {
155 + $license = $this->helper->load('license');
156 + if ($license && method_exists($license, 'reactivate') && $license->reactivate()) {
157 + return $this->request($key, $api_server);
158 + }
159 + }
160 + if (isset($response['error']) && $response['error'] == 'License Key does not match this site. Please correct License Key to continue usage.') {
161 + $license = $this->helper->load('license');
162 + if ($license && method_exists($license, 'restore') && $license->restore()) {
163 + return $this->request($key, $api_server);
164 + }
165 + }
166 + }
104 167 return $response;
105 168 }
106 169 }
107 170 return false;
@@ -106,25 +169,38 @@
106 169 }
107 170 return false;
108 171 }
109 172
110 - /**
111 - * Remove API Request options
112 - */
113 - public function flush() {
114 - $this->api = null;
173 + public function get_api_server($api_server = false) {
174 + if (!$api_server) {
175 + if (defined('E2PDF_API_SERVER')) {
176 + $api_server = E2PDF_API_SERVER;
177 + } else {
178 + $api_server = apply_filters('e2pdf_api', get_option('e2pdf_api', 'api.e2pdf.com'));
179 + }
180 + return $api_server;
181 + }
182 + switch ($api_server) {
183 + case 'api.e2pdf.com':
184 + $api_server = 'api2.e2pdf.com';
185 + break;
186 + case 'api2.e2pdf.com':
187 + $api_server = 'api3.e2pdf.com';
188 + break;
189 + case 'api3.e2pdf.com':
190 + $api_server = 'api4.e2pdf.com';
191 + break;
192 + case 'api4.e2pdf.com':
193 + $api_server = 'api.e2pdf.com';
194 + break;
195 + default:
196 + break;
197 + }
198 + return $api_server;
115 199 }
116 200
117 - /**
118 - * Set options for API Request
119 - *
120 - * @param string $key - Key
121 - * @param mixed $value - Value
122 - */
201 + // set
123 202 public function set($key, $value = false) {
124 - if (!$this->api) {
125 - $this->api = new stdClass();
126 - }
127 203 if (is_array($key)) {
128 204 foreach ($key as $attr => $value) {
129 205 $this->api->$attr = $value;
130 206 }
@@ -130,25 +206,16 @@
130 206 }
131 207 } else {
132 208 $this->api->$key = $value;
133 209 }
210 + return $this;
134 211 }
135 212
136 - /**
137 - * Get License
138 - *
139 - * @return string - License Key
140 - */
141 - public function get_license() {
142 - return get_option('e2pdf_license');
213 + public function get($key) {
214 + return property_exists($this->api, $key) ? $this->api->$key : null;
143 215 }
144 216
145 - /**
146 - * Get Domain
147 - *
148 - * @return string - Domain
149 - */
150 - public function get_domain() {
151 - return site_url();
217 + // get license
218 + public function get_license() {
219 + return get_option('e2pdf_license', false);
152 220 }
153 -
154 221 }