#!/usr/bin/perl use vars '$hw_cgi_path'; $hw_cgi_path = '/home/sites/www.conventionplanit.com/web/weather'; use lib '/home/sites/www.conventionplanit.com/web/weather/hamlib'; #require perl 5.004 minimum require 5.004; # lets load up or needed libraries use strict; use HW::Common; use autouse 'HW::DBaccess' => qw(new_dbaccess); use HW::Cookies; use HW::SimpleIni; #set the global variables use vars qw($DEBUG $cfg %var %pp); $|=1; ######### Configuration Section # load a our default ini $cfg = HW::SimpleIni->new (-config_dir => "$hw_cgi_path/configs", -file => "$hw_cgi_path/configs/hw3.ini", -default => 'Defaults'); # Constants my $settings = 'SystemSettings'; my $defaults = 'Defaults'; %var = (base => $hw_cgi_path); #init pps %pp = (loaded=>{}, routine=>{}, pre=>[], post=>[]); my %FORM = (); my $cookies = new HW::Cookies if ($cfg->val($settings, 'allow_cookies')); if ($ENV{'REMOTE_HOST'} && $cfg->val('SystemSettings', 'ipbanning')) { (my $ip = my $orig_ip = $ENV{'REMOTE_HOST'}); $ip =~ s/\.+/_/g; $ip =~ s/[^\d_]//g; my $banned; my $banning_dir = $hw_cgi_path . '/' .$cfg->val('Paths', 'ipbanning'); if (-d "$banning_dir") { if (-e "$banning_dir/$ip.banned") { $banned =1; } } elsif (-e "$banning_dir/bannedips.txt") { open (BANNEDIPS, "$banning_dir/bannedips.txt"); while (!$banned && ) { chomp; $banned = 1 if ($orig_ip eq $_); } close (BANNEDIPS); } if ($banned) { print "Location: " . $cfg->val('SystemSettings', 'ipbanned_url') . "\n\n"; exit; } } #print "Content-type: text/html\n\nDEBUG MODE ON
"; &parse_input(\%FORM, $cfg->val($settings, 'allow_command_line')); #$DEBUG=0;$FORM{debug} = 0;$var{debug} = 0; &process_input(\%FORM, \%var, $cfg, $cookies); #Lets set the debug mode now # We set to the default or use user input setting if defaukt is 1 # lets print out initial debug info if debug on $DEBUG = $cfg->val($settings, 'debug_mode'); $DEBUG = $FORM{debug} if ($DEBUG == 1); if ($DEBUG) { $cfg->DEBUG(1); print "Content-type: text/html\n\n"; print "DEBUG INFO
\n"; &print_hash('ENV', \%ENV, 'FORM', \%FORM); print "
\n

HW3.ini settings
\n"; &print_cfg($cfg); print "


\n"; } # end if ($DEBUG) # lets get the config info as needed my ($config, $tforecast, $nofconfig) = @FORM{ 'config', 'forecast', 'nofc'}; $var{configs} = $config; foreach my $name (split(/,/, $config)) { next if !$name; $cfg->Import ( "$hw_cgi_path/configs/$name.ini", 'if_not_loaded') if ( -e "$hw_cgi_path/configs/$name.ini"); } $tforecast =~ s/\W//g; $cfg->Import ("$hw_cgi_path/configs/fc_$tforecast.ini", 'if_not_loaded') if ($tforecast && !$nofconfig && -e "$hw_cgi_path/configs/fc_$tforecast.ini"); # if logging is turned on then lets log. if ($cfg->val('SystemSettings', 'logging')) { my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time); $mon++; $year+=1900; open (LOGFILE , ">>$hw_cgi_path/" . $cfg->val('Paths', 'logs') . "/log$year" . sprintf('%02u%02u',$mon,$mday) . '.txt'); print LOGFILE "$ENV{'REMOTE_HOST'}|" . $year . sprintf('-%02u-%02u|%02u:%02u:%02u',$mon,$mday,$hour,$min,$sec) . "|$ENV{'REQUEST_METHOD'}|$ENV{'QUERY_STRING'}|$ENV{'HTTP_USER_AGENT'}|$ENV{'HTTP_REFERER'}\n"; close (LOGFILE); } &print_cfg($cfg) if $DEBUG; print "

" if $DEBUG; $var{scripturl} = $cfg->val('Paths', 'scripturl'); #lets clean the cahe directory if need be &clean_cache($hw_cgi_path. '/' . $cfg->val('Paths', 'cache'), split(/,/, $cfg->val('CleanCacheDirs', 'clean_cache'))); my ($error, $out, $t, $wx_lib, @hashes); if ($FORM{forecast} eq 'pass') { my $pass = $var{'pass'}; $t = $cfg->val('PassTemplates', $pass); $out = 1; unless ($FORM{dpp} eq '0') { &process_place(\%FORM, \%var, $cfg, $var{'allow_mp'}, $hw_cgi_path .'/'. $cfg->val('Paths', 'base'), $cfg->val('Paths', 'zipcodedb'), $cfg->val('Paths', 'altplacesdb'), $cfg->val('Paths', 'zonedb'),$cfg->val('Paths', 'metardb'), $cfg->val('Paths', 'us_counties'), $cfg->val('Paths', 'icaodb'),$cfg->val('Paths', 'citiesdb')); if ($var{'dopass'}) { $t = $cfg->val('PassTemplates', $var{'mult_places_pass'}); } } } # $|=1; # my $mime = $cfg->val('SystemSettings', 'mime_type'); # print 'Content-Type: ' . $mime . "\n\n" if $mime; if (!$t) { ($error, $out, $t, $wx_lib, @hashes) = &get_forecast_info($cfg, \%FORM, \%var, 1, $hw_cgi_path, $var{'allow_mp'}, $DEBUG); } #print "t=$t
"; &cgidie("Error : $error") if $error; $out =1; if ($out && $cfg->val('Defaults', 'output mode') != 2 && $var{om} < 2) { &cgidie ("Error: No template set") if !$t; require HW::Template; my $template_dir = $cfg->val('Paths', 'templates'); $template_dir = 'templates' if !$template_dir; #if not a full path must be relative to cgi path if ($template_dir !~ m%^/|[A-Za-z]:%) { $template_dir = $hw_cgi_path . '/' . $template_dir; } $template_dir =~ s/\r//g; &load_parse_plugins($cfg, \%pp, \%var); my $template = new HW::Template($template_dir . '/include', 0, 'html'); print "t= $template_dir/$t\n\n" if $DEBUG; $var{country} = 'us' if (!$var{country} && !$FORM{country}); #set up the HW include special fields.. #i.e IMAP=path here thus in template we can allow # %%HWI=IMAP:xxxxx%% foreach my $hwi_sec ($cfg->Parameters('HWI Sections')) { my $val = $cfg->val('HWI Sections', $hwi_sec); $val = $hw_cgi_path . '/' . $val if ($val !~ m%^/|[A-Za-z]:%); $val =~ s/%%(\w+)%%/my $a = $var{$1} || $FORM{$1}; $a;/ge; $template->$hwi_sec($val); } #handle alt tag. if there. we first try & get file from AltTemplate ini # if not then if there is no 'alt template security' set in ini we try # the alt param with 'html' extension.. if all fails we default back to # the standard template set in $t if ($var{alt}) { (my $alt = $var{alt}) =~ s/\W//g; my $temp_alt = $cfg->val('AltTemplates', $alt); if (!$temp_alt && !$cfg->val('SystemSettings', 'alt template security')) { $temp_alt = $alt . '.html' if ($alt && -e "$template_dir/$alt.html"); } $temp_alt .= '.html' if ($temp_alt !~ /\./); $t = $temp_alt if ($temp_alt && -e "$template_dir/$temp_alt"); } &load_hwv($cfg, \%var); $|=1; if ($cookies) { $cookies->set_cookies(\%FORM, $var{cookie}, 0,0,$cfg->val('SystemSettings', 'server_name'), $cfg->val('SystemSettings', 'script_name')); } my $mime = $cfg->val('SystemSettings', 'mime_type'); print 'Content-Type: ' . $mime . "\n\n" if $mime; $template->print_template("$template_dir/$t",\&hw3_parse_line, [\%var, \%FORM, $wx_lib, @hashes]); } exit; ################################################## # Subroutines ################################################## sub load_hwv { my ($cfg, $var,$mode) = @_; my $hwvs = $cfg->SectionParms('HWV Presets'); foreach my $name (keys %{$hwvs}) { $name = 'hwv' . $name if ($name !~ /^hwv/); $$var{$name} = $$hwvs{$name} if (!$mode || !exists $$var{$name}); } } ####### # Loads all the parser plugins that are set in the configs sub load_parse_plugins { my ($cfg, $pp, $var) = @_; my $plugins = $cfg->SectionParms('Parse Plugins'); foreach my $name (keys %{$plugins}) { if ((split(/\|/, $$plugins{$name}))[4]) { &load_parse_plugin($cfg, $pp, $var, $name); } } } #load a parser plugin if not previously loaded sub load_parse_plugin { my ($cfg, $pp, $var, $name) = @_; my $parms = $cfg->val('Parse Plugins', $name); return if (!$name || $$pp{loaded}{$name}); my ($pm, $routine, $pre, $post) = split(/\|/, $parms); (my $reqpm = $pm) =~ s!::!/!g; require "$reqpm.pm"; $$pp{loaded}{$name} = new $pm($cfg,\%var, $DEBUG); $$pp{routine}{$name} = $routine; push (@{$$pp{pre}}, $name) if $pre; push (@{$$pp{post}}, $name) if $post; } #un load a parser plugin if previously loaded sub unload_parse_plugin { my ( $pp, $name) = @_; $$pp{loaded}{$name} = undef $$pp{routine}{$name} = undef } sub get_forecast_info { my ($cfg, $FORM, $var, $allow_process_place, $hw_cgi_path, $allow_mult, $DEBUG) = @_; my $forecast = ($$FORM{forecast}) ? $$FORM{forecast} : $cfg->val($defaults, 'forecast'); $forecast = lc trim($forecast); my $forecast_info = $cfg->val('ForecastTypes', $forecast); if ($allow_process_place && (split(/,/, $forecast_info))[5] == 1) { &process_place($FORM, \%var, $cfg, $allow_mult, $hw_cgi_path .'/'. $cfg->val('Paths', 'base'), $cfg->val('Paths', 'zipcodedb'), $cfg->val('Paths', 'altplacesdb'), $cfg->val('Paths', 'zonedb'),$cfg->val('Paths', 'metardb'), $cfg->val('Paths', 'us_counties')); if ($forecast=~/z(?:one|andh)/ && $$var{country} !~ m/^(?:us|ca)$/) { $forecast='tafzone'; $forecast_info = $cfg->val('ForecastTypes', $forecast); } if ($$var{'dopass'} && $allow_mult) { return ('', 1, $cfg->val('PassTemplates', $$var{'mult_places_pass'}), '', @hashes); } } &cgidie ("The forecast type $forecast is not a valid type") if (!$forecast_info); $$FORM{forecast} = $forecast; my ($lib, $code, $tcode) = (split(/,/, $forecast_info))[3,4,5]; ################################## # Load in Library & call routine #print "loading lib file $lib
\n" if $DEBUG; #print "loading lib file $lib
\n"; require "HW3Plugins/$lib.pm" || &cgidie ("Missing needed library file $lib"); my $lib_name = 'HW3Plugins::' . $lib; my $wx_lib = new $lib_name ($cfg, $DEBUG); #print "wxlib= " . $wx_lib . "\n"; my ($error, $out, $t, @hashes)=$wx_lib->$code(\%var, \%FORM, $forecast, $forecast_info, $hw_cgi_path); ################################## #print "forecast = $forecast
error=$error
t=$t
out=$out : !
\n"; return ($error, $out, $t, $wx_lib, @hashes); } sub do_forecast { my $args = &trim(shift); my $allow_mult = shift; my $ref_hashes = shift; my (%args, %tvar); &parse_input(\%args, 0, 0, $args); &process_input(\%args, \%var, $cfg); # if we need to load a new config then lest do it my ($config, $tforecast, $nofconfig) = (@args{'config', 'forecast', 'nofc'}); my $configs = $var{configs}; foreach my $name (split(/,/, $config)) { next if (!$name || index(0,$configs, $name) > -1); $cfg->Import ( "$hw_cgi_path/configs/$name.ini",'if_not_loaded') if ( -e "$hw_cgi_path/configs/$name.ini"); } $tforecast =~ s/\W//g; $cfg->Import ( "$hw_cgi_path/configs/fc_$tforecast.ini", 'if_not_loaded') if ($tforecast && !$nofconfig); &load_hwv($cfg, \%var); my $mode = $args{mode}; foreach my $key (%args) { $FORM{$key} = $args{$key}; } my ($error, $out, $t, $wx_lib, @hashes) = &get_forecast_info($cfg, \%FORM, \%var, $mode, $hw_cgi_path, $allow_mult, $DEBUG); #$ref_hashes = [$wx_lib, @hashes, @$ref_hashes]; unshift (@$ref_hashes, $wx_lib); #foreach my $hash (@hashes) { unshift (@$ref_hashes, $hash); } } # end sub do_forecast sub hw3_parse_line { local ($_) = shift; my $post_parse = shift; #return $_ if $post_parse; my ( $template, $print_now, $fh, $pm, $ref_extra_parse, $ref_hashes) = @_; if (!$post_parse) { #pre parse s/%%COOKIE:(\w+)%%/my $a = $var{'chips'}{$1} if (ref($var{'chips'}) eq 'HASH'); $a;/eg; s/%%FORECAST\s(.+)\sFORECAST%%/&do_forecast($template->parse_line($1, 0,$fh, $pm, $ref_extra_parse, $ref_hashes), 0, $ref_hashes);""/eg; s!%%CONFIG=(.+?)%%! my $a = $template->parse_line($1, 0,$fh, $pm, $ref_extra_parse, $ref_hashes); $a =~ s%\W%%g; $cfg->Import ( "$hw_cgi_path/configs/$a.ini") if ($a && -e "$hw_cgi_path/configs/$a.ini"); &load_hwv($cfg, \%var); ''!egx; s!%%USE=(.+?)%%! my $a = $template->parse_line($1, 0,$fh, $pm, $ref_extra_parse, $ref_hashes); &load_parse_plugin($cfg,\%pp, \%var, $a); '';!egx; s!%%UNUSE=(.+?)%%! my $a = $template->parse_line($1, 0,$fh, $pm, $ref_extra_parse, $ref_hashes); &unload_parse_plugin(\%pp, $a); '';!egx; foreach my $pp_name (@{$pp{pre}}) { my $rou = $pp{routine}{$pp_name}; my $plugin = $pp{loaded}{$pp_name}; ($_, $pm) = $plugin->$rou($_, $post_parse,$template, $print_now, $fh, $pm, $ref_extra_parse, $ref_hashes) if $plugin; } } else { #post parse foreach my $pp_name (@{$pp{post}}) { my $rou = $pp{routine}{$pp_name}; my $plugin = $pp{loaded}{$pp_name}; ($_, $pm) = $plugin->$rou($_, $post_parse,$template, $print_now, $fh, $pm, $ref_extra_parse, $ref_hashes) if $plugin; } } return ($_,$pm) } ####################### # print_hash # # print out the cfg values ####################### sub print_hash { my %hashes = @_; foreach my $hash (sort keys %hashes) { print "
\nHASH = $hash
\n"; my $hash_ref = $hashes{$hash}; foreach my $key (sort keys %$hash_ref) { print " $key = $$hash_ref{$key}
\n"; } } } # end sub print_hash ####################### # print_cfg # # print out the cfg values ####################### sub print_cfg { my $cfg = shift; foreach my $section (sort $cfg->Sections) { print "
\nSECTION = $section
\n"; foreach my $param (sort $cfg->Parameters($section)) { my $value = $cfg->val($section, $param); print " $param = $value
\n"; } } } # end sub print_cfg ############################################# ## process_input ############################################# sub process_input { my ($form, $var, $cfg, $get_cookies) = @_; if ($get_cookies) { my $kill_pands = 1 if (!exists $$form{'pands'} && (exists $$form{'place'} || exists $$form{'fips'} || exists $$form{'county'} || exists $$form{'icao'})); ($$var{cookie}, $$var{chips}) = $get_cookies->get_cookies($form, 'HW3', $$form{'debug'}); $$form{'pands'} = undef if $kill_pands; } my $configs = "$$form{user},$$form{theme},$$form{config}"; $configs =~ s/[^\w,]//g; $configs =~ s/,,+//g; $$form{config} = ($configs) ? $configs : $cfg->val('Defaults', 'configs'); ($$var{alt} = $$form{alt}) =~ s/\W//g; ($$var{pass} = $$form{pass}) =~ s/\W//g; if ($$form{maxdays}) { $$var{maxdays} = $$form{maxdays}; } elsif (!$$var{maxdays}) { $$var{maxdays} = ( $cfg->val('Defaults', 'maxdays') || 5); } $$var{'mode'} = (exists $$form{'map'}) ? $$form{'map'} : $cfg->val('Defaults', 'map'); if ($$form{'daysonly'}) { $$var{'daysonly'} = $$form{'daysonly'}; } elsif (!$$var{'daysonly'}) { $$var{'daysonly'} = ($cfg->val('Defaults', 'daysonly') || 0); } $var{om} = (exists $$form{'om'}) ? $$form{'om'} : $cfg->val('Defaults', 'output mode'); my $forecast = lc trim($$form{'forecast'}) || lc trim($$form{'do'}); $forecast = $cfg->val('Defaults', 'forecast') if (!$forecast); $$form{'forecast'} = $forecast; $$var{'allow_mp'} = (exists $$form{'allow_mp'}) ? $$form{'allow_mp'} : $cfg->val('Defaults', 'allow_multiple_places'); } ############################################# ## process_place ############################################# sub process_place { my ($form, $var, $cfg, $allow_mult, @db_paths) = @_; my $full_state; my ($error, $alt_zone_info, $alt_cc_info, $tzone, $tcwa, $tcounty, $tzipcode, $lat, $lon, $tlat, $tlon, $elev, $telev, $tzname, $ttzname, $tzdif, $ttzdif, $closeby, $full_country); my $db_access = &new_dbaccess(@db_paths); $db_access->DEBUG($DEBUG); my ($forecast, $zipcode, $pands, $place, $state, $country, $county, $icao, $zone, $fips, $radaricao) = (@$form{'forecast', 'zipcode', 'pands', 'place', 'state', 'country', 'county', 'icao', 'zone', 'fips','radar_icao'}); $country = lc &trim($country); $country =~ s/[^a-z]//g; $country = $cfg->val('Defaults', 'country') if (!$country); $zipcode = trim($zipcode); $pands = $zipcode if $zipcode; $pands = trim($pands); $fips = $county if (!$fips && $county =~ m/^\d\d\d\d\d$/); if ($zipcode || $pands =~ m/^\d+$/) { $pands =~ s/\W//g; $zipcode = $pands; ($zipcode, $place, $state, $country) = $db_access->get_zipcode_info($zipcode, $country); } elsif ($pands =~ m/^(.+),(.+),\s*(.+)$/) { ($place, $state, $country) = ($1, $2, $3); } elsif ($pands =~ /^(.+?)(?:,(.+)|[, ]+([A-Za-z][A-Za-z]))$/) { ($place, $state) = ($1, $2 || $3); print "woohoo
pands=$pands
place = $place
\n" if $DEBUG; } elsif($pands) { my $t_country; if ($pands =~ m/d\.?c\.?/i) { $place = 'washington'; $state ='dc'; $t_country='us'; } else { (my $total, $place, $state, $t_country, my @rest) = $db_access->check_for_city($pands, $country,$allow_mult); print "allow_mult=$allow_mult... total=$total
\n" if $DEBUG; if ($total > 1 && $allow_mult && $cfg->val('Defaults', 'pass_template_if_multiple')) { $$var{'dopass'} = 1; $$var{'mult_places_pass'} = $cfg->val('Defaults', 'pass_template_if_multiple'); $$var{'mult_places'} = [$place, $state, $t_country, @rest]; $$var{'mult_place'} = sub { return $var{'mult_places'}[$_[0]]; }; $$var{'mult_places_total'} = $total; return; } } if ($place) { $country = $t_country; } else { ($state, $full_state) = $db_access->match_state($pands, $country); if ($state) { $place = ''; $forecast = 'statehourly'; } # end if ($state) else { my ($tcountry, $tfull_country) = $db_access->match_country($pands); if ($tcountry) { $country = $tcountry; $full_country = $tfull_country; $place = ''; $forecast = 'country'; } elsif ($pands =~ /\w\w\w\w/) { $icao=$pands; } } } # end else } # end elsif($pands) elsif ($fips =~ m/^\d\d\d\d\d$/ && (lc $country eq 'us' || !$country)) { ($place, $state, $zone, $lon, $lat) = $db_access->get_place_from_fips($fips); $county = $fips; $place .= " county" if ($place !~ m/\scounty$/); } $place = lc $place; if ($country eq 'us' && ($place =~ /^(?:washington\s+)?d\.?c\.?/ || $place eq 'washington' && $state =~ /d\.?c\.?/i)) { $place = 'washington'; $state = 'dc'; } $place = $db_access->get_place_from_zone($zone, $state, $country) if ($zone && $state && !$place); $icao = $cfg->val('Defaults', 'icao') if (($forecast eq 'metar' || $forecast eq 'tafzone') && !$icao); if ($icao) { ($error, $place, $state, $full_country, $country, $tzdif, $lat, $lon, $elev, $tzname) = $db_access->get_icao_data($icao); print "error=$error" if $DEBUG; if (!$error) { $tzname = "GMT$tzdif" if !$tzname;} } $place = $cfg->val('Defaults', 'place') if !$place && !$icao; $place = lc &trim($place); $place =~ s/^mc(\w)/mc $1/; $place =~ s/^ft\.? /fort /; $place =~ s/^mt\.? /mount /; $place =~ s/^st(e?).? /saint$1 /; $state = lc &trim($state); $state = $cfg->val('Defaults', 'state') if !$state && !$icao; ($state, $full_state) = $db_access->match_state($state, $country) if !$full_state; if ($place && $state ) { ($error, my $found, $alt_zone_info, $alt_cc_info, $tzone, $tcounty, $tcwa, $tzipcode, $tlat, $tlon, $telev, $ttzname,$ttzdif, $radaricao, $closeby) = $db_access->check_for_alt_place($place, $state, $country); if ($error || !$found) { print "error=$error" if $DEBUG; $alt_zone_info = ($zone) ? "$zone:2" : "$place:0"; $alt_cc_info = ($icao) ? "$icao:2" : "$place:0"; $tzone = $tcounty = $tzipcode = $closeby = ''; } if (!$alt_cc_info) {$alt_cc_info = ($icao) ? "$icao:2" : "$place:0";} } $zipcode = $tzipcode if !$zipcode; $zone = $tzone if !$zone; $county = $tcounty if !$county; ($lat, $lon) = ($tlat, $tlon) if ($lat eq '' || $lon eq ''); $elev = $telev if !$elev; ($tzdif, $tzname) = ($ttzdif, $ttzname) if ($tzdif eq '' && !$tzname); $$var{closeby_total} = (my @close = split(/:/, $closeby)); if ($cfg->val('SystemSettings', 'DST') && index($cfg->val('SystemSettings', 'TZnames_with_DST'), uc $tzname)>-1) { if ($tzdif <0) {$tzdif++; } else { $tzdif--;} $tzname =~ s/S/D/i; } $country = 'us' if !$country; @$var{'forecast', 'zipcode', 'place', 'state', 'full_state', 'country', 'icao', 'zone', 'county', 'alt_zone_info', 'alt_cc_info', 'lat', 'lon', 'elev', 'tzname', 'tzdif','closeby','cwa', 'fips', 'radar_icao', 'full_country'} = ($forecast, $zipcode, $place, $state, $full_state, $country, $icao, $zone, $county, $alt_zone_info, $alt_cc_info, $lat, $lon, $elev, $tzname, $tzdif, $closeby,$tcwa, $fips, $radaricao, $full_country); } # end sub process_place ################################# # checksum ################################# sub checksum { my $str = shift; my $checksum = 0; for (my $i=0; $i