1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
<?php defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Imagify.io API for WordPress. */ class Imagify { use \Imagify\Traits\InstanceGetterTrait;
/** * The Imagify API endpoint. * * @var string */ const API_ENDPOINT = 'https://app.imagify.io/api/';
/** * The Imagify API key. * * @var string */ private $api_key = '';
/** * Random key used to store the API key in the request args. * * @var string */ private $secure_key = '';
/** * HTTP headers. Each http call must fill it (even if it's with an empty array). * * @var array */ private $headers = [];
/** * All (default) HTTP headers. They must not be modified once the class is instanciated, or it will affect any following HTTP calls. * * @var array */ private $all_headers = [];
/** * Filesystem object. * * @var object Imagify_Filesystem * @since 1.7.1 * @access protected * @author Grégory Viguier */ protected $filesystem;
/** * Use data fetched from the API. * * @var \stdClass|\WP_Error * @since 1.9.9 * @access protected * @author Grégory Viguier */ protected static $user;
/** * The constructor. */ protected function __construct() { if ( ! class_exists( 'Imagify_Filesystem' ) ) { // Dirty patch used when updating from 1.7. include_once IMAGIFY_PATH . 'inc/classes/class-imagify-filesystem.php'; }
$this->api_key = get_imagify_option( 'api_key' ); $this->secure_key = $this->generate_secure_key(); $this->filesystem = Imagify_Filesystem::get_instance();
$this->all_headers['Accept'] = 'Accept: application/json'; $this->all_headers['Content-Type'] = 'Content-Type: application/json'; $this->all_headers['Authorization'] = 'Authorization: token ' . $this->api_key; }
/** * Get your Imagify account infos. * * @access public * @since 1.6.5 * * @return object */ public function get_user() { global $wp_current_filter;
if ( isset( static::$user ) ) { return static::$user; }
if ( in_array( 'upgrader_post_install', (array) $wp_current_filter, true ) ) { // Dirty patch used when updating from 1.7. static::$user = new WP_Error(); return static::$user; }
$this->headers = $this->all_headers; static::$user = $this->http_call( 'users/me/', [ 'timeout' => 10 ] );
if ( is_wp_error( static::$user ) ) { return static::$user; }
$maybe_missing = [ 'account_type' => 'free', 'quota' => 0, 'extra_quota' => 0, 'extra_quota_consumed' => 0, 'consumed_current_month_quota' => 0, ];
foreach ( $maybe_missing as $name => $value ) { if ( ! isset( static::$user->$name ) ) { static::$user->$name = $value; } }
return static::$user; }
/** * Create a user on your Imagify account. * * @access public * @since 1.6.5 * * @param array $data All user data. * @return object */ public function create_user( $data ) { $this->headers = []; $data = array_merge( $data, [ 'from_plugin' => true, 'partner' => imagify_get_partner(), ] );
if ( ! $data['partner'] ) { unset( $data['partner'] ); }
$response = $this->http_call( 'users/', [ 'method' => 'POST', 'post_data' => $data, ] );
if ( ! is_wp_error( $response ) ) { imagify_delete_partner(); }
return $response; }
/** * Update an existing user on your Imagify account. * * @access public * @since 1.6.5 * * @param string $data All user data. * @return object */ public function update_user( $data ) { $this->headers = $this->all_headers;
return $this->http_call( 'users/me/', [ 'method' => 'PUT', 'post_data' => $data, 'timeout' => 10, ] ); }
/** * Check your Imagify API key status. * * @access public * @since 1.6.5 * * @param string $data The license key. * @return object */ public function get_status( $data ) { static $status = [];
if ( isset( $status[ $data ] ) ) { return $status[ $data ]; }
$this->headers = [ 'Authorization' => 'Authorization: token ' . $data, ];
$uri = 'status/'; $partner = imagify_get_partner();
if ( $partner ) { $uri .= '?partner=' . $partner; }
$status[ $data ] = $this->http_call( $uri, [ 'timeout' => 10 ] );
return $status[ $data ]; }
/** * Get the Imagify API version. * * @access public * @since 1.6.5 * * @return object */ public function get_api_version() { static $api_version;
if ( ! isset( $api_version ) ) { $this->headers = [ 'Authorization' => $this->all_headers['Authorization'], ];
$api_version = $this->http_call( 'version/', [ 'timeout' => 5 ] ); }
return $api_version; }
/** * Get Public Info. * * @access public * @since 1.6.5 * * @return object */ public function get_public_info() { $this->headers = $this->all_headers;
return $this->http_call( 'public-info' ); }
/** * Optimize an image from its binary content. * * @access public * @since 1.6.5 * @since 1.6.7 $data['image'] can contain the file path (prefered) or the result of `curl_file_create()`. * * @param string $data All options. * @return object */ public function upload_image( $data ) { $this->headers = [ 'Authorization' => $this->all_headers['Authorization'], ];
return $this->http_call( 'upload/', [ 'method' => 'POST', 'post_data' => $data, ] ); }
/** * Optimize an image from its URL. * * @access public * @since 1.6.5 * * @param string $data All options. Details here: --. * @return object */ public function fetch_image( $data ) { $this->headers = $this->all_headers;
return $this->http_call( 'fetch/', [ 'method' => 'POST', 'post_data' => wp_json_encode( $data ), ] ); }
/** * Get prices for plans. * * @access public * @since 1.6.5 * * @return object */ public function get_plans_prices() { $this->headers = $this->all_headers;
return $this->http_call( 'pricing/plan/' ); }
/** * Get prices for packs (One Time). * * @access public * @since 1.6.5 * * @return object */ public function get_packs_prices() { $this->headers = $this->all_headers;
return $this->http_call( 'pricing/pack/' ); }
/** * Get all prices (packs & plans included). * * @access public * @since 1.6.5 * * @return object */ public function get_all_prices() { $this->headers = $this->all_headers;
return $this->http_call( 'pricing/all/' ); }
/** * Get all prices (packs & plans included). * * @access public * @since 1.6.5 * * @param string $coupon A coupon code. * @return object */ public function check_coupon_code( $coupon ) { $this->headers = $this->all_headers;
return $this->http_call( 'coupons/' . $coupon . '/' ); }
/** * Get information about current discount. * * @access public * @since 1.6.5 * * @return object */ public function check_discount() { $this->headers = $this->all_headers;
return $this->http_call( 'pricing/discount/' ); }
/** * Make an HTTP call using curl. * * @access private * @since 1.6.5 * @since 1.6.7 Use `wp_remote_request()` when possible (when we don't need to send an image). * * @param string $url The URL to call. * @param array $args The request args. * @return object */ private function http_call( $url, $args = [] ) { $args = array_merge( [ 'method' => 'GET', 'post_data' => null, 'timeout' => 45, ], $args );
$endpoint = trim( $url, '/' ); /** * Filter the timeout value for any request to the API. * * @since 1.6.7 * @author Grégory Viguier * * @param int $timeout Timeout value in seconds. * @param string $endpoint The targetted endpoint. It's basically URI without heading nor trailing slash. */ $args['timeout'] = apply_filters( 'imagify_api_http_request_timeout', $args['timeout'], $endpoint );
// We need to send an image: we must use cURL directly. if ( isset( $args['post_data']['image'] ) ) { return $this->curl_http_call( $url, $args ); }
$args = array_merge( [ 'headers' => [], 'body' => $args['post_data'], 'sslverify' => apply_filters( 'https_ssl_verify', false ), ], $args );
unset( $args['post_data'] );
if ( $this->headers ) { foreach ( $this->headers as $name => $value ) { $value = explode( ':', $value, 2 ); $value = end( $value );
$args['headers'][ $name ] = trim( $value ); } }
if ( ! empty( $args['headers']['Authorization'] ) ) { // Make sure our API has not overwritten by some other plugin. $args[ $this->secure_key ] = preg_replace( '/^token /', '', $args['headers']['Authorization'] );
if ( ! has_filter( 'http_request_args', [ $this, 'force_api_key_header' ] ) ) { add_filter( 'http_request_args', [ $this, 'force_api_key_header' ], IMAGIFY_INT_MAX + 25, 2 ); } }
$response = wp_remote_request( self::API_ENDPOINT . $url, $args );
if ( is_wp_error( $response ) ) { return $response; }
$http_code = wp_remote_retrieve_response_code( $response ); $response = wp_remote_retrieve_body( $response );
return $this->handle_response( $response, $http_code ); }
/** * Make an HTTP call using curl. * * @access private * @since 1.6.7 * @throws Exception When curl_init() fails. * @author Grégory Viguier * * @param string $url The URL to call. * @param array $args The request arguments. * @return object */ private function curl_http_call( $url, $args = [] ) { // Check if curl is available. if ( ! Imagify_Requirements::supports_curl() ) { return new WP_Error( 'curl', 'cURL isn\'t installed on the server.' ); }
try { $url = self::API_ENDPOINT . $url; $ch = @curl_init();
if ( ! is_resource( $ch ) ) { throw new Exception( 'Could not initialize a new cURL handle' ); }
if ( isset( $args['post_data']['image'] ) && is_string( $args['post_data']['image'] ) && $this->filesystem->exists( $args['post_data']['image'] ) ) { $args['post_data']['image'] = curl_file_create( $args['post_data']['image'] ); }
// Handle proxies. $proxy = new WP_HTTP_Proxy();
if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { curl_setopt( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP ); curl_setopt( $ch, CURLOPT_PROXY, $proxy->host() ); curl_setopt( $ch, CURLOPT_PROXYPORT, $proxy->port() );
if ( $proxy->use_authentication() ) { curl_setopt( $ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $proxy->authentication() ); } }
if ( 'POST' === $args['method'] ) { curl_setopt( $ch, CURLOPT_POST, true ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); } elseif ( 'PUT' === $args['method'] ) { curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); }
if ( defined( 'CURLOPT_PROTOCOLS' ) ) { curl_setopt( $ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS ); }
$user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) );
curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_HEADER, false ); curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->headers ); curl_setopt( $ch, CURLOPT_TIMEOUT, $args['timeout'] ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $args['timeout'] ); curl_setopt( $ch, CURLOPT_USERAGENT, $user_agent ); @curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false ); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
/** * Tell which http version to use with cURL during image optimization. * * @since 1.8.4.1 * @since 1.9.9 Default value is `false`. * @author Grégory Viguier * * @param $use_version_1_0 bool True to use version 1.0. False for 1.1. Default is false. */ if ( apply_filters( 'imagify_curl_http_version_1_0', false ) ) { curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 ); } else { curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); }
$response = curl_exec( $ch ); $error = curl_error( $ch ); $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch ); } catch ( Exception $e ) { $args['headers'] = $this->headers; /** * Fires after a failed curl request. * * @since 1.6.9 * @author Grégory Viguier * * @param string $url The requested URL. * @param array $args The request arguments. * @param object $e The raised Exception. */ do_action( 'imagify_curl_http_response', $url, $args, $e );
return new WP_Error( 'curl', 'An error occurred (' . $e->getMessage() . ')' ); } // End try().
$args['headers'] = $this->headers;
/** * Fires after a successful curl request. * * @since 1.6.9 * @author Grégory Viguier * * @param string $url The requested URL. * @param array $args The request arguments. * @param string $response The request response. * @param int $http_code The request HTTP code. * @param string $error An error message. */ do_action( 'imagify_curl_http_response', $url, $args, $response, $http_code, $error );
return $this->handle_response( $response, $http_code, $error ); }
/** * Handle the request response and maybe trigger an error. * * @access private * @since 1.6.7 * @author Grégory Viguier * * @param string $response The request response. * @param int $http_code The request HTTP code. * @param string $error An error message. * @return object */ private function handle_response( $response, $http_code, $error = '' ) { $response = json_decode( $response );
if ( 401 === $http_code ) { // Reset the API validity cache if the API key is not valid. Imagify_Requirements::reset_cache( 'api_key_valid' ); }
if ( 200 !== $http_code && ! empty( $response->code ) ) { if ( ! empty( $response->detail ) ) { return new WP_Error( 'error ' . $http_code, $response->detail ); } if ( ! empty( $response->image ) ) { $error = (array) $response->image; $error = reset( $error ); return new WP_Error( 'error ' . $http_code, $error ); } }
if ( 413 === $http_code ) { return new WP_Error( 'error ' . $http_code, 'Your image is too big to be uploaded on our server.' ); }
if ( 200 !== $http_code ) { $error = trim( (string) $error ); $error = '' !== $error ? ' - ' . htmlentities( $error ) : ''; return new WP_Error( 'error ' . $http_code, "Our server returned an error ({$http_code}{$error})" ); }
if ( ! is_object( $response ) ) { return new WP_Error( 'invalid response', 'Our server returned an invalid response.', $response ); }
return $response; }
/** * Generate a random key. * Similar to wp_generate_password() but without filter. * * @access private * @since 1.8.4 * @see wp_generate_password() * @author Grégory Viguier * * @return string */ private function generate_secure_key() { $length = wp_rand( 12, 20 ); $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|'; $password = '';
for ( $i = 0; $i < $length; $i++ ) { $password .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 ); }
return $password; }
/** * Filter the arguments used in an HTTP request, to make sure our API key has not been overwritten by some other plugin. * * @access public * @since 1.8.4 * @author Grégory Viguier * * @param array $args An array of HTTP request arguments. * @param string $url The request URL. * @return array */ public function force_api_key_header( $args, $url ) { if ( strpos( $url, self::API_ENDPOINT ) === false ) { return $args; }
if ( ! empty( $args['headers']['Authorization'] ) || ! empty( $args[ $this->secure_key ] ) ) { if ( ! empty( $args[ $this->secure_key ] ) ) { $args['headers']['Authorization'] = 'token ' . $args[ $this->secure_key ]; } else { $args['headers']['Authorization'] = 'token ' . $this->api_key; } }
return $args; } }
|