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