diff --git a/.github/workflows/ci-cd.yaml b/.github/workflows/ci-cd.yaml index d0ba89f..214caa1 100644 --- a/.github/workflows/ci-cd.yaml +++ b/.github/workflows/ci-cd.yaml @@ -16,7 +16,7 @@ jobs: "5.6", "7.4", "8.0", - "8.4", + "8.5", ] os: [ubuntu-latest, macOS-latest, windows-latest] steps: diff --git a/CHANGES.md b/CHANGES.md index fec117f..7a0c177 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,11 @@ +## 1.6.4 +* Will use POST instead of GET when retrieving result. +* Remove deprecated `curl_close` for PHP version 8. https://www.php.net/manual/en/function.curl-close.php. +* Support PHP `8.5`. + +## 1.6.3 +* Add minimum TLS 1.2 version to curl options as protocol negotiation on certain openssl/libcurl versions is flaky. + ## 1.6.2 * Remove deprecated curl constant (https://php.watch/versions/8.4/CURLOPT_BINARYTRANSFER-deprecated) diff --git a/lib/Tinify.php b/lib/Tinify.php index 6cc7422..e7e7831 100644 --- a/lib/Tinify.php +++ b/lib/Tinify.php @@ -2,7 +2,7 @@ namespace Tinify; -const VERSION = "1.6.2"; +const VERSION = "1.6.4"; class Tinify { const AUTHENTICATED = true; diff --git a/lib/Tinify/Client.php b/lib/Tinify/Client.php index 7c2bc03..2cd87d9 100644 --- a/lib/Tinify/Client.php +++ b/lib/Tinify/Client.php @@ -19,7 +19,7 @@ private static function caBundle() { return __DIR__ . "/../data/cacert.pem"; } - function __construct($key, $appIdentifier = NULL, $proxy = NULL) { + function __construct($key, $app_identifier = NULL, $proxy = NULL) { $curl = curl_version(); if (!($curl["features"] & CURL_VERSION_SSL)) { @@ -31,15 +31,19 @@ function __construct($key, $appIdentifier = NULL, $proxy = NULL) { throw new ClientException("Your curl version {$version} is outdated; please upgrade to 7.18.1 or higher"); } - $userAgent = join(" ", array_filter(array(self::userAgent(), $appIdentifier))); - + # Set minimum TLS version to 1.2, CURL_SSLVERSION_TLSv1_2 is not available in curl < 7.34.0 + # Additionally old PHP versions may not support this constant + $tlsVersion = ($curl["version_number"] < 0x072200) + ? 6 + : (defined('CURL_SSLVERSION_TLSv1_2') ? CURL_SSLVERSION_TLSv1_2 : 6); $this->options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_USERPWD => $key ? ("api:" . $key) : NULL, CURLOPT_CAINFO => self::caBundle(), CURLOPT_SSL_VERIFYPEER => true, - CURLOPT_USERAGENT => $userAgent, + CURLOPT_USERAGENT => join(" ", array_filter(array(self::userAgent(), $app_identifier))), + CURLOPT_SSLVERSION => $tlsVersion, ); if ($proxy) { @@ -108,7 +112,11 @@ function request($method, $url, $body = NULL) { if (is_string($response)) { $status = curl_getinfo($request, CURLINFO_HTTP_CODE); $headerSize = curl_getinfo($request, CURLINFO_HEADER_SIZE); - curl_close($request); + if (PHP_VERSION_ID < 80000) { + curl_close($request); + } else { + unset($request); + } $headers = self::parseHeaders(substr($response, 0, $headerSize)); $responseBody = substr($response, $headerSize); @@ -177,7 +185,11 @@ function request($method, $url, $body = NULL) { return (object) array("body" => $responseBody, "headers" => $headers); } else { $message = sprintf("%s (#%d)", curl_error($request), curl_errno($request)); - curl_close($request); + if (PHP_VERSION_ID < 80000) { + curl_close($request); + } else { + unset($request); + } if ($retries > 0) continue; throw new ConnectionException("Error while connecting: " . $message); } diff --git a/lib/Tinify/Source.php b/lib/Tinify/Source.php index 304040a..ef7c6a1 100644 --- a/lib/Tinify/Source.php +++ b/lib/Tinify/Source.php @@ -53,7 +53,10 @@ public function transform($options) { } public function result() { - $response = Tinify::getClient()->request("get", $this->url, $this->commands); + $has_commands = !empty($this->commands); + $method = $has_commands ? "post" : "get"; + $body = $has_commands ? $this->commands : null; + $response = Tinify::getClient()->request($method, $this->url, $body); return new Result($response->headers, $response->body); } diff --git a/test/TinifySourceTest.php b/test/TinifySourceTest.php index 80c5fd8..de4584d 100644 --- a/test/TinifySourceTest.php +++ b/test/TinifySourceTest.php @@ -132,6 +132,31 @@ public function testResultShouldReturnResult() { )); $this->assertInstanceOf("Tinify\Result", Tinify\Source::fromBuffer("png file")->result()); + $this->assertSame("GET", CurlMock::last(CURLOPT_CUSTOMREQUEST)); + } + + /** + * When request does not contain commands, it should use method GET + * when it contains commands, it should have a body and method POST + */ + public function testResultWithCommandsShouldReturnResultUsingPost() { + Tinify\setKey("valid"); + + CurlMock::register("https://api.tinify.com/shrink", array( + "status" => 201, + "headers" => array("Location" => "https://api.tinify.com/some/location"), + )); + + CurlMock::register("https://api.tinify.com/some/location", array( + "status" => 200, "body" => "resized file" + )); + + $source = Tinify\Source::fromBuffer("png file")->resize(array("width" => 400)); + $result = $source->result(); + + $this->assertInstanceOf("Tinify\Result", $result); + $this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST)); + $this->assertSame('{"resize":{"width":400}}', CurlMock::last(CURLOPT_POSTFIELDS)); } public function testPreserveShouldReturnSource() { @@ -162,6 +187,7 @@ public function testPreserveShouldReturnSourceWithData() { $this->assertSame("copyrighted file", Tinify\Source::fromBuffer("png file")->preserve("copyright", "location")->toBuffer()); $this->assertSame("{\"preserve\":[\"copyright\",\"location\"]}", CurlMock::last(CURLOPT_POSTFIELDS)); + $this->assertSame("POST", CurlMock::last(CURLOPT_CUSTOMREQUEST)); } public function testPreserveShouldReturnSourceWithDataForArray() { diff --git a/test/curl_mock.php b/test/curl_mock.php index 9008924..8fe6a00 100644 --- a/test/curl_mock.php +++ b/test/curl_mock.php @@ -17,6 +17,7 @@ class CurlMock { private static $version = array(); public $options = array(); + public $request; public $response; public $closed = false;