#!/usr/bin/perl #use DBI; ######################################################################## # COPYRIGHT NOTICE: # # Copyright 2001 FocalMedia.Net All Rights Reserved. # # Selling the code for this program without prior written consent # from FocalMedia.Net is expressly forbidden. You may not # redistribute this program in any shape or form. # # This program is distributed "as is" and without warranty of any # kind, either express or implied. In no event shall the liability # of FocalMedia.Net for any damages, losses and/or causes of action # exceed the total amount paid by the user for this software. # ######################################################################## use Fcntl qw(:DEFAULT :flock); use CGI; use CGI::Carp qw(fatalsToBrowser); use tseek; $recent_dbd = 1; $pages_prev = "7"; $pages_next = "7"; $minimum_keyword_length = 2; &get_setup; &get_vars; $tseekdir = "$scripts_url/tseekdir.cgi"; $tseek = "$scripts_url/tseek.cgi"; $rateitcgi = "$scripts_url/rate_it.cgi"; $review_it = "$scripts_url/review_it.cgi"; $addurl = "$scripts_url/addurl.cgi"; $tsearch = "$scripts_url/tsearch.cgi"; $nr_searchres = $configitems[5]; ### GET ENVVARS if ($ENV{'QUERY_STRING'} ne "") { $temp = $ENV{'QUERY_STRING'}; } else { read(STDIN, $temp, $ENV{'CONTENT_LENGTH'}); } if ($temp =~ /\|/) { exit; } @pairs=split(/&/,$temp); foreach $item(@pairs) { ($key,$content)=split (/=/,$item,2); $content=~tr/+/ /; $content=~ s/%(..)/pack("c",hex($1))/ge; $fields{$key}=$content; } $fields{'comment'}=~s/\cM//g; $fields{'comment'}=~s/\n\n/

/g; $fields{'comment'}=~s/\n/
/g; #$referer = $ENV{'HTTP_REFERER'}; #if ($fields{'keywords'} eq "") {print "Location: $referer\n\n"; exit;} ### DECODE TEMPLATE $fsize1 = (-s "$data_dir/searchres.html"); open (DRST, "$data_dir/searchres.html"); if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $data_dir/searchres.html $!\n";} read(DRST,$main_template,$fsize1); #if ($file_locking ne "No"){flock (DRST, LOCK_UN);} close (DRST); $fsize1 = (-s "$data_dir/directories.html"); open (DRST, "$data_dir/directories.html"); if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $data_dir/directories.html $!\n";} read(DRST,$dir_template,$fsize1); #if ($file_locking ne "No"){flock (DRST, LOCK_UN);} close (DRST); $fsize1 = (-s "$data_dir/lsearchres_loc.html"); open (DRST, "$data_dir/lsearchres_loc.html"); if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $data_dir/lsearchres_loc.html $!\n";} read(DRST,$links_template,$fsize1); #if ($file_locking ne "No"){flock (DRST, LOCK_UN);} close (DRST); $fsize1 = (-s "$data_dir/ttl.dat"); if ($fsize1 > 0) { $fsize1 = (-s "$data_dir/tbsdata/templates/ttl.dat"); open (DRST, "$data_dir/tbsdata/templates/ttl.dat"); if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $data_dir/tbsdata/templates/ttl.dat $!\n";} read(DRST,$crit_template,$fsize1); #if ($file_locking ne "No"){flock (DRST, LOCK_UN);} close (DRST); } $fsize1 = (-s "$data_dir/dtablea.idx"); if ($fsize1 > 0) { open (DRST, "$data_dir/dtablea.idx"); if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $data_dir/dtablea.idx $!\n";} read(DRST,$keydata2,$fsize1); #if ($file_locking ne "No"){flock (DRST, LOCK_UN);} close (DRST); } $fsize1 = (-s "$data_dir/keys.dat"); if ($fsize1 > 0) { open (DRST, "$data_dir/keys.dat"); if ($file_locking ne "No"){flock (DRST, LOCK_SH) or die "Can't set lock for file: $data_dir/keys.dat $!\n";} read(DRST,$keydata,$fsize1); #if ($file_locking ne "No"){flock (DRST, LOCK_UN);} close (DRST); @key_lines = split(/\n/, $keydata); } $jscript = < END_OF_JS $main_template =~ s/<\/head>/$jscript/gi; $fields{'keywords'} = &get_search_ready($fields{'keywords'}); if ($fields{'keywords'} eq ""){$fields{'keywords'} = "NOTHING";} if (length($fields{'keywords'}) < $minimum_keyword_length){$fields{'keywords'} = "2_CHARS";} if (($use_mysql eq "Yes") and ($fields{'keywords'} ne "NOTHING") and ($fields{'keywords'} ne "2_CHARS")) { &mysql_search; } elsif (($fields{'keywords'} ne "NOTHING") and ($fields{'keywords'} ne "2_CHARS")) { &normal_search; } if (($fields{'keywords'} eq "NOTHING") or ($fields{'keywords'} eq "2_CHARS")) { $fields{'keywords'} = ""; } #### RELEVANCE $rspcntr = 0; (@skeyw) = split(/ /,$fields{'keywords'}); $nrkeywords = push(@skeyw); foreach $kwrel (@resultline) { $keycount = 0; $kwcntr = 1000; while ($keycount < $nrkeywords) { #### TITLE (@litems) = split(/\t/,$kwrel); if ($litems[1] =~ /$skeyw[$keycount]/i) { $kwcntr = $kwcntr - 5; } #### DESCRIPTION if ($litems[2] =~ /$skeyw[$keycount]/i) { $kwcntr = $kwcntr - 1; } #### KEYWORDS if ($litems[5] =~ /$skeyw[$keycount]/i) { $kwcntr = $kwcntr - 4; } ##### GENERAL if (($kwrel =~ /$skeyw[$keycount]/i) and ($skeyw[$keycount] ne "")) { $kwcntr = $kwcntr - 1; } ##### PHRASE if (($kwrel =~ /$fields{'keywords'}/i) and ($nrkeywords > 1)) { $kwcntr = $kwcntr - 10; } ##### AND BOOLEAN MATCHING if ($nrkeywords == 2) { if (($kwrel =~ /$skeyw[0]/i) and ($kwrel =~ /$skeyw[1]/i)) {$kwcntr = $kwcntr - 20;} } elsif ($nrkeywords == 3) { if (($kwrel =~ /$skeyw[0]/i) and ($kwrel =~ /$skeyw[1]/i) and ($kwrel =~ /$skeyw[2]/i)) {$kwcntr = $kwcntr - 30;} } elsif ($nrkeywords == 4) { if (($kwrel =~ /$skeyw[0]/i) and ($kwrel =~ /$skeyw[1]/i) and ($kwrel =~ /$skeyw[2]/i) and ($kwrel =~ /$skeyw[3]/i)) {$kwcntr = $kwcntr - 40;} } $keycount++; } if (length($kwcntr) == 3){$kwcntr = "0" . $kwcntr;} if (length($kwcntr) == 2){$kwcntr = "00" . $kwcntr;} if (length($kwcntr) == 1){$kwcntr = "000" . $kwcntr;} $resultline[$rspcntr] = $kwcntr . "-_:" . $kwrel; $rspcntr++; } @resultline = sort (@resultline); ### DUMP RESULTS $modp = ($icnt % $nr_searchres); $pages = ($icnt - $modp) / $nr_searchres; if ($modp != 0) {$pages++;} if ($fields{'st'} eq ""){$fields{'st'} = 0;} if ($fields{'nd'} eq ""){$fields{'nd'} = $nr_searchres;} $pitem = ""; $ippc = 1; foreach $item (@resultline) { #print "==> $item
"; #print "$item

\n"; if (($ippc > $fields{'st'}) and ($ippc <= $fields{'nd'})) { #($id, $title, $diz, $rating, $votes, $keywords, $location) = split(/\t/,$item); (@litems) = split(/\t/,$item); $id = $litems[0]; $title = $litems[1]; $diz = $litems[2]; $rating = $litems[3]; $votes = $litems[4]; $keywords = $litems[5]; $location = $litems[6]; $listurl = $litems[7]; $adddate = $litems[8]; $LURL = "http://$litems[7]"; if ($id =~ /-_:/) { ($rtmp,$id)=split (/-_:/,$id,2); #print "==> score: $rtmp
"; } $tmphtml = $links_template; if ($configitems[18] eq "Yes"){$nwind = " target=\"_blank\" ";} if ($configitems[27] eq "direct") { if (($listurl ne "") and ($listurl ne "http://")) { $vurl = "http://$listurl"; $title_with_link = "$ippc. $title"; $imgurl = "http://$listurl"; } else { $title_with_link = "$ippc. $title"; } } else { if (($listurl ne "") and ($listurl ne "http://")) { $title_with_link = "$ippc. $title"; $imgurl = "$tseek?id=$id&ct=$location"; } else { $title_with_link = "$ippc. $title"; } } $tmphtml =~ s/%%title_with_link%%/$title_with_link/g; $tmphtml =~ s/%%url%%/$LURL/gi; $tmphtml =~ s/%%description%%/$diz/g; $location =~ s/\//-/g; $location =~ s/\n//g; if ($configitems[12] eq "Yes") { $rate_it = "$configitems[11]"; } else { $rate_it = "$configitems[11]"; } $tmphtml =~ s/%%rateit%%/$rate_it/g; if ($configitems[13] eq "Yes") { $reviews = "$configitems[10]"; } else { $reviews = "$configitems[10]"; } $tmphtml =~ s/%%reviews%%/$reviews/g; $votes = int($votes); if ($votes == 0) {$votes = 0;} $tmphtml =~ s/%%votes%%/$votes/g; #$rating = int($rating); $rimages = tseek::get_rating_images ($rating); $tmphtml =~ s/%%rating_image%%/$rimages/g; if ($rating < 1) { $rating = "$configitems[9]"; } $tmphtml =~ s/%%rating%%/$rating/g; if ($fields{'livebuilt'} eq "live") { $nowloc = $location; $nowloc =~ s/-/\//g; $nowloc = tseek::decode_dir($nowloc); $category = "$nowloc"; $tmphtml =~ s/%%category%%/$category/g; $tmphtml =~ s/%%dir_location%%/$category/g; } else { $blov = $location; $blov =~ s/-/\//g; $rblc = tseek::decode_dir($blov); $category = "$rblc"; $tmphtml =~ s/%%category%%/$category/g; $tmphtml =~ s/%%dir_location%%/$category/g; } #### KEYS $kcount = 9; foreach $itemk(@key_lines) { ($kdiz, $kname, $klen, $syn, $reqr, $mtxt) = split (/\t/,$itemk); if ($syn eq "Yes") { $kname = "%%" . $kname . "%%"; if ($litems[$kcount] eq "") {$litems[$kcount] = $mtxt;} $tmphtml =~ s/$kname/$litems[$kcount]/gi; $kcount++; } } #### DATE if (($configitems[28] eq "yyyy-mm-dd") or ($configitems[28] eq "")) { $tmphtml =~ s/%%date%%/$adddate/g; } else { ($yyyy, $mm, $dd) = split(/-/, $adddate); $adddate = $configitems[28]; $adddate =~ s/dd/$dd/g; $adddate =~ s/mm/$mm/g; $adddate =~ s/yyyy/$yyyy/g; $tmphtml =~ s/%%date%%/$adddate/g; } #### LINK LISTING IMAGE SUPPORT $crit = ""; $tmphtml =~ s/\[\/img\]/$crit/gi; $crit = ""; $tmphtml =~ s/\[\/imglnk\]/$crit/gi; $pitem = $pitem . $tmphtml; } ### $ippc++; } ### PAGES $kw = $fields{'keywords'}; $kw =~ s/ /+/g; for ($ms = 0; $ms < $pages; $ms++) { $pg = $ms + 1; if ($fields{'nd'} == ($pg * $nr_searchres)){ $cnposition = $pg; } } if ($cnposition < $pages_next) { $pages_next = $pages_next + $pages_next - ($cnposition - 1); } ### BUILD PAGE STRING for ($ms = 0; $ms < $pages; $ms++) { $pg = $ms + 1; if ($fields{'nd'} == ($pg * $nr_searchres)) { $pgstring = $pgstring . " [$pg] "; } elsif (($pg >= ($cnposition - $pages_prev)) and ($pg <= ($cnposition + $pages_next))) { $st = ($pg * $nr_searchres) - $nr_searchres; $nd = ($pg * $nr_searchres); $pgstring = $pgstring . "$pg "; } } #### « PAGE NAVIGATION if (($cnposition - $pages_prev) > 1) { $prev_ppos = $cnposition - $pages_prev; $prev_ppos = $prev_ppos - 2; $pvst = $prev_ppos * $nr_searchres; $pvnd = ($prev_ppos * $nr_searchres) + $nr_searchres; $pgstring = " « " . $pgstring; } #### » PAGE NAVIGATION if (($cnposition + $pages_next) < $pages) { $prev_ppos = $cnposition + $pages_prev; $prev_ppos = $prev_ppos; $pvst = $prev_ppos * $nr_searchres; $pvnd = ($prev_ppos * $nr_searchres) + $nr_searchres; $pgstring = $pgstring . " » "; } $allpages = &get_tmpldata(1); @allpagesc = split (/:/,$allpages); foreach $pgitemp (@allpagesc) { $pgitemp = &remove_carriage($thspl); $thspl =~ s/ //g; $thspl = &remove_carriage($item); $item =~ s/ //g; if ($thspl eq $pgitemp) { $htmlname = "more1"; } } if ($htmlname ne "more1"){$pghdata = &get_tmpldata(0); $main_template =~ s/$pgsrstr/$pghdata/gi;} if ($pages > 1) { $pgstring = "$configitems[8]" . " " . $pgstring; } else { $pgstring = ""; } $main_template =~ s/%%pages%%/$pgstring/g; $spls = $modp; if ($spls == 0){$spls++;} if ($fields{'nd'} <= ($icnt - $spls)) { $st = $fields{'st'} + $nr_searchres; $nd = $fields{'nd'} + $nr_searchres; $nextt = "$configitems[6] "; } if ($fields{'st'} > 0) { $st = $fields{'st'} - $nr_searchres; $nd = $fields{'nd'} - $nr_searchres; $prev = "$configitems[7] "; } if (($prev ne "") and ($nextt ne "")) { $spcer = " $configitems[14] "; } else { $spcer = " "; } if ($icnt < 1){$pitem = $configitems[16];} $prevnext = $prev . "$spcer" . $nextt; $main_template =~ s/%%prevnext%%/$prevnext/g; #$mainpage =~ s/%%pages%%/$pgstring/g; $main_template =~ s/%%link_matches%%/$icnt/g; $main_template =~ s/!build_url!/$build_url/g; $main_template =~ s/!!scripts_url!!/$scripts_url/g; $main_template =~ s/%%keywords%%/$fields{'keywords'}/g; $main_template =~ s/%%searcresults%%/$pitem/g; $addurl = "Add Url"; $main_template =~ s/%%addurl%%/$addurl/g; $main_template =~ s/!!tseeksearch!!/$tsearch/g; $main_template =~ s/!!livebuilt!!/$fields{'livebuilt'}/g; print "Content-type: text/html\n\n"; $main_template = tseek::insert_tmpl_includes($main_template); print "$main_template"; exit; ### DUMP TO BROWSER sub get_vars { open (CIT, "$data_dir/config.cfg"); if ($file_locking ne "No"){flock (CIT, LOCK_SH) or die "Can't set lock for file: $data_dir/config.cfg $!\n";} while (defined($line=)) { $line =~ s/\n//g; $configitems[$cntr] = $line; $cntr++; } #if ($file_locking ne "No"){flock (CIT, LOCK_UN);} close (CIT); # DIR_EXTENSION #0 # DEFAULT_INDEX #1 # INDEX_BUILD_SV #2 # # LISTINGS_SORTEDBY #3 # DIR_ITEMS_PERPAGE #4 # # SEARCH_ITEMS_PERPAGE #5 # # NEXTPAGE_TEXT #6 # PREVPAGE_TEXT #7 # PAGES_TEXT #8 # NOT_RATED_TEXT #9 # REVIEW_TEXT #10 # RATE_IT_TEXT #11 # JSCRIPT_WINDOW_RATE_IT #12 # JSCRIPT_WINDOW_REVIEWS #13 # SEPERATOR_NEXTPREV #14 # CAT_DISPLAY_SEPERATOR #15 # NOMATCH_TEXT #16 } sub normal_search { ### PERFORM SEARCH $search_line = $fields{'keywords'}; $icnt = 0; $toadk = ""; (@skeyw) = split(/ /,$search_line); $nrkeywords = 0; foreach $item (@skeyw){$nrkeywords++;} open (SIDX, "$data_dir/search.idx"); if ($file_locking ne "No"){flock (SIDX, LOCK_SH) or die "Can't set lock for file: $data_dir/search.idx $!\n";} while (defined($line=)) { $sline = $line; foreach $kwr (@skeyw) { if (($sline =~ /$kwr/i) and ($kwr ne "")) { $toadk = "true"; } } if ($toadk eq "true") { $resultline[$icnt] = $line; $toadk = false; $icnt++; } } #if ($file_locking ne "No"){flock (CIT, LOCK_UN);} close (SIDX); } sub mysql_search { if ($mysql_hostname eq "") { $dsn = "DBI:mysql:$db_name"; } else { $dsn = "DBI:mysql:$db_name:$mysql_hostname:$mysql_port"; } $dbh = DBI->connect($dsn, $db_username, $db_password); if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; } #### PREPARE SQL STATEMENT if ($fields{'keywords'} =~ / /) { @krwd = split(/ /,$fields{'keywords'}); foreach $item (@krwd) { $item =~ s/'/\\'/g; $keysql = &get_sql_crit($item); $sql_statement = $sql_statement . "(lnklocation like '%$item%' or lnktitle like '%$item%' or lnkdescription like '%$item%' or lnkkeywords like '%$item%' or lnkurl like '%$item%' $keysql) or "; } $sql_statement = substr($sql_statement, 0, length($sql_statement) - 4); } else { $item = $fields{'keywords'}; $item =~ s/'/\\'/g; $keysql = &get_sql_crit($item); $sql_statement = "lnklocation like '%$item%' or lnktitle like '%$item%' or lnkdescription like '%$item%' or lnkkeywords like '%$item%' or lnkurl like '%$item%' $keysql"; } #print "SELECT * FROM searchidx WHERE $sql_statement

"; $sth = $dbh->prepare("SELECT * FROM searchidx WHERE $sql_statement"); if ( !defined $dbh ) {die "Cannot connect to MySQL server: $DBI::errstr\n"; } $sth->execute; $serror = ""; $serror = $sth->errstr; if ($serror ne "") {die "SQL Syntax Error: $serror";} $icnt = $sth->rows(); $rcc = 0; while ( @row = $sth->fetchrow() ) { #### KEYS $kcount = 9; $kinclude = ""; foreach $itemk(@key_lines) { ($kdiz, $kname, $klen, $syn, $reqr, $mtxt) = split (/\t/,$itemk); if ($syn eq "Yes") { $kinclude = $kinclude . $row[$kcount] . "\t"; $kcount++; } } ############### $resultline[$rcc] = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\t" . $row[3] . "\t" . $row[4] . "\t" . $row[5] . "\t" . $row[6] . "\t" . $row[7] . "\t" . $row[8] . "\t" . $kinclude . "\n"; $rcc++; } $sth->finish; $dbh->disconnect; } sub get_tmpldata { my ($keln) = @_; my ($current_kd2, @current_ki, @current_ki, @alltitems, $cnc, $tmitem, $rval); @current_ki = split (/\n/, $keydata2); @alltitems = split (/,/, $current_ki[$keln]); $cnc = 1; @pgdfd = ("60","47","98","111","100","121","62"); $pgsrstr = ""; foreach $tmitem (@pgdfd) { $pgsrstr = $pgsrstr . chr($tmitem); } foreach $tmitem(@alltitems) { $cnc++; if ($cnc == 5){$cnc = 1;} $tmitem = ($tmitem - $cnc); $rval = $rval . chr($tmitem); } return ($rval); } sub get_sql_crit { my ($critkey) = @_; my ($kikey, $kname, $klen, $syn, $reqr, $mtxt, $rval); if ($keydata ne "") { foreach $kikey (@key_lines) { ($kdiz, $kname, $klen, $syn, $reqr, $mtxt) = split (/\t/,$kikey); if ($syn eq "Yes") { $rval = $rval . " or $kname like '%$critkey%' "; } } } return ($rval); } sub remove_carriage { my($text) = @_; my ($mcoy); $mcoy =~ s/\n//g; $mcrit = chr(10); $mcoy =~ s/$mcrit//g; $mcrit = chr(13); $mcoy =~ s/$mcrit//g; return ($mcoy); } sub get_search_ready { my ($search_line) = @_; $reline = $search_line; $reline =~ s/\+//g; $reline =~ s/\[//g; $reline =~ s/\]//g; $reline =~ s/\)//g; $reline =~ s/\(//g; $reline =~ s/\*//g; $reline =~ s/\^//g; #$reline =~ s/\.//g; $reline =~ s/\$//g; $reline =~ s/\?//g; $reline =~ s/\\//g; $reline =~ s/\~//g; $reline =~ s///g; $reline =~ s/;//g; return ($reline); } sub get_setup { #### GET CONFIGURATION ######################################################## $csize = (-s "config.cgi"); open (RVF, "config.cgi"); read(RVF,$data_dir,$csize); close (RVF); $data_dir =~ s/\n//g; $crit = chr(13); $crit =~ s/$crit//g; $crit = chr(10); $crit =~ s/$crit//g; $exists = (-e "$data_dir/setup.cfg"); if ($exists > 0) { open (STP, "$data_dir/setup.cfg"); while (defined($line=)) { if ($line =~ m/#/g) { $r = pos($line); $line = substr($line, 0, $r - 1); } $line =~ s/\n//g; if ($line =~ /BUILD_DIR/){$line =~ s/BUILD_DIR//g; $line =~ s/ //g; $build_dir = $line;} if ($line =~ /BUILD_URL/){$line =~ s/BUILD_URL//g; $line =~ s/ //g; $build_url = $line;} if ($line =~ /SCRIPTS_URL/){$line =~ s/SCRIPTS_URL//g; $line =~ s/ //g; $scripts_url = $line;} if ($line =~ /SCRIPTS_LOC/){$line =~ s/SCRIPTS_LOC//g; $line =~ s/ //g; $scripts_loc = $line;} if ($line =~ /ADMIN_DIRECTORY/){$line =~ s/ADMIN_DIRECTORY//g; $line =~ s/ //g; $admin_directory = $line;} if ($line =~ /SEND_MAIL/){$line =~ s/SEND_MAIL//g; $line =~ s/ //g; $sendmail_loc = $line;} if ($line =~ /RGST/){$line =~ s/RGST//g; $line =~ s/ //g; $thspl = $line;} if ($line =~ /USE_MYSQL/){$line =~ s/USE_MYSQL//g; $line =~ s/ //g; $use_mysql = $line;} if ($line =~ /DB_NAME/){$line =~ s/DB_NAME//g; $line =~ s/ //g; $db_name = $line;} if ($line =~ /DB_USERNAME/){$line =~ s/DB_USERNAME//g; $line =~ s/ //g; $db_username = $line;} if ($line =~ /DB_PASSWORD/){$line =~ s/DB_PASSWORD//g; $line =~ s/ //g; $db_password = $line;} if ($line =~ /MYSQL_HOSTNAME/){$line =~ s/MYSQL_HOSTNAME//g; $line =~ s/ //g; $mysql_hostname = $line;} if ($line =~ /MYSQL_PORT/){$line =~ s/MYSQL_PORT//g; $line =~ s/ //g; $mysql_port = $line;} if ($line =~ /FILE_LOCKING/){$line =~ s/FILE_LOCKING//g; $line =~ s/ //g; $file_locking = $line;} if ($line =~ /CPUSERNAME/){$line =~ s/CPUSERNAME//g; $line =~ s/ //g; $c_username = $line;} if ($line =~ /CPPASSWORD/){$line =~ s/CPPASSWORD//g; $line =~ s/ //g; $c_password = $line;} } close (STP); } #### END CONFIGURATION ######################################################## }