fix missing bold for your account item
[brisk.git] / web / admin.php
1 <?php
2   /*
3    *  brisk - admin.php
4    *
5    *  Copyright (C) 2011-2015 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 require_once("Obj/brisk.phh");
28 require_once("Obj/dbase_pgsql.phh");
29
30 ini_set("max_execution_time",  "300");
31
32 class ImpPoints
33 {
34     var $time;
35     var $tsess;
36     var $user_sess;
37     var $isauth;
38     var $username;
39     var $useraddr;
40     var $where;
41     var $ttok;
42     var $tidx;
43     var $nplayers;
44     var $logins;
45     var $pts;
46
47     function ImpPoints($s)
48     {
49         $arr = explode('|', $s);
50
51         if (count($arr) != 20) {
52             return FALSE;
53         }
54
55
56         $this->time      = $arr[0];
57         $this->usess     = $arr[1];
58         $this->isauth    = $arr[2];
59         $this->username  = $arr[3];
60         $this->useraddr  = $arr[4];
61         $this->where     = $arr[5];
62         $this->ttok      = $arr[6];
63         $this->tidx      = $arr[7];
64         $this->nplayers  = $arr[8];
65
66         $this->logins = array();
67         $this->pts    = array();
68
69         for ($i = 9 ; $i < 19 ; $i+=2) {
70             $idx = ($i - 9) / 2;
71             $this->logins[$idx] = strtolower($arr[$i]);
72             $this->pts[$idx]    = $arr[$i+1];
73         }
74     }
75 }
76
77 $cont = "";
78
79 function main()
80 {
81     GLOBAL $cont, $G_dbpfx, $G_alarm_passwd, $F_pass_private, $F_ACT, $F_filename;
82
83
84     if (FALSE && $F_pass_private != $G_alarm_passwd) {
85         $cont .= sprintf("Wrong password, operation aborted.<br>\n");
86         return;
87     }
88
89     if ($F_ACT == "append") {
90         do {
91             /*
92               if ($F_pass_private != $G_alarm_passwd) {
93                   $cont .= sprintf("Wrong password, operation aborted.<br>\n");
94                   break;
95               }*/
96             $cont .= sprintf("FILENAME: %s<br>\n", $F_filename); 
97             if (($olddb = new LoginDBOld($F_filename)) == FALSE) {
98                 $cont .= sprintf("Loading failed.<br>\n"); 
99                 break;
100             }
101
102             if (($newdb = BriskDB::create()) == FALSE) {
103                 $cont .= sprintf("Database connection failed.<br>\n"); 
104                 break;
105             }
106             $newdb->users_load();
107             if ($newdb->addusers_from_olddb($olddb, $cont) == FALSE) {
108                 $cont .= sprintf("Insert failed.<br>\n"); 
109             }
110             $cont .= sprintf("<b>SUCCESS</b><br>Item number: %d<br>\n", $olddb->count());
111         } while (0);
112     }
113     else if ($F_ACT == "pointsimp") {
114         do {
115             if (!file_exists($F_filename)) {
116                 $cont .= sprintf("File [%s] not exists.<br>\n", $F_filename); 
117                 break;
118             }
119
120             $cont .= sprintf("FILENAME: %s<br>\n", $F_filename); 
121
122             if (!($fp = @fopen($F_filename, "r"))) {
123                 $cont .= sprintf("Open file [%s] failed.<br>\n", $F_filename); 
124                 break;
125             }
126
127             if (($newdb = BriskDB::create()) == FALSE) {
128                 $cont .= sprintf("Database connection failed.<br>\n"); 
129                 break;
130             }
131
132             $newdb->users_load();
133             $dbconn = $newdb->getdbconn();
134             for ($pts_n = 0 ;  !feof($fp) ; $pts_n++) {
135                 $bf = fgets($fp, 4096);
136                 if ($bf == FALSE)
137                     break;
138
139                 if (($pts = new ImpPoints($bf)) == FALSE) {
140                     $cont .= sprintf("Import failed at line [%s]<br>\n", eschtml($bf));
141                     break;
142                 }
143                 if ($pts->time < 1285884000) {
144                     continue;
145                 }
146                 // else {
147                 //     $cont .= sprintf("ttok: %s<br>\n", $pts->ttok);
148                 // }
149
150                 /*
151                  * matches management
152                  */
153                 $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($pts->ttok));
154                 if (($mtc_pg  = pg_query($dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
155                     // match not exists, insert it
156                     $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
157                                        $G_dbpfx, escsql($pts->ttok), $pts->tidx);
158                     if ( ! (($mtc_pg  = pg_query($dbconn->db(), $mtc_sql)) != FALSE && 
159                             pg_affected_rows($mtc_pg) == 1) ) {
160                         $cont .= sprintf("Matches insert failed at line [%s] [%s]<br>\n", 
161                                          eschtml($bf), eschtml($mtc_sql));
162                         break;                        
163                     }
164
165                 }
166                 $mtc_obj = pg_fetch_object($mtc_pg,0);
167                 // $cont .= sprintf("MTC: %s<br>\n", esclfhtml(print_r($mtc_obj, TRUE)));
168                 // $cont .= sprintf("pts_n: %d  mtc_match_code: %d<br>\n", $pts_n, $mtc_obj->code);
169
170                 /*
171                  * games management
172                  */
173                 $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d and tstamp = to_timestamp(%d);",
174                                    $G_dbpfx, $mtc_obj->code, $pts->time);
175                 if (($gam_pg  = pg_query($dbconn->db(), $gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
176                     // match not exists, insert it
177                     $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp) 
178                                                VALUES (%d, to_timestamp(%d)) RETURNING *;",
179                                        $G_dbpfx, $mtc_obj->code, $pts->time);
180                     if ( ! (($gam_pg  = pg_query($dbconn->db(), $gam_sql)) != FALSE && 
181                             pg_affected_rows($gam_pg) == 1) ) {
182                         $cont .= sprintf("Games insert failed at line [%s] [%s]<br>\n", 
183                                          eschtml($bf), eschtml($gam_sql));
184                         break;                        
185                     }
186                 }
187                 $gam_obj = pg_fetch_object($gam_pg,0);
188                 // $cont .= sprintf("GAM: %s<br>\n", esclfhtml(print_r($gam_obj, TRUE)));
189                 // $cont .= sprintf("pts_n: %d  mtc_match_code: %d<br>\n", $pts_n, $gam_obj->code);
190
191                 /*
192                  * points management
193                  */
194                 for ($i = 0 ; $i < 5 ; $i++) {
195                     /* get the login associated code */
196                     $usr_sql = sprintf("SELECT * FROM %susers WHERE login = '%s';",
197                                        $G_dbpfx, escsql($pts->logins[$i]));
198                     if (($usr_pg  = pg_query($dbconn->db(), $usr_sql)) == FALSE || pg_numrows($usr_pg) != 1) {
199                         $cont .= sprintf("User [%s] not found [%s]<br>\n", eschtml($pts->logins[$i]), eschtml($usr_sql));
200                         save_rej($pts->logins[$i]);
201                         continue;
202                     }
203                     $usr_obj = pg_fetch_object($usr_pg,0);
204
205                     /* put points */
206                     $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts) 
207                                                VALUES (%d, %d, %d) RETURNING *;",
208                                        $G_dbpfx, $gam_obj->code, $usr_obj->code, $pts->pts[$i]);
209                     if ( ! (($pts_pg  = pg_query($dbconn->db(), $pts_sql)) != FALSE && 
210                             pg_affected_rows($pts_pg) == 1) ) {
211                         $cont .= sprintf("Point insert failed at line [%s] [%s] idx: [%d]<br>\n", 
212                                          eschtml($bf), eschtml($gam_sql), $i);
213                         break;
214                     }
215
216                 }
217
218             }
219             fclose($fp);
220         } while (0);
221         $cont .= sprintf("FINE FILE<br>\n");
222     }
223 }
224
225 function save_rej($s)
226 {
227     if (($fp = fopen(LEGAL_PATH."/rej.txt", "a+")) == FALSE)
228         return;
229
230     fwrite($fp, sprintf("%s\n", $s));
231     fclose($fp);
232 }
233
234 main();
235
236 ?>
237 <html>
238 <head>
239 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
240 </head>
241 <body>
242
243 <?php
244     echo "$cont";
245 ?>
246 <b>Append users from a file</b><br>
247 <form accept-charset="utf-8" method="post" action="<?php echo $PHP_SELF;?>">
248       <input type="hidden" name="F_ACT" value="append">
249       <table><tr><td>Admin Password:</td>
250       <td><input name="F_pass_private" type="password" value=""></td></tr>
251       <tr><td>Filename:</td>
252       <td><input type="text" name="F_filename"></td></tr>
253       <tr><td colspan=2><input type="submit" value="append users"></td></tr>
254       </table>
255 </form>
256 <hr>
257 <b>Points importer from file to db</b><br>
258 <form accept-charset="utf-8" method="post" action="<?php echo $PHP_SELF;?>">
259       <input type="hidden" name="F_ACT" value="pointsimp">
260       <table><tr><td>Admin Password:</td>
261       <td><input name="F_pass_private" type="password" value=""></td></tr>
262       <tr><td>Filename:</td>
263       <td><input type="text" name="F_filename"></td></tr>
264       <tr><td colspan=2><input type="submit" value="import points"></td></tr>
265       </table>
266 </form>
267
268
269 </body>
270 </html>