Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [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          $mform->addHelpButton('name', 'name', 'url');
  42          if (!empty($CFG->formatstringstriptags)) {
  43              $mform->setType('name', PARAM_TEXT);
  44          } else {
  45              $mform->setType('name', PARAM_CLEANHTML);
  46          }
  47          $mform->addRule('name', null, 'required', null, 'client');
  48          $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  49          $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), array('usefilepicker'=>true));
  50          $mform->setType('externalurl', PARAM_RAW_TRIMMED);
  51          $mform->addRule('externalurl', null, 'required', null, 'client');
  52          $this->standard_intro_elements();
  53          $element = $mform->getElement('introeditor');
  54          $attributes = $element->getAttributes();
  55          $attributes['rows'] = 5;
  56          $element->setAttributes($attributes);
  57          //-------------------------------------------------------
  58          $mform->addElement('header', 'optionssection', get_string('appearance'));
  59  
  60          if ($this->current->instance) {
  61              $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
  62          } else {
  63              $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
  64          }
  65          if (count($options) == 1) {
  66              $mform->addElement('hidden', 'display');
  67              $mform->setType('display', PARAM_INT);
  68              reset($options);
  69              $mform->setDefault('display', key($options));
  70          } else {
  71              $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
  72              $mform->setDefault('display', $config->display);
  73              $mform->addHelpButton('display', 'displayselect', 'url');
  74          }
  75  
  76          if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
  77              $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
  78              if (count($options) > 1) {
  79                  $mform->hideIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  80              }
  81              $mform->setType('popupwidth', PARAM_INT);
  82              $mform->setDefault('popupwidth', $config->popupwidth);
  83  
  84              $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
  85              if (count($options) > 1) {
  86                  $mform->hideIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
  87              }
  88              $mform->setType('popupheight', PARAM_INT);
  89              $mform->setDefault('popupheight', $config->popupheight);
  90          }
  91  
  92          if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
  93            array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
  94            array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
  95              $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
  96              $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
  97              $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
  98              $mform->hideIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
  99              $mform->setDefault('printintro', $config->printintro);
 100          }
 101  
 102          //-------------------------------------------------------
 103          $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
 104          $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
 105  
 106          if (empty($this->current->parameters)) {
 107              $parcount = 5;
 108          } else {
 109              $parcount = 5 + count((array) unserialize_array($this->current->parameters));
 110              $parcount = ($parcount > 100) ? 100 : $parcount;
 111          }
 112          $options = url_get_variable_options($config);
 113  
 114          for ($i=0; $i < $parcount; $i++) {
 115              $parameter = "parameter_$i";
 116              $variable  = "variable_$i";
 117              $pargroup = "pargoup_$i";
 118              $group = array(
 119                  $mform->createElement('text', $parameter, '', array('size'=>'12')),
 120                  $mform->createElement('selectgroups', $variable, '', $options),
 121              );
 122              $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
 123              $mform->setType($parameter, PARAM_RAW);
 124          }
 125  
 126          //-------------------------------------------------------
 127          $this->standard_coursemodule_elements();
 128  
 129          //-------------------------------------------------------
 130          $this->add_action_buttons();
 131      }
 132  
 133      function data_preprocessing(&$default_values) {
 134          if (!empty($default_values['displayoptions'])) {
 135              $displayoptions = (array) unserialize_array($default_values['displayoptions']);
 136              if (isset($displayoptions['printintro'])) {
 137                  $default_values['printintro'] = $displayoptions['printintro'];
 138              }
 139              if (!empty($displayoptions['popupwidth'])) {
 140                  $default_values['popupwidth'] = $displayoptions['popupwidth'];
 141              }
 142              if (!empty($displayoptions['popupheight'])) {
 143                  $default_values['popupheight'] = $displayoptions['popupheight'];
 144              }
 145          }
 146          if (!empty($default_values['parameters'])) {
 147              $parameters = (array) unserialize_array($default_values['parameters']);
 148              $i = 0;
 149              foreach ($parameters as $parameter=>$variable) {
 150                  $default_values['parameter_'.$i] = $parameter;
 151                  $default_values['variable_'.$i]  = $variable;
 152                  $i++;
 153              }
 154          }
 155      }
 156  
 157      function validation($data, $files) {
 158          $errors = parent::validation($data, $files);
 159  
 160          // Validating Entered url, we are looking for obvious problems only,
 161          // teachers are responsible for testing if it actually works.
 162  
 163          // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
 164  
 165          // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
 166  
 167          if (!empty($data['externalurl'])) {
 168              $url = $data['externalurl'];
 169              if (preg_match('|^/|', $url)) {
 170                  // links relative to server root are ok - no validation necessary
 171  
 172              } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
 173                  // normal URL
 174                  if (!url_appears_valid_url($url)) {
 175                      $errors['externalurl'] = get_string('invalidurl', 'url');
 176                  }
 177  
 178              } else if (preg_match('|^[a-z]+:|i', $url)) {
 179                  // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
 180                  // we do not validate these at all, sorry
 181  
 182              } else {
 183                  // invalid URI, we try to fix it by adding 'http://' prefix,
 184                  // relative links are NOT allowed because we display the link on different pages!
 185                  if (!url_appears_valid_url('http://'.$url)) {
 186                      $errors['externalurl'] = get_string('invalidurl', 'url');
 187                  }
 188              }
 189          }
 190          return $errors;
 191      }
 192  
 193  }