Skip to content

Replace Zend_Validate with laminas-validate #3199

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

Closed
wants to merge 6 commits into from
Closed
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
11 changes: 7 additions & 4 deletions app/code/core/Mage/Admin/Model/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\NotEmpty as NotEmptyValidator;
use Laminas\Validator\Regex as RegexValidator;

/**
* Class Mage_Admin_Model_Block
*
Expand All @@ -38,21 +41,21 @@ protected function _construct()

/**
* @return array|bool
* @throws Exception
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = [];

if (!Zend_Validate::is($this->getBlockName(), 'NotEmpty')) {
$notEmptyValidator = new NotEmptyValidator();
if (!$notEmptyValidator->isValid($this->getBlockName())) {
$errors[] = Mage::helper('adminhtml')->__('Block Name is required field.');
}
$disallowedBlockNames = Mage::helper('admin/block')->getDisallowedBlockNames();
if (in_array($this->getBlockName(), $disallowedBlockNames)) {
$errors[] = Mage::helper('adminhtml')->__('Block Name is disallowed.');
}
if (!Zend_Validate::is($this->getBlockName(), 'Regex', ['/^[-_a-zA-Z0-9]+\/[-_a-zA-Z0-9\/]+$/'])) {
$regexValidator = new RegexValidator(['pattern' => '/^[-_a-zA-Z0-9]+\/[-_a-zA-Z0-9\/]+$/']);
if (!$regexValidator->isValid($this->getBlockName())) {
$errors[] = Mage::helper('adminhtml')->__('Block Name is incorrect.');
}

Expand Down
22 changes: 14 additions & 8 deletions app/code/core/Mage/Admin/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\EmailAddress as EmailAddressValidator;
use Laminas\Validator\NotEmpty as NotEmptyValidator;

/**
* Admin user model
*
Expand Down Expand Up @@ -514,7 +517,7 @@ public function findFirstAvailableMenu($parent = null, $path = '', $level = 0)
return (string)$child->action;
} elseif ($child->children) {
$action = $this->findFirstAvailableMenu($child->children, $path . $childName . '/', $level + 1);
return $action ? $action : (string)$child->action;
return $action ?: (string)$child->action;
}
}
}
Expand Down Expand Up @@ -568,25 +571,27 @@ public function getStartupPageUrl()
* Returns TRUE or array of errors.
*
* @return array|true
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = new ArrayObject();

if (!Zend_Validate::is($this->getUsername(), 'NotEmpty')) {
$emailAddressValidator = new EmailAddressValidator();
$notEmptyValidator = new NotEmptyValidator();

if (!$notEmptyValidator->isValid($this->getUsername())) {
$errors->append(Mage::helper('adminhtml')->__('User Name is required field.'));
}

if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
if (!$notEmptyValidator->isValid($this->getFirstname())) {
$errors->append(Mage::helper('adminhtml')->__('First Name is required field.'));
}

if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
if (!$notEmptyValidator->isValid($this->getLastname())) {
$errors->append(Mage::helper('adminhtml')->__('Last Name is required field.'));
}

if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
if (!$emailAddressValidator->isValid($this->getEmail())) {
$errors->append(Mage::helper('adminhtml')->__('Please enter a valid email.'));
}

Expand Down Expand Up @@ -634,13 +639,14 @@ public function validate()
*
* @param string $password
* @return array|true
* @throws Zend_Validate_Exception
*/
public function validateCurrentPassword($password)
{
$result = [];

if (!Zend_Validate::is($password, 'NotEmpty')) {
$notEmptyValidator = new NotEmptyValidator();

if (!$notEmptyValidator->isValid($password)) {
$result[] = $this->_getHelper('adminhtml')->__('Current password field cannot be empty.');
} elseif (is_null($this->getId()) || !Mage::helper('core')->validateHash($password, $this->getPassword())) {
$result[] = $this->_getHelper('adminhtml')->__('Invalid current password.');
Expand Down
10 changes: 7 additions & 3 deletions app/code/core/Mage/Admin/Model/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\NotEmpty as NotEmptyValidator;
use Laminas\Validator\Regex as RegexValidator;

/**
* Class Mage_Admin_Model_Variable
*
Expand All @@ -39,16 +42,17 @@ protected function _construct()
/**
* @return array|bool
* @throws Exception
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = [];

if (!Zend_Validate::is($this->getVariableName(), 'NotEmpty')) {
$notEmptyValidator = new NotEmptyValidator();
if (!$notEmptyValidator->isValid($this->getVariableName())) {
$errors[] = Mage::helper('adminhtml')->__('Variable Name is required field.');
}
if (!Zend_Validate::is($this->getVariableName(), 'Regex', ['/^[-_a-zA-Z0-9\/]*$/'])) {
$regexValidator = new RegexValidator(['pattern' => '/^[-_a-zA-Z0-9\/]*$/']);
if (!$regexValidator->isValid($this->getVariableName())) {
$errors[] = Mage::helper('adminhtml')->__('Variable Name is incorrect.');
}

Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Adminhtml/Model/Email/PathValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\AbstractValidator;

/**
* Validator for Email Template
*
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Model_Email_PathValidator extends Zend_Validate_Abstract
class Mage_Adminhtml_Model_Email_PathValidator extends AbstractValidator
{
/**
* Returns true if and only if $value meets the validation requirements
Expand Down
10 changes: 6 additions & 4 deletions app/code/core/Mage/Adminhtml/Model/LayoutUpdate/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\AbstractValidator;

/**
* Validator for custom layout update
*
Expand All @@ -21,7 +23,7 @@
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Model_LayoutUpdate_Validator extends Zend_Validate_Abstract
class Mage_Adminhtml_Model_LayoutUpdate_Validator extends AbstractValidator
{
public const XML_INVALID = 'invalidXml';
public const INVALID_TEMPLATE_PATH = 'invalidTemplatePath';
Expand Down Expand Up @@ -103,7 +105,7 @@ protected function _initValidator()
$this->_disallowedBlock = $this->_validator->getDisallowedBlocks();
$this->_protectedExpressions = $this->_validator->getProtectedExpressions();
$this->_disallowedXPathExpressions = $this->_validator->getDisallowedXpathValidationExpression();
$this->_validator->setMessages($this->_messageTemplates);
$this->_validator->setMessages($this->messageTemplates);
}

/**
Expand All @@ -113,8 +115,8 @@ protected function _initValidator()
*/
protected function _initMessageTemplates()
{
if (!$this->_messageTemplates) {
$this->_messageTemplates = [
if (!count($this->messageTemplates)) {
$this->messageTemplates = [
self::PROTECTED_ATTR_HELPER_IN_TAG_ACTION_VAR =>
Mage::helper('adminhtml')->__('Helper attributes should not be used in custom layout updates.'),
self::XML_INVALID => Mage::helper('adminhtml')->__('XML data is invalid.'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\EmailAddress as EmailAddressValidator;

/**
* System config email field backend model
*
Expand All @@ -24,7 +26,8 @@ class Mage_Adminhtml_Model_System_Config_Backend_Email_Address extends Mage_Core
protected function _beforeSave()
{
$value = $this->getValue();
if (!Zend_Validate::is($value, 'EmailAddress')) {
$emailAddressValidator = new EmailAddressValidator();
if (!$emailAddressValidator->isValid($value)) {
Mage::throwException(Mage::helper('adminhtml')->__('Invalid email address "%s".', $value));
}
return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\Regex as RegexValidator;

/**
* Catalog product attribute controller
*
Expand Down Expand Up @@ -206,7 +208,7 @@ public function saveAction()

//validate attribute_code
if (isset($data['attribute_code'])) {
$validatorAttrCode = new Zend_Validate_Regex(['pattern' => '/^(?!event$)[a-z][a-z_0-9]{1,254}$/']);
$validatorAttrCode = new RegexValidator(['pattern' => '/^(?!event$)[a-z][a-z_0-9]{1,254}$/']);
if (!$validatorAttrCode->isValid($data['attribute_code'])) {
$session->addError(
Mage::helper('catalog')->__('Attribute code is invalid. Please use only letters (a-z), numbers (0-9) or underscore(_) in this field, first character should be a letter. Do not use "event" for an attribute code.')
Expand Down
6 changes: 4 additions & 2 deletions app/code/core/Mage/Adminhtml/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\EmailAddress as EmailAddressValidator;

/**
* Index admin controller
*
Expand Down Expand Up @@ -218,8 +220,8 @@ public function forgotpasswordAction()

if ($this->_validateFormKey()) {
if (!empty($email)) {
// Validate received data to be an email address
if (Zend_Validate::is($email, 'EmailAddress')) {
$emailAddressValidator = new EmailAddressValidator();
if ($emailAddressValidator->isValid($email)) {
$collection = Mage::getResourceModel('admin/user_collection');
/** @var Mage_Admin_Model_Resource_User_Collection $collection */
$collection->addFieldToFilter('email', $email);
Expand Down
15 changes: 10 additions & 5 deletions app/code/core/Mage/Api/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\EmailAddress as EmailAddressValidator;
use Laminas\Validator\NotEmpty as NotEmptyValidator;

/**
* Api model
*
Expand Down Expand Up @@ -361,25 +364,27 @@ protected function _getHelper($helperName)
* Validate user attribute values.
*
* @return array|true
* @throws Zend_Validate_Exception
*/
public function validate()
{
$errors = new ArrayObject();

if (!Zend_Validate::is($this->getUsername(), 'NotEmpty')) {
$emailAddressValidator = new EmailAddressValidator();
$notEmptyValidator = new NotEmptyValidator();

if (!$notEmptyValidator->isValid($this->getUsername())) {
$errors->append($this->_getHelper('api')->__('User Name is required field.'));
}

if (!Zend_Validate::is($this->getFirstname(), 'NotEmpty')) {
if (!$notEmptyValidator->isValid($this->getFirstname())) {
$errors->append($this->_getHelper('api')->__('First Name is required field.'));
}

if (!Zend_Validate::is($this->getLastname(), 'NotEmpty')) {
if (!$notEmptyValidator->isValid($this->getLastname())) {
$errors->append($this->_getHelper('api')->__('Last Name is required field.'));
}

if (!Zend_Validate::is($this->getEmail(), 'EmailAddress')) {
if (!$emailAddressValidator->isValid($this->getEmail())) {
$errors->append($this->_getHelper('api')->__('Please enter a valid email.'));
}

Expand Down
5 changes: 3 additions & 2 deletions app/code/core/Mage/Api2/Model/Resource/Validator/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\NotEmpty as NotEmptyValidator;

/**
* API2 Fields Validator
*
Expand Down Expand Up @@ -146,7 +148,7 @@ public function isValidData(array $data, $isPartial = false)

// required fields
if (!$isPartial && count($this->_requiredFields) > 0) {
$notEmptyValidator = new Zend_Validate_NotEmpty();
$notEmptyValidator = new NotEmptyValidator();
foreach ($this->_requiredFields as $requiredField) {
if (!$notEmptyValidator->isValid($data[$requiredField] ?? null)) {
$isValid = false;
Expand All @@ -160,7 +162,6 @@ public function isValidData(array $data, $isPartial = false)
// fields rules
foreach ($data as $field => $value) {
if (isset($this->_validators[$field])) {
/** @var Zend_Validate_Interface $validator */
$validator = $this->_validators[$field];
if (!$validator->isValid($value)) {
$isValid = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\Between as BetweenValidator;
use Laminas\Validator\StringLength as StringLengthValidator;

/**
* API2 catalog_product Validator
*
Expand Down Expand Up @@ -132,8 +135,9 @@ protected function _validateAttributes($data, $productEntity)
$this->_critical('Missing "type_id" in request.', Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
}
// Validate weight
$betweenValidator = new BetweenValidator(['min' => 0, 'max' => self::MAX_DECIMAL_VALUE]);
if (isset($data['weight']) && !empty($data['weight']) && $data['weight'] > 0
&& !Zend_Validate::is($data['weight'], 'Between', [0, self::MAX_DECIMAL_VALUE])
&& !$betweenValidator->isValid($data['weight'])
) {
$this->_addError('The "weight" value is not within the specified range.');
}
Expand Down Expand Up @@ -278,7 +282,9 @@ protected function _validateSku($data)
if ($this->_isUpdate() && !isset($data['sku'])) {
return true;
}
if (!Zend_Validate::is((string)$data['sku'], 'StringLength', ['min' => 0, 'max' => 64])) {

$stringLengthValidator = new StringLengthValidator(['min' => 0, 'max' => 64]);
if (!$stringLengthValidator->isValid((string)$data['sku'])) {
$this->_addError('SKU length should be 64 characters maximum.');
}
}
Expand Down
5 changes: 4 additions & 1 deletion app/code/core/Mage/Checkout/Model/Type/Onepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\EmailAddress as EmailAddressValidator;

/**
* One page checkout processing model
*
Expand Down Expand Up @@ -508,7 +510,8 @@ protected function _processValidateCustomer(Mage_Sales_Model_Quote_Address $addr
}
} elseif (self::METHOD_GUEST == $this->getQuote()->getCheckoutMethod()) {
$email = $address->getData('email');
if (!Zend_Validate::is($email, 'EmailAddress')) {
$emailAddressValidator = new EmailAddressValidator();
if (!$emailAddressValidator->isValid($email)) {
return [
'error' => -1,
'message' => Mage::helper('checkout')->__('Invalid email address "%s"', $email)
Expand Down
12 changes: 9 additions & 3 deletions app/code/core/Mage/Contacts/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

use Laminas\Validator\EmailAddress as EmailAddressValidator;
use Laminas\Validator\NotEmpty as NotEmptyValidator;

/**
* Contacts index controller
*
Expand Down Expand Up @@ -63,15 +66,18 @@ public function postAction()

$error = false;

if (!Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
$emailAddressValidator = new EmailAddressValidator();
$notEmptyValidator = new NotEmptyValidator();

if (!$notEmptyValidator->isValid(trim($post['name']))) {
$error = true;
}

if (!Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
if (!$notEmptyValidator->isValid(trim($post['comment']))) {
$error = true;
}

if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
if (!$emailAddressValidator->isValid(trim($post['email']))) {
$error = true;
}

Expand Down
Loading