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 Field class for the Joomla Platform.
  14.  * Text field for passwords
  15.  *
  16.  * @package     Joomla.Platform
  17.  * @subpackage  Form
  18.  * @link        http://www.w3.org/TR/html-markup/input.password.html#input.password
  19.  * @note        Two password fields may be validated as matching using JFormRuleEquals
  20.  * @since       11.1
  21.  */
  22. {
  23.     /**
  24.      * The form field type.
  25.      *
  26.      * @var    string 
  27.      * @since  11.1
  28.      */
  29.     protected $type = 'Password';
  30.  
  31.     /**
  32.      * The threshold of password field.
  33.      *
  34.      * @var    integer 
  35.      * @since  3.2
  36.      */
  37.     protected $threshold = 66;
  38.  
  39.     /**
  40.      * The allowable maxlength of password.
  41.      *
  42.      * @var    integer 
  43.      * @since  3.2
  44.      */
  45.     protected $maxLength;
  46.  
  47.     /**
  48.      * Whether to attach a password strength meter or not.
  49.      *
  50.      * @var    boolean 
  51.      * @since  3.2
  52.      */
  53.     protected $meter = false;
  54.  
  55.     /**
  56.      * Method to get certain otherwise inaccessible properties from the form field object.
  57.      *
  58.      * @param   string  $name  The property name for which to the the value.
  59.      *
  60.      * @return  mixed  The property value or null.
  61.      *
  62.      * @since   3.2
  63.      */
  64.     public function __get($name)
  65.     {
  66.         switch ($name)
  67.         {
  68.             case 'threshold':
  69.             case 'maxLength':
  70.             case 'meter':
  71.                 return $this->$name;
  72.         }
  73.  
  74.         return parent::__get($name);
  75.     }
  76.  
  77.     /**
  78.      * Method to set certain otherwise inaccessible properties of the form field object.
  79.      *
  80.      * @param   string  $name   The property name for which to the the value.
  81.      * @param   mixed   $value  The value of the property.
  82.      *
  83.      * @return  void 
  84.      *
  85.      * @since   3.2
  86.      */
  87.     public function __set($name, $value)
  88.     {
  89.         $value = (string) $value;
  90.  
  91.         switch ($name)
  92.         {
  93.             case 'maxLength':
  94.             case 'threshold':
  95.                 $this->$name = $value;
  96.                 break;
  97.  
  98.             case 'meter':
  99.                 $this->$meter = ($value === 'true' || $value === $name || $value === '1');
  100.                 break;
  101.  
  102.             default:
  103.                 parent::__set($name, $value);
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * Method to attach a JForm object to the field.
  109.      *
  110.      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
  111.      * @param   mixed             $value    The form field value to validate.
  112.      * @param   string            $group    The field name group control value. This acts as as an array container for the field.
  113.      *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  114.      *                                       full field name would end up being "bar[foo]".
  115.      *
  116.      * @return  boolean  True on success.
  117.      *
  118.      * @see     JFormField::setup()
  119.      * @since   3.2
  120.      */
  121.     public function setup(SimpleXMLElement $element, $value, $group = null)
  122.     {
  123.         $return = parent::setup($element, $value, $group);
  124.  
  125.         if ($return)
  126.         {
  127.             $this->maxLength = $this->element['maxlength'] ? (int) $this->element['maxlength'] : 99;
  128.             $this->threshold = $this->element['threshold'] ? (int) $this->element['threshold'] : 66;
  129.  
  130.             $meter       = (string) $this->element['strengthmeter'];
  131.             $this->meter = ($meter == 'true' || $meter == 'on' || $meter == '1');
  132.         }
  133.  
  134.         return $return;
  135.     }
  136.  
  137.     /**
  138.      * Method to get the field input markup for password.
  139.      *
  140.      * @return  string  The field input markup.
  141.      *
  142.      * @since   11.1
  143.      */
  144.     protected function getInput()
  145.     {
  146.         // Translate placeholder text
  147.         $hint = $this->translateHint ? JText::_($this->hint) : $this->hint;
  148.  
  149.         // Initialize some field attributes.
  150.         $size         = !empty($this->size) ? ' size="' . $this->size . '"' : '';
  151.         $maxLength    = !empty($this->maxLength) ? ' maxlength="' . $this->maxLength . '"' : '';
  152.         $class        = !empty($this->class) ? ' class="' . $this->class . '"' : '';
  153.         $readonly     = $this->readonly ? ' readonly' : '';
  154.         $disabled     = $this->disabled ? ' disabled' : '';
  155.         $required     = $this->required ? ' required aria-required="true"' : '';
  156.         $hint         = $hint ? ' placeholder="' . $hint . '"' : '';
  157.         $autocomplete = !$this->autocomplete ? ' autocomplete="off"' : '';
  158.         $autofocus    = $this->autofocus ? ' autofocus' : '';
  159.  
  160.         if ($this->meter)
  161.         {
  162.             JHtml::_('script', 'system/passwordstrength.js', true, true);
  163.             $script = 'new Form.PasswordStrength("' . $this->id . '",
  164.                 {
  165.                     threshold: ' . $this->threshold . ',
  166.                     onUpdate: function(element, strength, threshold) {
  167.                         element.set("data-passwordstrength", strength);
  168.                     }
  169.                 }
  170.             );';
  171.  
  172.             // Load script on document load.
  173.             JFactory::getDocument()->addScriptDeclaration(
  174.                 "jQuery(document).ready(function(){" . $script . "});"
  175.             );
  176.         }
  177.  
  178.         // Including fallback code for HTML5 non supported browsers.
  179.         JHtml::_('jquery.framework');
  180.         JHtml::_('script', 'system/html5fallback.js', false, true);
  181.  
  182.         return '<input type="password" name="' . $this->name . '" id="' . $this->id . '"' .
  183.             ' value="' . htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8') . '"' . $hint . $autocomplete .
  184.             $class . $readonly . $disabled . $size . $maxLength . $required . $autofocus . ' />';
  185.     }
  186. }

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