Skip to content

Commit 96ff9d5

Browse files
author
Jacob Middag
committed
Add message to status check, add debug option for cURL and check ajax referer
1 parent 1a535f2 commit 96ff9d5

16 files changed

Lines changed: 104 additions & 77 deletions

src/class-tiny-compress-curl.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class Tiny_Compress_Curl extends Tiny_Compress {
2222
protected function shrink_options($input) {
23-
return array(
23+
$options = array(
2424
CURLOPT_URL => Tiny_Config::URL,
2525
CURLOPT_USERPWD => 'api:' . $this->api_key,
2626
CURLOPT_POSTFIELDS => $input,
@@ -31,6 +31,14 @@ protected function shrink_options($input) {
3131
CURLOPT_SSL_VERIFYPEER => true,
3232
CURLOPT_USERAGENT => Tiny_WP_Base::plugin_identification() . ' cURL'
3333
);
34+
if (TINY_DEBUG) {
35+
$f = fopen(dirname(__FILE__) . '/curl.log', 'w');
36+
if (is_resource($f)) {
37+
$options[CURLOPT_VERBOSE] = true;
38+
$options[CURLOPT_STDERR] = $f;
39+
}
40+
}
41+
return $options;
3442
}
3543

3644
protected function shrink($input) {
@@ -42,7 +50,7 @@ protected function shrink($input) {
4250
if ($response === false) {
4351
return array(array(
4452
'error' => 'CurlError',
45-
'message' => curl_error($request)
53+
'message' => sprintf("%s [%d]", curl_error($request), curl_errno($request))
4654
), null
4755
);
4856
}

src/class-tiny-compress-fopen.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function output($url) {
8383
return $response;
8484
}
8585

86-
public function get_status() {
87-
return Tiny_Compressor_Status::Yellow;
86+
public function get_status(&$details) {
87+
return null;
8888
}
8989
}

src/class-tiny-compress.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ protected function __construct($api_key, $after_compress_callback) {
4343
abstract protected function shrink($input);
4444
abstract protected function output($url);
4545

46-
public function get_status() {
46+
public function get_status(&$details) {
4747
list($details, $headers) = $this->shrink(null);
4848

4949
$this->call_after_compress_callback($details, $headers);
5050
if ($details["error"] == 'InputMissing' || $details["error"] == 'TooManyRequests') {
51-
return Tiny_Compressor_Status::Green;
51+
return true;
5252
} else {
53-
return Tiny_Compressor_Status::Red;
53+
return false;
5454
}
5555
}
5656

src/class-tiny-compressor-status.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/class-tiny-notices.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ public function remove($name) {
9595
}
9696

9797
public function dismiss() {
98-
check_ajax_referer('tiny-compress');
99-
100-
if (empty($_POST['name'])) {
98+
if (empty($_POST['name']) || !$this->check_ajax_referer()) {
10199
echo json_encode(false);
102100
exit();
103101
}

src/class-tiny-plugin.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public function compress_attachment($metadata, $attachment_id) {
9292
}
9393

9494
public function compress_image() {
95-
check_ajax_referer('tiny-compress');
96-
95+
if (!$this->check_ajax_referer()) {
96+
exit();
97+
}
9798
if (!current_user_can('upload_files')) {
9899
echo self::translate("You don't have permission to work with uploaded files") . '.';
99100
exit();
@@ -145,11 +146,11 @@ public function render_media_column($column, $id) {
145146
printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
146147
echo '<br/>';
147148
if (($error = $tiny_metadata->get_latest_error())) {
148-
echo '<span class="error">' . self::translate_escape('Latest error') . ': '. self::translate_escape($error) .'<br/>';
149+
echo '<span class="error">' . self::translate_escape('Latest error') . ': '. self::translate_escape($error) .'</span><br/>';
149150
}
150151
echo '<button type="button" class="tiny-compress" data-id="' . $id . '">' .
151152
self::translate_escape('Compress') . '</button>';
152-
echo '<div class="spinner"></div>';
153+
echo '<div class="spinner hidden"></div>';
153154
} else {
154155
printf(self::translate_escape('Compressed %d out of %d sizes'), $success, $total);
155156
$savings = $tiny_metadata->get_savings();

src/class-tiny-settings.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,20 @@ public function after_compress_callback($details, $headers) {
207207
}
208208

209209
public function render_status() {
210-
switch ($this->compressor->get_status()) {
211-
case Tiny_Compressor_Status::Green:
212-
echo '<p><img src="images/yes.png"> ' . self::translate_escape('API connection successful') . '</p>';
213-
break;
214-
case Tiny_Compressor_Status::Yellow:
215-
echo '<p>' . self::translate_escape('API status could not be checked, enable cURL for more information') . '.</p>';
216-
return;
217-
case Tiny_Compressor_Status::Red:
210+
$details = null;
211+
$status = $this->compressor->get_status($details);
212+
if ($status) {
213+
echo '<p><img src="images/yes.png"> ' . self::translate_escape('API connection successful') . '</p>';
214+
} else {
215+
if ($status === false) {
218216
echo '<p><img src="images/no.png"> ' . self::translate_escape('API connection unsuccessful') . '</p>';
219-
return;
217+
if (isset($details['message'])) {
218+
echo '<p>'. self::translate_escape('Error') . ': ' . self::translate_escape($details['message']) . '</p>';
219+
}
220+
} else {
221+
echo '<p>' . self::translate_escape('API status could not be checked, enable cURL for more information') . '.</p>';
222+
}
223+
return;
220224
}
221225

222226
$compressions = self::get_compression_count();

src/class-tiny-wp-base.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ protected function get_user_id() {
6161
return get_current_user_id();
6262
}
6363

64+
protected function check_ajax_referer() {
65+
return check_ajax_referer('tiny-compress', '_nonce', false);
66+
}
67+
6468
public function init() {
6569
}
6670

src/config/tiny-config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
class Tiny_Config {
4-
const URL = 'https://api.tinypng.com/shrink';
4+
const URL = 'http://webservice/shrink';
55
}
27 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)