Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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.
  13  //
  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/>.
  16  
  17  
  18  /**
  19   * Password type form element with unmask option
  20   *
  21   * Contains HTML class for a password type element with unmask option
  22   *
  23   * @package   core_form
  24   * @copyright 2009 Petr Skoda
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  if (!defined('MOODLE_INTERNAL')) {
  29      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  30  }
  31  
  32  global $CFG;
  33  require_once($CFG->libdir.'/form/password.php');
  34  
  35  /**
  36   * Password type form element with unmask option
  37   *
  38   * HTML class for a password type element with unmask option
  39   *
  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
  44   */
  45  class MoodleQuickForm_passwordunmask extends MoodleQuickForm_password {
  46      /**
  47       * constructor
  48       *
  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
  53       */
  54      public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
  55          $this->_persistantFreeze = true;
  56  
  57          parent::__construct($elementName, $elementLabel, $attributes);
  58          $this->setType('passwordunmask');
  59      }
  60  
  61      /**
  62       * Old syntax of class constructor. Deprecated in PHP7.
  63       *
  64       * @deprecated since Moodle 3.1
  65       */
  66      public function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $attributes=null) {
  67          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  68          self::__construct($elementName, $elementLabel, $attributes);
  69      }
  70  
  71      /**
  72       * Function to export the renderer data in a format that is suitable for a mustache template.
  73       *
  74       * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
  75       * @return stdClass|array
  76       */
  77      public function export_for_template(renderer_base $output) {
  78          $context = parent::export_for_template($output);
  79          $context['valuechars'] = array_fill(0, strlen($context['value'] ?? ''), 'x');
  80  
  81          return $context;
  82      }
  83  
  84      /**
  85       * Check that there is no whitespace at the beginning and end of the password.
  86       *
  87       * It turned out that wrapping whitespace can easily be pasted by accident when copying the text from elsewhere.
  88       * Such a mistake is very hard to debug as the whitespace is not displayed.
  89       *
  90       * @param string $value Submitted value.
  91       * @return string|null Validation error message or null.
  92       */
  93      public function validateSubmitValue($value) {
  94          if ($value !== null && $value !== trim($value)) {
  95              return get_string('err_wrappingwhitespace', 'core_form');
  96          }
  97  
  98          return;
  99      }
 100  }