[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Rule/ -> PasswordRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Rule;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Form\FormRule;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\Registry\Registry;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Form Rule class for the Joomla Platform.
  25   *
  26   * @since  3.1.2
  27   */
  28  class PasswordRule extends FormRule
  29  {
  30      /**
  31       * Method to test if two values are not equal. To use this rule, the form
  32       * XML needs a validate attribute of equals and a field attribute
  33       * that is equal to the field to test against.
  34       *
  35       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  36       * @param   mixed              $value    The form field value to validate.
  37       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  38       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  39       *                                       full field name would end up being "bar[foo]".
  40       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  41       * @param   Form               $form     The form object for which the field is being tested.
  42       *
  43       * @return  boolean  True if the value is valid, false otherwise.
  44       *
  45       * @since   3.1.2
  46       * @throws  \InvalidArgumentException
  47       * @throws  \UnexpectedValueException
  48       */
  49      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  50      {
  51          $meter            = isset($element['strengthmeter']) ? ' meter="0"' : '1';
  52          $threshold        = isset($element['threshold']) ? (int) $element['threshold'] : 66;
  53          $minimumLength    = isset($element['minimum_length']) ? (int) $element['minimum_length'] : 12;
  54          $minimumIntegers  = isset($element['minimum_integers']) ? (int) $element['minimum_integers'] : 0;
  55          $minimumSymbols   = isset($element['minimum_symbols']) ? (int) $element['minimum_symbols'] : 0;
  56          $minimumUppercase = isset($element['minimum_uppercase']) ? (int) $element['minimum_uppercase'] : 0;
  57          $minimumLowercase = isset($element['minimum_lowercase']) ? (int) $element['minimum_lowercase'] : 0;
  58  
  59          // In the installer we don't have any access to the
  60          // database yet so use the hard coded default settings
  61          if (!Factory::getApplication()->isClient('installation')) {
  62              // If we have parameters from com_users, use those instead.
  63              // Some of these may be empty for legacy reasons.
  64              $params = ComponentHelper::getParams('com_users');
  65  
  66              if (!empty($params)) {
  67                  $minimumLengthp    = $params->get('minimum_length', 12);
  68                  $minimumIntegersp  = $params->get('minimum_integers', 0);
  69                  $minimumSymbolsp   = $params->get('minimum_symbols', 0);
  70                  $minimumUppercasep = $params->get('minimum_uppercase', 0);
  71                  $minimumLowercasep = $params->get('minimum_lowercase', 0);
  72                  $meterp            = $params->get('meter');
  73                  $thresholdp        = $params->get('threshold', 66);
  74  
  75                  empty($minimumLengthp) ? : $minimumLength = (int) $minimumLengthp;
  76                  empty($minimumIntegersp) ? : $minimumIntegers = (int) $minimumIntegersp;
  77                  empty($minimumSymbolsp) ? : $minimumSymbols = (int) $minimumSymbolsp;
  78                  empty($minimumUppercasep) ? : $minimumUppercase = (int) $minimumUppercasep;
  79                  empty($minimumLowercasep) ? : $minimumLowercase = (int) $minimumLowercasep;
  80                  empty($meterp) ? : $meter = $meterp;
  81                  empty($thresholdp) ? : $threshold = $thresholdp;
  82              }
  83          }
  84  
  85          // If the field is empty and not required, the field is valid.
  86          $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required');
  87  
  88          if (!$required && empty($value)) {
  89              return true;
  90          }
  91  
  92          $valueLength = \strlen($value);
  93  
  94          // We set a maximum length to prevent abuse since it is unfiltered.
  95          if ($valueLength > 4096) {
  96              Factory::getApplication()->enqueueMessage(Text::_('JFIELD_PASSWORD_TOO_LONG'), 'error');
  97          }
  98  
  99          // We don't allow white space inside passwords
 100          $valueTrim = trim($value);
 101  
 102          // Set a variable to check if any errors are made in password
 103          $validPassword = true;
 104  
 105          if (\strlen($valueTrim) !== $valueLength) {
 106              Factory::getApplication()->enqueueMessage(
 107                  Text::_('JFIELD_PASSWORD_SPACES_IN_PASSWORD'),
 108                  'error'
 109              );
 110  
 111              $validPassword = false;
 112          }
 113  
 114          // Minimum number of integers required
 115          if (!empty($minimumIntegers)) {
 116              $nInts = preg_match_all('/[0-9]/', $value, $imatch);
 117  
 118              if ($nInts < $minimumIntegers) {
 119                  Factory::getApplication()->enqueueMessage(
 120                      Text::plural('JFIELD_PASSWORD_NOT_ENOUGH_INTEGERS_N', $minimumIntegers),
 121                      'error'
 122                  );
 123  
 124                  $validPassword = false;
 125              }
 126          }
 127  
 128          // Minimum number of symbols required
 129          if (!empty($minimumSymbols)) {
 130              $nsymbols = preg_match_all('[\W]', $value, $smatch);
 131  
 132              if ($nsymbols < $minimumSymbols) {
 133                  Factory::getApplication()->enqueueMessage(
 134                      Text::plural('JFIELD_PASSWORD_NOT_ENOUGH_SYMBOLS_N', $minimumSymbols),
 135                      'error'
 136                  );
 137  
 138                  $validPassword = false;
 139              }
 140          }
 141  
 142          // Minimum number of upper case ASCII characters required
 143          if (!empty($minimumUppercase)) {
 144              $nUppercase = preg_match_all('/[A-Z]/', $value, $umatch);
 145  
 146              if ($nUppercase < $minimumUppercase) {
 147                  Factory::getApplication()->enqueueMessage(
 148                      Text::plural('JFIELD_PASSWORD_NOT_ENOUGH_UPPERCASE_LETTERS_N', $minimumUppercase),
 149                      'error'
 150                  );
 151  
 152                  $validPassword = false;
 153              }
 154          }
 155  
 156          // Minimum number of lower case ASCII characters required
 157          if (!empty($minimumLowercase)) {
 158              $nLowercase = preg_match_all('/[a-z]/', $value, $umatch);
 159  
 160              if ($nLowercase < $minimumLowercase) {
 161                  Factory::getApplication()->enqueueMessage(
 162                      Text::plural('JFIELD_PASSWORD_NOT_ENOUGH_LOWERCASE_LETTERS_N', $minimumLowercase),
 163                      'error'
 164                  );
 165  
 166                  $validPassword = false;
 167              }
 168          }
 169  
 170          // Minimum length option
 171          if (!empty($minimumLength)) {
 172              if (\strlen((string) $value) < $minimumLength) {
 173                  Factory::getApplication()->enqueueMessage(
 174                      Text::plural('JFIELD_PASSWORD_TOO_SHORT_N', $minimumLength),
 175                      'error'
 176                  );
 177  
 178                  $validPassword = false;
 179              }
 180          }
 181  
 182          // If valid has violated any rules above return false.
 183          if (!$validPassword) {
 184              return false;
 185          }
 186  
 187          return true;
 188      }
 189  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer