#!/usr/bin/perl
##########################################################
# CSVread Xtra 11/May/2004
# © 1999-2004 EZscripting.co.uk
# Script by Alexandre Golovkine
##########################################################
# Instructions: http://www.EZscripting.co.uk/csvread/
# FAQ: http://www.EZscripting.co.uk/faq/
# Help Forum: http://www.EZscripting.co.uk/helpforum/
##########################################################
my $CSV_file = "/home/content/N/i/n/NinaSutton/html/databases/cj.txt";
my $HTML_template = "/home/content/N/i/n/NinaSutton/html/templates/cj_headline.htm";
my $scriptname = "http://www.dealsfordivas.com/cgi/mall_cj.pl";
my $no_matches_found = "Sorry, this retailer is coming soon!";
my $num_random = 0;
my $records_per_page = 50;
my $textPrevious = "Previous";
my $textNext = "Next";
my $pageSelectorRange = 25;
my $order_desc = 1;
my $separator = '|';
my $min_query_length = 0;
my $default_show_mode = 0;
my $kill_image_buttons_value = 1;
my @DateFields = ('DATE');
##########################################################
# Advanced Settings auto ID
my $ID_use = 0;
my $ID_field_name = "ID";
##########################################################
my $field_name_database = 'database';
my %DB=(); # Do not edit this line
$DB{'db1'} = '/home/content/N/i/n/NinaSutton/html/databases/1869846_12895.txt';
##########################################################
my $field_name_template = 'template';
my %TMP =(); # Do not edit this line
$TMP{'template1'} = '/home/content/N/i/n/NinaSutton/html/templates/cj_headline.htm';
##########################################################
# EZscripting.co.uk © 1999 - 2004 Copyright Darren Deans
# The scripts are available for private and commercial use.
# You can use the scripts in any website you build.
# It is prohibited to sell the scripts in any format to anybody.
# The scripts may only be distributed by EZscripting.co.uk.
# The redistribution of modified versions of the scripts is prohibited.
# EZscripting.co.uk accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts.
# By downloading and using this script you agree to the terms and conditions.
##########################################################
use strict;
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);
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_template){
$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("You have bad file!") if !@fields;
error("Database is clear!") if $base_length<1;
my @conditions=split(/&/,$qs);
my $m=0;
my @header=();
my @search_value=();
my $method;
if($ID_use && $qs =~m/show=([^\&\Z]*)/){
my $show= $1;
if($field_name_template && $qs=~m/$field_name_template=([^\&\Z]*)/){
$HTML_template=$TMP{$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){
(my $name, my $value) = split(/=/, $_);
if($name eq 'search'){
error("Sorry, the minimum number of characters to search is $min_query_length!
") 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($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($2, $1, "!=");}
elsif($_=~/([^=<>!]+)>=([^=<>!]+)/){@data = search($2, $1, ">=");}
elsif($_=~/([^=<>!]+)<=([^=<>!]+)/){@data = search($2, $1, "<=");}
elsif($_=~/([^=<>!]+)>([^=<>!]+)/) {@data = search($2, $1, ">");}
elsif($_=~/([^=<>!]+)<([^=<>!]+)/) {@data = search($2, $1, "<");}
elsif($_=~/([^=<>!]+)=([^=<>!]+)/) {@data = search($2, $1);}
elsif($_=~/([^=<>!]+)!!([^=<>!]+)/){@data = search($2, $1, "!!");}
$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';
###date!
#
###matched data
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;
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++];}
$result.=get_record($tmp);
}
my $pages;
$qs=~s/page=([^\&\Z]*)//;
$qs=~s/\&\&/\&/;
$qs=~s/\&\Z//;
$qs =~s/"/"/g;
$qs .="&database=$database" if $database;
#set page selector
$pages_out= "$textPrevious " if $FORM{page}>0;
my $pageStart=0;
my $pageEnd = $total/$records_per_page;
if($pageSelectorRange){
$pageStart=$FORM{page}- int($pageSelectorRange/2) if $FORM{page}- int($pageSelectorRange/2)>0;
if($FORM{page}$p|;}
}
$pages_out.= " $textNext" if $FORM{page} < $total/$records_per_page-1;
$INSERT{'#_matches'} = @data;
$INSERT{'#_total'} = $base_length;
$INSERT{'#_pages'} = $pages;
$INSERT{'#_page_selector'}=$pages_out;
$INSERT{'#_search'} = join(", ", @search_value);
$INSERT{'#_date'}=(1900+$year)."-".($mon+1)."-$mday";
$INSERT{'#_time'}="$hour".":"."$mins".":"."$secs"." GMT";
if ($default_show_mode){
$result=get_record($no_matches_found)."
";
$INSERT{'#_matches'} = 0;
$INSERT{'#_total'} = 0;
$INSERT{'#_pages'} = 1;
$INSERT{'#_page_selector'}=1;
}
unless (@data){
$result = get_record($no_matches_found)."
";
$INSERT{'#_page_selector'} = 1 ;
}
print get_record($head), $result, get_record($foot);
undef $result; undef $head; undef $foot;
exit;
#########################################################
sub order{
my @myArr=();
my @new=();
my %myHash=();
my $position;
my $i=0; my $ind; my $key;
foreach(@fields){$position=$i if $_ eq $FORM{order_by}; $i++;}
$i=0;
foreach(@data){
my @line=split($my_separator,$_);
chomp $line[$position];
$line[$position] = modify_date($line[$position], 1) if $DateFields{$FORM{order_by}};
$ind = "$line[$position].$i";
$myHash{"$ind"} = $i;
$myArr[$i] = "$ind";
$i++;
}
if($FORM{order} eq '123' or $FORM{order} eq '321'){@myArr= sort { $a <=> $b } @myArr;}
else{@myArr = sort {lc $a cmp lc $b } @myArr;}
@myArr = reverse @myArr if $FORM{order} eq '321' or $FORM{order} eq 'cba';
$i=0;
foreach $key (@myArr){$new[$i++]= @data[$myHash{$key}];
}
return @new;
}
sub random_record{
my @new_data=();
my $my_num_random;
error("Can't display less than 1 random records. Set \$num_random to a value greater than 1.") if $num_random < 1;
if($_[0] or $num_random > @data){$my_num_random=@data;}
else{$my_num_random=$num_random;}
my $tmp; my %uniq;
for(0..$my_num_random-1){
$tmp=int(rand(@data));
redo if $uniq{$tmp};
$new_data[$_]=$data[$tmp];
$uniq{$tmp} = 1;
}
return @new_data;
}
sub search{
my $word=shift;
my $field=shift;
my $action=shift;
$word=~tr/+/ /;
$word=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
#$word=~s/\*/\\*/g;
if ($DateFields{$field}){$word=modify_date($word);}
my $position=-1;
my $m=0;
if($field){
$field=~tr/+/ /;
$field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
foreach(@fields){$position=$m if $_ eq $field; $m++; }
}
my %match;
$word =~s/ +/ /g;
my @new_data=();
my @keys; my $key; my $record;
if($word=~m/\A"(.*)"\Z/){$keys[0]=$1;}
elsif(!$action and ($method eq "perfect" or $method eq "exact")){$keys[0]=$word;}
else{@keys= split(" ", $word);}
if($action eq '!='){for(0..@data-1){$match{$_} = 1;}}
foreach $key (@keys){
$m=0;
foreach $record (@data){
chomp $record;
$record =~ s/"/"/g;
$record =~ s/'/'/g;
my @line = split($my_separator, $record);
if($field && $position>-1){
if($DateFields{$field} ){
$line[$position]=modify_date($line[$position]);
unless ($line[$position]){$m++; next};
}
if($action eq '!='){
$match{$m} = 0 if $line[$position]=~m/$key/i;
}
elsif($action eq '>='){$match{$m} = 1 if $line[$position]>=$key;}
elsif($action eq '<='){$match{$m} = 1 if $line[$position]<=$key;}
elsif($action eq '>'){$match{$m} = 1 if $line[$position]>$key;}
elsif($action eq '<'){$match{$m} = 1 if $line[$position]<$key;}
elsif($action eq '!!'){$match{$m} = 1 if $line[$position]!=$key;}
elsif($method eq "perfect"){$match{$m} = 1 if $line[$position] eq $key;}
else{$match{$m}++ if $line[$position]=~m/\Q$key/i;}
}
else{
foreach(@line){
if($method eq "perfect"){if($_ eq $key){$match{$m}++; last;}}
elsif ($_=~m/\Q$key/i){$match{$m}++; last;}
}
}
$m++;
}
}
$m=0; my $k=0;
foreach(@data){
$match{$m} = 0 if $match{$m}<@keys && $method eq "all";
$new_data[$k++] = $_ if $match{$m};
$m++;
}
$default_show_mode=0;
return @new_data;
}
sub modify_date{
my $date = shift;
my $switch = shift;
my $time;
##date format 10:16:47 2003-10-19
if ($date=~m/(\d+):(\d+):(\d+) (\d\d\d\d)-(\d+)-(\d+)/){
my $h=0; my $m=0; my $s=0;
my $day; my $mon;
if($switch){$h=$1; $m=$2; $s=$3; return 0 if $h>23 or $m>59 or $s>59;}
return 0 if $6>31 or $5>12;
$mon=$5;$day=$6;
$mon="0".$mon if length($mon)==1;
$day="0".$day if length($day)==1;
$m="0".$m if length($m)==1;
$s="0".$s if length($s)==1;
$h="0".$h if length($h)==1;
$time=$4.$mon.$day.$h.$m.$s;
return $time;
}
elsif($date=~m/(\d\d\d\d)[-\/\.](\d+)[-\/\.](\d+)/){
my $day; my $mon;
return 0 if $2==0 or $3>31;
$mon=$2;$day=$3;
$mon="0".$mon if length($mon)==1;
$day="0".$day if length($day)==1;
$time=$1.$mon.$day."000000";
return $time; }
elsif($date =~m/TODAY/i){
my $Tday = $mday; my $Tmonth=$mon+1;
$Tday = "0".$Tday if length($Tday)<2;
$Tmonth = "0".$Tmonth if length($Tmonth)<2;
my $Ttime = "000000";
$Ttime="235959" if $switch;
return 1900+$year.$Tmonth.$Tday.$Ttime;
}
elsif($date =~m/NOW/i){
my $Tday = $mday;
my $Tmonth=$mon+1;
my $Thour=$hour;
my $Tmins=$mins;
my $Tsec=$secs;
$Tday = "0".$Tday if length($Tday)<2;
$Tmonth = "0".$Tmonth if length($Tmonth)<2;
$Thour = "0".$Thour if length($Thour)<2;
$Tmins = "0".$Tmins if length($Tmins)<2;
$Tsec = "0".$Tsec if length($Tsec)<2;
return 1900+$year.$Tmonth.$Tday.$Thour.$Tmins.$Tsec;
}
else{ return 0;}
}
sub get_record{
my $text = $_[0];
$text =~ s{<<(.*?)>>}{exists($INSERT{$1}) ? $INSERT{$1} : ""}gsex;
return $text;
}
sub get_html{
my @txt = read_file($_[0]);
my $txt;
foreach(@txt){$txt.=$_;}
$txt=~/(.*)(.*)<\/template>(.*)/s;
error("Template-tag not found!") if !$1 or !$2;
return ($1,$2,$3);
}
sub read_file{
open(F, $_[0]) || error("Can't open file $_[0]!");
my @data = ;
close F;
return @data;
}
sub error{
print "Error
$_[0]
";
exit;
}
##########################################################
# EZscripting.co.uk © 1999 - 2004 Copyright Darren Deans
# The scripts are available for private and commercial use.
# You can use the scripts in any website you build.
# It is prohibited to sell the scripts in any format to anybody.
# The scripts may only be distributed by EZscripting.co.uk.
# The redistribution of modified versions of the scripts is prohibited.
# EZscripting.co.uk accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts.
# By downloading and using this script you agree to the terms and conditions.
##########################################################