5 * Copyright (C) 2011 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
27 require_once("Obj/brisk.phh");
28 require_once("Obj/dbase_pgsql.phh");
30 ini_set("max_execution_time", "300");
47 function ImpPoints($s)
50 $arr = explode('|', $s);
51 // error_log("TTOK: ".count($arr), 0);
53 if (count($arr) != 20) {
54 error_log("COUNT: ".count($arr));
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];
69 $this->logins = array();
72 for ($i = 9 ; $i < 19 ; $i+=2) {
74 $this->logins[$idx] = strtolower($arr[$i]);
75 $this->pts[$idx] = $arr[$i+1];
84 GLOBAL $cont, $G_dbpfx, $G_alarm_passwd, $F_pass_private, $F_ACT, $F_filename;
87 if (FALSE && $F_pass_private != $G_alarm_passwd) {
88 $cont .= sprintf("Wrong password, operation aborted.<br>\n");
92 if ($F_ACT == "append") {
95 if ($F_pass_private != $G_alarm_passwd) {
96 $cont .= sprintf("Wrong password, operation aborted.<br>\n");
99 $cont .= sprintf("FILENAME: %s<br>\n", $F_filename);
100 if (($olddb = new LoginDBOld($F_filename)) == FALSE) {
101 $cont .= sprintf("Loading failed.<br>\n");
105 // FIXME: now create can return FALSE
106 $newdb = BriskDB::create();
107 $newdb->users_load();
108 if ($newdb->addusers_from_olddb($olddb, $cont) == FALSE) {
109 $cont .= sprintf("Insert failed.<br>\n");
111 $cont .= sprintf("<b>SUCCESS</b><br>Item number: %d<br>\n", $olddb->count());
114 else if ($F_ACT == "pointsimp") {
116 if (!file_exists($F_filename)) {
117 $cont .= sprintf("File [%s] not exists.<br>\n", $F_filename);
121 $cont .= sprintf("FILENAME: %s<br>\n", $F_filename);
123 if (!($fp = @fopen($F_filename, "r"))) {
124 $cont .= sprintf("Open file [%s] failed.<br>\n", $F_filename);
128 // FIXME: now create can return FALSE
129 $newdb = BriskDB::create();
130 $newdb->users_load();
131 $dbconn = $newdb->getdbconn();
132 for ($pts_n = 0 ; !feof($fp) ; $pts_n++) {
133 $bf = fgets($fp, 4096);
137 if (($pts = new ImpPoints($bf)) == FALSE) {
138 $cont .= sprintf("Import failed at line [%s]<br>\n", eschtml($bf));
141 if ($pts->time < 1285884000) {
145 // $cont .= sprintf("ttok: %s<br>\n", $pts->ttok);
151 $mtc_sql = sprintf("SELECT * FROM %sbin5_matches WHERE ttok = '%s';", $G_dbpfx, escsql($pts->ttok));
152 if (($mtc_pg = pg_query($dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
153 // match not exists, insert it
154 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx) VALUES ('%s', %d) RETURNING *;",
155 $G_dbpfx, escsql($pts->ttok), $pts->tidx);
156 if ( ! (($mtc_pg = pg_query($dbconn->db(), $mtc_sql)) != FALSE &&
157 pg_affected_rows($mtc_pg) == 1) ) {
158 $cont .= sprintf("Matches insert failed at line [%s] [%s]<br>\n",
159 eschtml($bf), eschtml($mtc_sql));
164 $mtc_obj = pg_fetch_object($mtc_pg,0);
165 // $cont .= sprintf("MTC: %s<br>\n", esclfhtml(print_r($mtc_obj, TRUE)));
166 // $cont .= sprintf("pts_n: %d mtc_match_code: %d<br>\n", $pts_n, $mtc_obj->code);
171 $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d and tstamp = to_timestamp(%d);",
172 $G_dbpfx, $mtc_obj->code, $pts->time);
173 if (($gam_pg = pg_query($dbconn->db(), $gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
174 // match not exists, insert it
175 $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp)
176 VALUES (%d, to_timestamp(%d)) RETURNING *;",
177 $G_dbpfx, $mtc_obj->code, $pts->time);
178 if ( ! (($gam_pg = pg_query($dbconn->db(), $gam_sql)) != FALSE &&
179 pg_affected_rows($gam_pg) == 1) ) {
180 $cont .= sprintf("Games insert failed at line [%s] [%s]<br>\n",
181 eschtml($bf), eschtml($gam_sql));
185 $gam_obj = pg_fetch_object($gam_pg,0);
186 // $cont .= sprintf("GAM: %s<br>\n", esclfhtml(print_r($gam_obj, TRUE)));
187 // $cont .= sprintf("pts_n: %d mtc_match_code: %d<br>\n", $pts_n, $gam_obj->code);
192 for ($i = 0 ; $i < 5 ; $i++) {
193 /* get the login associated code */
194 $usr_sql = sprintf("SELECT * FROM %susers WHERE login = '%s';",
195 $G_dbpfx, escsql($pts->logins[$i]));
196 if (($usr_pg = pg_query($dbconn->db(), $usr_sql)) == FALSE || pg_numrows($usr_pg) != 1) {
197 $cont .= sprintf("User [%s] not found [%s]<br>\n", eschtml($pts->logins[$i]), eschtml($usr_sql));
198 save_rej($pts->logins[$i]);
201 $usr_obj = pg_fetch_object($usr_pg,0);
204 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
205 VALUES (%d, %d, %d) RETURNING *;",
206 $G_dbpfx, $gam_obj->code, $usr_obj->code, $pts->pts[$i]);
207 if ( ! (($pts_pg = pg_query($dbconn->db(), $pts_sql)) != FALSE &&
208 pg_affected_rows($pts_pg) == 1) ) {
209 $cont .= sprintf("Point insert failed at line [%s] [%s] idx: [%d]<br>\n",
210 eschtml($bf), eschtml($gam_sql), $i);
219 $cont .= sprintf("FINE FILE<br>\n");
223 function save_rej($s)
225 if (($fp = fopen(LEGAL_PATH."/rej.txt", "a+")) == FALSE)
228 fwrite($fp, sprintf("%s\n", $s));
238 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
245 <b>Append users from a file</b><br>
246 <form accept-charset="utf-8" method="post" action="<?php echo $PHP_SELF;?>">
247 <input type="hidden" name="F_ACT" value="append">
248 <table><tr><td>Admin Password:</td>
249 <td><input name="F_pass_private" type="password" value=""></td></tr>
250 <tr><td>Filename:</td>
251 <td><input type="text" name="F_filename"></td></tr>
252 <tr><td colspan=2><input type="submit" value="append users"></td></tr>
256 <b>Points importer from file to db</b><br>
257 <form accept-charset="utf-8" method="post" action="<?php echo $PHP_SELF;?>">
258 <input type="hidden" name="F_ACT" value="pointsimp">
259 <table><tr><td>Admin Password:</td>
260 <td><input name="F_pass_private" type="password" value=""></td></tr>
261 <tr><td>Filename:</td>
262 <td><input type="text" name="F_filename"></td></tr>
263 <tr><td colspan=2><input type="submit" value="import points"></td></tr>