]> ruin.nu Git - ndwebbie.git/blob - users.pl
more fatal warnings and other cleanup
[ndwebbie.git] / users.pl
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
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 #   the 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 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 use strict;
21 use warnings FATAL => 'all';
22 use POSIX;
23 our $BODY;
24 our $DBH;
25 our $LOG;
26
27 $ND::TEMPLATE->param(TITLE => 'Users');
28
29 die "You don't have access" unless isHC();
30
31 my $error = '';
32 my $user;
33 if (defined param('user') && param('user') =~ /^(\d+)$/){
34         my $query = $DBH->prepare(q{
35 SELECT uid,username,hostmask,coords(x,y,z) AS planet,attack_points,defense_points,scan_points,humor_points  
36         FROM users u LEFT OUTER JOIN current_planet_stats p ON u.planet = p.id
37 WHERE uid = ?;
38 }) or $error .= "<p> Something went wrong: </p>";
39         $user = $DBH->selectrow_hashref($query,undef,$1) or $error.= "<p> Something went wrong: ".$DBH->errstr."</p>";
40 }
41
42
43 if ($user && defined param('cmd') && param('cmd') eq 'change'){
44         $DBH->begin_work;
45         for my $param (param()){
46                 if ($param =~ /^c:(\w+)$/){
47                         my $column = $1;
48                         my $value = param($column);
49                         if ($column eq 'planet'){
50                                 if ($value eq ''){
51                                         $value = undef;
52                                 }elsif($value =~ /^(\d+)\D+(\d+)\D+(\d+)$/){
53                                         ($value) = $DBH->selectrow_array(q{SELECT id FROM
54                                                 current_planet_stats WHERE x = ? and y = ? and z =?}
55                                                 ,undef,$1,$2,$3);
56                                 }
57                         }
58                         if ($DBH->do(qq{UPDATE users SET $column = ? WHERE uid = ? }
59                                         ,undef,$value,$user->{uid})){
60                                 $user->{$column} = param($column);
61                                 $LOG->execute($ND::UID,"HC set $column to $value for user: $user->{uid}");
62                         }else{
63                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
64                         }
65                 }
66         }
67         my $groups = $DBH->prepare('SELECT gid,groupname FROM groups');
68         my $delgroup = $DBH->prepare(q{DELETE FROM groupmembers WHERE uid = ? AND gid = ?});
69         my $addgroup = $DBH->prepare(q{INSERT INTO groupmembers (uid,gid) VALUES(?,?)});
70         $groups->execute();
71         while (my $group = $groups->fetchrow_hashref){
72                 my $query;
73                 next unless defined param($group->{gid});
74                 if (param($group->{gid}) eq 'remove'){
75                         $query = $delgroup;
76                 }elsif(param($group->{gid}) eq 'add'){
77                         $query = $addgroup;
78                 }
79                 if ($query){
80                         if ($query->execute($user->{uid},$group->{gid})){
81                                 $LOG->execute($ND::UID,"HC added user: $user->{uid} to group: $group->{gid}");
82                         }else{
83                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
84                         }
85                 }
86         }
87         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
88 }
89
90 if ($user){
91         $BODY->param(User => $user->{uid});
92         $BODY->param(Username => $user->{username});
93         $BODY->param(Hostmask => $user->{hostmask});
94         $BODY->param(Planet => $user->{planet});
95         $BODY->param(Attack_points => $user->{attack_points});
96         $BODY->param(Defense_points => $user->{defense_points});
97         $BODY->param(Scan_points => $user->{scan_points});
98         $BODY->param(humor_points => $user->{humor_points});
99
100         my $groups = $DBH->prepare(q{SELECT g.gid,g.groupname,uid FROM groups g LEFT OUTER JOIN (SELECT gid,uid FROM groupmembers WHERE uid = ?) AS gm ON g.gid = gm.gid});
101         $groups->execute($user->{uid});
102
103         my @addgroups;
104         my @remgroups;
105 while (my $group = $groups->fetchrow_hashref){
106         if ($group->{uid}){
107                 push @remgroups,{Id => $group->{gid}, Name => $group->{groupname}};
108         }else{
109                 push @addgroups,{Id => $group->{gid}, Name => $group->{groupname}};
110         }
111 }
112 $BODY->param(RemoveGroups => \@remgroups);
113 $BODY->param(AddGroups => \@addgroups);
114
115 }else{
116         my $query = $DBH->prepare(qq{SELECT u.uid,username,TRIM(',' FROM concat(g.groupname||',')) AS groups
117                 FROM users u LEFT OUTER JOIN (groupmembers gm NATURAL JOIN groups g) ON gm.uid = u.uid
118                 WHERE u.uid > 0
119                 GROUP BY u.uid,username
120                 ORDER BY username})or $error .= $DBH->errstr;
121         $query->execute or $error .= $DBH->errstr;
122         my @users;
123         my $i = 0;
124         while (my $user = $query->fetchrow_hashref){
125                 $i++;
126                 $user->{ODD} = $i % 2;
127                 push @users, $user;
128         }
129         $BODY->param(Users => \@users);
130 }
131 $BODY->param(Error => $error);
132 1;