// set download method from main settings
($useCURL == true) ? $dldata = 'GetDataByCurl' : $dldata = 'getWSBystream';
// set the time zone
date_default_timezone_set("$ourTZ");
// check for map in folder
if($useWFOmap == true and !file_exists($ws_mapfolder.$mapname.$ift)){ // IF use map and the map is not in the folder
$useWFOmap = false; // turn off map and use drop down menu
$noted .= "<!-- The required map is missing -->\n"; // display notice
}
// template map - ./folder/mapname.extension
$mainmap = $ws_mapfolder.$mapname.$ift;
// copy default WFO code
$wswfo = $NWSregion;
// Set the DEFAULT weather story & image
(in_array($wswfo, $hpwxs)) ? $wxsurl = '
http://www.weather.gov/'.$wswfo.'/' : $wxsurl = '
http://www.weather.gov/'.$wswfo.'/weatherstory';
// IF the WFO is using https
if(in_array($wswfo, $usehttps)){$wxsurl = preg_replace('/https:/', 'https:', $wxsurl);} // change http to https
// auto clean cache file
$defcachedata = $ws_cachefolder.'wsdata_'.$NWSregion.'.php'; // default WFO data file
if(is_file($defcachedata) and filemtime($defcachedata) < strtotime("today")){ // IF default WFO cache file is found AND the filetime is older than today
if($autocacheclean == true and $ws_cachefolder != './cache/'){ // IF auto cache clean is true AND not in a default cache folder
$cfs = array(); // set array
$i = 0; // set counter
foreach (glob($ws_cachefolder."ws*.*") as $cachedfile) { // FOR EACH file in the cache folder
if(filemtime($cachedfile) < strtotime("today")){ // IF the file time is older than today - before 12:00am
$cfs[] = filesize($cachedfile); // set the cache file size
unlink($cachedfile); // delete the file
$i++; // increment counter
}
}
$dfs = array_sum($cfs); // get the deleted cache file size sum
$fb = getFileSize($dfs); // set to mbytes or gbytes
$noted .= "<!-- Cache files deleted: $i - $fb -->\n"; // display notice
}
}
// print_r($wxsurl); echo "<br />\n";
// get submitted location
if(isset($_GET['i']) and strlen($_GET['i']) == "3"){ // IF a WFO was selected
$newwswfo = $_GET['i']; // get the location
$wswfo = htmlspecialchars(strip_tags($newwswfo)); // clean the location
$wxsurl = '
http://www.weather.gov/'.$wswfo.'/weatherstory'; // set the link
if(in_array($wswfo, $hpwxs)){ // IF the WS is on the WFO home page
$wxsurl = '
http://www.weather.gov/'.$wswfo.'/'; // change the path
}
if(in_array($wswfo, $usehttps)){ // IF the WFO is using https
$wxsurl = preg_replace('/http:/', 'https:', $wxsurl); // change http to https
}
// MAP CREATION
if($useWFOmap){ // IF using the map feature
$mm = imagecreatefrompng($mainmap); // creat map from map template
$orig_size = getimagesize($mainmap); // get original map size
$red = imagecolorallocate($mm, 255, 0, 0); // designate red because of county color
imagecolortransparent($mm, $red); // make transparent color
$im = imagecreatetruecolor($orig_size[0], $orig_size[1]); // create blank image the size of the map
$id = imagecreatefrompng($wfomaps.$wswfo.'.png'); // create image from county alert png file
imagecopymerge($mm, $id, 0, 0, 0, 0, $orig_size[0],$orig_size[1] ,100); // merge the county image to the map image
imagedestroy($id); // destroy the original county image
imagepng($mm, $ws_cachefolder.$mapname.$ift,2); // save the image
imagedestroy($im); // destroy the transparent image
$swfoi = $ws_cachefolder.$mapname.$ift; // set selected WFO image
}
}
else{ // OR ELSE if no selection, create default location map
if($useWFOmap){
if(!file_exists($ws_cachefolder.$mapname.$NWSregion.'.png')) { // IF the WFO boundary image is found
$mm = imagecreatefrompng($mainmap); // create image from US map file
$orig_size = getimagesize($mainmap); // get original map size
$red = imagecolorallocate($mm, 255, 0, 0); // designate red because of county color
imagecolortransparent($mm, $red); // make transparent color
$im = imagecreatetruecolor($orig_size[0], $orig_size[1]); // create blank image the size of the map
$id = imagecreatefrompng($wfomaps.$wswfo.'.png'); // create image from county alert png file
imagecopymerge($mm, $id, 0, 0, 0, 0, $orig_size[0],$orig_size[1] ,100); // merge the county image to the map image
imagedestroy($id); // destroy the original county image
imagepng($mm, $ws_cachefolder.$mapname.$NWSregion.'.png',2); // save the image
imagedestroy($im); // destroy the transparent image
}
$swfoi = $ws_cachefolder.$mapname.$NWSregion.'.png'; // set default WFO image
}
}
// get submitted image
if(isset($_GET['ifn'])){ // IF a headline was selected
$imName = htmlspecialchars(strip_tags($_GET['ifn'])); // clean up the name
$wxsimage = '
http://www.weather.gov/images/'.$wswfo.'/wxstory/'.$imName.'.png'; // set image path
$noted .= "<!-- $imName selected -->\n"; // display notice
}
// create unique cache filenames
$wsimg = $ws_cachefolder.'wsimg_'.$wswfo.'.png'; // Weather Story image
$ws_cachefile = $ws_cachefolder.'wsdata_'.$wswfo.'.php'; // Weather Story text
// update cache file policy
if(file_exists($ws_cachefile)) { // IF the data cache file exists
$cft = filemtime($ws_cachefile); // get cache file last modified time
$cfAge = $uts - $cft; // get cache file age in seconds
if($cfAge < $updateTextTime) { // IF cache file has not expired
$updateCache = false; // don't update
$noted .= "<!-- Cached data displayed -->\n"; // display notice
}
if(!empty($_GET['mu'])) { // IF you want to manually update the report webpagefilename.php?mu=1
$updateCache = true; // set update cache
$noted .= "<!-- manually updated -->\n"; // display notice
}
}
// IF the WFO code is in the above array, set the location
if(array_key_exists($wswfo, $fo)){
$nfo = $fo[$wswfo];
}
if($updateCache) { // IF the cache file needs updating
if(!$html = $dldata($wxsurl)) { // IF the weather story is not found
$noted .= "<!-- First attempt in getting NWS URL data failed -->\n"; // create note
sleep(1); // wait a second !
if(!$html = $dldata($wxsurl)) { // IF the second chance WS fails
sleep(1); // wait a second...
$noted .= "<!-- Second attempt in getting NWS URL data failed -->\n"; // create note
if(!$html = $dldata($wxsurl)) { // IF the last chance to get the WS failed
$html = ''; // set data to null
$noted .= "<!-- FAILURE: Could not get the WS data after 3 attempts -->\n"; // create note
}
}
}
// fix characters
$html = preg_replace('/°/', '°', $html); // change it to an entity
$html = preg_replace('/‘/', '\'', $html); // change stray character
$html = preg_replace('/<u>/', '', $html); // fix error
$html = preg_replace('/<\/u>/', '', $html); // fix error
$html = preg_replace('/<br>/', '<br />', $html); // fix error
// MAIN IMAGE URL
preg_match('|c-tab__content">.*image"><img s.*"(.*)" style|Uis', $html, $image); // search for the image
if(isset($image[1])){ // IF the image is available
$iurl = $image[1]; // pick out the image URL
if(!preg_match("|http|",$iurl)) {$iurl = '
http://www.weather.gov'.$iurl;}
$emi = explode("/", $iurl); // split up the URL
$imgMn = array_pop($emi); // get file name from the last array
$ifn = substr($imgMn, 0, -4); // cut off the extension - image file name w/o extension
$ift = substr($imgMn, -3, 3); // get the image file type/extension
$di = $dldata($iurl); // get the image
$wxsimage = $ws_cachefolder.'wsimg_'.$wswfo.'_'.$ifn.'.'.$ift; // assemble image cache file name
$wsif = fopen($wxsimage, 'w'); // open weather story image file
fwrite($wsif, $di); // write image file to cache folder
fclose($wsif); // close weather story image file
$noted .= "<!-- Weather Story image downloaded -->\n"; // display remark
// MAIN DESCRIPTION TEXT
preg_match('|is-active">.*description">(.*)</div>|Uis', $html, $story); // search for the story
(isset($story[1])) ? $wsdata = $story[1] : $wsdata = ''; // IF the story is found, set variable
// remvoe added non-story information
if($wsdata <> '' and preg_match('/<hr>/Uis', $wsdata)){ // IF data shows a horizontal rule
$wsdata = preg_replace('/<hr>.*/', ' +more', $wsdata); // delete the hotizontal line and everythig after it
}
$wsdata = preg_replace("/target='_blank'/", 'onclick="window.open(this.href,\'_blank\');return false;"', $wsdata); // fix to validate
if(mb_detect_encoding($wsdata) == 'UTF-8'){ // IF character encoding is set to URF-8
$wsdata = html_entity_decode(strip_tags($wsdata)); // decode story text html enities
$wsdata = htmlentities($wsdata); // convert all story text html entities
$noted .= "<!-- UTF-8 was detected in the main story -->\n"; // create note
}
// print_r($wsdata); echo "<br />\n";