#!/usr/bin/perl # # ldpsrch.cgi # requires perl5 and CGI module # # Use the CGI modules for HTML processing. # use CGI; use Carp; # Set the path for security and taint checking. # $ENV{'PATH'} = '/usr/bin:/bin'; # Some globals, used within... # local($_LDP_URL) = 'http://tldp.org/'; # Create a new qry object for HTML form parsing. Get variables. # local($qry) = new CGI; local($srch) = $qry->param('words') || ''; local($_URL) = $qry->param('url') || ''; local($_SVR) = $qry->param('svr') || ''; if( $_SVR eq '' ) { $_SVR = $qry->referer(); } if( $_SVR eq '' || $_SVR =~ /junkb/i || $_SVR =~ /[\ \+\&\@\#\$\%\=\!\"\(\)\|\;\,]+/ ) { $_SVR = $_LDP_URL; } elsif( index($_SVR, "\.") == -1 ) { $_SVR = $_LDP_URL; } else { $_SVR =~ s/\/[\w]+\.html$//; } $_SVR =~ s/\/$//; $_SVR =~ s/\/cgi-bin\/ldpsrch\.cgi//g; $qry->param('svr', $_SVR); print $qry->header(); open(FHANDLE, "./templates/searchform.tmp"); my($s) = join('', ); close(FHANDLE); # fill-in search parms # if( $srch eq '' ) { $srch = $qry->param('q'); } $s =~ s/%%KYWDS/$srch/g; $s =~ s/%%SVR/$_SVR/g; print $s; &search(); print "\n


\n\n\n\n"; exit(0); # # perform a search, display the result # sub search{ my($srch_str) = $qry->param('words') || ''; if( $srch_str eq '' ) { $srch_str = $qry->param('q'); } # err: no search info provided # if( $srch_str eq '' ) { print "

No Search Information Submitted

\n", "

No query/keywords have been entered.

"; return; } use LWP::UserAgent; use URI::URL; use HTTP::Request; use HTTP::Response; use HTTP::Status; $ua = LWP::UserAgent->new(); $ua -> agent('ldpsrch 1.0'); my($s) = "&hl=en&filter=0&lr=&ie=UTF-8&cof=L:http://tldp.org/images/ldp.gif%3BLH:120%3BLW:300%3BBGC:%23AAAAAA%3BT:%23000000%3BLC:%23003355%3BVLC:%23003355%3BALC:%23003355%3BAH:center%3BS:http://www.tldp.org%3B&domains=www.tldp.org&sitesearch=www.tldp.org&q=" . &escape($srch_str); my($st_pt) = $qry->param('start') || ''; if( $st_pt ne '' ) { $s .= "&start=${st_pt}&sa=N&svr=${_SVR}"; } my($req) = new HTTP::Request GET => "http://www.google.com/custom?${s}"; my($res) = $ua->request($req); if (!($res->is_success)) { "

Error Performing Search...

\n"; return; } my($buf) = $res->content; ## print "\n"; $buf =~ s/^.*>Results//si; $tmp = substr($buf, 0, index($buf, '(')); print "

${tmp}

\n\n"; $buf =~ s/^.*
//si; $buf =~ s/div>
.*$//si; $buf =~ s/href=\"http:\/\/www\.tldp\.org/href=\"${_SVR}/g; $buf =~ s/href=\"\/custom\?/href=\"http:\/\/tldp\.org\/cgi-bin\/ldpsrch\.cgi\?svr=${_SVR}\&/g; print $buf; } sub escape { my($toencode) = @_; $toencode =~ s/\ /%20/g; $toencode =~ s/\+/%2B/g; $toencode =~ s/\"/%22/g; $toencode =~ s/\&/%26/g; $toencode =~ s/\'/%27/g; return $toencode; }