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