usermgmt improved (not yet finished)
[brisk.git] / web / usermgmt.php
1 <?php
2 /*
3  *  brisk - usermgmt.php
4  *
5  *  Copyright (C) 2014      Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 $G_base = "";
26
27 $mlang_stat_day = array( 'normal match'=> array( 'it' => 'Partite normali',
28                                                  'en' => 'Normal matches' ),
29                          'special match' => array( 'it' => 'Partite speciali',
30                                                    'en' => 'Special matches'),
31
32                          'info_total'=> array( 'it' => 'totali',
33                                                'en' => 'En totali')
34                          );
35
36 ini_set("max_execution_time",  "240");
37
38 require_once($G_base."Obj/brisk.phh");
39 require_once($G_base."Obj/user.phh");
40 require_once($G_base."Obj/auth.phh");
41 require_once($G_base."Obj/dbase_${G_dbasetype}.phh");
42 require_once($G_base."briskin5/Obj/briskin5.phh");
43 require_once($G_base."briskin5/Obj/placing.phh");
44 require_once($G_base."spush/brisk-spush.phh");
45
46 function check_auth()
47 {
48     GLOBAL $G_alarm_passwd, $sess, $_POST, $_SERVER;
49
50     $socket = FALSE;
51     $ret = FALSE;
52     $ip = $_SERVER["REMOTE_ADDR"];
53     $stp = 0;
54     $private = md5($G_alarm_passwd.$ip.$sess);
55     $cmd = array ("cmd" => "userauth", "sess" => $sess, "private" => $private, "the_end" => "true");
56     $cmd_ser = cmd_serialize($cmd);
57     $cmd_len = mb_strlen($cmd_ser, "ASCII");
58
59     do {
60         if (($socket = stream_socket_client("unix://".USOCK_PATH."2")) == FALSE)
61             break;
62         $stp = 1;
63         if (($rwr = fwrite($socket, $cmd_ser, $cmd_len)) == FALSE
64             || $rwr != $cmd_len)
65             break;
66         fflush($socket);
67         $stp = 2;
68         if (($buf = fread($socket, 4096)) == FALSE)
69             break;
70         $res = cmd_deserialize($buf);
71         $stp = 3;
72         if (!isset($res['val']) || $res['val'] != 200)
73             break;
74         $ret = TRUE;
75         $stp = 4;
76     } while (0);
77     if ($socket != FALSE)
78         fclose($socket);
79
80     if ($stp < 4) {
81         echo "STP: $stp<br>";
82     }
83     return ($ret);
84 }
85
86 function main() {
87     GLOBAL $G_dbpfx, $G_alarm_passwd, $f_mailusers, $sess, $_POST, $_SERVER;
88
89     if (check_auth() == FALSE) {
90         echo "Authentication failed";
91         exit;
92     }
93
94     if (isset($_POST['f_accept'])) {
95         $action = "accept";
96     }
97     else if (isset($_POST['f_delete'])) {
98         $action = "delete";
99     }
100
101
102     if ($action == "accept") {
103         if (($bdb = BriskDB::create()) == FALSE) {
104             log_crit("stat-day: database connection failed");
105             break;
106         }
107
108         foreach($_POST as $key => $value) {
109             if (substr($key, 0, 9) != "f_newuser")
110                 continue;
111
112             $id = (int)substr($key, 9);
113             if ($id <= 0)
114                 continue;
115
116
117             // retrieve list of active tournaments
118             $usr_sql = sprintf("
119 SELECT usr.*, guar.login AS guar_login
120      FROM %susers AS usr
121      JOIN %susers AS guar ON guar.code = usr.guar_code
122      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
123          AND usr.disa_reas = %d AND usr.code = %d;",
124                                $G_dbpfx, $G_dbpfx,
125                                USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
126                                USER_DIS_REA_NU_TOBECHK, $id);
127             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
128                 log_crit("stat-day: select from tournaments failed");
129                 break;
130             }
131             $usr_obj = pg_fetch_object($usr_pg, 0);
132             
133             printf("KEY: %s: %s %s<br>\n", $id, $value, $usr_obj->login);
134             // change state
135             // send mail
136             // populate
137         }
138         exit;
139     }
140     else {
141         do {
142             if (($bdb = BriskDB::create()) == FALSE) {
143                 log_crit("stat-day: database connection failed");
144                 break;
145             }
146
147             // retrieve list of active tournaments
148             $usr_sql = sprintf("
149 SELECT usr.*, guar.login AS guar_login 
150      FROM %susers AS usr 
151      JOIN %susers AS guar ON guar.code = usr.guar_code 
152      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
153          AND usr.disa_reas = %d;", 
154                                $G_dbpfx, $G_dbpfx,
155                                USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
156                                USER_DIS_REA_NU_TOBECHK);
157             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
158                 log_crit("stat-day: select from tournaments failed");
159                 break;
160             }
161             
162             $usr_n = pg_numrows($usr_pg);
163             $tab_lines = "";
164             for ($i = 0 ; $i < $usr_n ; $i++) {
165                 $usr_obj = pg_fetch_object($usr_pg, $i);
166                 
167                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" CHECKED></td><td>%s</td><td></td></tr>\n",
168                                       $usr_obj->code, eschtml($usr_obj->login), eschtml($usr_obj->guar_login));
169             }
170             ?>
171 <html>
172 <body>
173 <form action="<?php echo "$PHP_SELF"; ?>" method="POST">
174 <table>
175 <?php
176      echo $tab_lines;
177 ?>
178 </table>
179 <input type="submit" name="f_accept" value="Accept">
180 <input type="submit" name="f_delete" value="Delete">
181 </form>
182 </body>
183 </html>
184 <?php
185         } while(FALSE);
186     }
187 }
188
189
190 main();
191
192
193 ?>