PluginProbe
Translation with DeepL API / 2.4.3.9
Translation with DeepL API v2.4.3.9
trunk 0.1.20180323 1.0 1.2.5.1 1.5.2.5 1.7.5 2.4.3.12 2.4.3.9 2.4.5
wpdeepl / client / deeplapi.class.php

deeplapi.class.php in Translation with DeepL API 2.4.3.9, at client/deeplapi.class.php

453 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class DeepLApi extends DeepLData {
4 protected $endPoint = '';
5 protected $http_mode = '';
6 private $endPointURL = false;
7 protected $authKey = '';
8 protected $log = false;
9 protected $headers = array();
10
11 public $languages;
12 public $doing_cron;
13 public $instance;
14 public $result;
15 public $why_no_cache;
16 public $final_request;
17 public $format;
18
19 protected $cache_dir = false;
20
21 protected $request = array();
22 protected $uniqid = false;
23 protected $cache_file = false;
24 protected $response = false;
25 public $cache_prefix = false;
26 protected $cache_id = false;
27
28 public $allow_cache = true;
29 public $response_type = 'fresh';
30 public $request_cache_file = '';
31 public $response_cache_file = '';
32
33 public $start_microtime = 0;
34 public $end_microtime = 0;
35 public $request_microtime = 0;
36
37 const TIMEOUT = 15;
38
39 public function __construct( $languages = array(), $log = false ) {
40 $this->authKey = DeepLConfiguration::getAPIKey();
41 //$this->request['auth_key'] = $auth_key;
42 $this->log = $log;
43
44 $this->languages = DeepLConfiguration::DefaultsAllLanguages();
45
46 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
47 $this->doing_cron = true;
48 }
49 else {
50 $this->doing_cron = false;
51 }
52
53 $this->instance = $this;
54
55 $this->cache_dir = $this->getCacheDirectory();
56 }
57
58 public function wasItCached() {
59 return $this->response_type === 'cache';
60 }
61
62 public function getTimeElapsed() {
63 return $this->request_microtime;
64 }
65
66 abstract public function buildBody( $mode, $endPoint );
67
68 public function getRequestUniqueID() {
69 if ( $this->cache_id ) {
70 $this->uniqid = $this->cache_id;
71 }
72 elseif ( !$this->uniqid ) {
73 $this->uniqid = microtime();
74 }
75 return $this->uniqid;
76 }
77
78 public function isValidRequest() {
79 return true;
80 }
81
82 /*
83 protected function maybeLimitRequestSize() {
84 }
85 */
86 protected function saveRequest() {
87 if ( isset( $this->request_cache_file ) ) {
88 file_put_contents( $this->request_cache_file, json_encode( $this->final_request ) );
89 }
90 }
91
92 protected function addHeaders( $headers ) {
93 return $headers;
94 }
95
96 protected function doRequest( $mode = false ) {
97 if( !$mode )
98 $mode = $this->http_mode;
99
100
101 if ( $mode == 'GET' ) {
102 $response = $this->doGETRequest();
103 }
104 elseif( $mode == 'DELETE' ) {
105 $response = $this->doGETRequest( 'DELETE' );
106 }
107 elseif( $mode == 'PUT' ) {
108 $response = $this->doGETRequest( 'PUT' );
109 }
110 else {
111 $response = $this->doPOSTRequest();
112 }
113
114 return $response;
115 }
116
117 static function getInstance() {
118 return self::instance;
119 }
120
121 protected function setCacheID( $idstring ) {
122 $this->cache_id = $idstring;
123 }
124
125 public function setCachePrefix( $prefix = '' ) {
126 if ( !empty( $prefix ) ) {
127 $this->cache_prefix = $prefix;
128 }
129 else {
130 $this->cache_prefix = '';
131 }
132 }
133
134 public function getCacheFile( $type ) {
135 if ( $type == 'response' ) {
136 return $this->response_cache_file;
137 }
138 elseif ( $type == 'request' ) {
139 return $this->request_cache_file;
140 }
141 }
142
143 public function setCacheNames() {
144 $cache_name = '';
145 if ( !empty( $this->cache_prefix ) ) {
146 $cache_name .= $this->cache_prefix .'-';
147 }
148 $cache_name .= $this->cache_id;
149 $this->request_cache_file = trailingslashit( $this->cache_dir ) . $cache_name . '-request';
150 $this->response_cache_file = trailingslashit( $this->cache_dir ) . $cache_name . '-response';
151 }
152
153 public function request() {
154
155 $log_bits = array('class' => get_class($this));
156
157 if ( $this->allow_cache )
158 $this->uniqid = $this->getRequestUniqueID();
159
160 $log_bits['uniqid'] = $this->uniqid;
161
162 $this->setCacheNames();
163 $this->result = false;
164
165 if ( $this->isCacheValid( $this->response_cache_file ) ) {
166 $this->response_type = 'cache';
167 $this->why_no_cache = false;
168 $this->result = file_get_contents( $this->response_cache_file );
169 $log_bits['type'] = 'cached';
170
171 }
172 else {
173 $log_bits['type'] = 'fresh';
174 $log_bits['mode'] = $this->http_mode;
175 $this->start_microtime = microtime( true );
176 //echo "\nIS FRESH";
177 $this->response_type = 'fresh';
178
179
180 if ( $this->http_mode == 'GET' ) {
181 $response = $this->doRequest( 'GET' );
182 }
183 else {
184 $response = $this->doRequest( 'POST' );
185 }
186
187
188 $this->end_microtime = microtime( true );
189 $this->request_microtime = $this->end_microtime - $this->start_microtime;
190
191 if ( is_wp_error( $response ) ) {
192 return $response->get_error_message();
193 }
194
195 if ( $response['response']['code'] > 299 ) {
196 $log_bits['response_error'] = $response['response']['code'];
197 $log_bits['response'] = $response['response'];
198 if ( $response['response']['code'] == 429 ) {
199 $log_bits['error_cause'] = 'Excessive request size';
200 }
201 if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
202
203 $body = json_decode( $response['body'], true );
204 $message = isset( $body['message'] ) ? $body['message'] : false;
205 return new WP_Error( $message );
206 }
207
208 if ( array_key_exists( 'body', $response ) && strlen( $response['body'] ) )
209 $this->result = $response['body'];
210 else
211 $this->result = false;
212
213 if ( $this->result ) {
214 //echo " putting " . strlen( serialize( $this->result ) ) ." in "
215 try {
216 file_put_contents( $this->response_cache_file, $this->result );
217 } catch (Exception $e) {
218
219 }
220 }
221
222 $log_bits['request_microtime'] = $this->request_microtime;
223 }
224
225 if ( DeepLConfiguration::getLogLevel() ) {
226 if ( DeepLConfiguration::getLogLevel() > 1 ) {
227 foreach( $this->request as $key => $value ) {
228 if( is_string( $value ) && strlen( $value ) > 500 )
229 $log_bits['args'][$key] = "LEN=" . strlen( $value) .", string = " . substr( $value, 0, 100 );
230 else
231 $log_bits['args'][$key] = $value;
232
233 }
234 }
235 else {
236 foreach ( array ('source_lang', 'target_lang') as $key ) {
237 if ( isset( $this->request[$key] ) ) {
238 $log_bits[$key] = $this->request[$key];
239 }
240 }
241 }
242 if ( isset( $this->request['text'] ) ) {
243 $text_size = 0;
244 foreach ( $this->request['text'] as $i => $string ) {
245 $text_size += strlen($string);
246 }
247 $log_bits['text_size'] = $text_size;
248 }
249 }
250
251 if ($this->response_type == 'fresh') {
252 // test if JSON
253 json_decode($this->result);
254 if (json_last_error() == JSON_ERROR_NONE) {
255 $log_bits['response'] = json_decode($this->result, 1 );
256 if ( DeepLConfiguration::getLogLevel() ) {
257 if (isset( $log_bits['response']['translations'] ) ) {
258 $translated_text_size = 0;
259 foreach ( $log_bits['response']['translations'] as $i => $translation ) {
260 $translated_text_size += strlen( $translation['text'] );
261 }
262 $log_bits['translated_text_size'] = $translated_text_size;
263 }
264 }
265 }
266 else {
267 $log_bits['response'] = $this->result;
268 }
269
270 }
271 else {
272 $log_bits['response'] = '--hidden response because cached request--';
273
274 }
275 $log_bits['response_length'] = strlen($this->result);
276
277 if ( $this->result ) {
278 $this->result = json_decode( $this->result );
279
280 $this->result = ( array ) $this->result;
281 $this->result['response_type'] = $this->response_type;
282 if ( $this->why_no_cache ) {
283 $log_bits['why_no_cache'] = $this->why_no_cache;
284 $this->result['why_no_cache'] = $this->why_no_cache;
285 }
286 if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
287 return $this->result;
288 }
289 else {
290 if ( DeepLConfiguration::getLogLevel() ) wpdeepl_log($log_bits, "operation");
291 return $this->response['response']['message'];
292 }
293 }
294
295 public function getRequestTime( $result_in_milliseconds = false ) {
296 if ( !$this->request_microtime ) {
297 return false;
298 }
299
300 if ( $result_in_milliseconds ) {
301 return floatval( $this->request_microtime );
302 }
303 else {
304 return floatval( $this->request_microtime/1000 );
305 }
306 }
307
308 public function getEndPointURL( $add_auth_key = false ) {
309
310
311 $APIServer = DeeplConfiguration::getAPIServer();
312 if ( !$this->endPointURL ) {
313 if ( $this->endPoint === '' ) {
314 $this->endPointURL = $APIServer;
315 }
316 else {
317 $this->endPointURL = trailingslashit( $APIServer ) . $this->endPoint;
318 }
319 }
320
321 if ( $add_auth_key ) {
322 $this->endPointURL .= '?auth_key=' . $this->authKey;
323 }
324
325 return $this->endPointURL;
326 }
327 /*
328 * send a POST request
329 *
330 * @since 0.1
331 */
332 public function doPOSTRequest( $args = array() ) {
333
334
335 $args = array(
336 'method' => 'POST',
337 'timeout' => self::TIMEOUT,
338 );
339
340 $args['headers'] = array(
341 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey
342 );
343
344 $args['headers'] = $this->addHeaders( $args['headers'] );
345 $args['body'] = $this->buildBody( 'POST', $this->endPoint );
346
347 //plouf( $args ,__METHOD__ . "args");
348
349 /*
350 $args['auth_key'] = $this->authKey;
351
352 $request = array();
353 if ( $this->request ) {
354 foreach ( $this->request as $key => $value ) {
355 $request[$key] = $value;
356 }
357 }
358 $args['query'] = http_build_query( $request );
359 $args['timeout'] =
360 */
361 //$args['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
362 // $this->headers['Content-Length'] = strlen( $this->request['text'] );
363
364
365
366 $add_auth_key = false;
367 $remoteURL = $this->getEndPointURL( $add_auth_key );
368
369 $bits = array_merge( array( $remoteURL, 'POST' ), $args );
370 // unsafe
371 //wpdeepl_log( $bits, 'apiRequests');
372 // plouf( $args, "args" );
373 /*
374 $exec = "curl -X POST '$remoteURL' \\\n";
375 foreach( $args['headers'] as $key => $value ) {
376 $exec .= "-H '$key: $value' \\\n";
377 }
378 foreach( $args['body'] as $key => $value ) {
379 $exec .= "-d '$value' \\\n";
380 }
381
382 plouf( $exec );
383 $response = exec ( $exec );
384 plouf( $response );
385 die('okaz4a6e4a68e4a86e');
386 */
387
388 //plouf( $args, $remoteURL ); die('68z4e6z48a68ee8aok');
389
390 //$args['body'] = str_replace('target_lang=NO', 'target_lang=NB', $args['body'] );
391 $response = wp_remote_post( $remoteURL, $args );
392 //plouf( $args, $remoteURL, " post request"); plouf( $response ); die('okesoezporjkezor');
393
394 // plouf( $args, " args pour $remoteURL" ); plouf( $response, "zeirjzpeijrpizerjpirj" ); die('okaze6aze44e');
395 //plouf( $args, "args");
396
397 // plouf( $args, "POST request to $remoteURL ");die('okaz5ea5zea4e');
398
399 if ( is_wp_error( $response ) ) {
400 $this->response = $response->get_error_message();
401 plouf( $args, $this->getEndPointURL() );
402 die( __METHOD__ );
403 }
404 else {
405 $this->response = $response;
406 }
407 return $response;
408 }
409
410 /*
411 * send a GET request
412 *
413 * @since 0.1
414 */
415 public function doGETRequest( $alt_mode = false ) {
416
417 $method = 'GET';
418 if( in_array( $alt_mode, array('PUT', 'DELETE' ) ) )
419 $method = $alt_mode;
420 $args = array(
421 'method' => $method,
422 'timeout' => self::TIMEOUT,
423 );
424
425 $args['headers'] = array(
426 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey
427 );
428 $args['headers'] = $this->addHeaders( $args['headers'] );
429
430 $remoteURL = $this->getEndPointURL();
431 //if( $args['query'] ) $remoteURL .= '?' . $args['query'];
432 //plouf( $args, "REQUESTING URL = '$url'" ); die( '6ze846az6e646eok' );
433
434 $curl_string = 'curl -X ' . $args['method'] . " '$remoteURL'\n";
435 foreach( $args['headers'] as $key => $value ) {
436 $curl_string .= "$key: $value\n";
437 }
438 //echo "\n CURL = " . str_replace("\n", '<br />', $curl_string );
439 $bits = array_merge( array( $remoteURL, 'GET' ), $args );
440 // unsafe
441 //wpdeepl_log( $bits, 'apiRequests');
442
443 // plouf( $args, $remoteURL ); die('oazea4ze9684e84azek');
444
445 $response = wp_remote_get( $remoteURL, $args );
446 if ( is_wp_error( $response ) ) {
447 $this->response = $response->get_error_message();
448 } else {
449 $this->response = $response;
450 }
451 return $response;
452 }
453 }