]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Admin.pm
Merge branch 'master' of /home/vorner/pciids
[pciids.git] / PciIds / Html / Admin.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::Admin;
20 use strict;
21 use warnings;
22 use PciIds::Users;
23 use PciIds::Html::Util;
24 use PciIds::Html::Users;
25 use PciIds::Html::Forms;
26 use PciIds::Notifications;
27 use PciIds::Address;
28 use PciIds::Log;
29 use PciIds::Startup;
30 use Apache2::Const qw(:common :http);
31
32 sub safeEncode( $ ) {
33         my( $text ) = @_;
34         return encode( $text ) if defined $text;
35         return '';
36 }
37
38 sub mailEncode( $$ ) {
39         my( $email, $user ) = @_;
40         return '' unless defined $email;
41         ( $user ) = $email =~ /^(.*)@/ unless( defined $user );
42         return "<a href='mailto:$email'>".encode( $user )."</a>";
43 }
44
45 sub genHist( $$$$$$$$$$$ ) {
46         my( $class, $email, $login, $time, $name, $note, $disc, $selname, $selvalue, $delname, $delvalue ) = @_;
47         print "<tr class='$class'><td>";
48         print "<span class='author'>".mailEncode( $email, $login )."<br></span>" if( defined $email );
49         print "<span class='time'>".safeEncode( $time )."</span>";
50         print "<td>";
51         if( defined $name ) {
52                 if( $name eq '' ) {
53                         print "<span class='name'>Deletion request<br></span>";
54                 } else {
55                         print "<span class='name'>Name: ".encode( $name )."<br></span>";
56                 }
57         }
58         print "<span class='note'>Note: ".encode( $note )."<br></span>" if defined $note && $note ne '';
59         print safeEncode( $disc );
60         print "<td class='selects'><input type='radio' name='$selname' value='$selvalue'>\n";
61         print "<td class='deletes'><input type='checkbox' name='$delname' value='$delvalue'>\n" if defined $delname
62 }
63
64 sub genNewForm( $$ ) {
65         my( $num, $values ) = @_;
66         print "<tr class='newhistory'><td>";
67         if( @{$values} ) {
68                 print "<select id='ans-$num' name='ans-$num' onchange='answer( \"$num\" );'><option value='0'>Stock answers</option>";
69                 my $i = 0;
70                 foreach( @{$values} ) {
71                         $i ++;
72                         my( $name ) = @{$_};
73                         print "<option value='$i'>$name</option>";
74                 }
75                 print "</select>\n";
76         } else {
77                 print "No stock answers";
78         }
79         print "<td><span class='newname'>Name: <input type='text' name='name-$num'></span><span class='newnote'>Note: <input type='text' name='note-$num'></span><br>\n";
80         print "<textarea id='disc-$num' name='disc-$num'></textarea>\n";
81         print "<td><td class='deletes'><input type='checkbox' name='loc-$num-softdel' value='del'>\n";
82 }
83
84 sub loadStock() {
85         my @stock;
86         if( open ANS, "$directory/cf/answers" ) {
87                 my( $name, $text, $lines );
88                 while( defined( $name = <ANS> ) ) {
89                         $lines = '';
90                         while( defined( $text = <ANS> ) ) {
91                                 chomp $text;
92                                 last if $text eq '';
93                                 $lines .= "\n" if $lines ne '';
94                                 $lines .= $text;
95                         }
96                         push @stock, [ encode( $name ), $lines ];
97                 }
98                 close ANS;
99         } else {
100                 print STDERR "Could not find stock answers.\n";
101         }
102         return \@stock;
103 }
104
105 sub genNewAdminForm( $$$$$ ) {
106         my( $req, $args, $tables, $error, $auth ) = @_;
107         my $address = PciIds::Address::new( $req->uri() );
108         my $prefix = $address->get();
109         my $limit = delete $args->{'limit'};
110         $prefix = '' if( $args->{'global'} );
111         my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
112         genHtmlHead( $req, $caption, undef );
113         my $glob = delete $args->{'global'};
114         my $moreButt = 'admin?limit='.( $limit ? int( $limit * 2 ) : 160 ).buildExcept( 'action', $args );
115         $moreButt .= '?global='.$glob if defined $glob;
116         genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), $glob ? [ 'Local', 'admin'.buildExcept( 'action', $args ) ] : [ 'Global', 'admin?global=1'.buildExcept( 'action', $args ) ], [ 'More items', $moreButt ], [ 'Help', 'help', 'admin' ] ], [ [ 'Log out', 'logout' ] ] );
117         print "<div class='error'>$error</div>\n" if( defined $error );
118         print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
119         my $lastId;
120         my $started = 0;
121         my $cnt = 0;
122         my $hiscnt = 0;
123         my $subcnt;
124         my $stock = loadStock();
125         print "<input type='hidden' name='jscript-active' id='jscript-active' value='0'>";#Trick to find out if user has JScript
126         print "<script type='text/javascript'><!--\n document.getElementById(\"jscript-active\").value = \"1\"; \n";
127         if( @{$stock} ) {
128                 print "function answers() {\nreturn new Array( \"\" ";
129                 foreach( @{$stock} ) {
130                         my( $x, $text ) = @{$_};
131                         $text =~ s/"/\\"/g;
132                         $text =~ s/\n/\\n/g;
133                         print ', "'.$text.'"';
134                 }
135                 print ");\n}\n";
136         }
137         print '
138 function answer( id ) {
139         var ans, index;
140         ans = answers();
141         index = document.getElementById( "ans-" + id ).value;
142         document.getElementById( "disc-" + id ).value = ans[index];
143 }
144 ';
145         print "// --></script>";
146         foreach( @{$tables->adminDump( $prefix, $limit )} ) {
147                 my( $locId, $actName, $actNote, $actHist, $actEmail, $actLogin, $actDisc, $actTime,
148                         $hist, $disc, $name, $note, $email, $login, $time ) = @{$_};
149                 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
150                         last if( $hiscnt > ( defined $limit ? $limit : 80 ) );
151                         $lastId = $locId;
152                         if( $started ) {
153                                 genNewForm( $cnt, $stock );
154                                 print "</table>\n";
155                         } else {
156                                 $started = 1;
157                         }
158                         my $addr = PciIds::Address::new( $locId );
159                         if( defined( $actHist ) ) {
160                                 print "<table class='item'>\n";
161                         } else {
162                                 print "<table class='unnamedItem'>\n";
163                         }
164                         print "<col class='author'><col class='main'><col class='controls' span='2'>\n";
165                         print "<tr class='label'><p>\n";
166                         print "<td class='path' colspan='2'>";
167                         genPathBare( $req, $addr, 1, 0 );
168                         print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
169                         $subcnt = 0;
170                         $cnt ++;
171                         print "<td class='selects'><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
172                         print "<td class='deletes'><input type='checkbox' name='loc-$cnt-del' value='del'>" if hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) );
173                         print "<input type='hidden' name='loc-$cnt' value='$locId'>";
174                         if( defined $actHist ) {
175                                 genHist( 'main-history', $actEmail, $actLogin, $actTime, $actName, $actNote, $actDisc, "loc-$cnt-sel", 'seen', undef, undef );
176                         } else {
177                                 print "<tr class='main-history'><td class='empty' colspan='2'><td><input type='radio' name='loc-$cnt-sel' value='seen'>";
178                         }
179                 }
180                 $hiscnt ++;
181                 $subcnt ++;
182                 genHist( 'unseen-history', $email, $login, $time, $name, $note, $disc, "loc-$cnt-sel", $hist, "del-$hiscnt", "del-$hist" );
183                 print "<input type='hidden' name='owner-$hist' value='$locId'>";
184                 print "<input type='hidden' name='his-$cnt-$subcnt' value='$hist'>";
185         }
186         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
187         if( $started ) {
188                 genNewForm( $cnt, $stock );
189                 print "</table>\n";
190                 print "<p><input type='submit' name='submit' value='Submit'>\n";
191                 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
192                 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
193         } else {
194                 print "<p>No pending items.\n";
195         }
196         print "</form>\n";
197         genHtmlFooter( 1, $req, $args );
198         return OK;
199 }
200
201 sub adminForm( $$$$ ) {
202         my( $req, $args, $tables, $auth ) = @_;
203         if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
204                 return genNewAdminForm( $req, $args, $tables, undef, $auth );
205         } else {
206                 return notLoggedComplaint( $req, $args, $auth );
207         }
208 }
209
210 my $errors;
211
212 sub appendError( $ ) {
213         if( $errors eq '' ) {
214                 $errors = "<p>".shift;
215         } else {
216                 $errors .= "<br>".shift;
217         }
218 }
219
220 sub submitAdminForm( $$$$ ) {
221         my( $req, $args, $tables, $auth ) = @_;
222         my $authid = $auth->{'authid'};
223         if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
224                 my( %deleted, %approved );
225                 my $hasJscript = getFormValue( 'jscript-active', '0' );
226                 my $ans;
227                 $ans = loadStock() unless $hasJscript;#If there is jscript, no need to translate
228                 my $maxcnt = getFormValue( 'max-cnt', 0 );
229                 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
230                 $errors = '';
231                 # Scan for deleted histories
232                 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
233                         my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/;
234                         $deleted{$del} = 1 if( defined $del && $del ne '' );
235                 }
236                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
237                         my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
238                         $approved{$sel} = 1 if( defined $sel && $sel ne '' );
239                 }
240                 # Check for collisions
241                 my %collision;
242                 foreach my $id ( keys %deleted ) {
243                         if( $approved{$id} ) {
244                                 my $owner = getFormValue( "owner-$id", '' );
245                                 appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() );
246                                 $collision{$owner} = $_;
247                                 delete $deleted{$id};
248                                 delete $approved{$id};
249                         }
250                 }
251                 #Do the deletes
252                 my %modified;
253                 foreach my $del ( keys %deleted ) {
254                         $tables->deleteHistory( $del );
255                         $modified{getFormValue( "owner-$del", '' )} = 1;
256                         tulog( $authid, "Discussion deleted $del" );
257                 }
258                 #Handle the items
259                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
260                         my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
261                         next if $collision{$addr->get()};
262                         next unless defined $addr;
263                         my $del = getFormValue( "loc-$i-del", '' );
264                         if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) {
265                                 $tables->deleteItem( $addr->get() );
266                                 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
267                                 next;
268                         }
269                         my $name = getFormValue( "name-$i", undef );
270                         $name = undef if defined $name && $name eq '';
271                         my $note = getFormValue( "note-$i", undef );
272                         $note = undef if defined $note && $note eq '';
273                         my $discussion = getFormValue( "disc-$i", '' );
274                         unless( $hasJscript ) {#Modified by the stock answers, is no jscript at user
275                                 my $index = getFormValue( "ans-$i", '0' );
276                                 $discussion = $ans->[$index-1]->[1] if $index;
277                         }
278                         $discussion = undef if defined $discussion && $discussion eq '';
279                         my $delete = 0;
280                         if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
281                                 $delete = 1;
282                                 $name = undef;
283                                 $note = undef;
284                         }
285                         if( defined $note && !defined $name ) {
286                                 appendError( "You must specify name if you set note at item ".$addr->pretty() );
287                                 next;
288                         }
289                         my $action = $modified{$addr->get()};
290                         $action = 1 if getFormValue( "loc-$i-sel", '' ) eq 'seen';
291                         my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
292                         if( defined $name || defined $discussion || $delete ) {
293                                 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr );
294                                 $tables->markChecked( $histId );
295                                 $select = $histId if defined $name || $delete;
296                                 tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
297                                 $action = 1;
298                                 notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
299                         }
300                         if( defined $select && select ne '' ) {
301                                 $tables->setMainHistory( $addr->get(), $select );
302                                 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
303                                 $action = 1;
304                                 notify( $tables, $addr->get(), $select, 2, 2 );
305                         }
306                         if( $action ) {#Approve anything in this item
307                                 my $subcnt = getFormValue( "loc-$i-subcnt", 0 );
308                                 for( my $j = 1;  $j <= $subcnt; $j ++ ) {
309                                         my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/;
310                                         next unless defined $id;
311                                         next if $deleted{$id};
312                                         $tables->markChecked( $id );
313                                         tulog( $authid, "Discussion checked $id" );
314                                 }
315                         }
316                 }
317                 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
318         } else {
319                 return notLoggedComplaint( $req, $args, $auth );
320         }
321 }
322
323 1;