websocket graceful shutdown and safety queue
[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 foreach (array("HTTP_HOST", "DOCUMENT_ROOT") as $i) {
26     if (isset($_SERVER[$i])) {
27         $$i = $_SERVER[$i];
28         }
29     }
30
31 foreach (array("pazz") as $i) {
32     if (isset($_POST[$i])) {
33         $$i = $_POST[$i];
34         }
35     }
36
37 foreach (array("sess") as $i) {
38     if (isset($_COOKIE[$i])) {
39         $$i = $_COOKIE[$i];
40         }
41     }
42
43 foreach (array("sess") as $i) {
44     if (isset($_COOKIE[$i])) {
45         $$i = $_COOKIE[$i];
46         }
47     }
48
49 $G_base = "";
50
51 $mlang_umgmt = array( 'nu_psubj' => array( 'it' => 'Brisk: credenziali di accesso.',
52                                            'en' => 'Brisk: credentials.'),
53                       'nu_ptext' => array( 'it' =>
54 'Ciao, sono l\' amministratore del sito di Brisk.
55
56 La verifica del tuo indirizzo di posta elettronica e del tuo nickname è andata a buon fine, per accedere al sito
57 d\'ora in poi potrai utilizzare l\' utente \'%s\' e la password \'%s\'.
58
59 Benvenuto e buone partite, mop.',
60                                            'en' => 'EN ptext [%s] [%s]'),
61                       'nu_phtml' => array( 'it' => 'Ciao, sono l\' amministratore del sito di Brisk.<br><br>
62 La verifica del tuo indirizzo di posta elettronica e del tuo nickname è andata a buon fine.<br><br>Per accedere al  sito d\'ora in poi potrai usare l\' utente \'%s\' e la password \'%s\'.<br><br>
63 Benvenuto e buone partite, mop.<br>',
64                                            'en' => 'EN phtml [%s] [%s]')
65                       );
66
67
68 ini_set("max_execution_time",  "240");
69
70 require_once($G_base."Obj/brisk.phh");
71 require_once($G_base."Obj/user.phh");
72 require_once($G_base."Obj/auth.phh");
73 require_once($G_base."Obj/mail.phh");
74 require_once($G_base."Obj/dbase_base.phh");
75 require_once($G_base."Obj/dbase_${G_dbasetype}.phh");
76 require_once($G_base."briskin5/Obj/briskin5.phh");
77 require_once($G_base."briskin5/Obj/placing.phh");
78 require_once($G_base."spush/brisk-spush.phh");
79 require_once($G_base."index_wr.php");
80
81 function check_auth()
82 {
83     GLOBAL $G_alarm_passwd, $sess, $_POST, $_SERVER;
84
85     $socket = FALSE;
86     $ret = FALSE;
87     if (array_key_exists("HTTP_X_REAL_IP", $_SERVER)) {
88         $ip = $_SERVER["HTTP_X_REAL_IP"];
89     }
90     else {
91         $ip = $_SERVER["REMOTE_ADDR"];
92     }
93     $stp = 0;
94     $private = md5($G_alarm_passwd.$ip.$sess);
95     $cmd = array ("cmd" => "userauth", "sess" => $sess, "private" => $private, "the_end" => "true");
96     $cmd_ser = cmd_serialize($cmd);
97     $cmd_len = mb_strlen($cmd_ser, "ASCII");
98
99     do {
100         if (($socket = stream_socket_client('unix://'.USOCK_PATH_PFX.'_admin.sock')) == FALSE)
101             break;
102         $stp = 1;
103         if (($rwr = fwrite($socket, $cmd_ser, $cmd_len)) == FALSE
104             || $rwr != $cmd_len)
105             break;
106         fflush($socket);
107         $stp = 2;
108         if (($buf = fread($socket, 4096)) == FALSE)
109             break;
110         $res = cmd_deserialize($buf);
111         $stp = 3;
112         if (!isset($res['val']) || $res['val'] != 200)
113             break;
114         $ret = TRUE;
115         $stp = 4;
116     } while (0);
117     if ($socket != FALSE)
118         fclose($socket);
119
120     if ($stp < 4) {
121         echo "STP: $stp<br>";
122     }
123     return ($ret);
124 }
125
126 $s_style = "
127 <style>
128      table.the_tab {
129             border-collapse: collapse;
130             margin: 8px;
131             }
132
133      table.the_tab td {
134             border: 1px solid black;
135             padding: 8px;
136             }
137 </style>";
138
139 function main() {
140     GLOBAL $s_style, $G_dbpfx, $G_lang, $G_alarm_passwd, $G_proto, $G_domain, $G_webbase;
141     GLOBAL $mlang_umgmt, $mlang_indwr, $f_mailusers, $sess, $_POST, $_SERVER;
142
143
144     $curtime = time();
145     $status = "";
146
147     if (check_auth() == FALSE) {
148         echo "Authentication failed";
149         exit;
150     }
151
152     $nocheck = FALSE;
153     if (isset($_GET['f_nocheck'])) {
154         $nocheck = TRUE;
155     }
156
157     if (isset($_GET['do']) && $_GET['do'] == 'newuser') {
158         if (isset($_POST['f_accept'])) {
159             $action = "accept";
160         }
161         else if (isset($_POST['f_delete'])) {
162             $action = "delete";
163         }
164         else {
165             $action = "show";
166         }
167
168         if ($action == "accept") {
169             foreach($_POST as $key => $value) {
170                 if (substr($key, 0, 9) != "f_newuser")
171                     continue;
172
173                 $id = (int)substr($key, 9);
174                 if ($id <= 0)
175                     continue;
176
177                 // check existence of username or email
178                 $is_trans = FALSE;
179                 $res = FALSE;
180                 do {
181                     if (($bdb = BriskDB::create()) == FALSE)
182                         break;
183
184                     // retrieve list added users
185                     $usr_sql = sprintf("
186 SELECT usr.*, guar.login AS guar_login
187      FROM %susers AS usr
188      JOIN %susers AS guar ON guar.code = usr.guar_code
189      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
190          AND usr.disa_reas = %d AND usr.code = %d;",
191                                $G_dbpfx, $G_dbpfx,
192                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
193                                USER_DIS_REA_NU_ADDED, $id);
194                     if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
195                         log_crit("stat-day: select from tournaments failed");
196                         break;
197                     }
198                     $usr_n = pg_numrows($usr_pg);
199                     if ($usr_n != 1) {
200                         $status .= sprintf("Inconsistency for code %d, returned %d records, skipped.<br>",
201                                           $id, $usr_n);
202                         break;
203                     }
204
205                     $usr_obj = pg_fetch_object($usr_pg, 0);
206
207                     $bdb->transaction('BEGIN');
208                     $is_trans = TRUE;
209
210
211                     if (($bdb->user_update_flag_ty($usr_obj->code, USER_FLAG_TY_DISABLE,
212                                                    TRUE, USER_DIS_REA_NU_ADDED,
213                                                    TRUE, USER_DIS_REA_NU_MAILED)) == FALSE) {
214                         echo "fail 2<br>";
215                         break;
216                     }
217
218                     if (($mail_code = $bdb->mail_reserve_code()) == FALSE) {
219                         fprintf(STDERR, "ERROR: mail reserve code FAILED\n");
220                         break;
221                     }
222                     $hash = md5($curtime . $G_alarm_passwd . $usr_obj->login . $usr_obj->email);
223
224                     $confirm_page = sprintf("%s://%s/%s/mailmgr.php?f_act=checkmail&f_code=%d&f_hash=%s",
225                                             $G_proto, $G_domain, $G_webbase, $mail_code, $hash);
226                     $subj = $mlang_indwr['nu_msubj'][$G_lang];
227                     if (($usr_obj->type & USER_FLAG_TY_APPR) == USER_FLAG_TY_APPR) {
228                         $body_txt = sprintf($mlang_indwr['ap_mtext'][$G_lang],
229                                             $usr_obj->login, $confirm_page);
230                         $body_htm = sprintf($mlang_indwr['ap_mhtml'][$G_lang],
231                                             $usr_obj->login, $confirm_page);
232                     }
233                     else {
234                         $body_txt = sprintf($mlang_indwr['nu_mtext'][$G_lang],
235                                             $usr_obj->guar_login, $usr_obj->login, $confirm_page);
236                         $body_htm = sprintf($mlang_indwr['nu_mhtml'][$G_lang],
237                                             $usr_obj->guar_login, $usr_obj->login, $confirm_page);
238                     }
239
240                     $mail_item = new MailDBItem($mail_code, $usr_obj->code, MAIL_TYP_CHECK,
241                                                 $curtime, $subj, $body_txt, $body_htm, $hash);
242
243                     if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
244                         // mail error
245                         fprintf(STDERR, "ERROR: mail send FAILED\n");
246                         break;
247                     }
248                     // save the mail
249                     if ($mail_item->store($bdb) == FALSE) {
250                         // store mail error
251                         fprintf(STDERR, "ERROR: store mail FAILED\n");
252                         break;
253                     }
254                     $status .= sprintf("status change for %s: SUCCESS<br>", $usr_obj->login);
255                     $bdb->transaction('COMMIT');
256                     $res = TRUE;
257                 } while(FALSE);
258                 if ($res == FALSE) {
259                     $status .= sprintf("Error occurred during accept action<br>");
260                     if ($is_trans)
261                         $bdb->transaction('ROLLBACK');
262                     break;
263                 }
264             }
265         } // else if ($action == "accept") {
266         else if ($action == "delete") {
267             foreach($_POST as $key => $value) {
268                 if (substr($key, 0, 9) != "f_newuser")
269                     continue;
270
271                 $id = (int)substr($key, 9);
272                 if ($id <= 0)
273                     continue;
274
275                 // check existence of username or email
276                 $is_trans = FALSE;
277                 $res = FALSE;
278                 do {
279                     if (($bdb = BriskDB::create()) == FALSE)
280                         break;
281
282                     // retrieve list added users
283                     $usr_sql = sprintf("
284 SELECT usr.*, guar.login AS guar_login
285      FROM %susers AS usr
286      JOIN %susers AS guar ON guar.code = usr.guar_code
287      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
288          AND usr.disa_reas = %d AND usr.code = %d;",
289                                $G_dbpfx, $G_dbpfx,
290                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
291                                USER_DIS_REA_NU_ADDED, $id);
292                     if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
293                         log_crit("stat-day: select from tournaments failed");
294                         break;
295                     }
296                     $usr_n = pg_numrows($usr_pg);
297                     if ($usr_n != 1) {
298                         $status .= sprintf("Inconsistency for code %d, returned %d records, skipped.<br>",
299                                           $id, $usr_n);
300                         break;
301                     }
302
303                     $usr_obj = pg_fetch_object($usr_pg, 0);
304
305                     $bdb->transaction('BEGIN');
306                     $is_trans = TRUE;
307
308                     // retrieve list added users
309                     $usr_sql = sprintf("
310                          DELETE FROM %susers
311                              WHERE (type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer))
312                                AND disa_reas = %d AND code = %d;",
313                                $G_dbpfx, USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
314                                USER_DIS_REA_NU_ADDED, $id);
315                     if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
316                         log_crit(sprintf("Delete of user %d failed", $id));
317                         break;
318                     }
319
320                     $status .= sprintf("User %s removed: SUCCESS<br>", $usr_obj->login);
321                     $bdb->transaction('COMMIT');
322                     $res = TRUE;
323                 } while(FALSE);
324                 if ($res == FALSE) {
325                     $status .= sprintf("Error occurred during delete action<br>");
326                     if ($is_trans)
327                         $bdb->transaction('ROLLBACK');
328                     break;
329                 }
330             }
331         } // else if ($action == "accept") {
332
333
334
335         do {
336             if (($bdb = BriskDB::create()) == FALSE) {
337                 log_crit("stat-day: database connection failed");
338                 break;
339             }
340
341             // retrieve list added users
342             $usr_sql = sprintf("
343 SELECT usr.*, guar.login AS guar_login
344      FROM %susers AS usr
345      JOIN %susers AS guar ON guar.code = usr.guar_code
346      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
347          AND usr.disa_reas = %d ORDER BY usr.lintm;",
348                                $G_dbpfx, $G_dbpfx,
349                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
350                                USER_DIS_REA_NU_ADDED);
351             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
352                 log_crit("stat-day: select from tournaments failed");
353                 break;
354             }
355             $usr_n = pg_numrows($usr_pg);
356             $tab_lines = "<tr><th></th><th>User</th><th>Guar</th><th>Date</th></tr>";
357             for ($i = 0 ; $i < $usr_n ; $i++) {
358                 $usr_obj = pg_fetch_object($usr_pg, $i);
359
360                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" %s></td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
361                                       $usr_obj->code, ($nocheck ? "" : "CHECKED"),
362                                       eschtml($usr_obj->login), eschtml($usr_obj->guar_login), $usr_obj->lintm);
363             }
364
365
366             ?>
367 <html>
368 <head>
369 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
370 <title>Brisk: new imported users management.</title>
371      <?php echo "$s_style"; ?>
372 </head>
373 <body>
374 <h2> New imported users management.</h2>
375      <?php if ($status != "") { echo "$status"; } ?>
376 <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
377 <table class="the_tab">
378 <?php
379      echo $tab_lines;
380 ?>
381 </table>
382 <input type="submit" name="f_accept" value="Newuser Accept">
383 <input type="submit" name="f_delete" value="Newuser Delete">
384 </form>
385 </body>
386 </html>
387 <?php
388            exit;
389         } while(FALSE);
390         printf("Some error occurred during newuser visualization\n");
391         exit;
392     }
393
394     if (isset($_GET['do']) && $_GET['do'] == 'mailed') {
395         if (isset($_POST['f_resend'])) {
396             $action = "resend";
397         }
398         else if (isset($_POST['f_delete'])) {
399             $action = "delete";
400         }
401         else {
402             $action = "show";
403         }
404
405         if ($action == "resend") {
406             foreach($_POST as $key => $value) {
407                 if (substr($key, 0, 9) != "f_newuser")
408                     continue;
409
410                 $id = (int)substr($key, 9);
411                 if ($id <= 0)
412                     continue;
413
414                 $res = FALSE;
415                 do {
416                     if (($bdb = BriskDB::create()) == FALSE) {
417                         $status .= "1<br>";
418                         break;
419                     }
420                     // retrieve list added users
421                     $mai_sql = sprintf("
422 SELECT mail.*, usr.email AS email
423      FROM %susers AS usr
424      JOIN %smails AS mail ON mail.ucode = usr.code
425      WHERE mail.ucode = %d AND mail.type = %d",
426                                        $G_dbpfx, $G_dbpfx, $id, MAIL_TYP_CHECK);
427                     if (($mai_pg = pg_query($bdb->dbconn->db(), $mai_sql)) == FALSE) {
428                         log_crit("retrieve mail failed");
429                         $status .= "2<br>";
430                         break;
431                     }
432                     $mai_n = pg_numrows($mai_pg);
433                     if ($mai_n != 1) {
434                         $status .= sprintf("Inconsistency for code %d, returned %d records, skipped.<br>",
435                                           $id, $mai_n);
436                         break;
437                     }
438                     $mai_obj = pg_fetch_object($mai_pg, 0);
439                     $mail = MailDBItem::MailDBItemFromRecord($mai_obj);
440
441                     if (brisk_mail($mai_obj->email, $mail->subj, $mail->body_txt, $mail->body_htm) == FALSE) {
442                         // mail error
443                         $status .= sprintf("Send mail filed for user id %d<br>\n", $id);
444                         break;
445                     }
446                     $res = TRUE;
447                 } while(FALSE);
448                 if ($res == FALSE) {
449                     $status .= sprintf("Error occurred during resend action<br>");
450                     break;
451                 }
452             } // foreach
453         }
454
455         do {
456             if (($bdb = BriskDB::create()) == FALSE) {
457                 log_crit("stat-day: database connection failed");
458                 break;
459             }
460
461             // retrieve list added users
462             $usr_sql = sprintf("
463 SELECT usr.*, guar.login AS guar_login
464      FROM %susers AS usr
465      JOIN %susers AS guar ON guar.code = usr.guar_code
466      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
467          AND usr.disa_reas = %d ORDER BY usr.lintm;",
468                                $G_dbpfx, $G_dbpfx,
469                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
470                                USER_DIS_REA_NU_MAILED);
471             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
472                 log_crit("stat-day: select from tournaments failed");
473                 break;
474             }
475             $usr_n = pg_numrows($usr_pg);
476             $tab_lines = "<tr><th></th><th>User</th><th>Guar</th><th>Date</th></tr>";
477             for ($i = 0 ; $i < $usr_n ; $i++) {
478                 $usr_obj = pg_fetch_object($usr_pg, $i);
479
480                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" %s></td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
481                                       $usr_obj->code, ($nocheck ? "" : "CHECKED"),
482                                       eschtml($usr_obj->login), eschtml($usr_obj->guar_login), $usr_obj->lintm);
483             }
484             ?>
485 <html>
486 <head>
487 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
488 <title>Brisk: new mailed users management.</title>
489      <?php echo "$s_style"; ?>
490 </head>
491 <body>
492 <h2> New mailed users management.</h2>
493      <?php if ($status != "") { echo "$status"; } ?>
494 <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
495 <table class="the_tab">
496 <?php
497      echo $tab_lines;
498 ?>
499 </table>
500 <input type="submit" name="f_resend" value="Mailed Resend">
501 <input type="submit" name="f_delete" value="Mailed Delete">
502 </form>
503 </body>
504 </html>
505 <?php
506            exit;
507         } while(FALSE);
508         printf("Some error occurred during newuser visualization\n");
509         exit;
510     }
511     else { // if ($_GET['do'] ...
512         if (isset($_POST['f_accept'])) {
513             $action = "accept";
514         }
515         else if (isset($_POST['f_delete'])) {
516             $action = "delete";
517         }
518         else {
519             $action = "show";
520         }
521
522         if ($action == "accept") {
523             if (($bdb = BriskDB::create()) == FALSE) {
524                 log_crit("stat-day: database connection failed");
525                 break;
526             }
527
528             foreach($_POST as $key => $value) {
529                 if (substr($key, 0, 9) != "f_newuser")
530                     continue;
531
532                 $id = (int)substr($key, 9);
533                 if ($id <= 0)
534                     continue;
535
536
537                 // retrieve list of active tournaments
538                 $usr_sql = sprintf("
539 SELECT usr.*, guar.login AS guar_login
540      FROM %susers AS usr
541      JOIN %susers AS guar ON guar.code = usr.guar_code
542      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
543          AND usr.disa_reas = %d AND usr.code = %d;",
544                                    $G_dbpfx, $G_dbpfx,
545                                    USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
546                                    USER_DIS_REA_NU_TOBECHK, $id);
547                 if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
548                     log_crit("stat-day: select from tournaments failed");
549                     break;
550                 }
551                 $usr_obj = pg_fetch_object($usr_pg, 0);
552
553                 printf("KEY: %s: %s %s<br>\n", $id, $value, $usr_obj->login);
554                 // change state
555                 $passwd = passwd_gen();
556
557                 if (($bdb->user_update_passwd($usr_obj->code, $passwd)) == FALSE) {
558                     echo "fail 1.5<br>";
559                     break;
560                 }
561
562                 if (($bdb->user_update_flag_ty($usr_obj->code, USER_FLAG_TY_DISABLE,
563                                                TRUE, USER_DIS_REA_NU_TOBECHK,
564                                                FALSE, USER_DIS_REA_NONE)) == FALSE) {
565                     echo "fail 2<br>";
566                     break;
567                 }
568
569                 $bdb->user_update_login_time($usr_obj->code, 0);
570
571                 // send mail
572                 $subj = $mlang_umgmt['nu_psubj'][$G_lang];
573                 $body_txt = sprintf($mlang_umgmt['nu_ptext'][$G_lang],
574                                     $usr_obj->login, $passwd);
575                 $body_htm = sprintf($mlang_umgmt['nu_phtml'][$G_lang],
576                                     $usr_obj->login, $passwd);
577
578                 log_step(sprintf("[%s], [%s], [%s], [%s]\n", $usr_obj->email, $subj, $body_txt, $body_htm));
579
580
581                 if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
582                     // mail error
583                     fprintf(STDERR, "ERROR: mail send FAILED\n");
584                     break;
585                 }
586             }
587             exit;
588         }
589         else if ($action == "delete") {
590             foreach($_POST as $key => $value) {
591                 if (substr($key, 0, 9) != "f_newuser")
592                     continue;
593
594                 $id = (int)substr($key, 9);
595                 if ($id <= 0)
596                     continue;
597
598                 // check existence of username or email
599                 $is_trans = FALSE;
600                 $res = FALSE;
601                 do {
602                     if (($bdb = BriskDB::create()) == FALSE)
603                         break;
604
605                     // retrieve list added users
606                     $usr_sql = sprintf("
607 SELECT usr.*, guar.login AS guar_login
608      FROM %susers AS usr
609      JOIN %susers AS guar ON guar.code = usr.guar_code
610      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
611          AND usr.disa_reas = %d AND usr.code = %d;",
612                                $G_dbpfx, $G_dbpfx,
613                                        USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
614                                        USER_DIS_REA_NU_TOBECHK, $id);
615
616
617                     if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
618                         log_crit("stat-day: select from tournaments failed");
619                         break;
620                     }
621                     $usr_n = pg_numrows($usr_pg);
622                     if ($usr_n != 1) {
623                         $status .= sprintf("Inconsistency for code %d, returned %d records, skipped.<br>",
624                                           $id, $usr_n);
625                         break;
626                     }
627
628                     $usr_obj = pg_fetch_object($usr_pg, 0);
629
630                     $bdb->transaction('BEGIN');
631                     $is_trans = TRUE;
632
633                     $del_sql = sprintf("DELETE FROM %susers WHERE code = %d;",
634                                        $G_dbpfx, $usr_obj->code);
635
636                     if (($del_pg = pg_query($bdb->dbconn->db(), $del_sql)) == FALSE) {
637                         log_crit(sprintf("Delete user %d failed", $usr_obj->code));
638                         break;
639                     }
640
641                     // FIXME: add to index_wr.php strings
642                     $subj = "Brisk: nickname rifiutato";
643                     // the same for both cases:
644                     //  if (($usr_obj->type & USER_FLAG_TY_APPR) == USER_FLAG_TY_APPR) {
645                         $body_txt = sprintf('Ciao, sono l\' amministratore del sito di Brisk.
646
647 Ti volevo segnalare che il nickname \'%s\' con cui ti volevi registrare
648 non ha superato la fase di verifica manuale; il motivo può essere
649 la sua illeggibilità per gli altri utenti o il contenuto poco ortodosso
650 o troppo aggressivo o ci sono troppe cifre consecutive o qualcosa del genere.
651
652 La procedura di registrazione va ripetuta.
653
654 Saluti e buone partite, mop.', $usr_obj->login);
655
656                         $body_htm = sprintf('Ciao, sono l\' amministratore del sito di Brisk.<br><br>
657 Ti volevo segnalare che il nickname \'%s\' con cui ti volevi registrare
658 non ha superato la fase di verifica manuale; il motivo può essere
659 la sua illeggibilità per gli altri utenti o il contenuto poco ortodosso
660 o troppo aggressivo o ci sono troppe cifre consecutive o qualcosa del genere.<br><br>
661 La procedura di registrazione va ripetuta.<br><br>
662 Saluti e buone partite, mop.', $usr_obj->login);
663                     /* } */
664                     /* else { */
665                     /*     $body_txt = sprintf($mlang_indwr['nu_mtext'][$G_lang], */
666                     /*                         $usr_obj->guar_login, $usr_obj->login, $confirm_page); */
667                     /*     $body_htm = sprintf($mlang_indwr['nu_mhtml'][$G_lang], */
668                     /*                         $usr_obj->guar_login, $usr_obj->login, $confirm_page); */
669                     /* } */
670
671                     if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
672                         // mail error
673                         fprintf(STDERR, "ERROR: mail send FAILED\n");
674                         break;
675                     }
676                     $status .= sprintf("user delete for %s: SUCCESS<br>", $usr_obj->login);
677                     $bdb->transaction('COMMIT');
678                     $res = TRUE;
679                 } while(FALSE);
680                 if ($res == FALSE) {
681                     $status .= sprintf("Error occurred during accept action<br>");
682                     if ($is_trans)
683                         $bdb->transaction('ROLLBACK');
684                     break;
685                 }
686                 printf("Registration %s for login %s deleted<br>\n", $usr_obj->code, $usr_obj->login);
687             }
688         }
689         else {
690             do {
691             if (($bdb = BriskDB::create()) == FALSE) {
692                 log_crit("stat-day: database connection failed");
693                 break;
694             }
695
696             // retrieve list of active tournaments
697             $usr_sql = sprintf("
698 SELECT usr.*, guar.login AS guar_login
699      FROM %susers AS usr
700      JOIN %susers AS guar ON guar.code = usr.guar_code
701      WHERE usr.type & (CAST (X'%x' as integer)) = (CAST (X'%x' as integer))
702          AND usr.disa_reas = %d ORDER BY usr.lintm;",
703                                $G_dbpfx, $G_dbpfx,
704                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
705                                USER_DIS_REA_NU_TOBECHK);
706             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
707                 log_crit("stat-day: select from tournaments failed");
708                 break;
709             }
710
711             $usr_n = pg_numrows($usr_pg);
712             $tab_lines = "<tr><th></th><th>User</th><th>EMail</th><th>Guar</th><th>Apprendice</th><th>Date</th></tr>";
713             for ($i = 0 ; $i < $usr_n ; $i++) {
714                 $usr_obj = pg_fetch_object($usr_pg, $i);
715
716                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" %s></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
717                                       $usr_obj->code, ($nocheck ? "" : "CHECKED"),
718                                       eschtml($usr_obj->login), eschtml($usr_obj->email), eschtml($usr_obj->guar_login),
719                                       ($usr_obj->type & USER_FLAG_TY_APPR ? "Yes" : "No"),
720                                       $usr_obj->lintm);
721             }
722             ?>
723 <html>
724 <head>
725 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
726 <title>Brisk: email verified user management.</title>
727      <?php echo "$s_style"; ?>
728 </head>
729      <body>
730      <h2> E-mail verified user management.</h2>
731      <?php if ($status != "") { echo "$status"; } ?>
732      <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
733      <table class="the_tab">
734      <?php
735      echo $tab_lines;
736             ?>
737             </table>
738                   <input type="submit" name="f_accept" value="Accept">
739                   <input type="submit" name="f_delete" value="Delete">
740                   </form>
741                   </body>
742                   </html>
743                   <?php
744                   } while(FALSE);
745         } // else of if ($action ...
746     } // else of if ($do ...
747 }
748
749 main();
750
751 ?>