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