Diff for /db/prgsrc/db.cgi between versions 1.124 and 1.132

version 1.124, 2003/08/29 21:52:25 version 1.132, 2004/03/17 10:26:31
Line 2 Line 2
   
 use DBI;  use DBI;
 use CGI ':all';  use CGI ':all';
 use strict;  #use strict;
 use Time::Local;  use Time::Local;
   my $proxyredirect=1;
 use POSIX qw(locale_h);  use POSIX qw(locale_h);
 use locale;  use locale;
 use vars qw($opt_z);  use vars qw($opt_z);
 use Getopt::Std;  use Getopt::Std;
   #my ($dbuser,$dbname,$dbpass,$dbhost);
   require "dbdefs.pl";
   my $url=url||'';
   $dbuser||="piataev";
   $dbname||="chgk";
   $dbpass||="";
   $dbhost||="localhost";
 getopts('z');  getopts('z');
 $opt_z||=param("makehtml");  $opt_z||=param("makehtml");
 my $timestamp="_timestamp.tmp";  my $timestamp="_timestamp.tmp";
   my $usehash=0;
 my $paramtour;  my $paramtour;
 my $withanswers=param('answer')||param('answers');  my $withanswers=param('answer')||param('answers');
 open STDERR, ">/var/tmp/errors1";  open STDERR, ">/var/tmp/errors1";
 my $newsurl='http://news.chgk.info/';  my $newsurl='http://news.chgk.info/';
 my $reklama="../dimrub/db/reklama.html";  my $reklama="../dimrub/db/reklama.html";
 my $footer="../dimrub/db/footer.html";  my $footer="../dimrub/db/footer.html";
   $footer="../../chgk/footer.html" if $url=~/zaba/;
   $reklama="../../chgk/reklama.html" if $url=~/zaba/;
 my $datefooter="../dimrub/db/date";  my $datefooter="../dimrub/db/date";
   $datefooter="../../chgk/date" if $url=~/zaba/;
   
 my $fname;  my $fname;
 $reklama="../reklama.html" if $opt_z;  $reklama="../reklama.html" if $opt_z;
Line 33  if ($^O =~ /win/i) { Line 44  if ($^O =~ /win/i) {
 {  {
   $realHTMLDIR="/home/piataev/public_html/dimrub/db/files/";    $realHTMLDIR="/home/piataev/public_html/dimrub/db/files/";
 }  }
   
   
 my $usehtml=$opt_z||0;  my $usehtml=$opt_z||0;
 $usehtml=1;  $usehtml=1;
   $usehtml=0 if $url=~/zaba/;
   
 my $usewas=0;  my $usewas=0;
 my $cashednumber=500;  my $cashednumber=500;
 my $outputnumber=10;  my $outputnumber=10;
 my ($proxyptext,$proxysstr);  my ($proxyptext,$proxysstr);
 my $printqueries=0;  my $printqueries=0;
 my $url=url||'';  
 my $qs=query_string;  my $qs=query_string;
 my $globaloutput;  my $globaloutput;
 my %forbidden=();  my %forbidden=();
 my $debug=0; #added by R7  my $debug=0; #added by R7
   my $metod=param('metod')||'';
 my $outputkvo=param('kvo') ||$outputnumber;  my $outputkvo=param('kvo') ||$outputnumber;
 $outputkvo=100 if $outputkvo>100;  $outputkvo=100 if $outputkvo>100;
   
 if (param('debug')) {$debug=1; $printqueries=1}  if (param('debug')) {$debug=1; $printqueries=1}
 *STDERR=*STDOUT if $debug;  *STDERR=*STDOUT if $debug;
 if ($url !~ /db\.chgk\.info/ && $url !~ /localhost/ && $url !~ /bilbo/) {  if ($url !~ /db\.chgk\.info/ && $url !~ /localhost/ && $url !~ /bilbo/ && $url !~ /zaba/) {
    my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs";     my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs";
    Redirect ($u);     Redirect ($u);
    exit;     exit;
 }  }
   
   if ($proxyredirect && $metod=~/proxy/ && $url !~ /localhost/ && $url !~ /bilbo/ && $url !~ /zaba/) {
      my $u="http://chgk.zaba.ru/cgi-bin/db.cgi?$qs";
      Redirect ($u);
      exit;
   }
   
 #if (!param('sstr') && param('all')) {  #if (!param('sstr') && param('all')) {
 #          my $destination='http://db.chgk.info/all.html';  #          my $destination='http://db.chgk.info/all.html';
 #               Redirect($destination);  #               Redirect($destination);
Line 93  my %TypeName=('children'=>'Д', 'game'=>' Line 115  my %TypeName=('children'=>'Д', 'game'=>'
               'chgk'=>'Ч', 'brain'=>'Б', 'beskrylka'=>'Л','ehruditka'=>'Э');                'chgk'=>'Ч', 'brain'=>'Б', 'beskrylka'=>'Л','ehruditka'=>'Э');
   
   
   sub countz {
     my ($dbh,$type)=@_;
     
     my $sth=$dbh->prepare("select count(*) from Questions where Type LIKE '%$type%'");
     $sth->execute();
     my ($tmp)=$sth->fetchrow();
     return $tmp;
   }
   
 my $all=param('all');  my $all=param('all');
 $all=0 if lc $all eq 'no';  $all=0 if lc $all eq 'no';
Line 260  sub tourhref { Line 290  sub tourhref {
         return $res;          return $res;
    } else {     } else {
         $res=$url;          $res=$url;
         $res.="?tour=$t";          $res.=$a?"?answers=1&":"?";
         $res.=$a?"&answers=1":"";          $res.="tour=$t";
   
         return $res;          return $res;
    }     }
   
Line 398  action="/znatoki/cgi-bin/db.cgi"> Line 429  action="/znatoki/cgi-bin/db.cgi">
 </td>  </td>
 </tr>  </tr>
 </table>  </table>
   <p><i>Если при попытке поиска выдаётся сообщение об ошибке, 
   попробуйте воспользоваться 
   <a href="http://chgk.zaba.ru/search.html">зеркалом базы</a></i>
 </center>  </center>
   
 EOT  EOT
Line 436  sub makeproxysstr { Line 470  sub makeproxysstr {
 #      $good{$words[$_]}=1 foreach 0..4;  #      $good{$words[$_]}=1 foreach 0..4;
       foreach (@words)        foreach (@words)
       {        {
          $good{$_}=1 if $c{$_}<200;           $good{$_}=1 if $c{$_}<200 && length $_>2;
       }        }
   
       $good{$words[$_]}=0 foreach 16..$#words;        $good{$words[$_]}=0 foreach 16..$#words;
Line 738  sub makewhere { Line 772  sub makewhere {
       $type .= ($_=$TypeName{$_}) foreach @type;        $type .= ($_=$TypeName{$_}) foreach @type;
       my $where=' 0 ';        my $where=' 0 ';
       foreach (@type) {        foreach (@type) {
              $where.= " OR (Type ='$_') OR (Type ='$_Д') ";               $where.= " OR (Type ='$_') OR (Type ='$_Д') OR (Type ='Д$_') ";
       }         } 
       $where.= "OR (Type='ЧБ')" if ($type=~/Ч|Б/);        $where.= "OR (Type='ЧБ')" if ($type=~/Ч|Б/);
       return $where;        return $where;
Line 966  sub PrintRandom { Line 1000  sub PrintRandom {
    my %q;     my %q;
    my $answer=$razd?0:1;     my $answer=$razd?0:1;
    my @answers;     my @answers;
   #my $t=time;
    my (@Questions) = &Get12Random($dbh, $type, $num);     my (@Questions) = &Get12Random($dbh, $type, $num);
         my ($output, $i) = ('', 0);  
   
           my ($output, $i) = ('', 0);
   #$output.="time=".(time-$t).p;
         if ($text) {          if ($text) {
                 $output .= "    $num случайных вопросов.\n\n";                  $output .= "    $num случайных вопросов.\n\n";
         } else {          } else {
Line 1050  sub PrintTournament { Line 1086  sub PrintTournament {
                         };                          };
                 }                  }
         } else {          } else {
                 my ($qnum) = GetQNum($dbh);                  my ($qnum) = GetQNum($dbh,0);
                 $output .= h2("Банк Вопросов: $qnum вопрос".&Suffix($qnum))                   my ($qnum1) = GetQNum($dbh,1);
                           . p . "\n";                  $output .= h2("База вопросов").
                    h3("$qnum запис".&Suffix2($qnum).
                    " (уникальных $qnum1)");
   
   #                h4("<table>".
   #                Tr(td("Из них:"), td("Вопросов ЧГК: ".countz($dbh,'Ч'))).               
   #                Tr(td("&nbsp;"), td("Вопросов для брейна: ".countz($dbh,'Б'))).                 
   #                Tr(td("&nbsp;"), td("Вопросов для ЧГК и брейна: ".countz($dbh,'ЧБ'))).          
   #                Tr(td("&nbsp;"), td("Интернет-вопросов: ".countz($dbh,'И'))).           
   #                Tr(td("&nbsp;"), td("Бескрылок: ".countz($dbh,'Л'))).
   #                Tr(td("&nbsp;"),td("Заданий для Своей Игры: ".countz($dbh,'Я'))).
   #                Tr(td("&nbsp;"),td("Эрудиток: ".countz($dbh,'Э'))))."</table>"
         }          }
   
         for ($i = 0; $i <= $#Tours; $i++) {          for ($i = 0; $i <= $#Tours; $i++) {
Line 1143  sub Suffix { Line 1190  sub Suffix {
         return $suffix;          return $suffix;
 }  }
   
   
   sub Suffix1 {
           my ($qnum) = @_;
           my ($suffix) = 'я' if $qnum =~ /[234]$/;
          $suffix = 'е' if $qnum =~ /1$/;
          $suffix = 'й' if $qnum =~ /[567890]$/ || $qnum =~ /1.$/;
           return $suffix;
   }
   
   sub Suffix2 {
           my ($qnum) = @_;
           my ($suffix) = 'и' if $qnum =~ /[234]$/;
          $suffix = 'ь' if $qnum =~ /1$/;
          $suffix = 'ей' if $qnum =~ /[567890]$/ || $qnum =~ /1.$/;
           return $suffix;
   }
   
 sub IsTour {  sub IsTour {
         my ($dbh, $Id,$n) = @_;          my ($dbh, $Id,$n) = @_;
   
Line 1249  sub PrintField { Line 1313  sub PrintField {
             $value =~ s/<[\/\w]*?>//sg;              $value =~ s/<[\/\w]*?>//sg;
         } else {          } else {
             $value =~ s/^\s+/<br>&nbsp;&nbsp;&nbsp;&nbsp;/mg;              $value =~ s/^\s+/<br>&nbsp;&nbsp;&nbsp;&nbsp;/mg;
               $value =~ s/(\s+)-+(\s+)/$1&#0150;$2/mg;
               $value =~ s/\s+\&#0150/&nbsp;\&#0150/mg 
                           if $value !~ /^\|/;
             $value =~ s/^\|([^\n]*)/<pre>$1<\/pre>/mg;              $value =~ s/^\|([^\n]*)/<pre>$1<\/pre>/mg;
             $value =~ s/\s+-+\s+/&nbsp;&#0150; /mg;  
             $value =~ s/(http:\/\/\S+[^\s\)\(\,\.])/<a href="$1">$1<\/a>/g if $header !~ /^Авто/;              $value =~ s/(http:\/\/\S+[^\s\)\(\,\.])/<a href="$1">$1<\/a>/g if $header !~ /^Авто/;
 #           $value =~ s/(http:\/\/(?:\w+.)+[\w\\\~]+(\?[^\s.]+)?)/<a href="$1">$1<\/a>/g if $header !~ /^Авто/;  #           $value =~ s/(http:\/\/(?:\w+.)+[\w\\\~]+(\?[^\s.]+)?)/<a href="$1">$1<\/a>/g if $header !~ /^Авто/;
 #           $value =~ s/(\s)"/$1&#147;/mg;  #           $value =~ s/(\s)"/$1&#147;/mg;
Line 1405  qid=$qid"}, 'Близкие вопросы').p Line 1471  qid=$qid"}, 'Близкие вопросы').p
   
 # Returns the total number of questions currently in the DB.  # Returns the total number of questions currently in the DB.
 sub GetQNum {  sub GetQNum {
         my ($dbh) = @_;          my ($dbh,$x) = @_;
         my ($sth) = $dbh->prepare("SELECT COUNT(*) FROM Questions");          my ($sth) = $dbh->prepare("SELECT COUNT(*) FROM Questions");
         $sth->execute;          $sth->execute;
         my $tmp=($sth->fetchrow)[0];          my $tmp=($sth->fetchrow)[0];
         $sth->finish;          $sth->finish;
         return $tmp;          ($sth)=  $dbh -> prepare("select distinct count(first) FROM equalto");
                      $sth -> execute;
           my ($c)=$sth->fetchrow;
   
   #                  $i++  while ( my  ($first, $second)=$sth -> fetchrow)
   #                  {
   #                       $forbidden{$first}=1;
   #                  }
                     $sth->finish;
   
           return $tmp-($x?$c:0);
 }  }
 sub GetMaxQId {  sub GetMaxQId {
         my ($dbh) = @_;          my ($dbh) = @_;
Line 1431  sub Get12Random { Line 1507  sub Get12Random {
         srand;          srand;
         my $where=0;          my $where=0;
         my $r=int (rand(10000));          my $r=int (rand(10000));
           my $w1=$r<5000? "QuestionId<50000 ":"QuestionId>=50000";
           $w1=1 if $url=~/zaba/;
           $w1=1 if $type!~/Ч/;
         foreach (split '', $type)          foreach (split '', $type)
         {          {
            $where.= " OR (Type ='$_') OR (Type ='$_Д') ";             $where.= " OR (Type ='$_') OR (Type ='$_Д') ";
Line 1441  sub Get12Random { Line 1519  sub Get12Random {
 #   $q="select QuestionId, QuestionId/$r-floor(QuestionId/$r) as val   #   $q="select QuestionId, QuestionId/$r-floor(QuestionId/$r) as val 
 #       from Questions where $where order by val limit $num";  #       from Questions where $where order by val limit $num";
 # Когда на куличках появится mysql >=3.23 надо заменить на order by rand();  # Когда на куличках появится mysql >=3.23 надо заменить на order by rand();
    $q="select QuestionId from Questions where $where order by rand() limit $num";     $q="select QuestionId from Questions where ($w1) AND ($where) order by rand() limit $num";
   
   
    $sth=$dbh->prepare($q);     $sth=$dbh->prepare($q);
Line 1858  MAIN: Line 1936  MAIN:
         $tour = (param('tour')) ? param('tour') : 0;          $tour = (param('tour')) ? param('tour') : 0;
         my $texttour=$tour;          my $texttour=$tour;
         my ($sth,$dbh);          my ($sth,$dbh);
         $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")          my($dsn) = "DBI:mysql:database=$dbname;host=$dbhost";
           $dbh = DBI->connect($dsn, $dbuser, $dbpass) 
   #       $dbh = DBI->connect("DBI:mysql:$dbname", $username, $dbpass)
                 or do {                  or do {
                         print h1("Временные проблемы") . "База вопросов временно не                          print header.h1("Временные проблемы") . "База вопросов временно не
                         работает. Заходите попозже.";                          работает. Заходите попозже.";
                         print &Include_virtual("$reklama") if $url!~/localhost/;                          print &Include_virtual("$reklama") if $url!~/localhost/;
                     print end_html;                      print end_html;
Line 1869  MAIN: Line 1949  MAIN:
   
   
         if (param('qid') && (param('qid')=~/^\d+$/) || $tour && $tour=~/^\d+$/) {          if (param('qid') && (param('qid')=~/^\d+$/) || $tour && $tour=~/^\d+$/) {
            my $destination='http://db.chgk.info/search.html';             my $destination='http://chgk.zaba.ru/search.html';
 #               print header (-'Content-Type' => 'text/html',  #               print header (-'Content-Type' => 'text/html',
 #                -'Location:'=> 'http:\\db.chgk.info');  #                -'Location:'=> 'http:\\db.chgk.info');
                 Redirect($destination);                  Redirect($destination);
                 exit                  exit
         }          }
   
         if ($tour && (!param('answers')||!param('answers')<=1))          if ($tour && !param('qnumber') && (!param('answers')||(param('answers')<=1)))
         {                 {       
                 my $n=param('tour');                  my $n=param('tour');
                 $n=~s/.txt$//;                          $n=~s/.txt$//;        
Line 1885  MAIN: Line 1965  MAIN:
                 my $d=$destination;                  my $d=$destination;
                 $d=~s/$HTMLDIR/$realHTMLDIR/;                  $d=~s/$HTMLDIR/$realHTMLDIR/;
 #               print header.$destination;  #               print header.$destination;
                 print header."$d|".(-e "$realHTMLDIR$n.html");  #                print header."$d|".(-e "$realHTMLDIR$n.html");
                 if (-e $d) {                  if (-e $d) {
                         Redirect($destination);                          Redirect($destination);
                         exit                          exit
                 }                  }
                 $d=~s/\.\d+//;                  $d=~s/\.\d+//;
                 $destination=~s/\.\d+//;                  $destination=~s/\.\d+//;
 print br.br.$d;  
                 if (-e $d) {                  if (-e $d) {
                         Redirect($destination);                          Redirect($destination);
                         exit                          exit
Line 1925  print br.br.$d; Line 2004  print br.br.$d;
   
         if ($text && !param ('comp')) {          if ($text && !param ('comp')) {
                 print header('text/plain');                  print header('text/plain');
         } elsif (!param('comp')) {print header;}          } elsif (!param('comp')) {
                   print header(-charset =>'koi8-r')}
           my $sstr=param('sstr');            my $sstr=param('sstr');
           $opt_z||=param("makehtml");            $opt_z||=param("makehtml");
           if (param('qid')) {            if (param('qid')) {
Line 1979  print br.br.$d; Line 2059  print br.br.$d;
            my $title="Результаты поиска на \"". ($proxysstr||$sstr) .'"'              my $title="Результаты поиска на \"". ($proxysstr||$sstr) .'"' 
                                                     if ($proxysstr||$sstr);                                                      if ($proxysstr||$sstr);
            $title||="База вопросов";             $title||="База вопросов";
   
            $globaloutput.=start_html(-"title"=>$title,             $globaloutput.=start_html(-"title"=>$title,
                    -author=>'dimrub@icomverse.com',                     -author=>'dimrub@icomverse.com',
                    -bgcolor=>'#fff0e0',                     -bgcolor=>'#fff0e0',
                                   -vlink=>'#800020');                                    -vlink=>'#800020');#}
 $globaloutput.="<style>  $globaloutput.="<style>
 td      {font-size: x-small; font-family : sans-serif}  td      {font-size: x-small; font-family : sans-serif}
 th      {font-size: x-small; font-family : sans-serif}  th      {font-size: x-small; font-family : sans-serif}
Line 1992  th {font-size: x-small; font-family : sa Line 2071  th {font-size: x-small; font-family : sa
                 $globaloutput.=&Include_virtual("$reklama")||'';                  $globaloutput.=&Include_virtual("$reklama")||'';
         }          }
   
        if (!$opt_z && length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)|(all=)/i) {         if ($usehash && !$opt_z && length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)|(all=)/i) {
                 my $sth=$dbh->prepare("SELECT page,times,t from hash where query=".$dbh->quote($qs));                  my $sth=$dbh->prepare("SELECT page,times,t from hash where query=".$dbh->quote($qs));
                 $sth->execute();                  $sth->execute();
                 my ($p,$times,$t)=$sth->fetchrow();                  my ($p,$times,$t)=$sth->fetchrow();
Line 2177  my ($sth) = $dbh->prepare("SELECT t1.Id, Line 2256  my ($sth) = $dbh->prepare("SELECT t1.Id,
         }          }
         if (!$opt_z){           if (!$opt_z){ 
           print $globaloutput;            print $globaloutput;
           if (($qs!~ /(rand)|(sstr)|(comp)/i) && (length $qs<=255)) {            if (($qs!~ /(rand)|(sstr)|(comp)/i) && (length $qs<=255) && $usehash) {
                 $globaloutput=  $dbh->quote($globaloutput);                  $globaloutput=  $dbh->quote($globaloutput);
                 $dbh->do("insert into hash (query,page) values (".                  $dbh->do("insert into hash (query,page) values (".
                 $dbh->quote($qs).                  $dbh->quote($qs).

Removed from v.1.124  
changed lines
  Added in v.1.132


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>