Skip to content

Instantly share code, notes, and snippets.

@six0h
Forked from bmonkman/SpaceNorad.php
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save six0h/9727717 to your computer and use it in GitHub Desktop.
Save six0h/9727717 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
// CONFIGURATION
define('STORAGE_DIR', "/var/tmp"); // Folder to store files in
define('COOKIE_FILE', STORAGE_DIR . "/sn-run"); // File to store Cookie
define('GAME_NUMBER', "5517444584046592"); // Game number from URL
define('MY_ID', 5); // Player id
define('USERNAME', '');
define('PASSWORD', '');
$allies = array(); // Optional ally ids - disable notifications for these players if it gets too annoying.
$from = $to = ""; // Notification address
$msg = ''; // Empty email message
$alert = false; // Turn Red-Alert Off
// SETUP
// ################################################################### //
touch(COOKIE_FILE); // Make sure cookie file exists
clearFleets(); // Clear out old cache files
// DATA REQUEST
// ################################################################### //
// Get data, log in if necessary
if(!$data = getData())
{
doLogin(USERNAME,PASSWORD);
$data = getData();
// Make sure an error wasn't returned
if(isset($data['event']) && $data['event'] == 'order:error') exit();
}
// NORAD FORENSICS AND MAIL CURATOR
// ################################################################### //
// Loop through fleets and check data
foreach ($data['report']['fleets'] as $fleet)
{
// Get player ID for Fleet
$playerId = $fleet['puid'];
// Get Destination Star for current fleet
(isset($fleet['o'][0][1]) ?
$dest = $fleet['o'][0][1]
:$dest = '');
// If this fleet is not you, or your allies, and has a destination
if ($playerId != MY_ID && !in_array($playerId, $allies) && !empty($dest))
{
// Destination Information
$destName = @$data['report']['stars'][$dest]['n'];
$destOwner = @$data['report']['stars'][$dest]['puid'];
$destOwnerName = @$data['report']['players'][$destOwner]['alias'];
// Galaxy Information
$currentSystem = @$fleet['ouid'];
$currentSystemName = @$data['report']['stars'][$currentSystem]['n'];
// Carrier Information
$fleetName = $fleet['n'];
$fleetShips = $fleet['st'];
$playerName = $data['report']['players'][$playerId]['alias'];
// Write and check cache file
// Cache based on fleet name and destination. If a fleet/dest pair already exists, don't bother notifying
$cacheFile = "/var/tmp/NORAD-{$fleetName}-{$destName}";
if (file_exists($cacheFile))
{
touch($cacheFile);
continue; // Move on to next fleet
}
touch($cacheFile);
// Append message for Email
$msg .= "{$playerName} - {$fleetShips} ships ({$fleetName}) spotted heading for {$destName} ({$destOwnerName})!\n";
if ($destOwner == MY_ID)
{
$alert = true;
$msg .= "SPACE NORAD SAYS: WAKE THE F UP!\n";
}
}
}
// MAILER
// ################################################################### //
if(!empty($msg))
{
// Set email subject if alert has been set
$subject = $alert ? 'Fleet Movement - INCOMING ATTACK' : 'Fleet Movement';
} else {
$msg = "There are no fleet movements at this time";
$subject = "No Fleet Movement at this time";
}
// If email content has been set
if (!empty($msg))
mail($to, $subject, $msg, "From: {$from}");
// FUNCTIONS
// ################################################################### //
// Clear old cache files for flights that have reached their destination
function clearFleets() {
foreach(glob("/var/tmp/NORAD*") as $fleet)
{
if (filemtime(STORAGE_DIR . "/{$fleet}") < (date('U') < 60))
{
unlink(STORAGE_DIR . "/{$fleet}");
}
}
};
// Make call to get Game Data
function getData()
{
$opts = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "type=order&order=full_universe_report&version=7&game_number=".GAME_NUMBER,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => COOKIE_FILE,
CURLOPT_COOKIEJAR => COOKIE_FILE,
);
$ch = curl_init('http://triton.ironhelmet.com/grequest/order');
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$err = curl_errno($ch);
$msg = curl_error($ch);
curl_close($ch);
if ($err != 0)
{
return false;
}
return json_decode($response, true);
}
// Login and store cookie
function doLogin($username, $password)
{
$opts = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "type=login&alias={$username}&password={$password}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => COOKIE_FILE,
CURLOPT_COOKIEJAR => COOKIE_FILE,
);
$ch = curl_init('http://triton.ironhelmet.com/arequest/login');
curl_setopt_array($ch, $opts);
$response = curl_exec($ch);
$err = curl_errno($ch);
$msg = curl_error($ch);
curl_close($ch);
if ($err != 0)
{
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment