Skip to content

Instantly share code, notes, and snippets.

@svpernova09
Created February 26, 2016 14:10
Show Gist options
  • Save svpernova09/c5e06aeb488b27892912 to your computer and use it in GitHub Desktop.
Save svpernova09/c5e06aeb488b27892912 to your computer and use it in GitHub Desktop.
<html lang="fi">
<head>
<meta charset="utf-8">
<style>
table { width: 100%; }
</style>
</head>
<body>
<form action="<? $_SERVER['PHP_SELF'] ?>" method="post">
<table>
<tr><td style="width: 150px; ">Wowhead search URL:</td><td><input style="width: 100%; " type="text" name="whurl"></td><td style="width: 1%;"><input type="submit" name="doit" value="Convert"></td></tr>
<?
// Wowhead search parser for TSM
// Author: Beeq (runtu at stc dot cx)
$result = "";
$allitems = Array();
if (filter_var($_POST['whurl'], FILTER_VALIDATE_URL) === false) {
exit('Please go back and submit a valid URL');
}
$url = str_replace(" ","%20",urldecode($_POST['whurl']));
$args = explode(".com",$url); // Get the URL arguments to second value into an array $args. For example http://www.wowhead.com/items=16 -> /items=16
// Prepare the data to be sent to Wowhead server
$out = "GET " . $args[1] . " HTTP/1.1\r\nHost: www.wowhead.com\r\nUser-Agent: TSM\r\nConnection: close\r\n\r\n";
// Open socket to Wowhead server
$fp = fsockopen("www.wowhead.com", 80, $errno, $errstr, 30);
if ( ! $fp ) // if socket can not be opened
{
// then terminate the script with an error message
die("$errstr ($errno)<br />\n");
}
else
{
// If socket is succesfully opened,
// Send $out request to Wowhead server
fwrite( $fp, $out );
// Loop until all the response data have been fetched from Wowhead server
while ( ! feof( $fp ) )
{
// Get part of the response data into $fetch string
$fetch = trim( htmlspecialchars( fgets( $fp,128 ) ) );
// Remove chunk checksums from final result -- START
//
if ( strlen( $fetch ) != 8 ) // If $fetch length is not 8 characters, then it's not a chunk checksum
{
$result .= $fetch; // so it can be added to the final result string, $result
}
else
{
// If $fetch length is 8 characters, then it MIGHT be a chunk cheksum
if ( ! ctype_xdigit( $fetch ) ) // However, if $fetch is not representing a hexadecimal digit,
{
$result .= $fetch; // then it can be added to the final result string, $result
}
}
//
// Remove chunk checksums from final result -- END
}
// All response data from Wowhead server should been now fetched, and the response data without chunk checksums should be in string $result
fclose($fp);
// Close socket
}
// HTML Parser -- START
//
// If anything goes wrong, the reason is here somewhere :)
//
$splitter = explode( ";_[" , $result ); // Split string $result to an array $splitter
for ( $a = 0; $a < count( $splitter ); $a++ ) // Loop through all the $splitter split results
{
$splitterb = explode( "]" , $splitter[ $a ] ); // Split each $splitter split result to an array $splitterb
if ( ctype_digit( $splitterb[ 0 ] ) ) // If the first value in array $splitterb is numeric, then that's our item ID
{
if ( ! in_array( $splitterb[ 0 ] , $allitems ) ) // If the item ID is not yet in $allitems array,
{
array_push( $allitems , $splitterb[ 0 ] ); // then let's add the item ID to it!
}
}
}
// HTML Parser -- END
$i = count ( $allitems ); // Count Item IDs in an array, this value SHOULD match the Wowhead's search
if ( $i > 0 && $i < 1000 )
{
// If search seems fine (1-999 results)..
sort( $allitems ); // Sort $allitems array from lowest to highest
echo '<tr><td>TSM result:</td><td colspan=2><input style="width: 100%; " readonly style="width: 90%" type="text" name="tsmresult" value="';
for ( $a = 0; $a < $i; $a++ ) // Loop through all the Item IDs in $allitems array
{
echo $allitems[ $a ]; // Output the next Item ID
if ( $a < $i-1 ) { echo ","; } // If there are still Item IDs left, then add a comma to the output
}
echo '"></td></tr><tr><td colspan=3>' . $i . " items found.</td></tr>";
echo '</table>';
}
if ( $i > 999 ) { echo "</table>** More than 999 results found. Please, add more filters and exclude possible beta results"; }
if ( $i < 1 ) { echo "</table>** No results. Please, check the Wowhead search URL"; }
?>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment