1 package PciIds::Html::Admin;
5 use PciIds::Html::Util;
6 use PciIds::Html::Users;
7 use PciIds::Html::Forms;
8 use PciIds::Notifications;
12 use Apache2::Const qw(:common :http);
16 return encode( $text ) if defined $text;
20 sub mailEncode( $$ ) {
21 my( $email, $user ) = @_;
22 return '' unless defined $email;
23 ( $user ) = $email =~ /^(.*)@/ unless( defined $user );
24 return "<a href='mailto:$email'>".encode( $user )."</a>";
27 sub genHist( $$$$$$$$$$$ ) {
28 my( $class, $email, $login, $time, $name, $note, $disc, $selname, $selvalue, $delname, $delvalue ) = @_;
29 print "<tr class='$class'><td>";
30 print "<span class='author'>".mailEncode( $email, $login )."<br></span>" if( defined $email );
31 print "<span class='time'>".safeEncode( $time )."</span>";
35 print "<span class='name'>Deletion request<br></span>";
37 print "<span class='name'>Name: ".encode( $name )."<br></span>";
40 print "<span class='note'>Note: ".encode( $note )."<br></span>" if defined $note && $note ne '';
41 print safeEncode( $disc );
42 print "<td class='selects'><input type='radio' name='$selname' value='$selvalue'>\n";
43 print "<td class='deletes'><input type='checkbox' name='$delname' value='$delvalue'>\n" if defined $delname
46 sub genNewForm( $$ ) {
47 my( $num, $values ) = @_;
48 print "<tr class='newhistory'><td>";
50 print "<select id='ans-$num' name='ans-$num' onchange='answer( \"$num\" );'><option value='0'>Stock answers</option>";
52 foreach( @{$values} ) {
55 print "<option value='$i'>$name</option>";
59 print "No stock answers";
61 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";
62 print "<textarea id='disc-$num' name='disc-$num'></textarea>\n";
63 print "<td><td class='deletes'><input type='checkbox' name='loc-$num-softdel' value='del'>\n";
68 if( open ANS, "$directory/answers" ) {
69 my( $name, $text, $lines );
70 while( defined( $name = <ANS> ) ) {
72 while( defined( $text = <ANS> ) ) {
75 $lines .= '<br>' if $lines ne '';
76 $lines .= encode( $text );
78 push @stock, [ encode( $name ), $lines ];
82 print STDERR "Could not find stock answers.\n";
87 sub genNewAdminForm( $$$$$ ) {
88 my( $req, $args, $tables, $error, $auth ) = @_;
89 my $address = PciIds::Address::new( $req->uri() );
90 my $prefix = $address->get();
91 my $limit = delete $args->{'limit'};
92 $prefix = '' if( $args->{'global'} );
93 my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
94 genHtmlHead( $req, $caption, undef );
95 my $glob = delete $args->{'global'};
96 my $moreButt = 'admin?limit='.( $limit ? int( $limit * 2 ) : 160 ).buildExcept( 'action', $args );
97 $moreButt .= '?global='.$glob if defined $glob;
98 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' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
99 print "<div class='error'>$error</div>\n" if( defined $error );
100 print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
106 my $stock = loadStock();
107 print "<input type='hidden' name='jscript-active' id='jscript-active' value='0'>";#Trick to find out if user has JScript
108 print "<script type='text/javascript'><!--\n document.getElementById(\"jscript-active\").value = \"1\"; \n";
110 print "function answers() {\nreturn new Array( \"\" ";
111 foreach( @{$stock} ) {
112 my( $x, $text ) = @{$_};
113 print ', "'.$text.'"';
118 function answer( id ) {
121 index = document.getElementById( "ans-" + id ).value;
122 document.getElementById( "disc-" + id ).value = ans[index];
125 print "// --></script>";
126 foreach( @{$tables->adminDump( $prefix, $limit )} ) {
127 my( $locId, $actName, $actNote, $actHist, $actEmail, $actLogin, $actDisc, $actTime,
128 $hist, $disc, $name, $note, $email, $login, $time ) = @{$_};
129 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
130 last if( $hiscnt > ( defined $limit ? $limit : 80 ) );
133 genNewForm( $cnt, $stock );
138 my $addr = PciIds::Address::new( $locId );
139 if( defined( $actHist ) ) {
140 print "<table class='item'>\n";
142 print "<table class='unnamedItem'>\n";
144 print "<col class='author'><col class='main'><col class='controls' span='2'>\n";
145 print "<tr class='label'><p>\n";
146 print "<td class='path' colspan='2'>";
147 genPathBare( $req, $addr, 1, 0 );
148 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
151 print "<td class='selects'><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
152 print "<td class='deletes'><input type='checkbox' name='loc-$cnt-del' value='del'>" if hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) );
153 print "<input type='hidden' name='loc-$cnt' value='$locId'>";
154 if( defined $actHist ) {
155 genHist( 'main-history', $actEmail, $actLogin, $actTime, $actName, $actNote, $actDisc, "loc-$cnt-sel", 'seen', undef, undef );
157 print "<tr class='main-history'><td class='empty' colspan='2'><td><input type='radio' name='loc-$cnt-sel' value='seen'>";
162 genHist( 'unseen-history', $email, $login, $time, $name, $note, $disc, "loc-$cnt-sel", $hist, "del-$hiscnt", "del-$hist" );
163 print "<input type='hidden' name='owner-$hist' value='$locId'>";
164 print "<input type='hidden' name='his-$cnt-$subcnt' value='$hist'>";
166 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
168 genNewForm( $cnt, $stock );
170 print "<p><input type='submit' name='submit' value='Submit'>\n";
171 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
172 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
174 print "<p>No pending items.\n";
181 sub adminForm( $$$$ ) {
182 my( $req, $args, $tables, $auth ) = @_;
183 if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
184 return genNewAdminForm( $req, $args, $tables, undef, $auth );
186 return notLoggedComplaint( $req, $args, $auth );
192 sub appendError( $ ) {
193 if( $errors eq '' ) {
194 $errors = "<p>".shift;
196 $errors .= "<br>".shift;
200 sub submitAdminForm( $$$$ ) {
201 my( $req, $args, $tables, $auth ) = @_;
202 my $authid = $auth->{'authid'};
203 if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
204 my( %deleted, %approved );
205 my $hasJscript = getFormValue( 'jscript-active', '0' );
207 $ans = loadStock() unless $hasJscript;#If there is jscript, no need to translate
208 my $maxcnt = getFormValue( 'max-cnt', 0 );
209 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
211 # Scan for deleted histories
212 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
213 my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/;
214 $deleted{$del} = 1 if( defined $del && $del ne '' );
216 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
217 my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
218 $approved{$sel} = 1 if( defined $sel && $sel ne '' );
220 # Check for collisions
222 foreach my $id ( keys %deleted ) {
223 if( $approved{$id} ) {
224 my $owner = getFormValue( "owner-$id", '' );
225 appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() );
226 $collision{$owner} = $_;
227 delete $deleted{$id};
228 delete $approved{$id};
233 foreach my $del ( keys %deleted ) {
234 $tables->deleteHistory( $del );
235 $modified{getFormValue( "owner-$del", '' )} = 1;
236 tulog( $authid, "Discussion deleted $del" );
239 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
240 my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
241 next if $collision{$addr->get()};
242 next unless defined $addr;
243 my $del = getFormValue( "loc-$i-del", '' );
244 if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) {
245 $tables->deleteItem( $addr->get() );
246 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
249 my $name = getFormValue( "name-$i", undef );
250 $name = undef if defined $name && $name eq '';
251 my $note = getFormValue( "note-$i", undef );
252 $note = undef if defined $note && $note eq '';
253 my $discussion = getFormValue( "disc-$i", '' );
254 unless( $hasJscript ) {#Modified by the stock answers, is no jscript at user
255 my $index = getFormValue( "ans-$i", '0' );
256 $discussion = $ans->[$index-1]->[1] if $index;
258 $discussion = undef if defined $discussion && $discussion eq '';
260 if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
265 if( defined $note && !defined $name ) {
266 appendError( "You must specify name if you set note at item ".$addr->pretty() );
269 my $action = $modified{$addr->get()};
270 $action = 1 if getFormValue( "loc-$i-sel", '' ) eq 'seen';
271 my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
272 if( defined $name || defined $discussion || $delete ) {
273 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr );
274 $tables->markChecked( $histId );
275 $select = $histId if defined $name || $delete;
276 tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
278 notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
280 if( defined $select && select ne '' ) {
281 $tables->setMainHistory( $addr->get(), $select );
282 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
284 notify( $tables, $addr->get(), $select, 2, 2 );
286 if( $action ) {#Approve anything in this item
287 my $subcnt = getFormValue( "loc-$i-subcnt", 0 );
288 for( my $j = 1; $j <= $subcnt; $j ++ ) {
289 my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/;
290 next unless defined $id;
291 next if $deleted{$id};
292 $tables->markChecked( $id );
293 tulog( $authid, "Discussion checked $id" );
297 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
299 return notLoggedComplaint( $req, $args, $auth );