0byt3m1n1
Path:
/
data
/
0
/
0
/
23
/
63
/
23715
/
user
/
24201
/
cgi-bin
/
[
Home
]
File: readcsv.pl
#!/usr/local/bin/perl print "Content-type: text/html"."\n\n"; ###################################### # # # ReadCSV v0.1 # # Copyright 2000 by Mutasem Abudahab # # mutasem@abudahab.com # # http://ezscripting.com # # # # Last modified on May 23, 2000 # # # ###################################### # # Copyright Notice: # Copyright 2000 Mutasem R. Abudahab. All Rights Reserved. # # This code may be used and modified by anyone so long as this header and # copyright information remains intact. By using this code you agree to # indemnify Mutasem R. Abudahab from any liability that might arise from its # use. You must obtain written consent before selling or redistributing # this code. #------------------- Start working on the script --------------------# # The following variables should be set to define the locations # and URLs of various files. # Change the value of $CSV_file to the PATH to your CSV database $CSV_file="data.csv"; # Change the value of $HTML_template to the path to the HTML file # where the script should expect to find your requested HTML template. $HTML_template="../offers/csv.html"; # Change the value of $no_matches_found to the string the script should return # if it does not find any matches. $no_matches_found="Sorry, ReadCSV could not find any matches to your query."; #---------- You should not change anything below this line ----------# @in_lines=&modify_CSV($CSV_file); $cond=$ENV{'QUERY_STRING'}; @conditions=split(/&/,$cond); foreach $rule (@conditions){ $rule=~tr/+/ /; $rule=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex ($1))/eg; } if(open(HTML,$HTML_template)){ } else{ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> Can't open HTML template file.\n"; print "<BR>Please, check that you have provided the cgi script with correct HTML template"; print " path in \$HTML_template variable.\n"; print "</BODY></HTML>"; exit(0); } @HTML_lines=<HTML>; @template_lines; $last_read; foreach $lin (@HTML_lines){ if($lin ne "\n"){ chomp($lin); } } close(HTML); $html_length=@HTML_lines; $html_length=$html_length-1; $w=0; READ_HTML: for($o=0;$o<=$html_length;$o=$o+1){ if($HTML_lines[$o]!~/<template>/i){ print $HTML_lines[$o]; } if($HTML_lines[$o]=~/<template>/i){ ($dummy,$remain)=split(/<template>/i,$HTML_lines[$o]); print $dummy; $template_lines[$w]="<template>".$remain; $o=$o+1; $w=1; while($HTML_lines[$o]!~/<\/template>/i){ if($HTML_lines[$o] eq ""){ last READ_HTML; } $template_lines[$w]=$HTML_lines[$o]; $o++; $w=$w+1; } ($dummy,$remain)=split(/<\/template>/i,$HTML_lines[$o]); $template_lines[$w]=$remain."</template>\n"; last READ_HTML; } } $ll=@template_lines; if($ll==0){ $sf=999; } $last_read=$o; $c_match=0; $length=@in_lines; $length=$length-1; @header_entries=split(/,/,$in_lines[0]); $h_length=@header_entries; chomp(@header_entries); $h_length=$h_length-1; $c_length=@conditions; $c_length=$c_length-1; for($l=1;$l<=$length;$l=$l+1){ @line_data=split(/,/,$in_lines[$l]); chomp(@line_data); CHECK_CONDITIONS: for($x=0;$x<=$c_length;$x=$x+1){ if($conditions[$x]=~/[^=<>!]==[^=<>!]/){ ($var,$val)=split(/==/,$conditions[$x]); for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z]=~/^$val$/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($conditions[$x]=~/[^=<>!]=[^=<>!]/){ ($var,$val)=split(/=/,$conditions[$x]); if($var eq "search"){ foreach $vv (@line_data){ if($vv=~/$val/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z]=~/$val/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($conditions[$x]=~/[^=<>!]<<[^=<>!]/){ ($var,$val)=split(/<</,$conditions[$x]); if($val=~/\D/){ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> This Argument can only be used with Numbers .\n"; print "</BODY></HTML>"; exit(0); } for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z] < $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($conditions[$x]=~/[^=<>!]>>[^=<>!]/){ ($var,$val)=split(/>>/,$conditions[$x]); if($val=~/\D/){ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> This Argument can only be used with Numbers .\n"; print "</BODY></HTML>"; exit(0); } for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z] > $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($conditions[$x]=~/[^=<>!]<=[^=<>!]/){ ($var,$val)=split(/<=/,$conditions[$x]); if($val=~/\D/){ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> This Argument can only be used with Numbers .\n"; print "</BODY></HTML>"; exit(0); } for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z] <= $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($conditions[$x]=~/[^=<>!]>=[^=<>!]/){ ($var,$val)=split(/>=/,$conditions[$x]); if($val=~/\D/){ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> This Argument can only be used with Numbers .\n"; print "</BODY></HTML>"; exit(0); } for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z] >= $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($conditions[$x]=~/[^=<>!]!=[^=<>!]/){ ($var,$val)=split(/!=/,$conditions[$x]); for($z=0;$z<=$h_length;$z=$z+1){ if($header_entries[$z] eq $var){ if($line_data[$z]!~/^$val$/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } else{ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> Unreadable request found in your query.\n"; print "<BR>Please, check the syntax of your query."; print "</BODY></HTML>"; exit(0); } ;;;; } if($c_match ==($c_length+1)){ $ll=@template_lines; $ll=$ll-1; for($v=0;$v<=$ll;$v=$v+1){ $out=$template_lines[$v]; while($out=~/<<([^<^>]+)>>/){ $request=$1; for($r=0;$r<=$h_length;$r=$r+1){ if($header_entries[$r] eq $request){ $line_data[$r]=~s/%01/"/g; $line_data[$r]=~s/%02/,/g; $out=~s/<<[^<^>]+>>/$line_data[$r]/; } } } $sf=999; print "$out"; } } $c_match=0; } if($sf != 999){ print $no_matches_found."<BR>\n"; } print $dummy; for($v=($last_read+1);$v<=$html_length;$v=$v+1){ print $HTML_lines[$v]; } sub modify_CSV { if(open(CSV,$_[0])){ } else{ print "<HTML><HEAD><TITLE>Error message</TITLE><BODY>\n"; print "<B>Error:</B> Can't open CSV file.\n"; print "<BR>Please, check that you have provided the cgi script with correct CSV file"; print " path in \$CSV_file variable.\n"; print "</BODY></HTML>"; exit(0); } @lines=<CSV>; $lines_length=@lines; $lines_length=$lines_length-1; for($l=0;$l<=$lines_length;$l=$l+1){ $lines[$l]=~s/""/%01/g; while($lines[$l]=~/("[^"]+")/){ $match=$1; $match=~s/"//g; $match=~s/,/%02/g; $lines[$l]=~s/("[^"]+")/$match/; } } close(CSV); return @lines; }