From: dongsheng Date: Mon, 14 Jul 2008 08:14:31 +0000 (+0000) Subject: MDL-15244, add ability to block ip addresses for HEAD. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ab99c8f014f3ef2d187b80c8a5576eb64c7c6949;p=moodle.git MDL-15244, add ability to block ip addresses for HEAD. --- diff --git a/admin/ipblocker.php b/admin/ipblocker.php new file mode 100644 index 0000000000..d676ec9555 --- /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 78e4cf8cad..edabdcd491 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 0486e4dcff..f04719740d 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 d91003e135..96c694df04 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 ?>