Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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          // 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" ';
  63              }
  64          }
  65          $this->_persistantFreeze = true;
  66  
  67          parent::__construct($elementName, $elementLabel, $attributes);
  68          $this->setType('passwordunmask');
  69      }
  70  
  71      /**
  72       * Old syntax of class constructor. Deprecated in PHP7.
  73       *
  74       * @deprecated since Moodle 3.1
  75       */
  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);
  79      }
  80  
  81      /**
  82       * Function to export the renderer data in a format that is suitable for a mustache template.
  83       *
  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
  86       */
  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');
  90  
  91          return $context;
  92      }
  93  
  94      /**
  95       * Check that there is no whitespace at the beginning and end of the password.
  96       *
  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.
  99       *
 100       * @param array $value Submitted value.
 101       * @return string|null Validation error message or null.
 102       */
 103      public function validateSubmitValue($value) {
 104          if ($value !== null && $value !== trim($value)) {
 105              return get_string('err_wrappingwhitespace', 'core_form');
 106          }
 107  
 108          return;
 109      }
 110  }