mirror of
https://github.com/LdesignMedia/moodle-availability_ipaddress.git
synced 2026-10-22 11:08:07 +02:00
The custom IP field and the predefined ranges form accepted invalid entries such as 2001:db8::1garbage, 192.168.1.0/24garbage or 192.168.1.0/999. address_in_subnet() silently skips those entries, so a "must not" restriction with a typo let blocked users in. Validation now only accepts formats address_in_subnet() can match: a single address, a subnet (mask 0-32 / 0-128) or a range in the last group with end >= start. IPv6 forms cleanremoteaddr() cannot parse are rejected as well. JS and PHP share the same rules; PHP also checks each entry against address_in_subnet() as a safety net. - Allow spaces after commas in the restriction field - Support IPv6 ranges, e.g. 2001:db8::1-ff - Add PHPUnit tests for helper::is_valid_ipaddress_list() - Bump version to 2026101500 (5.3.2)
115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
* Form for managing predefined IP ranges.
|
|
*
|
|
* @package availability_ipaddress
|
|
* @copyright 04/08/2025 LdesignMedia.nl - Luuk Verhoeven
|
|
* @author Vincent Cornelis
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
namespace availability_ipaddress\form;
|
|
|
|
use availability_ipaddress\helper;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
global $CFG;
|
|
require_once($CFG->libdir . '/formslib.php');
|
|
|
|
/**
|
|
* Form for managing predefined IP ranges.
|
|
*
|
|
* @package availability_ipaddress
|
|
* @copyright 04/08/2025 LdesignMedia.nl - Luuk Verhoeven
|
|
* @author Vincent Cornelis
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
class range_form extends \moodleform {
|
|
/**
|
|
* Define the form.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function definition(): void {
|
|
$mform = $this->_form;
|
|
$id = $this->_customdata['id'] ?? 0;
|
|
|
|
// Hidden id field.
|
|
$mform->addElement('hidden', 'id');
|
|
$mform->setType('id', PARAM_INT);
|
|
$mform->setDefault('id', $id);
|
|
|
|
// Name field.
|
|
$mform->addElement('text', 'name', get_string('range_name', 'availability_ipaddress'));
|
|
$mform->setType('name', PARAM_TEXT);
|
|
$mform->addRule('name', null, 'required', null, 'client');
|
|
$mform->addHelpButton('name', 'range_name', 'availability_ipaddress');
|
|
|
|
// IP addresses field.
|
|
$mform->addElement(
|
|
'text',
|
|
'ipaddresses',
|
|
get_string('ipaddresses', 'availability_ipaddress'),
|
|
['size' => 100]
|
|
);
|
|
$mform->setType('ipaddresses', PARAM_TEXT);
|
|
$mform->addRule('ipaddresses', null, 'required', null, 'client');
|
|
$mform->addHelpButton('ipaddresses', 'ipaddresses_help', 'availability_ipaddress');
|
|
|
|
// Description field.
|
|
$mform->addElement(
|
|
'textarea',
|
|
'description',
|
|
get_string('description'),
|
|
['rows' => 3, 'cols' => 60]
|
|
);
|
|
$mform->setType('description', PARAM_TEXT);
|
|
|
|
// Enabled field.
|
|
$mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'availability_ipaddress'));
|
|
$mform->setDefault('enabled', 1);
|
|
|
|
// Action buttons.
|
|
$this->add_action_buttons();
|
|
}
|
|
|
|
/**
|
|
* Validate the form data.
|
|
*
|
|
* Note: Parameter type declarations cannot be added here as the parent
|
|
* moodleform::validation() method doesn't have them, and PHP requires
|
|
* compatibility with parent method signatures when overriding.
|
|
*
|
|
* @param array $data
|
|
* @param array $files
|
|
*
|
|
* @return array
|
|
*/
|
|
public function validation($data, $files) {
|
|
$errors = parent::validation($data, $files);
|
|
|
|
// Validate IP addresses, the field is required so an empty value is invalid as well.
|
|
if (!helper::is_valid_ipaddress_list($data['ipaddresses'] ?? '')) {
|
|
$errors['ipaddresses'] = get_string('error_ipaddress', 'availability_ipaddress');
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
}
|