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