genLocMenu( $req, $args, [ logItem( $auth ), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'newitem' ], [ 'ID syntax', 'help', $address->helpName() ] ] );
print "<div class='clear'></div></div>\n";
print "<div class='error'>$error</div>\n" if( defined $error );
- print "<form name='newitem' id='newitem' method='POST' action=''>\n<table>";
+ print "<form name='newitem' id='newitem' method='POST' action='".( $args->{'full_links'} ? 'http://'.$req->hostname().$req->uri().buildExcept( 'action', $args ).'?action=newitem' : '' )."'>\n<table>";
genFormEx( [ [ 'input', 'Id:', 'text', 'id', 'maxlength="50"' ],
[ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
[ 'input', 'Note*:', 'text', 'note', 'maxlength="1024"' ],
genLocMenu( $req, $args, [ logItem( $auth ), $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'newhistory' ] ] );
print "<div class='clear'></div></div>\n";
print "<div class='error'>$error</div>\n" if( defined $error );
- print "<form name='newhistory' id='newhistory' method='POST' action=''>\n<table>";
+ print "<form name='newhistory' id='newhistory' method='POST' action='".( $args->{'full_links'} ? 'http://'.$req->hostname().$req->uri().buildExcept( 'action', $args ).'?action=newhistory' : '' )."'>\n<table>";
genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ],
[ 'input', 'Request deletion', 'checkbox', 'delete', 'value="delete"' ],
[ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
use PciIds::Html::Notifications;
use PciIds::Html::Help;
use Apache2::Const qw(:common :http);
+use base 'Exporter';
+
+our @EXPORT = qw(&callHandler);
$ENV{'PATH'} = '';
my $dbh = connectDb();
}
);
-sub handler( $$ ) {
- my( $req, $hasSSL ) = @_;
- my $args = parseArgs( $req->args() );
- return HTTPRedirect( $req, $req->uri()."index.html" ) if( $req->uri() eq '/' && ( !defined $args->{'action'} || $args->{'action'} ne 'help' ) );
- return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ );
+sub callHandler( $$$$$$ ) {
+ my( $req, $args, $tables, $auth, $hasSSL, $meth ) = @_;
my $action = $args->{'action'};
$action = '' unless( defined $action );
- my $method = $handlers{$req->method()};
+ my $method = $handlers{$meth};
return HTTP_METHOD_NOT_ALLOWED unless( defined $method );#Can't handle this method
my $sub = $method->{$action};
return HTTP_BAD_REQUEST unless( defined $sub );#I do not know this action for given method
- my $auth = checkLogin( $req, $tables );#Check if logged in
+ $auth = checkLogin( $req, $tables ) unless defined $auth;#Check if logged in
$auth->{'ssl'} = $hasSSL;
- my( $result );
+ return &{$sub}( $req, $args, $tables, $auth );#Just do the right thing
+}
+
+sub handler( $$ ) {
+ my( $req, $hasSSL ) = @_;
+ my $args = parseArgs( $req->args() );
+ return HTTPRedirect( $req, $req->uri()."index.html" ) if( $req->uri() eq '/' && ( !defined $args->{'action'} || $args->{'action'} ne 'help' ) );
+ return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ );
+ my $result;
eval {
- $result = &{$sub}( $req, $args, $tables, $auth );#Just do the right thing
+ $result = callHandler( $req, $args, $tables, undef, $hasSSL, $req->method() );
$tables->commit();
};
if( $@ ) {
print "</div>\n";
}
print "</div>\n" if( $diss );
+ my $url_prefix = $args->{'full_links'} ? 'http://'.$req->hostname() : '';
unless( $address->leaf() ) {
print "<h2>Sub-items</h2>\n";
my $restricts = $address->defaultRestrictList();
print "<p>";
my $url = '/read/'.$address->get().buildExcept( 'restrict', $args ).'?restrict=';
foreach( @{$restricts} ) {
- print "<a href='".$url.$_->[0]."'>".$_->[1]."</a> ";
+ print "<a href='".$url_prefix.$url.$_->[0]."'>".$_->[1]."</a> ";
}
}
my $url = '/read/'.$address->get().buildExcept( 'sort', $args );
my $sort = ( $args->{'sort'} or 'id' );
my( $sort_id, $sort_name ) = ( ( $sort eq 'id' ? 'rid' : 'id' ), ( $sort eq 'name' ? 'rname' : 'name' ) );
- genTableHead( 'subnodes', [ '<a href="'.$url.'?sort='.$sort_id.'">Id</a>', '<a href="'.$url.'?sort='.$sort_name.'">Name</a>', 'Note' ], [ 'id-col', 'name-col', 'note-col' ] );
+ genTableHead( 'subnodes', [ '<a href="'.$url_prefix.$url.'?sort='.$sort_id.'">Id</a>', '<a href="'.$url_prefix.$url.'?sort='.$sort_name.'">Name</a>', 'Note' ], [ 'id-col', 'name-col', 'note-col' ] );
$args->{'restrict'} = $address->defaultRestrict() unless( defined( $args->{'restrict'} ) );
- $tables->nodes( $address->get(), $args );
+ $tables->nodes( $address->get(), $args, $url_prefix );
genTableTail();
}
genHtmlTail();
return bless PciIds::DBQ::new( $dbh );
}
-sub formatLink( $ ) {
+sub formatLink( $$ ) {
my $address = PciIds::Address::new( shift );
- return '<a href="/read/'.$address->get().'">'.$address->tail().'</a>';
+ my $url_prefix = shift;
+ return '<a href="'.$url_prefix.'/read/'.$address->get().'">'.$address->tail().'</a>';
}
-sub nodes( $$$ ) {
- my( $self, $parent, $args ) = @_;
+sub nodes( $$$$ ) {
+ my( $self, $parent, $args, $url_prefix ) = @_;
my $restrict = $args->{'restrict'};
$restrict = '' unless( defined $restrict );
$restrict = PciIds::Address::new( $parent )->restrictRex( $restrict );#How do I know if the restrict is OK?
- htmlFormatTable( PciIds::DBQ::nodes( $self, $parent, $args, $restrict ), 3, [], [ \&formatLink ], sub { 1; }, sub {
+ htmlFormatTable( PciIds::DBQ::nodes( $self, $parent, $args, $restrict ), 3, [], [ sub {
+ return formatLink( shift, $url_prefix );
+ }, ], sub { 1; }, sub {
my $name = shift->[ 1 ];
return ' class="'.( defined $name && $name ne '' ? 'item' : 'unnamedItem' ).'"';
} );
use PciIds::Email;
use PciIds::Users;
use PciIds::Address;
+use PciIds::Html::Handler;
use CGI;
use CGI::Cookie;
use Apache2::Const qw(:common);
$logged = $salted eq $passwd;
}
if( $logged ) {
- $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) ) );
+ my $cookie = new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) );
+ $req->headers_out->add( 'Set-Cookie' => $cookie );
$args->{'action'} = ( defined $args->{'redirectaction'} ) ? $args->{'redirectaction'} : 'list';
- my $prefix = ( !defined( $args->{'action'} ) or ( $args->{'action'} eq '' ) or ( $args->{'action'} eq 'list' ) ) ? 'read' : 'mods';
- my $url = "http://".$req->hostname().setAddrPrefix( $req->uri(), $prefix ).buildExcept( 'redirectaction', $args );
- genHtmlHead( $req, 'Logged in', undef );
- print "<div class='top'>\n";
- print '<h1>Logged in</h1>';
- genPath( $req, PciIds::Address::new( $req->uri() ), 1 );
- print "<div class='clear'></div></div>\n";
- print "<p>You are logged in" . ( defined $args->{'redirectaction'} ? ", continue with your <a href='$url'>action</a>.\n" : ".\n" );
- print '<div class="lastlog"><p>'.encode( $last ).'</div>' if( defined( $last ) );
- genHtmlTail();
- return OK;
+ delete $args->{'redirectaction'};
+ $args->{'full_links'} = 1;
+ my $auth = checkLoginInternal( $req, $tables, $cookie );
+ return PciIds::Html::Handler::callHandler( $req, $args, $tables, $auth, 1, 'GET' );
} else {
return genLoginForm( $req, $args, 'Invalid login credetials', $data );
}
return PciIds::Html::List::list( $req, $args, $tables, {} );
}
-sub checkLogin( $$ ) {
- my( $req, $tables ) = @_;
- my $cookies = fetch CGI::Cookie;
- my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookies->{'auth'} ) ? $cookies->{'auth'}->value : undef );
+sub checkLoginInternal( $$$ ) {
+ my( $req, $tables, $cookie ) = @_;
+ my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookie ) ? $cookie->value : undef );
if( $regen ) {
$req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights, $name ) ) );
}
return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror, 'name' => $authed ? $name : undef };
}
+sub checkLogin( $$ ) {
+ my( $req, $tables ) = @_;
+ my $cookies = fetch CGI::Cookie;
+ my $cookie = $cookies->{'auth'};
+ return checkLoginInternal( $req, $tables, $cookie );
+}
+
sub notLoggedComplaint( $$$ ) {
my( $req, $args, $auth ) = @_;
return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
my( $except, $args ) = @_;
my %backup = %{$args};
delete $backup{$except};
+ delete $backup{'full_links'};#This one is internal
return buildArgs( \%backup );
}