[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> PasswordField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 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\Field;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Form Field class for the Joomla Platform.
  22   * Text field for passwords
  23   *
  24   * @link   https://html.spec.whatwg.org/multipage/input.html#password-state-(type=password)
  25   * @note   Two password fields may be validated as matching using \Joomla\CMS\Form\Rule\EqualsRule
  26   * @since  1.7.0
  27   */
  28  class PasswordField extends FormField
  29  {
  30      /**
  31       * The form field type.
  32       *
  33       * @var    string
  34       * @since  1.7.0
  35       */
  36      protected $type = 'Password';
  37  
  38      /**
  39       * The threshold of password field.
  40       *
  41       * @var    integer
  42       * @since  3.2
  43       */
  44      protected $threshold = 66;
  45  
  46      /**
  47       * The allowable maxlength of password.
  48       *
  49       * @var    integer
  50       * @since  3.2
  51       */
  52      protected $maxLength;
  53  
  54      /**
  55       * Whether to attach a password strength meter or not.
  56       *
  57       * @var    boolean
  58       * @since  3.2
  59       */
  60      protected $meter = false;
  61  
  62      /**
  63       * Whether to attach a password strength meter or not.
  64       *
  65       * @var    boolean
  66       * @since  4.0.0
  67       */
  68      protected $force = false;
  69  
  70      /**
  71       * Name of the layout being used to render the field
  72       *
  73       * @var    string
  74       * @since  3.7
  75       */
  76      protected $layout = 'joomla.form.field.password';
  77  
  78      /**
  79       * Attach an unlock button and disable the input field,
  80       * also remove the value from the output.
  81       *
  82       * @var    boolean
  83       * @since  3.9.24
  84       */
  85      protected $lock = false;
  86  
  87      /**
  88       * Method to get certain otherwise inaccessible properties from the form field object.
  89       *
  90       * @param   string  $name  The property name for which to get the value.
  91       *
  92       * @return  mixed  The property value or null.
  93       *
  94       * @since   3.2
  95       */
  96      public function __get($name)
  97      {
  98          switch ($name) {
  99              case 'lock':
 100              case 'threshold':
 101              case 'maxLength':
 102              case 'meter':
 103              case 'force':
 104                  return $this->$name;
 105          }
 106  
 107          return parent::__get($name);
 108      }
 109  
 110      /**
 111       * Method to set certain otherwise inaccessible properties of the form field object.
 112       *
 113       * @param   string  $name   The property name for which to set the value.
 114       * @param   mixed   $value  The value of the property.
 115       *
 116       * @return  void
 117       *
 118       * @since   3.2
 119       */
 120      public function __set($name, $value)
 121      {
 122          $value = (string) $value;
 123  
 124          switch ($name) {
 125              case 'maxLength':
 126              case 'threshold':
 127                  $this->$name = $value;
 128                  break;
 129  
 130              case 'lock':
 131              case 'meter':
 132              case 'force':
 133                  $this->$name = ($value === 'true' || $value === $name || $value === '1');
 134                  break;
 135  
 136              default:
 137                  parent::__set($name, $value);
 138          }
 139      }
 140  
 141      /**
 142       * Method to attach a Form object to the field.
 143       *
 144       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 145       * @param   mixed              $value    The form field value to validate.
 146       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 147       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 148       *                                       full field name would end up being "bar[foo]".
 149       *
 150       * @return  boolean  True on success.
 151       *
 152       * @see     FormField::setup()
 153       * @since   3.2
 154       */
 155      public function setup(\SimpleXMLElement $element, $value, $group = null)
 156      {
 157          $return = parent::setup($element, $value, $group);
 158  
 159          if ($return) {
 160              $lock               = (string) $this->element['lock'];
 161              $this->lock         = ($lock === 'true' || $lock === 'on' || $lock === '1');
 162              $this->maxLength    = $this->element['maxlength'] ? (int) $this->element['maxlength'] : 99;
 163              $this->threshold    = $this->element['threshold'] ? (int) $this->element['threshold'] : 66;
 164              $meter              = (string) $this->element['strengthmeter'];
 165              $this->meter        = ($meter === 'true' || $meter === 'on' || $meter === '1');
 166              $force              = (string) $this->element['forcePassword'];
 167              $this->force        = (($force === 'true' || $force === 'on' || $force === '1') && $this->meter === true);
 168              $rules              = (string) $this->element['rules'];
 169              $this->rules        = (($rules === 'true' || $rules === 'on' || $rules === '1') && $this->meter === true);
 170  
 171              // Set some initial values
 172              $this->minLength    = 12;
 173              $this->minIntegers  = 0;
 174              $this->minSymbols   = 0;
 175              $this->minUppercase = 0;
 176              $this->minLowercase = 0;
 177  
 178              if (Factory::getApplication()->get('db') != '') {
 179                  $this->minLength    = (int) ComponentHelper::getParams('com_users')->get('minimum_length', 12);
 180                  $this->minIntegers  = (int) ComponentHelper::getParams('com_users')->get('minimum_integers', 0);
 181                  $this->minSymbols   = (int) ComponentHelper::getParams('com_users')->get('minimum_symbols', 0);
 182                  $this->minUppercase = (int) ComponentHelper::getParams('com_users')->get('minimum_uppercase', 0);
 183                  $this->minLowercase = (int) ComponentHelper::getParams('com_users')->get('minimum_lowercase', 0);
 184              }
 185          }
 186  
 187          return $return;
 188      }
 189  
 190      /**
 191       * Method to get the field input markup for password.
 192       *
 193       * @return  string  The field input markup.
 194       *
 195       * @since   1.7.0
 196       */
 197      protected function getInput()
 198      {
 199          // Trim the trailing line in the layout file
 200          return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
 201      }
 202  
 203      /**
 204       * Method to get the data to be passed to the layout for rendering.
 205       *
 206       * @return  array
 207       *
 208       * @since 3.7
 209       */
 210      protected function getLayoutData()
 211      {
 212          $data = parent::getLayoutData();
 213  
 214          // Initialize some field attributes.
 215          $extraData = array(
 216              'lock'           => $this->lock,
 217              'maxLength'      => $this->maxLength,
 218              'meter'          => $this->meter,
 219              'threshold'      => $this->threshold,
 220              'minLength'      => $this->minLength,
 221              'minIntegers'    => $this->minIntegers,
 222              'minSymbols'     => $this->minSymbols,
 223              'minUppercase'   => $this->minUppercase,
 224              'minLowercase'   => $this->minLowercase,
 225              'forcePassword'  => $this->force,
 226              'rules'          => $this->rules,
 227          );
 228  
 229          return array_merge($data, $extraData);
 230      }
 231  }


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