Choose the Main Category for the link you wish to add
#!/usr/bin/perl print "Content-type: text/html"."\n\n"; ###################################### # # # ReadCSVPlus v1.0 # # Copyright 2000 by Mutasem Abudahab # # mutasem@abudahab.com # # http://ezscripting.com # # # # Last modified on July 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 --------------------# # Replace "data.csv" with the full path to your csv database file $csv_file_path="../data/category.csv"; # Replace "template.html" with the full path to your HTML template. $html_template_path="../includes/drop_category.html"; # Replace "http://www.domain.com/images/new.gif" with the URL to the graphic # image that will be displayed next to a record, when ReadCSVPlus marks it as # new $new_image_file="../images/new.gif"; # Replace this value with the number of days related to <<#local_date>> after # which the script will stop displaying the 'new' image graphic. $new_compare_dat="1"; # Replace this value with the number of random records the script will display # when you query for a random display (display=random). $num_random=1; # Replace this value with the message the script will display, when it finds # no matches to a query $no_matches_found="sorry, no category found"; #-------------- DO NOT CHANGE ANYTHING UNDER THIS LINE --------------# $nv=0; $qs=$ENV{'QUERY_STRING'}; if($sort_field=&pick_up("sort_a",$qs)){ $sort_a="yes"; } if($s_field=&pick_up("sort_d",$qs)){ $sort_d="yes"; } if($sort_d eq "yes"){ $sort_field=$s_field; } @csv_lines=&modify_CSV($csv_file_path); $lcl=@csv_lines; @headers=split(",",$csv_lines[0]); foreach $one (@headers){ $one=&search_prepare($one); } $l_headers=@headers; for($l=0;$l<=($l_headers-1);$l=$l+1){ if($headers[$l]=~/^$sort_field$/){ $sort_location=$l+1; } if(($headers[$l]=~/^#date_local$/)||($headers[$l]=~/^#date_local$/)){ $date_location=$l+1; } if($headers[$l]=~/^\#_approved$/){ $valid_location=$l+1; } } if((($sort_a eq "yes")||($sort_d eq "yes"))&&($sort_location < 1)){ &produce_error( "Cannot locate the requested sort field in CSV file header." ); } if($rec_range=&pick_up("rec_range",$qs)){ if($rec_range=~/^\((\d+)\)\((\d+)-(\d+)\)(.+)/){ $page=$1; $rec_start=$2; $rec_end=$3; $recs=$4; @matched_lines=split(" ",$recs); foreach $match (@matched_lines){ if($match=~/\D/){ &produce_error( "unpropper form found in rec_range in URL." ); } } goto PARTIAL; } else{ &produce_error( "unpropper form found in rec_range in URL." ); } } if($display=&pick_up("display",$qs)){ if($display eq "random"){ $display="random"; goto PREPARE; } elsif($display eq "all"){ $display="all"; goto PREPARE; } else{ &produce_error( "undefined display mode requested." ); } } @conditions=split("&",$qs); $l_conditions=@conditions; $absolute=$l_conditions; foreach $one (@conditions){ $one=~s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/eg; $one=~tr/+/ /; } for($l=1;$l<=(@csv_lines-1);$l=$l+1){ @line_entries=split(",",$csv_lines[$l]); foreach $entry (@line_entries){ $entry=&search_prepare($entry); $entry=&sweep_spaces($entry); } CHECK_CONDITIONS: foreach $rule (@conditions){ if($rule=~/[^=<>!]<[^=<>!]/){ ($var,$val)=split("<",$rule); &error_if_nonnumiric; &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] < $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($rule=~/[^=<>!]>[^=<>!]/){ ($var,$val)=split(">",$rule); &error_if_nonnumiric; &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] > $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($rule=~/[^=<>!]>[^=<>!]/){ ($var,$val)=split(">",$rule); &error_if_nonnumiric; &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] > $val){ print $line_entries[$p].""; $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif(($rule=~/[^=<>!](>=)[^=<>!]/)||($rule=~/[^=<>!](=>)[^=<>!]/)){ ($var,$val)=split($+,$rule); &error_if_nonnumiric; &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] >= $val){ print $line_entries[$p].""; $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif(($rule=~/[^=<>!](<=)[^=<>!]/)||($rule=~/[^=<>!](=<)[^=<>!]/)){ ($var,$val)=split($+,$rule); &error_if_nonnumiric; &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] <= $val){ print $line_entries[$p].""; $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif($rule=~/[^=<>!]!![^=<>!]/){ ($var,$val)=split("!!",$rule); &error_if_nonnumiric; &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] != $val){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } elsif(($rule=~/[^=<>!](!=)[^=<>!]/)||($rule=~/[^=<>!](=!)[^=<>!]/)){ ($var,$val)=split($+,$rule); &error_if_meta; $var=&sweep_spaces($var); $val=&sweep_spaces($val); if($val=~/^".+"$/){ $val=~s/^"//; $val=~s/"$//; for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] !~ /^$val$/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } #end of the new if exact else{ for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] !~ /$val/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } } elsif(($rule=~/[^=<>!]=[^=<>!]/)||($rule=~/[^=<>!]=$/)){ ($var,$val)=split("=",$rule); &error_if_meta; if($val !~/\S/){ $c_match=$c_match+1; next CHECK_CONDITIONS; } $var=&sweep_spaces($var); $val=&sweep_spaces($val); if($val=~/^".+"$/){ $val=~s/^"//; $val=~s/"$//; for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] =~ /^$val$/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } else{ for($p=0;$p<=($l_headers-1);$p=$p+1){ if($var=~/^$headers[$p]$/){ if($line_entries[$p] =~ /$val/i){ $c_match=$c_match+1; next CHECK_CONDITIONS; } } } } } else{ &produce_error( "Unreadable request found in your query.", "Please, check the syntax of your query." ); } } if($c_match == $absolute){ $matched_lines[$num]=$l; if($sort_location != 0){ $sort_values[$num]=$line_entries[$sort_location-1]; } if($date_location != 0){ $date_values[$num]=$line_entries[$date_location-1]; $date_values[$num]=&sweep_spaces($date_values[$num]); if($date_values[$num]!~/\d{1,2}[,-\/\\]\d{1,2}[,-\/\\]\d{4}/){ $date_values[$num]="not dated"; } } $num=$num+1; } $c_match=0; } PREPARE: $lcsv=@csv_lines-1; if($display eq "all"){ @matched_lines=(1..$lcsv); } if($display eq "random"){ srand; if($num_random < 1){ &produce_error( "cannot display less than 1 random records." ); } if($num_random > $lcsv){ $num_random=$lcsv; } @matched_lines=(); $num=0; $l=1; REDO: for($l;$l<=$num_random;$l=$l+1){ $record_n=int(rand($lcsv))+1; $matched_lines[$num]=$record_n; for($u=0;$u<=($num-1);$u=$u+1){ if($matched_lines[$num] == $matched_lines[$u]){ goto REDO; } } $num=$num+1; } } $lml=@matched_lines; if(($sort_a eq "yes")||($sort_d eq "yes")){ &sort_results; } SHOW: if(open(HTML,$html_template_path)){ @html_lines=; close(HTML); $html=join("",@html_lines); unless($html=~/(.*)(.*)<\/template>(.*)/is){ &produce_error( "Cannot find template tags in HTML template." ); } $upper=$1; $html_temp=$2; $footer=$3; foreach $part ($upper,$html_temp,$footer){ $part=&translate_special_variables($part,($lml-$nv),($lcl-1)); } if(($lml-$nv) == 0){ print $upper; print $no_matches_found."\n"; print $footer; exit(0); } print $upper."\n"; for($w=0;$w<=(@matched_lines-1);$w=$w+1){ @m_e=split(",",$csv_lines[$matched_lines[$w]]); $out=$html_temp; foreach $m_entry (@m_e){ $m_entry=&search_prepare($m_entry); } for($lh=0;$lh<=($l_headers-1);$lh=$lh+1){ $out=~s/<<$headers[$lh]>>/$m_e[$lh]/isg; } if($out=~/<>/i){ $new_img=&check_if_new($new_compare_dat,$new_image_file,$date_values[$w]); $out=~s/<>/$new_img/isg; } $out=~s/<<.*>>//g; print $out; } print $footer."\n"; } else{ &produce_error( "could not open HTML template file. Please check that you have provided", "the script with the correct path in \$html_template_path variable." ); } exit; sub modify_CSV { if(open(CSV,$_[0])){ } else{ &produce_error( "Can't open CSV file.", "Please, check that you have provided the cgi script with correct CSV file path.", ); } $ccc=0; while($in_lin=){ if($in_lin!~/\S/){ next; } $lines[$ccc]=$in_lin; $ccc=$ccc+1; } $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); chomp(@lines); return @lines; } sub search_prepare{ $_[0]=~s/%01/"/g; $_[0]=~s/%02/,/g; return $_[0]; } sub produce_error { print "Error message\n"; print ""; print ""; print "ERROR MESSAGE:"; print ""; foreach $line (@_){ print "$line\n"; } print "For more information go to WWW.EZSCRIPTING.COM"; print ""; print "Report Bug - FAQ - Discussion Forums"; print "ReadCSVPlus by Mutasem Abudahab, EZScripting.com"; print ""; exit(0); return (1); } sub error_if_nonnumiric { if($val =~ /\D/){ &produce_error( "Cannot use non-numiric values as right arguments of:", "1- Greater than.", "2- Less than.", "3- Equal or greater than.", "4- Equal or less than.", "Operators." ); } return (1); } sub sweep_spaces{ $_[0]=~s/^ +//; $_[0]=~s/ +$//; return $_[0]; } sub translate_special_variables{ ($secs,$mins,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime(time); $b_time="$secs".":"."$mins".":"."$hour"." GMT"; $b_date="$mday".",".($mon+1).",".(1900+$year); $_[0]=~s/<<#_matches>>/$_[1]/isg; $_[0]=~s/<<#_total>>/$_[2]/isg; $_[0]=~s/<<#_date>>/$b_date/isg; $_[0]=~s/<<#_time>>/$b_time/isg; return $_[0]; } sub pick_up{ if(($_[1]=~/^$_[0]=([^&\b]+)/i)||($_[1]=~/&$_[0]=([^&\b]+)/i)){ $pick=$+; $pick=~s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/eg; $pick=~tr/+/ /; $_[1]=~s/$_[0]=[^&\b]+//i; $_[1]=~s/&&/&/g; $_[1]=~s/^&//g; $_[1]=~s/&$//g; } else{ return 0; } return $pick; } sub check_if_new{ if($_[0] < 1){ &produce_error( "you need to provide \$new_compare_dat with a value", "in order to place an image presenting new records." ); } if($_[1] eq ""){ &produce_error( "you need to provide \$new_image_file with a path to an image file", "in order to place an image presenting new records." ); } if($_[2] eq "not dated"){ return "<>"; } ($dd,$mm,$yy)=split(",",$_[2]); $days=$dd+($mm*30)+($yy*364); ($secs,$mins,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime(time); $ndays=$mday+(($mon+1)*30)+((1900+$year)*364); $def=$ndays-$days; if($def <= $new_compare_dat){ return ""; } return "<>"; } sub sort_results{ local $l; for($l=0;$l<=(@matched_lines-1);$l=$l+1){ $new_len=length $matched_lines[$l]; if($new_len>=$dig_len){ $dig_len=$new_len; } } for($l=0;$l<=(@matched_lines-1);$l=$l+1){ $new_m_val=$dig_len-(length $matched_lines[$l]); $sort_values[$l]=$sort_values[$l]."|||||".("0"x$new_m_val).$matched_lines[$l]; } @sort_values=sort(@sort_values); foreach $s_value (@sort_values){ ($dummy,$matched_lines[$ghg])=split(/\|\|\|\|\|/,$s_value); $matched_lines[$ghg]=~s/^0//; $ghg=$ghg+1; } if($sort_d eq "yes"){ @matched_lines=reverse(@matched_lines); } } sub error_if_meta{ if(($var=~/\/\$\^<>\|\\\[\]\(\)\{\}&=\?/)||($val=~/\/\?\$\^<>\|\\\[\]\(\)\{\}&=\?/)){ &produce_error( "U have been using forbidden reserved meta characters \?\$\^<>\|\\\[\]\(\)\{\}&=/ " ); } } Kindly keep entries tidy. Valid entries are highly appreciated
Report Bug - FAQ - Discussion Forums
ReadCSVPlus by Mutasem Abudahab, EZScripting.com
Categories displayed with ReadCSV