#!/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);
    
    }