Merge branch 'MDL-62560-master'
[moodle.git] / mod / url / mod_form.php
blob5fb483d0b4aa18c634fe5bb6498059184f1e24e0
1 <?php
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.
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/>.
18 /**
19 * URL configuration form
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
26 defined('MOODLE_INTERNAL') || die;
28 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
29 require_once($CFG->dirroot.'/mod/url/locallib.php');
31 class mod_url_mod_form extends moodleform_mod {
32 function definition() {
33 global $CFG, $DB;
34 $mform = $this->_form;
36 $config = get_config('url');
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);
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'));
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));
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');
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->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
80 $mform->setType('popupwidth', PARAM_INT);
81 $mform->setDefault('popupwidth', $config->popupwidth);
83 $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
84 if (count($options) > 1) {
85 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
87 $mform->setType('popupheight', PARAM_INT);
88 $mform->setDefault('popupheight', $config->popupheight);
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->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
96 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
97 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
98 $mform->setDefault('printintro', $config->printintro);
101 //-------------------------------------------------------
102 $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
103 $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
105 if (empty($this->current->parameters)) {
106 $parcount = 5;
107 } else {
108 $parcount = 5 + count(unserialize($this->current->parameters));
109 $parcount = ($parcount > 100) ? 100 : $parcount;
111 $options = url_get_variable_options($config);
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),
121 $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
122 $mform->setType($parameter, PARAM_RAW);
125 //-------------------------------------------------------
126 $this->standard_coursemodule_elements();
128 //-------------------------------------------------------
129 $this->add_action_buttons();
132 function data_preprocessing(&$default_values) {
133 if (!empty($default_values['displayoptions'])) {
134 $displayoptions = unserialize($default_values['displayoptions']);
135 if (isset($displayoptions['printintro'])) {
136 $default_values['printintro'] = $displayoptions['printintro'];
138 if (!empty($displayoptions['popupwidth'])) {
139 $default_values['popupwidth'] = $displayoptions['popupwidth'];
141 if (!empty($displayoptions['popupheight'])) {
142 $default_values['popupheight'] = $displayoptions['popupheight'];
145 if (!empty($default_values['parameters'])) {
146 $parameters = unserialize($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++;
156 function validation($data, $files) {
157 $errors = parent::validation($data, $files);
159 // Validating Entered url, we are looking for obvious problems only,
160 // teachers are responsible for testing if it actually works.
162 // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
164 // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
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
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');
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
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');
189 return $errors;