3 * brisk - dbase_pgsql.phh
5 * Copyright (C) 2006-2015 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.
25 require_once("${G_base}Obj/dbase_base.phh");
27 $escsql_from = array( "\\", "'" );
28 $escsql_to = array( "\\\\", "\\'" );
32 GLOBAL $escsql_from, $escsql_to;
34 return str_replace($escsql_from, $escsql_to, $s);
39 static $dbcnnx = FALSE;
44 $this->db = DBConn::$dbcnnx;
47 static function create()
51 if (DBConn::$dbcnnx == FALSE) {
52 if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth, PGSQL_CONNECT_FORCE_NEW))) {
62 static function destroy()
64 if (DBConn::$dbcnnx != FALSE) {
65 DBConn::$dbcnnx = FALSE;
66 return (pg_close(DBConn::$dbcnnx));
71 static function recover()
74 return (self::create());
89 function BriskDB($dbconn)
91 $this->dbconn = $dbconn;
94 static function create()
96 GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
100 log_main("BriskDB create:start");
103 if (($dbconn = DBConn::create()) == FALSE) {
107 $ret = new BriskDB($dbconn);
115 if (($res = pg_query($this->dbconn->db(), $sql)) == FALSE) {
116 // try to recover the connection
117 if (($this->dbconn = DBConn::recover()) == FALSE)
119 return (pg_query($this->dbconn->db(), $sql));
125 function last_error()
127 return pg_last_error($this->dbconn->db);
130 function users_load()
134 function login_exists($login)
138 /* check the existence of the nick in the BriskDB */
139 log_main("login_exists: ".$login);
141 $user_sql = sprintf("SELECT * FROM %susers WHERE login = lower('%s')",
142 $G_dbpfx, escsql($login));
143 if (($user_pg = $this->query($user_sql)) != FALSE)
144 if (pg_numrows($user_pg) == 1)
150 function getrecord_bylogin($login) {
153 $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);
154 if (($user_pg = $this->query($user_sql)) == FALSE) {
157 if (pg_numrows($user_pg) != 1)
160 $user_obj = pg_fetch_object($user_pg, 0);
165 function user_add($login, $pass, $email, $type, $disa_reas, $guar_code) {
168 $usr_sql = sprintf("INSERT INTO %susers (login, pass, email, type, disa_reas, guar_code, lintm)
169 VALUES ('%s', '%s', '%s', %d, %d, %d, now()) RETURNING *;",
170 $G_dbpfx, escsql(strtolower($login)), escsql($pass), escsql($email),
171 $type, $disa_reas, $guar_code);
173 if (! (($usr_pg = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
176 $usr_obj = pg_fetch_object($usr_pg, 0);
181 function transaction($cmd) {
182 if ($cmd != "BEGIN" && $cmd != "COMMIT" && $cmd != "ROLLBACK")
185 $trans_sql = sprintf("%s;", $cmd);
186 if (($trans_pg = $this->query($trans_sql)) == FALSE) {
194 $laddr is native php int (32 or 64 bit)
195 if ret True is ip is free
197 function selfreg_check($laddr)
199 GLOBAL $G_dbpfx, $G_selfreg_tout, $G_selfreg_mask;
201 $sere_sql = sprintf("DELETE from %sselfreg_chk WHERE atime < now();", $G_dbpfx);
202 if (($sere_pg = $this->query($sere_sql)) == FALSE) {
206 if (($sere_pg = $this->query($sere_sql)) == FALSE) {
210 $sere_sql = sprintf("SELECT * FROM %sselfreg_chk WHERE (ip & %d) = %d;",
211 $G_dbpfx, int2four($G_selfreg_mask), int2four($laddr & $G_selfreg_mask));
212 if (($sere_pg = $this->query($sere_sql)) == FALSE) {
216 $ret = pg_numrows($sere_pg);
218 if ($ret === FALSE) {
221 else if ($ret === 0) {
229 // unreachable branch
235 $laddr is native php int (32 or 64 bit)
236 if ret True is ip is free
238 function selfreg_set($laddr)
240 GLOBAL $G_dbpfx, $G_selfreg_tout, $G_selfreg_mask;
242 $newi_sql = sprintf("INSERT INTO %sselfreg_chk (ip, atime) VALUES (%d, now() + interval '%d seconds');",
243 $G_dbpfx, int2four($laddr & $G_selfreg_mask), $G_selfreg_tout);
244 if (($newi_pg = $this->query($newi_sql)) == FALSE) {
251 to be able to add mail record code into the record itself I must reserve it before.
253 function mail_reserve_code() {
256 $mail_sql = sprintf("SELECT nextval('%smails_code_seq'::regclass) AS nextval;", $G_dbpfx);
257 if (($mail_pg = $this->query($mail_sql)) == FALSE) {
260 if (pg_numrows($mail_pg) != 1)
263 $mail_obj = pg_fetch_object($mail_pg, 0);
265 return ($mail_obj->nextval);
268 function check_record_by_login_or_email($login, $email) {
271 $arr_fie = array('login', 'email');
272 $arr_val = array($login, $email);
274 for ($i = 0 ; $i < 2 ; $i++) {
275 $user_sql = sprintf("SELECT * FROM %susers WHERE %s = lower('%s');",
276 $G_dbpfx, $arr_fie[$i], escsql($arr_val[$i]));
277 if (($user_pg = $this->query($user_sql)) == FALSE) {
278 fprintf(STDERR, "QUERY [%s]_ FALSE", $user_sql);
281 if (pg_numrows($user_pg) == 1) {
289 function getrecord_bycode($code) {
292 $user_sql = sprintf("SELECT * FROM %susers WHERE code = %d;", $G_dbpfx, $code);
293 if (($user_pg = $this->query($user_sql)) == FALSE) {
296 if (pg_numrows($user_pg) != 1)
299 $user_obj = pg_fetch_object($user_pg, 0);
304 function user_update_login_time($code, $lintm)
308 $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
310 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
317 function user_update_flag_ty($code, $type, $old_val, $old_reas, $new_val, $new_reas)
321 $user_sql = sprintf("UPDATE %susers SET (type, disa_reas)
322 = (type & ~(CAST (X'%08x' as integer)) | (CAST (X'%08x' as integer)), %d)
323 WHERE code = %d AND (type & (CAST (X'%08x' as integer)))
324 = (CAST (X'%08x' as integer)) AND disa_reas = %d;",
325 $G_dbpfx, $type, ($new_val ? $type : 0), $new_reas,
326 $code, $type, ($old_val ? $type : 0), $old_reas);
328 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
335 function user_update_passwd($code, $passwd)
339 $user_sql = sprintf("UPDATE %susers SET (pass) = (md5('%s')) WHERE code = %d;",
340 $G_dbpfx, $passwd, $code);
342 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
349 function user_prefs_update($code, $flags, $supp_comp)
353 $user_sql = sprintf("UPDATE %susers SET (type, supp_comp) = (%d, '%s') WHERE code = %d;",
354 $G_dbpfx, $flags, escsql($supp_comp), $code);
355 fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
356 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
359 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
364 function user_state_update($code, $flags, $disa_reas)
368 $user_sql = sprintf("UPDATE %susers SET (type, disa_reas) = (%d, %d) WHERE code = %d;",
369 $G_dbpfx, $flags, $disa_reas, $code);
370 fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
371 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
374 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
379 function user_tos_update($code, $tos_vers)
383 $user_sql = sprintf("UPDATE %susers SET (tos_vers) = ('%s') WHERE code = %d;",
384 $G_dbpfx, escsql($tos_vers), $code);
385 fprintf(STDERR, "REQUEST [%s]\n", $user_sql);
386 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
389 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
395 if success return a LoginDBItem object
397 function login_verify($login, $pass)
403 log_main("login_verify: ".$login);
405 //O /* check the existence of the nick in the BriskDB */
406 //O for ($i = 0 ; $i < $this->item_n ; $i++) {
407 //O log_main("login_verify: BEGIN");
409 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
413 log_main("login[".$user_obj->code."]: ".$user_obj->login);
415 /* if it exists check for a valid challenge */
416 if (($a_sem = Challenges::lock_data(TRUE)) != FALSE) {
417 if (($chals = &Challenges::load_data()) != FALSE) {
418 for ($e = 0 ; $e < $chals->item_n ; $e++) {
419 log_main("challenge[".$e."]: ".$chals->item[$e]->login);
420 if (strcmp($login, $chals->item[$e]->login) == 0) {
421 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
423 if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
424 log_main("login_verify SUCCESS for ".$login);
427 $this->user_update_login_time($user_obj->code, time());
428 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
432 } // end for ($e = 0 ...
435 if ($chals->ismod()) {
436 Challenges::save_data(&$chals);
439 Challenges::unlock_data($a_sem);
442 // O } // if (strcasecmp($this->item[$i]->login, ...
448 function getitem_bylogin($login, &$id) {
452 log_main("getitem_bylogin: ".$login);
454 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
457 $id = $user_obj->code;
458 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
461 function getitem_bycode($code) {
464 log_main("getitem_bycode: ".$code);
466 if (($user_obj = $this->getrecord_bycode($code)) == FALSE)
469 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
473 function getmail($login)
477 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
480 return ($ret->email);
483 function addusers_from_olddb($olddb, &$cont)
487 for ($i = 0 ; $i < $olddb->count() ; $i++) {
488 $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
489 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
490 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
492 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
493 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
503 return ($this->dbconn);
506 // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position
507 function users_get($match_code, $with_minmaxtidx, $is_newmatch)
511 if ($is_newmatch) { // is new
512 $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
513 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p,
514 %susers AS u, %sbin5_table_orders AS o
515 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
516 AND m.code = o.mcode AND u.code = o.ucode AND m.code = %d
517 GROUP BY u.code, u.login%s, o.pos
519 ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
520 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
521 ($with_minmaxtidx ? ", m.tidx" : ""));
524 $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
525 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
526 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d
527 GROUP BY u.code, u.login%s;",
528 ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
529 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
530 ($with_minmaxtidx ? ", m.tidx" : ""));
533 if (($usr_pg = pg_query($this->dbconn->db(), $usr_sql)) == FALSE ) {
534 log_crit(sprintf("%s::%s: pg_query usr_sql failed [%s]", __CLASS__, __FUNCTION__, $usr_sql));
537 $usr_n = pg_numrows($usr_pg);
538 if ($usr_n != BIN5_PLAYERS_N) {
539 log_crit(sprintf("%s::%s: wrong number of players [%s] %d", __CLASS__, __FUNCTION__, $usr_sql, $usr_n));
544 if ($with_minmaxtidx)
545 $fields = array('code', 'login', 'first', 'last', 'tidx');
547 $fields = array('code', 'login');
549 for ($u = 0 ; $u < $usr_n ; $u++) {
550 $usr_obj = pg_fetch_object($usr_pg, $u);
551 $users[$u] = array();
552 foreach($fields as $field) {
553 $users[$u][$field] = $usr_obj->$field;
559 // out: tab->{points,points_n,old_reason}, in: tab->ttok
560 function match_continue($match_code, $table, $tidx)
563 $sql_ttok = escsql($table->table_token);
565 if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) {
566 log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__));
570 $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code);
571 if (($num_pg = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) {
572 log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__));
575 $num_obj = pg_fetch_object($num_pg, 0);
576 $table->points_n = $num_obj->points_n;
578 // TAG: POINTS_MANAGEMENT
579 $tot_sql = sprintf("SELECT sum(p.pts * (2^g.mult)) AS pts
580 FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u,
581 %sbin5_table_orders AS o
582 WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code
583 AND p.ucode = o.ucode AND g.mcode = o.mcode
584 GROUP BY p.ucode, o.pos
586 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code);
587 if (($tot_pg = pg_query($this->dbconn->db(), $tot_sql)) == FALSE
588 || pg_numrows($tot_pg) != BIN5_PLAYERS_N) {
589 log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__));
594 foreach ($users as $user) {
595 // TAG: POINTS_MANAGEMENT
596 $pts_sql = sprintf("SELECT p.pts AS pts, g.mult AS mult
597 FROM %sbin5_points as p, %sbin5_games as g
598 WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d
599 ORDER BY g.tstamp ASC
600 LIMIT %d OFFSET %d;",
601 $G_dbpfx, $G_dbpfx, $match_code, $user['code'],
603 ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS));
605 // points of the match for each user
606 if (($pts_pg = $this->query($pts_sql)) == FALSE) {
607 log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__));
610 $pts_n = pg_numrows($pts_pg);
611 if ($pts_n > $table->points_n) {
612 // inconsistent scenario number of points great than number of games
613 log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__));
616 // TAG: POINTS_MANAGEMENT
617 for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) {
618 $pts_obj = pg_fetch_object($pts_pg, $i);
619 $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts * pow(2, $pts_obj->mult);
621 $tot_obj = pg_fetch_object($tot_pg, $u);
622 $table->total[$u] = $tot_obj->pts;
627 $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY tstamp DESC LIMIT 1;", $G_dbpfx, $match_code);
628 if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
629 log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__));
632 $gam_obj = pg_fetch_object($gam_pg, 0);
634 $table->old_reason = game_description($gam_obj->act, 'html', $gam_obj->mult,
635 $gam_obj->asta_win, ($gam_obj->asta_win != -1 ?
636 $users[$gam_obj->asta_win]['login'] : ""),
637 $gam_obj->friend, ($gam_obj->friend != -1 ?
638 $users[$gam_obj->friend]['login'] : ""),
639 $gam_obj->pnt, $gam_obj->asta_pnt);
641 // update matches with new ttok and table idx
642 $mtc_sql = sprintf("UPDATE %sbin5_matches SET (ttok, tidx) = ('%s', %d) WHERE code = %d RETURNING *;",
643 $G_dbpfx, $sql_ttok, $tidx, $match_code);
644 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
645 log_crit(sprintf("%s::%s: update matches table failed", __CLASS__, __FUNCTION__));
652 function match_order_get(&$match_data, $match_code, $exp_num)
656 $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;",
657 $G_dbpfx, $match_code);
659 if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) {
660 log_crit(sprintf("%s: fails for id or users number", __FUNCTION__));
665 for ($i = 0 ; $i < $exp_num ; $i++) {
666 $ord_obj = pg_fetch_object($ord_pg, $i);
667 $ucodes[$i] = $ord_obj->ucode;
670 if ($match_data !== NULL) {
671 $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;",
672 $G_dbpfx, $match_code);
674 if (($mtdt_pg = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) {
675 log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code));
679 $mtdt_obj = pg_fetch_object($mtdt_pg, 0);
681 foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) {
682 $match_data[$match_name] = $mtdt_obj->$match_name;
691 function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts)
694 $sql_ttok = escsql($table->table_token);
700 /* check the existence of the nick in the BriskDB */
701 log_main("bin5_points_save: ");
704 if ($this->query("BEGIN") == FALSE) {
713 $mtc_sql = sprintf("UPDATE %sbin5_matches SET (mazzo_next, mult_next) = (%d, %d) WHERE ttok = '%s' RETURNING *;",
714 $G_dbpfx, $table->mazzo, $table->mult, $sql_ttok);
715 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
717 // match not exists, insert it
718 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx, mazzo_next, mult_next) VALUES ('%s', %d, %d, %d) RETURNING *;",
719 $G_dbpfx, $sql_ttok, $tidx, $table->mazzo, $table->mult);
720 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_affected_rows($mtc_pg) != 1) {
721 log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
724 $mtc_obj = pg_fetch_object($mtc_pg, 0);
726 for ($i = 0 ; $i < $n ; $i++) {
727 $ord_sql = sprintf("INSERT INTO %sbin5_table_orders (mcode, ucode, pos) VALUES (%d, %d, %d);",
728 $G_dbpfx, $mtc_obj->code, $ucodes[$i], $i);
729 if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_affected_rows($ord_pg) != 1 ) {
730 log_crit(sprintf("bin5_points_save: failed at insert table order [%s]", $ord_sql));
733 $codes_where .= sprintf("%scode = %d", ($i == 0 ? "" : " OR "), $ucodes[$i]);
738 $cnt_sql = sprintf("UPDATE %susers SET (match_cnt, game_cnt)
739 = (match_cnt+1, game_cnt+1) WHERE %s;",
740 $G_dbpfx, $codes_where);
742 if (($cnt_pg = $this->query($cnt_sql)) == FALSE || pg_affected_rows($cnt_pg) != $n) {
743 log_crit(sprintf("bin5_points_save: failed increment match and game [%s]", $cnt_sql));
748 $mtc_obj = pg_fetch_object($mtc_pg,0);
750 for ($i = 0 ; $i < $n ; $i++) {
751 $codes_where .= sprintf("%scode = %d", ($i == 0 ? "" : " OR "), $ucodes[$i]);
754 $cnt_sql = sprintf("UPDATE %susers SET (game_cnt)
755 = (game_cnt+1) WHERE %s;",
756 $G_dbpfx, $codes_where);
758 if (($cnt_pg = $this->query($cnt_sql)) == FALSE || pg_affected_rows($cnt_pg) != $n) {
759 log_crit(sprintf("bin5_points_save: failed increment game [%s]", $cnt_sql));
767 $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult)
768 VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
769 $G_dbpfx, $mtc_obj->code, $date, $action,
770 $table->old_asta_pnt, $table->old_pnt,
771 $table->old_asta_win,
773 $table->old_mazzo, $table->old_mult);
774 if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
775 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
779 $gam_obj = pg_fetch_object($gam_pg,0);
784 for ($i = 0 ; $i < $n ; $i++) {
786 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
787 VALUES (%d, %d, %d);",
788 $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
789 if (($pts_pg = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
790 log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
797 if ($this->query("COMMIT") == FALSE) {
803 $table->match_id = $mtc_obj->code;
808 $this->query("ROLLBACK");
813 function mail_add_fromitem($mail) {
817 INSERT INTO %smails (code, ucode, type, tstamp, subj, body_txt, body_htm, hash)
818 VALUES (%d, %d, %d, to_timestamp(%d), '%s', '%s', '%s', '%s') RETURNING *;",
819 $G_dbpfx, $mail->code, $mail->ucode, $mail->type, $mail->tstamp,
820 escsql($mail->subj), escsql($mail->body_txt), escsql($mail->body_htm),
821 ($mail->hash == NULL ? "" : escsql($mail->hash))
824 if (! (($usr_pg = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
827 $usr_obj = pg_fetch_object($usr_pg, 0);
832 function mail_check($code, $type, $hash)
836 $mai_sql = sprintf("SELECT * FROM %smails WHERE code = %d AND type = %d AND hash = '%s';",
837 $G_dbpfx, $code, $type, escsql($hash));
838 if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_numrows($mai_pg) != 1) {
843 $mai_obj = pg_fetch_object($mai_pg, 0);
847 function mail_delete($code)
851 $mai_sql = sprintf("DELETE FROM %smails WHERE code = %d;", $G_dbpfx, $code);
853 if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_affected_rows($mai_pg) != 1) {
860 } // End class BriskDB
867 function LoginDBOld($filename)
869 GLOBAL $DOCUMENT_ROOT;
870 log_main("LoginDBOld create:start");
872 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
873 require("$DOCUMENT_ROOT/Etc/".$filename);
878 $this->item_n = count($this->item);
879 log_main("LoginDBOld create:end");
884 return ($this->item_n);
887 } // End class LoginDBOld