MyBB Community Forums

Full Version: IP Restricted Group
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to make a plugin that automatically registers a user to a group that has permission to see/edit a particular section of the forums, but only if the user is in a particular IP range. I also want the plugin to unregister a user if he/she is no longer in that IP range. This is the easiest way I could think of to restrict access to part of my forums to an IP range.

I am trying to learn PHP. I will be taking web programming this semester (C.S.A. major), but I need a solution before then. I'll show you what I have so far. I've replaced the IP ranges for my college (I'll be using this plugin so that people on campus have access to more of the forum) with ###. Once I become more familiar with PHP, I'll write a backend for the admin panel so people can input IP ranges and group IDs that way.

The plugin checks for your IP, sees if it's within the range, and then does one of two things. I just don't know where to go from there.

If anyone can help, that would be much appreciated.

campusrange.php
in the plugin folder

<?php

	// MyBB plugin:  Campus Range
	// This plugin is free to use and modify
	// Creative Commons License:  http://creativecommons.org/licenses/by-nc/3.0/us/
	// Developed by Curtis Prevo and whoever decides to help me....

	// Disallow direct access to this file for security reasons.
	if (!defined("IN_MYBB"))
	{
		die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
	}
	
	$plugins->add_hook("pre_output_page", "campusrange_pre_output_page");
	
	function campusrange_info()
	{
		return Array(
			"name" => "Campus Range",
			"description" => "Adds people on campus to a special group",
			"website" => "",
			"author" => "Curtis Prevo",
			"authorsite" => "",
			"version" => "0.1.1",
			"guid" => "",
			"compatibility" => "16"
		);
	}
	
	function campusrange_pre_output_page()
	{
		$ip=$_SERVER['REMOTE_ADDR'];
		$octets = explode('.',$ip);
		$restrictedforums=array(4,9,10,11,29,34);
		global $mybb;
		
		if (($octets[0] == ### && $octets[1] == ### && $octets[2] > ### && $octets[2] < ###) || $mybb->usergroup['isadmin'] == 1 || $mybb->usergroup['issupermod'] == 1)
		{
			echo "Admins and supermods also need access to those forums, even off-campus.  There should be an IF statement to check if the person is already in the group.  If not, it should add them.";
		}
		else
		{
			echo "There should be another IF statement to find out if the person is already removed from the group.  If not, it should remove them.";
		}
	}

?>

Aha!!! I got it working with the help of my apartment mate (he just got back from a vacation), a graduated computer science major. Anyway, the plugin makes a user automatically join/leave a usergroup based on whether or not they are in a particular IP range (or supermod or admin, but you can change it rather easily). You must specify the IP range and the group ID. In the future, I'll actually release it through MyBB with a backend admin settings panel thingy. For now, it took me long enough to get this far. I'll tackle the rest after I actually learn PHP. C++ only gets you so far.

I have replaced the usergroup ID with "GID" (for which a simple "find and replace" should suffice) and the IP ranges with asterisks. You can probably figure out how to edit it for your own purposes. PM me if you can't. I'm sure you can, though. I don't know PHP and I made this (with help....).

<?php

	// MyBB plugin:  User Group by IP Range
	// This plugin is free to use and modify, just remember to attribute me
	// Restricts moderator and/or administrator account access to specific IP addresses.
	// Creative Commons License:  http://creativecommons.org/licenses/by-nc-nd/3.0/us
	// Developed by Curtis Prevo, with thanks to Rob Scott

	// Disallow direct access to this file for security reasons.
	if (!defined("IN_MYBB"))
	{
		die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
	}
	
	$plugins->add_hook("pre_output_page", "group_by_ip_range_pre_output_page");
	
	function group_by_ip_range_info()
	{
		return Array(
			"name" => "User Group by IP Range",
			"description" => "Makes it so a usergroup is added or taken away based on IP",
			"website" => "",
			"author" => "Curtis Prevo",
			"authorsite" => "http://curtisprevo.com/",
			"version" => "0.1",
			"guid" => "",
			"compatibility" => "16"
		);
	}
	
	function group_by_ip_range_pre_output_page()
	{
		$ip=$_SERVER['REMOTE_ADDR'];
		$octets = explode('.',$ip);
		global $mybb,$db;
		$arrayofgroups = explode (',',$mybb->user['additionalgroups']);
		$partofgroup = (in_array ('USER_GROUP_NUMBER_GOES_HERE',$arrayofgroups)) ? true:false;
		
		if (($octets[0] == *** && $octets[1] == *** && $octets[2] > *** && $octets[2] < ***) || $mybb->usergroup['isadmin'] == 1 || $mybb->usergroup['issupermod'] == 1)
		{
			if (!$partofgroup)
			{
				$arrayofgroups[]=GID;
				$mybb->user['additionalgroups'] = implode (',',$arrayofgroups);
				$db->update_query("users", array('additionalgroups' => implode (',',$arrayofgroups)),'uid='.$mybb->user['uid']);
			}
		}
		else
		{
			if ($partofgroup)
			{
				$array_key=array_search('GID',$arrayofgroups);
				unset($arrayofgroups[$array_key]);
				$mybb->user['additionalgroups'] = implode (',',$arrayofgroups);
				$db->update_query("users", array('additionalgroups' => implode (',',$arrayofgroups)),'uid='.$mybb->user['uid']);
			}
		}
	}

?>