#!/usr/local/bin/perl
$VERSION = "1.12";
# CGI code.pl
# Version 1.11
# Part of "WWW Cyrillic Encoding Suite"
# Get docs and newest version from
# http://www.neystadt.org/cyrillic/
#
# Copyright (c) 1997-98, John Neystadt
# You may install this script on your web site for free
# To obtain permision for redistribution or any other usage
# contact john@neystadt.org.
#
# Drop me a line if you deploy this script on your site.
# This script translates WEB pages from one Russian code to another.
# Developed by Leonid Neishtadt (http://www.neystadt.org/leonid/)
# e-mail: leonid@neystadt.org
#
# Currently the following codes are supported:
# DOS (alternate) code page CP866 (dos)
# Windows code page CP1251 (win).
# UNIX code KOI8-r (koi8 or nocs for supressing charset Metatag),
# ISO-8859-5 (iso),
# Macintosh (mac),
# Volapuk (transliteration) (vol) - only as output code.
# AUTO - auto selection of output encoding according to platform where browser
# runs (Windows, UNIX, MACINTOSH, OS/2)
#
# Usage: Copy this script into cgi-bin directory,
# refer to it as ..../cgi-bin/code.pl/"tab"/"URL to be translated"
# where "tab" is one of the above encodings or 'rus' for displaying menu
# with available codes.
# It is also can be coded as 'fromcode-tocode' for explicit definition of
# the original file encoding.
# "URL" is absolute URL from the server root (Don't forget to set $path).
# or full URL like http://cnn.com.
# All relative references from this page to other WEB pages will be also
# translated through the same code table (isn't supported yet for full URLs).
#
# Source encoding is taken from Metatag like:
#
# The tag is changed during translation or deleted for 'vol' and 'nocs'.
# If the tag is absent default encoding is taken from variable $defcode.
#
# It is recommended that you put on all
# your pages, and choose only destination encoding in urls. Do no worry for
# old buggy browsers which can't display correctly pages with this meta-tag
# NOCS encoding converts page to koi8 and deletes the meta-tag
#
# READABLE URLS
# -------------
# If you use APPACHE you can add the lines similar to those to your webserver
# configuration files:
#
# ScriptAlias /koi8 /home/www/neystadt/cgi-bin/code.pl/koi8
# ScriptAlias /win /home/www/neystadt/cgi-bin/code.pl/win
# ScriptAlias /dos /home/www/neystadt/cgi-bin/code.pl/dos
# ScriptAlias /mac /home/www/neystadt/cgi-bin/code.pl/mac
# ScriptAlias /iso /home/www/neystadt/cgi-bin/code.pl/iso
# ScriptAlias /vol /home/www/neystadt/cgi-bin/code.pl/vol
# ScriptAlias /lat /home/www/neystadt/cgi-bin/code.pl/vol
# ScriptAlias /nocs /home/www/neystadt/cgi-bin/code.pl/nocs
#
# From now you will be able to translate urls like http://www.neystadt.org/russia/
# simply by prefixing the url with encoding: http://www.neystadt.org/koi8/russia/
# or http://www.neystadt.org/lat/russia/.
#
# Note that code.pl automatically finds index.html if directory names is given
# (like in example above). The index file name can be changed by $IndexFileName
# parameters in the script.
=head1 NAME
code.pl - CGI script to convert on-the-fly html pages across cyrillic charsets
=cut
use Convert::Cyrillic;
use LWP::UserAgent;
use HTTP::Headers::UserAgent;
$path=".."; # <==== path from cgi-bin to the server root.
$defcode="WIN"; # <==== default source encoding
$maxsize=500000; # maximum file size
$IndexFileName = 'index.html';
$UserAgent=$ENV{HTTP_USER_AGENT};
$scrname=$ENV{SCRIPT_NAME};
$file=$ENV{PATH_INFO};
$file=~s/^$scrname//;
$file=~s/\+/ /go;
$file=~s/%(..)/pack("c",hex($1))/ge;
if ($file=~/[\.\/\\]([^\.\/\\]+)$/o) {$ext=lc($1);} else {$ext='html';}
$file=~s%^\/([^\/]*)%%o;
$lang=uc($+);
if ($lang eq 'RUS') {
print "Content-type: text/html\n