From e599949801c288a2b2998c9546e5cde50f60d38f Mon Sep 17 00:00:00 2001 From: Alexey Kalnov Date: Sat, 28 Sep 2024 09:06:32 +0300 Subject: [PATCH 1/4] Compatible with php 8.3 --- securimage.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/securimage.php b/securimage.php index d30d976..149c6a9 100644 --- a/securimage.php +++ b/securimage.php @@ -683,6 +683,11 @@ class Securimage */ protected $gdsignaturecolor; + /** + * @var int + */ + protected $gdnoisecolor; + /** * Create a new securimage object, pass options to set in the constructor. * @@ -1451,7 +1456,7 @@ public function rangeDownload($audio) $audioLength = $size = strlen($audio); if (isset($_SERVER['HTTP_RANGE'])) { - list( , $range) = explode('=', $_SERVER['HTTP_RANGE']); // bytes=byte-range-set + [ , $range] = explode('=', $_SERVER['HTTP_RANGE']); // bytes=byte-range-set $range = trim($range); if (strpos($range, ',') !== false) { @@ -1981,11 +1986,11 @@ protected function distortedCopy() $py = array(); // y coordinates of poles $rad = array(); // radius of distortion from pole $amp = array(); // amplitude - $x = ($this->image_width / 4); // lowest x coordinate of a pole + $x = round($this->image_width / 4); // lowest x coordinate of a pole $maxX = $this->image_width - $x; // maximum x coordinate of a pole - $dx = mt_rand($x / 10, $x); // horizontal distance between poles + $dx = mt_rand(round($x / 10), $x); // horizontal distance between poles $y = mt_rand(20, $this->image_height - 20); // random y coord - $dy = mt_rand(20, $this->image_height * 0.7); // y distance + $dy = mt_rand(20, round($this->image_height * 0.7)); // y distance $minY = 20; // minimum y coordinate $maxY = $this->image_height - 20; // maximum y cooddinate @@ -2018,7 +2023,7 @@ protected function distortedCopy() if ($r > $rad[$i]) { continue; } - $rscale = $amp[$i] * sin(3.14 * $r / $rad[$i]); + $rscale = round($amp[$i] * sin(3.14 * $r / $rad[$i])); $x += $dx * $rscale; $y += $dy * $rscale; } @@ -2043,11 +2048,11 @@ protected function drawLines() for ($line = 0; $line < $this->num_lines; ++ $line) { $x = $this->image_width * (1 + $line) / ($this->num_lines + 1); $x += (0.5 - $this->frand()) * $this->image_width / $this->num_lines; - $y = mt_rand($this->image_height * 0.1, $this->image_height * 0.9); + $y = mt_rand(round($this->image_height * 0.1), $this->image_height * 0.9); $theta = ($this->frand() - 0.5) * M_PI * 0.33; $w = $this->image_width; - $len = mt_rand($w * 0.4, $w * 0.7); + $len = mt_rand(round($w * 0.4), round($w * 0.7)); $lwid = mt_rand(0, 2); $k = $this->frand() * 0.6 + 0.2; @@ -2065,8 +2070,8 @@ protected function drawLines() $ldy = round($dx * $lwid); for ($i = 0; $i < $n; ++ $i) { - $x = $x0 + $i * $dx + $amp * $dy * sin($k * $i * $step + $phi); - $y = $y0 + $i * $dy - $amp * $dx * sin($k * $i * $step + $phi); + $x = round($x0 + $i * $dx + $amp * $dy * sin($k * $i * $step + $phi)); + $y = round($y0 + $i * $dy - $amp * $dx * sin($k * $i * $step + $phi)); imagefilledrectangle($this->im, $x, $y, $x + $lwid, $y + $lwid, $this->gdlinecolor); } } From 8587b12aa6a5208b9350700a34258443d030a9e0 Mon Sep 17 00:00:00 2001 From: Alexey Kalnov Date: Sat, 28 Sep 2024 17:46:53 +0300 Subject: [PATCH 2/4] Compatible with php 8.3 --- CaptchaObject.php | 2 +- StorageAdapter/Mysqli.php | 2 +- StorageAdapter/PDO.php | 2 +- StorageAdapter/Session.php | 27 +++++++++++++++------------ WavFile.php | 5 ++++- securimage.php | 9 +++++++-- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/CaptchaObject.php b/CaptchaObject.php index 119bd9c..3f7e944 100644 --- a/CaptchaObject.php +++ b/CaptchaObject.php @@ -6,7 +6,7 @@ class CaptchaObject { public $captchaId; public $captchaImageData; - public $captchaImageAudio; + public $captchaAudioData; public $creationTime; public $code; public $code_display; diff --git a/StorageAdapter/Mysqli.php b/StorageAdapter/Mysqli.php index 6879c1c..9fc7e87 100644 --- a/StorageAdapter/Mysqli.php +++ b/StorageAdapter/Mysqli.php @@ -50,7 +50,7 @@ public function get($captchaId, $what = null) $info->code = $code['code']; $info->code_display = $code['code_display']; $info->creationTime = (int)$code['created']; - $info->captchaImageAudio = $code['audio_data']; + $info->captchaAudioData = $code['audio_data']; return $info; } diff --git a/StorageAdapter/PDO.php b/StorageAdapter/PDO.php index 4141cc1..21bf598 100644 --- a/StorageAdapter/PDO.php +++ b/StorageAdapter/PDO.php @@ -51,7 +51,7 @@ public function get($captchaId, $what = null) $info->code = $code['code']; $info->code_display = $code['code_display']; $info->creationTime = (int)$code['created']; - $info->captchaImageAudio = $code['audio_data']; + $info->captchaAudioData = $code['audio_data']; return $info; } diff --git a/StorageAdapter/Session.php b/StorageAdapter/Session.php index b43aad9..21b5fd3 100644 --- a/StorageAdapter/Session.php +++ b/StorageAdapter/Session.php @@ -17,33 +17,36 @@ public function __construct($options = null) $this->bootstrap(); } - public function store($captchaId, $captchaInfo) + public function store($captchaId, $captchaInfo): bool { if ((function_exists('session_status') && PHP_SESSION_ACTIVE == session_status()) || session_id() != '') { - $_SESSION['securimage_data'][$captchaId] = $captchaInfo; - + $_SESSION['securimage_data'][$captchaId] = serialize($captchaInfo); return true; } return false; } - public function storeAudioData($captchaId, $audioData) + public function storeAudioData($captchaId, $audioData): bool { - if (isset($_SESSION['securimage_data'][$captchaId]) && - $_SESSION['securimage_data'][$captchaId] instanceof \Securimage\CaptchaObject - ) { - $_SESSION['securimage_data'][$captchaId]->captchaAudioData = $audioData; - return true; + $info = $this->get($captchaId); + if ($info === null) { + return false; } - return false; + $info->captchaAudioData = $audioData; + $this->store($captchaId, $info); + return true; } - public function get($captchaId, $what = null) + public function get($captchaId, $what = null): ?\Securimage\CaptchaObject { if (isset($_SESSION['securimage_data'][$captchaId])) { - return $_SESSION['securimage_data'][$captchaId]; + $info = unserialize($_SESSION['securimage_data'][$captchaId]); + if (!($info instanceof \Securimage\CaptchaObject)) { + return null; + } + return $info; } return null; diff --git a/WavFile.php b/WavFile.php index f1180a5..4c245e5 100644 --- a/WavFile.php +++ b/WavFile.php @@ -1701,7 +1701,10 @@ public function filter($filters, $blockOffset = 0, $numBlocks = null) /************* DEGRADE FILTER *******************/ if ($filter_degrade) { - $sampleFloat += rand(1000000 * ($degrade_quality - 1), 1000000 * (1 - $degrade_quality)) / 1000000; + $sampleFloat += rand( + round(1000000 * ($degrade_quality - 1)), + round(1000000 * (1 - $degrade_quality)) + ) / 1000000; } /************* VOLUME FILTER *******************/ diff --git a/securimage.php b/securimage.php index 149c6a9..162fe03 100644 --- a/securimage.php +++ b/securimage.php @@ -614,7 +614,12 @@ class Securimage * * @var string */ - protected $captcha_code; + protected $code_entered; + + /** + * @var string + */ + protected $correct_code; /** * Time (in seconds) that the captcha was solved in (correctly or incorrectly). @@ -1380,7 +1385,7 @@ public function outputAudioFile($format = null) require_once dirname(__FILE__) . '/WavFile.php'; $audio = $this->getAudibleCode(); - if (strtolower($format) == 'mp3') { + if (!empty($format) && strtolower($format) === 'mp3') { $audio = $this->wavToMp3($audio); } From cf97ccce0169e91641373e690f22278d9e71ce48 Mon Sep 17 00:00:00 2001 From: Alexey Kalnov Date: Sat, 28 Sep 2024 18:00:05 +0300 Subject: [PATCH 3/4] Compatible with php 8.3 --- StorageAdapter/AdapterInterface.php | 6 ++++-- securimage.php | 17 +++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/StorageAdapter/AdapterInterface.php b/StorageAdapter/AdapterInterface.php index eccc7a7..777f83c 100644 --- a/StorageAdapter/AdapterInterface.php +++ b/StorageAdapter/AdapterInterface.php @@ -2,6 +2,8 @@ namespace Securimage\StorageAdapter; +use Securimage\CaptchaObject; + interface AdapterInterface { /** @@ -25,9 +27,9 @@ public function storeAudioData($captchaId, $audioData); * * @param string $captchaId The captcha ID to fetch info for * @param int $what What info to retrieve (e.g. code, image data, all) - * @return mixed false on failure, captcha info otherwise + * @return ?CaptchaObject NULL on failure, captcha info otherwise */ - public function get($captchaId, $what = null); + public function get($captchaId, $what = null): ?CaptchaObject; /** * Delete captcha data from the store diff --git a/securimage.php b/securimage.php index 162fe03..ca1b45d 100644 --- a/securimage.php +++ b/securimage.php @@ -46,6 +46,8 @@ * */ +use Securimage\StorageAdapter\AdapterInterface; + /** * Securimage CAPTCHA Class. @@ -648,9 +650,9 @@ class Securimage /** * List of storage adapters for persisting captcha data * - * @var array Array of one or more storage adapters + * @var list Array of one or more storage adapters */ - protected $storage_adapters = array(); + protected $storage_adapters = []; /** * Flag indicating whether or not HTTP headers will be sent when outputting @@ -2471,19 +2473,18 @@ protected function saveAudioData($data, $captchaId) /** * Gets audio file contents from the session or database - * - * @return string|boolean Audio contents on success, or false if no audio found in session or DB + * @return ?string Audio contents on success, or NULL if no audio found in session or DB */ - protected function getAudioData($captchaId) + protected function getAudioData($captchaId): ?string { foreach($this->storage_adapters as $adapter) { $info = $adapter->get($captchaId); - if ($info && !empty($info->captchaImageAudio)) { - return $info->captchaImageAudio; + if ($info && !empty($info->captchaAudioData)) { + return $info->captchaAudioData; } } - return false; + return null; } /** From 7fe09987b9f86866e5e37642f8e6e00299fe7063 Mon Sep 17 00:00:00 2001 From: Alexey Kalnov Date: Mon, 30 Sep 2024 11:46:02 +0300 Subject: [PATCH 4/4] remove return types --- StorageAdapter/AdapterInterface.php | 2 +- StorageAdapter/Session.php | 4 ++-- securimage.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/StorageAdapter/AdapterInterface.php b/StorageAdapter/AdapterInterface.php index 777f83c..4bc69ab 100644 --- a/StorageAdapter/AdapterInterface.php +++ b/StorageAdapter/AdapterInterface.php @@ -29,7 +29,7 @@ public function storeAudioData($captchaId, $audioData); * @param int $what What info to retrieve (e.g. code, image data, all) * @return ?CaptchaObject NULL on failure, captcha info otherwise */ - public function get($captchaId, $what = null): ?CaptchaObject; + public function get($captchaId, $what = null); /** * Delete captcha data from the store diff --git a/StorageAdapter/Session.php b/StorageAdapter/Session.php index 21b5fd3..2ebdf33 100644 --- a/StorageAdapter/Session.php +++ b/StorageAdapter/Session.php @@ -17,7 +17,7 @@ public function __construct($options = null) $this->bootstrap(); } - public function store($captchaId, $captchaInfo): bool + public function store($captchaId, $captchaInfo) { if ((function_exists('session_status') && PHP_SESSION_ACTIVE == session_status()) || session_id() != '') { $_SESSION['securimage_data'][$captchaId] = serialize($captchaInfo); @@ -27,7 +27,7 @@ public function store($captchaId, $captchaInfo): bool return false; } - public function storeAudioData($captchaId, $audioData): bool + public function storeAudioData($captchaId, $audioData) { $info = $this->get($captchaId); if ($info === null) { diff --git a/securimage.php b/securimage.php index ca1b45d..1b5a2e2 100644 --- a/securimage.php +++ b/securimage.php @@ -1997,7 +1997,7 @@ protected function distortedCopy() $maxX = $this->image_width - $x; // maximum x coordinate of a pole $dx = mt_rand(round($x / 10), $x); // horizontal distance between poles $y = mt_rand(20, $this->image_height - 20); // random y coord - $dy = mt_rand(20, round($this->image_height * 0.7)); // y distance + $dy = mt_rand(20, round($this->image_height * 0.7, 0)); // y distance $minY = 20; // minimum y coordinate $maxY = $this->image_height - 20; // maximum y cooddinate @@ -2055,7 +2055,7 @@ protected function drawLines() for ($line = 0; $line < $this->num_lines; ++ $line) { $x = $this->image_width * (1 + $line) / ($this->num_lines + 1); $x += (0.5 - $this->frand()) * $this->image_width / $this->num_lines; - $y = mt_rand(round($this->image_height * 0.1), $this->image_height * 0.9); + $y = mt_rand(floor($this->image_height * 0.1), floor($this->image_height * 0.9)); $theta = ($this->frand() - 0.5) * M_PI * 0.33; $w = $this->image_width; @@ -2475,7 +2475,7 @@ protected function saveAudioData($data, $captchaId) * Gets audio file contents from the session or database * @return ?string Audio contents on success, or NULL if no audio found in session or DB */ - protected function getAudioData($captchaId): ?string + protected function getAudioData($captchaId) { foreach($this->storage_adapters as $adapter) { $info = $adapter->get($captchaId);