<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://gekkoware.sourceforge.net
	*/

	if (!defined("IN-BLOCK")) die();

	/*
	<%%>
	<?xml version="1.0" encoding="UTF-8"?>
	<blockscript>
		<info lang="en">
			<title>Weather</title>
			<description>Fetchs and displays weather information from msnbc.com</description>
			<author><![CDATA[xiam &lt;<a href="mailto:xiam@users.sourceforge.net">xiam@users.sourceforge.net</a>&gt;]]></author>
			<homepage><![CDATA[<a href="http://www.gekkoware.org">http://www.gekkoware.org</a>]]></homepage>
		</info>
		<info lang="es">
			<title>Clima</title>
			<description><![CDATA[Obtiene y muestra informaci&oacute;n del clima desde msnbc.com]]></description>
		</info>
		<variable name="accid"><![CDATA[MXOA0069]]></variable>
		<variable name="units"><![CDATA[C]]></variable>
		<variable name="refresh_rate"><![CDATA[7200]]></variable>
	</blockscript>
	<%%>
	*/

	require_once GEKKO_ROOT."lib/network.lib.php";
	require_once GEKKO_ROOT."lib/file.lib.php";

	$cache_key = cache_assign_key($accid, $units);

	if ($refresh_rate < 7200)
		$refresh_rate = 7200;

	$buff = "";

	// checking cache life
	if (($life = cache_check_life($cache_key)) !== false) {
		if ((time() - $life) < $refresh_rate) {
			cache_read($cache_key, $buff);
		}
	}

	if (!$buff) {
		// no valid cache was found

		$url = "http://msnbc.com/m/chnk/d/weather_d_src.asp?acid=$accid";
		$icon_url = "http://image.weather.com/web/common/wxicons/31/";

		$tmp = @saveFile($url, "temp/", "$accid.tmp");

		if (file_exists($tmp["abs_loc"])) {

			preg_match_all("/this\.([a-z]*)\s*=\s*\"(.*)\";/i", fileread($tmp["abs_loc"]), $match);

			$buff = Array();

			if ($match) {
				foreach ($match[1] as $key => $id) {
					$val = $match[2][$key];
					$buff[strtoupper(substr($id, 2))] = $val;
				}
				// fahrenheit to celcious
				$buff["TEMP"] = ($units == "C") ? round((5/9)*($buff["TEMP"]-32))."&deg; C" : $buff["TEMP"]."&deg; F";

				// date conversion
				$u = explode(" ", $buff["LASTUP"]);

				if (count($u) == 2) {

					$d = explode("/", $u[0]);
					$h = explode(":", $u[1]);

					$buff["LASTUP"] = dateFormat(mktime($h[0], $h[1], $h[2], $d[0], $d[1], $d[2]));

					$buff["ICON"] = $icon_url.$buff["CICON"].".gif";

					$fore = explode("|", $buff["FORE"]);
					for ($i = 1; $i <= 5; $i++) {
						$buff["FORECAST_{$i}_DATE"] = $fore[$i+4];
						$buff["FORECAST_{$i}_WEEKDAY"] = $fore[$i-1];
						$buff["FORECAST_{$i}_LOW_TEMP"] = ($units == "C") ? round((5/9)*($fore[$i+9]-32))."&deg; C" : $fore[$i+9]."&deg; F";
						$buff["FORECAST_{$i}_HI_TEMP"] = ($units == "C") ? round((5/9)*($fore[$i+19]-32))."&deg; C" : $fore[$i+19]."&deg; F";
						$buff["FORECAST_{$i}_ICON"] = $icon_url.$fore[$i+29].".gif";
					}

					// saving cache
					$buff = serialize($buff);
					cache_save($cache_key, $buff);
				} else {
					$outdate = true;
				}

			} elseif ($life !== false || isset($outdate)) {

				// couldn't fetch fresh information, using outdated cache
				cache_read($cache_key, $buff);

			}
			@unlink($tmp["abs_loc"]);
		}
	}

	$return = "";
	if ($buff && (($buff = @unserialize($buff)) !== false)) {
		// html
		$tpl = new ContentBlock();
		$tpl->load("blocks/weatherBlock.tpl");
		$tpl->set_arr($buff);
		$return = $tpl->make();
	}

?>