From ab99c8f014f3ef2d187b80c8a5576eb64c7c6949 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Mon, 14 Jul 2008 08:14:31 +0000 Subject: [PATCH] MDL-15244, add ability to block ip addresses for HEAD. --- admin/ipblocker.php | 44 +++++++++++++++++++++++++++++++++++++++ admin/settings/server.php | 2 ++ lang/en_utf8/admin.php | 2 ++ lib/setup.php | 12 +++++++++++ 4 files changed, 60 insertions(+) create mode 100644 admin/ipblocker.php diff --git a/admin/ipblocker.php b/admin/ipblocker.php new file mode 100644 index 00000000000..d676ec95550 --- /dev/null +++ b/admin/ipblocker.php @@ -0,0 +1,44 @@ +libdir.'/adminlib.php'); + $iplist = optional_param('list', '', PARAM_CLEAN); + admin_externalpage_setup('ipblocker'); + + if ($form = data_submitted()) { + if (confirm_sesskey()) { + $ips = explode("\n", $iplist); + $result = array(); + foreach($ips as $ip) { + if(preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}$#', $ip, $match) || + preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}(\/\d{1,2})$#', $ip, $match) || + preg_match('#^(\d{1,3})(\.\d{1,3}){3}(-\d{1,3})$#', $ip, $match)) { + $result[] = $ip; + } + } + set_config('blockedip', serialize($result)); + } + } + + admin_externalpage_print_header(); + $iplist = unserialize(get_config(null, 'blockedip')); + if(empty($iplist)) { + $iplist = array(); + } + $str = ''; + foreach($iplist as $ip){ + $str .= $ip."\n"; + } + + echo '
'; + echo '
'; + echo '

'.get_string('blockediplist', 'admin').'

'; + print_textarea(false, 20, 50, 600, 400, "list", $str); + echo '

'; + echo ''; + echo helpbutton('blockip', 'Help'); + echo '

'; + echo '
'; + echo '
'; + + admin_externalpage_print_footer(); +?> diff --git a/admin/settings/server.php b/admin/settings/server.php index 78e4cf8cadf..edabdcd491d 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -161,6 +161,8 @@ $ADMIN->add('server', $temp); $ADMIN->add('server', new admin_externalpage('maintenancemode', get_string('sitemaintenancemode', 'admin'), "$CFG->wwwroot/$CFG->admin/maintenance.php")); +$ADMIN->add('server', new admin_externalpage('ipblocker', get_string('ipblocker', 'admin'), "$CFG->wwwroot/$CFG->admin/ipblocker.php")); + $temp = new admin_settingpage('cleanup', get_string('cleanup', 'admin')); $temp->add(new admin_setting_configselect('longtimenosee', get_string('longtimenosee', 'admin'), get_string('configlongtimenosee', 'admin'), 120, array(0 => get_string('never'), diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 0486e4dcff8..f04719740d8 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -27,6 +27,7 @@ $string['backups'] = 'Backups'; $string['badwordsconfig'] = 'Enter your list of bad words separated by commas.'; $string['badwordsdefault'] = 'If the custom list is empty, a default list from the language pack will be used.'; $string['badwordslist'] = 'Custom bad words list'; +$string['blockediplist'] = 'Blocked IP Address List'; $string['blockinstances'] = 'Instances'; $string['blockmultiple'] = 'Multiple'; $string['blocksettings'] = 'Manage blocks'; @@ -413,6 +414,7 @@ $string['installedlangs'] = 'Installed language packs'; $string['intcachemax'] = 'Int. cache max'; $string['invalidsection'] = 'Invalid section.'; $string['invaliduserchangeme'] = 'Username \"changeme\" is reserved -- you cannot create an account with it.'; +$string['ipblocker'] = 'IP Blocker'; $string['iplookup'] = 'IP address lookup'; $string['iplookupinfo'] = ' By default Moodle uses the free online NetGeo (The Internet Geographic Database) server to lookup location of IP addresses, unfortunately this database is not maintained anymore and may return wildly incorrect data. diff --git a/lib/setup.php b/lib/setup.php index d91003e1353..96c694df041 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -527,6 +527,18 @@ global $HTTPSPAGEREQUIRED; } } + $iplist = unserialize(get_config(null, 'blockedip')); + if(!empty($iplist)) { + foreach($iplist as $ip) { + if(address_in_subnet(getremoteaddr(), $ip)){ + // Telling the banned user the site is not + // available currently. + echo get_string('sitemaintenance', 'admin'); + die; + } + } + } + /// note: we can not block non utf-8 installatrions here, because empty mysql database /// might be converted to utf-8 in admin/index.php during installation ?>