text
// rss-tropical-test2.php Version 1.02 - 04-Aug-2008 fixed XHTML validation issues
// added pull of contents from links in feed to fill out the
// missing description fields from the feeds.
// script available at http://saratoga-weather.org/scripts.php
//
// you may copy/modify/use this script as you see fit,
// no warranty is expressed or implied.
//
// This script gets the current RSS Tropical Cyclone Advisories released by the NHC/TPC
// from www.nhc.noaa.gov/aboutrss.shtml and provides either
// a summary (titles only, with links) or details. It returns
// 'There are no active watches, warnings or advisories'
// if there are no cited hazards in the Zone.
//
// output: creates XHTML 1.0-Strict HTML page (default)
//
// Options on URL:
// inc=Y -- returns only the body code for inclusion
// in other webpages. Omit to return full HTML.
// summary=Y -- returns only the titles of the cited hazards
// N.N or greater.
// zone=[A|P|AS] -- select the zone to use:
// 'A' for Atlantic, Caribbean, and Gulf of Mexico
// 'P' for Eastern Pacific tropical cyclones
// 'AS' for Atlantic in Spanish Language
//
// example URL:
// http://your.website/rss-tropical-test2.php?zone=A&inc=Y
// would return data without HTML header/footer for hazards in Atlantic
//
// Usage:
// you can use this webpage standalone (customize the HTML portion below)
// or you can include it in an existing page:
// no parms: include("rss-tropical-test2.php");
// parms: include("http://your.website/rss-tropical.test2.php?inc=Y&summary=Y&zone=A");
//
//
// settings:
// change $Zone default(below) to your stations county zone
// other settings are outlined below, and are optional
//
// settings ----------------------------------------------------------
if ( ! isset($_REQUEST['zone']) ) {
$_REQUEST['zone']="A"; // <=== Change this default to your Zone
}
$Zone = $_REQUEST['zone'];
$hurlURL = $_SERVER['PHP_SELF']; // <=== change this default to webpage// to open for details
// on that page, you can have the following PHP
//
// if ( ! isset($_REQUEST['zone']) ) {
// $_REQUEST['zone']="A"; // <=== Change this to your Zone
// }
// $Zone = $_REQUEST['zone'];
//
// include("http://your.website/rss-tropical-test2.php?zone=$Zone&inc=Y");
//
//
//
$HD = "h2"; // <=== type of heading for advisorys <$HD>...$HD>
// end of settings -------------------------------------------------
$RSSURL = 'https://www.nhc.noaa.gov/index-at.xml';
if ($Zone == 'P') {
$RSSURL = 'https://www.nhc.noaa.gov/index-ep.xml';
}
if ($Zone == 'AS') {
$RSSURL = 'https://www.nhc.noaa.gov/index-at-sp.xml';
}
// get request parameters
// full html output or just contents (inc=Y)?
if ( ! isset($_REQUEST['inc']) ) {
$_REQUEST['inc']="";
}
$includeOnly = $_REQUEST['inc']; // any nonblank is ok
if ($includeOnly) {$includeOnly = "Y";}
if ( isset($_REQUEST['summary'])) {
$doSummary = TRUE;
} else {
$doSummary = FALSE;
}
// the following is another way to pass the name of the page for testing
if (isset($_REQUEST['detailpage']) ) {
$hurlURL = $_REQUEST['detailpage'];
}
if (isset($_REQUEST['sce']) and $_REQUEST['sce'] == 'view' ) {
$filenameReal = __FILE__;
$download_size = filesize($filenameReal);
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: text/plain");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
header('Connection: close');
readfile($filenameReal);
exit;
}
// begin code -----------------------------------------------------------
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$lastBuildDate = "";
$tracking = "";
$Summary = "";
$WLink = 0;
$PageTitle = "";
// function for XML parsing .. invoked at start of XML element
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link, $lastBuildDate,$doSummary,$Summary, $PageTitle;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
if ($name == "LASTBUILDDATE") {
$tag = $name;
}
}
// function for XML parsing .. invoked at end of XML element
// bulk of the work is done here
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $lastBuildDate, $tracking,$doSummary,$Summary,$Zone,$hurlURL,$WLink,$HD, $PageTitle;
if ($name == "ITEM") {
if (preg_match('/\.[shp|kml]/i',$title) ) {
$title = "";
$description = "";
$link = "";
$insideitem = false;
return;
}
if (preg_match("|There are no tropical|i",$description)) {
if (! $doSummary) {
printf("%s",htmlspecialchars(trim($description)));
}
$Summary = htmlspecialchars(trim($description));
} else {
$WLink++;
$description = preg_replace("|
|s","
\n",$description);
$description = preg_replace("|href=http([^>]+)>|is","href=\"http\\1\">",$description);
if (! $doSummary) {
printf("<$HD>\n%s$HD>\n", trim($link),htmlspecialchars(trim($title)));
// printf("
Updated: %s
\n
%s\n", $lastBuildDate,trim($description)); print "\n"; if (strlen($description) < 100 ) { printf("
%s
\n",trim($description)); $text = grabContents($link); print "\n"; print "\n$text\n"; } else { $nl = strpos($description,"\n"); print "\n"; if ($nl >= 80) { printf("
%s
\n",trim($description)); } else { printf("%s\n",trim($description)); } } } $Summary .= "" . htmlspecialchars(trim($title)) . "
(.*)|Uis',$html,$stuff); $contentraw = $stuff[1][0]; // $content = explode("\n",$contentraw); // print ""; return trim($contentraw); } function fetchUrlWithoutHanging($url) // thanks to Tom at Carterlake.org for this script fragment { // Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed $numberOfSeconds=5; // Suppress error reporting so Web site visitors are unaware if the feed fails error_reporting(0); // Extract resource path and domain from URL ready for fsockopen $URLparts = parse_url($url); $domain = $URLparts['host']; $resourcePath = $URLparts['path']; // Establish a connection $socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds); if (!$socketConnection) { // You may wish to remove the following debugging line on a live Web site // print(""); } // end if else { $xml = ''; fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n"); // Loop until end of file while (!feof($socketConnection)) { $xml .= fgets($socketConnection, 128); } // end while fclose ($socketConnection); } // end else return($xml); } // end function ?>