Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 29 additions & 34 deletions src/class-tiny-compress-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

if ( ! defined( '\Tinify\VERSION' ) ) {
/* Load vendored client if it is not yet loaded. */
require_once __DIR__ . '/vendor/tinify/Tinify/Exception.php';
require_once __DIR__ . '/vendor/tinify/Tinify/ResultMeta.php';
require_once __DIR__ . '/vendor/tinify/Tinify/Result.php';
require_once __DIR__ . '/vendor/tinify/Tinify/Source.php';
require_once __DIR__ . '/vendor/tinify/Tinify/Client.php';
require_once __DIR__ . '/vendor/tinify/Tinify.php';
require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Exception.php';
require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/ResultMeta.php';
require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Result.php';
require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Source.php';
require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify/Client.php';
require_once dirname( __FILE__ ) . '/vendor/tinify/Tinify.php';
}

class Tiny_Compress_Client extends Tiny_Compress {
Expand All @@ -48,7 +48,7 @@ class Tiny_Compress_Client extends Tiny_Compress {
const CONNECT_TIMEOUT = 8;

private $last_error_code = 0;
private $last_message = '';
private $last_message = '';
private $proxy;

protected function __construct( $api_key, $after_compress_callback ) {
Expand Down Expand Up @@ -120,49 +120,44 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to )
}

$compress_result = $source->result();
$meta = array(
'input' => array(
$meta = array(
'input' => array(
'size' => strlen( $input ),
'type' => Tiny_Helpers::get_mimetype( $input ),
),
'output' => array(
'size' => $compress_result->size(),
'type' => $compress_result->mediaType(),
'width' => $compress_result->width(),
'size' => $compress_result->size(),
'type' => $compress_result->mediaType(),
'width' => $compress_result->width(),
'height' => $compress_result->height(),
'ratio' => round( $compress_result->size() / strlen( $input ), 4 ),
'ratio' => round( $compress_result->size() / strlen( $input ), 4 ),
),
);

$buffer = $compress_result->toBuffer();
$result = array( $buffer, $meta, null );

if ( count( $convert_to ) > 0 ) {
$convert_source = $source->convert(
array(
'type' => $convert_to,
)
);
$convert_result = $convert_source->result();
$convert_source = $source->convert( array(
'type' => $convert_to,
) );
$convert_result = $convert_source->result();
$meta['convert'] = array(
'type' => $convert_result->mediaType(),
'size' => $convert_result->size(),
);
$convert_buffer = $convert_result->toBuffer();
$result = array( $buffer, $meta, $convert_buffer );
$convert_buffer = $convert_result->toBuffer();
$result = array( $buffer, $meta, $convert_buffer );
}

return $result;
} catch ( \Tinify\Exception $err ) {
$this->last_error_code = $err->status;

Tiny_Logger::error(
'client compress error',
array(
'error' => $err->getMessage(),
'status' => $err->status,
)
);
Tiny_Logger::error('client compress error', array(
'error' => $err->getMessage(),
'status' => $err->status,
));

throw new Tiny_Exception(
$err->getMessage(),
Expand Down Expand Up @@ -193,30 +188,30 @@ public function create_key( $email, $options ) {

private function set_request_options( $client ) {
/* The client does not let us override cURL properties yet, so we have
to use a reflection property. */
to use a reflection property. */
$property = new ReflectionProperty( $client, 'options' );
$property->setAccessible( true );
$options = $property->getValue( $client );

// Set API request timeout to prevent indefinite hanging
$options[ CURLOPT_TIMEOUT ] = self::API_TIMEOUT;
$options[ CURLOPT_TIMEOUT ] = self::API_TIMEOUT;
$options[ CURLOPT_CONNECTTIMEOUT ] = self::CONNECT_TIMEOUT;

if ( TINY_DEBUG ) {
$file = fopen( __DIR__ . '/curl.log', 'w' );
$file = fopen( dirname( __FILE__ ) . '/curl.log', 'w' );
if ( is_resource( $file ) ) {
$options[ CURLOPT_VERBOSE ] = true;
$options[ CURLOPT_STDERR ] = $file;
$options[ CURLOPT_STDERR ] = $file;
}
}

if ( $this->proxy->is_enabled() && $this->proxy->send_through_proxy( $url ) ) {
$options[ CURLOPT_PROXYTYPE ] = CURLPROXY_HTTP;
$options[ CURLOPT_PROXY ] = $this->proxy->host();
$options[ CURLOPT_PROXY ] = $this->proxy->host();
$options[ CURLOPT_PROXYPORT ] = $this->proxy->port();

if ( $this->proxy->use_authentication() ) {
$options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY;
$options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY;
$options[ CURLOPT_PROXYUSERPWD ] = $this->proxy->authentication();
}
}
Expand Down
74 changes: 31 additions & 43 deletions src/class-tiny-compress-fopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function get_key() {
}

protected function validate() {
$params = $this->request_options( 'GET' );
$url = Tiny_Config::KEYS_URL . '/' . $this->get_key();
$params = $this->request_options( 'GET' );
$url = Tiny_Config::KEYS_URL . '/' . $this->get_key();
list($details, $headers, $status_code) = $this->request( $params, $url );

if ( 429 == $status_code || 400 == $status_code || 200 == $status_code ) {
Expand All @@ -83,16 +83,13 @@ protected function validate() {
}

protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) {
$params = $this->request_options( 'POST', $input );
$params = $this->request_options( 'POST', $input );
list($details, $headers, $status_code) = $this->request( $params );

Tiny_Logger::debug(
'client fopen compress out',
array(
'details' => $details,
'status' => $status_code,
)
);
Tiny_Logger::debug('client fopen compress out', array(
'details' => $details,
'status' => $status_code,
));

$output_url = isset( $headers['location'] ) ? $headers['location'] : null;
if ( $status_code >= 400 && is_array( $details ) && isset( $details['error'] ) ) {
Expand All @@ -114,10 +111,7 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to )
);
}

$params = $this->output_request_options(
$resize_opts,
$preserve_opts
);
$params = $this->output_request_options( $resize_opts, $preserve_opts );
list($output, $headers, $status_code) = $this->request( $params, $output_url );

if ( $status_code >= 400 && is_array( $output ) && isset( $output['error'] ) ) {
Expand All @@ -142,19 +136,16 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to )
}

$meta = array(
'input' => array(
'input' => array(
'size' => strlen( $input ),
'type' => Tiny_Helpers::get_mimetype( $input ),
),
'output' => array(
'size' => strlen( $output ),
'type' => $headers['content-type'],
'width' => intval( $headers['image-width'] ),
'size' => strlen( $output ),
'type' => $headers['content-type'],
'width' => intval( $headers['image-width'] ),
'height' => intval( $headers['image-height'] ),
'ratio' => round(
strlen( $output ) / strlen( $input ),
4
),
'ratio' => round( strlen( $output ) / strlen( $input ), 4 ),
),
);

Expand All @@ -175,11 +166,11 @@ protected function compress( $input, $resize_opts, $preserve_opts, $convert_to )
$convert_params,
$output_url
);
$meta['convert'] = array(
$meta['convert'] = array(
'type' => $convert_headers['content-type'],
'size' => strlen( $convert_output ),
);
$convert = $convert_output;
$convert = $convert_output;

}

Expand All @@ -199,13 +190,13 @@ private function request( $params, $url = Tiny_Config::SHRINK_URL ) {
}

$meta_data = stream_get_meta_data( $request );
$headers = $meta_data['wrapper_data'];
$headers = $meta_data['wrapper_data'];
if ( ! is_array( $headers ) ) {
$headers = iterator_to_array( $headers );
}

$status_code = $this->parse_status_code( $headers );
$headers = $this->parse_headers( $headers );
$headers = $this->parse_headers( $headers );

if ( isset( $headers['compression-count'] ) ) {
$this->compression_count = intval( $headers['compression-count'] );
Expand Down Expand Up @@ -262,22 +253,19 @@ private function parse_headers( $headers ) {
private function request_options( $method, $body = null, $headers = array() ) {
return array(
'http' => array(
'method' => $method,
'header' => array_merge(
$headers,
array(
'Authorization: Basic ' . base64_encode( 'api:' . $this->api_key ),
'User-Agent: ' . self::identifier(),
'Content-Type: multipart/form-data',
)
),
'content' => $body,
'method' => $method,
'header' => array_merge($headers, array(
'Authorization: Basic ' . base64_encode( 'api:' . $this->api_key ),
'User-Agent: ' . self::identifier(),
'Content-Type: multipart/form-data',
)),
'content' => $body,
'follow_location' => 0,
'max_redirects' => 1, // Necessary for PHP 5.2
'ignore_errors' => true, // Apparently, a 201 is a failure
'max_redirects' => 1, // Necessary for PHP 5.2
'ignore_errors' => true, // Apparently, a 201 is a failure
),
'ssl' => array(
'cafile' => $this->get_ca_file(),
'ssl' => array(
'cafile' => $this->get_ca_file(),
'verify_peer' => true,
),
);
Expand All @@ -303,16 +291,16 @@ private function output_request_options( $resize_opts, $preserve_opts ) {
}

private static function get_ca_file() {
return __DIR__ . '/data/cacert.pem';
return dirname( __FILE__ ) . '/data/cacert.pem';
}

private static function decode( $text ) {
$result = json_decode( $text, true );
if ( null === $result ) {
$message = sprintf(
'JSON: %s [%d]',
( PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error' ),
( PHP_VERSION_ID >= 50300 ? json_last_error() : 'Error' )
(PHP_VERSION_ID >= 50500 ? json_last_error_msg() : 'Unknown error'),
(PHP_VERSION_ID >= 50300 ? json_last_error() : 'Error')
);

throw new Tiny_Exception( $message, 'JsonError' );
Expand Down
Loading
Loading