1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 * @link http://www.w3.org/TR/html-markup/input.password.html#input.password
17 * @note Two password fields may be validated as matching using JFormRuleEquals
18 * @since 11.1
19 */
20 class JFormFieldPassword extends JFormField
21 {
22 /**
23 * The form field type.
24 *
25 * @var string
26 * @since 11.1
27 */
28 protected $type = 'Password';
29
30 /**
31 * The threshold of password field.
32 *
33 * @var integer
34 * @since 3.2
35 */
36 protected $threshold = 66;
37
38 /**
39 * The allowable maxlength of password.
40 *
41 * @var integer
42 * @since 3.2
43 */
44 protected $maxLength;
45
46 /**
47 * Whether to attach a password strength meter or not.
48 *
49 * @var boolean
50 * @since 3.2
51 */
52 protected $meter = false;
53
54 /**
55 * Name of the layout being used to render the field
56 *
57 * @var string
58 * @since 3.7
59 */
60 protected $layout = 'joomla.form.field.password';
61
62 /**
63 * Method to get certain otherwise inaccessible properties from the form field object.
64 *
65 * @param string $name The property name for which to the the value.
66 *
67 * @return mixed The property value or null.
68 *
69 * @since 3.2
70 */
71 public function __get($name)
72 {
73 switch ($name)
74 {
75 case 'threshold':
76 case 'maxLength':
77 case 'meter':
78 return $this->$name;
79 }
80
81 return parent::__get($name);
82 }
83
84 /**
85 * Method to set certain otherwise inaccessible properties of the form field object.
86 *
87 * @param string $name The property name for which to the the value.
88 * @param mixed $value The value of the property.
89 *
90 * @return void
91 *
92 * @since 3.2
93 */
94 public function __set($name, $value)
95 {
96 $value = (string) $value;
97
98 switch ($name)
99 {
100 case 'maxLength':
101 case 'threshold':
102 $this->$name = $value;
103 break;
104
105 case 'meter':
106 $this->meter = ($value === 'true' || $value === $name || $value === '1');
107 break;
108
109 default:
110 parent::__set($name, $value);
111 }
112 }
113
114 /**
115 * Method to attach a JForm object to the field.
116 *
117 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
118 * @param mixed $value The form field value to validate.
119 * @param string $group The field name group control value. This acts as an array container for the field.
120 * For example if the field has name="foo" and the group value is set to "bar" then the
121 * full field name would end up being "bar[foo]".
122 *
123 * @return boolean True on success.
124 *
125 * @see JFormField::setup()
126 * @since 3.2
127 */
128 public function setup(SimpleXMLElement $element, $value, $group = null)
129 {
130 $return = parent::setup($element, $value, $group);
131
132 if ($return)
133 {
134 $this->maxLength = $this->element['maxlength'] ? (int) $this->element['maxlength'] : 99;
135 $this->threshold = $this->element['threshold'] ? (int) $this->element['threshold'] : 66;
136
137 $meter = (string) $this->element['strengthmeter'];
138 $this->meter = ($meter == 'true' || $meter == 'on' || $meter == '1');
139 }
140
141 return $return;
142 }
143
144 /**
145 * Method to get the field input markup for password.
146 *
147 * @return string The field input markup.
148 *
149 * @since 11.1
150 */
151 protected function getInput()
152 {
153 // Trim the trailing line in the layout file
154 return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
155 }
156
157 /**
158 * Method to get the data to be passed to the layout for rendering.
159 *
160 * @return array
161 *
162 * @since 3.7
163 */
164 protected function getLayoutData()
165 {
166 $data = parent::getLayoutData();
167
168 // Initialize some field attributes.
169 $extraData = array(
170 'maxLength' => $this->maxLength,
171 'meter' => $this->meter,
172 'threshold' => $this->threshold,
173 );
174
175 return array_merge($data, $extraData);
176 }
177 }
178