<?php

/**
 * All of the files in this directory, regardless of extension, are loaded via require_once(),
 * so be careful with what you put in here.
 **/
 
/**
 * Sample Class
 **/
 
require_once("$IP/extensions/WoWItem/includes/wowiteminfobase.class");

/**
  * This sample class will parse the HTML from CustomSite's World of Warcraft website to retrieve item  
  *    information and display it in an popup window.
  * *** This will not work as-is! Except by some bizarre coincidence.
  */
wfWoWItem_RegisterClass('customsite', array('custom''site'));
class 
customsite
{

//*******************************************************************************************************************************  
    
var $parent;

    function 
__construct()
    {
        
$url 'http://www.example.com/';
        
$search $url '?search=%s';
        
$stats $url '?item=%s';
        
$icons $url;
        
$this->parent = new wowiteminfobase(__CLASS__$url$search$stats$icons, array(
            
/*
                override any of these: 'fgcolor', 'bgcolor', 'border', 'opacity', 'fadetime', 'fadein', 'fadeout'.
                using an array with the above as the key and the new value as the element value.
             */
        
));
    }

    function 
getiteminfo($itemnumber$img false)
    {
        if (!
is_int($itemnumber))
        {
            
$itemnumber $this->getitemid($itemnumber); 
            if (!
$itemnumber && preg_match('((.*) of .*)'$itemnumber$matches)) {
                
$itemnumber $this->getitemid(trim($matches[1]));
            }
        }

        
$tmpitem $this->parent->getCachedItemInfo($itemnumber);
        if (
$tmpitem) {
            
$this->parent->saveItemInfo($this$tmpitem$itemnumber);
        }
        
$itemhtml = array(
            
'top' => '<div class="wowitem"><span class="iname"><span class="%s">%s</span></span><br />' "\n",
        );

        if (
$this->items[$itemnumber]) {
            
$results $this->items[$itemnumber];
        } else {
            
$output = array();
            
$itemid $itemnumber;
            
$page itemstats_read_url(sprintf($this->parent->stats$itemid));
            
$begin false;
            foreach(
explode("\n"$page) AS $rawData) {
                if (
preg_match('(<div id=".*?" class="tooltip" style=".*?">)'$rawData$matches)) {
                    
$begin true;
                    continue;
                }
                if (
trim($rawData) == '</div>')) {
                    
$itemhtml['stats'] .= '<br /><br />Item display is courtesy <a href="'$this->parent->url .'">'substr(str_replace('http://'''$this->parent->url), 0, -1) .'</a>';
                    break;
                }
                if (
$begin) {
                    
$begin false;
                    if (
preg_match('(<b class="quality(.*?)">(.*?)</b>)'$rawData$nameMatches)) {
                        
$popquality $nameMatches[1];
                        
$output['item_name'] = $nameMatches[2];
                    } else {
                        
$itemhtml['stats'] .= $rawData;
                    }
                }
                if (
preg_match('(<img src="(.*?)">)'$rawData$matches)) {
                    
$output['icon'] = $this->parent->icons strtolower($matches[1]);
                }
            }
            switch (
$popquality)
            {
                case 
"0":
                    
$style "greyname";
                    break;
                case 
"1":
                    
$style "whitename";
                    break;
                case 
"2":
                    
$style "greenname";
                    break;
                case 
"3":
                    
$style "bluename";
                    break;
                case 
"4":
                    
$style "purplename";
                    break;
                case 
"5":
                    
$style "orangename";
                    break;
                case 
"6":
                    
$style "redname";
                    break;
                case 
"7":
                    
$style "goldname";
                    break;
                default:
                    
$style "greyname";
                    break;
            }
        
            
$itemhtml['top'] = sprintf($itemhtml['top'], $style$output['item_name']);
            
$itemhtml['bottom'] = '</div>' "\n";
    
            
$results['itemnumber'] = $itemnumber;
            
$results['itemname'] = $output['item_name'];
            
$results['itemquality'] = $style;
            
$results['itemhtml'] = implode("\n"$itemhtml);
            
            
$temp = array();
            
$i 1;
            foreach (
explode("\n"$results['itemhtml']) AS $i => $line) {
                
$temp[$i] = trim($line);
            }
            
$results['itemhtml'] = implode($temp);
            
$results['itemicon'] = $output['icon'];
    
            
$results['itemicon'] = $this->addIconLocal($results['itemicon']);
        }
        if (
$img == '__IMG__') {
            
$results['displayitemhtml'] = str_replace('<span class="iname">''<img style="float: left" src="'.$results['itemicon'].'">' '<span class="iname">'$results['itemhtml']);
        } else if (
is_string($img)) {
            
$results['displayitemhtml'] = str_replace('<span class="iname">''<img style="float: left" src="'.$img.'">' '<span class="iname">'$results['itemhtml']);
        } else {
            
$results['displayitemhtml'] = $results['itemhtml'];
        }

        
$this->parent->saveItemInfo($this$results$itemnumber);
        return 
$results['itemname'] == "" false $results;
    }

    function 
showpopup($itemnumber$img=""$received="")
    {
        
$iteminfo    $this->getiteminfo($itemnumber);
        
$popname   $iteminfo['itemname'];
        
$popquality    $iteminfo['itemquality'];
                
        if (
$img)
        {
            if (
$img == "__IMG__") {
                
$icon $iteminfo['itemicon'];
            } else {
                
$icon $img;
            }
            
$pophtml "<img src='" $icon "' border=0 width='50' height='50'>" $iteminfo['displayitemhtml']; 
        }
        else
        {
            
$pophtml $iteminfo['displayitemhtml'];
        }
        
        if (
$iteminfo['itemname'] == "")
        {
         
             
$error "<span
                          onmouseover='return overlib(\""
rawurlencode('<span class="iname">Item not found!</span>') ."\", CAPTION, \"ERROR\", FGCOLOR, \"#FFFFFF\", CGCOLOR, \"#FF0000\", AUTOSTATUS);'
                          onmouseout='nd();'>Item Not Found</a>"
;
             return 
false;
             break;
          
        }
        if (
$received != "")
        {
            
$pophtml .= "<div class='wowitem'><span class='sm'><br> Looted by: " $received "</span></div>";
        }

        
$mypoptable =  '<a href="'sprintf($this->parent->stats$iteminfo['itemnumber']) .'" onmouseover="return overlib(''.rawurlencode($pophtml).'', FGCOLOR, '#FFFFFF', BGCOLOR, '#000000',  BORDER, '0', DECODE, CAPBELOW, FULLHTML, ';
        
$mypoptable .= ' FILTER, FILTEROPACITY,'' . $this->parent->opacity . '', FADETIME, '1', FADEIN, '6', FADEOUT, '6',';
        
$mypoptable .= ' -AUTOSTATUS);" onmouseout="return nd();" class=''.$popquality.'' target="_blank">';

        if (
$img == "")
        {
            
$mypoptable .= '[' $popname ']';
        }
        else
        {
            if (
$img == "__IMG__")
            {
               
$mypoptable .= "<img src='" $iteminfo['itemicon'] . "' border=0>";  
            }
            else
            {
               
$mypoptable .= "<img src='" $img "' border=0>"
            }
        }
        
        
$mypoptable .= '</a>';

        return 
$mypoptable;
    }
    
    function 
getitemid($wowitemname)
    {
        
$search urlencode(trim($wowitemname));
        
$page itemstats_read_url(sprintf($this->parent->search$search));
        foreach(
explode("\n"$page) AS $line) {
            
$line trim($line);
            if (
preg_match('(itemid: (.*?), name: '.*?'\)'$line$matches)) {
                
$itemid $matches[1];
                break;
            } elseif (
preg_match('(<a href="/\?item=(.*?)">.*?</a>)'$line$matches)) {
                
$itemid $matches[1];
                break;
            }
        }
        return 
$itemid;
    }
    
    function 
addIconLocal($icon) {
        return 
$this->parent->addIconLocal($icon);
    }
}

?>