<?php
/* 
	POMM  v1.3
	Player Online Map for MangOs

	Show online players position on map. Update without refresh.
	Show tooltip with location, race, class and level of player.
	Show realm status.

	16.09.2006		http://pomm.da.ru/
	
	Created by mirage666 (c) (mailto:mirage666@pisem.net icq# 152263154)
*/

require_once("../../scripts/config.php");

$host = $dbhost;				// HOST for Mangos database
$user = $dbuser;				// USER for Mangos database
$password = $dbpass;			// PASS for Mangos database
$db = $mangos_db['1'];			// NAME of Mangos database

$hostr = $dbhost;				// HOST for Realm database
$userr = $dbuser;				// USER for Realm database
$passwordr = $dbpass;			// PASS for Realm database
$dbr = $realm_db;				// NAME of Realm database

$serverr = $server;				// Server adress (for realm status)
$portt = $port['1'];			// Server port (for realm status) 8085 or 3724

$img_base = "img/";				// Image dir
$time= "10";					// Update time (seconds), 0 - not update.
$show_time="1";					// Show update timer 1 - on, 0 - off
$database_encoding = 'CP1251';	// Set encoding




// Function part

$maps_a = Array(
	0 => 'Azeroth', 
	1 => 'Kalimdor', 
	2 => 'UnderMine',
	13 => 'Test zone',
	17 => 'Kalidar',
	30 => 'Alterac Valley', 
	33 => 'Shadowfang Keep Instance', 
	34 => 'The Stockade Instance', 
	35 => 'Stormwind Prizon', 
	36 => 'Deadmines Instance', 
	37 => 'Plains of Snow', 
	43 => 'Wailing Caverns Instance', 
	44 => 'Monastery Interior', 
	47 => 'Razorfen Kraul Instance', 
	48 => 'Blackfathom Deeps Instance', 
	70 => 'Uldaman Instance', 
	90 => 'Gnomeregan Instance', 
	109 => 'Sunken Temple Instance', 
	129 => 'Razorfen Downs Instance', 
	150 => 'Outland', 
	169 => 'Emerald Forest', 
	189 => 'Scarlet Monastery Instance', 
	209 => 'Zul\`Farrak Instance', 
	229 => 'Blackrock Spire Instance', 
	230 => 'Blackrock Depths Instance', 
	249 => 'Onyxia\`s Lair Instance', 
	269 => 'Caverns of Time', 
	289 => 'Scholomance Instance', 
	309 => 'Zul\`Gurub Instance', 
	329 => 'Stratholme Instance', 
	349 => 'Mauradon Instance', 
	369 => 'Deeprun Tram', 
	389 => 'Ragefire Chasm Instance', 
	409 => 'The Molten Core Instance', 
	429 => 'Dire Maul Instance', 
	449 => 'Alliance PVP Barracks', 
	450 => 'Horde PVP Barracks', 
	451 => 'Development Land', 
	469 => 'Blackwing Lair Instance', 
	489 => 'Warsong Gulch', 
	509 => 'Ruins of Ahn\`Qiraj Instance', 
	529 => 'Arathi Basin',
	531 => 'Temple of Ahn\`Qiraj Instance', 
	533 => 'Naxxramas Instance',
);

$zone = Array(
	0 => Array(
		Array(700,10,1244,1873,'Undercity',1497),
		Array(-840,-1330,-5050,-4560,'Ironforge',1537),
		Array(1190,200,-9074,-8280,'Stormwind City',1519),
		Array(-2170,-4400,-7348,-6006,'Badlands',3),
		Array(-500,-4400,-4485,-2367,'Wetlands',11),
		Array(2220,-2250,-15422,-11299,'Stranglethorn Vale',33),
		Array(-1724,-3540,-9918,-8667,'Redridge Mountains',44),
		Array(-2480,-4400,-6006,-4485,'Loch Modan',38),
		Array(662,-1638,-11299,-9990,'Duskwood',10),
		Array(-1638,-2344,-11299,-9918,'Deadwind Pass',41),
		Array(834,-1724,-9990,-8526,'Elwynn Forest',12),
		Array(-500,-3100,-8667,-7348,'Burning Steppes',46),
		Array(-608,-2170,-7348,-6285,'Searing Gorge',51),
		Array(2000,-2480,-6612,-4485,'Dun Morogh',1),
		Array(-1575,-5425,-432,805,'The Hinterlands',47),
		Array(3016,662,-11299,-9400,'Westfall',40),
		Array(600,-1575,-1874,220,'Hillsbrad Foothills',267),
		Array(-2725,-6056,805,3800,'Eastern Plaguelands',139),
		Array(-850,-2725,805,3400,'Western Plaguelands',28),
		Array(2200,600,-900,1525,'Silverpine Forest',130),
		Array(2200,-850,1525,3400,'Tirisfal Glades',85),
		Array(-2250,-3520,-12800,-10666,'Blasted Lands',4),
		Array(-2344,-4516,-11070,-9600,'Swamp of Sorrows',8),
		Array(-1575,-3900,-2367,-432,'Arathi Highlands',45),
		Array(600,-1575,220,1525,'Alterac Mountains',36),
	),
	1 => Array(
		Array(2698,2030,9575,10267,'Darnassus',1657),
		Array(326,-360,-1490,-910,'Thunder Bluff',1638),
		Array(-3849,-4809,1387,2222,'Orgrimmar',1637),
		Array(-1300,-3250,7142,8500,'Moonglade',493),
		Array(2021,-400,-9000,-6016,'Silithus',1377),
		Array(-2259,-7000,4150,8500,'Winterspring',618),
		Array(-400,-2094,-8221,-6016,'Un\`Goro Crater',490),
		Array(-590,-2259,3580,7142,'Felwood',361),
		Array(-3787,-8000,1370,6000,'Azshara',16),
		Array(-1900,-5500,-10475,-6825,'Tanaris',440),
		Array(-2478,-5500,-5135,-2330,'Dustwallow Marsh',15),
		Array(360,-1536,-3474,-412,'Mulgore',215),
		Array(4000,-804,-6828,-2477,'Feralas',357),
		Array(3500,360,-2477,372,'Desolace',405),
		Array(-804,-5500,-6828,-4566,'Thousand Needles',400),
		Array(-3758,-5500,-1300,1370,'Durotar',14),
		Array(1000,-3787,1370,4150,'Ashenvale',331),
		Array(2500,-1300,4150,8500,'Darkshore',148),
		Array(3814,-1100,8600,11831,'Teldrassil',141),
		Array(3500,-804,-412,3580,'Stonetalon Mountains',406),
		Array(-804,-4200,-4566,1370,'The Barrens',17),
	),
);

$def = Array(
	'character_race' => Array(
		1 => 'Human',
		2 => 'Orc',
		3 => 'Dwarf',
		4 => 'Night Elf',
		5 => 'Undead',
		6 => 'Tauren',
		7 => 'Gnome',
		8 => 'Troll',
		9 => 'Goblin',
	),

	'character_class' => Array(
		1 => 'Warrior',
		2 => 'Paladin',
		3 => 'Hunter',
		4 => 'Rogue',
		5 => 'Priest',
		7 => 'Shaman',
		8 => 'Mage',
		9 => 'Warlock',
		11 => 'Druid',
	),
);

class DBLayer
{
	var $link_id;
	var $query_result;
	var $saved_queries = array();
	var $num_queries = 0;

	function DBLayer($db_host, $db_username, $db_password, $db_name)
	{
		$this->link_id = @mysql_connect($db_host, $db_username, $db_password, true);

		if ($this->link_id)
		{
			if (@mysql_select_db($db_name, $this->link_id))
				return $this->link_id;
			else
				error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
		}
		else
			error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
	}

	function query($sql)
	{
		$this->query_result = @mysql_query($sql, $this->link_id);

		if ($this->query_result)
		{
			++$this->num_queries;
			return $this->query_result;
		}
		else
		{
			return false;
		}
	}


	function result($query_id = 0, $row = 0)
	{
		return ($query_id) ? @mysql_result($query_id, $row) : false;
	}


	function fetch_assoc($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
	}


	function fetch_row($query_id = 0)
	{
		return ($query_id) ? @mysql_fetch_row($query_id) : false;
	}


	function num_rows($query_id = 0)
	{
		return ($query_id) ? @mysql_num_rows($query_id) : false;
	}


	function affected_rows()
	{
		return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
	}


	function insert_id()
	{
		return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
	}


	function get_num_queries()
	{
		return $this->num_queries;
	}


	function get_saved_queries()
	{
		return $this->saved_queries;
	}


	function free_result($query_id = false)
	{
		return ($query_id) ? @mysql_free_result($query_id) : false;
	}


	function escape($str)
	{
		if (function_exists('mysql_real_escape_string'))
			return mysql_real_escape_string($str, $this->link_id);
		else
			return mysql_escape_string($str);
	}


	function error()
	{
		$result['error_sql'] = @current(@end($this->saved_queries));
		$result['error_no'] = @mysql_errno($this->link_id);
		$result['error_msg'] = @mysql_error($this->link_id);

		return $result;
	}


	function close()
	{
		if ($this->link_id)
		{
			if ($this->query_result)
				@mysql_free_result($this->query_result);

			return @mysql_close($this->link_id);
		}
		else
			return false;
	}
}

function error($message, $file, $line, $db_error = false)
{
	global $siteerrors;
	$s = "\t\t".'Error: <strong>'.$message.'.</strong>'."\n";
	echo $s;
}

function get_player_position($x,$y,$m) {
 $xpos = round(($x / 1000) * 17.7,0);
 $ypos = round(($y / 1000) * 17.7,0);
 switch ($m)
 {
   case 1:
    $pos['x'] = 154 - $ypos;
    $pos['y'] = 258 - $xpos;
    break;
   case 0:
    $pos['x'] = 572 - $ypos;
    $pos['y'] = 174 - $xpos;
	break;
   default:
    $pos['x'] = 572 - $ypos;
    $pos['y'] = 174 - $xpos;
 }
 return $pos;
}

function get_zone_name($mapid, $x, $y){
global $maps_a, $zone;

if (!empty($maps_a[$mapid]))
	{
	$zmap=$maps_a[$mapid];
	if (($mapid==0) or ($mapid==1))
		{
		$i=0; $c=count($zone[$mapid]);
		while ($i<$c)
			{
	if ($zone[$mapid][$i][2] < $x  AND $zone[$mapid][$i][3] > $x AND $zone[$mapid][$i][1] < $y  AND $zone[$mapid][$i][0] > $y) {$zmap=$zone[$mapid][$i][4]; break;}
			$i++;
			}
		}
	} else $zmap="Unknown zone";
return $zmap;
    } 

function test_realm(){
	global $serverr, $portt;
	$s = @fsockopen("$serverr", $portt, $ERROR_NO, $ERROR_STR,(float)0.5);
	if($s){@fclose($s);return true;} else return false;
}

function get_realm_name(){
	global $hostr, $userr, $passwordr, $dbr, $database_encoding;
	$realm_db = new DBLayer($hostr, $userr, $passwordr, $dbr);
	$realm_db->query("SET NAMES $database_encoding");
	$query = $realm_db->query("SELECT * FROM `realmlist`");
	$result = $realm_db->fetch_assoc($query);
	$realm_db->close();
	unset($realm_db);
	return($result['name']);
}

if (!defined("PATH_SEPARATOR"))
  define("PATH_SEPARATOR", getenv("COMSPEC")? ";" : ":");
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.dirname(__FILE__));
?>