add cmd unix socket with 'userauth' command implemented, incomplete usermgmt page...
[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
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         if (($rwr = fwrite($socket, $cmd_ser, $cmd_len)) == FALSE
63             || $rwr != $cmd_len)
64             break;
65         fflush($socket);
66         if (($buf = fread($socket, 4096)) == FALSE)
67             break;
68         $res = cmd_deserialize($buf);
69         if (!isset($res['val']) || $res['val'] != 200)
70             break;
71         $ret = TRUE;
72     } while (0);
73     if ($socket != FALSE)
74         fclose($socket);
75         
76     return ($ret);
77 }
78
79 function main($action) {
80     GLOBAL $G_dbpfx, $G_alarm_passwd, $f_mailusers, $sess, $_POST, $_SERVER;
81
82     if (check_auth() == FALSE) {
83         echo "Authentication failed";
84         exit;
85     }
86
87     if (isset($f_mailusers)) {
88         $action = "listnew";
89     }
90
91     if ($action == "listnew") {
92         echo "pippo";
93     }
94     else {
95         do {
96             
97             if (($bdb = BriskDB::create()) == FALSE) {
98                 log_crit("stat-day: database connection failed");
99                 break;
100             }
101             
102             // retrieve list of active tournaments
103             $usr_sql = sprintf("
104 SELECT usr.*, guar.login AS guar_login 
105      FROM %susers AS usr 
106      JOIN %susers AS guar ON guar.code = usr.guar_code 
107      WHERE ( (usr.type & (CAST (X'%x' as integer))) = (CAST (X'%x' as integer)) )
108          AND usr.disa_reas = %d;", 
109                                $G_dbpfx, $G_dbpfx,
110                                USER_FLAG_TY_ALL, USER_FLAG_TY_DISABLE,
111                                USER_DIS_REA_NU_TOBECHK);
112             if (($usr_pg = pg_query($bdb->dbconn->db(), $usr_sql)) == FALSE) {
113                 log_crit("stat-day: select from tournaments failed");
114                 break;
115             }
116             
117             $usr_n = pg_numrows($usr_pg);
118             printf("Number of tournaments: %d\n", $usr_n);
119
120             $tab_lines = "";
121             // loop on tournaments
122             for ($i = 0 ; $i < $usr_n ; $i++) {
123                 // log_crit("stat-day: LOOP i");
124                 $usr_obj = pg_fetch_object($usr_pg, $i);
125                 
126                 $tab_lines .= sprintf("<tr><td><input name=\"f_newuser%d\" type=\"checkbox\" CHECKED></td><td>%s</td><td></td></tr>\n",
127                                       $usr_obj->code, eschtml($usr_obj->login), eschtml($usr_obj->guar_login));
128             }
129             ?>
130 <html>
131 <body>
132 <form action="<?php echo "$PHP_SELF"; ?>" method="POST">
133 <table>
134 <?php
135      echo $tab_lines;
136 ?>
137 </table>
138 <input type="submit" name="f_mailusers" value="Done">
139 </form>
140 </body>
141 </html>
142 <?php
143       
144         } while(FALSE);
145     }
146 }
147
148 if (!isset($f_action)) {
149     $action = FALSE;
150 }
151
152 main($action);
153
154
155 ?>