<?php
/***************************************************************************
 *
 *  Multiple Registrations Detector plugin (/inc/plugins/mrd.php)
 *  Author: Pirata Nervo
 *  Copyright: © 2009-2010 Pirata Nervo
 *  
 *  Website: http://consoleworld.net
 *  License: license.txt
 *
 *  Detects multiple accounts based on registration IP and last login IP
 *
 ***************************************************************************/

/****************************************************************************
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

// add hooks
$plugins->add_hook('admin_load', 'mrd_admin');
$plugins->add_hook('admin_tools_menu', 'mrd_admin_tools_menu');
$plugins->add_hook('admin_tools_action_handler', 'mrd_admin_tools_action_handler');
$plugins->add_hook('admin_tools_permissions', 'mrd_admin_permissions');

function mrd_info()
{
	return array(
		"name"			=> "Multiple Registrations Detector",
		"description"	=> "Detects multiple accounts based on registration IP and last login IP.",
		"website"		=> "http://mybb-plugins.com",
		"author"		=> "Pirata Nervo",
		"authorsite"	=> "http://consoleaddicted.com",
		"version"		=> "1.1",
		"guid" 			=> "aef7057f23228e856e846d65d6511d73",
		"compatibility"	=> "18*"
	);
}


function mrd_activate()
{
	global $db, $lang;
}


function mrd_deactivate()
{
	global $db, $mybb;
}

/*************************************************************************************/
// ADMIN PART
/*************************************************************************************/

function mrd_admin_tools_menu(&$sub_menu)
{
	global $lang;
	
	$lang->load('mrd');
	$sub_menu[] = array('id' => 'mrd', 'title' => $lang->mrd_index, 'link' => 'index.php?module=tools-mrd');
}

function mrd_admin_tools_action_handler(&$actions)
{
	$actions['mrd'] = array('active' => 'mrd', 'file' => 'mrd');
}

function mrd_admin_permissions(&$admin_permissions)
{
  	global $db, $mybb, $lang;
  
	$lang->load("mrd", false, true);
	$admin_permissions['mrd'] = $lang->mrd_canmanage;
	
}

function mrd_admin()
{
	global $db, $lang, $mybb, $page, $run_module, $action_file, $mybbadmin, $plugins;
	
	$lang->load("mrd", false, true);
	
	if($run_module == 'tools' && $action_file == 'mrd')
	{
		$page->add_breadcrumb_item($lang->mrd, 'index.php?module=tools-mrd');	
		$page->output_header($lang->mrd);
				
		$sub_tabs['mrd_count'] = array(
			'title'			=> $lang->mrd,
			'link'			=> 'index.php?module=tools-mrd',
			'description'	=> $lang->mrd_desc
		);
		
		$page->output_nav_tabs($sub_tabs, 'mrd_count');
		
		if (!$mybb->input['action'])
		{			
			$form = new Form("index.php?module=tools-mrd&amp;action=detect_reg", "post", "mrd");
	
			$form_container = new FormContainer($lang->mrd_detect_reg);
			$form_container->output_row($lang->mrd_detect_reg_desc, $lang->mrd_detect_reg_desc_desc, '', 'desc');
			$form_container->end();
			
			$buttons = "";
			$buttons[] = $form->generate_submit_button($lang->mrd_submit);
			$form->output_submit_wrapper($buttons);
			$form->end();
			
			echo "<br />";
			
			$form = new Form("index.php?module=tools-mrd&amp;action=detect_login", "post", "mrd");
	
			$form_container = new FormContainer($lang->mrd_detect_login);
			$form_container->output_row($lang->mrd_detect_login_desc, $lang->mrd_detect_login_desc_desc, '', 'desc');
			$form_container->end();
			
			$buttons = "";
			$buttons[] = $form->generate_submit_button($lang->mrd_submit);
			$form->output_submit_wrapper($buttons);
			$form->end();
		}
		elseif ($mybb->input['action'] == 'detect_reg')
		{
			if($mybb->request_method == "post")
			{
				// query users and sort them by regip, no need to sort them later
				$users = array();
				$query = $db->simple_select('users', 'uid,username,regip', '', array('order_by' => 'regip', 'order_dir' => 'ASC'));
				while($user = $db->fetch_array($query)) {
					$users[$user['uid']] = array('regip' => $user['regip'], 'username' => $user['username']);
				}
				
				$regips = array();
				
				// place all regip in a new array and the key of each member is the uid
				foreach ($users as $uid => $user)
				{
					$regips[$uid] = $user['regip'];
				}
				
				// duplicate registration ips
				$dup_regips = $regips;
				
				// this array will store duplicated uid's
				$dup_dup_regips = array();
				
				foreach ($dup_regips as $uid => $regip)
				{
					// search for elements with the same value
					$duplicates = array_keys($dup_regips, $regip);
					
					// if we have more than 1 element, it has duplicates
					if (count($duplicates) > 1)
					{
						foreach ($duplicates as $duplicate)
						{
							// store the uid so we can grab the IP later
							$dup_dup_regips[] = $duplicate;
							
							// unset the element from the array now
							unset($dup_regips[$duplicate]);
						}
					}
				}
				
				// now we have all duplicate IP's stored in $dup_dup_regips so we must re-build a new $users array
				// assign each regip to a uid (key) and assign the username to each uid
				$dup_users = $users;
				$users = array();
				foreach ($dup_users as $uid => $user)
				{
					// uid not in the duplicated regips array (the array contains the uid's of the duplicarted accounts)
					if (!in_array($uid, $dup_dup_regips))
						continue;
					
					// store the user data in our new users array
					$users[$uid] = $user;
				}
				
				// table
				$table = new Table;
				$table->construct_header($lang->mrd_user, array('width' => '50%'));
				$table->construct_header($lang->mrd_ip, array('width' => '50%'));
				
				foreach ($users as $uid => $user)
				{
					$table->construct_cell(build_profile_link(htmlspecialchars_uni($user['username']), intval($uid))); // user
					$table->construct_cell(my_inet_ntop($db->unescape_binary($user['regip']))); // reg ip
					
					$table->construct_row();
				}
				
				if($table->num_rows() == 0)
				{
					$table->construct_cell($lang->mrd_no_data, array('colspan' => 2));
					
					$table->construct_row();
				}
				
				$table->output($lang->mrd_detect_reg);
			}
		}
		elseif ($mybb->input['action'] == 'detect_login')
		{
			if($mybb->request_method == "post")
			{
				// query users and sort them by lastip, no need to sort them later
				$users = array();
				$query = $db->simple_select('users', 'uid,username,lastip', 'lastip!=\'\'', array('order_by' => 'lastip', 'order_dir' => 'ASC'));
				while($user = $db->fetch_array($query)) {
					$users[$user['uid']] = array('lastip' => $user['lastip'], 'username' => $user['username']);
				}
				
				$lastips = array();
				
				// place all lastip in a new array and the key of each member is the uid
				foreach ($users as $uid => $user)
				{
					$lastips[$uid] = $user['lastip'];
				}
				
				// duplicate registration ips
				$dup_lastips = $lastips;
				
				// this array will store duplicated uid's
				$dup_dup_lastips = array();
				
				foreach ($dup_lastips as $uid => $lastip)
				{
					// search for elements with the same value
					$duplicates = array_keys($dup_lastips, $lastip);
					
					// if we have more than 1 element, it has duplicates
					if (count($duplicates) > 1)
					{
						foreach ($duplicates as $duplicate)
						{
							// store the uid so we can grab the IP later
							$dup_dup_lastips[] = $duplicate;
							
							// unset the element from the array now
							unset($dup_lastips[$duplicate]);
						}
					}
				}
				
				// now we have all duplicate IP's stored in $dup_dup_lastips so we must re-build a new $users array
				// assign each lastip to a uid (key) and assign the username to each uid
				$dup_users = $users;
				$users = array();
				foreach ($dup_users as $uid => $user)
				{
					// uid not in the duplicated lastips array (the array contains the uid's of the duplicarted accounts)
					if (!in_array($uid, $dup_dup_lastips))
						continue;
					
					// store the user data in our new users array
					$users[$uid] = $user;
				}
				
				// table
				$table = new Table;
				$table->construct_header($lang->mrd_user, array('width' => '50%'));
				$table->construct_header($lang->mrd_ip, array('width' => '50%'));
				
				foreach ($users as $uid => $user)
				{
					$table->construct_cell(build_profile_link(htmlspecialchars_uni($user['username']), intval($uid))); // user
					$table->construct_cell(my_inet_ntop($db->unescape_binary($user['lastip']))); // last ip
					
					$table->construct_row();
				}
				
				if($table->num_rows() == 0)
				{
					$table->construct_cell($lang->mrd_no_data, array('colspan' => 2));
					
					$table->construct_row();
				}
				
				$table->output($lang->mrd_detect_reg);
			}
		}
		
		$page->output_footer();
		exit;
	}
}

?>
