From: Michael Andreen Date: Fri, 29 Dec 2006 12:42:43 +0000 (+0000) Subject: restructing to modules X-Git-Url: https://ruin.nu/git/?p=ndwebbie.git;a=commitdiff_plain;h=b0f108eb4600f42fe3391acbe86381ce0c2d4681 restructing to modules --- diff --git a/ND.pm b/ND.pm new file mode 100755 index 0000000..c2bb6c0 --- /dev/null +++ b/ND.pm @@ -0,0 +1,142 @@ +#!/usr/bin/perl -w -T +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ + +package ND; +use CGI qw/:standard/; +use HTML::Template; +use DBI; +use DBD::Pg qw(:pg_types); +use Apache2::Request; +use ND::Include; +use ND::DB; +use strict; +use warnings FATAL => 'all'; + +sub handler { + local $ND::r = shift; + local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M"); + local $ND::DBH; + local $ND::USER; + local $ND::UID; + local $ND::PLANET; + local $ND::TEMPLATE; + local $ND::BODY; + local $ND::TICK; + local %ND::GROUPS; + local $ND::PAGE = $ND::req->param('page'); + + if ($ENV{'SCRIPT_NAME'} =~ /(\w+)(\.(pl|php|pm))?$/){ + $ND::PAGE = $1 unless $1 eq 'index' and $3 eq 'pl'; + } + page(); + return Apache2::Const::OK; +} + +sub page { + our $DBH = ND::DB::DB(); + our $USER = $ENV{'REMOTE_USER'}; + my $error;# = $ND::r->param('page'); + + chdir '/var/www/ndawn/code'; + + our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1); + + our ($UID,$PLANET) = $DBH->selectrow_array('SELECT uid,planet FROM users WHERE username = ?' + ,undef,$ENV{'REMOTE_USER'}); + + our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef); + + + my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?'); + $query->execute($UID); + + our $ATTACKER = 0; + undef our %GROUPS; + while (my ($name,$attack,$gid) = $query->fetchrow()){ + $GROUPS{$name} = $gid; + $ATTACKER = 1 if $attack; + } + + + our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)'); + + my $page = 'main'; + if ($ND::PAGE =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users|alliances|memberIntel|resources|planetNaps)$/){ + $page = $1; + } + + our $XML = 0; + $XML = 1 if param('xml') and $page =~ /^(raids)$/; + + our $AJAX = 1; + + my $type = 'text/html'; + if ($XML){ + $type = 'text/xml'; + $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1); + $ND::BODY = HTML::Template->new(filename => "templates/${page}.xml.tmpl", cache => 1); + }else{ + $ND::BODY = HTML::Template->new(filename => "templates/${page}.tmpl", global_vars => 1, cache => 1); + $ND::BODY->param(PAGE => $page); + } + + + unless (my $return = do "${page}.pl"){ + $error .= "

couldn't parse $page: $@

" if $@; + $error .= "

couldn't do $page: $!

" unless defined $return; + $error .= "

couldn't run $page

" unless $return; + } + + unless ($XML){ + my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID); + + + $TEMPLATE->param(Tick => $TICK); + $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember()); + $TEMPLATE->param(isHC => isHC()); + $TEMPLATE->param(isDC => isDC()); + $TEMPLATE->param(isBC => isBC()); + $TEMPLATE->param(isIntel => isBC()); + $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))); + if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){ + $ND::TEMPLATE->param(Targets => listTargets()); + } + $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1'); + $TEMPLATE->param(Error => $error); + + } + $ND::TEMPLATE->param(BODY => $ND::BODY->output); + my $output = $TEMPLATE->output; + print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output); + print $output; + + + $DBH->disconnect; + $DBH = undef; + $UID = undef; + $USER = undef; + $PLANET = undef; + $TEMPLATE = undef; + $TICK = undef; + undef %GROUPS; + $ND::BODY = undef; +} + +1; diff --git a/ND/Include.pm b/ND/Include.pm new file mode 100644 index 0000000..a01a37f --- /dev/null +++ b/ND/Include.pm @@ -0,0 +1,123 @@ +#************************************************************************** +# Copyright (C) 2006 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ + +package ND::Include; +use strict; +use warnings FATAL => 'all'; +require Exporter; + +our @ISA = qw/Exporter/; + +our @EXPORT = qw/isMember isHC isDC isBC isOfficer isScanner isIntel parseMarkup min max listTargets + alliances intelquery/; + +sub isMember { + return exists $ND::GROUPS{Members}; +} + +sub isHC { + return exists $ND::GROUPS{HC}; +} + +sub isDC { + return exists $ND::GROUPS{DC}; +} + +sub isBC { + return exists $ND::GROUPS{BC}; +} + +sub isOfficer { + return exists $ND::GROUPS{Officers}; +} + +sub isScanner { + return exists $ND::GROUPS{Scanners}; +} + +sub isIntel { + return exists $ND::GROUPS{Intel}; +} + +sub parseMarkup { + my ($text) = @_; + + $text =~ s{\n}{\n
}g; + $text =~ s{\[B\](.*?)\[/B\]}{$1}; + return $text; +} + +sub min { + my ($x,$y) = @_; + return ($x > $y ? $y : $x); +} + +sub max { + my ($x,$y) = @_; + return ($x < $y ? $y : $x); +} + +sub listTargets { + my $query = $ND::DBH->prepare(qq{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, released_coords, coords(x,y,z),c.launched,c.wave,c.joinable +FROM raid_claims c + JOIN raid_targets t ON c.target = t.id + JOIN raids r ON t.raid = r.id + JOIN current_planet_stats p ON t.planet = p.id +WHERE c.uid = ? AND r.tick+c.wave > ? AND r.open AND not r.removed +ORDER BY r.tick+c.wave,x,y,z}); + $query->execute($ND::UID,$ND::TICK); + my @targets; + while (my $target = $query->fetchrow_hashref){ + my $coords = "Target $target->{id}"; + $coords = $target->{coords} if $target->{released_coords}; + push @targets,{Coords => $coords, Launched => $target->{launched}, Raid => $target->{raid} + , Target => $target->{id}, Tick => $target->{landingtick}, Wave => $target->{wave} + , AJAX => $ND::AJAX, JoinName => $target->{joinable} ? 'N' : 'J' + , Joinable => $target->{joinable} ? 'FALSE' : 'TRUE'}; + } + my $template = HTML::Template->new(filename => "templates/targetlist.tmpl", cache => 1); + $template->param(Targets => \@targets); + return $template->output; +} + +sub alliances { + my ($alliance) = @_; + my @alliances; + push @alliances,{Id => -1, Name => ' ', Selected => not $alliance}; + my $query = $ND::DBH->prepare(q{SELECT id,name FROM alliances ORDER BY name}); + $query->execute; + while (my $ally = $query->fetchrow_hashref){ + push @alliances,{Id => $ally->{id}, Name => $ally->{name}, Selected => $alliance == $ally->{id}}; + } + return @alliances; +} + +sub intelquery { + my ($columns,$where) = @_; + return qq{ +SELECT $columns, i.mission, i.tick AS landingtick,MIN(i.eta) AS eta, i.amount, i.ingal, u.username +FROM (intel i NATURAL JOIN users u) + JOIN current_planet_stats t ON i.target = t.id + JOIN current_planet_stats o ON i.sender = o.id +WHERE $where +GROUP BY i.tick,i.mission,t.x,t.y,t.z,o.x,o.y,o.z,i.amount,i.ingal,u.username,t.alliance,o.alliance,t.nick,o.nick +ORDER BY i.tick DESC, i.mission}; +} + +1; diff --git a/apache-conf.conf b/apache-conf.conf new file mode 100644 index 0000000..3406de1 --- /dev/null +++ b/apache-conf.conf @@ -0,0 +1,168 @@ +### Section 3: Virtual Hosts +# +# VirtualHost: If you want to maintain multiple domains/hostnames on your +# machine you can setup VirtualHost containers for them. Most configurations +# use only name-based virtual hosts so the server doesn't need to worry about +# IP addresses. This is indicated by the asterisks in the directives below. +# +# Please see the documentation at +# +# for further details before you try to setup virtual hosts. +# +# You may use the command line option '-S' to verify your virtual host +# configuration. + +# +# Use name-based virtual hosting. +# +#NameVirtualHost guranga.org:80 + +# +# VirtualHost example: +# Almost any Apache directive may go into a VirtualHost container. +# The first VirtualHost section is used for requests without a known +# server name. +# +# +# ServerAdmin webmaster@dummy-host.example.com +# DocumentRoot /www/docs/dummy-host.example.com +# ServerName dummy-host.example.com +# ErrorLog @rel_logfiledir@/dummy-host.example.com-error_log +# CustomLog @rel_logfiledir@/dummy-host.example.com-access_log common +# + + +# +# The First Virtual Host is also your DEFAULT Virtual Host. +# This means any requests that do not match any other vhosts will +# goto this virtual host. +# + + + ServerName nd.ruin.nu + ServerAlias webbie.ndawn.com + + RewriteEngine On + RewriteCond %{HTTPS} !=on + RewriteRule ^/(.*) https://%{SERVER_NAME}%{REQUEST_URI} [R] + + + + + ServerName nd.ruin.nu + ServerAlias webbie.ndawn.com + + ErrorLog logs/ndawn_error_log + + #TransferLog logs/ndawn_access_log + CustomLog logs/ndawn_access_log "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" + + + + SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown \ + downgrade-1.0 force-response-1.0 + + # + # DocumentRoot: The directory out of which you will serve your + # documents. By default, all requests are taken from this directory, but + # symbolic links and aliases may be used to point to other locations. + # + DocumentRoot "/var/www/ndawn/htdocs" + + # + # This should be changed to whatever you set DocumentRoot to. + # + + + # + # Possible values for the Options directive are "None", "All", + # or any combination of: + # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews + # + # Note that "MultiViews" must be named *explicitly* --- "Options All" + # doesn't give it to you. + # + # The Options directive is both complicated and important. Please see + # http://httpd.apache.org/docs-2.0/mod/core.html#options + # for more information. + # + Options Indexes FollowSymLinks + + # + # AllowOverride controls what directives may be placed in .htaccess files. + # It can be "All", "None", or any combination of the keywords: + # Options FileInfo AuthConfig Limit + # + AllowOverride None + + + + AuthName "NewDawn authentication" + AuthType basic + + Auth_PG_host localhost + Auth_PG_user ndawn + Auth_PG_pwd Ni7ueYae + Auth_PG_database ndawn + Auth_PG_hash_type MD5 + + Auth_PG_pwd_table users + Auth_PG_uid_field username + Auth_PG_pwd_field password + + Auth_PG_grp_table usersingroup + Auth_PG_grp_group_field groupname + Auth_PG_grp_user_field username + + #Auth_PG_log_table access_log + #Auth_PG_log_uname_field login + #Auth_PG_log_date_field date + #Auth_PG_log_uri_field request + #Auth_PG_log_addrs_field ip_address + + # + # Controls who can get stuff from this server. + # + Order Deny,Allow + Require valid-user + + + + + # Disallow browsing of Subversion working copy administrative dirs. + + Order allow,deny + Deny from all + + + Order allow,deny + Deny from all + + + PerlSwitches -T -I/var/www/ndawn/code/ + PerlRequire /var/www/ndawn/code/startup.pl + + SetHandler perl-script + PerlResponseHandler ND + + + + SetHandler perl-script + PerlResponseHandler Apache2::Status + Order deny,allow + Deny from all + Allow from 127.0.0.1 + Allow from 193.11.248.227 + + + + + # this must match a Processor + ServerEnvironment apache apache + + # these are optional - defaults to the values specified in httpd.conf + MinSpareProcessors 4 + MaxProcessors 20 + + + diff --git a/include.pl b/include.pl deleted file mode 100644 index 76c4541..0000000 --- a/include.pl +++ /dev/null @@ -1,116 +0,0 @@ -#************************************************************************** -# Copyright (C) 2006 by Michael Andreen * -# * -# This program is free software; you can redistribute it and/or modify * -# it under the terms of the GNU General Public License as published by * -# the Free Software Foundation; either version 2 of the License, or * -# (at your option) any later version. * -# * -# This program is distributed in the hope that it will be useful, * -# but WITHOUT ANY WARRANTY; without even the implied warranty of * -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -# GNU General Public License for more details. * -# * -# You should have received a copy of the GNU General Public License * -# along with this program; if not, write to the * -# Free Software Foundation, Inc., * -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * -#**************************************************************************/ - -use strict; - - -sub isMember { - return exists $ND::GROUPS{Members}; -} - -sub isHC { - return exists $ND::GROUPS{HC}; -} - -sub isDC { - return exists $ND::GROUPS{DC}; -} - -sub isBC { - return exists $ND::GROUPS{BC}; -} - -sub isOfficer { - return exists $ND::GROUPS{Officers}; -} - -sub isScanner { - return exists $ND::GROUPS{Scanners}; -} - -sub isIntel { - return exists $ND::GROUPS{Intel}; -} - -sub parseMarkup { - my ($text) = @_; - - $text =~ s{\n}{\n
}g; - $text =~ s{\[B\](.*?)\[/B\]}{$1}; - return $text; -} - -sub min { - my ($x,$y) = @_; - return ($x > $y ? $y : $x); -} - -sub max { - my ($x,$y) = @_; - return ($x < $y ? $y : $x); -} - -sub listTargets { - my $query = $ND::DBH->prepare(qq{SELECT t.id, r.id AS raid, r.tick+c.wave-1 AS landingtick, released_coords, coords(x,y,z),c.launched,c.wave,c.joinable -FROM raid_claims c - JOIN raid_targets t ON c.target = t.id - JOIN raids r ON t.raid = r.id - JOIN current_planet_stats p ON t.planet = p.id -WHERE c.uid = ? AND r.tick+c.wave > ? AND r.open AND not r.removed -ORDER BY r.tick+c.wave,x,y,z}); - $query->execute($ND::UID,$ND::TICK); - my @targets; - while (my $target = $query->fetchrow_hashref){ - my $coords = "Target $target->{id}"; - $coords = $target->{coords} if $target->{released_coords}; - push @targets,{Coords => $coords, Launched => $target->{launched}, Raid => $target->{raid} - , Target => $target->{id}, Tick => $target->{landingtick}, Wave => $target->{wave} - , AJAX => $ND::AJAX, JoinName => $target->{joinable} ? 'N' : 'J' - , Joinable => $target->{joinable} ? 'FALSE' : 'TRUE'}; - } - my $template = HTML::Template->new(filename => "templates/targetlist.tmpl", cache => 1); - $template->param(Targets => \@targets); - return $template->output; -} - -sub alliances { - my ($alliance) = @_; - my @alliances; - push @alliances,{Id => -1, Name => ' ', Selected => not $alliance}; - my $query = $ND::DBH->prepare(q{SELECT id,name FROM alliances ORDER BY name}); - $query->execute; - while (my $ally = $query->fetchrow_hashref){ - push @alliances,{Id => $ally->{id}, Name => $ally->{name}, Selected => $alliance == $ally->{id}}; - } - return @alliances; -} - -sub intelquery { - my ($columns,$where) = @_; - return qq{ -SELECT $columns, i.mission, i.tick AS landingtick,MIN(i.eta) AS eta, i.amount, i.ingal, u.username -FROM (intel i NATURAL JOIN users u) - JOIN current_planet_stats t ON i.target = t.id - JOIN current_planet_stats o ON i.sender = o.id -WHERE $where -GROUP BY i.tick,i.mission,t.x,t.y,t.z,o.x,o.y,o.z,i.amount,i.ingal,u.username,t.alliance,o.alliance,t.nick,o.nick -ORDER BY i.tick DESC, i.mission}; -} - -1; diff --git a/index.pl b/index.pl deleted file mode 100755 index c0ab40f..0000000 --- a/index.pl +++ /dev/null @@ -1,135 +0,0 @@ -#!/usr/bin/perl -w -T -#************************************************************************** -# Copyright (C) 2006 by Michael Andreen * -# * -# This program is free software; you can redistribute it and/or modify * -# it under the terms of the GNU General Public License as published by * -# the Free Software Foundation; either version 2 of the License, or * -# (at your option) any later version. * -# * -# This program is distributed in the hope that it will be useful, * -# but WITHOUT ANY WARRANTY; without even the implied warranty of * -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -# GNU General Public License for more details. * -# * -# You should have received a copy of the GNU General Public License * -# along with this program; if not, write to the * -# Free Software Foundation, Inc., * -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * -#**************************************************************************/ - -package ND; -use CGI qw/:standard/; -use HTML::Template; -use DBI; -use DBD::Pg qw(:pg_types); -#use Apache2::Request; -use strict; - -local $ND::DBH; -local $ND::USER; -local $ND::UID; -local $ND::PLANET; -local $ND::TEMPLATE; -local $ND::BODY; -local $ND::TICK; - -our $DBH = undef; -our $USER = $ENV{'REMOTE_USER'}; -my $error;# = $ND::r->param('page'); - -if ($ENV{'DOCUMENT_ROOT'} =~ m{((\w|/)+)}){ - chdir $1; -} - -our $TEMPLATE = HTML::Template->new(filename => 'templates/skel.tmpl', global_vars => 1, cache => 1); - -for my $file ("db.pl","include.pl"){ - unless (my $return = do $file){ - print "couldn't parse $file: $@" if $@; - print "couldn't do $file: $!" unless defined $return; - print "couldn't run $file" unless $return; - } -} - -our ($UID,$PLANET) = $DBH->selectrow_array('SELECT uid,planet FROM users WHERE username = ?' - ,undef,$ENV{'REMOTE_USER'}); - -our ($TICK) = $DBH->selectrow_array('SELECT tick()',undef); - - -my $query = $DBH->prepare('SELECT groupname,attack,gid from groupmembers NATURAL JOIN groups WHERE uid = ?'); -$query->execute($UID); - -our $ATTACKER = 0; -undef our %GROUPS; -while (my ($name,$attack,$gid) = $query->fetchrow()){ - $GROUPS{$name} = $gid; - $ATTACKER = 1 if $attack; -} - - -our $LOG = $DBH->prepare('INSERT INTO log (uid,text) VALUES(?,?)'); - -my $page = 'main'; -if (param('page') =~ /^(main|check|motd|points|covop|top100|launchConfirmation|addintel|defrequest|raids|editRaid|calls|intel|users|alliances|memberIntel|resources|planetNaps)$/){ - $page = $1; -} - -our $XML = 0; -$XML = 1 if param('xml') and $page =~ /^(raids)$/; - -our $AJAX = 1; - -my $type = 'text/html'; -if ($XML){ - $type = 'text/xml'; - $ND::TEMPLATE = HTML::Template->new(filename => "templates/xml.tmpl", cache => 1); - $ND::BODY = HTML::Template->new(filename => "templates/${page}.xml.tmpl", cache => 1); -}else{ - $ND::BODY = HTML::Template->new(filename => "templates/${page}.tmpl", global_vars => 1, cache => 1); - $ND::BODY->param(PAGE => $page); -} - - -unless (my $return = do "${page}.pl"){ - $error .= "

couldn't parse $page: $@

" if $@; - $error .= "

couldn't do $page: $!

" unless defined $return; - $error .= "

couldn't run $page

" unless $return; -} - -unless ($XML){ - my $fleetupdate = $DBH->selectrow_array('SELECT landing_tick FROM fleets WHERE uid = ? AND fleet = 0',undef,$UID); - - - $TEMPLATE->param(Tick => $TICK); - $TEMPLATE->param(isMember => (($TICK - $fleetupdate < 24) || isScanner()) && $PLANET && isMember()); - $TEMPLATE->param(isHC => isHC()); - $TEMPLATE->param(isDC => isDC()); - $TEMPLATE->param(isBC => isBC()); - $TEMPLATE->param(isIntel => isBC()); - $TEMPLATE->param(isAttacker => $ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))); - if ($ATTACKER && (!isMember() || ((($TICK - $fleetupdate < 24) || isScanner()) && $PLANET))){ - $ND::TEMPLATE->param(Targets => listTargets()); - } - $TEMPLATE->param(Coords => param('coords') ? param('coords') : '1:1:1'); - $TEMPLATE->param(Error => $error); - -} -$ND::TEMPLATE->param(BODY => $ND::BODY->output); -my $output = $TEMPLATE->output; -print header(-type=> $type, -charset => 'utf-8', -Content_Length => length $output); -print $output; - - -$DBH->disconnect; -$DBH = undef; -$UID = undef; -$USER = undef; -$PLANET = undef; -$TEMPLATE = undef; -$TICK = undef; -undef %GROUPS; -$ND::BODY = undef; - -1; diff --git a/startup.pl b/startup.pl new file mode 100644 index 0000000..eb3bfe8 --- /dev/null +++ b/startup.pl @@ -0,0 +1,15 @@ +use lib qw(/var/www/ndawn/code/); + +use POSIX; +use CGI qw/:standard/; +use HTML::Template; + +use Apache::DBI(); +DBI->install_driver("Pg"); +use DBI; +use DBD::Pg qw(:pg_types); +use DB; +use ND::Include; + + +1;