]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Util.pm
Merge branch 'master' of /home/vorner/pciids
[pciids.git] / PciIds / Html / Util.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Html::Util;
20 use strict;
21 use warnings;
22 use HTML::Entities;
23 use base 'Exporter';
24 use PciIds::Users;
25 use Apache2::Const qw(:common :http);
26 use APR::Table;
27
28 our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem &genLocMenu &genCustomHead &genPathBare &protoName &genHtmlFooter);
29
30 sub encode( $ ) {
31         return encode_entities( shift, "\"'&<>" );
32 }
33
34 sub protoName( $ ) {
35         my( $hasSSL ) = ( @_ );
36         return 'http' . ( 's' x $hasSSL );
37 }
38
39 sub genHtmlHead( $$$ ) {
40         my( $req, $caption, $metas ) = @_;
41         $req->content_type( 'text/html; charset=utf-8' );
42         $req->headers_out->add( 'Cache-control' => 'no-cache' );
43         print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'."\n";
44         print '<html lang="en"><head><title>'.encode( $caption )."</title>\n";
45         print "<link rel='stylesheet' type='text/css' media='screen' href='/static/screen.css'>\n";
46         print "<link rel='stylesheet' type='text/css' media='print' href='/static/print.css'>\n";
47         print "<link rel='stylesheet' type='text/css' media='screen,print' href='/static/common.css'>\n";
48         print $metas if( defined( $metas ) );
49         print "</head><body>\n";
50 }
51
52 sub genHtmlTail() {
53         print '</body></html>';
54 }
55
56 sub genHtmlFooter( $$$ ) {
57         my( $jump, $req, $args ) = @_;
58         if( $jump ) {
59                 require PciIds::Html::Jump;
60                 PciIds::Html::Jump::jumpWindow( $req, $args );
61         }
62         print '
63 <div class="footer"><hr>
64 <div class="campaign"><a href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=35833" width="88" height="31" alt="SourceForge"></a></div>
65 <div class="campaign"><a href="http://www.anybrowser.org/campaign/"><img src="/static/anybrowser.png" width=88 height=31 alt="Best viewed with any browser."></a></div>
66 <p class="maintain">
67 Maintained by <a href="http://mj.ucw.cz/">Martin Mares</a> and <a href="http://vorner.pretel.cz/">Michal
68 Vaner</a> with great help by volunteers from the <a href="http://www.sourceforge.net/projects/pciids/">pciids project</a>
69 at <a href="http://www.sourceforge.net/">SourceForge</a>.
70 <p class="maintain">No counters, no frames, no syntax errors.
71 </div>';
72         genHtmlTail();
73 }
74
75 sub htmlDiv( $$ ) {
76         my( $class, $text ) = @_;
77         return '<div class="'.$class.'">'.$text.'</div>';
78 }
79
80 sub item( $$ ) {
81         my( $url, $label ) = @_;
82         print "  <li><a href='".$url."'>$label</a>\n";
83 }
84
85 sub genCustomMenu( $$$$ ) {
86         my( $req, $address, $args, $list ) = @_;
87         my $url;
88         if( defined $address ) {
89                 $url = '/'.$address->get().buildExcept( 'action', $args ).'?action=';
90         } else {
91                 $url = '/?action=';
92         }
93         print "<ul>\n";
94         foreach( @{$list} ) {
95                 my( $label, $action, $param ) = @{$_};
96                 if( $action eq 'jump' ) {
97                         print "<li>\n";
98                         require PciIds::Html::Jump;
99                         PciIds::Html::Jump::jumpWindow( $req, $args );
100                 } else {
101                         my $prefix = '/mods';
102                         $prefix = '/read' if( !defined( $action ) or ( $action eq 'list' ) or ( $action eq '' ) or ( $action eq 'help' ) );
103                         my $suffix = '';
104                         $suffix = '?help='.$param if( $action eq 'help' );
105                         item( $prefix.$url.$action.$suffix, $label );
106                 }
107         }
108         print "</ul>\n";
109 }
110
111 sub logItem( $ ) {
112         my( $auth ) = @_;
113         if( defined( $auth->{'authid'} ) ) {
114                 return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
115         } else {
116                 return [ 'Log in', 'login' ];
117         }
118 }
119
120 sub genMenu( $$$$$ ) {
121         my( $req, $address, $args, $auth, $append ) = @_;
122         my @list;
123         if( defined $address ) {
124                 push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
125                 push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
126         }
127         push @list, [ 'Administer', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
128         push @list, @{$append} if defined $append;
129         if( @list ) {
130                 print "<div class='lmenu'>\n";
131                 genCustomMenu( $req, $address, $args, \@list );
132                 print "</div>\n";
133         }
134         @list = ( logItem( $auth ) );
135         push @list, [ 'Profile', 'profile' ] if defined $auth->{'authid'};
136         push @list, [ 'Notifications', 'notifications' ] if defined $auth->{'authid'};
137         print "<div class='rmenu'>\n";
138         genCustomMenu( $req, $address, $args, \@list );
139         print "</div>\n";
140 }
141
142 sub genTableHead( $$$ ) {
143         my( $class, $captions, $cols ) = @_;
144         print '<table class="'.$class.'">';
145         foreach( @{$cols} ) {
146                 print "<col class='$_'>\n";
147         }
148         print "<tr>\n";
149         foreach( @{$captions} ) {
150                 print '<th>'.$_."\n";
151         }
152 }
153
154 sub genTableTail() {
155         print '</table>';
156 }
157
158 sub parseArgs( $ ) {
159         my %result;
160         foreach( split /\?/, shift ) {
161                 next unless( /=/ );
162                 my( $name, $value ) = /^([^=]+)=(.*)$/;
163                 $result{$name} = $value;
164         }
165         return \%result;
166 }
167
168 sub buildArgs( $ ) {
169         my( $args ) = @_;
170         my $result = '';
171         foreach( keys %{$args} ) {
172                 $result .= "?$_=".$args->{$_} if( defined $args->{$_} );
173         }
174         return $result;
175 }
176
177 sub buildExcept( $$ ) {
178         my( $except, $args ) = @_;
179         my %backup = %{$args};
180         delete $backup{$except};
181         return buildArgs( \%backup );
182 }
183
184 sub setAddrPrefix( $$ ) {
185         my( $addr, $prefix ) = @_;
186         $addr =~ s/\/(mods|read|static)//;
187         return "/$prefix$addr";
188 }
189
190 sub HTTPRedirect( $$ ) {
191         my( $req, $link ) = @_;
192         $req->headers_out->add( 'Location' => $link );
193         return HTTP_SEE_OTHER;
194 }
195
196 sub genPathBare( $$$$$ ) {
197         my( $req, $address, $printAddr, $started, $tables ) = @_;
198         my $path;
199         if( defined $address ) {
200                 $path = $address->path();
201         } else {
202                 $path = [];
203         }
204         foreach my $item ( reverse @{$path} ) {
205                 my( $addr, $exception, $myAddr ) = @{$item};
206                 if( $started ) {
207                         if( $exception ) {
208                                 print ", ";
209                         } else {
210                                 print " -&gt; ";
211                         }
212                 } else {
213                         $started = 1;
214                 }
215                 print "(" if( $exception );
216                 if( !$printAddr && $myAddr ) {
217                         print "<strong>".encode( $addr->pretty() )."</strong>";
218                 } else {
219                         my $title = '';
220                         $title = ' title="'.encode( $tables->itemName( $addr->get() ) ).'"' if defined $tables;
221                         print "<a href='/read/".$addr->get()."'$title>".encode( $addr->pretty() )."</a>";
222                 }
223                 print ")" if( $exception );
224         }
225 }
226
227 sub genPath( $$$ ) {
228         my( $req, $address, $printAddr ) = @_;
229         print "<div class='path'>\n";
230         print "<p><a href='/'>Main</a>";
231         genPathBare( $req, $address, $printAddr, 1, undef );
232         print "</div>\n";
233 }
234
235 sub genLocMenu( $$$$$ ) {
236         my( $req, $args, $addr, $lactions, $ractions ) = @_;
237         print "<div class='lmenu'>\n";
238         genCustomMenu( $req, $addr, $args, $lactions );
239         print "</div>\n<div class='rmenu'>\n";
240         genCustomMenu( $req, $addr, $args, $ractions );
241         print "</div>\n";
242 }
243
244 sub genCustomHead( $$$$$$ ) {
245         my( $req, $args, $addr, $caption, $lactions, $ractions ) = @_;
246         print "<div class='top'>\n";
247         genLocMenu( $req, $args, $addr, $lactions, $ractions );
248         print "<div class='bluesquare'><h1>$caption</h1><p class='home'>The PCI ID Repository</div>\n";
249         print "<div class='clear'></div></div>\n";
250         genPath( $req, $addr, 1 );
251 }
252
253 1;