]> ruin.nu Git - ndwebbie.git/commitdiff
use Tie::File instead of hardcoded regex for valid pages
authorMichael Andreen <harv@ruin.nu>
Fri, 29 Dec 2006 13:02:28 +0000 (13:02 +0000)
committerMichael Andreen <harv@ruin.nu>
Fri, 29 Dec 2006 13:02:28 +0000 (13:02 +0000)
ND.pm
pages [new file with mode: 0644]
startup.pl

diff --git a/ND.pm b/ND.pm
index c2bb6c0dc3ea39989602a4add5e6de8fee143e45..d62a7ea89537bc94d3f6f16c3af452c9624ba26a 100755 (executable)
--- a/ND.pm
+++ b/ND.pm
@@ -26,9 +26,13 @@ use DBD::Pg qw(:pg_types);
 use Apache2::Request;
 use ND::Include;
 use ND::DB;
+use Tie::File;
+use Fcntl 'O_RDONLY';
 use strict;
 use warnings FATAL => 'all';
 
+tie our @PAGES, 'Tie::File', "/var/www/ndawn/code/pages", mode => O_RDONLY or die $!;
+
 sub handler {
        local $ND::r = shift;
        local $ND::req = Apache2::Request->new($ND::r, POST_MAX => "1M");
@@ -77,13 +81,10 @@ sub page {
 
        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;
-       }
+       $ND::PAGE = 'main' unless grep { /^$ND::PAGE$/ } @PAGES;
 
        our $XML = 0;
-       $XML = 1 if param('xml') and $page =~ /^(raids)$/;
+       $XML = 1 if param('xml') and $ND::PAGE =~ /^(raids)$/;
 
        our $AJAX = 1;
 
@@ -91,17 +92,17 @@ sub page {
        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);
+               $ND::BODY = HTML::Template->new(filename => "templates/$ND::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);
+               $ND::BODY = HTML::Template->new(filename => "templates/$ND::PAGE.tmpl", global_vars => 1, cache => 1);
+               $ND::BODY->param(PAGE => $ND::PAGE);
        }
 
 
-       unless (my $return = do "${page}.pl"){
-               $error .= "<p><b>couldn't parse $page: $@</b></p>" if $@;
-               $error .= "<p><b>couldn't do $page: $!</b></p>"    unless defined $return;
-               $error .= "<p><b>couldn't run $page</b></p>"       unless $return;
+       unless (my $return = do "$ND::PAGE.pl"){
+               $error .= "<p><b>couldn't parse $ND::page: $@</b></p>" if $@;
+               $error .= "<p><b>couldn't do $ND::page: $!</b></p>"    unless defined $return;
+               $error .= "<p><b>couldn't run $ND::page</b></p>"       unless $return;
        }
 
        unless ($XML){
diff --git a/pages b/pages
new file mode 100644 (file)
index 0000000..13b0138
--- /dev/null
+++ b/pages
@@ -0,0 +1,18 @@
+main
+check
+motd
+points
+covop
+top100
+launchConfirmation
+addintel
+defrequest
+raids
+editRaid
+calls
+intel
+users
+alliances
+memberIntel
+resources
+planetNaps
index eb3bfe877a54871b705af20547f7ef193c2b323c..cf9b74862457d864b01f405e659d1ded1ca9ec43 100644 (file)
@@ -1,6 +1,7 @@
 use lib qw(/var/www/ndawn/code/);
 
 use POSIX;
+
 use CGI qw/:standard/;
 use HTML::Template;
 
@@ -8,8 +9,11 @@ use Apache::DBI();
 DBI->install_driver("Pg");
 use DBI;
 use DBD::Pg qw(:pg_types);
+
 use DB;
 use ND::Include;
 
+use Tie::File;
+
 
 1;