1 package PciIds::Html::Util;
7 use Apache2::Const qw(:common :http);
10 our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem &genLocMenu);
13 return encode_entities( shift, "\"'&<>" );
16 sub genHtmlHead( $$$ ) {
17 my( $req, $caption, $metas ) = @_;
18 $req->content_type( 'text/html; charset=utf-8' );
19 $req->headers_out->add( 'Cache-control' => 'no-cache' );
20 print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'."\n";
21 print '<html lang="en"><head><title>'.encode( $caption )."</title>\n";
22 print "<link rel='stylesheet' type='text/css' media='screen' href='/static/screen.css'>\n";
23 print "<link rel='stylesheet' type='text/css' media='print' href='/static/print.css'>\n";
24 print $metas if( defined( $metas ) );
25 print "</head><body>\n";
29 print '</body></html>';
33 my( $class, $text ) = @_;
34 return '<div class="'.$class.'">'.$text.'</div>';
38 my( $url, $label ) = @_;
39 print " <li><a href='".$url."'>$label</a>\n";
42 sub genCustomMenu( $$$$ ) {
43 my( $req, $address, $args, $list ) = @_;
45 if( defined $address ) {
46 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
48 $url = '/read/?action=';
50 print "<div class='menu'>\n<ul>\n";
52 my( $label, $action, $param ) = @{$_};
54 $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
56 $suffix = '?help='.$param if( $action eq 'help' );
57 item( 'http://'.$req->hostname().$prefix.$url.$action.$suffix, $label );
59 print "</ul></div>\n";
64 if( defined( $auth->{'authid'} ) ) {
65 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
67 return [ 'Log in', 'login' ];
71 sub genMenu( $$$$$ ) {
72 my( $req, $address, $args, $auth, $help ) = @_;
73 my @list = ( logItem( $auth ) );
74 if( defined $address ) {
75 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
76 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
78 push @list, [ 'Administrate', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
79 push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
80 push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
81 push @list, [ 'Help', 'help', $help ] if defined $help;
82 genCustomMenu( $req, $address, $args, \@list );
85 sub genTableHead( $$$ ) {
86 my( $class, $captions, $cols ) = @_;
87 print '<table class="'.$class.'">';
89 print "<col class='$_'>\n";
92 foreach( @{$captions} ) {
103 foreach( split /\?/, shift ) {
105 my( $name, $value ) = /^([^=]+)=(.*)$/;
106 $result{$name} = $value;
114 $result .= "?$_=".$args->{$_} foreach( keys %{$args} );
118 sub buildExcept( $$ ) {
119 my( $except, $args ) = @_;
120 my %backup = %{$args};
121 delete $backup{$except};
122 return buildArgs( \%backup );
125 sub setAddrPrefix( $$ ) {
126 my( $addr, $prefix ) = @_;
127 $addr =~ s/\/(mods|read|static)//;
128 return "/$prefix$addr";
131 sub HTTPRedirect( $$ ) {
132 my( $req, $link ) = @_;
133 $req->headers_out->add( 'Location' => $link );
134 return HTTP_SEE_OTHER;
138 my( $req, $address, $printAddr ) = @_;
140 if( defined $address ) {
141 $path = $address->path();
142 unshift @{$path}, $address if( $printAddr );
146 print "<div class='navigation'><ul>\n";
147 foreach my $addr ( @{$path} ) {
148 print " <li><a href='http://".$req->hostname()."/read/".$addr->get()."/'>".encode( $addr->pretty() )."</a>\n";
150 print "<li><a href='http://".$req->hostname()."/index.html'>Main page</a>\n";
154 sub genLocMenu( $$$ ) {
155 my( $req, $args, $actions ) = @_;
156 my $addr = PciIds::Address::new( $req->uri() );
157 genCustomMenu( $req, $addr, $args, $actions );
158 genPath( $req, $addr, 1 );