#!/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. # # mALENXKOE PREDUPREZHDENIE. sKRIPT RASCHITAN DLYA RABOTY S TEKSTOVYMI FAJLAMI # PODGOTOVLENNYMI V REDAKTORE rK NA RABOCHEJ STANCII bESTA-88. # pO|TOMU BUKOVKI S EGO PODACHI VAM POKAZHUTTSYA PEREVERNUTYMI. # nE POLENITESX ISPRAVITX TABLICHKI PEREKODIROVKI V FUNKCII KOI # $ALARM_TIME=2400; # LIMIT VREMENI 40 MINUT - ZATEM - SAMOUNICHTOZHENIE $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'; # bUDEM DAVATX FAJLY KUSOCHKAMI PO 80 KB $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'); # NEDELYA NACHINAETSYA S VOSKRESENXYA # $|=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 { # ESLI NE NASHE RASSHIRENIE, TO OTDATX EGO NA OTKUP httpd print "Location: http:$ROOT_WWW$ENV{PATH_INFO}\n\n"; exit 0 ; } close(DOCUMENT); close(STDOUT); exit(0); sub file_not_found # VYRUGATXSYA I 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; # pODOZREVAYU, CHTO NA printe MY OPYATX MOZHEM POVISNUTX. dAEM EMU 10 SEK print "\n
"; exit 1; } sub finaltimeout { exit 1; die "Final timeout. Aborting $filename"; } sub txt2html { # pREVRASHCHAET rk-SHNUYU RASKRASKU V HTML-TEGI $_=; # NET LI V PERVOJ STROKE ZAGLAVIYA? /^ \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/^----*$/
$1$2
/g ; print &KOI($PEREKODER, $_ ); }; } sub KOI # a TY ZNAESHX, CHTO ON KALECHIT sami ARGUMENTY ???????? { if ( @_[0] eq '-K' ) { @_[1] =~ tr/ABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH\377YX|YUYAabvgdezhzijklmnoprstufhcchshshch®y'eyuya¡/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/; } elsif ( @_[0] eq '-k' ) { @_[1] =~ tr/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/ABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH\377YX|YUYAabvgdezhzijklmnoprstufhcchshshch®y'eyuya¡/; } elsif ( @_[0] eq '-w' ) { @_[1] =~ tr/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/yuabcdefghijklmnopyarstuzhv'yzsheshchch®YUABCDEFGHIJKLMNOPYARSTUZHVXYZSH|SHCHCH\377¡/; } elsif ( @_[0] eq '-i' ) { @_[1] =~ tr/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/°±²³´µ¶·¸¹º»¼½¾¿yuabcdefghijklmnopyarstuzhv'yzsheshchch®YUABCDEFGHIJKLMNO¡/; } elsif ( @_[0] eq '-I' ) { @_[1] =~ tr/°±²³´µ¶·¸¹º»¼½¾¿yuabcdefghijklmnopyarstuzhv'yzsheshchch®YUABCDEFGHIJKLMNO¡/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/; } elsif ( @_[0] eq '-W' ) { @_[1] =~ tr/yuabcdefghijklmnopyarstuzhv'yzsheshchch®YUABCDEFGHIJKLMNOPYARSTUZHVXYZSH|SHCHCH\377¡/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/; } elsif ( @_[0] eq '-a' ) { @_[1] =~ tr/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/\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³/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/; @_[1] =~ s/\015*\n/\n/g; } elsif ( @_[0] eq '-l' ) {@_[1] =~ tr/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/ABWGDEVZIJKLMNOPRSTUFHC+{}¬YX|~Qabwgdevzijklmnoprstufhc=\133\135®yxe`q¡/; } elsif ( @_[0] eq '-L' ) {@_[1] =~ tr/ABWGDEVZIJKLMNOPRSTUFHC+{}¬YX|~Qabwgdevzijklmnoprstufhc=\133\135®yxe`q¡/abvgdezhzijklmnoprstufhcchshshch¬y'eyuyaABVGDEZHZIJKLMNOPRSTUFHCCHSHSHCH®YX|YUYA¡/; } @_[1]; } sub rkdir2html { # NET LI V PERVOJ STROKE ZAGLAVIYA? $_=; if ($_ =~ /^ (.*)/) { $_=" $1 \n"; # @kodirowki=("volapuk","windows","iso_8859_5","alt","KOI"); form for $i (@kodirowki) {$encoding.="[$i]";} $_.=" $1
$what_s_new$search_formChoose ".$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 ARGUMENT - IMYA FAJL, VOZVRASHCHAEM SIMVOLXNUYU STROKU VREMYA MODIFIKACII # 2 ARGUMENT "Full | Brief" - ZADAET FORMAT TEKSTOVOJ STROKI S DATOJ ($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/.*\/// ; # eTO BUDET PROSTOE IMYA FAJLA if ( -d $filename ) # eSLI Contents ZHMUT NA KATALOGE, TO VOJTI V NEGO { 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";} # bUDEM DAVATX FAJLY KUSOCHKAMI PO 80 KB # $piece_size=81920; # SM. VYSHE $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 "; 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
$1$2
\n"; }else{ $_="$1$2\n" ; } $zagolowok++;print &KOI($PEREKODER,$_); }; } print " "; 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/^----*$/
$1$2
/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 { # BEZ ARGUMENTOV DADIM POLNYJ SPISOK FAJLOV KATALOGA if (@_[0] eq 'What-s-new'){$Ne_Tolxko_new=0;}else{$Ne_Tolxko_new=1;} # 'What-s-new' - TOGDA DAEM TOLXKO FAJLY S MODIFIKACIEJ < 31 DNEJ $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) { # DIREKTORIYU ZACHITALI V MASSIV I OTSORTIROVALI ($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 { # oTREZAEM KUSOCHEK ZAPROSHENNOGO FAJLA/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); }