home *** CD-ROM | disk | FTP | other *** search
/ Creative Review 33 / Creative-Review-CD-ROM-33.iso / mac / MANPER / CGI / SEARCH.CGI < prev    next >
Text File  |  1997-08-21  |  5KB  |  324 lines

  1. #!/usr/bin/perl
  2.  
  3. $directory = $0;
  4. $directory =~ s#/[^/]*$##;
  5. $program = $0;
  6. $program =~ s#^.*/##;
  7. $directory="." if ($directory eq $program);
  8. unshift(@INC, $directory);
  9.  
  10. print "Content-type: text/html\n\n";
  11.  
  12. $formdata=<STDIN>;
  13. $formdata=~s/\s+$//;
  14.  
  15.  
  16. LOOP: foreach(split(/&/,$formdata))
  17. {
  18. ($name, $value)=split(/=/, $_);
  19. $name=~s/\+/ /g;
  20. $name=~s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
  21. $value=~s/\+/ /g;
  22. $value=~s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
  23.  
  24. if ($name=~/^\d+$/)
  25.     {
  26.     if ($value eq "")
  27.         {
  28.         next LOOP;
  29.         }
  30.     
  31.     $value=~s/^\s+//;
  32.     $value=~s/\s+$//;
  33.     $hidden.="<input type=hidden name=\"$name\" value=\"$value\">\n";
  34.     if ($data{$name} eq "")
  35.         {
  36.         $data{$name}=$value;
  37.         }
  38.     else
  39.         {
  40.          if ($options{$name}<4)
  41.             {
  42.             $data{$name}.=", $value";
  43.             }
  44.         elsif ($options{$name}==7)
  45.             {
  46.             $data{$name}.="-$value";
  47.             }
  48.         }
  49.         
  50.     }
  51. elsif ($name=~/^\d+_option$/)
  52.     {
  53.     ($opt, $dm)=split(/_/, $name);
  54.     $options{$opt}=$value;
  55.     $hidden.="<input type=hidden name=\"$name\" value=\"$value\">\n";
  56.     }
  57. else
  58.     {
  59.     ${$name}=$value;
  60.     if ($name ne "start")
  61.         {
  62.         $hidden.="<input type=hidden name=\"$name\" value=\"$value\">\n";
  63.         }
  64.     }
  65. }
  66.  
  67. $output_number=$output_number || 10;
  68. $start=$start || 0;
  69.  
  70.  
  71. require "setup.pl";
  72.  
  73. foreach (keys(%data))
  74.     {
  75.     $option=$options{$_};
  76.     undef ($local_command);
  77.     if ($option<=4)
  78.         {
  79.         @input=split(/\s*,\s*/, $data{$_});
  80.         }
  81.     else
  82.         {
  83.         @input=($data{$_});
  84.         }
  85.         
  86.     
  87.     foreach $input (@input)
  88.         {
  89.         &local_command;
  90.         }
  91.         
  92.     $command.=" && ($local_command)";
  93.     }
  94. $command=~s/^ && //;
  95.  
  96. if ($command eq "")
  97.     {
  98.     $command=1;
  99.     }
  100.  
  101. # matching
  102.  
  103.  
  104. $count=0;
  105. $file="$root/$file";
  106. unless (open (FILE, $file))
  107.     {
  108.     print "Cannot open $file";
  109.     exit;
  110.     }
  111. $/=$r_sep;
  112. @data=<FILE>;
  113. close FILE;
  114.  
  115. LOOP1: for ($a=$start; $a<@data; $a++)
  116.     {
  117.     $data[$a]=~s/$r_sep$//;
  118.     if ($quotes eq "on")
  119.         {
  120.         # put quotes around empty fields
  121.         $data[$a]=~s/,,/,"",/g;
  122.         $data[$a]=~s/,,/,"",/g;
  123.         $data[$a]=~s/^,/"",/;
  124.         $data[$a]=~s/,$/,""/;
  125.         eval ("\@fields=($data[$a]);");
  126.         }
  127.     else
  128.         {
  129.         @fields=split(/$f_sep/, $data[$a]);
  130.         }
  131.     if (eval($command))
  132.         {
  133.         $count++;
  134.         if ($count==1)
  135.             {
  136.             &print_template;
  137.             }
  138.         if ($count<=$output_number)
  139.             {
  140.             $line=$record;
  141.     
  142.             if ($f_price ne "")
  143.                 {
  144.                 $fields[$f_price]=&display_price($fields[$f_price]);
  145.                 }
  146.             $line=~s/!(\d+)!/$fields[$1]/g;
  147.             $line=~s/<a href="">([^<]+)<\/a>/$1/gi;
  148.             $line=~s/<img src=""[^>]+>//ig;
  149.             print $line;
  150.             }
  151.         elsif ($count==$output_number+1)
  152.             {
  153.             &print_bottom;
  154.             last LOOP1;
  155.             }
  156.         
  157.         }
  158.     
  159.     }
  160.  
  161.  
  162. if ($count==0)
  163.     {
  164.     unless (open (NO, "$root/nomatch.htm"))
  165.         {
  166.         print "Cannot open file $root/nomatch.htm";
  167.         exit;
  168.         } 
  169.     print (<NO>);
  170.     }
  171.  
  172. elsif ($count<=$output_number)
  173.     {
  174.     &print_bottom;
  175.     }
  176.  
  177. if ($log==1 && $start==0)
  178.     {
  179.     @numbers=sort numbers (keys(%data));
  180.     for ($i=0; $i<=$numbers[@numbers-1]; $i++)
  181.         {
  182.         $entry.="\"$data{$i}\",";
  183.         }
  184.     $entry=~s/,$/\n/;
  185.     open (LOG, ">>$root/log.txt");
  186.     chmod (0666, "$root/log.txt");
  187.     print LOG $entry;
  188.     close LOG;
  189.     }
  190.  
  191.  
  192. sub print_template
  193. {
  194. $template="$root/listtemp.htm";
  195.  
  196. unless (open (TEMPL, $template))
  197.     {
  198.     print "Cannot open $template";
  199.     exit;
  200.     }
  201. undef ($/);
  202. $templ=<TEMPL>;
  203. close TEMPL;
  204. ($top, $record, $bottom)=split(/<!--record-->/, $templ);
  205. print $top;
  206. }
  207.  
  208. sub print_bottom
  209. {
  210. ($part1, $part2)=split(/!np!/, $bottom);
  211. print "<FORM name=\"next\" method=post action=\"$cgi/search.cgi\">\n";
  212. print $hidden;
  213. print "<input type=hidden name=\"start\" value=\"$a\">\n";
  214. print "</form>\n";
  215. print $part1;
  216. if ($start != 0)
  217.     {
  218.     print "<a href=\"javascript:self.history.go(-1)\">$previous</a>\n";
  219.     }
  220. if ($count==$output_number+1)
  221.     {
  222.     print "<a href=\"javascript:document.next.submit()\">$next</a>\n";
  223.     }
  224. print $part2;
  225. }  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. sub numbers
  232. {
  233. if ($a>$b) {
  234.    $retval=1;
  235.    }
  236. elsif ($a==$b) {
  237.   $retval=0;
  238.  }
  239. else {
  240.   $retval=-1;
  241.  }
  242.  $retval;
  243. }    
  244.      
  245.  
  246.  
  247. sub local_command
  248. {
  249. if ($option>3)
  250.     {
  251.     $input=~s/[^\d\.-]+//g;
  252.     }
  253. if ($option==1 || $option==4)
  254.     {
  255.     $local_command.=" || (\$fields[$_] eq \"$input\")";
  256.     }
  257. elsif ($option==2)
  258.     {
  259.     $local_command.=" || (\$fields[$_]=~/^$input\$/i)";
  260.     }
  261. elsif ($option==3)
  262.     {
  263.     $local_command.=" || (\$fields[$_]=~/\\b$input\\b/i)";
  264.     }
  265. elsif ($option==5)
  266.     {
  267.     $local_command.=" || (\$fields[$_]>=$input)";
  268.     }
  269. elsif ($option==6)
  270.     {
  271.     $local_command.=" || (\$fields[$_]<=$input)";
  272.     }
  273. elsif ($option==7)
  274.     {
  275.     ($min, $max)=split(/\s*-\s*/, $input);
  276.     $min=~s/[^\d\.]+//g;
  277.     $max=~s/[^\d\.]+//g;
  278.     $local_command.=" || (\$fields[$_]>=$min) && (\$fields[$_]<=$max)";
  279.     }
  280. $local_command=~s/^ \|\| //;
  281.  
  282. }
  283.  
  284. sub display_price
  285. {
  286. local($price)=@_;
  287. ($dollars, $cents)=split(/\./, $price);
  288. if ($cents=~/^(\d{2})(\d)/)
  289.     {
  290.     $cents=$1;
  291.     if ($2>5)
  292.         {
  293.         $cents++;
  294.         }
  295.     if ($cents==100)
  296.         {
  297.         $dollars++;
  298.         $cents="00";
  299.         }
  300.     }
  301. if ($cents=~/^\d$/)
  302.     {
  303.     $cents=$&."0";
  304.     }
  305. elsif ($cents eq "")
  306.     {
  307.     $cents="00";
  308.     }
  309.  
  310. $price=$dollars.".$cents";
  311. return $price;
  312. }
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.     
  320.  
  321.  
  322.  
  323.                 
  324.