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