]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15244, add ability to block ip addresses for HEAD.
authordongsheng <dongsheng>
Mon, 14 Jul 2008 08:14:31 +0000 (08:14 +0000)
committerdongsheng <dongsheng>
Mon, 14 Jul 2008 08:14:31 +0000 (08:14 +0000)
admin/ipblocker.php [new file with mode: 0644]
admin/settings/server.php
lang/en_utf8/admin.php
lib/setup.php

diff --git a/admin/ipblocker.php b/admin/ipblocker.php
new file mode 100644 (file)
index 0000000..d676ec9
--- /dev/null
@@ -0,0 +1,44 @@
+<?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();
+?>
index 78e4cf8cadf4483526f60e41f8d740609a19f3ac..edabdcd491d551bbedcfbf0fea781e51730a7072 100644 (file)
@@ -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'),
index 0486e4dcff81b1a1f3d1014ec46f56aca332ca29..f04719740d8e37848a27ccec615a08a8f9da455a 100644 (file)
@@ -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 <em>wildly incorrect</em> data.
index d91003e1353d5b7d19f9737f650fabea9ba4a714..96c694df04145924e152ad94f1babc7abe40a5a5 100644 (file)
@@ -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
 ?>