Handle hostnames with upper-case letters
[webmin.git] / useradmin / my_user_chooser.cgi
1 #!/usr/local/bin/perl
2 # my_user_chooser.cgi
3 # A modified version of chooser.cgi that uses the my_ functions
4
5 $trust_unknown_referers = 1;
6 require './user-lib.pl';
7 &init_config();
8 &ReadParse(undef, undef, 1);
9
10 if ($in{'multi'}) {
11         # selecting multiple users.
12         if ($in{'frame'} == 0) {
13                 # base frame
14                 &PrintHeader();
15                 print "<script>\n";
16                 @ul = split(/\s+/, $in{'user'});
17                 $len = @ul;
18                 print "sel = new Array($len);\n";
19                 print "selr = new Array($len);\n";
20                 for($i=0; $i<$len; $i++) {
21                         print "sel[$i] = \"".
22                               &quote_escape($ul[$i], '"')."\";\n";
23                         @uinfo = &my_getpwnam($ul[$i]);
24                         if (@uinfo) {
25                                 print "selr[$i] = \"".
26                                       &quote_escape($uinfo[6], '"')."\";\n";
27                                 }
28                         else {
29                                 print "selr[$i] = \"???\";\n";
30                                 }
31                         }
32                 print "</script>\n";
33                 print "<title>$text{'users_title1'}</title>\n";
34                 print "<frameset cols='50%,50%'>\n";
35                 print "<frame src=\"my_user_chooser.cgi?frame=1&multi=1\">\n";
36                 print "<frameset rows='*,50' frameborder=no>\n";
37                 print " <frame src=\"my_user_chooser.cgi?frame=2&multi=1\">\n";
38                 print " <frame src=\"my_user_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
39                 print "</frameset>\n";
40                 print "</frameset>\n";
41                 }
42         elsif ($in{'frame'} == 1) {
43                 # list of all users to choose from
44                 &popup_header();
45                 print "<script>\n";
46                 print "function adduser(u, r)\n";
47                 print "{\n";
48                 print "top.sel[top.sel.length] = u\n";
49                 print "top.selr[top.selr.length] = r\n";
50                 print "top.frames[1].location = top.frames[1].location\n";
51                 print "return false;\n";
52                 print "}\n";
53                 print "</script>\n";
54                 print "<font size=+1>$text{'users_all'}</font>\n";
55                 print "<table width=100%>\n";
56                 foreach $u (&get_users_list()) {
57                         if ($in{'user'} eq $u->[0]) { print "<tr $cb>\n"; }
58                         else { print "<tr>\n"; }
59                         $u->[6] =~ s/'/&#39;/g;
60                         print "<td width=20%><a href=\"\" onClick='return adduser(\"$u->[0]\", \"$u->[6]\")'>$u->[0]</a></td>\n";
61                         print "<td>$u->[6]</td> </tr>\n";
62                         }
63                 print "</table>\n";
64                 &popup_footer();
65                 }
66         elsif ($in{'frame'} == 2) {
67                 # show chosen users
68                 &popup_header();
69                 print "<font size=+1>$text{'users_sel'}</font>\n";
70                 print <<'EOF';
71 <table width=100%>
72 <script>
73 function sub(j)
74 {
75 sel2 = new Array(); selr2 = new Array();
76 for(k=0,l=0; k<top.sel.length; k++) {
77         if (k != j) {
78                 sel2[l] = top.sel[k];
79                 selr2[l] = top.selr[k];
80                 l++;
81                 }
82         }
83 top.sel = sel2; top.selr = selr2;
84 top.frames[1].location = top.frames[1].location;
85 return false;
86 }
87 for(i=0; i<top.sel.length; i++) {
88         document.write("<tr>\n");
89         document.write("<td><a href=\"\" onClick='return sub("+i+")'>"+top.sel[i]+"</a></td>\n");
90         document.write("<td>"+top.selr[i]+"</td>\n");
91         }
92 </script>
93 </table>
94 EOF
95                 &popup_footer();
96                 }
97         elsif ($in{'frame'} == 3) {
98                 # output OK and Cancel buttons
99                 &popup_header();
100                 print "<script>\n";
101                 print "function qjoin(l)\n";
102                 print "{\n";
103                 print "rv = \"\";\n";
104                 print "for(i=0; i<l.length; i++) {\n";
105                 print "    if (rv != '') rv += ' ';\n";
106                 print "    if (l[i].indexOf(' ') < 0) rv += l[i];\n";
107                 print "    else rv += '\"'+l[i]+'\"'\n";
108                 print "    }\n";
109                 print "return rv;\n";
110                 print "}\n";
111                 print "</script>\n";
112                 print "<form>\n";
113                 print "<input type=button value=\"$text{'users_ok'}\" ",
114                       "onClick='top.opener.ifield.value = qjoin(top.sel); ",
115                       "top.close()'>\n";
116                 print "<input type=button value=\"$text{'users_cancel'}\" ",
117                       "onClick='top.close()'>\n";
118                 print "&nbsp;&nbsp;<input type=button value=\"$text{'users_clear'}\" onClick='top.sel = new Array(); top.selr = new Array(); top.frames[1].location = top.frames[1].location'>\n";
119                 print "</form>\n";
120                 &popup_footer();
121                 }
122         }
123 else {
124         # selecting just one user .. display a list of all users to choose from
125         &popup_header($text{'users_title2'});
126         print "<script>\n";
127         print "function select(f)\n";
128         print "{\n";
129         print "top.opener.ifield.value = f;\n";
130         print "top.close();\n";
131         print "return false;\n";
132         print "}\n";
133         print "</script>\n";
134         print "<table width=100%>\n";
135         foreach $u (&get_users_list()) {
136                 if ($in{'user'} eq $u->[0]) { print "<tr $cb>\n"; }
137                 else { print "<tr>\n"; }
138                 print "<td width=20%><a href=\"\" onClick='return select(\"$u->[0]\")'>$u->[0]</a></td>\n";
139                 print "<td>$u->[6]</td> </tr>\n";
140                 }
141         print "</table>\n";
142         &popup_footer();
143         }
144
145 sub get_users_list
146 {
147 local(@uinfo, @users, %ucan);
148 if ($access{'uedit_mode'} == 2 || $access{'uedit_mode'} == 3) {
149         map { $ucan{$_}++ } split(/\s+/, $access{'uedit'});
150         }
151 &my_setpwent();
152 while(@uinfo = &my_getpwent()) {
153         if ($access{'uedit_mode'} == 0 ||
154             $access{'uedit_mode'} == 2 && $ucan{$uinfo[0]} ||
155             $access{'uedit_mode'} == 3 && !$ucan{$uinfo[0]} ||
156             $access{'uedit_mode'} == 4 &&
157                 (!$access{'uedit'} || $uinfo[2] >= $access{'uedit'}) &&
158                 (!$access{'uedit2'} || $uinfo[2] <= $access{'uedit2'}) ||
159             $access{'uedit_mode'} == 5 && $uinfo[3] == $access{'uedit'}) {
160                 push(@users, [ @uinfo ]);
161                 }
162         }
163 &my_endpwent();
164 return sort { $a->[0] cmp $b->[0] } @users;
165 }
166