<?php
/**
 * Account manager functions file
 * @author Lauri Lüüs <mad@webpoint.ee>
 * @version $Id: functions.php 4 2005-05-24 00:50:28Z madest $
 * @package AccountManager
 * @filesource
 */
 
/**
 * Displays L2j server status
 * @param string
 * @return bool
 */
function is_online($server_ip)
{
	$ip = gethostbyname("{$server_ip}");
	if($fp = @fsockopen($ip, 7777,$un,$sinn,2)) {
		fclose($fp);
		return true;
	} else {
		return false;
	}
}

/**
 * Counts all accounts
 * @return int - account count
 */
function account_count()
{
	if($q = mysql_query("SELECT COUNT('login') AS acs FROM accounts"))
	{
		$r = mysql_fetch_array($q);
		return $r['acs'];
	} else {
		return false;
	}
}

//-/**
//- * Counts all characters
//- * @return int - character count
//- */
//-function char_count()
//-{
//-	$q = mysql_query("SELECT COUNT('charId') AS acs FROM characters");
//-	$r = mysql_fetch_array($q);
//-	return $r['acs'];	
//-}

/**
 * Get Visitor IP address
 * @return string - visitor ip address
 */
function get_ip()
{
	$remote_addr = getenv("REMOTE_ADDR");
	$forwarded_for = getenv("X_HTTP_FORWARDED_FOR");

	if($forwarded_for) {
		$client_ip = $forwarded_for;
	}else{
		$client_ip = $remote_addr;
	}
	return $client_ip;
}

/**
 * Create SQL Connection
 * @param string Sql username
 * @param string Sql password
 * @param string Sql database name
 */
function connection($user, $pass, $database, $host="localhost")
{
	$db = @mysql_connect($host,$user,$pass);
	if($db)
	{
		$selected = mysql_select_db($database,$db);
		
		if($selected)
		{
			mysql_query('SET NAMES UTF8');
		//	mysql_set_charset('UTF8');	//PHP>=5.2.3
			return true;
		}
	} else {
		return false;
	}
}
?>