Merge branch 'master' into auto-maintenance
[brisk.git] / web / Obj / dbase_pgsql.phh
1 <?php
2   /*
3    *  brisk - dbase_pgsql.phh
4    *
5    *  Copyright (C) 2006-2012 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 require_once("${G_base}Obj/dbase_base.phh");
26
27 $escsql_from = array( "\\",   "'"   );
28 $escsql_to   = array( "\\\\", "\\'" );
29
30 function escsql($s)
31 {
32     GLOBAL $escsql_from, $escsql_to;
33
34     return str_replace($escsql_from, $escsql_to, $s);
35 }
36
37 class DBConn
38 {
39     static $dbcnnx = FALSE;
40     var $db = FALSE;
41
42     function DBConn()
43     {
44         $this->db = DBConn::$dbcnnx;
45     }
46
47     static function create()
48     {
49         GLOBAL $G_dbauth;
50
51         if (DBConn::$dbcnnx == FALSE) {
52             if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth, PGSQL_CONNECT_FORCE_NEW))) {
53                 return (FALSE);
54             }
55         }
56
57         $out = new DBConn();
58
59         return $out;
60     }
61
62     static function destroy()
63     {
64         if (DBConn::$dbcnnx != FALSE) {
65             DBConn::$dbcnnx = FALSE;
66             return (pg_close(DBConn::$dbcnnx));
67         }
68         return TRUE;
69     }
70
71     static function recover()
72     {
73         self::destroy();
74         return (self::create());
75     }
76
77     function db()
78     {
79         return ($this->db);
80     }
81 }
82
83 class BriskDB
84 {
85     var $dbconn;
86     var $item;
87     var $item_n;
88
89     function BriskDB($dbconn)
90     {
91         $this->dbconn = $dbconn;
92     }
93
94     static function create()
95     {
96         GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
97
98         $ret = FALSE;
99
100         log_main("BriskDB create:start");
101
102         do {
103             if (($dbconn = DBConn::create()) == FALSE) {
104                 break;
105             }
106
107             $ret = new BriskDB($dbconn);
108         } while (0);
109
110         return ($ret);
111     }
112
113     function query($sql)
114     {
115         if (($res = pg_query($this->dbconn->db(), $sql)) == FALSE) {
116             // try to recover the connection
117             if (($this->dbconn = DBConn::recover()) == FALSE)
118                 return FALSE;
119             return (pg_query($this->dbconn->db(), $sql));
120         }
121
122         return ($res);
123     }
124
125     function users_load()
126     {
127     }
128
129     function login_exists($login)
130     {
131         GLOBAL $G_dbpfx;
132
133         /* check the existence of the nick in the BriskDB */
134         log_main("login_exists: ".$login);
135
136         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;",
137                             $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
138         if (($user_pg = $this->query($user_sql)) != FALSE)
139             if (pg_numrows($user_pg) == 1)
140                 return TRUE;
141
142         return FALSE;
143     }
144
145     function getrecord_bylogin($login) {
146         GLOBAL $G_dbpfx;
147
148         $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s') AND (type & CAST (X'%08x' as integer)) = 0;",  $G_dbpfx, escsql($login), USER_FLAG_TY_DISABLE);
149         if (($user_pg  = $this->query($user_sql)) == FALSE) {
150             return FALSE;
151         }
152         if (pg_numrows($user_pg) != 1)
153             return FALSE;
154
155         $user_obj = pg_fetch_object($user_pg, 0);
156
157         return ($user_obj);
158     }
159
160     function getrecord_bycode($code) {
161         GLOBAL $G_dbpfx;
162
163         $user_sql = sprintf("SELECT * FROM %susers WHERE code = %d;",  $G_dbpfx, $code);
164         if (($user_pg  = $this->query($user_sql)) == FALSE) {
165             return FALSE;
166         }
167         if (pg_numrows($user_pg) != 1)
168             return FALSE;
169
170         $user_obj = pg_fetch_object($user_pg, 0);
171
172         return ($user_obj);
173     }
174
175     function user_update_login_time($code, $lintm)
176     {
177         GLOBAL $G_dbpfx;
178
179         $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
180
181         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
182              return FALSE;
183         }
184
185         return TRUE;
186     }
187
188     function user_prefs_update($code, $flags, $supp_comp)
189     {
190         GLOBAL $G_dbpfx;
191
192         $user_sql = sprintf("UPDATE %susers SET (type, supp_comp) = (%d, '%s') WHERE code = %d;",
193                             $G_dbpfx, $flags, escsql($supp_comp), $code);
194         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
195         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
196              return FALSE;
197         }
198         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
199
200         return TRUE;
201     }
202
203     function user_state_update($code, $flags, $disa_reas)
204     {
205         GLOBAL $G_dbpfx;
206
207         $user_sql = sprintf("UPDATE %susers SET (type, disa_reas) = (%d, %d) WHERE code = %d;",
208                             $G_dbpfx, $flags, $disa_reas, $code);
209         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
210         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
211              return FALSE;
212         }
213         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
214
215         return TRUE;
216     }
217
218     function user_tos_update($code, $tos_vers)
219     {
220         GLOBAL $G_dbpfx;
221
222         $user_sql = sprintf("UPDATE %susers SET (tos_vers) = ('%s') WHERE code = %d;",
223                             $G_dbpfx, escsql($tos_vers), $code);
224         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
225         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
226              return FALSE;
227         }
228         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
229
230         return TRUE;
231     }
232
233     /*
234       if success return a LoginDBItem object
235      */
236     function login_verify($login, $pass)
237     {
238         GLOBAL $G_dbpfx;
239
240         $ret = FALSE;
241
242         log_main("login_verify: ".$login);
243
244         //O /* check the existence of the nick in the BriskDB */
245         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
246         //O log_main("login_verify: BEGIN");
247
248         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
249             return FALSE;
250         }
251
252         log_main("login[".$user_obj->code."]: ".$user_obj->login);
253
254         /* if it exists check for a valid challenge */
255         if (($a_sem = Challenges::lock_data(TRUE)) != FALSE) {
256             if (($chals = &Challenges::load_data()) != FALSE) {
257                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
258                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
259                     if (strcmp($login, $chals->item[$e]->login) == 0) {
260                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
261
262                         if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
263                             log_main("login_verify SUCCESS for ".$login);
264
265                             $chals->rem($login);
266                             $this->user_update_login_time($user_obj->code, time());
267                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
268                             break;
269                         }
270                     }
271                 } // end for ($e = 0 ...
272             }
273
274             if ($chals->ismod()) {
275                 Challenges::save_data(&$chals);
276             }
277
278             Challenges::unlock_data($a_sem);
279         }
280         //O break;
281         // O } //  if (strcasecmp($this->item[$i]->login, ...
282         //O }
283
284         return ($ret);
285     }
286
287     function getitem_bylogin($login, &$id) {
288         $ret = FALSE;
289         $id = -1;
290
291         log_main("getitem_bylogin: ".$login);
292
293         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
294             return $ret;
295
296         $id = $user_obj->code;
297         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
298     }
299
300     function getitem_bycode($code) {
301         $ret = FALSE;
302
303         log_main("getitem_bycode: ".$code);
304
305         if (($user_obj = $this->getrecord_bycode($code)) == FALSE)
306             return $ret;
307
308         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
309     }
310
311     // TODO FOR DB
312     function getmail($login)
313     {
314         log_main("getmail");
315
316         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
317             return FALSE;
318
319         return ($ret->email);
320     }
321
322     function addusers_from_olddb($olddb, &$cont)
323     {
324         GLOBAL $G_dbpfx;
325
326         for ($i = 0 ; $i < $olddb->count() ; $i++) {
327             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
328                                 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
329                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
330
331             if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
332                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
333
334                 return FALSE;
335             }
336         }
337         return TRUE;
338     }
339
340     function getdbconn()
341     {
342         return ($this->dbconn);
343     }
344
345     // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position
346     function users_get($match_code, $with_minmaxtidx, $is_newmatch)
347     {
348         GLOBAL $G_dbpfx;
349
350         if ($is_newmatch) { // is new
351             $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
352                                   FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p,
353                                        %susers AS u, %sbin5_table_orders AS o
354                                   WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
355                                        AND m.code = o.mcode AND u.code = o.ucode AND m.code = %d
356                                   GROUP BY u.code, u.login%s, o.pos
357                                   ORDER BY o.pos;",
358                                ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
359                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
360                                ($with_minmaxtidx ? ", m.tidx" : ""));
361         }
362         else { // is old
363             $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
364                                   FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
365                                   WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d
366                                   GROUP BY u.code, u.login%s;",
367                                ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
368                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
369                                ($with_minmaxtidx ? ", m.tidx" : ""));
370         }
371
372         if (($usr_pg  = pg_query($this->dbconn->db(), $usr_sql)) == FALSE ) {
373             log_crit(sprintf("%s::%s: pg_query usr_sql failed [%s]", __CLASS__, __FUNCTION__, $usr_sql));
374             return (FALSE);
375         }
376         $usr_n = pg_numrows($usr_pg);
377         if ($usr_n != BIN5_PLAYERS_N) {
378             log_crit(sprintf("%s::%s: wrong number of players [%s] %d", __CLASS__, __FUNCTION__, $usr_sql, $usr_n));
379             return (FALSE);
380         }
381         $users = array();
382
383         if ($with_minmaxtidx)
384             $fields = array('code', 'login', 'first', 'last', 'tidx');
385         else
386             $fields = array('code', 'login');
387
388         for ($u = 0 ; $u < $usr_n ; $u++) {
389             $usr_obj = pg_fetch_object($usr_pg, $u);
390             $users[$u] = array();
391             foreach($fields as $field) {
392                 $users[$u][$field] = $usr_obj->$field;
393             }
394         }
395         return ($users);
396     }
397
398     // out: tab->{points,points_n,old_reason}, in: tab->ttok
399     function match_continue($match_code, $table, $tidx)
400     {
401         GLOBAL $G_dbpfx;
402         $sql_ttok = escsql($table->table_token);
403
404         if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) {
405             log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__));
406             return (FALSE);
407         }
408
409         $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code);
410         if (($num_pg  = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) {
411             log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__));
412             return (FALSE);
413         }
414         $num_obj = pg_fetch_object($num_pg, 0);
415         $table->points_n = $num_obj->points_n;
416
417         // TAG: POINTS_MANAGEMENT
418         $tot_sql = sprintf("SELECT sum(p.pts * (2^g.mult)) AS pts
419                             FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u,
420                                  %sbin5_table_orders AS o
421                             WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code
422                                   AND p.ucode = o.ucode AND g.mcode = o.mcode
423                             GROUP BY p.ucode, o.pos
424                             ORDER BY o.pos;",
425                            $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code);
426         if (($tot_pg  = pg_query($this->dbconn->db(), $tot_sql)) == FALSE
427             || pg_numrows($tot_pg) != BIN5_PLAYERS_N) {
428             log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__));
429             return(FALSE);
430         }
431
432         $u = 0;
433         foreach ($users as $user) {
434             // TAG: POINTS_MANAGEMENT
435             $pts_sql = sprintf("SELECT p.pts AS pts, g.mult AS mult
436                                     FROM %sbin5_points as p, %sbin5_games as g
437                                     WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d
438                                     ORDER BY g.tstamp ASC
439                                     LIMIT %d OFFSET %d;",
440                                $G_dbpfx, $G_dbpfx, $match_code, $user['code'],
441                                MAX_POINTS,
442                                ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS));
443
444             // points of the match for each user
445             if (($pts_pg  = $this->query($pts_sql)) == FALSE) {
446                 log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__));
447                 return (FALSE);
448             }
449             $pts_n = pg_numrows($pts_pg);
450             if ($pts_n > $table->points_n) {
451                 // inconsistent scenario number of points great than number of games
452                 log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__));
453                 return (FALSE);
454             }
455             // TAG: POINTS_MANAGEMENT
456             for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) {
457                 $pts_obj = pg_fetch_object($pts_pg, $i);
458                 $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts * pow(2, $pts_obj->mult);
459             }
460             $tot_obj = pg_fetch_object($tot_pg, $u);
461             $table->total[$u] = $tot_obj->pts;
462
463             $u++;
464         }
465
466         $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY tstamp DESC LIMIT 1;", $G_dbpfx, $match_code);
467         if (($gam_pg  = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
468             log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__));
469             return (FALSE);
470         }
471         $gam_obj = pg_fetch_object($gam_pg, 0);
472
473         $table->old_reason = game_description($gam_obj->act, 'html', $gam_obj->mult,
474                                               $gam_obj->asta_win, ($gam_obj->asta_win != -1 ?
475                                                                    $users[$gam_obj->asta_win]['login'] : ""),
476                                               $gam_obj->friend, ($gam_obj->friend != -1 ?
477                                                                  $users[$gam_obj->friend]['login'] : ""),
478                                               $gam_obj->pnt, $gam_obj->asta_pnt);
479
480         // update matches with new ttok and table idx
481         $mtc_sql = sprintf("UPDATE %sbin5_matches SET (ttok, tidx) = ('%s', %d) WHERE code = %d RETURNING *;",
482                            $G_dbpfx, $sql_ttok, $tidx, $match_code);
483         if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
484             log_crit(sprintf("%s::%s: update matches table failed", __CLASS__, __FUNCTION__));
485             return (FALSE);
486         }
487
488         return (TRUE);
489     }
490
491     function match_order_get(&$match_data, $match_code, $exp_num)
492     {
493         GLOBAL $G_dbpfx;
494
495         $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;",
496                            $G_dbpfx, $match_code);
497
498         if (($ord_pg  = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) {
499             log_crit(sprintf("%s: fails for id or users number", __FUNCTION__));
500             return (FALSE);
501         }
502
503         $ucodes = array();
504         for ($i = 0 ; $i < $exp_num ; $i++) {
505             $ord_obj = pg_fetch_object($ord_pg, $i);
506             $ucodes[$i] = $ord_obj->ucode;
507         }
508
509         if ($match_data !== NULL) {
510             $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;",
511                                 $G_dbpfx, $match_code);
512
513             if (($mtdt_pg  = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) {
514                 log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code));
515                 return (FALSE);
516             }
517
518             $mtdt_obj = pg_fetch_object($mtdt_pg, 0);
519
520             foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) {
521                 $match_data[$match_name] = $mtdt_obj->$match_name;
522             }
523         }
524
525         return ($ucodes);
526     }
527
528     //   ttok   text UNIQUE,
529     //   tidx
530     function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts)
531     {
532         GLOBAL $G_dbpfx;
533         $sql_ttok = escsql($table->table_token);
534
535         $is_trans = FALSE;
536         $ret = FALSE;
537
538         $n = count($ucodes);
539         /* check the existence of the nick in the BriskDB */
540         log_main("bin5_points_save: ");
541
542         do {
543             if ($this->query("BEGIN") == FALSE) {
544                 break;
545             }
546             $is_trans = TRUE;
547
548             /*
549              * matches management
550              */
551             $mtc_sql = sprintf("UPDATE %sbin5_matches SET (mazzo_next, mult_next) = (%d, %d) WHERE ttok = '%s' RETURNING *;",
552                                $G_dbpfx, $table->mazzo, $table->mult, $sql_ttok);
553             if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
554
555                 // match not exists, insert it
556                 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx, mazzo_next, mult_next) VALUES ('%s', %d, %d, %d) RETURNING *;",
557                                    $G_dbpfx, $sql_ttok, $tidx, $table->mazzo, $table->mult);
558                 if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_affected_rows($mtc_pg) != 1) {
559                     log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
560                     break;
561                 }
562                 $mtc_obj = pg_fetch_object($mtc_pg, 0);
563
564                 for ($i = 0 ; $i < $n ; $i++) {
565                     $ord_sql = sprintf("INSERT INTO %sbin5_table_orders (mcode, ucode, pos) VALUES (%d, %d, %d);",
566                                        $G_dbpfx, $mtc_obj->code, $ucodes[$i], $i);
567                     if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_affected_rows($ord_pg) != 1 ) {
568                         log_crit(sprintf("bin5_points_save: failed at insert table order [%s]", $ord_sql));
569                         break;
570                     }
571                 }
572                 if ($i < $n)
573                     break;
574             }
575             else {
576                 $mtc_obj = pg_fetch_object($mtc_pg,0);
577             }
578
579             /*
580              * games management
581              */
582             $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult)
583                                                VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
584                                $G_dbpfx, $mtc_obj->code, $date, $action,
585                                $table->old_asta_pnt, $table->old_pnt,
586                                $table->old_asta_win,
587                                $table->old_friend,
588                                $table->old_mazzo, $table->old_mult);
589             if (($gam_pg  = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
590                 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
591                 break;
592             }
593
594             $gam_obj = pg_fetch_object($gam_pg,0);
595
596             /*
597              * points management
598              */
599             for ($i = 0 ; $i < $n ; $i++) {
600                 /* put points */
601                 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
602                                                VALUES (%d, %d, %d);",
603                                    $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
604                 if (($pts_pg  = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
605                     log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
606                     break;
607                 }
608             }
609             if ($i < $n)
610                 break;
611
612             if ($this->query("COMMIT") == FALSE) {
613                 break;
614             }
615
616             $is_trans = FALSE;
617
618             $table->match_id = $mtc_obj->code;
619             $ret = TRUE;
620         } while (0);
621
622         if ($is_trans)
623             $this->query("ROLLBACK");
624
625         return $ret;
626     }
627
628 } // End class BriskDB
629
630 class LoginDBOld
631 {
632     var $item;
633     var $item_n;
634
635     function LoginDBOld($filename)
636     {
637         GLOBAL $DOCUMENT_ROOT;
638         log_main("LoginDBOld create:start");
639
640         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
641             require("$DOCUMENT_ROOT/Etc/".$filename);
642         }
643         else {
644             return (FALSE);
645         }
646         $this->item_n = count($this->item);
647         log_main("LoginDBOld create:end");
648     }
649
650     function count()
651     {
652         return ($this->item_n);
653     }
654
655 } // End class LoginDBOld
656
657 ?>