Created
September 13, 2010 19:51
-
-
Save basicxman/577905 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* @author: Andrew Horsman | |
* @description: Humans vs. Zombies Extractor | |
*/ | |
// Configuration, meh at a second config file. | |
$grabPage = 'http://www.hvzgatech.com/?page=killboard'; | |
$sqlServ = 'localhost'; | |
$sqlUser = 'root'; | |
$sqlPass = 'pass'; | |
$sqlDb = 'humans_zombies'; | |
// Grab the markup up from Humans vs. Zombies website. | |
echo "[+] Downloading page...\n"; | |
$humansZombiesPage = file_get_contents($grabPage); | |
// Define a Hash, everytime we find a table data cell (<td>) we'll insert the | |
// value into the current iterated key (go through the keys in a loop). | |
// An itearator variable will keep track of the current key we're on, | |
// we can use array_keys to list all of the $killboard keys in a standard | |
// array (one that is indexed from 0 thus allowing us to easily iterate | |
// through keys. | |
echo "[+] Creating table structure...\n"; | |
$killboard = array( | |
"name" => array(), | |
"team" => array(), | |
"kills" => array(), | |
"lastfed" => array(), | |
"slogan" => array() | |
); | |
$boardKeys = array_keys($killboard); | |
// Initial processing of the page, deleting all instances of \n and \r so we | |
// don't have to do a multiline RegEx. Then to avoid confusing actual data | |
// with the <td> tags above the fold, delete any part of the page before | |
// the `Slogan` column. | |
echo "[+] Grabbing specific page parts...\n"; | |
$humansZombiesPage = str_replace(array("\n", "\r"), '', $humansZombiesPage); | |
$humansZombiesPage = substr($humansZombiesPage, strpos($humansZombiesPage, "Slogan</td></tr>") + 16); | |
$humansZombiesPage = substr($humansZombiesPage, 0, strpos($humansZombiesPage, "</table>")); | |
// Yes, I'm using RegEx to parse HTML | |
// http://www.urbandictionary.com/define.php?term=ctfd | |
// This RegEx matches any sub string that contains a to z, A to Z, 0 to 9 or | |
// whitespace, and is wrapped in `<td>` followed by `</td>` (although these | |
// wrapped tags are not included in the actual match data). | |
echo "[+] Parsing...\n"; | |
if (!preg_match_all('/(?<=\<td\>)[a-zA-Z0-9\s:\-]*?(?=\<\/td\>)/', $humansZombiesPage, $matches)) | |
die("[-] No data found."); | |
// preg_match_all puts each RegEx _group_ match into an array, therefore we | |
// only want the first group at $matches[0]. | |
echo "[+] Poorly coded markup parsed...\n"; | |
$matches = $matches[0]; | |
// Here we're going to iterate through every match. Each time we find a match | |
// we will place it in the next key of the $killboards Hash. Therefore, the | |
// first match will go into $killboard["name"], the next into $killboard["team"] | |
// etc... $curCell keeps track of the current key, we increment this per loop | |
// and reset it when it hits the number of keys in $boardKeys. $boardKeys | |
// contains all the keys in $killboard. | |
echo "[+] Inserting into Hash...\n"; | |
$curCell = 0; | |
for ($i = 0; $i < count($matches); ++$i) { | |
$killboard[$boardKeys[$curCell]][] = $matches[$i]; // Append the match ([]) to the proper key in $killboard. | |
++$curCell; | |
if ($curCell == count($boardKeys)) $curCell = 0; | |
} | |
// Print the table header (each column double tab separated), followed by | |
// each group of $killboard's keys. Count the number of values in lastfed | |
// because this is the last column, if a group was not fully filled up, we | |
// won't get an out of bounds issue. | |
echo "[+] Displaying table...\n"; | |
echo "Name\t\tTeam\t\tKills\t\tLast Fed\t\tSlogan\n"; | |
for ($i = 0; $i < count($killboard[end($boardKeys)]); ++$i) | |
echo $killboard["name"][$i] . "\t\t" . $killboard["team"][$i] . "\t\t" . $killboard["kills"][$i] . "\t\t" . $killboard["lastfed"][$i] . "\t\t" . $killboard["slogan"][$i] . "\n"; | |
echo "[+] Connecting to the database...\n"; | |
// Let's put this in MySQL, shall we? Top of the mornin' SQL! | |
$dbc = mysql_connect($sqlServ, $sqlUser, $sqlPass) or | |
die("[-] Error connecting to MySQL - " . mysql_error()); | |
mysql_select_db($sqlDb, $dbc) or | |
die("[-] Error selecting database - " . mysql_error()); | |
// Create the records table just in case this is the first time ran. | |
$createHumansZombieTableQueryString = | |
'CREATE TABLE IF NOT EXISTS `records` (' . | |
'`id` INT(11) NOT NULL AUTO_INCREMENT,' . | |
'`name` VARCHAR(255) NOT NULL,' . | |
'`team` VARCHAR(255) NOT NULL,' . | |
'`kills` INT(11) NOT NULL,' . | |
'`last_fed` VARCHAR(255) NOT NULL,' . | |
'`slogan` VARCHAR(255) NOT NULL,' . | |
'`recorded_time` DATETIME NOT NULL,' . | |
'PRIMARY KEY (`id`)' . | |
')'; | |
mysql_query($createHumansZombieTableQueryString, $dbc) or | |
die("[-] Could not create table - " . mysql_error($dbc)); | |
// Time to populate... | |
for ($i = 0; $i < count($killboard[end($boardKeys)]); ++$i) { | |
$tmp_name = mysql_real_escape_string($killboard["name"][$i], $dbc); | |
$tmp_team = mysql_real_escape_string($killboard["team"][$i], $dbc); | |
$tmp_kills = mysql_real_escape_string($killboard["kills"][$i], $dbc); | |
$tmp_last = mysql_real_escape_string($killboard["lastfed"][$i], $dbc); | |
$tmp_slog = mysql_real_escape_string($killboard["slogan"][$i], $dbc); | |
$recordInsertQueryString = | |
"INSERT INTO `records` (`name`, `team`, `kills`, `last_fed`, `slogan`, `recorded_time`) VALUES (" . | |
"'" . $tmp_name . "', '" . | |
$tmp_team . "', " . | |
$tmp_kills . ", '" . | |
$tmp_last . "', '" . | |
$tmp_slog . "', NOW())"; | |
mysql_query($recordInsertQueryString, $dbc) or error_log("Could not add record - " . mysql_error($dbc)); | |
} | |
echo "[+] Added records...\n"; | |
mysql_close($dbc); | |
echo "[~] All done chaps!\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment