| 1 | 
           efrain | 
           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 | 
                   if ($config->allowvariables) {
  | 
        
        
            | 
            | 
           104 | 
                       $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
  | 
        
        
            | 
            | 
           105 | 
                       $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url'));
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
                       if (empty($this->current->parameters)) {
  | 
        
        
            | 
            | 
           108 | 
                           $parcount = 5;
  | 
        
        
            | 
            | 
           109 | 
                       } else {
  | 
        
        
            | 
            | 
           110 | 
                           $parcount = 5 + count((array)unserialize_array($this->current->parameters));
  | 
        
        
            | 
            | 
           111 | 
                           $parcount = ($parcount > 100) ? 100 : $parcount;
  | 
        
        
            | 
            | 
           112 | 
                       }
  | 
        
        
            | 
            | 
           113 | 
                       $options = url_get_variable_options($config);
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
                       for ($i = 0; $i < $parcount; $i++) {
  | 
        
        
            | 
            | 
           116 | 
                           $parameter = "parameter_$i";
  | 
        
        
            | 
            | 
           117 | 
                           $variable = "variable_$i";
  | 
        
        
            | 
            | 
           118 | 
                           $pargroup = "pargoup_$i";
  | 
        
        
            | 
            | 
           119 | 
                           $group = [
  | 
        
        
            | 
            | 
           120 | 
                               $mform->createElement('text', $parameter, '', ['size' => '12']),
  | 
        
        
            | 
            | 
           121 | 
                               $mform->createElement('selectgroups', $variable, '', $options),
  | 
        
        
            | 
            | 
           122 | 
                           ];
  | 
        
        
            | 
            | 
           123 | 
                           $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
  | 
        
        
            | 
            | 
           124 | 
                           $mform->setType($parameter, PARAM_RAW);
  | 
        
        
            | 
            | 
           125 | 
                       }
  | 
        
        
            | 
            | 
           126 | 
                   }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                   //-------------------------------------------------------
  | 
        
        
            | 
            | 
           129 | 
                   $this->standard_coursemodule_elements();
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
                   //-------------------------------------------------------
  | 
        
        
            | 
            | 
           132 | 
                   $this->add_action_buttons();
  | 
        
        
            | 
            | 
           133 | 
               }
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
               function data_preprocessing(&$default_values) {
  | 
        
        
            | 
            | 
           136 | 
                   if (!empty($default_values['displayoptions'])) {
  | 
        
        
            | 
            | 
           137 | 
                       $displayoptions = (array) unserialize_array($default_values['displayoptions']);
  | 
        
        
            | 
            | 
           138 | 
                       if (isset($displayoptions['printintro'])) {
  | 
        
        
            | 
            | 
           139 | 
                           $default_values['printintro'] = $displayoptions['printintro'];
  | 
        
        
            | 
            | 
           140 | 
                       }
  | 
        
        
            | 
            | 
           141 | 
                       if (!empty($displayoptions['popupwidth'])) {
  | 
        
        
            | 
            | 
           142 | 
                           $default_values['popupwidth'] = $displayoptions['popupwidth'];
  | 
        
        
            | 
            | 
           143 | 
                       }
  | 
        
        
            | 
            | 
           144 | 
                       if (!empty($displayoptions['popupheight'])) {
  | 
        
        
            | 
            | 
           145 | 
                           $default_values['popupheight'] = $displayoptions['popupheight'];
  | 
        
        
            | 
            | 
           146 | 
                       }
  | 
        
        
            | 
            | 
           147 | 
                   }
  | 
        
        
            | 
            | 
           148 | 
                   if (!empty($default_values['parameters'])) {
  | 
        
        
            | 
            | 
           149 | 
                       $parameters = (array) unserialize_array($default_values['parameters']);
  | 
        
        
            | 
            | 
           150 | 
                       $i = 0;
  | 
        
        
            | 
            | 
           151 | 
                       foreach ($parameters as $parameter=>$variable) {
  | 
        
        
            | 
            | 
           152 | 
                           $default_values['parameter_'.$i] = $parameter;
  | 
        
        
            | 
            | 
           153 | 
                           $default_values['variable_'.$i]  = $variable;
  | 
        
        
            | 
            | 
           154 | 
                           $i++;
  | 
        
        
            | 
            | 
           155 | 
                       }
  | 
        
        
            | 
            | 
           156 | 
                   }
  | 
        
        
            | 
            | 
           157 | 
               }
  | 
        
        
            | 
            | 
           158 | 
              | 
        
        
            | 
            | 
           159 | 
               function validation($data, $files) {
  | 
        
        
            | 
            | 
           160 | 
                   $errors = parent::validation($data, $files);
  | 
        
        
            | 
            | 
           161 | 
              | 
        
        
            | 
            | 
           162 | 
                   // Validating Entered url, we are looking for obvious problems only,
  | 
        
        
            | 
            | 
           163 | 
                   // teachers are responsible for testing if it actually works.
  | 
        
        
            | 
            | 
           164 | 
              | 
        
        
            | 
            | 
           165 | 
                   // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example.
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
                   // NOTE: do not try to explain the difference between URL and URI, people would be only confused...
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
                   if (!empty($data['externalurl'])) {
  | 
        
        
            | 
            | 
           170 | 
                       $url = $data['externalurl'];
  | 
        
        
            | 
            | 
           171 | 
                       if (preg_match('|^/|', $url)) {
  | 
        
        
            | 
            | 
           172 | 
                           // links relative to server root are ok - no validation necessary
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                       } else if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) {
  | 
        
        
            | 
            | 
           175 | 
                           // normal URL
  | 
        
        
            | 
            | 
           176 | 
                           if (!url_appears_valid_url($url)) {
  | 
        
        
            | 
            | 
           177 | 
                               $errors['externalurl'] = get_string('invalidurl', 'url');
  | 
        
        
            | 
            | 
           178 | 
                           }
  | 
        
        
            | 
            | 
           179 | 
              | 
        
        
            | 
            | 
           180 | 
                       } else if (preg_match('|^[a-z]+:|i', $url)) {
  | 
        
        
            | 
            | 
           181 | 
                           // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers,
  | 
        
        
            | 
            | 
           182 | 
                           // we do not validate these at all, sorry
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
                       } else {
  | 
        
        
            | 
            | 
           185 | 
                           // invalid URI, we try to fix it by adding 'http://' prefix,
  | 
        
        
            | 
            | 
           186 | 
                           // relative links are NOT allowed because we display the link on different pages!
  | 
        
        
            | 
            | 
           187 | 
                           if (!url_appears_valid_url('http://'.$url)) {
  | 
        
        
            | 
            | 
           188 | 
                               $errors['externalurl'] = get_string('invalidurl', 'url');
  | 
        
        
            | 
            | 
           189 | 
                           }
  | 
        
        
            | 
            | 
           190 | 
                       }
  | 
        
        
            | 
            | 
           191 | 
                   }
  | 
        
        
            | 
            | 
           192 | 
                   return $errors;
  | 
        
        
            | 
            | 
           193 | 
               }
  | 
        
        
            | 
            | 
           194 | 
              | 
        
        
            | 
            | 
           195 | 
           }
  |