Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / form / passwordunmask.php
blobe90ee4df89fdcd42f4ff3d20c456aefeda232def
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Password type form element with unmask option
21 * Contains HTML class for a password type element with unmask option
23 * @package core_form
24 * @copyright 2009 Petr Skoda
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 if (!defined('MOODLE_INTERNAL')) {
29 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
32 global $CFG;
33 require_once($CFG->libdir.'/form/password.php');
35 /**
36 * Password type form element with unmask option
38 * HTML class for a password type element with unmask option
40 * @package core_form
41 * @category form
42 * @copyright 2009 Petr Skoda {@link http://skodak.org}
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class MoodleQuickForm_passwordunmask extends MoodleQuickForm_password {
46 /**
47 * constructor
49 * @param string $elementName (optional) name of the password element
50 * @param string $elementLabel (optional) label for password element
51 * @param mixed $attributes (optional) Either a typical HTML attribute string
52 * or an associative array
54 public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
55 // no standard mform in moodle should allow autocomplete of passwords
56 if (empty($attributes)) {
57 $attributes = array('autocomplete'=>'off');
58 } else if (is_array($attributes)) {
59 $attributes['autocomplete'] = 'off';
60 } else {
61 if (strpos($attributes, 'autocomplete') === false) {
62 $attributes .= ' autocomplete="off" ';
65 $this->_persistantFreeze = true;
67 parent::__construct($elementName, $elementLabel, $attributes);
68 $this->setType('passwordunmask');
71 /**
72 * Old syntax of class constructor. Deprecated in PHP7.
74 * @deprecated since Moodle 3.1
76 public function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $attributes=null) {
77 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
78 self::__construct($elementName, $elementLabel, $attributes);
81 /**
82 * Function to export the renderer data in a format that is suitable for a mustache template.
84 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
85 * @return stdClass|array
87 public function export_for_template(renderer_base $output) {
88 $context = parent::export_for_template($output);
89 $context['valuechars'] = array_fill(0, strlen($context['value']), 'x');
91 return $context;
94 /**
95 * Check that there is no whitespace at the beginning and end of the password.
97 * It turned out that wrapping whitespace can easily be pasted by accident when copying the text from elsewhere.
98 * Such a mistake is very hard to debug as the whitespace is not displayed.
100 * @param array $value Submitted value.
101 * @return string|null Validation error message or null.
103 public function validateSubmitValue($value) {
104 if ($value !== null && $value !== trim($value)) {
105 return get_string('err_wrappingwhitespace', 'core_form');
108 return;