#!/usr/local/bin/perl # KOI.pl - CGI-Script (v0.1.1) by Maksim Moshkow, converts TXT-documents # to HTML 'on the fly'. Feel free to modify and use it. # # Íáìåîøëïå ðòåäõðòåöäåîéå. Óëòéðô òáóþéôáî äìñ òáâïôù ó ôåëóôï÷ùíé æáêìáíé # ðïäçïôï÷ìåîîùíé ÷ òåäáëôïòå Òë îá òáâïþåê óôáîãéé Âåóôá-88. # Ðïüôïíõ âõëï÷ëé ó åçï ðïäáþé ÷áí ðïëáöõôôóñ ðåòå÷åòîõôùíé. # Îå ðïìåîéôåóø éóðòá÷éôø ôáâìéþëé ðåòåëïäéòï÷ëé ÷ æõîëãéé KOI # $ALARM_TIME=2400; # ìéíéô ÷òåíåîé 40 íéîõô - úáôåí - óáíïõîéþôïöåîéå $SIG{'ALRM'} = 'timeout'; alarm $ALARM_TIME; if ($0 =~ /-KOI/) {$PEREKODER='-k';$KNAMKODER='-K';} elsif ($0 =~ /-windows/) {$PEREKODER='-w';$KNAMKODER='-W';} elsif ($0 =~ /-alt/) {$PEREKODER='-a';$KNAMKODER='-A';} elsif ($0 =~ /-iso_8859_5/) {$PEREKODER='-i';$KNAMKODER='-I';} elsif ($0 =~ /-volapuk/) {$PEREKODER='-l';$KNAMKODER='-L';} ; @kodirowki=('volapuk','windows','iso_8859_5','alt','KOI'); $ROOT_DIR = '/home/www/docs/book'; $ROOT_WWW = '/book'; # Âõäåí äá÷áôø æáêìù ëõóïþëáíé ðï 80 ëâ $piece_size=81920; $what_s_new="
"; @mesq=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); @nedelq=('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); # îåäåìñ îáþéîáåôóñ ó ÷ïóëòåóåîøñ # $|=1; # Try uncommenting this line if the script stays in memory $filename = $ROOT_DIR . $ENV{PATH_INFO}; $filename =~ s/\.\.//g ; # there is a '..' - someone tries to break in? if ($filename =~ /\/What-s-new$/) { &Directory("What-s-new") ; } if ($filename =~ /\/Contents$/) { &Contents ; } if ($filename =~ /\/with-big-pictures\.html$/) { &With_big_pictures ; } if ($filename =~ /\/with-icons\.html$/) { &With_icons ; } if ($filename =~ /\/Piece[0-9]*\.[0-9]*$/) { &Piece ; } if (-d $filename) { if ($filename =~ /\/$/) {} # there is a '/' at the end - Ok. else {print "Location: http:$ENV{SCRIPT_NAME}$PATH_INFO/\n\n"; exit 0 ;} if(-f $filename.'index.html'){$filename.='index.html';} elsif(-f $filename. '.dir'){$filename.= '.dir';} elsif(-f $filename. 'mim.dir'){$filename.= 'mim.dir';} elsif(-f $filename. 'index.htm'){$filename.= 'index.htm';} else { &Directory ;} } open(DOCUMENT, $filename) || &file_not_found("Can't open $filename"); if ($ENV{REMOTE_HOST} =~ /localhost/) { } else { $Additional_header="Last-modified: ". &Last_Modified($filename,'Full')."\n";} if ($filename =~ /\.html?$/i) { print "Content-type: text/html\n$Additional_header\n"; while (read(DOCUMENT,$bufer,100000)){print &KOI ($PEREKODER, $bufer) ;} }elsif ($filename =~ /\.dir$/) { print "Content-type: text/html\n$Additional_header\n"; &rkdir2html () ; }elsif ($filename =~ /\.txt$|\.doc$/) { print "Content-type: text/html\n$Additional_header\n
\n";
        &txt2html();
}elsif ($filename =~ /\.lbl$|\.hlp$|\.[0-9]*$/) {
	print "Content-type: text/plain\n$Additional_header\n";
        while (read(DOCUMENT,$bufer,100000)) {print &KOI($PEREKODER, $bufer);}
}elsif ($filename =~ /\.game$/) {
      print "Location: http:/cgi-bin/moshkow/Game-KOI.sh$ENV{PATH_INFO}/1\n\n";
        exit 0 ;
}else { # åóìé îå îáûå òáóûéòåîéå, ôï ïôäáôø åçï îá ïôëõð httpd
	print "Location: http:$ROOT_WWW$ENV{PATH_INFO}\n\n";
        exit 0 ;
}

close(DOCUMENT);
close(STDOUT);

exit(0);

sub file_not_found # ÷ùòõçáôøóñ é exit
{
print "Content-type: text/html\n\n",
"404 Not Found",
"

404 Not Found

", "The requested URL $ENV{PATH_INFO} was not found on this server.

", "You can mail all problems about this library to moshkow\@ipsun.ras.ru", ""; die @_[0]; } sub timeout { $SIG{'ALRM'} = 'finaltimeout'; alarm 10; # Ðïäïúòå÷áà, þôï îá printe íù ïðñôø íïöåí ðï÷éóîõôø. Äáåí åíõ 10 óåë print "\n

Timeout $ALARM_TIME while printing $filename
";
exit 1;
}

sub finaltimeout {
    exit 1;
    die "Final timeout. Aborting $filename";
}

sub txt2html { # Ðòå÷òáýáåô ÒË-ûîõà òáóëòáóëõ ÷ HTML-ôåçé

$_=; # îåô ìé ÷ ðåò÷ïê óôòïëå úáçìá÷éñ?
/^        \024(.*)\025/ && print &KOI($PEREKODER, "$1" );

open(DOCUMENT, $filename) ;

$zagolowok=0;
while (){
/^        \024(.*)\025(.*)/ && do { $_="\n" ; $zagolowok++;};
/^( *)(.*[^ ]) *¡ *([^ \n]*)/ && do { $_="$1$2\n" ;};
 s/\024//g;
 s/\025/<\/b>/g;
 s/\036D//g;
 s/\036E/<\/b><\/i>/g;
 s/^----*$/
/g ; print &KOI($PEREKODER, $_ ); }; } sub KOI # Á ôù úîáåûø, þôï ïî ëáìåþéô ÓÁÍÉ áòçõíåîôù ???????? { if ( @_[0] eq '-K' ) { @_[1] =~ tr/áâ÷çäåöúéêëìíîïðòóôõæèãþûý\377ùøüàñÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ¡/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/; } elsif ( @_[0] eq '-k' ) { @_[1] =~ tr/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/áâ÷çäåöúéêëìíîïðòóôõæèãþûý\377ùøüàñÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ¡/; } elsif ( @_[0] eq '-w' ) { @_[1] =~ tr/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ\377¡/; } elsif ( @_[0] eq '-i' ) { @_[1] =~ tr/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîï¡/; } elsif ( @_[0] eq '-I' ) { @_[1] =~ tr/°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîï¡/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/; } elsif ( @_[0] eq '-W' ) { @_[1] =~ tr/ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ\377¡/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/; } elsif ( @_[0] eq '-a' ) { @_[1] =~ tr/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357³/;@_[1] =~ s/\015*\n/\015\n/g; } elsif ( @_[0] eq '-A' ) { @_[1] =~ tr/\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357³/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/; @_[1] =~ s/\015*\n/\n/g; } elsif ( @_[0] eq '-l' ) {@_[1] =~ tr/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/ABWGDEVZIJKLMNOPRSTUFHC+{}¬YX|~Qabwgdevzijklmnoprstufhc=\133\135®yxe`q¡/; } elsif ( @_[0] eq '-L' ) {@_[1] =~ tr/ABWGDEVZIJKLMNOPRSTUFHC+{}¬YX|~Qabwgdevzijklmnoprstufhc=\133\135®yxe`q¡/ÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛݬÙØÜÀÑáâ÷çäåöúéêëìíîïðòóôõæèãþûý®ùøüàñ¡/; } @_[1]; } sub rkdir2html { # îåô ìé ÷ ðåò÷ïê óôòïëå úáçìá÷éñ? $_=; if ($_ =~ /^ (.*)/) { $_="$1\n

$1

$what_s_new$search_form
"; # @kodirowki=("volapuk","windows","iso_8859_5","alt","KOI"); form for $i (@kodirowki) {$encoding.="[$i]";} $_.="
  • Choose ".$encoding." encoding"; print &KOI($PEREKODER, $_ ); } else {open(DOCUMENT, $filename) ;} print "
    \n";
    
    while () {
     s/^----*$/
    / ; s/(.*[^ ]) *¡ *([^ \n]*)/
  • * <\/A>$1<\/i><\/b><\/A>/ ; print &KOI($PEREKODER, $_ ); } } sub Last_Modified { # 1 áòçõíåîô - éíñ æáêì, ÷ïú÷òáýáåí óéí÷ïìøîõà óôòïëõ ÷òåíñ íïäéæéëáãéé # 2 áòçõíåîô "Full | Brief" - úáäáåô æïòíáô ôåëóôï÷ïê óôòïëé ó äáôïê ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime((stat(@_[0]))[9]); if (@_[1]=~'Brief'){sprintf("%2d-%s-%d", # 29-Dec-95 $mday,@mesq[$mon],$year);} elsif(@_[1]=~'Full'){sprintf("%s, %2d-%s-%d %02d:%02d:%02d GMT", # Fri, 29-Dec-95 22:50:47 GMT @nedelq[$wday], $mday,@mesq[$mon],$year,$hour,$min,$sec);} else {sprintf("%s, %2d-%s-%d %02d:%02d:%02d GMT", @nedelq[$wday], $mday,@mesq[$mon],$year,$hour,$min,$sec);} } sub Contents { $filename =~ s/\/Contents// ; $ENV{PATH_INFO} =~ s/\/Contents// ; $name_link = $ENV{PATH_INFO} ; $name_link =~ s/.*\/// ; # Üôï âõäåô ðòïóôïå éíñ æáêìá if ( -d $filename ) # Åóìé Contents öíõô îá ëáôáìïçå, ôï ÷ïêôé ÷ îåçï { print "Location: http:$ENV{SCRIPT_NAME}$ENV{PATH_INFO}\n\n" ; exit 0 } if ($ENV{REMOTE_HOST} =~ /localhost/) { } else { $Additional_header="Last-modified: ". &Last_Modified($filename,'Full')."\n";} # Âõäåí äá÷áôø æáêìù ëõóïþëáíé ðï 80 ëâ # $piece_size=81920; # óí. ÷ùûå $filesize=(stat($filename) )[7]; if ( $filesize > $piece_size ) { $pieces="

  • Size $filesize bytes. If you permanently fail getting whole file, download it in 80Kb pieces:\n"; for ($i=1; $i-1 <= ($filesize / $piece_size) ; $i++ ) {$pieces.=sprintf ("[%d]\n",$i,$i);}; $pieces.="
    "; } print "Content-type: text/html\n$Additional_header\n". "$ENV{PATH_INFO}/Contents\n". "

    $ENV{PATH_INFO}/Contents

    \n". "
    \n";
    
    $zagolowok=0;
    open(DOCUMENT, $filename) || &file_not_found("Can't open $filename");
    while (){
    /^        \024(.*)\025(.*)/ && do {
        if ($1 =~ /[*]/){
    $_="\n

    \n"; }else{ $_="
  • $1$2\n" ; } $zagolowok++;print &KOI($PEREKODER,$_); }; } print ""; print $pieces; exit(0); } sub With_big_pictures { $filename =~ s/\/with-big-pictures\.html// ; if ($ENV{REMOTE_HOST} =~ /localhost/) { } else { $Additional_header="Last-modified: ". &Last_Modified($filename,'Full')."\n";} print "Content-type: text/html\n$Additional_header\n
    ";
    
    open(DOCUMENT, $filename) || &file_not_found("Can't open $filename");
    while (){
     s/\024//g;
     s/\025/<\/b>/g;
     s/\036D//g;
     s/\036E/<\/b><\/i>/g;
     s/^----*$/
    /g ; /^( *)(.*[^ ]) *¡ *([^ \n]*)\.jpg portret/ && do { $url=$3.".jpg" ; $_="
    $1$2\n" ;}; /^( *)(.*[^ ]) *¡ *([^ \n]*)\.jpg/ && do { $url=$3.".jpg" ; $_="
    $1$2\n" ;}; /^( *)(.*[^ ]) *¡ *([^ \n]*)/ && do { $url=$3 ; if ($url=~ /^mailto:|^http:|^ftp:|^\//) {$_="$1$2\n" ;} else {$_="$1$2\n" ;} }; print &KOI($PEREKODER, $_ ); } exit(0); } sub With_icons { $filename =~ s/\/with-icons\.html// ; if ($ENV{REMOTE_HOST} =~ /localhost/) { } else { $Additional_header="Last-modified: ". &Last_Modified($filename,'Full')."\n";} print "Content-type: text/html\n$Additional_header\n
    ";
    
    $zagolowok=0;
    
    open(DOCUMENT, $filename) || &file_not_found("Can't open $filename");
    while (){
    /^        \024(.*)\025(.*)/ && do { $_="" ; $zagolowok++;};
     s/\024//g;
     s/\025/<\/b>/g;
     s/\036D//g;
     s/\036E/<\/b><\/i>/g;
     s/^----*$/
    /g ; /^( *)(.*[^ ]) *¡ *([^ \n]*)\.jpg portret/ && do { $url=$3.".jpg" ; $_="
    $1$2\n";}; /^( *)(.*[^ ]) *¡ *([^ \n]*)\.jpg/ && do { $url=$3.".jpg" ; $_="
    $1$2\n";}; /^( *)(.*[^ ]) *¡ *([^ \n]*)/ && do { $url=$3 ; if ($url=~ /^mailto:|^http:|^ftp:|^\//) {$_="$1$2\n" ;} else {$_="$1$2\n" ;} }; print &KOI($PEREKODER, $_ ); } exit(0); } sub Directory { # âåú áòçõíåîôï÷ äáäéí ðïìîùê óðéóïë æáêìï÷ ëáôáìïçá if (@_[0] eq 'What-s-new'){$Ne_Tolxko_new=0;}else{$Ne_Tolxko_new=1;} # 'What-s-new' - ôïçäá äáåí ôïìøëï æáêìù ó íïäéæéëáãéåê < 31 äîåê $filename =~ s/\/What-s-new/\// ; if ($ENV{REMOTE_HOST} =~ /localhost/) { } else { $Additional_header="Last-modified: ". &Last_Modified($filename,'Full')."\n";} print "Content-type: text/html\n$Additional_header\n". "$ENV{PATH_INFO}\n

    $ENV{PATH_INFO}

    \n". "
    ".
    " Name                              Size   Last modified\n
    "; opendir(dirhandle, $filename)|| &file_not_found("Can't open dir $filename"); while ($fname=readdir(dirhandle)){@dir = ( @dir, $fname );} foreach $fname (sort @dir) { # äéòåëôïòéà úáþéôáìé ÷ íáóóé÷ é ïôóïòôéòï÷áìé ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename.$fname); if (-d _ ) {$fname.="/";$image="menu";}else{$image="unknown";} if (!($fname =~ /^\./ ) && ( $Ne_Tolxko_new || (time()-$mtime < 2678400)) ) { printf (" %-28.28s %9d \t %s \n",$fname,$image,substr($fname,0,24)."", $size, &Last_Modified($filename.$fname,'Brief') ); } } exit(0); } sub Piece { # Ïôòåúáåí ëõóïþåë úáðòïûåîîïçï æáêìá/Piece80.02 $ENV{PATH_INFO} =~ s/\/Piece.*// ; $filename =~ s/\/Piece([0-9]*).([0-9]*)// ; $piece_size=80*1024; $piece_number=$2; $poziciq=$piece_size*($piece_number-1); $filesize=(stat($filename) )[7]; if ($ENV{REMOTE_HOST} =~ /localhost/) { } else { $Additional_header="Last-modified: ". &Last_Modified($filename,'Full')."\n";} #"$ENV{PATH_INFO}/Piece $piece_size beginning at $poziciq
    \n";
    print "Content-type: text/plain\n$Additional_header\n";
    
    if ($filesize < $poziciq ) {
        print "You miss - file is shoter, than you request";
        exit 1;
    }
    
    open(DOCUMENT, $filename) || &file_not_found("Can't open $filename");
    
    seek(DOCUMENT,$poziciq,0) || &file_not_found("Can't seek $pozition $filename");
    
    read(DOCUMENT,$bufer,$piece_size) ;
      if ($filename =~ /\.html?$|\.txt$|\.doc$|\.lbl$|\.hlp$|\.[0-9]*$/ )
             { print &KOI ($PEREKODER, $bufer) }
        else { print $bufer; }
    
    close (DOCUMENT);
    
    exit (0);
    
    }