* weatherfeed.php - Converts weather.com xml feeds to JSON

Call it like this: 
	<script type="text/javascript" src="weatherfeed.php?z=14450"></script>

You can also specify a callback function with the callback query param:
	<script type="text/javascript" src="weatherfeed.php?z=14450&callback=my_callback"></script>

In order to disconnect this script from weather.com's servers refresh.php must be 
run as a cron job to update the weather feeds.  This script will connects to weather.com's 
servers if the zipcode doesn't already have a file in the cache folder.


// we need a zip, or this whole exercise is shite.
if (!$_GET['z']) exit;

//** If not...we make a cache & double-check our vars so we don't get effed
// these variable checking functions need error-checking! Exiting on error is not useful!
if(!preg_match('/^\d{5}$/', $_GET['z'])) { echo 'bad zip (not 5 digits)'; exit; }

//** identify cache file
$CACHE_FOLDER = 'cache/'; // Apache must have read/write permission for this folder
$json_cache = $CACHE_FOLDER . $_GET['z'];

//** If cache exists, we're all good
if(file_exists($json_cache)) { 
	$json = implode('',file($json_cache));
} else {
	// Fetch and process the weather feed
	$weather_feed = new weathercom_feed($_GET['z']);

	$json = "var weather_json = " . $weather_feed->parseCurrentWeather();
	$json .= "\nvar forecast_json = " . $weather_feed->parseForecast();
	// Write cache file
	$fp = fopen($json_cache, 'w');
	fwrite($fp, $json);

// echo out the json so the web site can have it's precious data!
header("Cache-Control: maxage=300"); // HTTP/1.1
header('Content-type: application/javascript');
echo $json;