#!/usr/local/bin/perl
# Copyright 1994-2000, Cunningham & Cunningham, Inc.
# in collaboration with Dave W. Smith
# Open Source for personal use only.
# ... and then only
# with the understanding that the owner(s) cannot be
# responsible for any behavior of the program or
# any damages that it may cause. See LICENSE.TXT

# use strict;
$|++;
#use locale ;
local $| = 1; 
$WikiDB = "../db/pustwiki.txt";

# print "Content-type: text/html\n\n";  # only needed if we use thankyou page

my $mark = "\263";
my $pomark = "\265";

my $query=&urldecode($ENV{'QUERY_STRING'}) ;

my $page = $query =~ /^(\S+\_\S+(\_\S+)?)$/
	? $&
	: die("BadSaveName\n");


if($ENV{'REQUEST_METHOD'} eq 'GET'){$_=$ENV{'QUERY_STRING'};}
elsif($ENV{'REQUEST_METHOD'} eq 'POST')
  {sysread(STDIN,$_,$ENV{'CONTENT_LENGTH'});}

my ($body, %body);
foreach $_ (split(/&/, $_)) {
	s/\+/ /g;
	s/\%(..)/pack('C', hex($1))/geo;
	($_, $body) = split (/=/, $_, 2);
	$body{$_} = $body;
}

$_ = $body{Text};
chomp;
s/\r\n/\n/g;  # replace DOS crlf with lf
s/\r/\n/g;  # replace cr with lf
s/\r//g;
s/&/&amp;/g;
s/</&lt;/g;
s/>/&gt;/g;
s/$mark//g;
s/\n/ $pomark/g;


$anket_str=join('#',($page,$_,"#"));
open(DATA,">>$WikiDB") || print_err("Cannot open $WikiDb $!");
#flock(DATA,2);  
print DATA "$anket_str\n";
#flock(DATA,8);
  close(DATA);


do "wiki.pl" ;

#---- Russian Subs

sub urldecode{
 local($val)=@_;
 $val=~s/\+/ /g;
 $val=~s/%([0-9a-hA-H]{2})/pack('C',hex($1))/ge;
 return $val;
 }

