From 92c2ce658111c7d86a552521c619083e41ebd834 Mon Sep 17 00:00:00 2001 From: Andrii Lutskevych Date: Tue, 21 May 2024 18:08:28 +0300 Subject: [PATCH] Fix: Curl Connection remaining open (#133) * add curl_close and fix undefined variable * change position of the curl_close * 128 - wrap by try-finally block * 128 - update changelog.md --------- Co-authored-by: alutskevich --- CHANGELOG.md | 4 ++++ MatomoTracker.php | 38 +++++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fcb693e..5afde19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ This is the Developer Changelog for Matomo PHP Tracker. All breaking changes or new features are listed below. +## Matomo PHP Tracker 3.3.1 +### Fixed +- closed curl connection + ## Matomo PHP Tracker 3.3.0 ### Removed - support for PHP versions lower than 7.2 diff --git a/MatomoTracker.php b/MatomoTracker.php index 7b65ace..234c6bf 100644 --- a/MatomoTracker.php +++ b/MatomoTracker.php @@ -2012,6 +2012,8 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal } } + $content = ''; + if (function_exists('curl_init') && function_exists('curl_exec')) { $options = $this->prepareCurlOptions($url, $method, $data, $forcePostUrlEncoded); @@ -2019,29 +2021,31 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal curl_setopt_array($ch, $options); ob_start(); $response = @curl_exec($ch); - ob_end_clean(); - $header = ''; - $content = ''; + try { + $header = ''; - if ($response === false) { - $curlError = curl_error($ch); - if (!empty($curlError)) { - throw new \RuntimeException($curlError); + if ($response === false) { + $curlError = curl_error($ch); + if (!empty($curlError)) { + throw new \RuntimeException($curlError); + } } - } - - if (!empty($response)) { - // extract header - $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); - $header = substr($response, 0, $headerSize); - // extract content - $content = substr($response, $headerSize); - } + if (!empty($response)) { + // extract header + $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + $header = substr($response, 0, $headerSize); - $this->parseIncomingCookies(explode("\r\n", $header)); + // extract content + $content = substr($response, $headerSize); + } + $this->parseIncomingCookies(explode("\r\n", $header)); + } finally { + curl_close($ch); + ob_end_clean(); + } } elseif (function_exists('stream_context_create')) { $stream_options = $this->prepareStreamOptions($method, $data, $forcePostUrlEncoded);