#!/usr/bin/perl
##########################################################
# CSVsearch Xtra & CSVread Xtra v 17/Aug/2006
# � 1999-2006 EZscripting.com
# Script by Alexandre Golovkine & Babelnotes.be
##########################################################
# Instructions: http://www.EZscripting.com/csvread/
# Instructions: http://www.EZscripting.com/csvsearch/
# FAQ: http://www.EZscripting.com/faq/
# Help Forum: http://www.EZscripting.com/helpforum/
##########################################################
## Template brackets are now [[fieldname]]
my $scriptname = "YWPWeather.pl";
my $CSV_file = "../YWP/Weather/Data/CurrentDB.txt";
my $HTML_template = "../YWP/Weather/Templates/WeatherPage.dwt";
my $no_matches_found = "
Sorry, No results found.
Use the back button on your browser to try again
";
my $num_random = 1;
my $records_per_page = 1000;
my $field_name_records = 'records';
my $textPrevious = "Previous";
my $textNext = "Next";
my $pageSelectorRange = 15;
my $order_desc = 0;
my $separator = '|';
my $min_query_length = 0;
my $min_query_length_error = "
Sorry, the minimum number of characters to search is $min_query_length!
Use the back button on your browser to try again
";
my $default_show_mode = 0;
my $kill_image_buttons_value = 1;
my @DateFields = ('DATE');
my @no_search_fields = ();
my @currency_fields = ();
my $server_timezone_offset = 0;
##########################################################
my $use_no_matches_template = 0;
my $no_matches_template = 'noresults.htm';
##########################################################
# Advanced Settings High Lighting
my $highLighting = 1;
my @highLightColors = ("#FFFFC1", "#CEFFCE", "#D7D7FF", "#FFCECE", "#CCCCCC");
my @no_highlight_fields = ('DATE','ID');
##########################################################
# Advanced Settings auto ID
my $ID_use = 90232;
my $ID_field_name = "ID";
##########################################################
# Advanced Settings Columns
my $use_columns = 0;
my $columns = 5;
my $tableWidth ="100%";
my $tableBorder = 0;
my $tableCellpadding = 6;
my $tableCellspacing = 0;
my $tableStyle = "";
my $tableBorderColor = "teal";
my $tableBgColor = "#ffffff";
##########################################################
# Advanced Settings Multi-database
my $field_name_database = 'mydatabase';
my %DB=();
$DB{'db1'} = 'database.txt';
$DB{'db2'} = 'database2.txt';
$DB{'db3'} = 'database3.txt';
$DB{'db4'} = 'database4.txt';
$DB{'db5'} = 'database5.txt';
##########################################################
# Advanced Settings Multi-template
my $field_name_template = 'mytemplate';
my %TMP =();
$TMP{'tp1'} = 'template.htm';
$TMP{'tp2'} = 'template.htm';
$TMP{'tp3'} = 'template.htm';
$TMP{'tp4'} = 'template.htm';
$TMP{'tp5'} = 'template.htm';
##########################################################
# Advanced Settings Include Pages
my $use_includes = 0;
my $error_on_missing = 0;
my %INCL=();
$INCL{'include1'} = "main-menu.htm";
$INCL{'include2'} = "mail-menu.htm";
$INCL{'include3'} = "main-menu.htm";
$INCL{'include4'} = "main-menu.htm";
$INCL{'include5'} = "main-menu.htm";
use strict;
my $r=shift;
$r=undef if ref($r)!~/^Apache/;
if($r){$r->content_type('text/html');}
else{print "Content-type: text/html\n\n";}
error("Error: Bad record separator!") if !$separator or $separator=~m /["'\.\\\/]/;
my $my_separator=$separator;
$my_separator="\\".$separator if $separator eq '|'or $separator eq ',' or $separator eq '.' or $separator eq '*';
my %DateFields;
for (@DateFields){$DateFields{$_}=1;}
(my $secs,my $mins,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=gmtime(time + ($server_timezone_offset * 3600));
my %INSERT=();
my %FORM=();
my $qs=$ENV{'QUERY_STRING'};
$qs=~s/(\?|\&)(|\w*\.)(x|y)=[^&\Z]*//g if $kill_image_buttons_value;
my $database;
## find database if $field_name_database
if ($field_name_database){
$database=~m/(.*)/;
$qs=~s/$field_name_database=([^\Z\&]*?)(\Z|\&)//;
$CSV_file = $DB{$1} if $1;
$database = $1 if $1;
}
error("Please, check path to your database file on head of script !") unless -f $CSV_file;
## read db
my @data = read_file($CSV_file);
chomp $data[0];
my @fields= split($my_separator, shift @data);
my $base_length = @data;
error("Data not currently available, please try again soon") if !@fields;
error("Data not currently available, please try again soon") if $base_length<1;
error("Problems loading include files") if ((!(load_includes())) && $error_on_missing);
## remove fields that are barred due to security restrictions
my $nbrforbiddenfields = @no_search_fields;
my $use_no_search_fields = 1;
if ($nbrforbiddenfields == 0)
{ $use_no_search_fields = 0; }
else
{ $use_no_search_fields = 1; }
my %forbiddenpositions;
if ($use_no_search_fields)
{
my $field; my $cnt = 0; my $forbiddenfield; my $line;
## check which fields are forbidden
foreach $field(@fields)
{ foreach $forbiddenfield(@no_search_fields)
{ if ($field eq $forbiddenfield) { $forbiddenpositions{$cnt} = "F";} }
if (!defined($forbiddenpositions{$cnt})) { $forbiddenpositions{$cnt} = "P";}
$cnt++;
}
}
## check whether some fields should not be highlighted
my $nbrnohlfields = @no_highlight_fields;
my $use_no_highlight_fields = 1;
if ($nbrnohlfields == 0)
{ $use_no_highlight_fields = 0;}
else
{ $use_no_highlight_fields = 1;}
my %forbiddenhighlights;
if ($use_no_highlight_fields)
{
my $field; my $cnt = 0; my $nohlfield; my $line;
## check which fields are forbidden
foreach $field(@fields)
{ foreach $nohlfield(@no_highlight_fields)
{ if ($field eq $nohlfield) { $forbiddenhighlights{$cnt} = "N";} }
if (!defined($forbiddenhighlights{$cnt})) { $forbiddenhighlights{$cnt} = "H";}
$cnt++;
}
}
my @conditions=split(/&/,$qs);
my $m=0;
my @header=();
my @search_value=();
my $method;
my $templateF;
my @searchTerms;
if($ID_use && $qs =~m/show=([^\&\Z]+)/){
my $show= $1;
if($field_name_template && $qs=~m/$field_name_template=([^\&\Z]+)/){
$HTML_template=$TMP{$1};
$templateF=$1;
error("Please select correct path to your template file") unless -f $HTML_template;
}
$method = "perfect";
@data = search($show, $ID_field_name);
}
else{
if($qs =~m/header=([^\&\Z]+)/){push @header,$1;}
if($qs =~m/method=([^\&\Z]+)/){$method=$1;}
foreach (@conditions){
$_ =~ s/%(..)/pack("c",hex($1))/ge;
(my $name, my $value) = split(/=/, $_);
if (checkval($_))
{
if($name eq 'search'){
push(@searchTerms, split(/\+/, $value));
minquerylength($value) if length($value) < $min_query_length;
@data = search($value, $header[0]);
$value=~tr/+/ /;
$value=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
push @search_value, $value;
}
elsif($name eq 'header' or $name eq 'method'){}
elsif($name eq 'hl')
{ if ($value eq 'on') { $highLighting = 1;}
elsif ($value eq 'off') { $highLighting = 0;}
}
elsif($value =~ /ez_range\(([\d\.]+),([\d\.]+)\)/)
{ push(@conditions,"$name>=$1"); push(@conditions,"$name<=$2"); }
elsif($name eq $field_name_records){$records_per_page=$value;}
elsif($field_name_template && $name eq $field_name_template ){
$HTML_template=$TMP{$value} if $value;
error("Please select correct path to your template file") unless -f $HTML_template;
}
elsif($name eq 'order_by' or $name eq 'order' or $name eq 'page'){$FORM{$name} = $value;}
elsif($name eq 'display'){@data = random_record();}
elsif($_=~/([^=<>!]+)([\!=<>]+)([^=<>!]+)/){@data = search($3, $1, $2); push(@searchTerms,$3);}
$m++;
}
}
}
(my $head, my $tmp, my $foot) = get_html($HTML_template);
my $result;
##sort
@data = reverse @data if $order_desc && !$FORM{order_by};
@data=order() if $FORM{order_by};
@data=random_record(1) if $FORM{order} eq 'random';
if ($highLighting)
{ my $term;
my $cnt = 0;
foreach $term(@searchTerms)
{
my $colorIndex = $cnt % (@highLightColors);
my $highLightColor = $highLightColors[$colorIndex];
my $fronthl = '';
my $endhl = '';
my $part;
foreach $part(@data)
{
my @parts = split($my_separator, $part);
my $cell = "";
my $cnt2 = 0;
foreach $cell(@parts)
{ if (($use_no_highlight_fields == 0) || ($forbiddenhighlights{$cnt2} eq "H"))
{ my $temp1 = $cell;my $temp2 = "";
my $oldindex = 0;my $i = iindex($temp1, $term);
my $pos_in_tot_string = $i;
while ($i >= 0)
{ my $pre = substr($temp1, 0, $i);
$temp2 .= $pre;
if (not_in_tag(substr($cell, 0, $pos_in_tot_string)))
{ $temp2 .= $fronthl.$term.$endhl;
} else
{ $temp2 .= $term;}
$temp1 = substr($temp1, $i+length($term));
$oldindex = $i;
$i = iindex($temp1, $term);
$pos_in_tot_string = length($temp2) + $i; }
$temp2 .= $temp1;
$cell = $temp2; }
$cnt2++; }
$part = join($separator, @parts); }
$cnt++; }
}
## output everything.
my $p; my $w; my $pages_out;
my $total = @data;
my $end = $FORM{page}*$records_per_page + $records_per_page;
$end = $total if $end>$total;
my $tr=0;
for($w=$FORM{page}*$records_per_page; $w<$end;$w++){
my @line = split($my_separator, $data[$w]);
$m=0; %INSERT=();
foreach(@fields){$INSERT{$_} = $line[$m++];}
if ($use_columns) {
$result .= "