1. <?php
  2. if(!class_exists('wowhead')) {
  3. class wowhead extends itt_parser {
  4. public static $shortcuts = array('puf' => 'urlfetcher');
  5. public $av_langs = array('en' => 'en_US', 'de' => 'de_DE', 'fr' => 'fr_FR', 'ru' => 'ru_RU', 'es' => 'es_ES');
  6. public $settings = array(
  7. 'itt_icon_loc' => array(
  8. 'type' => 'text',
  9. 'default' => 'https://wow.zamimg.com/images/wow/icons/large/'
  10. ),
  11. 'itt_icon_small_loc' => array(
  12. 'type' => 'text',
  13. 'default' => 'https://wow.zamimg.com/images/wow/icons/small/'
  14. ),
  15. 'itt_icon_ext' => array(
  16. 'type' => 'text',
  17. 'default' => '.jpg'
  18. ),
  19. 'itt_default_icon' => array(
  20. 'type' => 'text',
  21. 'default' => 'inv_misc_questionmark'
  22. )
  23. );
  24. private $searched_langs = array();
  25. public function __destruct(){
  26. unset($this->searched_langs);
  27. parent::__destruct();
  28. }
  29. protected function searchItemID($itemname, $lang, $searchagain=0) {
  30. $searchagain++;
  31. $this->pdl->log('infotooltip', 'wowhead->searchItemID called: itemname: '.$itemname.', lang: '.$lang.', searchagain: '.$searchagain);
  32. $item_id = 0;
  33. // Ignore blank names.
  34. $name = trim($itemname);
  35. if (empty($name)) { return null; }
  36. $encoded_name = urlencode($name);
  37. $encoded_name = str_replace('+' , '%20' , $encoded_name);
  38. $lang_prefix = ($lang == 'en') ? 'www' : $lang;
  39. $item_encoded_name = $encoded_name;
  40. $item_encoded_name = str_replace("%C3%", "%C3%20%", $item_encoded_name);
  41. $url = 'http://'.$lang_prefix.'.wowhead.com/item='.$item_encoded_name.'&xml';
  42. $this->pdl->log('infotooltip', 'Search for ItemID at '.$url);
  43. $item_data = $this->puf->fetch($url);
  44. $xml = simplexml_load_string($item_data);
  45. if(is_object($xml)) {
  46. $item_id = (int)$xml->item->attributes()->id;
  47. } else {
  48. $this->pdl->log('infotooltip', 'Invalid XML');
  49. }
  50. //Use normal search
  51. $url = 'http://'.$lang_prefix.'.wowhead.com/search?q='.$encoded_name;
  52. $search_data = $this->puf->fetch($url);
  53. $arrSearchMatches = array();
  54. preg_match_all("/\"id\":([0-9]*),\"level\":([0-9]*),\"name\":\"([0-9])".$itemname."\"/", $search_data, $arrSearchMatches);
  55. if (isset($arrSearchMatches[1]) && count($arrSearchMatches[1])){
  56. $arrUniqueIDs = array_unique($arrSearchMatches[1]);
  57. //Take the first one
  58. $item_id = $arrUniqueIDs[0];
  59. }
  60. //search in other languages
  61. if(!$item_id AND $searchagain < count($this->av_langs)) {
  62. $this->pdl->log('infotooltip', 'No Items found.');
  63. if(count($this->config['lang_prio']) >= $searchagain) {
  64. $this->pdl->log('infotooltip', 'Search again in other language.');
  65. $this->searched_langs[] = $lang;
  66. foreach($this->config['lang_prio'] as $slang) {
  67. if(!in_array($slang, $this->searched_langs)) {
  68. return $this->searchItemID($itemname, $slang, $searchagain);
  69. }
  70. }
  71. }
  72. }
  73. $debug_out = ($item_id > 0) ? 'Item-ID found: '.$item_id : 'No Item-ID found';
  74. $this->pdl->log('infotooltip', $debug_out);
  75. return array($item_id, 'items');
  76. }
  77. protected function getItemData($item_id, $lang, $itemname='', $type='items'){
  78. $orig_id = $item_id;
  79. if(!$item_id) {
  80. $item['baditem'] = true;
  81. return $item;
  82. }
  83. $bonus = 0;
  84. $myItemData = array(
  85. 'ench' => 0,
  86. 'gems' => array(),
  87. 'lvl' => 0,
  88. 'spez' => 0,
  89. 'upgd_type' => 0,
  90. 'bonus' => array(),
  91. 'num_bonus' => 0,
  92. 'upgd_id' => '',
  93. );
  94. $arrItemData = explode(':', $item_id);
  95. if (count($arrItemData) > 1){
  96. $item_id = $arrItemData[0];
  97. //Detect if old or new (6.2.0) format
  98. $intTotalCount = count($arrItemData);
  99. $intBonusCount = $arrItemData[12];
  100. $isUpgrade = ((int)$arrItemData[10] != 0) ? true : false;
  101. $intShouldCount = 13 + $intBonusCount + (($isUpgrade) ? 1 : 0);
  102. $blnIsNewFormat = ($intShouldCount === $intTotalCount) ? true : false;
  103. $arrBonus = array();
  104. if($blnIsNewFormat){
  105. foreach($arrItemData as $key => $val){
  106. if ($key == 1) $myItemData['ench'] = $val;
  107. if ($key == 8) $myItemData['lvl'] = $val;
  108. if ($key == 9) $myItemData['spez'] = $val;
  109. if ($key == 10) $myItemData['upgd_type'] = $val;
  110. if ($key == 11) $myItemData['inst_diff'] = $val;
  111. if ($key == 12) $myItemData['num_bonus'] = $val;
  112. if ($key > 2 && $key < 7){
  113. $myItemData['gems'][] = $val;
  114. }
  115. if ($key > 12 && $key < (13+$myItemData['num_bonus'])){
  116. $myItemData['bonus'][] = $val;
  117. }
  118. if($key > (12+$myItemData['num_bonus'])){
  119. $myItemData['upgd_id'] = $val;
  120. }
  121. //124139:0 : 0: 0: 0: 0: 0: 0: 100: 267: 0: 6: 1: 567
  122. //itemID:enchant:gemID1:gemID2:gemID3:gemID4:suffixID:uniqueID:linkLevel:specializationID:upgradeTypeID:instanceDifficultyID:numBonusIDs:bonusID1:bonusID2
  123. }
  124. } else {
  125. foreach($arrItemData as $key => $val){
  126. if ($key == 1) $myItemData['ench'] = $val;
  127. if ($key == 8) $myItemData['lvl'] = $val;
  128. if ($key == 9) $myItemData['upgd_id'] = $val;
  129. if ($key > 2 && $key < 7){
  130. $myItemData['gems'][] = $val;
  131. }
  132. if ($key > 11){
  133. $myItemData['bonus'][] = $val;
  134. }
  135. }
  136. //112417:0:0:0:0:0:0:0:lvl90:upg 491:dif 5:2:448:449
  137. //itemID:enchant:gem1:gem2:gem3:gem4:suffixID:uniqueID:level:upgradeId:instanceDifficultyID:numBonusIDs:bonusID1:bonusID2...
  138. }
  139. }
  140. $item = array('id' => $item_id);
  141. $url = ($lang == 'en') ? 'www' : $lang;
  142. $item['link'] = 'http://'.$url.'.wowhead.com/item='.$item['id'].'&power&bonus='.implode(':', $myItemData['bonus']).'&upgd='.$myItemData['upgd_id'].'&lvl='.$myItemData['lvl'].'&ench='.$myItemData['ench'].'&gems='.implode(',',$myItemData['gems']);
  143. $this->pdl->log('infotooltip', 'fetch item-data from: '.$item['link']);
  144. $someJS = $this->puf->fetch($item['link'], array('Cookie: cookieLangId="'.$lang.'";'));
  145. if ($someJS){
  146. $arrMatches = array();
  147. $intCount = preg_match("/name_(.*): '(.*)',(\s*)quality: (.*),(\s*)icon: '(.*)',(\s*)tooltip_(.*): '(.*)'/", $someJS, $arrMatches);
  148. if ($intCount){
  149. $item['name'] = htmlentities(stripslashes($arrMatches[2]));
  150. $html = $arrMatches[9];
  151. $template_html = trim(file_get_contents($this->root_path.'games/wow/infotooltip/templates/wow_popup.tpl'));
  152. $item['html'] = str_replace('{ITEM_HTML}', stripslashes($html), $template_html);
  153. $item['lang'] = $lang;
  154. $item['icon'] = htmlentities($arrMatches[6]);
  155. $item['color'] = 'q'.(int)$arrMatches[4];
  156. //Reset Item ID, because the full name is the one we should store in DB
  157. $item['id'] = $orig_id;
  158. return $item;
  159. } else {
  160. $this->pdl->log('infotooltip', 'no match found');
  161. $item['baditem'] = true;
  162. return $item;
  163. }
  164. } else {
  165. $this->pdl->log('infotooltip', 'no data from URL');
  166. $item['baditem'] = true;
  167. return $item;
  168. }
  169. $item['baditem'] = true;
  170. return $item;
  171. }
  172. }
  173. }
  174. ?>