Merge pull request #4592 from rob006/captcha-validator

Fix deprecation in `CCaptchaAction` for PHP 8.1+
This commit is contained in:
Marco van 't Wout
2025-07-28 09:48:51 +02:00
committed by GitHub
4 changed files with 53 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ Version 1.1.32 under development
--------------------------------
- Enh #4587: Add socket connection support to `CRedisCache` (mateusmetzker)
- Bug #4591: Fix deprecation in `CCaptchaAction` for PHP 8.1+ (rob006)
Version 1.1.31 April 10, 2025
--------------------------------

View File

@@ -47,8 +47,7 @@ class CCaptchaValidator extends CValidator
if($this->allowEmpty && $this->isEmpty($value))
return;
$captcha=$this->getCaptchaAction();
// reason of array checking is explained here: https://github.com/yiisoft/yii/issues/1955
if(is_array($value) || !$captcha->validate($value,$this->caseSensitive))
if(!$captcha->validate($value,$this->caseSensitive))
{
$message=$this->message!==null?$this->message:Yii::t('yii','The verification code is incorrect.');
$this->addError($object,$attribute,$message);
@@ -121,4 +120,3 @@ if(jQuery.trim(value)!='') {
return $js;
}
}

View File

@@ -173,6 +173,8 @@ class CCaptchaAction extends CAction
*/
public function validate($input,$caseSensitive)
{
if(!is_string($input))
return false;
$code = $this->getVerifyCode();
$valid = $caseSensitive ? ($input === $code) : strcasecmp($input,$code)===0;
$session = Yii::app()->session;

View File

@@ -0,0 +1,49 @@
<?php
require_once 'ModelMock.php';
/**
* Class CCaptchaValidatorTest.
*
* @author Robert Korulczyk <robert@korulczyk.pl>
*/
class CCaptchaValidatorTest extends CTestCase
{
public function testInvalidInput() {
Yii::app()->setController(new CCaptchaValidatorTestController('CCaptchaValidatorTest'));
$rules = array(
array('foo', 'captcha', 'allowEmpty' => false),
array('bar', 'captcha', 'allowEmpty' => true),
);
$stub = $this->getMock('ModelMock', array('rules'));
$stub->expects($this->any())
->method('rules')
->will($this->returnValue($rules));
$stub->foo = null;
$stub->bar = null;
$this->assertFalse($stub->validate());
$stub->foo = array();
$stub->bar = array();
$this->assertFalse($stub->validate());
}
}
/**
* Class CCaptchaValidatorTestController.
*
* @author Robert Korulczyk <robert@korulczyk.pl>
*/
class CCaptchaValidatorTestController extends CController
{
public function actions()
{
return array(
'captcha' => 'CCaptchaAction',
);
}
}