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/AdapterInterface.php b/StorageAdapter/AdapterInterface.php index eccc7a7..4bc69ab 100644 --- a/StorageAdapter/AdapterInterface.php +++ b/StorageAdapter/AdapterInterface.php @@ -2,6 +2,8 @@ namespace Securimage\StorageAdapter; +use Securimage\CaptchaObject; + interface AdapterInterface { /** @@ -25,7 +27,7 @@ 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); 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..2ebdf33 100644 --- a/StorageAdapter/Session.php +++ b/StorageAdapter/Session.php @@ -20,8 +20,7 @@ public function __construct($options = null) public function store($captchaId, $captchaInfo) { 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; } @@ -30,20 +29,24 @@ public function store($captchaId, $captchaInfo) public function storeAudioData($captchaId, $audioData) { - 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 d30d976..1b5a2e2 100644 --- a/securimage.php +++ b/securimage.php @@ -46,6 +46,8 @@ * */ +use Securimage\StorageAdapter\AdapterInterface; + /** * Securimage CAPTCHA Class. @@ -614,7 +616,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). @@ -643,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 @@ -683,6 +690,11 @@ class Securimage */ protected $gdsignaturecolor; + /** + * @var int + */ + protected $gdnoisecolor; + /** * Create a new securimage object, pass options to set in the constructor. * @@ -1375,7 +1387,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); } @@ -1451,7 +1463,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 +1993,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, 0)); // y distance $minY = 20; // minimum y coordinate $maxY = $this->image_height - 20; // maximum y cooddinate @@ -2018,7 +2030,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 +2055,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(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; - $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 +2077,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); } } @@ -2461,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) { 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; } /**