Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with php 8.3 #122

Open
wants to merge 4 commits into
base: nextgen
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CaptchaObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CaptchaObject
{
public $captchaId;
public $captchaImageData;
public $captchaImageAudio;
public $captchaAudioData;
public $creationTime;
public $code;
public $code_display;
Expand Down
4 changes: 3 additions & 1 deletion StorageAdapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Securimage\StorageAdapter;

use Securimage\CaptchaObject;

interface AdapterInterface
{
/**
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion StorageAdapter/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion StorageAdapter/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 13 additions & 10 deletions StorageAdapter/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion WavFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 *******************/
Expand Down
47 changes: 29 additions & 18 deletions securimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
*
*/

use Securimage\StorageAdapter\AdapterInterface;


/**
* Securimage CAPTCHA Class.
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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<AdapterInterface> 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
Expand Down Expand Up @@ -683,6 +690,11 @@ class Securimage
*/
protected $gdsignaturecolor;

/**
* @var int
*/
protected $gdnoisecolor;

/**
* Create a new securimage object, pass options to set in the constructor.
*
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down