Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * URL configuration form
  20   *
  21   * @package    mod_url
  22   * @copyright  2009 Petr Skoda  {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  29  require_once($CFG->dirroot.'/mod/url/locallib.php');
  30  
  31  class mod_url_mod_form extends moodleform_mod {
  32      function definition() {
  33          global $CFG, $DB;
  34          $mform = $this->_form;
  35  
  36          $config = get_config('url');
  37  
  38          //-------------------------------------------------------
  39          $mform->addElement('header', 'general', get_string('general', 'form'));
  40          $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
  41          if (!empty($CFG->formatstringstriptags)) {
  42              $mform->setType('name', PARAM_TEXT);
  43          } else {
  44              $mform->setType('name', PARAM_CLEANHTML);
  45          }
  46          $mform->addRule('name', null, 'required', null, 'client');
  47          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  48          $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
  49          $mform->setType('externalurl', PARAM_RAW_TRIMMED);
  50          $mform->addRule('externalurl', null, 'required', null, 'client');
  51          $this->standard_intro_elements();
  52          $element = $mform->getElement('introeditor');
  53          $attributes = $element->getAttributes();
  54          $attributes['rows'] = 5;
  55          $element->setAttributes($attributes);
  56          //-------------------------------------------------------
  57          $mform->addElement('header', 'optionssection', get_string('appearance'));
  58  
  59          if ($this->current->instance) {
  60              $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
  61          } else {
  62              $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
  63          }
  64          if (count($options) == 1) {
  65              $mform->addElement('hidden', 'display');
  66              $mform->setType('display', PARAM_INT);
  67              reset($options);
  68              $mform->setDefault('display', key($options));
  69          } else {
  70              $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
  71              $mform->setDefault('display', $config->display);
  72              $mform->addHelpButton('display', 'displayselect', 'url');
  73          }
  74  
  75          if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  76              $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
  77              if (count($options) > 1) {
  78                  $mform->hideIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  79              }
  80              $mform->setType('popupwidth', PARAM_INT);
  81              $mform->setDefault('popupwidth', $config->popupwidth);
  82  
  83              $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
  84              if (count($options) > 1) {
  85                  $mform->hideIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  86              }
  87              $mform->setType('popupheight', PARAM_INT);
  88              $mform->setDefault('popupheight', $config->popupheight);
  89          }
  90  
  91          if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  92            array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  93            array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  94              $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
  95              $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  96              $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  97              $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  98              $mform->setDefault('printintro', $config->printintro);
  99          }
 100  
 101          //-------------------------------------------------------
 102          $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
 103          $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
 104  
 105          if (empty($this->current->parameters)) {
 106              $parcount = 5;
 107          } else {
 108              $parcount = 5 + count((array) unserialize_array($this->current->parameters));
 109              $parcount = ($parcount > 100) ? 100 : $parcount;
 110          }
 111          $options = url_get_variable_options($config);
 112  
 113          for ($i=0; $i < $parcount; $i++) {
 114              $parameter = "parameter_$i";
 115              $variable  = "variable_$i";
 116              $pargroup = "pargoup_$i";
 117              $group = array(
 118                  $mform->createElement('text', $parameter, '', array('size'=>'12')),
 119                  $mform->createElement('selectgroups', $variable, '', $options),
 120              );
 121              $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
 122              $mform->setType($parameter, PARAM_RAW);
 123          }
 124  
 125          //-------------------------------------------------------
 126          $this->standard_coursemodule_elements();
 127  
 128          //-------------------------------------------------------
 129          $this->add_action_buttons();
 130      }
 131  
 132      function data_preprocessing(&$default_values) {
 133          if (!empty($default_values['displayoptions'])) {
 134              $displayoptions = (array) unserialize_array($default_values['displayoptions']);
 135              if (isset($displayoptions['printintro'])) {
 136                  $default_values['printintro'] = $displayoptions['printintro'];
 137              }
 138              if (!empty($displayoptions['popupwidth'])) {
 139                  $default_values['popupwidth'] = $displayoptions['popupwidth'];
 140              }
 141              if (!empty($displayoptions['popupheight'])) {
 142                  $default_values['popupheight'] = $displayoptions['popupheight'];
 143              }
 144          }
 145          if (!empty($default_values['parameters'])) {
 146              $parameters = (array) unserialize_array($default_values['parameters']);
 147              $i = 0;
 148              foreach ($parameters as $parameter=>$variable) {
 149                  $default_values['parameter_'.$i] = $parameter;
 150                  $default_values['variable_'.$i]  = $variable;
 151                  $i++;
 152              }
 153          }
 154      }
 155  
 156      function validation($data, $files) {
 157          $errors = parent::validation($data, $files);
 158  
 159          // Validating Entered url, we are looking for obvious problems only,
 160          // teachers are responsible for testing if it actually works.
 161  
 162          // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
 163  
 164          // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
 165  
 166          if (!empty($data['externalurl'])) {
 167              $url = $data['externalurl'];
 168              if (preg_match('|^/|', $url)) {
 169                  // links relative to server root are ok - no validation necessary
 170  
 171              } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
 172                  // normal URL
 173                  if (!url_appears_valid_url($url)) {
 174                      $errors['externalurl'] = get_string('invalidurl', 'url');
 175                  }
 176  
 177              } else if (preg_match('|^[a-z]+:|i', $url)) {
 178                  // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
 179                  // we do not validate these at all, sorry
 180  
 181              } else {
 182                  // invalid URI, we try to fix it by adding 'http://' prefix,
 183                  // relative links are NOT allowed because we display the link on different pages!
 184                  if (!url_appears_valid_url('http://'.$url)) {
 185                      $errors['externalurl'] = get_string('invalidurl', 'url');
 186                  }
 187              }
 188          }
 189          return $errors;
 190      }
 191  
 192  }