Source for file password.php

Documentation is available at password.php

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

Documentation generated on Tue, 19 Nov 2013 15:10:19 +0100 by phpDocumentor 1.4.3