PluginProbe
RabbitLoader / 4.0.2
RabbitLoader v4.0.2
4.0.2 4.0.1 4.0 3.2.0 3.1.1 3.1.0 3.0.5 3.0.3 3.0.4 2.17.1 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.7 2.18.0 2.18.1 2.18.2 2.18.3 2.18.4 2.18.5 2.18.6 2.18.7 2.18.8 All 129 releases
rabbit-loader / inc / RabbitLoader / SDK / API.php

API.php in RabbitLoader 4.0.2, at inc/RabbitLoader/SDK/API.php

256 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace RabbitLoader\SDK;
5
6 class API
7 {
8
9 private $hostV1 = '';
10 private $hostV2 = '';
11 private $licenseKey = '';
12 private $platform = [];
13 private $debug = false;
14
15 public function __construct($licenseKey, $platform)
16 {
17 $this->hostV1 = 'https://api-v1.rabbitloader.com/';
18 $this->hostV2 = 'https://api-v2.rabbitloader.com/';
19 if (!empty($_ENV['RL_PHP_SDK_HOST'])) {
20 $this->hostV1 = $_ENV['RL_PHP_SDK_HOST'];
21 $this->hostV2 = $_ENV['RL_PHP_SDK_HOST'];
22 }
23 $this->licenseKey = $licenseKey;
24 $this->platform = $platform;
25 }
26
27 /**
28 * @param bool $debug
29 */
30 public function setDebug($debug)
31 {
32 $this->debug = $debug;
33 }
34
35 public function refreshV1(Cache $cf, $url, $force)
36 {
37 if ($this->debug) {
38 Util::sendHeader('x-rl-refresh: start', true);
39 }
40 $response = [
41 'url' => $url
42 ];
43 $httpCode = 0;
44 try {
45 if (!$cf->exists(Cache::TTL_SHORT)) {
46 $response['short_missing'] = true;
47 return;
48 }
49 if ($cf->exists(Cache::TTL_LONG) && !$force) {
50 $response['long_found'] = true;
51 return;
52 }
53 $headers = json_decode($cf->get(Cache::TTL_SHORT, 'h'), true);
54 if (empty($headers)) {
55 return;
56 }
57 $html = $cf->get(Cache::TTL_SHORT, 'c');
58 if (empty($html)) {
59 return;
60 }
61 $fields = [
62 'url_b64' => base64_encode($url),
63 'html' => $html,
64 'headers' => $headers,
65 'plugins' => []
66 ];
67 if (WordPress::isWp()) {
68 $fields['plugins'] = WordPress::plugins();
69 }
70 $this->remoteV1('page/get_cache', $fields, $result, $httpCode);
71 if (!empty($result['data']['html'])) {
72 $response['saved'] = $cf->save(Cache::TTL_LONG, $result['data']['html'], $result['data']['headers']);
73 $response['deleted'] = $cf->delete(Cache::TTL_SHORT);
74 } else {
75 $response = $result;
76 }
77 } catch (\Throwable $e) {
78 Exc::catch($e);
79 }
80 if ($this->debug) {
81 Util::sendHeader('x-rl-refresh: finish', true);
82 Util::sendHeader('x-rl-httpCode: ' . $httpCode, true);
83 }
84 return $response;
85 }
86
87 private function remoteV1($endpoint, &$fields, &$result, &$httpCode)
88 {
89 $ignoreSSL = true;
90 $url = $this->hostV1 . 'api/v1/' . $endpoint;
91 $fields_string = http_build_query($fields);
92
93 $ch = curl_init();
94 curl_setopt($ch, CURLOPT_URL, $url);
95 curl_setopt($ch, CURLOPT_POST, true);
96 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
97 curl_setopt($ch, CURLOPT_HTTPHEADER, [
98 'Authorization: Bearer ' . $this->licenseKey,
99 ]);
100 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
101 curl_setopt($ch, CURLOPT_HEADER, 0);
102 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
103 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
104 curl_setopt($ch, CURLOPT_ENCODING, ''); //Curl automatically sends the appropriate header based on the supported algorithms, and automatically decodes the response.
105
106 if ($ignoreSSL) {
107 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
108 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
109 }
110 $httpCode = 0;
111 $response = curl_exec($ch);
112 $curlError = curl_error($ch);
113 if (!empty($curlError)) {
114 if ($this->debug) {
115 echo $curlError;
116 }
117 return;
118 }
119 $httpCode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
120 curl_close($ch);
121 $result = json_decode($response, true);
122 if ($result === null && $this->debug) {
123 echo "Failed to decode JSON $response";
124 }
125 return true;
126 }
127
128 public function heartbeat()
129 {
130 try {
131 $data = $this->platform;
132 $data += [
133 'error_log' => Exc::getAndClean(),
134 'cdn_loop' => empty($_SERVER['HTTP_CDN_LOOP']) ? '' : $_SERVER['HTTP_CDN_LOOP'],
135 'server_addr' => empty($_SERVER['SERVER_ADDR']) ? '' : $_SERVER['SERVER_ADDR'],
136 ];
137
138 if (empty($data['cdn_loop']) && !empty($_SERVER['HTTP_INCAP_CLIENT_IP'])) {
139 $data['cdn_loop'] = 'incap';
140 }
141 if (empty($data['server_addr']) && !empty($_SERVER['LOCAL_ADDR'])) {
142 $data['server_addr'] = $_SERVER['LOCAL_ADDR'];
143 }
144 $this->remoteV1('domain/heartbeat', $data, $result, $httpCode);
145 Util::sendHeader('x-rl-hb-code: ' . $httpCode, true);
146 } catch (\Throwable $e) {
147 Exc::catch($e);
148 Util::sendHeader('x-rl-hb-thro: ' . $e->getMessage() . ':' . $e->getLine(), true);
149 } catch (\Exception $e) {
150 Exc::catch($e);
151 Util::sendHeader('x-rl-hb-exc: ' . $e->getMessage() . ':' . $e->getLine(), true);
152 }
153 }
154
155
156 public function refreshV2(Cache $cf, $url, $force)
157 {
158 if ($this->debug) {
159 Util::sendHeader('x-rl-refresh: start', true);
160 }
161 $response = [
162 'url' => $url
163 ];
164 $httpCode = 0;
165 try {
166 if (!$cf->exists(Cache::TTL_SHORT)) {
167 $response['short_missing'] = true;
168 return;
169 }
170 if ($cf->exists(Cache::TTL_LONG) && !$force) {
171 $response['long_found'] = true;
172 return;
173 }
174 $headers = json_decode($cf->get(Cache::TTL_SHORT, 'h'), true);
175 if (empty($headers)) {
176 return;
177 }
178 $html = $cf->get(Cache::TTL_SHORT, 'c');
179 if (empty($html)) {
180 return;
181 }
182 $fields = [
183 'url' => $url,
184 'urlBase64' => base64_encode($url),
185 'html' => $html,
186 'headers' => $headers,
187 'plugins' => []
188 ];
189 if (WordPress::isWp()) {
190 $fields['plugins'] = WordPress::plugins();
191 }
192 $errorMessages = '';
193 $this->remoteV2("url/domain/defaultdid/page/optimize", $fields, $result, $httpCode, $errorMessages);
194 if (empty($result['error']) && !empty($result['html'])) {
195 $response['saved'] = $cf->save(Cache::TTL_LONG, $result['html'], $result['headers']);
196 $response['deleted'] = $cf->delete(Cache::TTL_SHORT);
197 } else {
198 $response = $result;
199 Util::sendHeader('x-rl-ble: ' . $result['error'], true);
200 }
201 Util::sendHeader('x-rl-last-error: ' . $errorMessages, true);
202 } catch (\Throwable $e) {
203 Exc::catch($e);
204 }
205 if ($this->debug) {
206 Util::sendHeader('x-rl-refresh: finish', true);
207 Util::sendHeader('x-rl-httpCode: ' . $httpCode, true);
208 }
209 return $response;
210 }
211
212 private function remoteV2($endpoint, &$fields, &$result, &$httpCode, &$errMsg)
213 {
214 $ignoreSSL = true;
215 $url = $this->hostV2 . $endpoint;
216 $encodedDate = json_encode($fields);
217 if (!$encodedDate) {
218 $errMsg = json_last_error_msg();
219 return;
220 }
221 $ch = curl_init();
222 curl_setopt($ch, CURLOPT_URL, $url);
223 curl_setopt($ch, CURLOPT_POST, true);
224 curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedDate);
225 curl_setopt($ch, CURLOPT_HTTPHEADER, [
226 'Authorization: Bearer ' . $this->licenseKey,
227 'Content-Type: application/json',
228 'Accept: application/json',
229 ]);
230 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
231 curl_setopt($ch, CURLOPT_HEADER, 0);
232 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
233 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
234 curl_setopt($ch, CURLOPT_ENCODING, ''); //Curl automatically sends the appropriate header based on the supported algorithms, and automatically decodes the response.
235
236 if ($ignoreSSL) {
237 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
238 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
239 }
240 $httpCode = 0;
241 $response = curl_exec($ch);
242 $curlError = curl_error($ch);
243 if (!empty($curlError)) {
244 $errMsg = $curlError;
245 return;
246 }
247 $httpCode = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
248 curl_close($ch);
249 $result = json_decode($response, true);
250 if ($result === null) {
251 $errMsg = json_last_error_msg();
252 }
253 return true;
254 }
255 }
256