complete registration flow (submit, mail verification, admin approvation)
[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_umgmt = array( 'nu_psubj' => array( 'it' => 'Brisk: credenziali di accesso.',
28                                            'en' => 'Brisk: credentials.'),
29                       'nu_ptext' => array( 'it' =>
30 'Ciao, sono l\' amministratore del sito di Brisk.
31
32 La verifica del tuo indirizzo di posta elettronica e del tuo nickname è andata a buon fine, per accedere al sito
33 d\'ora in poi potrai utilizzare l\' utente \'%s\' e la password \'%s\'.
34
35 Benvenuto e buone partite, mop.',
36                                            'en' => 'EN ptext [%s] [%s]'),
37                       'nu_phtml' => array( 'it' => 'Ciao, sono l\' amministratore del sito di Brisk.<br><br>
38 La verifica del tuo indirizzo di posta elettronica e del tuo nickname è andata a buon fine, per accedere al  sito d\'ora in poi potrai usare l\' utente \'%s\' e la password \'%s\'.<br>
39 Benvenuto e buone partite, mop.<br>',
40                                            'en' => 'EN phtml [%s] [%s]')
41                       );
42
43
44 ini_set("max_execution_time",  "240");
45
46 require_once($G_base."Obj/brisk.phh");
47 require_once($G_base."Obj/user.phh");
48 require_once($G_base."Obj/auth.phh");
49 require_once($G_base."Obj/mail.phh");
50 require_once($G_base."Obj/dbase_${G_dbasetype}.phh");
51 require_once($G_base."briskin5/Obj/briskin5.phh");
52 require_once($G_base."briskin5/Obj/placing.phh");
53 require_once($G_base."spush/brisk-spush.phh");
54
55 function check_auth()
56 {
57     GLOBAL $G_alarm_passwd, $sess, $_POST, $_SERVER;
58
59     $socket = FALSE;
60     $ret = FALSE;
61     $ip = $_SERVER["REMOTE_ADDR"];
62     $stp = 0;
63     $private = md5($G_alarm_passwd.$ip.$sess);
64     $cmd = array ("cmd" => "userauth", "sess" => $sess, "private" => $private, "the_end" => "true");
65     $cmd_ser = cmd_serialize($cmd);
66     $cmd_len = mb_strlen($cmd_ser, "ASCII");
67
68     do {
69         if (($socket = stream_socket_client("unix://".USOCK_PATH."2")) == FALSE)
70             break;
71         $stp = 1;
72         if (($rwr = fwrite($socket, $cmd_ser, $cmd_len)) == FALSE
73             || $rwr != $cmd_len)
74             break;
75         fflush($socket);
76         $stp = 2;
77         if (($buf = fread($socket, 4096)) == FALSE)
78             break;
79         $res = cmd_deserialize($buf);
80         $stp = 3;
81         if (!isset($res['val']) || $res['val'] != 200)
82             break;
83         $ret = TRUE;
84         $stp = 4;
85     } while (0);
86     if ($socket != FALSE)
87         fclose($socket);
88
89     if ($stp < 4) {
90         echo "STP: $stp<br>";
91     }
92     return ($ret);
93 }
94
95 function main() {
96     GLOBAL $G_dbpfx, $G_lang, $G_alarm_passwd, $mlang_umgmt, $f_mailusers, $sess, $_POST, $_SERVER;
97
98     if (check_auth() == FALSE) {
99         echo "Authentication failed";
100         exit;
101     }
102
103     if (isset($_POST['f_accept'])) {
104         $action = "accept";
105     }
106     else if (isset($_POST['f_delete'])) {
107         $action = "delete";
108     }
109
110
111     if ($action == "accept") {
112         if (($bdb = BriskDB::create()) == FALSE) {
113             log_crit("stat-day: database connection failed");
114             break;
115         }
116
117         foreach($_POST as $key => $value) {
118             if (substr($key, 0, 9) != "f_newuser")
119                 continue;
120
121             $id = (int)substr($key, 9);
122             if ($id <= 0)
123                 continue;
124
125
126             // retrieve list of active tournaments
127             $usr_sql = sprintf("
128 SELECT usr.*, guar.login AS guar_login
129      FROM %susers AS usr
130      JOIN %susers AS guar ON guar.code = usr.guar_code
131      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
132          AND usr.disa_reas = %d AND usr.code = %d;",
133                                $G_dbpfx, $G_dbpfx,
134                                USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
135                                USER_DIS_REA_NU_TOBECHK, $id);
136             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
137                 log_crit("stat-day: select from tournaments failed");
138                 break;
139             }
140             $usr_obj = pg_fetch_object($usr_pg, 0);
141             
142             printf("KEY: %s: %s %s<br>\n", $id, $value, $usr_obj->login);
143             // change state
144             $passwd = passwd_gen();
145
146             if (($bdb->user_update_passwd($usr_obj->code, $passwd)) == FALSE) {
147                 echo "fail 1.5<br>";
148                 break;
149             }
150
151             if (($bdb->user_update_flag_ty($usr_obj->code,
152                                         USER_FLAG_TY_DISABLE, USER_DIS_REA_NU_TOBECHK,
153                                         USER_FLAG_TY_NORM, USER_DIS_REA_NU_NONE)) == FALSE) {
154                 echo "fail 2<br>";
155                 break;
156             }
157
158             // send mail
159             $subj = $mlang_umgmt['nu_psubj'][$G_lang];
160             $body_txt = sprintf($mlang_umgmt['nu_ptext'][$G_lang],
161                                 $usr_obj->login, $passwd);
162             $body_htm = sprintf($mlang_umgmt['nu_phtml'][$G_lang],
163                                 $usr_obj->login, $passwd);
164
165             if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
166                 // mail error
167                 fprintf(STDERR, "ERROR: mail send FAILED\n");
168                 break;
169             }
170         }
171         exit;
172     }
173     else {
174         do {
175             if (($bdb = BriskDB::create()) == FALSE) {
176                 log_crit("stat-day: database connection failed");
177                 break;
178             }
179
180             // retrieve list of active tournaments
181             $usr_sql = sprintf("
182 SELECT usr.*, guar.login AS guar_login 
183      FROM %susers AS usr 
184      JOIN %susers AS guar ON guar.code = usr.guar_code 
185      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
186          AND usr.disa_reas = %d;", 
187                                $G_dbpfx, $G_dbpfx,
188                                USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
189                                USER_DIS_REA_NU_TOBECHK);
190             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
191                 log_crit("stat-day: select from tournaments failed");
192                 break;
193             }
194             
195             $usr_n = pg_numrows($usr_pg);
196             $tab_lines = "";
197             for ($i = 0 ; $i < $usr_n ; $i++) {
198                 $usr_obj = pg_fetch_object($usr_pg, $i);
199                 
200                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" CHECKED></td><td>%s</td><td></td></tr>\n",
201                                       $usr_obj->code, eschtml($usr_obj->login), eschtml($usr_obj->guar_login));
202             }
203             ?>
204 <html>
205 <body>
206 <form action="<?php echo "$PHP_SELF"; ?>" method="POST">
207 <table>
208 <?php
209      echo $tab_lines;
210 ?>
211 </table>
212 <input type="submit" name="f_accept" value="Accept">
213 <input type="submit" name="f_delete" value="Delete">
214 </form>
215 </body>
216 </html>
217 <?php
218         } while(FALSE);
219     }
220 }
221
222
223 main();
224
225
226 ?>