#!/bin/perl ########################################################## # CSVread & CSVsearch v 29/Jan/2005 # ?1999-2005 EZscripting.com # Script by Alexandre Golovkine ########################################################## # Template brackets are now [[fieldname]] my $CSV_file = "database.txt"; my $HTML_template = "temp1.html"; my $no_matches_found = "Sorry, no results found"; my $ID_use = 0; my $ID_field_name = "ID"; ########################################################## # 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 ########################################################## #%FORM = parse_cgi(); print "Content-type: text/html\n\n"; (my $head, my $tmp, my $foot) = get_html($HTML_template); $qs=$ENV{'QUERY_STRING'}; ##read db my @data = read_file($CSV_file); chomp $data[0]; my @fields= split('\|', shift @data); $base_length = @data; error("You have bad file!") if !@fields; error("Database is clear!") if $base_length<1; if($qs =~m/header=([^\&\Z]*)/){push @header,$1;} if($ID_use && $qs =~m/show=([^\&\Z]*)/){ @data = search($1,$ID_field_name); } else{ @conditions=split(/&/,$qs); my $a=0; foreach (@conditions){ ($name, $value) = split(/=/, $_); if($name eq 'search'){ $FORM{search} = $value; @data = search($value, $header[0]); } elsif($name eq 'header'){} elsif($_=~/([^=<>!]+)!=([^=<>!]+)/){@data = search($value, $1, "!=");} elsif($_=~/([^=<>!]+)=([^=<>!]+)/){@data = search($value, $1);} $a++; } } my $result; ##matched data foreach(@data){ chomp; @line = split('\|', $_); $a=0; %INSERT=(); foreach(@fields){$INSERT{$_} = $line[$a++];} $result.=get_record($tmp) } %INSERT=(); $INSERT{'#_matches'} = @data; $INSERT{'#_total'} = $base_length; $result = $no_matches_found."
" unless @data; print get_record($head), $result, get_record($foot); undef $result; undef $head; undef $foot; exit; ######################################################### 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; my $position=-1; my $a=0; if($field){ $field=~tr/+/ /; $field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg; foreach(@fields){$position=$a if $_ eq $field; $a++;} } my %match; $word =~s/ +/ /g; my @new_data=(); my @keys= split(" ", $word); if($action eq '!='){for(0..@data-1){$match{$_} = 1;}} foreach $key (@keys){ $a=0; foreach $record (@data){ @line = split('\|', $record); if($field && $position>-1){ if($action eq '!='){ $match{$a} = 0 if $line[$position]=~m/\Q$key/i; } else{$match{$a} = 1 if $line[$position]=~m/\Q$key/i;} } else{ foreach(@line){if ($_=~m/\Q$key/i){$match{$a} = 1; last;}} } $a++; } } $a=0; my $b=0; foreach(@data){ $new_data[$b++] = $_ if $match{$a}; $a++; } return @new_data; } 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=~/(.*)