#!/usr/bin/perl
use strict;
# The first line is needed when this is run as a CGI, and it doesn't hurt
# when it's running under mod_perl. -w schould be appended when debugging.
my $mod_perl = 0; # 1=mod_perl, 0=CGI
if($ENV{GATEWAY_INTERFACE} =~ /^CGI-Perl/) { $mod_perl =1; }
#############################################################
############ Admin configuration start ######################
my $password_on_edit = 1; # set to 0 to disable password protection of the edit pages
############ Admin configuration end ########################
#############################################################
my %name;
my %user_data; my $r;
if($mod_perl) {
$r = Apache->request;
%user_data = $r->args;
my %env=$r->cgi_env;
%name = %{&where(\%env)};
} else {
use K_CGI;
use Mysql;
use Bazar;
use AltaDeja2SQL_WHERE;
# this function isn't available on linuxrx.com until the next Apache reload
%user_data = K_CGI::hent_get_data(\%ENV);
%name = %{&where(\%ENV)};
}
my %colors = %{&colors(\%user_data)};
my $dbh;
eval {
unless($dbh = Mysql->connect('','Bazar')) { &fejl("We don\'t seem to be able to cummnicate with our backend database server at the moment.
Please try again in a few minutes."); return 1;}
}; &fejl($@) if($@);
### UserID start
my $reason = "";
unless(length($user_data{'UserID'}) > 0) {
$user_data{'UserID'} = time . ":" . $ENV{'REMOTE_ADDR'};
$reason = "no ID";
}
else {
my($tid, $ip) = ($user_data{'UserID'} =~ /^(.+):(.+)$/);
unless(length($ip) > 0) {
# version 1 UserID's were formatted in another way
$reason = "Old style ID $user_data{'UserID'}";
$user_data{'UserID'} = time . ":" . $ENV{'REMOTE_ADDR'};
} elsif($ENV{'REMOTE_ADDR'} ne $ip) {
$reason = "IP mismatch";
$user_data{'UserID'} = time . ":" . $ENV{'REMOTE_ADDR'};
} else {
# Issue a new ID if it's more than 30 minutes since
# the last visit with that ID
my $s = $dbh->query("select unix_timestamp(Day) from stats where UserID like \'$user_data{'UserID'}\' order by DAY DESC limit 1");
my ($sidst) = $s->fetchrow();
if(($sidst + 1800) < time) {
if($sidst eq "") { $reason = "bogus UserID $user_data{'UserID'}"; }
else { $reason = "Too old [$sidst] " . time; }
$user_data{'UserID'} = time . ":" . $ENV{'REMOTE_ADDR'};
}
}
}
my $sth = $dbh->query("insert into stats (UserID, side, verbose, comment, edit, referer) values (\'$user_data{'UserID'}\', \'$user_data{'side'}\', \'$user_data{'verbose'}\', \'$user_data{'comment'}\', \'$user_data{'edit'}\', \'$ENV{'HTTP_REFERER'}\')");
if($reason ne "") { $sth = $dbh->query("insert into NewID (Reason) values (\'$reason\')"); }
### UserID end
my $side = &side(\%user_data, $dbh, $r);
unless($side eq "") {
$r->print(&html_top(\%name, \%colors)) if($mod_perl);
$r->print($side) if($mod_perl);
$r->print(&html_footer(\%name, \%user_data)) if($mod_perl);
print(&html_top(\%name, \%colors)) unless($mod_perl);
print($side) unless($mod_perl);
print(&html_footer(\%name, \%user_data)) unless($mod_perl);
}
if($mod_perl) { return 1; }
#
# That's all folks
#
sub side {
my %user_data = %{$_[0]};
my $dbh = $_[1];
my $r=$_[2];
my $retur;
my $sth;
my %hash; my $q;
my $why = "";
my $mode;
# The meat of the script is inside exception handling.
# That way regardless of what goes wrong (most likely
# some db-stuff or a missing perl library) we can just
# die() on purpose or involuntary and the exceptionhandling
# will print a nicely formatted error message.
# Ther's of course nothing wrong with calling &fejl() directly instead,
# it will end the program and prevent any cruft in being printed.
#
eval {
# Lets figure out why we are here
my (%menues, @row);
$sth=$dbh->query("select Navn from lister where instanceof_char like \'menu\'");
while(@row = $sth->fetchrow) { $menues{$row[0]} = 1; }
if(exists $user_data{'liste'}) { $why = "editing"; }
elsif(exists $user_data{'Commit'}) { $why = "commit"; }
elsif(exists $user_data{'commit_final'}) { $why = "commit_final"; }
elsif($user_data{'side'} eq "edit_page") { $why = "edit_page"; $user_data{'edit'} = "yes"; }
elsif($user_data{'side'} eq "questioning") { $why = "questioning"; $user_data{'edit'} = "yes"; }
elsif($user_data{'side'} eq "questioning_send") { $why = "questioning_send"; $user_data{'edit'} = "yes"; }
elsif($user_data{'side'} eq "questioning_unrelated") { $why = "questioning_unrelated"; $user_data{'edit'} = "yes"; }
elsif($user_data{'side'} eq "questioning_finished") { $why = "questioning_finished"; $user_data{'edit'} = "yes"; }
elsif(exists $user_data{'query'}) { $why = "search"; }
else {
$user_data{'side'} = "Frontpage" unless(exists $menues{$user_data{'side'}});
}
# otherwise show a page from side
# demand password for all editing functionality
if($password_on_edit) {
if($user_data{'edit'} eq "yes") {
my $access = $dbh->query("select * from edit_access");
my %users; my %hash;
while(%hash = $access->fetchhash) {
$users{$hash{'Base64'}} = [$hash{'User'}, $hash{'Pass'}];
}
my %headers = $r->headers_in;
unless(K_CGI::demand_password(\%headers, \%users, $r, "The editing pages")) { return ""; }
}
}
# mode buttons and search field
# $mode is a subroutine ref, it print's a
| Back<\/A><\/td>\n"; $retur .= " | Edit an entry<\/b><\/font><\/font><\/td>\n";
$retur .= "<\/table> \n"; if($user_data{'liste'} eq "") { # Make a new entry before continuing if none is specified #$sth=$dbh->query("lock tables lister WRITE"); $sth=$dbh->query("select ID from lister ORDER by ID DESC LIMIT 1") || die "select error: " . $dbh->errmsg; %hash = $sth->fetchhash;#print "[$hash{'ID'}] \n"; if(exists $user_data{'Navn'}) { my $q = "insert into lister ("; my $q1 = ") VALUES ("; $sth=$dbh->query("select * from lister limit 0"); foreach($sth->name) { unless(($_ eq "ID") || (($_ eq "instanceof_char") && (!$user_data{'instanceof_char'})) ) { $q .= "$_,"; $q1 .= "\'" . $user_data{$_} . "\',"; }} chop $q1; chop $q; $q .= $q1 . ")"; $sth=$dbh->query($q) || die $q . " " . $dbh->errmsg; } else { $sth=$dbh->query("insert into lister (Navn) values (\'$hash{'ID'}_unknown\')") || die "select error: " . $dbh->errmsg; } #$sth=$dbh->query("unlock tables"); $sth = $dbh->query("select * from lister where ID like \'" . ($hash{'ID'} +1) . "\'") || die "select error: " . $dbh->errmsg; } else { # else use the specified one $sth = $dbh->query("select * from lister where ID=$user_data{'liste'}") || die "select error: " . $dbh->errmsg; } $retur .= " |