From d9f1de409fac8d9bff593813fec3db1f0c08cdc7 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Mon, 7 Mar 2011 08:31:22 +0100 Subject: [PATCH] create static method in BriskDB function to allow constructor fail and manage of the failure in the code --- web/Obj/brisk.phh | 79 +++++++++++++++----------- web/Obj/dbase_pgsql.phh | 2 +- web/admin.php | 13 +++-- web/briskin5/Obj/placing.phh | 13 +++-- web/briskin5/index_wr.php | 11 ++-- web/briskin5/statadm.php | 13 +++-- web/index_wr.php | 106 +++++++++++++++++------------------ 7 files changed, 132 insertions(+), 105 deletions(-) diff --git a/web/Obj/brisk.phh b/web/Obj/brisk.phh index 480f3a2..07c6168 100644 --- a/web/Obj/brisk.phh +++ b/web/Obj/brisk.phh @@ -120,7 +120,9 @@ $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi', 'tit_onauth'=>array( 'it' => '(solo aut.)', 'en' => '(only aut.)'), 'tit_onisol'=>array( 'it' => '(isolam.to)', - 'en' => '(isolation)') + 'en' => '(isolation)'), + 'db_failed' =>array('it' => '
Il collegamento al database è fallito.
Temporaneamente tutte le autenticazioni verranno sospese, accederai a Brisk come un utente normale.

Ci scusiamo per il disagio.', + 'en' => 'Connection to the database failed
All authentications are suspended temporarly, you login as normal user.
We are about the limitation') ); @@ -129,7 +131,7 @@ $G_false = FALSE; $G_lng = langtolng($G_lang); $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 ); -$G_brisk_version = "2.5.0"; +$G_brisk_version = "3.2.0"; /* MLANG: ALL THE INFO STRINGS IN brisk.phh */ $root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), NOVITA\': informazioni al tavolo sul chiamante, nuovo stato di supporter del sito.', @@ -572,10 +574,11 @@ class Table { define(USER_FLAG_AUTH, 0x02); define(USER_FLAG_MAP_AUTH, 0x0c); - define(USER_FLAG_LISTAUTH, 0x04); define(USER_FLAG_ISOLAUTH, 0x08); +define(USER_FLAG_DBFAILED, 0x10); + // user status define(USER_FLAG_S_NORM, 0x000); // done define(USER_FLAG_S_PAU, 0x100); // done @@ -1127,6 +1130,11 @@ class Room { $ret .= sprintf('stat = "%s";', $user->stat); $ret .= root_wellcome($user); + if ($user->flags & USER_FLAG_DBFAILED) { + $ret .= "gst.st = ".($user->step+1)."; "; + $ret .= show_notify($mlang_brisk['db_failed'][$G_lang], 0, $mlang_brisk['btn_close'][$G_lang], 400, 140); + } + $ret .= sprintf('subst = "%s";', $user->subst); $itin = ($user->flags & USER_FLAG_AUTH ? "" : ""); $itou = ($user->flags & USER_FLAG_AUTH ? "" : ""); @@ -1627,14 +1635,14 @@ class Room { $user->name = $name_new; // OK - nick changed /* se nome gia' in uso, segnala cosa potrebbe capitare */ if (($user->flags & USER_FLAG_AUTH) == 0) { - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); - $bdb->users_load(); - /* MLANG: "Il nickname \'%s\' è già registrato, se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghostN." */ - if ($bdb->login_exists($name_new)) { - $prestr = sprintf($mlang_brisk['nickjust'][$G_lang], xcape($name_new)); - $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr); - } + if (($bdb = BriskDB::create()) != FALSE) { + $bdb->users_load(); + /* MLANG: "Il nickname \'%s\' è già registrato, se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghostN." */ + if ($bdb->login_exists($name_new)) { + $prestr = sprintf($mlang_brisk['nickjust'][$G_lang], xcape($name_new)); + $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr); + } + } } log_main("chatt_send start set"); @@ -1864,8 +1872,6 @@ class Room { return ($G_false); } - - /* * function &add_user(&$room, &$sess, &$idx, $name, $pass, $ip) * @@ -1875,11 +1881,9 @@ class Room { * if ($idx == -3 && ret == FALSE) => wrong password * if ($idx == -1 && ret == FALSE) => no space left * if ($idx == 0 && ret == user) => SUCCESS - * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db + * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db) */ - - function &add_user(&$sess, &$idx, $name, $pass, $ip) { GLOBAL $G_base, $G_false, $CO_list; @@ -1907,24 +1911,28 @@ class Room { /* if pass != FALSE verify the login with pass */ log_auth("XXX", "auth1"); - // FIXME: now create can return FALSE - if (($bdb = BriskDB::create()) != FALSE) + if (($bdb = BriskDB::create()) != FALSE) { $bdb->users_load(); - if ($pass != FALSE && $bdb != FALSE) { // TODO: here add a method to $bdb to check if the db is available. - log_auth("XXX", "auth2"); - $authenticate = $bdb->login_verify($name_new, $pass, $code); - log_auth("XXX", "authenticate: ".($authenticate != FALSE ? "TRUE" : "FALSE")); - - if ($authenticate != FALSE) { - $user_type = $authenticate->type_get(); - } - else { - $idx = -3; - return ($G_false); - } + if ($pass != FALSE) { // TODO: here add a method to $bdb to check if the db is available. + log_auth("XXX", "auth2"); + $authenticate = $bdb->login_verify($name_new, $pass, $code); + log_auth("XXX", "authenticate: ".($authenticate != FALSE ? "TRUE" : "FALSE")); + + if ($authenticate != FALSE) { + $user_type = $authenticate->type_get(); + } + else { + $idx = -3; + return ($G_false); + } + } + else { + $login_exists = $bdb->login_exists($name_new); + } } else { - $login_exists = $bdb->login_exists($name_new); + // if db is down, send a warning and verify only current users + // no actions at this moment } for ($i = 0 ; $i < MAX_PLAYERS ; $i++) { /* free user ? */ @@ -1932,7 +1940,7 @@ class Room { if ($idx == -1) $idx = $i; } - if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) { + if ($idfree == -1 && strcmp($this->user[$i]->sess, "") == 0) { $idfree = $i; continue; // NOTE: CHECK IT !! } @@ -1952,6 +1960,8 @@ class Room { log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new)); + /* there is another user logged with your account and you and him have authenticated => che new user + get the session of the old user */ if ($ghost > -1 && $ghost_auth && ($authenticate != FALSE)) { /* swap session */ @@ -2006,8 +2016,9 @@ class Room { $this->user[$idx]->bantime = 0; $this->user[$idx]->ip = $ip; - $this->user[$idx]->flags = ($authenticate != FALSE ? USER_FLAG_AUTH : 0x00) | $user_type; - + $this->user[$idx]->flags = $user_type; + $this->user[$idx]->flags |= ($authenticate != FALSE ? USER_FLAG_AUTH : 0x00); + $this->user[$idx]->flags |= ( ($pass != FALSE && $bdb == FALSE) ? USER_FLAG_DBFAILED : 0x00); log_auth("XXX", sprintf("FLAGS: [%x]", $this->user[$idx]->flags)); diff --git a/web/Obj/dbase_pgsql.phh b/web/Obj/dbase_pgsql.phh index c3643bb..f4aa819 100644 --- a/web/Obj/dbase_pgsql.phh +++ b/web/Obj/dbase_pgsql.phh @@ -52,7 +52,7 @@ class DBConn if (DBConn::$dbcnnx == FALSE) { if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) { - return FALSE; + return ($ret); } } diff --git a/web/admin.php b/web/admin.php index 7c87e1d..6c65f9e 100644 --- a/web/admin.php +++ b/web/admin.php @@ -102,8 +102,10 @@ function main() break; } - // FIXME: now create can return FALSE - $newdb = BriskDB::create(); + if (($newdb = BriskDB::create()) == FALSE) { + $cont .= sprintf("Database connection failed.
\n"); + break; + } $newdb->users_load(); if ($newdb->addusers_from_olddb($olddb, $cont) == FALSE) { $cont .= sprintf("Insert failed.
\n"); @@ -125,8 +127,11 @@ function main() break; } - // FIXME: now create can return FALSE - $newdb = BriskDB::create(); + if (($newdb = BriskDB::create()) == FALSE) { + $cont .= sprintf("Database connection failed.
\n"); + break; + } + $newdb->users_load(); $dbconn = $newdb->getdbconn(); for ($pts_n = 0 ; !feof($fp) ; $pts_n++) { diff --git a/web/briskin5/Obj/placing.phh b/web/briskin5/Obj/placing.phh index 5e1144a..81a4417 100644 --- a/web/briskin5/Obj/placing.phh +++ b/web/briskin5/Obj/placing.phh @@ -142,8 +142,11 @@ function placing_time_file() function placing_time_pgsql() { GLOBAL $G_dbpfx; - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); + + if (($bdb = BriskDB::create()) == FALSE) { + log_crit("placing: database connection failed"); + return (FALSE); + } $mti_sql = sprintf("SELECT CAST(EXTRACT(EPOCH FROM mtime) AS INTEGER) as mtime FROM %sbin5_places_mtime WHERE code = 0;", $G_dbpfx); @@ -252,8 +255,10 @@ function placing_show_pgsql(&$user, $ty, $subty) { GLOBAL $G_dbpfx; - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); + if (($bdb = BriskDB::create()) == FALSE) { + log_crit("placing: database connection failed"); + return (""); + } if ($user != FALSE) { $pla_sql = sprintf("SELECT * from %sbin5_places where type = %d AND (rank <= %d OR ucode = '%s');", diff --git a/web/briskin5/index_wr.php b/web/briskin5/index_wr.php index 6c27840..1c0735e 100644 --- a/web/briskin5/index_wr.php +++ b/web/briskin5/index_wr.php @@ -494,11 +494,14 @@ else if ($user->stat == 'table') { if ($user->table_orig < TABLES_AUTH_N) { require_once("../Obj/dbase_".$G_dbasetype.".phh"); + if (($bdb = BriskDB::create()) != FALSE) { + $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur); + unset($bdb); + } + else { + log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", "DATABASE CONNECTION FAILED"); + } log_points($curtime, $user, "STAT:BRISKIN5:FINISH_GAME", $plist); - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); - $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur); - unset($bdb); } $table->game_next(); diff --git a/web/briskin5/statadm.php b/web/briskin5/statadm.php index 15fd27c..07b847e 100644 --- a/web/briskin5/statadm.php +++ b/web/briskin5/statadm.php @@ -58,8 +58,11 @@ function main_file($curtime) fclose($fp_start); } - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); + if (($bdb = BriskDB::create()) == FALSE) { + echo "database connection failed"; + exit; + } + $bdb->users_load(); for ($i = 0 ; $i < $bdb->count() ; $i++) { @@ -211,8 +214,10 @@ function main_pgsql($curtime) { GLOBAL $G_dbpfx; - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); + if (($bdb = BriskDB::create()) == FALSE) { + echo "database connection failed"; + exit; + } $limi = array( TRI_LIMIT, MON_LIMIT, WEE_LIMIT ); $ming = array( TRI_MIN_GAMES, MON_MIN_GAMES, WEE_MIN_GAMES ); diff --git a/web/index_wr.php b/web/index_wr.php index 8e20cd9..1c8dd20 100644 --- a/web/index_wr.php +++ b/web/index_wr.php @@ -42,6 +42,8 @@ $mlang_indwr = array( 'btn_backtotab' => array( 'it' => 'Torna ai tavoli.', 'en' => 'close' ), 'commerr' => array( 'it' => 'E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.', 'en' => 'An error was occurred during the saving, try again or contact the administrator.'), + 'coerrdb' => array( 'it' => 'Il database è temporaneamente irraggiungibile, riprova più tardi o contatta l\'amministratore.', + 'en' => 'The database is temporarly unavailable, retry to later or conctact the administrator.'), 'warrmust' => array( 'it' => 'Per autenticare qualcuno devi a tua volta essere autenticato.', 'en' => 'To authenticate somebody you have to be authenticated in your turn'), 'mesgrepl' => array( 'it' => '

Il messaggio è stato inoltrato all\'amministratore.', @@ -101,6 +103,9 @@ $is_spawn = FALSE; log_mop(0, 'index_wr.php: COMM: '.xcapemesg($mesg)); log_wr('COMM: '.xcapemesg($mesg)); +$curtime = time(); +$dt = date("H:i ", $curtime); + $sem = Room::lock_data(); if (($room = &Room::load_data()) == FALSE) { echo "Load data error"; @@ -118,7 +123,6 @@ if (($user = &$room->get_user($sess, &$idx)) == FALSE) { log_main("chal lock data success"); if (($chals = &Challenges::load_data()) != FALSE) { - $curtime = time(); $token = uniqid(""); // echo '2|'.$argz[1].'|'.$token.'|'.$_SERVER['REMOTE_ADDR'].'|'.$curtime.'|'; @@ -200,7 +204,6 @@ if ($argz[0] == 'shutdown') { else if ($argz[0] == 'warranty') { GLOBAL $cli_name, $cli_email; - $curtime = time(); $mesg_to_user = ""; log_wr("INFO:SKIP:argz == warranty name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH)); @@ -232,60 +235,62 @@ else if ($argz[0] == 'warranty') { if ($mesg_to_user != "") { $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; - $dt = date("H:i ", $curtime); $user->comm[$user->step % COMM_N] .= $mesg_to_user; $user->step_inc(); } } else if ($argz[0] == 'mesgtoadm') { - GLOBAL $cli_subj, $cli_mesg; - - $curtime = time(); - $mesg_to_user = ""; - - log_wr("INFO:SKIP:argz == mesgtoadm name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH)); - if ($user->flags & USER_FLAG_AUTH) { - if (($wa_lock = Warrant::lock_data()) != FALSE) { - // FIXME: now create can return FALSE - $bdb = BriskDB::create(); - $bdb->users_load(); - - if (($ema = $bdb->getmail($user->name)) != FALSE) { - // mail("nastasi", - mail("brisk@alternativeoutput.it", urldecode($cli_subj), urldecode($cli_mesg), sprintf("From: %s <%s>", $user->name, $ema)); - } - - if (($fp = @fopen(LEGAL_PATH."/messages.txt", 'a')) != FALSE) { - /* Unix time | session | nickname | IP | where was | mesg */ - fwrite($fp, sprintf("%ld|%s|%s|%s\n", $curtime, $user->name, - xcapelt(urldecode($cli_subj)), xcapelt(urldecode($cli_mesg)))); - fclose($fp); - } - Warrant::unlock_data($wa_lock); - $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; - /* MLANG: "" */ - $user->comm[$user->step % COMM_N] .= show_notify($mlang_indwr['mesgrepl'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110); - $user->step_inc(); - echo "1"; + GLOBAL $cli_subj, $cli_mesg; + + $mesg_to_user = ""; + + log_wr("INFO:SKIP:argz == mesgtoadm name: [".$user->name."] AUTH: ".($user->flags & USER_FLAG_AUTH)); + if ($user->flags & USER_FLAG_AUTH) { + if (($wa_lock = Warrant::lock_data()) != FALSE) { + if (($bdb = BriskDB::create()) != FALSE) { + $bdb->users_load(); + + if (($ema = $bdb->getmail($user->name)) != FALSE) { + // mail("nastasi", + mail("brisk@alternativeoutput.it", urldecode($cli_subj), urldecode($cli_mesg), sprintf("From: %s <%s>", $user->name, $ema)); + } + + if (($fp = @fopen(LEGAL_PATH."/messages.txt", 'a')) != FALSE) { + /* Unix time | session | nickname | IP | where was | mesg */ + fwrite($fp, sprintf("%ld|%s|%s|%s\n", $curtime, $user->name, + xcapelt(urldecode($cli_subj)), xcapelt(urldecode($cli_mesg)))); + fclose($fp); + } + Warrant::unlock_data($wa_lock); + $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; + /* MLANG: "" */ + $user->comm[$user->step % COMM_N] .= show_notify($mlang_indwr['mesgrepl'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110); + $user->step_inc(); + echo "1"; + } + else { + /* MLANG: "Il database è temporaneamente irraggiungibile, riprova più tardi o contatta l\'amministratore." */ + $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['coerrdb'][$G_lang]); + $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; + } + } + else { + /* MLANG: "E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore." */ + $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]); + } + } else { - /* MLANG: "E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore." */ - $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]); + /* MLANG: "Per autenticare qualcuno devi a tua volta essere autenticato." */ + $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['mesgmust'][$G_lang]); } - } - else { - /* MLANG: "Per autenticare qualcuno devi a tua volta essere autenticato." */ - $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['mesgmust'][$G_lang]); - } - - if ($mesg_to_user != "") { - $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; - - $dt = date("H:i ", $curtime); - $user->comm[$user->step % COMM_N] .= $mesg_to_user; - $user->step_inc(); - } + if ($mesg_to_user != "") { + $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; + + $user->comm[$user->step % COMM_N] .= $mesg_to_user; + $user->step_inc(); + } } @@ -294,7 +299,6 @@ else if ($argz[0] == 'poll') { GLOBAL $G_with_poll, $G_poll_name, $cli_choose, $cli_poll_name; $poll_lock = FALSE; - $curtime = time(); $mesg_to_user = ""; $fp = FALSE; @@ -392,7 +396,6 @@ else if ($argz[0] == 'poll') { if ($mesg_to_user != "") { $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; - $dt = date("H:i ", $curtime); $user->comm[$user->step % COMM_N] .= $mesg_to_user; $user->step_inc(); } @@ -502,13 +505,10 @@ else if ($user->stat == 'room') { $table_idx = $argz[1]; $table = &$room->table[$table_idx]; - $curtime = time(); - if ($G_shutdown || $table->wakeup_time > $curtime || ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) ) { $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; "; - $dt = date("H:i ", $curtime); /* MLANG: "Il server sta per essere riavviato, non possono avere inizio nuove partite.", "Il tavolo a cui volevi sederti richiede autentifica.", "Il tavolo si è appena liberato, ci si potrà sedere tra %d secondi." */ if ($G_shutdown) { $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['shutmsg'][$G_lang]); @@ -569,8 +569,6 @@ else if ($user->stat == 'room') { // START THE SPAWN HERE!!!! // - $curtime = time(); - // Create new spawned table $bri_sem = Briskin5::lock_data($table_idx); $table_token = uniqid(""); -- 2.17.1