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 $ret = pg_close(DBConn::$dbcnnx);
66 DBConn::$dbcnnx = FALSE;
72 static function recover()
75 return (self::create());
90 function BriskDB($dbconn)
92 $this->dbconn = $dbconn;
95 static function create()
97 GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
101 log_main("BriskDB create:start");
104 if (($dbconn = DBConn::create()) == FALSE) {
108 $ret = new BriskDB($dbconn);
117 if (($this->dbconn = DBConn::recover()) == FALSE)
120 if (($res = @pg_query($this->dbconn->db(), $sql)) == FALSE) {
121 error_log('pg_result_status: ' . pg_result_status($res));
122 error_log('pg_connection_status: ' . pg_connection_status($this->dbconn->db()));
123 // try to recover the connection
124 if (($this->dbconn = DBConn::recover()) == FALSE)
126 return (@pg_query($this->dbconn->db(), $sql));
132 function last_error()
134 return pg_last_error($this->dbconn->db);
137 function users_load()
141 function login_exists($login)
145 /* check the existence of the nick in the BriskDB */
146 log_main("login_exists: ".$login);
148 $user_sql = sprintf("SELECT * FROM %susers WHERE login = '%s'",
149 $G_dbpfx, escsql($login));
150 if (($user_pg = $this->query($user_sql)) != FALSE)
151 if (pg_numrows($user_pg) == 1)
157 function getrecord_bylogin($login) {
160 $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);
161 if (($user_pg = $this->query($user_sql)) == FALSE) {
164 if (pg_numrows($user_pg) != 1)
167 $user_obj = pg_fetch_object($user_pg, 0);
172 function user_add($login, $pass, $email, $type, $disa_reas, $guar_code) {
175 $usr_sql = sprintf("INSERT INTO %susers (login, pass, email, type, disa_reas, guar_code, lintm)
176 VALUES ('%s', '%s', '%s', %d, %d, %d, now()) RETURNING *;",
177 $G_dbpfx, escsql(strtolower($login)), escsql($pass), escsql($email),
178 $type, $disa_reas, $guar_code);
180 if (! (($usr_pg = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
183 $usr_obj = pg_fetch_object($usr_pg, 0);
188 function transaction($cmd) {
189 if ($cmd != "BEGIN" && $cmd != "COMMIT" && $cmd != "ROLLBACK")
192 $trans_sql = sprintf("%s;", $cmd);
193 if (($trans_pg = $this->query($trans_sql)) == FALSE) {
201 $laddr is native php int (32 or 64 bit)
202 if ret True is ip is free
204 function selfreg_check($laddr)
206 GLOBAL $G_dbpfx, $G_selfreg_tout, $G_selfreg_mask;
208 $sere_sql = sprintf("DELETE from %sselfreg_chk WHERE atime < now();", $G_dbpfx);
209 if (($sere_pg = $this->query($sere_sql)) == FALSE) {
213 $sere_sql = sprintf("SELECT * FROM %sselfreg_chk WHERE (ip & %d) = %d;",
214 $G_dbpfx, int2four($G_selfreg_mask), int2four($laddr & $G_selfreg_mask));
215 if (($sere_pg = $this->query($sere_sql)) == FALSE) {
219 $ret = pg_numrows($sere_pg);
221 if ($ret === FALSE) {
224 else if ($ret === 0) {
232 // unreachable branch
238 $laddr is native php int (32 or 64 bit)
239 if ret True is ip is free
241 function selfreg_set($laddr)
243 GLOBAL $G_dbpfx, $G_selfreg_tout, $G_selfreg_mask;
245 $newi_sql = sprintf("INSERT INTO %sselfreg_chk (ip, atime) VALUES (%d, now() + interval '%d seconds');",
246 $G_dbpfx, int2four($laddr & $G_selfreg_mask), $G_selfreg_tout);
247 if (($newi_pg = $this->query($newi_sql)) == FALSE) {
254 to be able to add mail record code into the record itself I must reserve it before.
256 function mail_reserve_code() {
259 $mail_sql = sprintf("SELECT nextval('%smails_code_seq'::regclass) AS nextval;", $G_dbpfx);
260 if (($mail_pg = $this->query($mail_sql)) == FALSE) {
263 if (pg_numrows($mail_pg) != 1)
266 $mail_obj = pg_fetch_object($mail_pg, 0);
268 return ($mail_obj->nextval);
271 function check_record_by_login_or_email($login, $email) {
274 $arr_fie = array('login', 'email');
275 $arr_val = array($login, $email);
277 for ($i = 0 ; $i < 2 ; $i++) {
278 $user_sql = sprintf("SELECT * FROM %susers WHERE %s = lower('%s');",
279 $G_dbpfx, $arr_fie[$i], escsql($arr_val[$i]));
280 if (($user_pg = $this->query($user_sql)) == FALSE) {
281 fprintf(STDERR, "QUERY [%s]_ FALSE", $user_sql);
284 if (pg_numrows($user_pg) == 1) {
292 function getrecord_bycode($code) {
295 $user_sql = sprintf("SELECT * FROM %susers WHERE code = %d;", $G_dbpfx, $code);
296 if (($user_pg = $this->query($user_sql)) == FALSE) {
299 if (pg_numrows($user_pg) != 1)
302 $user_obj = pg_fetch_object($user_pg, 0);
307 function user_update_login_time($code, $lintm)
311 $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
313 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
320 function user_update_flag_ty($code, $type, $old_val, $old_reas, $new_val, $new_reas)
324 $user_sql = sprintf("UPDATE %susers SET (type, disa_reas)
325 = (type & ~(CAST (X'%08x' as integer)) | (CAST (X'%08x' as integer)), %d)
326 WHERE code = %d AND (type & (CAST (X'%08x' as integer)))
327 = (CAST (X'%08x' as integer)) AND disa_reas = %d;",
328 $G_dbpfx, $type, ($new_val ? $type : 0), $new_reas,
329 $code, $type, ($old_val ? $type : 0), $old_reas);
331 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
338 function user_update_passwd($code, $passwd)
342 $user_sql = sprintf("UPDATE %susers SET (pass) = (md5('%s')) WHERE code = %d;",
343 $G_dbpfx, $passwd, $code);
345 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
352 function user_prefs_update($code, $flags, $supp_comp)
356 $user_sql = sprintf("UPDATE %susers SET (type, supp_comp) = (%d, '%s') WHERE code = %d;",
357 $G_dbpfx, $flags, escsql($supp_comp), $code);
358 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
365 function user_state_update($code, $flags, $disa_reas)
369 $user_sql = sprintf("UPDATE %susers SET (type, disa_reas) = (%d, %d) WHERE code = %d;",
370 $G_dbpfx, $flags, $disa_reas, $code);
371 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
378 function user_tos_update($code, $tos_vers)
382 $user_sql = sprintf("UPDATE %susers SET (tos_vers) = ('%s') WHERE code = %d;",
383 $G_dbpfx, escsql($tos_vers), $code);
384 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
392 if success return a LoginDBItem object
394 function login_verify($login, $pass)
400 log_main("login_verify: ".$login);
402 //O /* check the existence of the nick in the BriskDB */
403 //O for ($i = 0 ; $i < $this->item_n ; $i++) {
404 //O log_main("login_verify: BEGIN");
406 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
410 log_main("login[".$user_obj->code."]: ".$user_obj->login);
412 /* if it exists check for a valid challenge */
413 if (($a_sem = Challenges::lock_data(TRUE)) != FALSE) {
414 if (($chals = &Challenges::load_data()) != FALSE) {
415 for ($e = 0 ; $e < $chals->item_n ; $e++) {
416 log_main("challenge[".$e."]: ".$chals->item[$e]->login);
417 if (strcmp($login, $chals->item[$e]->login) == 0) {
418 log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
420 if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
421 log_main("login_verify SUCCESS for ".$login);
424 $this->user_update_login_time($user_obj->code, time());
425 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
429 } // end for ($e = 0 ...
432 if ($chals->ismod()) {
433 Challenges::save_data($chals);
436 Challenges::unlock_data($a_sem);
439 // O } // if (strcasecmp($this->item[$i]->login, ...
445 function getitem_bylogin($login, &$id) {
449 log_main("getitem_bylogin: ".$login);
451 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
454 $id = $user_obj->code;
455 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
458 function getitem_bycode($code) {
461 log_main("getitem_bycode: ".$code);
463 if (($user_obj = $this->getrecord_bycode($code)) == FALSE)
466 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
470 function getmail($login)
474 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
477 return ($ret->email);
480 function addusers_from_olddb($olddb, &$cont)
484 for ($i = 0 ; $i < $olddb->count() ; $i++) {
485 $user_sql = sprintf("INSERT INTO %susers ( login, pass, email, type) VALUES ('%s', '%s', '%s', %d);",
486 $G_dbpfx, escsql(strtolower($olddb->item[$i]->login)), escsql($olddb->item[$i]->pass),
487 escsql($olddb->item[$i]->email), $olddb->item[$i]->type & USER_FLAG_TY_ALL);
489 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
490 $cont .= sprintf("ERROR IN LINE: %s\n", eschtml($user_sql));
500 return ($this->dbconn);
503 // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position
504 function users_get($match_code, $with_minmaxtidx, $is_newmatch)
508 if ($is_newmatch) { // is new
509 $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
510 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p,
511 %susers AS u, %sbin5_table_orders AS o
512 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode
513 AND m.code = o.mcode AND u.code = o.ucode AND m.code = %d
514 GROUP BY u.code, u.login%s, o.pos
516 ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
517 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
518 ($with_minmaxtidx ? ", m.tidx" : ""));
521 $usr_sql = sprintf("SELECT u.code AS code, u.login AS login%s
522 FROM %sbin5_matches AS m, %sbin5_games AS g, %sbin5_points AS p, %susers AS u
523 WHERE m.code = g.mcode AND g.code = p.gcode AND u.code = p.ucode AND m.code = %d
524 GROUP BY u.code, u.login%s;",
525 ($with_minmaxtidx ? ", min(g.tstamp) AS first, max(g.tstamp) AS last, m.tidx AS tidx" : ""),
526 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code,
527 ($with_minmaxtidx ? ", m.tidx" : ""));
530 if (($usr_pg = pg_query($this->dbconn->db(), $usr_sql)) == FALSE ) {
531 log_crit(sprintf("%s::%s: pg_query usr_sql failed [%s]", __CLASS__, __FUNCTION__, $usr_sql));
534 $usr_n = pg_numrows($usr_pg);
535 if ($usr_n != BIN5_PLAYERS_N) {
536 log_crit(sprintf("%s::%s: wrong number of players [%s] %d", __CLASS__, __FUNCTION__, $usr_sql, $usr_n));
541 if ($with_minmaxtidx)
542 $fields = array('code', 'login', 'first', 'last', 'tidx');
544 $fields = array('code', 'login');
546 for ($u = 0 ; $u < $usr_n ; $u++) {
547 $usr_obj = pg_fetch_object($usr_pg, $u);
548 $users[$u] = array();
549 foreach($fields as $field) {
550 $users[$u][$field] = $usr_obj->$field;
556 // out: tab->{points,points_n,old_reason}, in: tab->ttok
557 function match_continue($match_code, $table, $tidx)
560 $sql_ttok = escsql($table->table_token);
562 if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) {
563 log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__));
567 $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code);
568 if (($num_pg = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) {
569 log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__));
572 $num_obj = pg_fetch_object($num_pg, 0);
573 $table->points_n = $num_obj->points_n;
575 // TAG: POINTS_MANAGEMENT
576 $tot_sql = sprintf("SELECT sum(p.pts * (2^g.mult)) AS pts
577 FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u,
578 %sbin5_table_orders AS o
579 WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code
580 AND p.ucode = o.ucode AND g.mcode = o.mcode
581 GROUP BY p.ucode, o.pos
583 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code);
584 if (($tot_pg = pg_query($this->dbconn->db(), $tot_sql)) == FALSE
585 || pg_numrows($tot_pg) != BIN5_PLAYERS_N) {
586 log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__));
591 foreach ($users as $user) {
592 // TAG: POINTS_MANAGEMENT
593 $pts_sql = sprintf("SELECT p.pts AS pts, g.mult AS mult
594 FROM %sbin5_points as p, %sbin5_games as g
595 WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d
596 ORDER BY g.tstamp ASC
597 LIMIT %d OFFSET %d;",
598 $G_dbpfx, $G_dbpfx, $match_code, $user['code'],
600 ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS));
602 // points of the match for each user
603 if (($pts_pg = $this->query($pts_sql)) == FALSE) {
604 log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__));
607 $pts_n = pg_numrows($pts_pg);
608 if ($pts_n > $table->points_n) {
609 // inconsistent scenario number of points great than number of games
610 log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__));
613 // TAG: POINTS_MANAGEMENT
614 for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) {
615 $pts_obj = pg_fetch_object($pts_pg, $i);
616 $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts * pow(2, $pts_obj->mult);
618 $tot_obj = pg_fetch_object($tot_pg, $u);
619 $table->total[$u] = $tot_obj->pts;
624 $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY tstamp DESC LIMIT 1;", $G_dbpfx, $match_code);
625 if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
626 log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__));
629 $gam_obj = pg_fetch_object($gam_pg, 0);
631 // update matches with new ttok and table idx
632 $mtc_sql = sprintf("UPDATE %sbin5_matches SET (ttok, tidx) = ('%s', %d) WHERE code = %d RETURNING *;",
633 $G_dbpfx, $sql_ttok, $tidx, $match_code);
634 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
635 log_crit(sprintf("%s::%s: update matches table failed", __CLASS__, __FUNCTION__));
638 $mtc_obj = pg_fetch_object($mtc_pg, 0);
640 $old_rules = $table->rules;
641 $rules_name = rules_id2name($mtc_obj->tcode);
642 $table->rules = new $rules_name($table);
645 $table->old_reason = ${rules_name}::game_description($gam_obj->act, 'html', $gam_obj->mult,
646 $gam_obj->asta_win, ($gam_obj->asta_win != -1 ?
647 $users[$gam_obj->asta_win]['login'] : ""),
648 $gam_obj->friend, ($gam_obj->friend != -1 ?
649 $users[$gam_obj->friend]['login'] : ""),
650 $gam_obj->pnt, $gam_obj->asta_pnt, $gam_obj->tourn_pts);
656 function match_order_get(&$match_data, $match_code, $exp_num)
660 $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;",
661 $G_dbpfx, $match_code);
663 if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) {
664 log_crit(sprintf("%s: fails for id or users number", __FUNCTION__));
669 for ($i = 0 ; $i < $exp_num ; $i++) {
670 $ord_obj = pg_fetch_object($ord_pg, $i);
671 $ucodes[$i] = $ord_obj->ucode;
674 if ($match_data !== NULL) {
675 $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;",
676 $G_dbpfx, $match_code);
678 if (($mtdt_pg = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) {
679 log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code));
683 $mtdt_obj = pg_fetch_object($mtdt_pg, 0);
685 foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) {
686 $match_data[$match_name] = $mtdt_obj->$match_name;
695 function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts)
698 $sql_ttok = escsql($table->table_token);
704 /* check the existence of the nick in the BriskDB */
705 log_main("bin5_points_save: ");
708 if ($this->query("BEGIN") == FALSE) {
717 $mtc_sql = sprintf("UPDATE %sbin5_matches SET (mazzo_next, mult_next) = (%d, %d) WHERE ttok = '%s' RETURNING *;",
718 $G_dbpfx, $table->mazzo, $table->mult, $sql_ttok);
719 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
721 // match not exists, insert it
722 // , BIN5_TOURNAMENT_NO_DRAW
723 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, ttype, tidx, mazzo_next, mult_next, tcode) VALUES ('%s', %d, %d, %d, %d, %d) RETURNING *;",
725 ($tidx < TABLES_CERT_N ? 1 : ($tidx < TABLES_AUTH_N ? 2 : (
726 $tidx < TABLES_APPR_N ? 3 : 4))),
727 $tidx, $table->mazzo, $table->mult, $table->rules->id_get());
728 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_affected_rows($mtc_pg) != 1) {
729 log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
732 $mtc_obj = pg_fetch_object($mtc_pg, 0);
734 for ($i = 0 ; $i < $n ; $i++) {
735 $ord_sql = sprintf("INSERT INTO %sbin5_table_orders (mcode, ucode, pos) VALUES (%d, %d, %d);",
736 $G_dbpfx, $mtc_obj->code, $ucodes[$i], $i);
737 if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_affected_rows($ord_pg) != 1 ) {
738 log_crit(sprintf("bin5_points_save: failed at insert table order [%s]", $ord_sql));
741 $codes_where .= sprintf("%scode = %d", ($i == 0 ? "" : " OR "), $ucodes[$i]);
746 $cnt_sql = sprintf("UPDATE %susers SET (match_cnt, game_cnt)
747 = (match_cnt+1, game_cnt+1) WHERE %s;",
748 $G_dbpfx, $codes_where);
750 if (($cnt_pg = $this->query($cnt_sql)) == FALSE || pg_affected_rows($cnt_pg) != $n) {
751 log_crit(sprintf("bin5_points_save: failed increment match and game [%s]", $cnt_sql));
756 $mtc_obj = pg_fetch_object($mtc_pg,0);
758 for ($i = 0 ; $i < $n ; $i++) {
759 $codes_where .= sprintf("%scode = %d", ($i == 0 ? "" : " OR "), $ucodes[$i]);
761 $cnt_sql = sprintf("UPDATE %susers SET (game_cnt)
762 = (game_cnt+1) WHERE %s;",
763 $G_dbpfx, $codes_where);
765 if (($cnt_pg = $this->query($cnt_sql)) == FALSE || pg_affected_rows($cnt_pg) != $n) {
766 log_crit(sprintf("bin5_points_save: failed increment game [%s]", $cnt_sql));
774 $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult, tourn_pts)
775 VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
776 $G_dbpfx, $mtc_obj->code, $date, $action,
777 $table->old_asta_pnt, $table->old_pnt,
778 $table->old_asta_win,
780 $table->old_mazzo, $table->old_mult,
781 $table->old_tourn_pts);
782 if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
783 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
787 $gam_obj = pg_fetch_object($gam_pg,0);
792 for ($i = 0 ; $i < $n ; $i++) {
794 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
795 VALUES (%d, %d, %d);",
796 $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
797 if (($pts_pg = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
798 log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
805 if ($this->query("COMMIT") == FALSE) {
811 $table->match_id = $mtc_obj->code;
816 $this->query("ROLLBACK");
821 function mail_add_fromitem($mail) {
825 INSERT INTO %smails (code, ucode, type, tstamp, subj, body_txt, body_htm, hash)
826 VALUES (%d, %d, %d, to_timestamp(%d), '%s', '%s', '%s', '%s') RETURNING *;",
827 $G_dbpfx, $mail->code, $mail->ucode, $mail->type, $mail->tstamp,
828 escsql($mail->subj), escsql($mail->body_txt), escsql($mail->body_htm),
829 ($mail->hash == NULL ? "" : escsql($mail->hash))
832 if (! (($usr_pg = $this->query($usr_sql)) != FALSE && pg_affected_rows($usr_pg) == 1) ) {
835 $usr_obj = pg_fetch_object($usr_pg, 0);
840 function mail_check($code, $type, $hash)
844 $mai_sql = sprintf("SELECT * FROM %smails WHERE code = %d AND type = %d AND hash = '%s';",
845 $G_dbpfx, $code, $type, escsql($hash));
846 if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_numrows($mai_pg) != 1) {
851 $mai_obj = pg_fetch_object($mai_pg, 0);
855 function mail_delete($code)
859 $mai_sql = sprintf("DELETE FROM %smails WHERE code = %d;", $G_dbpfx, $code);
861 if (($mai_pg = $this->query($mai_sql)) == FALSE || pg_affected_rows($mai_pg) != 1) {
867 function friendship_default()
869 return (array(usersnet_friend_getlabel(1) => "0",
870 usersnet_friend_getlabel(2) => "0",
871 usersnet_friend_getlabel(3) => "0",
872 usersnet_friend_getlabel(4) => "0",
873 usersnet_friend_getlabel(5) => "0"));
876 function usersnet_widefriend($owner, $target)
880 $widefriend = $this->friendship_default();
882 $wfri_sql = sprintf("SELECT * FROM %susersnet_widefriend WHERE owner = %d AND target = %d;",
883 $G_dbpfx, $owner, $target);
884 if (($wfri_pg = $this->query($wfri_sql)) == FALSE) {
885 return ($widefriend);
888 for ($i = 0 ; $i < pg_numrows($wfri_pg) ; $i++) {
889 $wfri_obj = pg_fetch_object($wfri_pg, $i);
890 $widefriend[usersnet_friend_getlabel(intval($wfri_obj->friend))] = $wfri_obj->count;
893 return ($widefriend);
896 function usersnet_wideskill($owner, $target)
902 $wskl_sql = sprintf("SELECT * FROM %susersnet_wideskill WHERE owner = %d AND target = %d;",
903 $G_dbpfx, $owner, $target);
904 if (($wskl_pg = $this->query($wskl_sql)) == FALSE) {
908 if (pg_numrows($wskl_pg) > 0) {
909 $wskl_obj = pg_fetch_object($wskl_pg, 0);
910 // TODO: UNCOMMENT IF THE NETWORK WORKS VERY WELL
911 // if ($wskl_obj->count >= 3)
912 $wideskill = sprintf("%3.2f", $wskl_obj->skill);
917 function usersnet_narrowfriend($owner, $target)
921 $narrowfriend = $this->friendship_default();
923 $nfri_sql = sprintf("SELECT * FROM %susersnet_narrowfriend WHERE owner = %d AND target = %d;",
924 $G_dbpfx, $owner, $target);
925 if (($nfri_pg = $this->query($nfri_sql)) == FALSE) {
926 return $narrowfriend;
929 for ($i = 0 ; $i < pg_numrows($nfri_pg) ; $i++) {
930 $nfri_obj = pg_fetch_object($nfri_pg, $i);
931 $narrowfriend[usersnet_friend_getlabel(intval($nfri_obj->friend))] = $nfri_obj->count;
933 return ($narrowfriend);
936 function usersnet_narrowskill($owner, $target)
942 $nskl_sql = sprintf("SELECT * FROM %susersnet_narrowskill WHERE owner = %d AND target = %d;",
943 $G_dbpfx, $owner, $target);
944 if (($nskl_pg = $this->query($nskl_sql)) == FALSE) {
945 return ($narrowskill);
948 if (pg_numrows($nskl_pg) > 0) {
949 $nskl_obj = pg_fetch_object($nskl_pg, 0);
950 // TODO: UNCOMMENT IF THE NETWORK WORKS VERY WELL
951 // if ($nskl_obj->count >= 3)
952 $narrowskill = sprintf("%3.2f", $nskl_obj->skill);
954 return ($narrowskill);
957 function usersnet_partyskill($owner, $target)
961 $partyskill = "non disponibile";
963 $pskl_sql = sprintf("SELECT * FROM %susersnet_party WHERE owner = %d AND target = %d;",
964 $G_dbpfx, $owner, $target);
965 if (($pskl_pg = $this->query($pskl_sql)) == FALSE) {
966 return ($partyskill);
969 if (pg_numrows($pskl_pg) > 0) {
970 $pskl_obj = pg_fetch_object($pskl_pg, 0);
971 // TODO: UNCOMMENT IF THE NETWORK WORKS VERY WELL
972 // if ($wskl_obj->count >= 3)
973 $partyskill = sprintf("%3.2f", $pskl_obj->skill);
975 return ($partyskill);
978 function usersnet_bycode($owner, $target, $widefriend, $narrowfriend)
983 $net_sql = sprintf("SELECT * FROM %susersnet WHERE owner = %d AND target = %d;",
984 $G_dbpfx, $owner, $target);
985 if (($net_pg = $this->query($net_sql)) == FALSE)
988 if (pg_numrows($net_pg) != 1)
991 $net_obj = pg_fetch_object($net_pg, 0);
993 return (UsersNetItem::UsersNetItemFromRecord($net_obj, $widefriend, $narrowfriend));
996 function usersnet_default($owner, $target, $widefriend, $narrowfriend)
998 return (UsersNetItem::UsersNetItemDefaults($owner, $target, $widefriend, $narrowfriend));
1001 function usersnet_save($owner_id, $json)
1008 $friend = usersnet_friend_getid($json->friend);
1010 $json->skill = intval($json->skill);
1011 $json->trust = intval($json->trust);
1013 if ($json->skill < 1 || $json->skill > 5 ||
1014 $json->trust < 1 || $json->trust > 5 ||
1019 $this->transaction('BEGIN');
1022 if ($friend == USERSNET_FRIEND_UNKNOWN) {
1024 $net_sql = sprintf("
1025 DELETE FROM %susersnet
1027 WHERE owner = %d AND us.login = '%s' AND target = us.code;",
1029 $owner_id, escsql(strtolower($json->login)));
1031 if (($net_pg = $this->query($net_sql)) == FALSE) {
1036 else { // if ($friend == USERSNET_FRIEND_UNKNOWN
1038 $net_sql = sprintf("
1039 UPDATE %susersnet SET (friend, skill, trust, mtime) =
1042 WHERE owner = %d AND us.login = '%s' AND target = us.code RETURNING *;",
1044 $friend, $json->skill, $json->trust,
1046 $owner_id, escsql(strtolower($json->login)));
1047 if (($net_pg = $this->query($net_sql)) == FALSE || pg_numrows($net_pg) == 0) {
1048 $net_sql = sprintf("
1049 INSERT INTO %susersnet SELECT %d AS owner, us.code as target,
1050 %d as friend, %d as skill, %d as trust
1051 FROM %susers as us WHERE us.login = '%s' RETURNING *;",
1052 $G_dbpfx, $owner_id,
1053 $friend, $json->skill, $json->trust,
1054 $G_dbpfx, escsql(strtolower($json->login)));
1055 if (($net_pg = $this->query($net_sql)) == FALSE) {
1056 log_wr('insert query failed');
1060 if (pg_numrows($net_pg) != 1) {
1061 log_wr(sprintf('insert numrow failed [%s] [%d]', $net_sql, pg_numrows($net_pg)));
1067 if (pg_numrows($net_pg) != 1) {
1068 log_wr('update numrow failed');
1074 $this->transaction('COMMIT');
1079 $this->transaction('ROLLBACK');
1083 } // End class BriskDB
1090 function LoginDBOld($filename)
1092 GLOBAL $DOCUMENT_ROOT;
1093 log_main("LoginDBOld create:start");
1095 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
1096 require("$DOCUMENT_ROOT/Etc/".$filename);
1101 $this->item_n = count($this->item);
1102 log_main("LoginDBOld create:end");
1107 return ($this->item_n);
1110 } // End class LoginDBOld