MDL-15244, add ability to block ip addresses.

This commit is contained in:
dongsheng 2008-07-16 02:16:42 +00:00
parent 972485277e
commit 4e63912156
5 changed files with 90 additions and 79 deletions

View file

@ -1,44 +0,0 @@
<?php // $Id$
require('../config.php');
require_once($CFG->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 '<div style="text-align:center;">';
echo '<form method="post">';
echo '<h1>'.get_string('blockediplist', 'admin').'</h1>';
print_textarea(false, 20, 50, 600, 400, "list", $str);
echo '<p><input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="submit" value="'.get_string('submit').'" />';
echo helpbutton('blockip', 'Help');
echo '</p>';
echo '</form>';
echo '</div>';
admin_externalpage_print_footer();
?>

View file

@ -161,8 +161,13 @@ $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('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('ipblocker', get_string('ipblocker', 'admin'));
$temp->add(new admin_setting_configcheckbox('enableallowedip', get_string('enableallowedip', 'admin'), get_string('enableallowedipdesc', 'admin'), 0));
$temp->add(new admin_setting_configiplist('allowedip', get_string('allowediplist', 'admin'),
'', ''));
$temp->add(new admin_setting_configiplist('blockedip', get_string('blockediplist', 'admin'),
'', ''));
$ADMIN->add('server', $temp);
$temp = new admin_settingpage('cleanup', get_string('cleanup', 'admin')); $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'), $temp->add(new admin_setting_configselect('longtimenosee', get_string('longtimenosee', 'admin'), get_string('configlongtimenosee', 'admin'), 120, array(0 => get_string('never'),

View file

@ -7,6 +7,7 @@ $string['adminseesownevents'] = 'Administrators are just like other users';
$string['allowcategorythemes'] = 'Allow category themes'; $string['allowcategorythemes'] = 'Allow category themes';
$string['allowcoursethemes'] = 'Allow course themes'; $string['allowcoursethemes'] = 'Allow course themes';
$string['allowdeletes'] = 'Allow deletes'; $string['allowdeletes'] = 'Allow deletes';
$string['allowediplist'] = 'Allowed IP list';
$string['allowemailaddresses'] = 'Allowed email domains'; $string['allowemailaddresses'] = 'Allowed email domains';
$string['allowobjectembed'] = 'Allow EMBED and OBJECT tags'; $string['allowobjectembed'] = 'Allow EMBED and OBJECT tags';
$string['allowrenames'] = 'Allow renames'; $string['allowrenames'] = 'Allow renames';
@ -27,7 +28,7 @@ $string['backups'] = 'Backups';
$string['badwordsconfig'] = 'Enter your list of bad words separated by commas.'; $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['badwordsdefault'] = 'If the custom list is empty, a default list from the language pack will be used.';
$string['badwordslist'] = 'Custom bad words list'; $string['badwordslist'] = 'Custom bad words list';
$string['blockediplist'] = 'Blocked IP Address List'; $string['blockediplist'] = 'Blocked IP List';
$string['blockinstances'] = 'Instances'; $string['blockinstances'] = 'Instances';
$string['blockmultiple'] = 'Multiple'; $string['blockmultiple'] = 'Multiple';
$string['blocksettings'] = 'Manage blocks'; $string['blocksettings'] = 'Manage blocks';
@ -334,6 +335,8 @@ $string['enablerecordcache'] = 'Enable Record Cache';
$string['enablerssfeeds'] = 'Enable RSS feeds'; $string['enablerssfeeds'] = 'Enable RSS feeds';
$string['enablestats'] = 'Enable statistics'; $string['enablestats'] = 'Enable statistics';
$string['enabletrusttext'] = 'Enable Trusted Content'; $string['enabletrusttext'] = 'Enable Trusted Content';
$string['enableallowedip'] = 'Allow clients from these IPs';
$string['enableallowedipdesc'] = 'If this option enabled, only IPs entered in allowed list are permitted, IPs are in blocked list are blocked at the same time.';
$string['encoding'] = 'Encoding'; $string['encoding'] = 'Encoding';
$string['enrolmultipleusers'] = 'Enrol the users'; $string['enrolmultipleusers'] = 'Enrol the users';
$string['environment'] = 'Environment'; $string['environment'] = 'Environment';
@ -415,6 +418,8 @@ $string['intcachemax'] = 'Int. cache max';
$string['invalidsection'] = 'Invalid section.'; $string['invalidsection'] = 'Invalid section.';
$string['invaliduserchangeme'] = 'Username \"changeme\" is reserved -- you cannot create an account with it.'; $string['invaliduserchangeme'] = 'Username \"changeme\" is reserved -- you cannot create an account with it.';
$string['ipblocker'] = 'IP Blocker'; $string['ipblocker'] = 'IP Blocker';
$string['ipinblockedlist'] = 'This site is not available currently.';
$string['ipoutallowedlist'] = 'This site is not available currently.';
$string['iplookup'] = 'IP address lookup'; $string['iplookup'] = 'IP address lookup';
$string['iplookupinfo'] = ' $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 <em>wildly incorrect</em> data. 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 <em>wildly incorrect</em> data.

View file

@ -201,7 +201,7 @@ function get_db_directories() {
$dbdirs[] = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$plugin.'/db'; $dbdirs[] = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$plugin.'/db';
} }
} }
/// Now quiz report plugins (mod/quiz/report/xxx/db) /// Now quiz report plugins (mod/quiz/report/xxx/db)
if ($plugins = get_list_of_plugins('mod/quiz/report', 'db')) { if ($plugins = get_list_of_plugins('mod/quiz/report', 'db')) {
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
@ -1883,7 +1883,7 @@ class admin_setting_configtext extends admin_setting {
$data = 0; $data = 0;
} }
// $data is a string // $data is a string
$validated = $this->validate($data); $validated = $this->validate($data);
if ($validated !== true) { if ($validated !== true) {
return $validated; return $validated;
} }
@ -1944,7 +1944,7 @@ class admin_setting_configtextarea extends admin_setting_configtext {
$defaultinfo = $default; $defaultinfo = $default;
if (!is_null($default) and $default !== '') { if (!is_null($default) and $default !== '') {
$defaultinfo = "\n".$default; $defaultinfo = "\n".$default;
} }
return format_admin_setting($this, $this->visiblename, return format_admin_setting($this, $this->visiblename,
'<div class="form-textarea form-textarea-advanced" ><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'">'. s($data) .'</textarea></div>', '<div class="form-textarea form-textarea-advanced" ><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'">'. s($data) .'</textarea></div>',
@ -2258,7 +2258,7 @@ class admin_setting_configmulticheckbox extends admin_setting {
$return .= '</div>'; $return .= '</div>';
return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query); return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
} }
} }
@ -2353,7 +2353,7 @@ class admin_setting_configselect extends admin_setting {
if (strpos($textlib->strtolower($value), $query) !== false) { if (strpos($textlib->strtolower($value), $query) !== false) {
return true; return true;
} }
} }
return false; return false;
} }
@ -2585,6 +2585,33 @@ class admin_setting_configtime extends admin_setting {
} }
class admin_setting_configiplist extends admin_setting_configtextarea {
function validate($data) {
if(!empty($data)) {
$ips = explode("\n", $data);
} else {
return true;
}
$result = true;
foreach($ips as $ip) {
$ip = trim($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 = true;
} else {
$result = false;
break;
}
}
if($result){
return true;
} else {
return get_string('validateerror', 'admin');
}
}
}
/** /**
* Special checkbox for calendar - resets SESSION vars. * Special checkbox for calendar - resets SESSION vars.
*/ */
@ -2773,7 +2800,7 @@ class admin_setting_sitesettext extends admin_setting_configtext {
function write_setting($data) { function write_setting($data) {
global $DB; global $DB;
$data = trim($data); $data = trim($data);
$validated = $this->validate($data); $validated = $this->validate($data);
if ($validated !== true) { if ($validated !== true) {
return $validated; return $validated;
} }
@ -3417,7 +3444,7 @@ class admin_setting_regradingcheckbox extends admin_setting_configcheckbox {
} }
return $return; return $return;
} }
} }
/** /**
@ -3562,7 +3589,7 @@ class admin_setting_gradecat_combo extends admin_setting {
$defaultinfo[] = get_string('advanced'); $defaultinfo[] = get_string('advanced');
} }
$defaultinfo = implode(', ', $defaultinfo); $defaultinfo = implode(', ', $defaultinfo);
} else { } else {
$defaultinfo = NULL; $defaultinfo = NULL;
} }
@ -4714,7 +4741,7 @@ function db_replace($search, $replace) {
/** /**
* Prints tables of detected plugins, one table per plugin type, * Prints tables of detected plugins, one table per plugin type,
* and prints whether they are part of the standard Moodle * and prints whether they are part of the standard Moodle
* distribution or not. * distribution or not.
*/ */
function print_plugin_tables() { function print_plugin_tables() {
@ -4735,7 +4762,7 @@ function print_plugin_tables() {
'scorm', 'scorm',
'survey', 'survey',
'wiki'); 'wiki');
$plugins_standard['blocks'] = array('activity_modules', $plugins_standard['blocks'] = array('activity_modules',
'admin', 'admin',
'admin_bookmarks', 'admin_bookmarks',
@ -4767,7 +4794,7 @@ function print_plugin_tables() {
'tag_flickr', 'tag_flickr',
'tag_youtube', 'tag_youtube',
'tags'); 'tags');
$plugins_standard['filter'] = array('activitynames', $plugins_standard['filter'] = array('activitynames',
'algebra', 'algebra',
'censor', 'censor',
@ -4794,14 +4821,14 @@ function print_plugin_tables() {
$plugins_ondisk['mod'] = get_list_of_plugins('mod', 'db'); $plugins_ondisk['mod'] = get_list_of_plugins('mod', 'db');
$plugins_ondisk['blocks'] = get_list_of_plugins('blocks', 'db'); $plugins_ondisk['blocks'] = get_list_of_plugins('blocks', 'db');
$plugins_ondisk['filter'] = get_list_of_plugins('filter', 'db'); $plugins_ondisk['filter'] = get_list_of_plugins('filter', 'db');
$strstandard = get_string('standard'); $strstandard = get_string('standard');
$strnonstandard = get_string('nonstandard'); $strnonstandard = get_string('nonstandard');
$strmissingfromdisk = '(' . get_string('missingfromdisk') . ')'; $strmissingfromdisk = '(' . get_string('missingfromdisk') . ')';
$strabouttobeinstalled = '(' . get_string('abouttobeinstalled') . ')'; $strabouttobeinstalled = '(' . get_string('abouttobeinstalled') . ')';
$html = ''; $html = '';
$html .= '<table class="generaltable plugincheckwrapper" cellspacing="4" cellpadding="1"><tr valign="top">'; $html .= '<table class="generaltable plugincheckwrapper" cellspacing="4" cellpadding="1"><tr valign="top">';
foreach ($plugins_ondisk as $cat => $list_ondisk) { foreach ($plugins_ondisk as $cat => $list_ondisk) {
@ -4817,8 +4844,8 @@ function print_plugin_tables() {
$html .= '<tr class="r0"><th class="header c0">' . get_string('directory') . "</th>\n" $html .= '<tr class="r0"><th class="header c0">' . get_string('directory') . "</th>\n"
. '<th class="header c1">' . get_string('name') . "</th>\n" . '<th class="header c1">' . get_string('name') . "</th>\n"
. '<th class="header c2">' . get_string('status') . "</th>\n</tr>\n"; . '<th class="header c2">' . get_string('status') . "</th>\n</tr>\n";
$row = 1; $row = 1;
foreach ($list_ondisk as $k => $plugin) { foreach ($list_ondisk as $k => $plugin) {
$status = 'ok'; $status = 'ok';
@ -4828,15 +4855,15 @@ function print_plugin_tables() {
if (!in_array($plugin, $plugins_standard[$cat])) { if (!in_array($plugin, $plugins_standard[$cat])) {
$standard = 'nonstandard'; $standard = 'nonstandard';
$status = 'warning'; $status = 'warning';
} }
// Get real name and full path of plugin // Get real name and full path of plugin
$plugin_name = "[[$plugin]]"; $plugin_name = "[[$plugin]]";
$plugin_path = "$cat/$plugin"; $plugin_path = "$cat/$plugin";
$plugin_name = get_plugin_name($plugin, $cat); $plugin_name = get_plugin_name($plugin, $cat);
// Determine if the plugin is about to be installed // Determine if the plugin is about to be installed
if ($cat != 'filter' && !in_array($plugin, $plugins_installed[$cat])) { if ($cat != 'filter' && !in_array($plugin, $plugins_installed[$cat])) {
$note = $strabouttobeinstalled; $note = $strabouttobeinstalled;
@ -4852,11 +4879,11 @@ function print_plugin_tables() {
// If the plugin was both on disk and in the db, unset the value from the installed plugins list // If the plugin was both on disk and in the db, unset the value from the installed plugins list
if ($key = array_search($plugin, $plugins_installed[$cat])) { if ($key = array_search($plugin, $plugins_installed[$cat])) {
unset($plugins_installed[$cat][$key]); unset($plugins_installed[$cat][$key]);
} }
} }
// If there are plugins left in the plugins_installed list, it means they are missing from disk // If there are plugins left in the plugins_installed list, it means they are missing from disk
foreach ($plugins_installed[$cat] as $k => $missing_plugin) { foreach ($plugins_installed[$cat] as $k => $missing_plugin) {
// Make sure the plugin really is missing from disk // Make sure the plugin really is missing from disk
if (!in_array($missing_plugin, $plugins_ondisk[$cat])) { if (!in_array($missing_plugin, $plugins_ondisk[$cat])) {
$standard = 'standard'; $standard = 'standard';
@ -4871,15 +4898,15 @@ function print_plugin_tables() {
. "<td class=\"cell c0\">?</td>\n" . "<td class=\"cell c0\">?</td>\n"
. "<td class=\"cell c1\">$plugin_name</td>\n" . "<td class=\"cell c1\">$plugin_name</td>\n"
. "<td class=\"$standard $status cell c2\">" . ${'str' . $standard} . " $strmissingfromdisk</td>\n</tr>\n"; . "<td class=\"$standard $status cell c2\">" . ${'str' . $standard} . " $strmissingfromdisk</td>\n</tr>\n";
$row++; $row++;
} }
} }
$html .= '</table></td>'; $html .= '</table></td>';
} }
$html .= '</tr></table><br />'; $html .= '</tr></table><br />';
echo $html; echo $html;
} }

View file

@ -527,15 +527,33 @@ global $HTTPSPAGEREQUIRED;
} }
} }
$iplist = unserialize(get_config(null, 'blockedip')); $allowediponly = get_config(null, 'enableallowedip');
if(!empty($iplist)) { if(!empty($allowediponly)){
foreach($iplist as $ip) { $allowediplist = get_config(null, 'allowedip');
$blockediplist = get_config(null, 'blockedip');
} else {
$blockediplist = get_config(null, 'blockedip');
}
if(!empty($blockediplist)) {
$blockediplist = explode("\n", $blockediplist);
foreach($blockediplist as $ip) {
$ip = trim($ip);
if(address_in_subnet(getremoteaddr(), $ip)){ if(address_in_subnet(getremoteaddr(), $ip)){
// Telling the banned user the site is not // Telling the banned user the site is not
// available currently. // available currently.
echo get_string('sitemaintenance', 'admin'); die(get_string('ipinblockedlist', 'admin'));
die; }
} }
}
if(!empty($allowediplist)) {
$allowediplist = explode("\n", $allowediplist);
foreach($allowediplist as $ip) {
$ip = trim($ip);
if(!address_in_subnet(getremoteaddr(), $ip)){
// Telling users only specfied users are
// allowed accessing this site.
die(get_string('ipoutallowedlist', 'admin'));
}
} }
} }