Invalid ZIP!\n\n"); } /* Establish MySQL connection, select DB, get coords */ $link = mysql_connect($db_host,$db_user,$db_pass); mysql_select_db($db_name); $sql = "SELECT state,latitude FROM ".$db_table." WHERE zip='".$zip."' LIMIT 0,1"; $res = mysql_query($sql); if((!$res || mysql_num_rows($res) == 0) && strlen($zip) != 7) { die("ZIP not found. Try another close ZIP, perhaps?"); } elseif(strlen($zip) == 7) { $coords[0] = ""; $coords[1] = 63; } else { $coords = mysql_fetch_row($res); $coords[0] = ", ".$coords[0]; } /* Source URLs - if you change them, you may also need to change the preg_match expressions */ $url_aurora = "http://www.gi.alaska.edu/cgi-bin/predict.cgi"; $url_weather = "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=".$zip; /* File naming variable */ $fprefix = strftime("%d%b%g")."_".$zip."_"; /* Scores assigned to different weather/aurora conditions */ $aurora_score_array = array( "at its maximum peak" => 10, "high" => 9, "active" => 7, "moderate" => 5, "quiet" => 1 ); $weather_score_array2 = array( "Clear" => 0, "Mostly clear" => -2, "Partly cloudy" => -3, "A chance" => -7, "Mostly cloudy" => -7, "showers" => -2, "rain" => 0, "snow" => -2, "fog" => -2, "Cloudy" => -10, "Rain" => -10, "Showers" => -10, "Snow" => -10, ); /* Parsing source URLs and calculating scores, index, and rating */ preg_match_all("|Auroral activity will be (.+) today|Uis",get_put($url_aurora),$aurora); preg_match("/
Tonight<\/div>\s*?
(\w+)[\s]?(cloudy|clear|sunny|chance|).*?(rain|snow|showers|fog|\.)/s",get_put($url_weather),$weather2); preg_match("|(.*),([\sa-zA-Z]*?).*Forecast : Weather Underground|Uis",get_put($url_weather),$title); preg_match("|, ([0-9]*)% of the Moon is Illuminated|Uis",get_put($url_weather),$moon); ($weather2[2] == "") ? $weather_score1=intval($weather_score_array2[$weather2[1]]) : $weather_score1=intval($weather_score_array2[$weather2[1]." ".$weather2[2]]); ($weather2[3] != ".") ? $weather_score2=intval($weather_score_array2[$weather2[3]]) : $weather_score2=0; $moon_score = intval($moon[1])*.05; if((10+intval($weather_score1+$weather_score2)) < 0) { $weather_score = 0; } else { $weather_score = (10+intval($weather_score1+$weather_score2)); } $aurora_score = intval($aurora_score_array[$aurora[1][0]]); if($coords[1] > 63 && $coords[1] < 73) { $geo_correct = 1; } elseif($coords[1] < 64 && $coords[1] > 61) { $geo_correct = .95; } elseif($coords[1] < 62 && $coords[1] > 60) { $geo_correct = .9; } elseif($coords[1] < 61 && $coords[1] > 56) { $geo_correct = .85; } elseif($coords[1] < 57 && $coords[1] > 47) { $geo_correct = .7; } elseif($coords[1] < 48 && $coords[1] > 35) { $geo_correct = .1; } elseif($coords[1] < 36) { $geo_correct = .05; } if($aurora_score == 10) { $geo_correct = $geo_correct+((1-$geo_correct)/3); } if(($weather_score*$aurora_score)-$moon_score < 0) { $aurora_raw = 0; } else { $aurora_raw = (($weather_score*$aurora_score)-$moon_score)*(5*$geo_correct); } if($weather_score != 0 && $aurora_score && $weather_score) { $aurora_index = (round($aurora_raw,1)/500)*(100); } else { $aurora_index = 0; } if($aurora_score == 0) { $aurora_rating = "No Aurora Prediction"; } elseif($aurora_raw >= 0 && $aurora_raw <= 2) { $aurora_rating = "Not Visible"; } elseif($aurora_raw > 2 && $aurora_raw < 176) { $aurora_rating = "Poor"; } elseif($aurora_raw > 175 && $aurora_raw < 250) { $aurora_rating = "Moderate"; } elseif($aurora_raw > 249 && $aurora_raw < 360) { $aurora_rating = "Good"; } elseif($aurora_raw > 359 && $aurora_raw < 500) { $aurora_rating = "Excellent"; } elseif($aurora_raw >= 500) { $aurora_rating = "Get Out There!"; } /* Printing the output */ $content = ""; if(!isset($_GET["wap"]) || $_GET["wap"] == 0) { if($embed == 0) { $content .= "\n\nAurora Viewing Index :: " .$title[1].", ".$title[2] ."\n" ."\n" ."\n" ."\n"; } $content .= "
"; $content .= "\n\n\n\n" ."\n\n\n" ."\n\n\n"; if($user == 1) { $content .= "\n\n" ."\n\n" ."\n\n" ."\n\n" ."\n\n" ."\n\n" ."\n\n" ."\n\n" ; } $content .= "\n\n" ."
\n" ."Aurora Prediction For:
" .$title[1].$coords[0] ."
\n" ."
\n" ."\n" .$aurora_rating."
\n" ."\n" ."" ."
".round($aurora_index,0)."%
\n" ."
 
ZIP: 

" ."

" ."

Data used in this prediction
Latitude: ".round($coords[1],1)."° N
Lat Correction: ".((-1+$geo_correct)*100)."%
Aurora Score:
".$aurora_score." out of 10
Weather Score:
".$weather_score." out of 10
Moon Phase:
".$moon[1]."% illuminated
Moon Score: -".$moon_score."

\"Latest
Latest Autrora Image!
\n" ."
"; if($embed == 0) { $content .= "\n"; } } elseif(isset($_GET["wap"]) && $_GET["wap"] == 1) { $content = "\n" ."\n" ."\n" ."\n\n" ."\n\n" ."\n" ."

\nAurora Viewing Index for:

\n" ."".$title[1].$coords[0]."
\n" .$aurora_rating." (".round($aurora_index,0)."%)

\n" ."Enter your ZIP/postal code:
\n" ."
\n" ."Example: 99801 or V6C3C9
\n" ."View Local Aurora Viewing Index
\n" ."

\n
\n
"; } if(isset($_GET["wap"]) && $_GET["wap"] == 1) { header("Content-type: text/vnd.wap.wml"); header('Content-Disposition: inline; filename="index.wml"'); } echo $content; /* Functions */ function aurora_color($int){ return sprintf("%02X", $int); } function get_put($url) { global $path, $fprefix; $rdf = parse_url($url); /* With this new code, set $days to be the number of days old a file should be before it's deleted. If you want to modify this so it's hours, minutes, or seconds, take out the /24, /60/24, or /60/60/24 in $diff respectively: */ $i = 0; $days = 1; if($handle = opendir($path)) { $files = array(); while(false !==($file = readdir($handle))) { if($file != "." && $file != ".." && $file != "index.html") { $diff = (time() - filectime($path.$file))/60/60; if ($diff > $days) { unlink($path.$file); } else { $files[] = $file; $i++; } } } rsort($files); reset($files); closedir($handle); } if(!in_array($fprefix.$rdf['host'], $files)) { $fp = fsockopen($rdf['host'], 80, $errno, $errstr, 15); if (!$fp) { $read_contents = "Problem!"; return; } if ($fp) { fputs($fp, "GET " . $rdf['path'] . "?" . $rdf['query'] . " HTTP/1.0\r\n"); fputs($fp, "HOST: " . $rdf['host'] . "\r\n\r\n"); $contents = ""; while(!feof($fp)) { $pagetext = fgets($fp,300); $contents .= chop($pagetext); } fputs($fp,"Connection: close\r\n\r\n"); fclose($fp); $file = $fprefix.$rdf['host']; $dest = fopen($path.$file,"w"); fwrite($dest,$contents); fclose($dest); $handle = fopen($path.$file,"r"); while (!feof($handle)) { $read_contents .= fread($handle, 8192); } fclose($handle); } } elseif(in_array($fprefix.$rdf['host'], $files)) { $file = $fprefix.$rdf['host']; $handle = fopen($path.$file,"r"); while (!feof($handle)) { $read_contents .= fread($handle, 8192); } fclose($handle); } return $read_contents; } ?>