pull conflicts solved
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Thu, 10 Mar 2011 22:34:31 +0000 (23:34 +0100)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.it>
Thu, 10 Mar 2011 22:34:31 +0000 (23:34 +0100)
19 files changed:
INSTALL.sh
TODO.txt
sql/STORAGE.txt
sql/builder.sh
sql/sql.d/001-struct.sql
web/Obj/auth.phh
web/Obj/brisk.phh
web/Obj/dbase_base.phh
web/Obj/dbase_file.phh
web/Obj/dbase_pgsql.phh
web/Obj/hardban.phh
web/admin.php
web/briskin5/Obj/briskin5.phh
web/briskin5/Obj/placing.phh
web/briskin5/index_rd.php
web/briskin5/index_wr.php
web/briskin5/statadm.php
web/index_rd.php
web/index_wr.php

index 8b7ea98..9864b2e 100755 (executable)
@@ -15,21 +15,53 @@ proxy_path="$HOME/brisk-priv/proxy"
 web_only="FALSE"
 brisk_conf="brisk.conf.pho"
 
-if [ "$1" = "--ck" ]; then
-    find -name '*.pho' -o -name '*.phh' -o -name '*.php' -exec php5 -l {} \;
+if [ "$1" = "chk" ]; then
+    set -e
+    oldifs="$IFS"
+    IFS='
+'
+    for i in $(find -name '*.pho' -o -name '*.phh' -o -name '*.php'); do
+        php5 -l $i
+    done
     exit 0
 fi
 
+# before all check errors on the sources
+$0 chk || exit 3
+
+if [ "$1" = "pkg" ]; then
+    if [ "$2" != "" ]; then
+        tag="$2"
+    else
+        tag="$(git describe)"
+    fi
+    nam1="brisk_${tag}.tgz"
+    nam2="brisk-img_${tag}.tgz"
+    echo "Build packages ${nam1} and ${nam2}."
+    read -p "Proceed [y/n]: " a
+    if [ "$a" != "y" -a  "$a" != "Y" ]; then
+        exit 1
+    fi
+    git archive --format=tar --prefix=brisk-${tag}/brisk/ $tag | gzip > ../$nam1
+    cd ../brisk-img
+    git archive --format=tar --prefix=brisk-${tag}/brisk-img/ $tag | gzip > ../$nam2
+    cd -
+    exit 0
+fi
+    
 if [ -f $HOME/.brisk_install ]; then
    . $HOME/.brisk_install
 fi
+
 if [ "x$cookie_path" = "x" ]; then
    cookie_path=$web_path
 fi
+
 function usage () {
     echo
     echo "$1 -h"
-    echo "$1 --ck                           - run lintian on all ph* files."
+    echo "$1 chk                          - run lintian on all ph* files."
+    echo "$1 pkg                          - build brisk packages."
     echo "$1 [-W] [-n 3|5] [-t <(n>=4)>] [-T <auth_tab>] [-a <auth_file_name>] [-f conffile] [-p outconf] [-d TRUE|FALSE] [-w web_dir] [-k <ftok_dir>] [-l <legal_path>] [-y <proxy_path>] [-c <cookie_path>]"
     echo "  -h this help"
     echo "  -f use this config file"
@@ -75,7 +107,7 @@ function searchetc() {
             echo "$pp/$dname"
             return 0
         fi
-        pp="`dirname "$pp"`"
+        pp="$(dirname "$pp")"
     done
     
     return 1
@@ -88,19 +120,19 @@ while [ $# -gt 0 ]; do
     # echo aa $1 xx $2 bb
     conffile=""
     case $1 in
-       -f*) conffile="`get_param "-f" "$1" "$2"`"; sh=$?;;
-       -p*) outconf="`get_param "-p" "$1" "$2"`"; sh=$?;;
-       -n*) players_n="`get_param "-n" "$1" "$2"`"; sh=$?;;
-       -t*) tables_n="`get_param "-t" "$1" "$2"`"; sh=$?;;
-       -T*) tables_auth_n="`get_param "-T" "$1" "$2"`"; sh=$?;;
-        -a*) brisk_auth_conf="`get_param "-a" "$1" "$2"`"; sh=$?;;
-       -d*) brisk_debug="`get_param "-d" "$1" "$2"`"; sh=$?;;
-       -w*) web_path="`get_param "-w" "$1" "$2"`"; sh=$?;;
-       -k*) ftok_path="`get_param "-k" "$1" "$2"`"; sh=$?;;
-       -y*) proxy_path="`get_param "-y" "$1" "$2"`"; sh=$?;;
-       -c*) cookie_path="`get_param "-c" "$1" "$2"`"; sh=$?;;
-       -C*) brisk_conf="`get_param "-C" "$1" "$2"`"; sh=$?;;
-       -l*) legal_path="`get_param "-l" "$1" "$2"`"; sh=$?;;
+       -f*) conffile="$(get_param "-f" "$1" "$2")"; sh=$?;;
+       -p*) outconf="$(get_param "-p" "$1" "$2")"; sh=$?;;
+       -n*) players_n="$(get_param "-n" "$1" "$2")"; sh=$?;;
+       -t*) tables_n="$(get_param "-t" "$1" "$2")"; sh=$?;;
+       -T*) tables_auth_n="$(get_param "-T" "$1" "$2")"; sh=$?;;
+        -a*) brisk_auth_conf="$(get_param "-a" "$1" "$2")"; sh=$?;;
+       -d*) brisk_debug="$(get_param "-d" "$1" "$2")"; sh=$?;;
+       -w*) web_path="$(get_param "-w" "$1" "$2")"; sh=$?;;
+       -k*) ftok_path="$(get_param "-k" "$1" "$2")"; sh=$?;;
+       -y*) proxy_path="$(get_param "-y" "$1" "$2")"; sh=$?;;
+       -c*) cookie_path="$(get_param "-c" "$1" "$2")"; sh=$?;;
+       -C*) brisk_conf="$(get_param "-C" "$1" "$2")"; sh=$?;;
+       -l*) legal_path="$(get_param "-l" "$1" "$2")"; sh=$?;;
        -W) web_only="TRUE";;
        -h) usage $0; exit 0;;
        *) usage $0; exit 1;;
@@ -155,8 +187,8 @@ fi
 #  Pre-check
 #
 # check for etc path existence
-dsta="`dirname "$web_path"`"
-etc_path="`searchetc "$dsta" Etc`"
+dsta="$(dirname "$web_path")"
+etc_path="$(searchetc "$dsta" Etc)"
 if [ $? -ne 0 ]; then
     echo "Etc directory not found"
     exit 1
@@ -202,17 +234,21 @@ if [ "$web_only" = "FALSE" ]; then
     chmod 666 ${ftokk_path}/warrant
     touch ${ftokk_path}/poll
     chmod 666 ${ftokk_path}/poll
-    for i in `seq 0 99`; do 
+    for i in $(seq 0 99); do 
         touch ${ftokk_path}/table$i
         chmod 666 ${ftokk_path}/table$i
     done
+    for i in $(seq 0 299); do
+        touch ${ftokk_path}/user$i
+        chmod 666 ${ftokk_path}/user$i
+    done
 fi
 install -d ${web_path}__
-for i in `find web -type d | grep -v /CVS | sed 's/^....//g'`; do
+for i in $(find web -type d | grep -v /CVS | sed 's/^....//g'); do
     install -d ${web_path}__/$i 
 done
 
-for i in `find web -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' | grep -v /CVS | sed 's/^....//g'`; do
+for i in $(find web -name '*.php' -o -name '*.phh' -o -name '*.pho' -o -name '*.css' -o -name '*.js' -o -name '*.mp3' -o -name '*.swf' | grep -v /CVS | sed 's/^....//g'); do
     install -m 644 web/$i ${web_path}__/$i
 done
 
@@ -227,16 +263,16 @@ else
 fi
 
 # .js substitutions
-sed -i "s/PLAYERS_N *= *[0-9]\+/PLAYERS_N = $players_n/g" `find ${web_path}__ -type f -name '*.js' -exec grep -l 'PLAYERS_N *= *[0-9]\+' {} \;`
+sed -i "s/PLAYERS_N *= *[0-9]\+/PLAYERS_N = $players_n/g" $(find ${web_path}__ -type f -name '*.js' -exec grep -l 'PLAYERS_N *= *[0-9]\+' {} \;)
 
-sed -i "s/^var G_send_time *= *[0-9]\+/var G_send_time = $send_time/g" `find ${web_path}__ -type f -name '*.js' -exec grep -l '^var G_send_time *= *[0-9]\+' {} \;`
+sed -i "s/^var G_send_time *= *[0-9]\+/var G_send_time = $send_time/g" $(find ${web_path}__ -type f -name '*.js' -exec grep -l '^var G_send_time *= *[0-9]\+' {} \;)
 
 # .ph[pho] substitutions
-sed -i "s/define *( *PLAYERS_N, *[0-9]\+ *)/define(PLAYERS_N, $players_n)/g" `find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *PLAYERS_N, *[0-9]\+ *)' {} \;`
+sed -i "s/define *( *PLAYERS_N, *[0-9]\+ *)/define(PLAYERS_N, $players_n)/g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *PLAYERS_N, *[0-9]\+ *)' {} \;)
 
-sed -i "s/define *( *BRISKIN5_PLAYERS_N, *[0-9]\+ *)/define(BRISKIN5_PLAYERS_N, $players_n)/g" `find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *BRISKIN5_PLAYERS_N, *[0-9]\+ *)' {} \;`
+sed -i "s/define *( *BRISKIN5_PLAYERS_N, *[0-9]\+ *)/define(BRISKIN5_PLAYERS_N, $players_n)/g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *BRISKIN5_PLAYERS_N, *[0-9]\+ *)' {} \;)
 
-sed -i "s@define *( *FTOK_PATH,[^)]*)@define(FTOK_PATH, \"$ftok_path\")@g" `find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *FTOK_PATH,[^)]*)' {} \;`
+sed -i "s@define *( *FTOK_PATH,[^)]*)@define(FTOK_PATH, \"$ftok_path\")@g" $(find ${web_path}__ -type f -name '*.ph*' -exec grep -l 'define *( *FTOK_PATH,[^)]*)' {} \;)
 
 sed -i "s@define *( *TABLES_N,[^)]*)@define(TABLES_N, $tables_n)@g" ${web_path}__/Obj/brisk.phh
 
index 3922384..e3331ae 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
          DONE - produrle
          DONE - mostrarle
 
+     STEP 1.1
+       DONE - points archiver
+
+     STEP 1.2
+       - partial usability if database go down.
+
      STEP 1.5
-       - sistema di inserimento nuovi utenti (riversatore da form di inserimento)
+     - sistema di inserimento nuovi utenti (riversatore da form di inserimento)
 
      STEP 2
      - aggiornare le funz di gestione db in modo che prevedano anche il flag SUSPEND
index 749e71f..df9bc21 100644 (file)
@@ -1,3 +1,6 @@
+== how to move match ==
+select m.code, m.ttok, min(g.tstamp) from bsk_bin5_matches as m, bsk_bin5_games as g where g.mcode = m.code GROUP BY m.code, m.ttok;
+
 == rende la view dei punteggi ==
 select m.ttok, m.tidx, g.code, g.tstamp, p.pts, u.login from bsk_bin5_points as p, bsk_bin5_games as g, bsk_bin5_matches as m, bsk_users as u where p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND g.tstamp > '2010-10-01 00:00:00' ORDER BY g.tstamp;
 
index 74e009c..221bd87 100755 (executable)
@@ -3,16 +3,22 @@
 #
 #  all this part is from mopshop and we will use it to construct the brisk database
 #
-DBHOST=127.0.0.1
-DBUSER=brisk
-DBBASE=brisk
-DBPASS=briskpass
-PFX="bsk_"
+
+if [ -f $HOME/.db.conf ]; then
+    source $HOME/.db.conf
+else
+    DBHOST=127.0.0.1
+    DBUSER=brisk
+    DBBASE=brisk
+    DBPASS=briskpass
+    PFX="bsk_"
+fi
 
 if [ -f $HOME/.brisk_install ]; then
-   . $HOME/.brisk_install
+    source $HOME/.brisk_install
 fi
 
+
 sqlexe () {
     local sht
     sht=$1
@@ -38,9 +44,6 @@ one_or_all() {
 # MAIN
 #
 sht=0
-if [ -f $HOME/.db.conf ]; then
-    source $HOME/.db.conf
-fi
 
 if [ "$1" = "-s" ]; then
     shift
@@ -59,26 +62,26 @@ elif [ "$1" = "build" ]; then
     ( echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) | sqlexe $sht
 elif [ "$1" = "rebuild" ]; then
     ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ; \
-      echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
+        echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
         | sqlexe $sht
 elif [ "$1" = "psql" ]; then
    psql -h $DBHOST -U $DBUSER $DBBASE $@
 elif [ "$1" = "piped" ]; then
    psql -h $DBHOST -U $DBUSER $DBBASE -t -q -A -F '|' $@
 elif [ "$1" = "dump" ]; then
-   if [ $# -eq 1 ]; then
-      pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
-   else
-      pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
-   fi
+    if [ $# -eq 1 ]; then
+        pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
+    else
+        pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
+    fi
 elif [ "$1" = "dumpall" ]; then
-   if [ $# -eq 1 ]; then
-      pg_dump -h $DBHOST -U $DBUSER $DBBASE
-   else
-      pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
-   fi
+    if [ $# -eq 1 ]; then
+        pg_dump -h $DBHOST -U $DBUSER $DBBASE
+    else
+        pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
+    fi
 elif [ "$1" = "add" ]; then
-   cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
+    cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
 else
     echo " USAGE"
     echo "   ./builder create"
index 3639258..302804e 100644 (file)
@@ -9,8 +9,8 @@ CREATE TABLE #PFX#users (
        pass   text,
        email  text UNIQUE,
        type   integer,
-       tsusp  timestamp DEFAULT to_timestamp(0) -- disable timeout
-       mtime  timestamp DEFAULT to_timestamp(0) -- last access
+       tsusp  timestamp DEFAULT to_timestamp(0), -- disable timeout
+       mtime  timestamp DEFAULT to_timestamp(0)  -- last access
        );
 
 DROP TABLE #PFX#groups;
index eff784f..963755a 100644 (file)
@@ -70,13 +70,13 @@ class Challenges {
         log_auth("xxx", sprintf("Challenges::add [%s]\n", $login));
         // FIXME Checks here
         if ($login == '') {
-            return ($G_false);
+            return (FALSE);
         }
         
         // log_auth("xxx", "LOOPI tstamp: ".$this->item[$i]->tstamp."  curtime: ".$curtime);
         
-        if (($chal = new Challenge($login, $token, $ip, $tstamp)) == null) {
-            return ($G_false);
+        if (($chal = new Challenge($login, $token, $ip, $tstamp)) == FALSE) {
+            return (FALSE);
         }
         
         $this->item[$this->item_n] = $chal;
@@ -151,7 +151,7 @@ class Challenges {
     }
 
     // Static functions
-    function &init_data()
+    static function create()
         {
             $chal =& new Challenges();
     
@@ -160,9 +160,9 @@ class Challenges {
             return $chal;
         }
 
-    function &load_data() 
+    function load_data() 
         {
-            GLOBAL $G_false, $sess;
+            GLOBAL $sess;
             $doexit = FALSE;
             do {
                 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
@@ -186,7 +186,7 @@ class Challenges {
                     if ($chals == FALSE) {
                         log_only("INIT CHALLENGES DATA");
          
-                        $chals =& Challenges::init_data();
+                        $chals =& Challenges::create();
                         if (@shm_put_var($shm, $tok, $chals) == FALSE) {
                             log_only("PUT_VAR FALLITA ".strlen(serialize($chals)));
                             log_only(serialize($chals));
@@ -199,18 +199,17 @@ class Challenges {
 
                 $chals->garbage_manager();
 
-                $ret = &$chals;
-                return ($ret);
+                return ($chals);
             } while (0);
     
             if ($doexit)
                 exit();
     
-            return ($G_false);
+            return (FALSE);
         }
   
 
-    function save_data(&$chals) 
+    function save_data($chals) 
     {
         $shm =   FALSE;
         $oldmod = $chals->mod;
index 34e08b0..5249ff5 100644 (file)
@@ -35,7 +35,11 @@ define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
 define(SHM_DIMS_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
 define(SHM_DIMS_DLT, 65536);
+
+define(SHM_DIMS_U_MIN, 4096);
+define(SHM_DIMS_U_MAX, 65536);
+define(SHM_DIMS_U_DLT, 4096);
+
 define(COMM_N, 18);
 define(COMM_GEN_N, 50);
 
@@ -120,16 +124,17 @@ $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'  => '<br>Il collegamento al database è fallito.<br>Temporaneamente tutte le autenticazioni verranno sospese, accederai a Brisk come un utente normale.<br><br>Ci scusiamo per il disagio.',
+                                          'en'  => 'Connection to the database failed<br>All authentications are suspended temporarly, you login as normal user.<br>We are about the limitation')
                       
 
 );
 
-$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.'), <b>NOVITA\'</b>: informazioni al tavolo sul chiamante, nuovo stato di supporter del sito.',
@@ -378,12 +383,10 @@ class Table {
   {
   }
   
-  function &create($idx) 
+  function create($idx) 
   {
-    GLOBAL $G_false;
-
     if (($thiz =& new Table()) == FALSE)
-      return ($G_false);
+      return (FALSE);
 
     $thiz->idx       =   $idx;
     $thiz->player    =   array();
@@ -402,10 +405,8 @@ class Table {
     return ($thiz);
   }
 
-  function copy(&$from)
+  function copy($from)
   {
-    GLOBAL $G_false;
-    
     $this->idx = $from->idx;
     $this->player = array();
     for ($i = 0 ; $i < $from->player_n ; $i++)
@@ -426,24 +427,20 @@ class Table {
     $this->wakeup_time = $from->wakeup_time;
   }
 
-  function &myclone(&$from)
+  function myclone($from)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new Table()) == FALSE)
-      return ($G_false);
+      return (FALSE);
 
     $this->copy($from);
 
     return ($thiz);
   }
   
-  function &spawn(&$from)
+  function spawn($from)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new Table()) == FALSE)
-      return ($G_false);
+      return (FALSE);
     
     $thiz->idx = $from->idx;
     $thiz->player = array();
@@ -465,11 +462,11 @@ class Table {
     return ($thiz);
   }
 
-  function wag_set(&$user, $mesg)
+  function wag_set($user, $mesg)
   {
     log_main("WAG_SET");
 
-    $this->wag_own  = &$user;
+    $this->wag_own  =  $user;
     $this->wag_com  =  $mesg;
     $this->wag_tout =  0;
   }
@@ -572,10 +569,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
@@ -601,6 +599,7 @@ define(USER_FLAG_TY_SUSPEND, 0x400000); // done
 define(USER_FLAG_TY_DISABLE, 0x800000); // done
 
 class User {
+  var $idx;        // index in the users array
   var $code;       // authentication code
   var $name;       // name of the user
   var $sess;       // session of the user
@@ -632,15 +631,18 @@ class User {
   var $chat_cur;      // Current chat line number
   var $chat_ban;      // Time for ban chat
   var $chat_dlt;      // Delta t for ban
+  var $shm_sz;
+
   function User() {
   }
 
-  function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
-    GLOBAL $G_false;
+  function create($idx, $name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
+    // error_log("User::create BEGIN", 0);
 
     if (($thiz =& new User()) == FALSE)
-      return ($G_false);
+      return (FALSE);
 
+    $thiz->idx   = $idx;
     $thiz->code  = -1;
     $thiz->name  = $name;
     $thiz->sess  = $sess;
@@ -670,14 +672,13 @@ class User {
     $thiz->table      = $table;
     $thiz->table_pos = -1;
     $thiz->table_token = "";
-
+    $thiz->shm_sz = SHM_DIMS_U_MIN;
     return ($thiz);
   }
 
-  function copy(&$from)
+  function copy($from)
   {
-    GLOBAL $G_false;
-    
+    $this->idx        = $from->idx;
     $this->code       = $from->code;
     $this->name       = $from->name;
     $this->sess       = $from->sess;
@@ -716,30 +717,27 @@ class User {
     $this->table_pos  = $from->table_pos;
     $this->table_token = $from->table_token;
     $this->the_end    = $from->the_end;
-
+    $this->shm_sz     = $from->shm_sz;
     return (TRUE);
   }
 
 
-  function &myclone(&$from)
+  function myclone($from)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new User()) == FALSE)
-      return ($G_false);
+      return (FALSE);
     
     $thiz->copy($from);
 
     return ($thiz);
   }
   
-  function &spawn(&$from, $table, $table_pos)
+  function spawn($from, $table, $table_pos)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new User()) == FALSE)
-      return ($G_false);
+      return (FALSE);
     
+    $thiz->idx        = $from->idx;
     $thiz->code       = $from->code;
     $thiz->name       = $from->name;
     $thiz->sess       = $from->sess;
@@ -780,10 +778,15 @@ class User {
     $thiz->table      = 0;
     $thiz->table_pos  = $table_pos;
     $thiz->table_token = $from->table_token;
+    $thiz->shm_sz      = $from->shm_sz;
 
     return ($thiz);
   }
 
+  function idx_get() {
+      return ($this->idx);
+  }
+
   function code_get() {
       return ($this->code);
   }
@@ -813,7 +816,7 @@ class User {
         mkdir(PROXY_PATH);
       if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
        break;
-      fwrite($fp, pack("l",$this->step), 4);
+      fwrite($fp, pack("LL",$this->step, $this->idx));
       fclose($fp);
 
       return (TRUE);
@@ -829,7 +832,7 @@ class User {
       if (file_exists(PROXY_PATH) == FALSE)
         mkdir(PROXY_PATH);
       $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
-      fwrite($fp, pack("l",$this->step), 4);
+      fwrite($fp, pack("LL", $this->step, $this->idx));
       fclose($fp);
 
       return (TRUE);
@@ -855,12 +858,105 @@ class User {
     $this->chat_dlt = 0;
     $this->the_end = FALSE;
   }
-} // end class User
 
 
 
+  // SHSPLIT save and load function for the User class.
+  function load_data($id) 
+  {
+    GLOBAL $sess;
+
+    // error_log("User::load_data BEGIN", 0);
+
+    $doexit = FALSE;
+    do {
+      if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
+       log_main("ftok failed");
+       $doexit = TRUE;
+       break;
+      }
+    
+      if (($shm_sz = sharedmem_sz($tok)) == -1) {
+       log_main("shmop_open failed");
+      }
+       
+      if ($shm_sz == -1)
+       $shm_sz = SHM_DIMS_U_MIN;
+
+      if ($shm = shm_attach($tok, $shm_sz)) {
+       $user = @shm_get_var($shm, $tok);
+       
+       log_only("bri ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($user === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($user) ?   "TRUE" : "FALSE"));
+       if (isset($user)) 
+         log_only("bri count ".count($user));
+       
+       if ($user == FALSE) {
+         log_only("INIT MAIN DATA");
+         
+          // SHSPLIT FIXME: init_data for User class ??
+         $user =& User::create($id, "", "");
+         if (@shm_put_var($shm, $tok, $user) == FALSE) {
+              log_only("PUT_VAR FALLITA ".strlen(serialize($user)));
+              log_only(serialize($user));
+         }
+       }
+
+       $user->shm_sz = $shm_sz;
+       
+       shm_detach($shm);
+      }
+      
+      //  
+      // SHSPLIT: load users from the shared memory
+      //
+      return ($user);
+    } while (0);
+    
+    if ($doexit)
+      exit();
+    
+    return (FALSE);
+  }
+  
 
+  function save_data(&$user, $id) 
+  {
+    GLOBAL $sess;
+    
+    $shm =   FALSE;
+    
+    // var_dump($user);
+    // error_log("User::save_data BEGIN", 0);
 
+    if (($tok = @ftok(FTOK_PATH."/user".$id, "B")) == -1) {
+      return (FALSE);
+    }
+    while ($user->shm_sz < SHM_DIMS_U_MAX) {
+      if (($shm = shm_attach($tok, $user->shm_sz)) == FALSE)
+       break;
+      
+      // log_only("PUT_VAR DI ".strlen(serialize($user)));
+      if (shm_put_var($shm, $tok, $user) != FALSE) {
+       shm_detach($shm);
+       return (TRUE);
+      }
+      if (shm_remove($shm) === FALSE) {
+       log_only("REMOVE FALLITA");
+       break;
+      }
+      shm_detach($shm);
+      $user->shm_sz += SHM_DIMS_U_DLT;
+    } 
+
+    if ($shm)
+      shm_detach($shm);
+    
+    return (FALSE);
+  }
+
+
+
+} // end class User
 
 
 function step_get($sess) {
@@ -873,15 +969,15 @@ function step_get($sess) {
       mkdir(PROXY_PATH);
     if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
       break;
-    if (($s = fread($fp, 4)) == FALSE)
+    if (($s = fread($fp, 8)) == FALSE)
       break;
-    if (strlen($s) != 4)
+    if (strlen($s) != 8)
       break;
-    $arr = unpack('l', $s);
+    $arr = unpack('Ls/Li', $s);
     fclose($fp);
 
     // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
-    return ($arr[1]);
+    return ($arr);
   } while (0);
 
   if ($fp != FALSE)
@@ -913,7 +1009,7 @@ class Room {
     $this->table = array();
 
     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
-      $this->user[$i] =& User::create("", "");
+        $this->user[$i] =& User::create($i, "", "");
     }
 
     for ($i = 0 ; $i < TABLES_N ; $i++) {
@@ -934,6 +1030,7 @@ class Room {
         $this->table[$i]->auth_only = FALSE;
     }
     $this->garbage_timeout = 0;
+    $this->shm_sz = SHM_DIMS_MIN;
   }
 
   function garbage_manager($force)
@@ -1127,6 +1224,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 ? "<i>" : "");
     $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
@@ -1445,6 +1547,7 @@ class Room {
     $msg = substr($mesg, 6, 128);
     $curtime = time();
     $dt = date("H:i ", $curtime);
+    $target = "";
 
     //
     //  Compute actions
@@ -1627,13 +1730,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) {
-          $bdb = new BriskDB();
-          $bdb->users_load();
-          /* MLANG: "Il nickname <b>\'%s\'</b> &egrave; gi&agrave; registrato, <b>se il suo proprietario si autentificher&agrave; verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
-          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 <b>\'%s\'</b> &egrave; gi&agrave; registrato, <b>se il suo proprietario si autentificher&agrave; verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
+                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");
@@ -1839,9 +1943,9 @@ class Room {
     return;
   } // function chatt_send( ...
 
-  function &get_user($sess, &$idx)
+  function get_user($sess, &$idx)
   {
-    GLOBAL $PHP_SELF, $G_false;
+    GLOBAL $PHP_SELF;
 
     if (validate_sess($sess)) {
       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
@@ -1860,13 +1964,11 @@ class Room {
       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
     }
 
-    return ($G_false);
+    return (FALSE);
   }
 
-  
-
   /*
-   * function &add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
+   * function add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
    *
    * RETURN VALUE:
    *   if ($idx >  -1    && ret == FALSE)  =>  duplicated nick
@@ -1874,14 +1976,12 @@ 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)
+  function add_user(&$sess, &$idx, $name, $pass, $ip)
   {
-    GLOBAL $G_base, $G_false, $CO_list;
+    GLOBAL $G_base, $CO_list;
 
     $idx = 0;
 
@@ -1896,7 +1996,7 @@ class Room {
 
     if (($name_new = validate_name($name)) == FALSE) {
       $idx = -2;
-      return ($G_false);
+      return (FALSE);
     }
 
     log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
@@ -1905,23 +2005,30 @@ class Room {
 
     /* if pass != FALSE verify the login with pass */
     log_auth("XXX", "auth1");
-    $bdb = new BriskDB();
-    $bdb->users_load();
-    if ($pass != FALSE) {
-      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 (($bdb = BriskDB::create()) != FALSE) {
+        $bdb->users_load();
+        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) {
+                // error_log(print_r(&$authenticate, TRUE),0);
+                $user_type = $authenticate->type_get();
+            }
+            else {
+                $idx = -3;
+                return (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 ? */
@@ -1929,7 +2036,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 !!
       }
@@ -1949,6 +2056,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 */
 
@@ -2003,8 +2112,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));
 
       
@@ -2075,10 +2185,10 @@ class Room {
       return ($ret);
     }
 
-    return ($G_false);
+    return (FALSE);
   }
   
-  function standup_update(&$user)
+  function standup_update($user)
   {
     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
       $user_cur = &$this->user[$i];
@@ -2104,18 +2214,21 @@ class Room {
   }
 
   // Static functions
-  function &init_data()
+  static function create()
   {
-    $room =& new Room();
+    $room = new Room();
     
     return $room;
   }
   
-
-  function &load_data() 
+  
+  function load_data() 
   {
-    GLOBAL $G_false, $sess;
+    GLOBAL $sess;
     $doexit = FALSE;
+
+    // error_log("Room::load_data BEGIN", 0);
+
     do {
       if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
        log_main("ftok failed");
@@ -2131,37 +2244,48 @@ class Room {
        $shm_sz = SHM_DIMS_MIN;
 
       if ($shm = shm_attach($tok, $shm_sz)) {
-       $room = @shm_get_var($shm, $tok);
-       
-       log_only("bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
-       if (isset($room)) 
-         log_only("bri count ".count($room));
-       
-       if ($room == FALSE) {
-         log_only("INIT MAIN DATA");
-         
-         $room =& Room::init_data();
-         if (@shm_put_var($shm, $tok, $room) == FALSE) {
-           log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
-           log_only(serialize($room));
-         }
-       }
-       $room->shm_sz = $shm_sz;
-       
-       shm_detach($shm);
+          $room = @shm_get_var($shm, $tok);
+          
+          log_only("bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
+          if (isset($room)) 
+              log_only("bri count ".count($room));
+          
+          if ($room == FALSE) {
+              log_only("INIT MAIN DATA");
+              shm_detach($shm);
+              
+              // error_log("DE CHE", 0);
+
+              $room = Room::create();
+              
+              if (Room::save_data($room) == FALSE)
+                  return FALSE;
+
+              return $room;
+          }
+          $room->shm_sz = $shm_sz;
+          
+          shm_detach($shm);
+
+          for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+              $room->user[$i] = User::load_data($i);
+          }
       }
-      $ret = &$room;
-      return ($ret);
+      
+      //  
+      // SHSPLIT: load users from the shared memory
+      //
+      return ($room);
     } while (0);
     
     if ($doexit)
       exit();
     
-    return ($G_false);
+    return (FALSE);
   }
   
 
-  function save_data(&$room) 
+  function save_data_orig(&$room) 
   {
     GLOBAL $sess;
     
@@ -2195,6 +2319,57 @@ class Room {
     return (FALSE);
   }
 
+
+  function save_data(&$room) 
+  {
+      GLOBAL $sess;
+    
+      $ret =   FALSE;
+      $shm =   FALSE;
+    
+      // var_dump($room);
+      // error_log("Room::save_data BEGIN", 0);
+    
+      if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) 
+          return (FALSE);
+    
+      // SHSPLIT: before save the $room you must save users, 
+      //          detach from main struct and (then) reattach
+      $user_park = array();
+      for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+          $user_park[$i]  = $room->user[$i];
+          $room->user[$i] = FALSE;
+      }
+
+      while ($room->shm_sz < SHM_DIMS_MAX) {
+          if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
+              break;
+      
+          // log_only("PUT_VAR DI ".strlen(serialize($room)));
+          if (shm_put_var($shm, $tok, $room) != FALSE) {
+              $ret = TRUE;
+              break;
+          }
+          if (shm_remove($shm) === FALSE) {
+              log_only("REMOVE FALLITA");
+              break;
+          }
+          shm_detach($shm);
+          $room->shm_sz += SHM_DIMS_DLT;
+      } 
+
+      if ($shm)
+          shm_detach($shm);
+    
+      // SHSPLIT: reattach users to the room class
+      for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+          User::save_data($user_park[$i], $i);
+          $room->user[$i] = $user_park[$i];
+      }
+
+      return ($ret);
+  }
+
   function lock_data()
   {
     GLOBAL $sess; 
index a827725..43b568d 100644 (file)
@@ -39,9 +39,9 @@ class LoginDBItem {
         $this->type  = $type;
     }
 
-    static function &LoginDBItemFromRecord($rec)
+    static function LoginDBItemFromRecord($rec)
     {
-        $ret = new LoginDBItem($rec->code, $rec->login, $rec->pass, 
+        $ret = new LoginDBItem($rec->code, $rec->login, $rec->pass, 
                                  $rec->email, $rec->type);
 
         return ($ret);
index 6801031..13dce47 100644 (file)
@@ -38,6 +38,13 @@ class BriskDB {
         log_main("BriskDB create:end");
     }
 
+    static function create()
+    {
+        $ret = new BriskDB();
+        
+        return ($ret);
+    }
+
     function users_load() 
     {
         GLOBAL $DOCUMENT_ROOT;
@@ -80,10 +87,8 @@ class BriskDB {
         return ($this->item[$idx]->login);
     }
 
-    function &getitem_bylogin($login, &$id)
+    function getitem_bylogin($login, &$id)
         {
-            GLOBAL $G_false;
-
             log_main("login_exists: ".$login);
     
             /* check the existence of the nick in the BriskDB */
@@ -96,7 +101,7 @@ class BriskDB {
                 }
             }
             $id = -1;
-            return ($G_false);
+            return (FALSE);
         }
 
     function getmail($login)
@@ -127,11 +132,9 @@ class BriskDB {
         return (FALSE);
     }
 
-    function &login_verify($login, $pass)
+    function login_verify($login, $pass)
     {
-        GLOBAL $G_false;
-        
-        $ret = &$G_false;
+        $ret = FALSE;
         
         log_main("login_verify: ".$login);
         
index a205b32..004edb9 100644 (file)
@@ -40,18 +40,23 @@ class DBConn
     var $db = FALSE;
     
     function DBConn()
+    {
+        $this->db = DBConn::$dbcnnx;        
+    }
+
+    static function create()
     {
         GLOBAL $G_dbauth;
         
         if (DBConn::$dbcnnx == FALSE) {
             if (!(DBConn::$dbcnnx = @pg_connect ($G_dbauth))) {
-                echo "DB connection failed.";
-                exit;
+                return (FALSE);
             }
         }
-        $this->db = DBConn::$dbcnnx;
 
-        return;
+        $out = new DBConn();
+        
+        return $out;
     }
     function db()
     {
@@ -65,14 +70,27 @@ class BriskDB
     var $item;
     var $item_n;
     
-    function BriskDB()
+    function BriskDB($dbconn)
     {
-        GLOBAL $DOCUMENT_ROOT, $G_dbpfx, $G_false;
+        $this->dbconn = $dbconn;
+    }
+
+    static function create()
+    {
+        GLOBAL $DOCUMENT_ROOT, $G_dbpfx;
+
+        $ret = FALSE;
+
         log_main("BriskDB create:start");
         
-        $this->dbconn = new DBConn();
+        do {
+            if (($dbconn = DBConn::create()) == FALSE) 
+                break;
+            
+            $ret = new BriskDB($dbconn);
+        } while (0);
         
-        log_main("BriskDB create:end");
+        return ($ret);
     }
 
     function users_load()
@@ -95,15 +113,15 @@ class BriskDB
         return FALSE;
     }
 
-    function &getrecord_bylogin($login) {
-        GLOBAL $G_false, $G_dbpfx;
+    function getrecord_bylogin($login) {
+        GLOBAL $G_dbpfx;
 
         $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);
         if (($user_pg  = pg_query($this->dbconn->db(), $user_sql)) == FALSE)
-            return $ret;
+            return FALSE;
         
         if (pg_numrows($user_pg) != 1)
-            return $ret;
+            return FALSE;
         
         $user_obj = pg_fetch_object($user_pg, 0);
 
@@ -112,11 +130,11 @@ class BriskDB
 
 
     
-    function &login_verify($login, $pass)
+    function login_verify($login, $pass)
     {
-        GLOBAL $G_dbpfx, $G_false;
+        GLOBAL $G_dbpfx;
         
-        $ret = &$G_false;
+        $ret = FALSE;
         
         log_main("login_verify: ".$login);
         
@@ -125,29 +143,36 @@ class BriskDB
         //O for ($i = 0 ; $i < $this->item_n ; $i++) {
         //O log_main("login_verify: BEGIN");
         
-        if (($user_obj = $this->getrecord_bylogin($login)) == FALSE)
-            return $ret;
+        if (($user_obj = $this->getrecord_bylogin($login)) == FALSE) {
+            return FALSE;
+        }
 
         log_main("login[".$user_obj->code."]: ".$user_obj->login);
         
         /* if it exists check for a valid challenge */
         if (($a_sem = Challenges::lock_data()) != FALSE) { 
+            // error_log("loop-2", 0);
             if (($chals = &Challenges::load_data()) != FALSE) {
+                // error_log("loop-1", 0);
                 for ($e = 0 ; $e < $chals->item_n ; $e++) {
-                    
+                    // error_log("loop", 0);
                     log_main("challenge[".$e."]: ".$chals->item[$e]->login);
                     if (strcmp($login, $chals->item[$e]->login) == 0) {
+                        // error_log("loop2", 0);
                         log_main("login_verify [".$pass."] with [".md5($chals->item[$e]->token.$user_obj->pass)."]");
                         
                         if (strcmp($pass, md5($chals->item[$e]->token.$user_obj->pass)) == 0) {
+                            // error_log("loop3", 0);
                             log_main("login_verify SUCCESS for ".$login);
                             
                             $chals->rem($login);
                             $ret = LoginDBItem::LoginDBItemFromRecord($user_obj);
+                            // error_log("dbitem: ".print_r($ret, TRUE));
                             break;
                         }
                     }
                 } // end for ($e = 0 ...
+                // error_log("end loop-1", 0);
             }
             
             if ($chals->ismod()) {
@@ -163,10 +188,8 @@ class BriskDB
         return ($ret);
     }
 
-    function &getitem_bylogin($login, &$id) {
-        GLOBAL $G_false;
-        
-        $ret = &$G_false;
+    function getitem_bylogin($login, &$id) {
+        $ret = FALSE;
         $id = -1;
         
         log_main("getitem_bylogin: ".$login);
@@ -209,10 +232,9 @@ class BriskDB
         return TRUE;
     }
 
-    function &getdbconn()
+    function getdbconn()
     {
-        $ret = $this->dbconn;
-        return ($ret);
+        return ($this->dbconn);
     }
 
     //   ttok   text UNIQUE,      
index dcdeb01..ffec9c6 100644 (file)
@@ -66,8 +66,8 @@ class Hardbans {
 
     log_auth("xxx", sprintf("Hardbans::add [%s]\n", $login));
 
-    if (($chal = new Hardban($login, $ip, $session, $timeout)) == null) {
-      return ($G_false);
+    if (($chal = new Hardban($login, $ip, $session, $timeout)) == FALSE) {
+      return (FALSE);
     }
 
     $this->item[$this->item_n] = $chal;
@@ -143,7 +143,7 @@ class Hardbans {
   }
 
   // Static functions
-  function &init_data()
+  static function create()
   {
     $chal =& new Hardbans();
     
@@ -152,9 +152,9 @@ class Hardbans {
     return $chal;
   }
 
-  function &load_data() 
+  function load_data() 
   {
-    GLOBAL $G_false, $sess;
+    GLOBAL $sess;
     $doexit = FALSE;
     do {
       if (($tok = @ftok(FTOK_PATH."/hardbans", "B")) == -1) {
@@ -178,7 +178,7 @@ class Hardbans {
        if ($hban == FALSE) {
          log_only("INIT HARDBAN DATA");
          
-         $hban =& Hardbans::init_data();
+         $hban =& Hardbans::create();
          if (@shm_put_var($shm, $tok, $hban) == FALSE) {
            log_only("PUT_VAR FALLITA ".strlen(serialize($hban)));
            log_only(serialize($hban));
@@ -191,18 +191,17 @@ class Hardbans {
 
       $hban->garbage_manager(TRUE);
 
-      $ret = &$hban;
-      return ($ret);
+      return ($hban);
     } while (0);
     
     if ($doexit)
       exit();
     
-    return ($G_false);
+    return (FALSE);
   }
   
 
-  function save_data(&$hban) 
+  function save_data($hban) 
   {
     $shm =   FALSE;
     $oldmod = $hban->mod;
index c36e475..6c65f9e 100644 (file)
@@ -101,7 +101,11 @@ function main()
                 $cont .= sprintf("Loading failed.<br>\n"); 
                 break;
             }
-            $newdb = new BriskDB();
+
+            if (($newdb = BriskDB::create()) == FALSE) {
+                $cont .= sprintf("Database connection failed.<br>\n"); 
+                break;
+            }
             $newdb->users_load();
             if ($newdb->addusers_from_olddb($olddb, $cont) == FALSE) {
                 $cont .= sprintf("Insert failed.<br>\n"); 
@@ -123,7 +127,11 @@ function main()
                 break;
             }
 
-            $newdb = new BriskDB();
+            if (($newdb = BriskDB::create()) == FALSE) {
+                $cont .= sprintf("Database connection failed.<br>\n"); 
+                break;
+            }
+
             $newdb->users_load();
             $dbconn = $newdb->getdbconn();
             for ($pts_n = 0 ;  !feof($fp) ; $pts_n++) {
index 3e5c685..0c36306 100644 (file)
@@ -170,12 +170,10 @@ class Table_briskin5 extends Table {
 
 
   /* CREATE() NOT USED
-  function &create($idx) 
+  function create($idx) 
   {
-    GLOBAL $G_false;
-
     if (($thiz =& new Table_briskin5()) == FALSE)
-      return ($G_false);
+      return (FALSE);
 
     $thiz->create($idx);
 
@@ -206,12 +204,10 @@ class Table_briskin5 extends Table {
   */
 
   /* CLONE() NOT USED
-  function &myclone(&$from)
+  function myclone(&$from)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new Table_briskin5()) == FALSE)
-      return ($G_false);
+      return (FALSE);
     
     parent::copy($from);
 
@@ -250,18 +246,17 @@ class Table_briskin5 extends Table {
     parent::copy($from);
   }
 
-  function &spawn(&$from)
+  function spawn(&$from)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new Table_briskin5()) == FALSE)
-      return ($G_false);
+      return (FALSE);
     
     $thiz->parentcopy($from);
 
     log_main("PLAYER_N - spawn.".$thiz->player_n);
 
-    $thiz->card = &$thiz->bunch_create();
+    $thiz->card = array();
+    $thiz->bunch_create();
     $thiz->mazzo    = rand(0,PLAYERS_N-1);
     $thiz->points_n = 0;
     $thiz->mult     = 1;
@@ -279,17 +274,26 @@ class Table_briskin5 extends Table {
   }
 
 
-  function &bunch_create()
-  {
-    $ret = array();
-
-    for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
-      // for ($i = 0 ; $i < 40 ; $i++) {
-      $ret[$i] =& new Card($i, 'bunch', 'no_owner');
-    }
+  //   function bunch_create_old() function AND 
+  //   {
+  //     $ret = array();
+  // 
+  //     for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
+  //       // for ($i = 0 ; $i < 40 ; $i++) {
+  //       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
+  //     }
+  // 
+  //     $oret = &$ret;
+  //     return ($oret);
+  //   }
 
-    $oret = &$ret;
-    return ($oret);
+  function bunch_create()
+  {
+      $ret = array();
+      
+      for ($i = 0 ; $i < (BRISKIN5_PLAYERS_N == 5 ? 40 : 24) ; $i++) {
+          $this->card[$i] = new Card($i, 'bunch', 'no_owner');
+      }
   }
 
   function bunch_make()
@@ -428,11 +432,9 @@ class User_briskin5 extends User {
   }
 
   /* CREATE NOT USED
-  function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
-    GLOBAL $G_false;
-
+  function create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
     if (($thiz =& new User()) == FALSE)
-      return ($G_false);
+      return (FALSE);
 
     $thiz->asta_card = -2;
     $thiz->asta_pnt  = -1;
@@ -461,12 +463,10 @@ class User_briskin5 extends User {
   }
 
   /* CLONE NOT USED
-  function &myclone(&$from)
+  function myclone(&$from)
   {
-    GLOBAL $G_false;
-    
     if (($thiz =& new User()) == FALSE)
-      return ($G_false);
+      return (FALSE);
 
     $thiz->copy($from);
 
@@ -474,12 +474,12 @@ class User_briskin5 extends User {
   } 
   */
   
-  function &spawn(&$from, $table, $table_pos)
+  function spawn(&$from, $table, $table_pos)
   {
-    GLOBAL $G_false, $CO_bin5_pref_ring_endauct;
+    GLOBAL $CO_bin5_pref_ring_endauct;
     
     if (($thiz =& new User_briskin5()) == FALSE)
-      return ($G_false);
+      return (FALSE);
     
     $thiz->parentcopy($from);
 
@@ -548,9 +548,9 @@ class Briskin5 {
   }
 
 
-  function &get_user($sess, &$idx)
+  function get_user($sess, &$idx)
   {
-    GLOBAL $PHP_SELF, $G_false;
+    GLOBAL $PHP_SELF;
 
     if (validate_sess($sess)) {
       for ($i = 0 ; $i < BRISKIN5_MAX_PLAYERS ; $i++) {
@@ -569,7 +569,7 @@ class Briskin5 {
       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
     }
 
-    return ($G_false);
+    return (FALSE);
   }
 
 
@@ -632,7 +632,7 @@ class Briskin5 {
   //
   //  static functions
   //
-  function &load_data($table_idx, $table_token = "") 
+  function load_data($table_idx, $table_token = "") 
   {
     GLOBAL $G_false, $sess;
     $doexit = FALSE;
@@ -678,7 +678,7 @@ class Briskin5 {
     if ($doexit)
       exit();
     
-    return ($G_false);
+    return (FALSE);
   }
   
 
index 31b97a3..d6d6547 100644 (file)
@@ -49,7 +49,7 @@ class Ptsgam {
     $this->gam = $gam;
   }
 
-  function &myclone()
+  function myclone()
   {
     $ret = new Ptsgam($this->username, $this->pts, $this->gam);
 
@@ -142,7 +142,11 @@ function placing_time_file()
 function placing_time_pgsql()
 {    
     GLOBAL $G_dbpfx;
-    $bdb = new BriskDB();
+    
+    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);
@@ -173,7 +177,7 @@ function placing_date($mtime)
 }
   
 
-function placing_show_file(&$user, $ty, $subty) 
+function placing_show_file($user, $ty, $subty) 
 {
   $tail = FALSE;
 
@@ -250,8 +254,11 @@ function placing_show_file(&$user, $ty, $subty)
 function placing_show_pgsql(&$user, $ty, $subty) 
 {
     GLOBAL $G_dbpfx;
-
-    $bdb = new BriskDB();
+    
+    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');",
index 2a35220..0d2314c 100644 (file)
@@ -120,7 +120,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
   if (($proxy_step = step_get($sess)) != FALSE) {
     // log_rd2("Postget".$proxy_step."zizi");
     
-    if ($cur_step == $proxy_step) {
+    if ($cur_step == $proxy_step['s']) {
       log_lock("P");
       return (FALSE);
     }
index 5045a54..1c0735e 100644 (file)
@@ -494,10 +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);
-            $bdb = new BriskDB();
-            $bdb->bin5_points_save($curtime, $table->table_token, $user->table_orig, $ucodes, $pt_cur);
-            unset($bdb);
           }
 
          $table->game_next();
index 3c3e954..07b847e 100644 (file)
 /*
   line example:
 1246428948|492e4e9e856b0|N|tre|172.22.1.90|STAT:BRISKIN5:FINISH_GAME|4a4afd4983039|6|3|tre|1|due|2|uno|-1|
-
-TODO:
-   - update the STAT file with differential
-   - recalculate points
-
    
 */
 
@@ -45,6 +40,7 @@ require_once("Obj/placing.phh");
 
 function main_file($curtime)
 {
+  GLOBAL $G_alarm_passwd;
   $tri = array();
   $mon = array();
   $wee = array();
@@ -62,7 +58,11 @@ function main_file($curtime)
     fclose($fp_start);
   }
 
-  $bdb = new BriskDB();
+  if (($bdb = BriskDB::create()) == FALSE) {
+    echo "database connection failed";
+    exit;
+  }
+      
   $bdb->users_load();
 
   for ($i = 0 ; $i < $bdb->count() ; $i++) {
@@ -214,7 +214,10 @@ function main_pgsql($curtime)
 {
     GLOBAL $G_dbpfx;
 
-    $bdb = new BriskDB();
+    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 );
@@ -225,14 +228,109 @@ function main_pgsql($curtime)
             log_crit("statadm: begin failed");
             break;
         }
+        
+        $mtc_sql = sprintf("CREATE TEMPORARY TABLE %sbin5_temp_matches ON COMMIT DROP AS SELECT m.code, max(g.tstamp) AS tstamp 
+                            FROM %sbin5_matches as m, %sbin5_games as g 
+                            WHERE g.mcode = m.code GROUP BY m.code, m.ttok",
+                           $G_dbpfx, $G_dbpfx, $G_dbpfx);
+        // error_log($mtc_sql, 0);
+        if (pg_query($bdb->dbconn->db(), $mtc_sql) == FALSE) {
+            log_crit("statadm: temporary matches table creation [$mtc_sql] failed");
+            break;
+        }
+        
+        $tmt_sql = sprintf("SELECT * FROM %sbin5_temp_matches WHERE  tstamp < to_timestamp(%d)",
+                           $G_dbpfx, $curtime - TRI_LIMIT);
+        // error_log($tmt_sql, 0);
+
+        // if deletable old matches exists then ...
+        if (($tmt_pg = pg_query($bdb->dbconn->db(), $tmt_sql)) != FALSE) {
+            //
+            // store matches before clean them
+            //
+            $fname = sprintf("%s/pts_archive%s.log", LEGAL_PATH, date("Ymd", $curtime));
+            if (($fp = @fopen($fname, 'a')) == FALSE) {
+                log_crit("statadm: log file [$fname] open failed");
+                break;
+            }
+            
+            $tmt_n = pg_numrows($tmt_pg);
+            // get matches
+            for ($m = 0 ; $m < $tmt_n ; $m++) {
+                $tmt_obj = pg_fetch_object($tmt_pg, $m);
+                
+                $mtc_sql = sprintf("SELECT * from %sbin5_matches WHERE code = %d",
+                                   $G_dbpfx, $tmt_obj->code);
+                
+                error_log($mtc_sql, 0);
+                if (($mtc_pg  = pg_query($bdb->dbconn->db(), $mtc_sql)) == FALSE || pg_numrows($mtc_pg) != 1) {
+                    log_crit("statadm: matches row select failed");
+                    break;
+                }
+                $mtc_obj = pg_fetch_object($mtc_pg, 0);
+                
+                if (fwrite($fp, sprintf("M|%d|%s|%d|\n", $mtc_obj->code, xcapelt($mtc_obj->ttok), $mtc_obj->tidx)) == FALSE) {
+                    log_crit("statadm: log file [$fname] write match failed");
+                    break;
+                }
+                
+                // get games associated to each match
+                $gam_sql = sprintf("SELECT code, mcode, EXTRACT(epoch FROM tstamp) AS tstamp FROM %sbin5_games 
+                                    WHERE mcode = %d ORDER BY tstamp",
+                                   $G_dbpfx, $mtc_obj->code);
+                if (($gam_pg  = pg_query($bdb->dbconn->db(), $gam_sql)) == FALSE) {
+                    log_crit("statadm: games row select failed");
+                    break;
+                }
+                
+                $gam_n = pg_numrows($gam_pg);
+                for ($g = 0 ; $g < $gam_n ; $g++) {
+                    $gam_obj = pg_fetch_object($gam_pg, $g);
+                    
+                    if (fwrite($fp, sprintf("G|%d|%d|%d|\n", $gam_obj->mcode, $gam_obj->code, $gam_obj->tstamp)) == FALSE) {
+                        log_crit("statadm: log file [$fname] write game failed");
+                        break;
+                    }
+                    $pts_sql = sprintf("SELECT * FROM %sbin5_points WHERE gcode = %d", $G_dbpfx, $gam_obj->code);
+                    if (($pts_pg  = pg_query($bdb->dbconn->db(), $pts_sql)) == FALSE) {
+                        log_crit("statadm: points row select [$pts_sql] failed");
+                        break;
+                    }
+                    $pts_n = pg_numrows($pts_pg);
+                    for ($p = 0 ; $p < $pts_n ; $p++) {
+                        $pts_obj = pg_fetch_object($pts_pg, $p);
+                        
+                        if (fwrite($fp, sprintf("P|%d|%d|%d|\n", $pts_obj->gcode, $pts_obj->ucode, $pts_obj->pts)) == FALSE) {
+                            log_crit("statadm: log file [$fname] write pts failed");
+                            break;
+                        }
+                    }
+                    if ($p < $pts_n)
+                        break;
+                }
+                if ($g < $gam_n)
+                    break;
+                
+                // delete match and all it's childs (games and points)
+                $del_sql = sprintf("DELETE FROM %sbin5_matches WHERE code = %d",
+                                   $G_dbpfx, $tmt_obj->code);
+                if (($del_pg = pg_query($bdb->dbconn->db(),$del_sql)) == FALSE || pg_affected_rows($del_pg) != 1) {
+                    log_crit("statadm: matches row deletion failed");
+                    break;
+                }
+                
+            }
+            if ($m < $tmt_n)
+                break;
+        } // if (($tmt_pg = pg_query($bdb->dbco...
 
-        // Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
+        // GEN: Truncate table (postgresql extension, in other SQL you must user unqualified DELETE
         $tru_sql = sprintf("TRUNCATE %sbin5_places;", $G_dbpfx);
         if (pg_query($bdb->dbconn->db(), $tru_sql) == FALSE) {
             log_crit("statadm: truncate failed");
             break;
         }
-
+        
         for ($dtime = 0 ; $dtime < count($limi) ; $dtime++) {
             $old_score = array( 1000000000, 1000000000);
             $old_gam   = array( -1, -1);
@@ -241,15 +339,15 @@ function main_pgsql($curtime)
             $pla_sql = sprintf("SELECT (float4(sum(p.pts)) * 100.0 ) /  float4(count(p.pts)) as score, sum(p.pts) as points, count(p.pts) as games, u.code as ucode, u.login as login
                                 FROM %sbin5_points as p, %sbin5_games as g, %sbin5_matches as m, %susers as u 
                                 WHERE p.ucode = u.code AND p.gcode = g.code AND g.mcode = m.code AND 
-                                      g.tstamp > to_timestamp(%d)
+                                      g.tstamp > to_timestamp(%d) AND g.tstamp <= to_timestamp(%d)
                                 GROUP BY u.code, u.login
                                 ORDER BY (float4(sum(p.pts)) * 100.0 ) /  float4(count(p.pts)) DESC, 
                                          count(p.pts) DESC",
-                               $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $ming[$dtime]);
+                               $G_dbpfx, $G_dbpfx, $G_dbpfx, $G_dbpfx, $curtime - $limi[$dtime], $curtime);
 
             // log_crit("statadm: INFO: [$pla_sql]");
 
-            if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE || pg_numrows($pla_pg) == 0) {
+            if (($pla_pg  = pg_query($bdb->dbconn->db(), $pla_sql)) == FALSE) {
                 // no point found, abort
                 log_crit("statadm: main placement select failed [$pla_sql]");
                 break;
@@ -324,7 +422,9 @@ function main()
     
     $fun_name = "main_${G_dbasetype}";
     
-    $curtime = time();
+    $ctime = time();
+
+    $curtime = ((int)($ctime / (24 * 3600))) * 24 * 3600 - (((int)substr(date("O", $ctime), 0, -2)) * 3600);
     if ($ret = $fun_name($curtime))
         echo "Success.<br>\n";
     else
index 7112182..88c740b 100644 (file)
@@ -135,7 +135,7 @@ function maincheck($sess, $cur_stat, $cur_subst, $cur_step, &$new_stat, &$new_su
   if (($proxy_step = step_get($sess)) != FALSE) {
     // log_rd2("Postget".$proxy_step."zizi");
 
-    if ($cur_step == $proxy_step) {
+    if ($cur_step == $proxy_step['s']) {
       log_lock("P");
       return (FALSE);
     }
index b030aff..9ac86bf 100644 (file)
@@ -42,6 +42,8 @@ $mlang_indwr = array( 'btn_backtotab' => array( 'it' => 'Torna ai tavoli.',
                                             'en' => 'close' ),
                       'commerr' => array( 'it' => '<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>',
                                           'en' => '<b>An error was occurred during the saving, try again or contact the administrator.</b>'),
+                      'coerrdb' => array( 'it' => '<b>Il database è temporaneamente irraggiungibile, riprova più tardi o contatta l\'amministratore.</b>',
+                                          'en' => '<b>The database is temporarly unavailable, retry to later or conctact the administrator.</b>'),
                       'warrmust' => array( 'it' => '<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>',
                                            'en' => 'To authenticate somebody you have to be authenticated in your turn'),
                       'mesgrepl' => array( 'it' => '<br><br>Il messaggio &egrave; 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.'|';
@@ -161,12 +165,10 @@ if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
     echo show_notify(str_replace("\n", " ", $G_room_roadmap[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lang], 400, 200);
   }
   else if ($argz[0] == 'placing') {
-    GLOBAL $G_false;
-
     require_once("briskin5/Obj/briskin5.phh");
     require_once("briskin5/Obj/placing.phh");
 
-    echo show_notify(str_replace("\n", " ", placings_show($G_false)), 0, $mlang_indwr['btn_close'][$G_lang], 800, 600);
+    echo show_notify(str_replace("\n", " ", placings_show(FALSE)), 0, $mlang_indwr['btn_close'][$G_lang], 800, 600);
   }
   else if ($argz[0] == 'whysupport') {
     echo show_notify(str_replace("\n", " ", $G_room_whysupport[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lng], 400, 200);
@@ -200,7 +202,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,59 +233,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) {
-      $bdb = new BriskDB();
-      $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: "<b>Il database è temporaneamente irraggiungibile, riprova più tardi o contatta l\'amministratore.</b>" */
+                $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: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
+            $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]);
+        }
+        
     }
     else {
-      /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
-      $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]);
+        /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
+        $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['mesgmust'][$G_lang]);
     }
     
-  }
-  else {
-    /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
-    $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();
+    }
 }
 
 
@@ -293,7 +297,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;
@@ -391,7 +394,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();
   }
@@ -501,13 +503,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: "<b>Il server sta per essere riavviato, non possono avere inizio nuove partite.</b>", "<b>Il tavolo a cui volevi sederti richiede autentifica.</b>", "<b>Il tavolo si &egrave; appena liberato, ci si potr&agrave; sedere tra %d secondi.</b>" */
         if ($G_shutdown) {
           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['shutmsg'][$G_lang]);
@@ -568,8 +567,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("");