<?php

set_time_limit(0);

/*///////////////////////////////////////////////////
	Includes
///////////////////////////////////////////////////*/

include_once('class.curl.php');

/*///////////////////////////////////////////////////
	Functions
///////////////////////////////////////////////////*/

function get_html($id) {
	if(!empty($id)) {
		$url = 'http://www.worldofwarcraft.com/charcopy/character-copy.html?realmId=' . $id;
	}
	$c = new curl($url);
	$c->setopt(CURLOPT_HEADER, 1);
	$c->setopt(CURLOPT_FOLLOWLOCATION, 1);
	$c->setopt(CURLOPT_RETURNTRANSFER, 1);
	$c->setopt(CURLOPT_SSL_VERIFYPEER, 0);
	$c->setopt(CURLOPT_SSL_VERIFYHOST, 0);
	$c->setopt(CURLOPT_COOKIEJAR, 'cookies.txt');
	$c->setopt(CURLOPT_COOKIEFILE, 'cookies.txt');
	$html = $c->exec();
	if ($theError = $c->hasError()) {
		echo $theError;
	}
	if($headers == 1) {
		return $c->getFollowedHeaders();
	}else{
		return $html;
	}
	$c->close();
}

function parse_html($content) {
	global $strings;
	$flag = -1;
	for($i=0; $i<count($strings); $i++) {
		if(preg_match($strings[$i], $content, $return, PREG_OFFSET_CAPTURE)) {
			$flag = $i;
			if(!empty($return[1][0])) {
				$server_name = $return[1][0];
			}else{
				$server_name = 'Unknown Error';
			}
		}else{
			$server_name = '';
		}
	}
	switch($flag) {
		case -1:
			return 'Empty';
			break;
		default:
			return $server_name;
	}
}

/*///////////////////////////////////////////////////
	Arrays
///////////////////////////////////////////////////*/

$strings = array (
	"/You must first select your realm from the drop-down lists above./sU",
	'/<td colspan = "8"  class = "serverStatus2"><small class = "blood">
You have no characters on (.*).
			<\/small><\/td>/sU',
);

$zones = array (
	"1136" => "Aegwynn",
	"1129" => "Agamaggan",
	"1137" => "Akama",
	"1282" => "Altar of Storms",
	"1293" => "Alterac Mountains",
	"1276" => "Andorhal",
	"1264" => "Anetheron",
	"1346" => "Anub'arak",
	"56" => "Archimonde",
	"69" => "Arthas",
	"1555" => "Auchindoun",
	"77" => "Azgalor",
	"1128" => "Azshara",
	"1075" => "Balnazzar",
	"1419" => "Barthilas",
	"1280" => "Black Dragonflight",
	"10" => "Blackrock",
	"1296" => "Blackwing Lair",
	"73" => "Bleeding Hollow",
	"1558" => "Blood Furnace",
	"119" => "Bloodscalp",
	"83" => "Bonechewer",
	"112" => "Boulderfist",
	"91" => "Burning Blade",
	"102" => "Burning Legion",
	"1067" => "Cho'gall",
	"1138" => "Chromaggus",
	"1556" => "Coilfang",
	"107" => "Crushridge",
	"109" => "Daggerspine",
	"1278" => "Dalvengyr",
	"157" => "Dark Iron",
	"120" => "Darkspear",
	"1351" => "Darrowmere",
	"15" => "Deathwing",
	"1286" => "Demon Soul",
	"1271" => "Dentarg",
	"79" => "Destromath",
	"81" => "Dethecus",
	"154" => "Detheroc",
	"84" => "Dragonmaw",
	"1362" => "Drak'Tharon",
	"1140" => "Drak'thul",
	"1425" => "Drakkari",
	"1429" => "Dreadmaul",
	"111" => "Dunemaul",
	"93" => "Eredar",
	"1277" => "Executus",
	"127" => "Firetree",
	"128" => "Frostmane",
	"1133" => "Frostmourne",
	"7" => "Frostwolf",
	"1141" => "Garithos",
	"92" => "Gorefiend",
	"80" => "Gorgonnash",
	"1068" => "Gul'dan",
	"1149" => "Gundrak",
	"129" => "Gurubashi",
	"1142" => "Hakkar",
	"1266" => "Haomarush",
	"57" => "Illidan",
	"1291" => "Jaedenar",
	"1144" => "Jubei'Thos",
	"155" => "Kalecgos",
	"16" => "Kel'Thuzad",
	"9" => "Kil'jaeden",
	"1146" => "Korgath",
	"101" => "Laughing Skull",
	"1295" => "Lethon",
	"95" => "Lightning's Blade",
	"78" => "Magtheridon",
	"1357" => "Maiev",
	"59" => "Mal'Ganis",
	"1148" => "Malorne",
	"70" => "Mannoroth",
	"1145" => "Mug'thol",
	"89" => "Nathrezim",
	"1131" => "Nazjatar",
	"8" => "Ner'zhul",
	"1285" => "Onyxia",
	"1427" => "Ragnaros",
	"1358" => "Rivendare",
	"76" => "Sargeras",
	"1267" => "Scilla",
	"94" => "Shadowmoon",
	"1557" => "Shattered Halls",
	"72" => "Shattered Hand",
	"74" => "Skullcrusher",
	"130" => "Smolderthorn",
	"82" => "Spinebreaker",
	"124" => "Spirestone",
	"108" => "Stonemaul",
	"58" => "Stormreaver",
	"110" => "Stormscale",
	"1433" => "Thaurissan",
	"1344" => "The Forgotten Coast",
	"1559" => "The Underbog",
	"103" => "Thunderlord",
	"11" => "Tichondrius",
	"1360" => "Tortheldrin",
	"156" => "Ursin",
	"1348" => "Vashj",
	"71" => "Warsong",
	"159" => "Wildhammer",
	"1268" => "Ysondre",
	"1259" => "Zuluhed",
	"1284" => "Aerie Peak",
	"106" => "Aggramar",
	"1070" => "Alexstrasza",
	"52" => "Alleria",
	"1418" => "Aman'Thul",
	"1363" => "Antonidas",
	"1288" => "Anvilmar",
	"1165" => "Arathor",
	"1566" => "Area 52",
	"1297" => "Arygos",
	"121" => "Azjol-Nerub",
	"1549" => "Azuremyst",
	"1190" => "Baelgun",
	"54" => "Blackhand",
	"1564" => "Blade's Edge",
	"1353" => "Bladefist",
	"64" => "Bloodhoof",
	"1371" => "Borean Tundra",
	"117" => "Bronzebeard",
	"1430" => "Caelestrasz",
	"1361" => "Cairne",
	"2" => "Cenarius",
	"66" => "Dalaran",
	"1434" => "Dath'Remar",
	"1582" => "Dawnbringer",
	"13" => "Doomhammer",
	"115" => "Draenor",
	"114" => "Dragonblight",
	"1139" => "Draka",
	"1377" => "Drenden",
	"63" => "Durotan",
	"1258" => "Duskwood",
	"1342" => "Echo Isles",
	"47" => "Eitrigg",
	"123" => "Eldre'Thalas",
	"67" => "Elune",
	"96" => "Eonar",
	"1565" => "Exodar",
	"1345" => "Fenris",
	"1576" => "Fizzcrank",
	"1581" => "Galakrond",
	"51" => "Garona",
	"1373" => "Garrosh",
	"1578" => "Ghostlands",
	"97" => "Gilneas",
	"1287" => "Gnomeregan",
	"158" => "Greymane",
	"1579" => "Grizzly Hills",
	"53" => "Hellscream",
	"1368" => "Hydraxis",
	"6" => "Hyjal",
	"14" => "Icecrown",
	"1069" => "Kael'thas",
	"98" => "Kargath",
	"65" => "Khadgar",
	"1143" => "Khaz Modan",
	"1134" => "Khaz'goroth",
	"4" => "Kilrogg",
	"1349" => "Korialstrasz",
	"1147" => "Kul Tiras",
	"1" => "Lightbringer",
	"99" => "Llane",
	"68" => "Lothar",
	"1173" => "Madoran",
	"1132" => "Malfurion",
	"104" => "Malygos",
	"62" => "Medivh",
	"1350" => "Misha",
	"1374" => "Mok'Nathal",
	"153" => "Moonrunner",
	"1182" => "Muradin",
	"1432" => "Nagrand",
	"1367" => "Nazgrel",
	"1375" => "Nesingwary",
	"1359" => "Nordrassil",
	"1262" => "Norgannon",
	"122" => "Perenolde",
	"5" => "Proudmoore",
	"1428" => "Quel'Thalas",
	"1372" => "Quel'dorei",
	"1072" => "Ravencrest",
	"1151" => "Rexxar",
	"151" => "Runetotem",
	"1153" => "Saurfang",
	"1185" => "Sen'jin",
	"85" => "Shadowsong",
	"1364" => "Shandris",
	"1354" => "Shu'halo",
	"86" => "Silvermoon",
	"131" => "Skywall",
	"160" => "Staghelm",
	"60" => "Stormrage",
	"113" => "Suramar",
	"1292" => "Tanaris",
	"90" => "Terenas",
	"1563" => "Terokkar",
	"1263" => "Thrall",
	"105" => "Thunderhorn",
	"1175" => "Trollbane",
	"1265" => "Turalyon",
	"1283" => "Uldaman",
	"116" => "Uldum",
	"1294" => "Undermine",
	"3" => "Uther",
	"1184" => "Vek'nilash",
	"1567" => "Velen",
	"55" => "Whisperwind",
	"87" => "Windrunner",
	"1355" => "Winterhoof",
	"1270" => "Ysera",
	"1572" => "Zangarmarsh",
	"61" => "Zul'jin",
	"162" => "Emerald Dream",
	"1130" => "Lightninghoof",
	"163" => "Maelstrom",
	"1352" => "Ravenholdt",
	"1289" => "The Venture Co",
	"164" => "Twisting Nether",
	"75" => "Argent Dawn",
	"1347" => "Blackwater Raiders",
	"88" => "Cenarion Circle",
	"100" => "Earthen Ring",
	"1370" => "Farstriders",
	"118" => "Feathermoon",
	"1071" => "Kirin Tor",
	"1365" => "Moon Guard",
	"126" => "Scarlet Crusade",
	"1290" => "Sentinels",
	"125" => "Shadow Council",
	"12" => "Silver Hand",
	"1356" => "Sisters of Elune",
	"1260" => "Steamwheedle Cartel",
	"1570" => "The Scryers",
	"1154" => "Thorium Brotherhood",
	"1369" => "Wyrmrest Accord",
);

/*///////////////////////////////////////////////////
	Code
///////////////////////////////////////////////////*/

for($i=1; $i<=1600; $i++) {
	if(!empty($zones[$i])) {
		$zone = $zones[$i];
	}else{
		$content = get_html($i);
		$content = parse_html($content);
		switch($content) {
			case 'Empty':
				unset($zones[$i]);
				break;
			default:
				$zones[$i] = '<span style="color: red;">'.$content.'</span>';
				echo '<p>'.$content.'</p>';
		}
		// $zones[$i] = '<span style="color: red;">Unknown</span>';
	}
	echo '<p>Finished #'.$i.'.</p>';
	flush();
	ob_flush();
}

ksort($zones);

echo '<pre>';

print_r($zones);

?>