MDL-14123 Full IPv6 support - replaced mnet ip_in_range() by standard ipv6 compatible...
[moodle.git] / admin / mnet / trustedhosts.php
CommitLineData
71558f85 1<?php
2 // Allows the admin to configure services for remote hosts
3
4 require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
5 require_once($CFG->libdir.'/adminlib.php');
6 include_once($CFG->dirroot.'/mnet/lib.php');
7
8 require_login();
1ae083e4 9 admin_externalpage_setup('trustedhosts');
71558f85 10
e75ac13b 11 $context = get_context_instance(CONTEXT_SYSTEM);
71558f85 12
13 require_capability('moodle/site:config', $context, $USER->id, true, "nopermissions");
14
1734e61a 15 if (!extension_loaded('openssl')) {
1ae083e4 16 admin_externalpage_print_header();
3c76e671 17 print_error('requiresopenssl', 'mnet', '', NULL, true);
18 }
19
20 if (!$site = get_site()) {
1ae083e4 21 admin_externalpage_print_header();
5a3be384 22 print_error('nosite', '', '', NULL, true);
1734e61a 23 }
24
71558f85 25 $trusted_hosts = '';//array();
26 $old_trusted_hosts = get_config('mnet', 'mnet_trusted_hosts');
c9fa3eee 27 if (!empty($old_trusted_hosts)) {
28 $old_trusted_hosts = explode(',', $old_trusted_hosts);
29 } else {
30 $old_trusted_hosts = array();
31 }
71558f85 32
33 $test_ip_address = optional_param('testipaddress', NULL, PARAM_HOST);
34 $in_range = false;
35 if (!empty($test_ip_address)) {
c9fa3eee 36 foreach($old_trusted_hosts as $host) {
b636fdfb 37 if (address_in_subnet($test_ip_address, $host)) {
71558f85 38 $in_range = true;
b636fdfb 39 $validated_by = $host;
71558f85 40 break;
41 }
42 }
43 }
44
45 /// If data submitted, process and store
46 if (($form = data_submitted()) && confirm_sesskey()) {
71558f85 47 $hostlist = preg_split("/[\s,]+/", $form->hostlist);
48 foreach($hostlist as $host) {
49 list($address, $mask) = explode('/', $host.'/');
50 if (empty($address)) continue;
51 if (strlen($mask) == 0) $mask = 32;
52 $trusted_hosts .= trim($address).'/'.trim($mask)."\n";
53 unset($address, $mask);
54 }
55 set_config('mnet_trusted_hosts', str_replace("\n", ',', $trusted_hosts), 'mnet');
c9fa3eee 56 } elseif (!empty($old_trusted_hosts)) {
57 foreach($old_trusted_hosts as $host) {
71558f85 58 list($address, $mask) = explode('/', $host.'/');
59 if (empty($address)) continue;
60 if (strlen($mask) == 0) $mask = 32;
61 $trusted_hosts .= trim($address).'/'.trim($mask)."\n";
62 unset($address, $mask);
63 }
64 }
65
66 include('./trustedhosts.html');
67?>