#!/usr/bin/perl
##########################################################
# CSVsearch Xtra & CSVread Xtra v 28/Mar/2005
# © 1999-2005 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 = "omlsmagnew.pl";
my $CSV_file = "group1.dat";
my $HTML_template = "templatemagnew.htm";
my $no_matches_found = "
Sorry, No results found.
Use the back button on your browser to try again
";
my $num_random = 5;
my $records_per_page = 1;
my $field_name_records = 'records';
my $textPrevious = "Previous";
my $textNext = "Next";
my $pageSelectorRange = 10;
my $order_desc = 1;
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 = 0;
my @DateFields = ('DATE');
my @no_search_fields = ();
##########################################################
# Advanced Settings High Lighting
my $highLighting = 0;
my @highLightColors = ("#FFFFC1", "#CEFFCE", "#D7D7FF", "#FFCECE", "#CCCCCC");
my @no_highlight_fields = ('join_id','group_id');
##########################################################
# Advanced Settings auto ID
my $ID_use = 0;
my $ID_field_name = "group_id";
##########################################################
my $field_name_database = 'group1.dat';
my %DB=();
$DB{'db1'} = 'database.txt';
$DB{'db2'} = 'database.txt';
$DB{'db3'} = 'database.txt';
$DB{'db4'} = 'database.txt';
$DB{'db5'} = 'database.txt';
##########################################################
my $field_name_template = 'templatemagnew.htm';
my %TMP =();
$TMP{'tp1'} = 'template.htm';
$TMP{'tp2'} = 'template.htm';
$TMP{'tp3'} = 'template.htm';
$TMP{'tp4'} = 'template.htm';
$TMP{'tp5'} = 'template.htm';
##########################################################
# EZscripting.com © 1999 - 2005
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com 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_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("You have bad file!") if !@fields;
error("Database is clear!") if $base_length<1;
## 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"; # F = Forbidden
}
}
if (!defined($forbiddenpositions{$cnt}))
{
$forbiddenpositions{$cnt} = "P"; # P = Public
}
$cnt++;
}
}
sub checkval {
(my $str) = @_;
my $name; my $value;
($name, $value) = split(/=/, $str);
if (defined($value) && !($value =~ m/^\s*$/))
{ return 1;}
else {
($name, $value) = split(/, $str);
if (defined($value) && !($value =~ m/^\s*$/))
{ return 1; }
else {
($name, $value) = split(/>/, $str);
if (defined($value) && !($value =~ m/^\s*$/))
{ return 1;}
else { return 0;}
}
}
}
## 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"; # No Highlight
}
}
if (!defined($forbiddenhighlights{$cnt}))
{
$forbiddenhighlights{$cnt} = "H"; # H = Highlight
}
$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'){
$searchTerms = $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;
}
if ($value eq 'off')
{
$highLighting = 0;
}
}
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); $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';
###date!
#
###matched data
## Search Term Highlighting.
if ($highLighting)
{
my @terms = split(/\+/, $searchTerms);
my $term;
my $cnt = 0;
foreach $term(@terms)
{
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++;
}
}
## end ST highlighting
## 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;
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 .="&$field_name_database=$database" if $database;
$qs .="&$field_name_template=$templateF" if $templateF;
#set page selector
$pages_out= " $textPrevious " if $FORM{page}>0;
my $pageStart=0;
my $pageEnd = $total/$records_per_page - 1;
if($pageSelectorRange){
$pageEnd = int($pageEnd+0.5);
$pageStart= $FORM{page}-int($pageSelectorRange/2) if $FORM{page}>int($pageSelectorRange/2);
$pageEnd=$pageStart+$pageSelectorRange-1 if $pageStart+$pageSelectorRange-1<$pageEnd;
$pageStart= $pageEnd-$pageSelectorRange+1 if $pageEnd-$pageSelectorRange+1>=0;
}
for($pages=$pageStart; $pages <= $pageEnd; $pages++){
$p=$pages+1;
if($FORM{page}==$pages){$pages_out.= " $p";}
else{$pages_out.= qq| $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 minquerylength {
(my $searchval) = @_;
(my $head, my $tmp, my $foot) = get_html($HTML_template);
$result=get_record($min_query_length_error)."
";
$INSERT{'#_matches'} = 0;
$INSERT{'#_total'} = $base_length;
$INSERT{'#_pages'} = 1;
$INSERT{'#_page_selector'}=1;
$INSERT{'#_search'} = $searchval;
$INSERT{'#_date'}=(1900+$year)."-".($mon+1)."-$mday";
$INSERT{'#_time'}="$hour".":"."$mins".":"."$secs"." GMT";
print get_record($head), $result, get_record($foot);
undef $result; undef $head; undef $foot;
exit;
}
sub iindex {
(my $string, my $searchingfor) = @_;
my $idx = index(uc($string), uc($searchingfor));
if ($idx >= 0)
{
$_[1] = substr($string, $idx, length($searchingfor));
}
return $idx;
}
sub not_in_tag
{
(my $string) = @_;
my @arr = split(//, $string);
my $num_tag_open = grep(/, @arr);
my $num_tag_close = grep(/>/, @arr);
return ($num_tag_open == $num_tag_close);
}
#########################################################
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 checkfields
{
my @fields = @_;
my $field; my $forbiddenfield;
foreach $field(@fields)
{
foreach $forbiddenfield(@no_search_fields)
{
if ($field eq $forbiddenfield)
{
return_no_results();
}
}
}
}
sub return_no_results {
(my $head, my $tmp, my $foot) = get_html($HTML_template);
$result=get_record($no_matches_found)."
";
$INSERT{'#_matches'} = 0;
$INSERT{'#_total'} = 0;
$INSERT{'#_pages'} = 1;
$INSERT{'#_page_selector'}=1;
print get_record($head), $result, get_record($foot);
exit(0);
}
sub search{
my $word=shift;
my $field=shift;
my $action=shift;
my @new_data=();
$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)
{
if ($use_no_search_fields)
{
checkfields($field);
}
$field=~tr/+/ /;
$field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
foreach(@fields){$position=$m if $_ eq $field; $m++; }
if ($method eq 'start')
{
my $entry;
foreach $entry(@data)
{
my @line = split($my_separator, $entry);
if ($line[$position] =~ /^$word/i)
{
push(@new_data, $entry)
}
}
$default_show_mode = 0;
return @new_data;
}
}
if ($method eq 'start') { return_no_results();}
my %match;
$word =~s/ +/ /g;
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{ my $cnt = 0;
foreach(@line)
{
if (($forbiddenpositions{$cnt} eq 'P') || $use_no_search_fields == 0)
{
if($method eq "perfect"){if($_ eq $key){$match{$m}++; last;}}
elsif ($_=~m/\Q$key/i){$match{$m}++; last;}
}
$cnt++;
}
}
$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+)/){
if($switch){return 0 if $1>23 or $2>59 or $3>59;}
return 0 if $6>31 or $5>12;
$time = sprintf("%04d%02d%02d%02d%02d%02d",$4,$5,$6,$1,$2,$3);
return $time;
}
##date format 2003-10-29 2003.10.29 2003/10/29
elsif($date=~m/(\d\d\d\d)[-\/\.](\d+)[-\/\.](\d+)/){
return 0 if $2==0 or $3>31;
$time = sprintf("%04d%02d%02d",$1,$2,$3)."000000";
return $time;
}
elsif($date =~m/TODAY/i){
my ($Tday, $Tmonth, $Ttime)= ($mday, $mon+1, "000000");
$Ttime="235959" if $switch;
$time = sprintf("%04d%02d%02d",1900+$year,$Tmonth,$Tday).$Ttime;
return $time;
}
elsif($date =~m/NOW/i){
my ($Tday, $Tmonth, $Thour,$Tmins,$Tsec)= ($mday, $mon+1, $hour, $mins, $secs);
$time = sprintf("%04d%02d%02d%02d%02d%02d",1900+$year,$Tmonth,$Tday,$Thour,$Tmins,$Tsec);
return $time;
}
else{ return 0;}
}
sub get_record{
my $text = $_[0];
$text =~ s{\[\[\s*(.*?)\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.com © 1999 - 2005
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com 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
##########################################################