old guaranties management and date management of new guaranteed added
[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 require_once($G_base."index_wr.php");
55
56 function check_auth()
57 {
58     GLOBAL $G_alarm_passwd, $sess, $_POST, $_SERVER;
59
60     $socket = FALSE;
61     $ret = FALSE;
62     $ip = $_SERVER["REMOTE_ADDR"];
63     $stp = 0;
64     $private = md5($G_alarm_passwd.$ip.$sess);
65     $cmd = array ("cmd" => "userauth", "sess" => $sess, "private" => $private, "the_end" => "true");
66     $cmd_ser = cmd_serialize($cmd);
67     $cmd_len = mb_strlen($cmd_ser, "ASCII");
68
69     do {
70         if (($socket = stream_socket_client("unix://".USOCK_PATH."2")) == FALSE)
71             break;
72         $stp = 1;
73         if (($rwr = fwrite($socket, $cmd_ser, $cmd_len)) == FALSE
74             || $rwr != $cmd_len)
75             break;
76         fflush($socket);
77         $stp = 2;
78         if (($buf = fread($socket, 4096)) == FALSE)
79             break;
80         $res = cmd_deserialize($buf);
81         $stp = 3;
82         if (!isset($res['val']) || $res['val'] != 200)
83             break;
84         $ret = TRUE;
85         $stp = 4;
86     } while (0);
87     if ($socket != FALSE)
88         fclose($socket);
89
90     if ($stp < 4) {
91         echo "STP: $stp<br>";
92     }
93     return ($ret);
94 }
95
96 function main() {
97     GLOBAL $G_dbpfx, $G_lang, $G_alarm_passwd, $G_domain, $G_webbase;
98     GLOBAL $mlang_umgmt, $mlang_indwr, $f_mailusers, $sess, $_POST, $_SERVER;
99
100     $curtime = time();
101     $status = "";
102
103     if (check_auth() == FALSE) {
104         echo "Authentication failed";
105         exit;
106     }
107
108     if (isset($_GET['do']) && $_GET['do'] == 'newuser') {
109         if (isset($_POST['f_accept'])) {
110             $action = "accept";
111         }
112         else if (isset($_POST['f_delete'])) {
113             $action = "delete";
114         }
115         else {
116             $action = "show";
117         }
118
119         if ($action == "accept") {
120             foreach($_POST as $key => $value) {
121                 if (substr($key, 0, 9) != "f_newuser")
122                     continue;
123
124                 $id = (int)substr($key, 9);
125                 if ($id <= 0)
126                     continue;
127
128                 // check existence of username or email
129                 $is_trans = FALSE;
130                 $res = FALSE;
131                 do {
132                     if (($bdb = BriskDB::create()) == FALSE)
133                         break;
134
135                     // retrieve list added users
136                     $usr_sql = sprintf("
137 SELECT usr.*, guar.login AS guar_login
138      FROM %susers AS usr
139      JOIN %susers AS guar ON guar.code = usr.guar_code
140      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
141          AND usr.disa_reas = %d AND usr.code = %d;",
142                                $G_dbpfx, $G_dbpfx,
143                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
144                                USER_DIS_REA_NU_ADDED, $id);
145                     if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
146                         log_crit("stat-day: select from tournaments failed");
147                         break;
148                     }
149                     $usr_n = pg_numrows($usr_pg);
150                     if ($usr_n != 1) {
151                         $status .= sprintf("Inconsistency for code %d, returned %d records, skipped.<br>",
152                                           $id, $usr_n);
153                         break;
154                     }
155
156                     $usr_obj = pg_fetch_object($usr_pg, 0);
157
158                     $bdb->transaction('BEGIN');
159                     $is_trans = TRUE;
160
161
162                     if (($bdb->user_update_flag_ty($usr_obj->code,
163                                                    USER_FLAG_TY_DISABLE, USER_DIS_REA_NU_ADDED,
164                                                    USER_FLAG_TY_DISABLE, USER_DIS_REA_NU_MAILED)) == FALSE) {
165                         echo "fail 2<br>";
166                         break;
167                     }
168
169                     if (($mail_code = $bdb->mail_reserve_code()) == FALSE) {
170                         fprintf(STDERR, "ERROR: mail reserve code FAILED\n");
171                         break;
172                     }
173                     $hash = md5($curtime . $G_alarm_passwd . $usr_obj->login . $usr_obj->email);
174
175                     $confirm_page = sprintf("http://%s/%s/mailmgr.php?f_act=checkmail&f_code=%d&f_hash=%s",
176                                             $G_domain, $G_webbase, $mail_code, $hash);
177                     $subj = $mlang_indwr['nu_msubj'][$G_lang];
178                     $body_txt = sprintf($mlang_indwr['nu_mtext'][$G_lang],
179                                         $usr_obj->guar_login, $usr_obj->login, $confirm_page);
180                     $body_htm = sprintf($mlang_indwr['nu_mhtml'][$G_lang],
181                                         $usr_obj->guar_login, $usr_obj->login, $confirm_page);
182
183                     $mail_item = new MailDBItem($mail_code, $usr_obj->code, MAIL_TYP_CHECK,
184                                                 $curtime, $subj, $body_txt, $body_htm, $hash);
185
186                     if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
187                         // mail error
188                         fprintf(STDERR, "ERROR: mail send FAILED\n");
189                         break;
190                     }
191                     // save the mail
192                     if ($mail_item->store($bdb) == FALSE) {
193                         // store mail error
194                         fprintf(STDERR, "ERROR: store mail FAILED\n");
195                         break;
196                     }
197                     $status .= sprintf("status change for %s: SUCCESS<br>", $usr_obj->login);
198                     $bdb->transaction('COMMIT');
199                     $res = TRUE;
200                 } while(FALSE);
201                 if ($res == FALSE) {
202                     $status .= sprintf("Error occurred during accept action<br>");
203                     if ($is_trans)
204                         $bdb->transaction('ROLLBACK');
205                     break;
206                 }
207             }
208         }
209
210         do {
211             if (($bdb = BriskDB::create()) == FALSE) {
212                 log_crit("stat-day: database connection failed");
213                 break;
214             }
215
216             // retrieve list added users
217             $usr_sql = sprintf("
218 SELECT usr.*, guar.login AS guar_login
219      FROM %susers AS usr
220      JOIN %susers AS guar ON guar.code = usr.guar_code
221      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
222          AND usr.disa_reas = %d;",
223                                $G_dbpfx, $G_dbpfx,
224                                USER_FLAG_TY_DISABLE, USER_FLAG_TY_DISABLE,
225                                USER_DIS_REA_NU_ADDED);
226             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
227                 log_crit("stat-day: select from tournaments failed");
228                 break;
229             }
230             $usr_n = pg_numrows($usr_pg);
231             $tab_lines = "";
232             for ($i = 0 ; $i < $usr_n ; $i++) {
233                 $usr_obj = pg_fetch_object($usr_pg, $i);
234
235                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" CHECKED></td><td>%s</td><td></td></tr>\n",
236                                       $usr_obj->code, eschtml($usr_obj->login), eschtml($usr_obj->guar_login));
237             }
238             ?>
239 <html>
240 <body>
241 <h2> New imported users management.</h2>
242      <?php if ($status != "") { echo "$status"; } ?>
243 <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
244 <table>
245 <?php
246      echo $tab_lines;
247 ?>
248 </table>
249 <input type="submit" name="f_accept" value="Newuser Accept">
250 <input type="submit" name="f_delete" value="Newuser Delete">
251 </form>
252 </body>
253 </html>
254 <?php
255            exit;
256         } while(FALSE);
257         printf("Some error occurred during newuser visualization\n");
258         exit;
259     }
260     else { // if ($_GET['do'] ...
261         if (isset($_POST['f_accept'])) {
262             $action = "accept";
263         }
264         else if (isset($_POST['f_delete'])) {
265             $action = "delete";
266         }
267         else {
268             $action = "show";
269         }
270
271         if ($action == "accept") {
272             if (($bdb = BriskDB::create()) == FALSE) {
273                 log_crit("stat-day: database connection failed");
274                 break;
275             }
276
277             foreach($_POST as $key => $value) {
278                 if (substr($key, 0, 9) != "f_newuser")
279                     continue;
280
281                 $id = (int)substr($key, 9);
282                 if ($id <= 0)
283                     continue;
284
285
286                 // retrieve list of active tournaments
287                 $usr_sql = sprintf("
288 SELECT usr.*, guar.login AS guar_login
289      FROM %susers AS usr
290      JOIN %susers AS guar ON guar.code = usr.guar_code
291      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
292          AND usr.disa_reas = %d AND usr.code = %d;",
293                                    $G_dbpfx, $G_dbpfx,
294                                    USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
295                                    USER_DIS_REA_NU_TOBECHK, $id);
296                 if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
297                     log_crit("stat-day: select from tournaments failed");
298                     break;
299                 }
300                 $usr_obj = pg_fetch_object($usr_pg, 0);
301
302                 printf("KEY: %s: %s %s<br>\n", $id, $value, $usr_obj->login);
303                 // change state
304                 $passwd = passwd_gen();
305
306                 if (($bdb->user_update_passwd($usr_obj->code, $passwd)) == FALSE) {
307                     echo "fail 1.5<br>";
308                     break;
309                 }
310
311                 if (($bdb->user_update_flag_ty($usr_obj->code,
312                                                USER_FLAG_TY_DISABLE, USER_DIS_REA_NU_TOBECHK,
313                                                USER_FLAG_TY_NORM, USER_DIS_REA_NONE)) == FALSE) {
314                     echo "fail 2<br>";
315                     break;
316                 }
317
318                 $bdb->user_update_login_time($usr_obj->code, 0);
319
320                 // send mail
321                 $subj = $mlang_umgmt['nu_psubj'][$G_lang];
322                 $body_txt = sprintf($mlang_umgmt['nu_ptext'][$G_lang],
323                                     $usr_obj->login, $passwd);
324                 $body_htm = sprintf($mlang_umgmt['nu_phtml'][$G_lang],
325                                     $usr_obj->login, $passwd);
326
327                 if (brisk_mail($usr_obj->email, $subj, $body_txt, $body_htm) == FALSE) {
328                     // mail error
329                     fprintf(STDERR, "ERROR: mail send FAILED\n");
330                     break;
331                 }
332             }
333             exit;
334         }
335         else {
336             do {
337             if (($bdb = BriskDB::create()) == FALSE) {
338                 log_crit("stat-day: database connection failed");
339                 break;
340             }
341
342             // retrieve list of active tournaments
343             $usr_sql = sprintf("
344 SELECT usr.*, guar.login AS guar_login
345      FROM %susers AS usr
346      JOIN %susers AS guar ON guar.code = usr.guar_code
347      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
348          AND usr.disa_reas = %d;",
349                                $G_dbpfx, $G_dbpfx,
350                                USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
351                                USER_DIS_REA_NU_TOBECHK);
352             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
353                 log_crit("stat-day: select from tournaments failed");
354                 break;
355             }
356
357             $usr_n = pg_numrows($usr_pg);
358             $tab_lines = "";
359             for ($i = 0 ; $i < $usr_n ; $i++) {
360                 $usr_obj = pg_fetch_object($usr_pg, $i);
361
362                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" CHECKED></td><td>%s</td><td></td></tr>\n",
363                                       $usr_obj->code, eschtml($usr_obj->login), eschtml($usr_obj->guar_login));
364             }
365             ?>
366 <html>
367      <body>
368      <h2> E-mail verified user management.</h2>
369      <?php if ($status != "") { echo "$status"; } ?>
370      <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
371      <table>
372      <?php
373      echo $tab_lines;
374             ?>
375             </table>
376                   <input type="submit" name="f_accept" value="Accept">
377                   <input type="submit" name="f_delete" value="Delete">
378                   </form>
379                   </body>
380                   </html>
381                   <?php
382                   } while(FALSE);
383         } // else of if ($action ...
384     } // else of if ($do ...
385 }
386
387 main();
388
389 ?>