usermgmt improved (not yet finished)
[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 user_add($login, $pass, $email, $type, $disa_reas, $guar_code) {
161         GLOBAL $G_dbpfx;
162
163         $usr_sql = sprintf("INSERT INTO %susers (login, pass, email, type, disa_reas, guar_code) 
164                             VALUES ('%s', '%s', '%s', %d, %d, %d) RETURNING *;",
165                            $G_dbpfx, escsql(strtolower($login)), escsql($pass), escsql($email), 
166                            $type, $disa_reas, $guar_code);
167
168         if (! (($usr_pg  = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
169             return FALSE;
170         }
171         $usr_obj = pg_fetch_object($usr_pg, 0);
172
173         return $usr_obj;
174     }
175
176     function transaction($cmd) {
177         if ($cmd != "BEGIN" && $cmd != "COMMIT" && $cmd != "ROLLBACK")
178             return FALSE;
179
180         $trans_sql = sprintf("%s;", $cmd);
181         if (($trans_pg  = $this->query($trans_sql)) == FALSE) {
182             return FALSE;
183         }
184
185         return (TRUE);
186     }
187
188     function mail_reserve_code() {
189         GLOBAL $G_dbpfx;
190
191         $mail_sql = sprintf("SELECT nextval('%smails_code_seq'::regclass) AS nextval;", $G_dbpfx);
192         if (($mail_pg  = $this->query($mail_sql)) == FALSE) {
193             return FALSE;
194         }
195         if (pg_numrows($mail_pg) != 1)
196             return FALSE;
197
198         $mail_obj = pg_fetch_object($mail_pg, 0);
199
200         return ($mail_obj->nextval);
201     }
202
203     function check_record_by_login_or_email($login, $email) {
204         GLOBAL $G_dbpfx;
205
206         $arr_fie = array('login', 'email');
207         $arr_val = array($login, $email);
208
209         for ($i = 0 ; $i < 2 ; $i++) {
210             $user_sql = sprintf("SELECT * FROM %susers WHERE %s = lower('%s');",
211                                 $G_dbpfx, $arr_fie[$i], escsql($arr_val[$i]));
212             if (($user_pg  = $this->query($user_sql)) == FALSE) {
213                 fprintf(STDERR, "QUERY [%s]_ FALSE", $user_sql);
214                 return (3);
215             }
216             if (pg_numrows($user_pg) == 1) {
217                 return ($i + 1);
218             }
219         }
220
221         return (0);
222     }
223
224     function getrecord_bycode($code) {
225         GLOBAL $G_dbpfx;
226
227         $user_sql = sprintf("SELECT * FROM %susers WHERE code = %d;",  $G_dbpfx, $code);
228         if (($user_pg  = $this->query($user_sql)) == FALSE) {
229             return FALSE;
230         }
231         if (pg_numrows($user_pg) != 1)
232             return FALSE;
233
234         $user_obj = pg_fetch_object($user_pg, 0);
235
236         return ($user_obj);
237     }
238
239     function user_update_login_time($code, $lintm)
240     {
241         GLOBAL $G_dbpfx;
242
243         $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
244
245         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
246              return FALSE;
247         }
248
249         return TRUE;
250     }
251
252     function user_prefs_update($code, $flags, $supp_comp)
253     {
254         GLOBAL $G_dbpfx;
255
256         $user_sql = sprintf("UPDATE %susers SET (type, supp_comp) = (%d, '%s') WHERE code = %d;",
257                             $G_dbpfx, $flags, escsql($supp_comp), $code);
258         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
259         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
260              return FALSE;
261         }
262         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
263
264         return TRUE;
265     }
266
267     function user_state_update($code, $flags, $disa_reas)
268     {
269         GLOBAL $G_dbpfx;
270
271         $user_sql = sprintf("UPDATE %susers SET (type, disa_reas) = (%d, %d) WHERE code = %d;",
272                             $G_dbpfx, $flags, $disa_reas, $code);
273         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
274         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
275              return FALSE;
276         }
277         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
278
279         return TRUE;
280     }
281
282     function user_tos_update($code, $tos_vers)
283     {
284         GLOBAL $G_dbpfx;
285
286         $user_sql = sprintf("UPDATE %susers SET (tos_vers) = ('%s') WHERE code = %d;",
287                             $G_dbpfx, escsql($tos_vers), $code);
288         fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
289         if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
290              return FALSE;
291         }
292         fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
293
294         return TRUE;
295     }
296
297     /*
298       if success return a LoginDBItem object
299      */
300     function login_verify($login, $pass)
301     {
302         GLOBAL $G_dbpfx;
303
304         $ret = FALSE;
305
306         log_main("login_verify: ".$login);
307
308         //O /* check the existence of the nick in the BriskDB */
309         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
310         //O log_main("login_verify: BEGIN");
311
312         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
313             return FALSE;
314         }
315
316         log_main("login[".$user_obj->code."]: ".$user_obj->login);
317
318         /* if it exists check for a valid challenge */
319         if (($a_sem = Challenges::lock_data(TRUE)) != FALSE) {
320             if (($chals = &Challenges::load_data()) != FALSE) {
321                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
322                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
323                     if (strcmp($login, $chals->item[$e]->login) == 0) {
324                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
325
326                         if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
327                             log_main("login_verify SUCCESS for ".$login);
328
329                             $chals->rem($login);
330                             $this->user_update_login_time($user_obj->code, time());
331                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
332                             break;
333                         }
334                     }
335                 } // end for ($e = 0 ...
336             }
337
338             if ($chals->ismod()) {
339                 Challenges::save_data(&$chals);
340             }
341
342             Challenges::unlock_data($a_sem);
343         }
344         //O break;
345         // O } //  if (strcasecmp($this->item[$i]->login, ...
346         //O }
347
348         return ($ret);
349     }
350
351     function getitem_bylogin($login, &$id) {
352         $ret = FALSE;
353         $id = -1;
354
355         log_main("getitem_bylogin: ".$login);
356
357         if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
358             return $ret;
359
360         $id = $user_obj->code;
361         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
362     }
363
364     function getitem_bycode($code) {
365         $ret = FALSE;
366
367         log_main("getitem_bycode: ".$code);
368
369         if (($user_obj = $this->getrecord_bycode($code)) == FALSE)
370             return $ret;
371
372         return (LoginDBItem::LoginDBItemFromRecord($user_obj));
373     }
374
375     // TODO FOR DB
376     function getmail($login)
377     {
378         log_main("getmail");
379
380         if (($ret = $this->getrecord_bylogin($login)) == FALSE)
381             return FALSE;
382
383         return ($ret->email);
384     }
385
386     function addusers_from_olddb($olddb, &$cont)
387     {
388         GLOBAL $G_dbpfx;
389
390         for ($i = 0 ; $i < $olddb->count() ; $i++) {
391             $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
392                                 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
393                                 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
394
395             if ( ! (($user_pg  = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
396                 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
397
398                 return FALSE;
399             }
400         }
401         return TRUE;
402     }
403
404     function getdbconn()
405     {
406         return ($this->dbconn);
407     }
408
409     // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position
410     function users_get($match_code, $with_minmaxtidx, $is_newmatch)
411     {
412         GLOBAL $G_dbpfx;
413
414         if ($is_newmatch) { // is new
415             $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
416                                   FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p,
417                                        %susers AS u, %sbin5_table_orders AS o
418                                   WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
419                                        AND m.code = o.mcode AND u.code = o.ucode AND m.code = %d
420                                   GROUP BY u.code, u.login%s, o.pos
421                                   ORDER BY o.pos;",
422                                ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
423                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
424                                ($with_minmaxtidx ? ", m.tidx" : ""));
425         }
426         else { // is old
427             $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
428                                   FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
429                                   WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d
430                                   GROUP BY u.code, u.login%s;",
431                                ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
432                                $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
433                                ($with_minmaxtidx ? ", m.tidx" : ""));
434         }
435
436         if (($usr_pg  = pg_query($this->dbconn->db(), $usr_sql)) == FALSE ) {
437             log_crit(sprintf("%s::%s: pg_query usr_sql failed [%s]", __CLASS__, __FUNCTION__, $usr_sql));
438             return (FALSE);
439         }
440         $usr_n = pg_numrows($usr_pg);
441         if ($usr_n != BIN5_PLAYERS_N) {
442             log_crit(sprintf("%s::%s: wrong number of players [%s] %d", __CLASS__, __FUNCTION__, $usr_sql, $usr_n));
443             return (FALSE);
444         }
445         $users = array();
446
447         if ($with_minmaxtidx)
448             $fields = array('code', 'login', 'first', 'last', 'tidx');
449         else
450             $fields = array('code', 'login');
451
452         for ($u = 0 ; $u < $usr_n ; $u++) {
453             $usr_obj = pg_fetch_object($usr_pg, $u);
454             $users[$u] = array();
455             foreach($fields as $field) {
456                 $users[$u][$field] = $usr_obj->$field;
457             }
458         }
459         return ($users);
460     }
461
462     // out: tab->{points,points_n,old_reason}, in: tab->ttok
463     function match_continue($match_code, $table, $tidx)
464     {
465         GLOBAL $G_dbpfx;
466         $sql_ttok = escsql($table->table_token);
467
468         if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) {
469             log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__));
470             return (FALSE);
471         }
472
473         $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code);
474         if (($num_pg  = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) {
475             log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__));
476             return (FALSE);
477         }
478         $num_obj = pg_fetch_object($num_pg, 0);
479         $table->points_n = $num_obj->points_n;
480
481         // TAG: POINTS_MANAGEMENT
482         $tot_sql = sprintf("SELECT sum(p.pts * (2^g.mult)) AS pts
483                             FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u,
484                                  %sbin5_table_orders AS o
485                             WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code
486                                   AND p.ucode = o.ucode AND g.mcode = o.mcode
487                             GROUP BY p.ucode, o.pos
488                             ORDER BY o.pos;",
489                            $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code);
490         if (($tot_pg  = pg_query($this->dbconn->db(), $tot_sql)) == FALSE
491             || pg_numrows($tot_pg) != BIN5_PLAYERS_N) {
492             log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__));
493             return(FALSE);
494         }
495
496         $u = 0;
497         foreach ($users as $user) {
498             // TAG: POINTS_MANAGEMENT
499             $pts_sql = sprintf("SELECT p.pts AS pts, g.mult AS mult
500                                     FROM %sbin5_points as p, %sbin5_games as g
501                                     WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d
502                                     ORDER BY g.tstamp ASC
503                                     LIMIT %d OFFSET %d;",
504                                $G_dbpfx, $G_dbpfx, $match_code, $user['code'],
505                                MAX_POINTS,
506                                ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS));
507
508             // points of the match for each user
509             if (($pts_pg  = $this->query($pts_sql)) == FALSE) {
510                 log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__));
511                 return (FALSE);
512             }
513             $pts_n = pg_numrows($pts_pg);
514             if ($pts_n > $table->points_n) {
515                 // inconsistent scenario number of points great than number of games
516                 log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__));
517                 return (FALSE);
518             }
519             // TAG: POINTS_MANAGEMENT
520             for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) {
521                 $pts_obj = pg_fetch_object($pts_pg, $i);
522                 $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts * pow(2, $pts_obj->mult);
523             }
524             $tot_obj = pg_fetch_object($tot_pg, $u);
525             $table->total[$u] = $tot_obj->pts;
526
527             $u++;
528         }
529
530         $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY tstamp DESC LIMIT 1;", $G_dbpfx, $match_code);
531         if (($gam_pg  = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
532             log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__));
533             return (FALSE);
534         }
535         $gam_obj = pg_fetch_object($gam_pg, 0);
536
537         $table->old_reason = game_description($gam_obj->act, 'html', $gam_obj->mult,
538                                               $gam_obj->asta_win, ($gam_obj->asta_win != -1 ?
539                                                                    $users[$gam_obj->asta_win]['login'] : ""),
540                                               $gam_obj->friend, ($gam_obj->friend != -1 ?
541                                                                  $users[$gam_obj->friend]['login'] : ""),
542                                               $gam_obj->pnt, $gam_obj->asta_pnt);
543
544         // update matches with new ttok and table idx
545         $mtc_sql = sprintf("UPDATE %sbin5_matches SET (ttok, tidx) = ('%s', %d) WHERE code = %d RETURNING *;",
546                            $G_dbpfx, $sql_ttok, $tidx, $match_code);
547         if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
548             log_crit(sprintf("%s::%s: update matches table failed", __CLASS__, __FUNCTION__));
549             return (FALSE);
550         }
551
552         return (TRUE);
553     }
554
555     function match_order_get(&$match_data, $match_code, $exp_num)
556     {
557         GLOBAL $G_dbpfx;
558
559         $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;",
560                            $G_dbpfx, $match_code);
561
562         if (($ord_pg  = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) {
563             log_crit(sprintf("%s: fails for id or users number", __FUNCTION__));
564             return (FALSE);
565         }
566
567         $ucodes = array();
568         for ($i = 0 ; $i < $exp_num ; $i++) {
569             $ord_obj = pg_fetch_object($ord_pg, $i);
570             $ucodes[$i] = $ord_obj->ucode;
571         }
572
573         if ($match_data !== NULL) {
574             $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;",
575                                 $G_dbpfx, $match_code);
576
577             if (($mtdt_pg  = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) {
578                 log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code));
579                 return (FALSE);
580             }
581
582             $mtdt_obj = pg_fetch_object($mtdt_pg, 0);
583
584             foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) {
585                 $match_data[$match_name] = $mtdt_obj->$match_name;
586             }
587         }
588
589         return ($ucodes);
590     }
591
592     //   ttok   text UNIQUE,
593     //   tidx
594     function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts)
595     {
596         GLOBAL $G_dbpfx;
597         $sql_ttok = escsql($table->table_token);
598
599         $is_trans = FALSE;
600         $ret = FALSE;
601
602         $n = count($ucodes);
603         /* check the existence of the nick in the BriskDB */
604         log_main("bin5_points_save: ");
605
606         do {
607             if ($this->query("BEGIN") == FALSE) {
608                 break;
609             }
610             $is_trans = TRUE;
611
612             /*
613              * matches management
614              */
615             $mtc_sql = sprintf("UPDATE %sbin5_matches SET (mazzo_next, mult_next) = (%d, %d) WHERE ttok = '%s' RETURNING *;",
616                                $G_dbpfx, $table->mazzo, $table->mult, $sql_ttok);
617             if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
618
619                 // match not exists, insert it
620                 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx, mazzo_next, mult_next) VALUES ('%s', %d, %d, %d) RETURNING *;",
621                                    $G_dbpfx, $sql_ttok, $tidx, $table->mazzo, $table->mult);
622                 if (($mtc_pg  = $this->query($mtc_sql)) == FALSE || pg_affected_rows($mtc_pg) != 1) {
623                     log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
624                     break;
625                 }
626                 $mtc_obj = pg_fetch_object($mtc_pg, 0);
627
628                 for ($i = 0 ; $i < $n ; $i++) {
629                     $ord_sql = sprintf("INSERT INTO %sbin5_table_orders (mcode, ucode, pos) VALUES (%d, %d, %d);",
630                                        $G_dbpfx, $mtc_obj->code, $ucodes[$i], $i);
631                     if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_affected_rows($ord_pg) != 1 ) {
632                         log_crit(sprintf("bin5_points_save: failed at insert table order [%s]", $ord_sql));
633                         break;
634                     }
635                 }
636                 if ($i < $n)
637                     break;
638             }
639             else {
640                 $mtc_obj = pg_fetch_object($mtc_pg,0);
641             }
642
643             /*
644              * games management
645              */
646             $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult)
647                                                VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
648                                $G_dbpfx, $mtc_obj->code, $date, $action,
649                                $table->old_asta_pnt, $table->old_pnt,
650                                $table->old_asta_win,
651                                $table->old_friend,
652                                $table->old_mazzo, $table->old_mult);
653             if (($gam_pg  = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
654                 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
655                 break;
656             }
657
658             $gam_obj = pg_fetch_object($gam_pg,0);
659
660             /*
661              * points management
662              */
663             for ($i = 0 ; $i < $n ; $i++) {
664                 /* put points */
665                 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
666                                                VALUES (%d, %d, %d);",
667                                    $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
668                 if (($pts_pg  = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
669                     log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
670                     break;
671                 }
672             }
673             if ($i < $n)
674                 break;
675
676             if ($this->query("COMMIT") == FALSE) {
677                 break;
678             }
679
680             $is_trans = FALSE;
681
682             $table->match_id = $mtc_obj->code;
683             $ret = TRUE;
684         } while (0);
685
686         if ($is_trans)
687             $this->query("ROLLBACK");
688
689         return $ret;
690     }
691
692 } // End class BriskDB
693
694 class LoginDBOld
695 {
696     var $item;
697     var $item_n;
698
699     function LoginDBOld($filename)
700     {
701         GLOBAL $DOCUMENT_ROOT;
702         log_main("LoginDBOld create:start");
703
704         if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
705             require("$DOCUMENT_ROOT/Etc/".$filename);
706         }
707         else {
708             return (FALSE);
709         }
710         $this->item_n = count($this->item);
711         log_main("LoginDBOld create:end");
712     }
713
714     function count()
715     {
716         return ($this->item_n);
717     }
718
719 } // End class LoginDBOld
720
721 ?>