<?php
/*
+---------------------------------------------------------------+
|       Менс Вагур из гильдии Сюрреал
|		сервера ФОРНОСТ Lotro RU
|		руку приложил 2011-01-21
+---------------------------------------------------------------+
*/

/*
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
|		2011-10-03 корректировка регулярных выражений под изменившийся HTML lorebook.lotro.com
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
*/

include_once(dirname(__FILE__) . '/urlreader.php');

class ParseLoreBookLotro
{
	function getItem($name)
    {
		$name = trim($name);
		if (empty($name))
		{
			return null;
		}
		$item['name'] = $name;
		$name = str_replace(' ', '_', $name);

    	$url = 'http://lorebook.lotro.com/index.php?title=Item:'.$name;
        $data = itemstats_read_url($url);
		
        if (preg_match('/;oldid=(\d+)">Permanent link/is', $data, $matches)) {
			$item['id'] = $matches[1];
        }
        if (preg_match('/<table class="tooltip"(.*)tc_br.gif".*?table>/is', $data, $matches)) {
            $item['lang'] = 'en';
			
			if (preg_match('/itemicon.*?src="(.*?)\.png/is', $matches[1], $iconlink))
				$item['icon'] = $iconlink[1].'.png';
			
            $item['link'] = $url;
			
			if (preg_match('/.png(.*?)<div class="(.*?)"/is', $matches[1], $htcolor))
				$item['color'] = $htcolor[2];
			
            $item['html'] = $matches[0];
			$item['html'] = str_replace('"', '\'', $item['html']);
			
            return $item;
        }
		
		echo 'Not found at ' . $url . '<br>';
        return $item;
	}

    function getItemId($item_id) 
	{
    	$url = 'http://lorebook.lotro.com/index.php?oldid='.$item_id;
        $data = itemstats_read_url($url);
        if (preg_match('/<table class="tooltip"(.*)tc_br.gif".*?table>/is', $data, $matches)) {
			global $db;
			$item['name'] = $db->sql_query_first("SELECT item_name FROM " . ITEMS_TABLE ." WHERE game_itemid = ".$item_id);
			
            $item['id'] = $item_id;
            $item['lang'] = 'en';
			
			if (preg_match('/itemicon.*?src="(.*?)\.png/is', $matches[1], $iconlink))
				$item['icon'] = $iconlink[1].'.png';
			
            $item['link'] = $url;
			
			if (preg_match('/.png(.*?)<div class="(.*?)"/is', $matches[1], $htcolor))
				$item['color'] = $htcolor[2];
			
            $item['html'] = $matches[0];
			$item['html'] = str_replace('"', '\'', $item['html']);
			
            return $item;
        }
		echo 'Not found at ' . $url . '<br>';
        return $item;
    }
}
?>