]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Admin.pm
Allow any length queries in admin dump
[pciids.git] / PciIds / Html / Admin.pm
1 package PciIds::Html::Admin;
2 use strict;
3 use warnings;
4 use PciIds::Users;
5 use PciIds::Html::Util;
6 use PciIds::Html::Users;
7 use PciIds::Html::Forms;
8 use PciIds::Notifications;
9 use PciIds::Address;
10 use PciIds::Log;
11 use Apache2::Const qw(:common :http);
12
13 sub safeEncode( $ ) {
14         my( $text ) = @_;
15         return encode( $text ) if defined $text;
16         return '';
17 }
18
19 sub mailEncode( $$ ) {
20         my( $email, $user ) = @_;
21         return '' unless defined $email;
22         ( $user ) = $email =~ /^(.*)@/ unless( defined $user );
23         return "<a href='mailto:$email'>".encode( $user )."</a>";
24 }
25
26 sub genHist( $$$$$$$$$$$ ) {
27         my( $class, $email, $login, $time, $name, $note, $disc, $selname, $selvalue, $delname, $delvalue ) = @_;
28         print "<tr class='$class'><td>";
29         print "<span class='author'>".mailEncode( $email, $login )."<br></span>" if( defined $email );
30         print "<span class='time'>".safeEncode( $time )."</span>";
31         print "<td>";
32         if( defined $name ) {
33                 if( $name eq '' ) {
34                         print "<span class='name'>Deletion request<br></span>";
35                 } else {
36                         print "<span class='name'>Name: ".encode( $name )."<br></span>";
37                 }
38         }
39         print "<span class='note'>Note: ".encode( $note )."<br></span>" if defined $note && $note ne '';
40         print safeEncode( $disc );
41         print "<td class='selects'><input type='radio' name='$selname' 'value='$selvalue'>\n";
42         print "<td class='deletes'><input type='checkbox' name='$delname' value='$delvalue'>\n" if defined $delname
43 }
44
45 sub genNewForm( $ ) {
46         my( $num ) = @_;
47         print "<tr class='newhistory'><td>TODO combo";
48         print "<td><span class='newname'>Name: <input type='text' name='name-$num'></span><span class='newnote'>Note: <input type='note-$num'></span><br>\n";
49         print "<textarea name='disc-$num'></textarea>\n";
50         print "<td><td class='deletes'><input type='checkbox' name='loc-$num-softdel' value='del'>\n";
51 }
52
53 sub genNewAdminForm( $$$$$ ) {
54         my( $req, $args, $tables, $error, $auth ) = @_;
55         my $address = PciIds::Address::new( $req->uri() );
56         my $prefix = $address->get();
57         my $limit = $args->{'limit'};
58         $prefix = '' if( $args->{'global'} );
59         my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
60         genHtmlHead( $req, $caption, undef );
61         my $glob = delete $args->{'global'};
62         genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), $glob ? [ 'Local', 'admin' ] : [ 'Global', 'admin?global=1' ], [ 'Help', 'help', 'admin' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
63         print "<div class='error'>$error</div>\n" if( defined $error );
64         print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
65         my $lastId;
66         my $started = 0;
67         my $cnt = 0;
68         my $hiscnt = 0;
69         my $subcnt;
70         foreach( @{$tables->adminDump( $prefix, $limit )} ) {
71                 my( $locId, $actName, $actNote, $actHist, $actEmail, $actLogin, $actDisc, $actTime,
72                         $hist, $disc, $name, $note, $email, $login, $time ) = @{$_};
73                 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
74                         last if( $hiscnt > ( defined $limit ? $limit : 80 ) );
75                         $lastId = $locId;
76                         if( $started ) {
77                                 genNewForm( $cnt );
78                                 print "</table>\n";
79                         } else {
80                                 $started = 1;
81                         }
82                         my $addr = PciIds::Address::new( $locId );
83                         if( defined( $actHist ) ) {
84                                 print "<table class='item'>\n";
85                         } else {
86                                 print "<table class='unnamedItem'>\n";
87                         }
88                         print "<col class='author'><col class='main'><col class='controls' span='2'>\n";
89                         print "<tr class='label'><p>\n";
90                         print "<td class='path' colspan='2'>";
91                         genPathBare( $req, $addr, 0, 0, 0 );
92                         print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
93                         $subcnt = 0;
94                         $cnt ++;
95                         print "<td class='selects'><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
96                         print "<td class='deletes'><input type='checkbox' name='loc-$cnt-del' value='del'>" if hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) );
97                         genHist( 'main-history', $actEmail, $actLogin, $actTime, $actName, $actNote, $actDisc, "loc-$cnt-sel", 'seen', undef, undef ) if( defined $actHist );
98                 }
99                 $hiscnt ++;
100                 $subcnt ++;
101                 genHist( 'unseen-history', $email, $login, $time, $name, $note, $disc, "loc-$cnt-sel", $hist, "del-$hiscnt", "del-$hist" );
102         }
103         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
104         if( $started ) {
105                 genNewForm( $cnt );
106                 print "</table>\n";
107                 print "<p><input type='submit' name='submit' value='Submit'>\n";
108                 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
109                 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
110         } else {
111                 print "<p>No pending items.\n";
112         }
113         print "</form>\n";
114         genHtmlTail();
115         return OK;
116 }
117
118 sub adminForm( $$$$ ) {
119         my( $req, $args, $tables, $auth ) = @_;
120         if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
121                 return genNewAdminForm( $req, $args, $tables, undef, $auth );
122         } else {
123                 return notLoggedComplaint( $req, $args, $auth );
124         }
125 }
126
127 my $errors;
128
129 sub appendError( $ ) {
130         if( $errors eq '' ) {
131                 $errors = "<p>".shift;
132         } else {
133                 $errors .= "<br>".shift;
134         }
135 }
136
137 sub submitAdminForm( $$$$ ) {
138         my( $req, $args, $tables, $auth ) = @_;
139         my $authid = $auth->{'authid'};
140         if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
141                 my( %deleted, %approved );
142                 my $maxcnt = getFormValue( 'max-cnt', 0 );
143                 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
144                 $errors = '';
145                 # Scan for approved and deleted items
146                 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
147                         my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/;
148                         $deleted{$del} = 1 if( defined $del && $del ne '' );
149                         my( $appr ) = getFormValue( "appr-$i", '' ) =~ /^appr-(\d+)$/;
150                         $approved{$appr} = 1 if( defined $appr && $appr ne '' );
151                 }
152                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
153                         my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
154                         $approved{$sel} = 1 if( defined $sel && $sel ne '' );
155                 }
156                 # Check for collisions
157                 my %collision;
158                 foreach my $id ( keys %deleted ) {
159                         if( $approved{$id} ) {
160                                 my $owner = getFormValue( "owner-$id", '' );
161                                 appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() );
162                                 $collision{$owner} = $_;
163                                 delete $deleted{$id};
164                                 delete $approved{$id};
165                         }
166                 }
167                 #Do the deletes and approves
168                 foreach my $del ( keys %deleted ) {
169                         $tables->deleteHistory( $del );
170                         tulog( $authid, "Discussion deleted $del" );
171                 }
172                 foreach my $appr ( keys %approved ) {
173                         $tables->markChecked( $appr );
174                         tulog( $authid, "Discussion checked $appr" );
175                 }
176                 #Handle the items
177                 my $defaultSeen = getFormValue( 'default-seen', '' ) =~ /^default-seen$/;
178                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
179                         my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
180                         next if $collision{$addr->get()};
181                         next unless defined $addr;
182                         my $del = getFormValue( "loc-$i-del", '' );
183                         if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) {
184                                 $tables->deleteItem( $addr->get() );
185                                 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
186                                 next;
187                         }
188                         my $name = getFormValue( "name-$i", undef );
189                         $name = undef if defined $name && $name eq '';
190                         my $note = getFormValue( "note-$i", undef );
191                         $note = undef if defined $note && $note eq '';
192                         my $discussion = getFormValue( "disc-$i", '' );
193                         $discussion = undef if defined $discussion && $discussion eq '';
194                         my $delete = 0;
195                         if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
196                                 $delete = 1;
197                                 $name = undef;
198                                 $note = undef;
199                         }
200                         if( defined $note && !defined $name ) {
201                                 appendError( "You must specify name if you set note at item ".$addr->pretty() );
202                                 next;
203                         }
204                         my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
205                         my $action = 0;
206                         if( defined $name || defined $discussion || $delete ) {
207                                 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr );
208                                 $tables->markChecked( $histId );
209                                 $select = $histId if defined $name || $delete;
210                                 tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
211                                 $action = 1;
212                                 notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
213                         }
214                         if( defined $select && select ne '' ) {
215                                 $tables->setMainHistory( $addr->get(), $select );
216                                 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
217                                 $action = 1;
218                                 notify( $tables, $addr->get(), $select, 2, 2 );
219                         }
220                         if( $action && $defaultSeen ) {#Approve anything in this item
221                                 my $subcnt = getFormValue( "loc-$i-subcnt", 0 );
222                                 for( my $j = 1;  $j <= $subcnt; $j ++ ) {
223                                         my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/;
224                                         next unless defined $id;
225                                         next if $approved{$id} || $deleted{$id};
226                                         $tables->markChecked( $id );
227                                         tulog( $authid, "Discussion checked $id" );
228                                 }
229                         }
230                 }
231                 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
232         } else {
233                 return notLoggedComplaint( $req, $args, $auth );
234         }
235 }
236
237 1;