<?php
//
// weatherbox - Grant Root 3/3/03
//
// This routine builds a "weatherbox" display suitable for inclusion in the side
// bar of a web page. It accepts a zip code from a user, looks it up at the
// Weather Underground site, and obtains a locality path such as "US/OH/Dayton".
// This locality is then stored as a cookie on the user's system.
//
// This include file must be placed near the top of the page, before any output
// is sent to the browser (since it uses a header to set a cookie). To place the
// actual weatherbox on the page, simply "echo $weatherbox;".
//
// Copyright 2003 Grant Root (grant@rootcentral.org)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details (www.gnu.org).

$weatherbox='';
$weatherzip='';
$weatherloc='';

// See if the weather locality cookie is set
if (isset($_COOKIE['rc_weatherloc']))
{
   // Apply sanity check to cookie value
   if (strlen($_COOKIE['rc_weatherloc']) > 100)
   {
      // Delete bad cookie
      setcookie('rc_weatherloc');
   }
   else
   {
      // Get locality from cookie value
      $weatherloc = $_COOKIE['rc_weatherloc'];
   }
}

// See if user has just entered a new zip code
if (isset($_GET['weatherzip']) and $_GET['weatherzip'] <> "00000")
{
   $weatherzip = $_GET['weatherzip'];
   // Validate zip code
   $weatherbox = "(Bad zip code entered.)<br />";
   if (ereg('^[0-9]{5}$', $weatherzip))
   {
      // Find out name of locality and set a cookie
      $weatherbox = "(Couldn't find new locality.)<br />";
      if ($pageArray = file("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=$weatherzip"))
      {
         $page = implode("", $pageArray);
         if (ereg("/geo/BannerPromo/([A-Za-z/]*)\.html", $page, $regs) and strlen($regs[1]) <= 30)
         {
            $weatherbox = '';
            $weatherloc = $regs[1];
            if (!setcookie('rc_weatherloc', $weatherloc, time()+60*60*24*3000, '/', '.rootcentral.org', 0))
            {
               $weatherbox = "(Cannot set cookie!)";
            }
         }
      }
   }
}   

// If we know a location, show the weather info
if (isset($weatherloc) and $_GET['weatherzip'] <> "00000")
{
   if (ereg('^[A-Za-z]*/([A-Za-z]*)/([A-Za-z]*)$', $weatherloc, $regs))
   {
      $weatherbox .= "Weather for $regs[2], $regs[1]:<br />";
   }

   // Display weather for this location
   $weatherbox .= "<font size=\"-1\">(Click for forcast)</font><br />";
   $weatherbox .= "<a href=\"http://www.wunderground.com/$weatherloc\" target=\"_blank\">";
   $weatherbox .= "<img src=\"http://banners.wunderground.com/banner/gizmotimetemp_both/language/www/$weatherloc.gif\"";
   $weatherbox .= " alt=\"Click for Forecast\" title=\"Click for Forecast\" height=\"41\" width=\"127\" border=\"0\" /></a><br />";
   $weatherbox .= "<a href=\"" . $_SERVER['PHP_SELF'] . "?weatherzip=00000\">Change location</a>";
}
else
{
   // Provide a form for setting the zip code
   $weatherbox .= "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"get\">\n";
   $weatherbox .= "<p>Enter zip code for local weather:</p>\n";
   $weatherbox .= "<input type=\"text\" name=\"weatherzip\" size=\"5\" maxlength=\"5\" />&nbsp;";
   $weatherbox .= "<input type=\"submit\" value=\"OK\" />\n";
   $weatherbox .= "</form>";
}
?>
