<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class admin_users_clones
{
	public function getClonesByIP($count=1, $period=0)
	{
		$result=Db::i()->select(
			"SELECT *
			FROM (
				SELECT last_ip, COUNT(last_ip) as counter
				FROM ?_users
				WHERE {last_visit>NOW() - INTERVAL ?d DAY AND }
				`status`!=-1 GROUP BY last_ip) as  i
			JOIN ?_users u ON i.last_ip=u.last_ip
			WHERE counter>?d
			AND `status`!=-1
			{AND last_visit>=NOW() - INTERVAL ?d DAY}
			ORDER BY last_visit, pass",
			($period?$period:DBSIMPLE_SKIP),
			$count,
			($period?$period:DBSIMPLE_SKIP)
		);

		return $this->_prepareClonesResult($result);
	}
	
	public function getClonesByID($count=1)
	{
		$result=Db::i()->select(
			"SELECT *, t1.real_id
			FROM ?_users t1
			JOIN
			(
				SELECT
				real_id
				FROM ?_users
				WHERE `status`!=-1
				GROUP by real_id         
				HAVING COUNT(user_id) > ?d
			)
			t2 ON t1.real_id = t2.real_id
			WHERE t1.status!=-1", $count
		);
		return $this->_prepareClonesResult($result);
	}
	
	protected function _prepareClonesResult($result)
	{
		$clones=array();

		if ($result)
		while ($clone=array_shift($result)) {
			$clone['ip']=inet_ntop($clone['ip']);
			$clone['pass']=substr($clone['pass'], 0, 6);
			$clones[$clone['real_id']][]=$clone;
		}
		return $clones;
	}
}