<?php
class EWRporta_Block_Weather extends XenForo_Model
{
	public function getModule()
	{
		return array(
			'advisory_html' => $this->getAdvisory,
			'weather_html' => $this->getWeather,
		);
	}
	public function getAdvisory()
	{
		$invalid_zone = 0;
		if (!preg_match("/^[a-z]{2}[C|Z][0-9]{3}$/i", $Zone)) {
		  $invalid_zone = 1; // valid zone syntax from $DefaultZone setting
		}
		if (isset($_GET['zone']) && preg_match("/^[a-z]{2}[C|Z][0-9]{3}$/i", $_GET['zone'])) {
		  $Zone = $_GET['zone'];  // valid zone syntax from input
		} else if (isset ($_GET['zone']) && !preg_match("/^[a-z]{2}[C|Z][0-9]{3}$/i", $_GET['zone'])) {
		  $invalid_zone = 1;  // invalid zone syntax from input
		}
		if (isset ($_GET['detailpage']) ) {
		  $detailpage = $_GET['detailpage'];
		}
		if (isset($detailpage) ) {
		  $t = parse_url(urldecode($detailpage));
		  if (isset ($t['path'])) {
			$t['path'] = htmlspecialchars(strip_tags($t['path']));
			$t['path'] = preg_replace('/[^A-Za-z0-9-._]/i','', $t['path']); // character filter
			$Status .= "<!-- detailpage\n" . $t['path'] . " -->\n";
			$hurlURL = $t['path'];
		  }
		}
		if (isset ($_GET['cache']) and strtolower($_GET['cache']) == 'refresh') {
		  $refetchSeconds = 1;
		}
		$Zone = strtoupper(preg_replace('/[^A-Za-z0-9]/i', '', $Zone));
		$RSS_URL = "http://www.weather.gov/alerts-beta/wwaatmget.php?x=$Zone";
		$t = pathinfo($PHP_SELF);
		$Program = $t['basename'];
		$cacheName = preg_replace('|\.txt$|i', "-$Zone.txt", $cacheName);
		$Status = "<!-- $Version -->\n";
		$html = '';
		if (!$invalid_zone) {
		}
		else {
		  $Status .= "<!-- getting new file from $RSS_URL -->\n";
		  if (!function_exists('curl_init')) {
			$xml = GrabURLWithoutHangingTW($RSS_URL);
		  }
		  else {
			$xml = curl_fetch_file($RSS_URL,0);
		  }
		  $Status .= $curl_debug;
		  $curl_debug = '';
		  $xml = str_replace("cap:", 'cap_', $xml);
		  $junk = 'n/a';
		  if (preg_match("/\r\n\r\n/", $xml)) {
			list($junk, $xml) = explode("\r\n\r\n", $xml, 2);
		  }
		  if ($xml == '') {
			$xml = $junk;    // curl method does not have the headers
			$junk = 'n/a';
		  }
		  if (preg_match("/invalid zone/i", $xml)) {
			$xml = 'invalid zone';
		  }
		  if ($xml != 'invalid zone') { // not going to be a valid data return, do not cache it
			$fp = fopen('/home4/thebooth/public_html/xxxx/weather/advisory-VAZ055.txt', "w");
			if ($fp) {
			  $write = fputs($fp, $xml);
			  fclose($fp);
			  $Status .= "<!-- cache saved to $cacheName -->\n";
			}
			else {
			  $Status .= "<!-- unable to write $cacheName -->\n";
			}
		  }
		  if (strlen($xml) < 300 && $xml != 'invalid zone') {  // not going to be a valid data return, do cache it
			$Status .= "<!-- HTML characters length = " . strlen($xml) . " -->\n";
			$Status .= "<!-- HTML headers received: " . htmlspecialchars(strip_tags(trim($junk))) . " -->\n";
			$Status .= "<!-- HTML received: " . htmlspecialchars(strip_tags(str_replace('-->','',trim($xml)))) . " -->\n";
			$get_file_failed = 1;
		  }
		}
		} else {
		  $xml = 'invalid zone';
		}
		if ($xml == 'invalid zone') {
			  $advisory_html = "${string}${Status}<div class=\"advisoryBoxnoactive\">Top Warning Information Unavailable, invalid advisory zone selected.</div>\n";
		}
		else if ($get_file_failed == 1) {
			  $advisory_html = "${string}${Status}" . graceful_error();
		}
		else if ($get_file_failed == 2) {
			  $advisory_html = "${string}${Status}<div class=\"advisoryBoxnoactive\">Top Warning Information Unavailable, cache file conflict, view source for details.</div>";
		}
		else {
		  $advisory_html = advisory_main_code();
		}
		$advisory_html .= "<!-- zone=$Zone -->\n";
		return "$advisory_html";
	}
	public function getWeather()
	{
		$weather_html = "
			<div>
				<table style='font-size:9px;' width='100%'>
					<tr align='center' valign='top'>
						<td>{xen:raw $forecasticons[0]}</td>
						<td>{xen:raw $forecasticons[1]}</td>
						<td>{xen:raw $forecasticons[2]}</td>
						<td>{xen:raw $forecasticons[3]}</td>
						<td>{xen:raw $forecasticons[4]}</td>
						<td>{xen:raw $forecasticons[5]}</td>
						<td>{xen:raw $forecasticons[6]}</td>
					</tr>
					<tr align='center' valign='top'>
						<td>{xen:raw $forecasttemp[0]}</td>
						<td>{xen:raw $forecasttemp[1]}</td>
						<td>{xen:raw $forecasttemp[2]}</td>
						<td>{xen:raw $forecasttemp[3]}</td>
						<td>{xen:raw $forecasttemp[4]}</td>
						<td>{xen:raw $forecasttemp[5]}</td>
						<td>{xen:raw $forecasttemp[6]}</td>
					</tr>
				</table>
			</div>
			<div>
				<table>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[0]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[0]}</td>
					</tr>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[1]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[1]}</td>
					</tr>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[2]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[2]}</td>
					</tr>
					<tr>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[3]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[3]}</td>
					</tr>
					<tr>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[4]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[4]}</td>
					</tr>
					<tr>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[5]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[5]}</td>
					</tr>
					<tr>
						<td colspan=7><hr/></td>
					</tr>
					<tr>
						<td><b>{xen:raw $forecasttitles[6]}</b></td>
						<td></td>
						<td colspan=5>{xen:raw $forecasttext[6]}</td>
					</tr>
			</div>";
		return "$weather_html";
	}
}