3 * brisk - dbase_pgsql.phh
5 * Copyright (C) 2006-2012 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 users_load()
129 function login_exists($login)
133 /* check the existence of the nick in the BriskDB */
134 log_main("login_exists: ".$login);
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)
145 function getrecord_bylogin($login) {
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) {
152 if (pg_numrows($user_pg) != 1)
155 $user_obj = pg_fetch_object($user_pg, 0);
160 function getrecord_bycode($code) {
163 $user_sql = sprintf("SELECT * FROM %susers WHERE code = %d;", $G_dbpfx, $code);
164 if (($user_pg = $this->query($user_sql)) == FALSE) {
167 if (pg_numrows($user_pg) != 1)
170 $user_obj = pg_fetch_object($user_pg, 0);
175 function user_update_login_time($code, $lintm)
179 $user_sql = sprintf("UPDATE %susers SET (lintm) = (date 'epoch' + %d * INTERVAL '1 second') WHERE code = %d;", $G_dbpfx, $lintm, $code);
181 if ( ! (($user_pg = $this->query($user_sql)) != FALSE && pg_affected_rows($user_pg) == 1) ) {
188 function user_prefs_update($code, $flags, $supp_comp)
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) ) {
198 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
203 function user_state_update($code, $flags, $disa_reas)
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) ) {
213 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
218 function user_tos_update($code, $tos_vers)
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) ) {
228 fprintf(STDERR, "REQUEST GOOD [%s]\n", $user_sql);
234 if success return a LoginDBItem object
236 function login_verify($login, $pass)
242 log_main("login_verify: ".$login);
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");
248 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
252 log_main("login[".$user_obj->code."]: ".$user_obj->login);
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)."]");
262 if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
263 log_main("login_verify SUCCESS for ".$login);
266 $this->user_update_login_time($user_obj->code, time());
267 $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
271 } // end for ($e = 0 ...
274 if ($chals->ismod()) {
275 Challenges::save_data(&$chals);
278 Challenges::unlock_data($a_sem);
281 // O } // if (strcasecmp($this->item[$i]->login, ...
287 function getitem_bylogin($login, &$id) {
291 log_main("getitem_bylogin: ".$login);
293 if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
296 $id = $user_obj->code;
297 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
300 function getitem_bycode($code) {
303 log_main("getitem_bycode: ".$code);
305 if (($user_obj = $this->getrecord_bycode($code)) == FALSE)
308 return (LoginDBItem::LoginDBItemFromRecord($user_obj));
312 function getmail($login)
316 if (($ret = $this->getrecord_bylogin($login)) == FALSE)
319 return ($ret->email);
322 function addusers_from_olddb($olddb, &$cont)
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);
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));
342 return ($this->dbconn);
345 // return array of array('code', 'login' [, 'first', 'last', 'tidx']) ordered by table position
346 function users_get($match_code, $with_minmaxtidx, $is_newmatch)
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
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" : ""));
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" : ""));
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));
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));
383 if ($with_minmaxtidx)
384 $fields = array('code', 'login', 'first', 'last', 'tidx');
386 $fields = array('code', 'login');
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;
398 // out: tab->{points,points_n,old_reason}, in: tab->ttok
399 function match_continue($match_code, $table, $tidx)
403 if (($users = $this->users_get($match_code, FALSE /*without minmaxidx*/, TRUE /*new game*/)) == FALSE) {
404 log_crit(sprintf("%s::%s: retrieve users fails", __CLASS__, __FUNCTION__));
408 $num_sql = sprintf("SELECT count(*) AS points_n FROM %sbin5_games WHERE mcode = %d;", $G_dbpfx, $match_code);
409 if (($num_pg = $this->query($num_sql)) == FALSE || pg_numrows($num_pg) != 1) {
410 log_crit(sprintf("%s::%s: get games number fails", __CLASS__, __FUNCTION__));
413 $num_obj = pg_fetch_object($num_pg, 0);
414 $table->points_n = $num_obj->points_n;
416 $tot_sql = sprintf("SELECT sum(p.pts) AS pts
417 FROM %sbin5_games AS g, %sbin5_points AS p, %susers AS u,
418 %sbin5_table_orders AS o
419 WHERE g.mcode = %d AND g.code = p.gcode AND p.ucode = u.code
420 AND p.ucode = o.ucode AND g.mcode = o.mcode
421 GROUP BY p.ucode, o.pos
423 $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $match_code);
424 if (($tot_pg = pg_query($this->dbconn->db(), $tot_sql)) == FALSE
425 || pg_numrows($tot_pg) != BIN5_PLAYERS_N) {
426 log_crit(sprintf("%s::%s: get games totals fails", __CLASS__, __FUNCTION__));
431 foreach ($users as $user) {
432 $pts_sql = sprintf("SELECT p.pts AS pts
433 FROM %sbin5_points as p, %sbin5_games as g
434 WHERE p.gcode = g.code AND g.mcode = %d AND p.ucode = %d
436 LIMIT %d OFFSET %d;",
437 $G_dbpfx, $G_dbpfx, $match_code, $user['code'],
439 ($num_obj->points_n < MAX_POINTS ? 0 : $num_obj->points_n - MAX_POINTS));
441 // points of the match for each user
442 if (($pts_pg = $this->query($pts_sql)) == FALSE) {
443 log_crit(sprintf("%s::%s: get points fails", __CLASS__, __FUNCTION__));
446 $pts_n = pg_numrows($pts_pg);
447 if ($pts_n > $table->points_n) {
448 // inconsistent scenario number of points great than number of games
449 log_crit(sprintf("%s::%s: number of points great than number of games", __CLASS__, __FUNCTION__));
452 for ($i = 0 , $ct = $table->points_n - $pts_n; $ct < $table->points_n ; $ct++, $i++) {
453 $pts_obj = pg_fetch_object($pts_pg, $i);
454 $table->points[$ct % MAX_POINTS][$u] = $pts_obj->pts;
456 $tot_obj = pg_fetch_object($tot_pg, $u);
457 $table->total[$u] = $tot_obj->pts;
462 $gam_sql = sprintf("SELECT * FROM %sbin5_games WHERE mcode = %d ORDER BY code DESC LIMIT 1;", $G_dbpfx, $match_code);
463 if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_numrows($gam_pg) != 1) {
464 log_crit(sprintf("%s::%s: get last game fails", __CLASS__, __FUNCTION__));
467 $gam_obj = pg_fetch_object($gam_pg, 0);
469 $table->old_reason = game_description($gam_obj->act, 'html', $gam_obj->mult,
470 $gam_obj->asta_win, ($gam_obj->asta_win != -1 ?
471 $users[$gam_obj->asta_win]['login'] : ""),
472 $gam_obj->friend, ($gam_obj->friend != -1 ?
473 $users[$gam_obj->friend]['login'] : ""),
474 $gam_obj->pnt, $gam_obj->asta_pnt);
479 function match_order_get(&$match_data, $match_code, $exp_num)
483 $ord_sql = sprintf("SELECT ucode FROM %sbin5_table_orders WHERE mcode = %d ORDER BY pos ASC;",
484 $G_dbpfx, $match_code);
486 if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_numrows($ord_pg) != $exp_num) {
487 log_crit(sprintf("%s: fails for id or users number", __FUNCTION__));
492 for ($i = 0 ; $i < $exp_num ; $i++) {
493 $ord_obj = pg_fetch_object($ord_pg, $i);
494 $ucodes[$i] = $ord_obj->ucode;
497 if ($match_data !== NULL) {
498 $mtdt_sql = sprintf("SELECT * FROM %sbin5_matches WHERE code = %d;",
499 $G_dbpfx, $match_code);
501 if (($mtdt_pg = $this->query($mtdt_sql)) == FALSE || pg_numrows($mtdt_pg) != 1) {
502 log_crit(sprintf("%s: fails retrieve match_data values [%d]", __FUNCTION__, $match_code));
506 $mtdt_obj = pg_fetch_object($mtdt_pg, 0);
508 foreach (array('ttok', 'tidx', 'mult_next', 'mazzo_next', 'tcode') as $match_name) {
509 $match_data[$match_name] = $mtdt_obj->$match_name;
518 function bin5_points_save($date, $table, $tidx, $action, $ucodes, $pts)
521 $sql_ttok = escsql($table->table_token);
527 /* check the existence of the nick in the BriskDB */
528 log_main("bin5_points_save: ");
531 if ($this->query("BEGIN") == FALSE) {
539 $mtc_sql = sprintf("UPDATE %sbin5_matches SET (mazzo_next, mult_next) = (%d, %d) WHERE ttok = '%s' RETURNING *;",
540 $G_dbpfx, $table->mazzo, $table->mult, $sql_ttok);
541 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
543 // match not exists, insert it
544 $mtc_sql = sprintf("INSERT INTO %sbin5_matches (ttok, tidx, mazzo_next, mult_next) VALUES ('%s', %d, %d, %d) RETURNING *;",
545 $G_dbpfx, $sql_ttok, $tidx, $table->mazzo, $table->mult);
546 if (($mtc_pg = $this->query($mtc_sql)) == FALSE || pg_affected_rows($mtc_pg) != 1) {
547 log_crit(sprintf("bin5_points_save: failed at insert match [%s]", $mtc_sql));
550 $mtc_obj = pg_fetch_object($mtc_pg, 0);
552 for ($i = 0 ; $i < $n ; $i++) {
553 $ord_sql = sprintf("INSERT INTO %sbin5_table_orders (mcode, ucode, pos) VALUES (%d, %d, %d);",
554 $G_dbpfx, $mtc_obj->code, $ucodes[$i], $i);
555 if (($ord_pg = $this->query($ord_sql)) == FALSE || pg_affected_rows($ord_pg) != 1 ) {
556 log_crit(sprintf("bin5_points_save: failed at insert table order [%s]", $ord_sql));
564 $mtc_obj = pg_fetch_object($mtc_pg,0);
570 $gam_sql = sprintf("INSERT INTO %sbin5_games (mcode, tstamp, act, asta_pnt, pnt, asta_win, friend, mazzo, mult)
571 VALUES (%d, to_timestamp(%d), %d, %d, %d, %d, %d, %d, %d) RETURNING *;",
572 $G_dbpfx, $mtc_obj->code, $date, $action,
573 $table->old_asta_pnt, $table->old_pnt,
574 $table->old_asta_win,
576 $table->old_mazzo, $table->old_mult);
577 if (($gam_pg = $this->query($gam_sql)) == FALSE || pg_affected_rows($gam_pg) != 1) {
578 log_crit(sprintf("bin5_points_save: failed at insert game [%s]", $gam_sql));
582 $gam_obj = pg_fetch_object($gam_pg,0);
587 for ($i = 0 ; $i < $n ; $i++) {
589 $pts_sql = sprintf("INSERT INTO %sbin5_points (gcode, ucode, pts)
590 VALUES (%d, %d, %d);",
591 $G_dbpfx, $gam_obj->code, $ucodes[$i], $pts[$i]);
592 if (($pts_pg = $this->query($pts_sql)) == FALSE || pg_affected_rows($pts_pg) != 1) {
593 log_crit(sprintf("bin5_points_save: failed at insert point [%s]", $pts_sql));
600 if ($this->query("COMMIT") == FALSE) {
606 $table->match_id = $mtc_obj->code;
611 $this->query("ROLLBACK");
616 } // End class BriskDB
623 function LoginDBOld($filename)
625 GLOBAL $DOCUMENT_ROOT;
626 log_main("LoginDBOld create:start");
628 if (file_exists("$DOCUMENT_ROOT/Etc/".$filename)) {
629 require("$DOCUMENT_ROOT/Etc/".$filename);
634 $this->item_n = count($this->item);
635 log_main("LoginDBOld create:end");
640 return ($this->item_n);
643 } // End class LoginDBOld