init script installation added and version updated
[brisk.git] / web / Obj / brisk.phh
index d2b7173..804c6ee 100644 (file)
@@ -58,7 +58,7 @@ define('EXPIRE_TIME_WAG', 10);
 define('WAKEUP_TIME', 12);
 // BAN_TIME da allineare anche in commons.js
 define('BAN_TIME', 3600);
-define('GARBAGE_TIMEOUT', 10);
+define('GARBAGE_TIMEOUT', 5);
 define('NICKSERV', "<i>BriskServ</i>");
 
 define('LOCK_SHARE_MAX', 10000);
@@ -140,10 +140,10 @@ $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
 $G_lng = langtolng($G_lang);
 
 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
-$G_brisk_version = "3.6.3";
+$G_brisk_version = "4.6.0";
 
 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
-$root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo sistema di evidenziazione degli utenti registrati.',
+$root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: aggiunti script di avvio automatico, gestione dei segnali e logging dell\' ultima connessione.',
                                        'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
                        'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
                                        'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
@@ -288,6 +288,18 @@ Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nasta
 <br><b>version '.$G_brisk_version.'</b><br><br>
 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
 
+function addrtoipv4($addr)
+{
+    $ipv4addr_arr = explode(':' , $addr);
+    if (isset($ipv4addr_arr[3])) {
+        $ipv4addr = $ipv4addr_arr[3];
+    }
+    else {
+        $ipv4addr = $addr;
+    }
+    return $ipv4addr;
+}
+
 function mop_flush()
 {
     for ($i = 0; $i < ob_get_level(); $i++)
@@ -325,42 +337,6 @@ function file_unlock($res)
     }
 }
 
-function webservers_exceeded()
-{
-    return(file_exists(PROXY_PATH."/webservers_exceded.flag"));
-}
-
-function webservers_check()
-{
-    GLOBAL $G_webserver_max;
-
-    /* FIXME: check all procs expirations */
-    return (10);
-
-    $ct = 0;
-
-    $dh = opendir('/proc');
-    while (($file = readdir($dh)) !== false) {
-        if (preg_match('/[0-9]+/', $file)) {
-            $cmdline = explode("\0", file_get_contents('/proc/'.$file.'/cmdline'));
-            // echo "xxx".$cmdline[0].$n;
-            if (strstr('/usr/sbin/apache2', $cmdline[0]) != FALSE) {
-                // echo "yyy".$cmdline[0].$n;
-                $ct++;
-            }
-        }
-    }
-    closedir($dh);
-
-    if ($ct >= $G_webserver_max) {
-        touch(PROXY_PATH."/webservers_exceded.flag");
-    }
-    else {
-        unlink(PROXY_PATH."/webservers_exceded.flag");
-    }
-    return ($ct);
-}
-
 $escpush_from = array("\\", "\"");
 $escpush_to   = array("\\\\", "\\\"");
 function escpush($s)
@@ -670,48 +646,102 @@ class Table {
 } // end class Table
 
 
+class Delay_Manager
+{
+    var $delta;
+    var $lastckeck;
+    var $triglevel;
 
+    function Delay_Manager($triglevel)
+    {
+        $this->triglevel = $triglevel;
+        $this->delta = array();
+        $this->lastcheck = 0;
+    }
 
-class Room {
-    static $delta_t;
+    function delta_get($curtime)
+    {
+        // clean too old delta items
+        for ($i = 0 ; $i < count($this->delta) ; $i++) {
+            if ($this->delta[$i][0] < $curtime) {
+                array_splice($this->delta, $i, 1);
+                $i--;
+            }
+        }
 
-  var $user;
-  var $table;
-  var $match;
-  var $comm; // commands for many people
-  var $step; // current step of the comm array
-  var $garbage_timeout;
-  var $shm_sz;
+        // add new delta items if delay exceeded $this->triglevel sec
+        if ($curtime > $this->lastcheck + $this->triglevel && $curtime < $this->lastcheck + 1200.0) {
+            $delta = $curtime - $this->lastcheck - $this->triglevel;
+            array_push($this->delta, array($curtime + $delta , $delta));
+            // fprintf(STDERR, "DELTA: add new delta [%f] [%f] [%f]\n", $this->triglevel, $curtime + $delta, $delta);
+        }
 
-  function Room () {
-    $this->user  = array();
-    $this->table = array();
-    $this->match = array();
+        // extract the maximum valid delta
+        $delta_max = 0.0;
+        for ($i = 0 ; $i < count($this->delta) ; $i++) {
+            $delta_cur = $this->delta[$i][1];
+            if ($delta_max < $delta_cur)
+                $delta_max = $delta_cur;
+        }
 
-    for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
-        $this->user[$i] = User::create($this, $i, "", "");
+        // fprintf(STDERR, "DELTA: status %d, delta_max: %f\n", count($this->delta), $delta_max);
+
+        return ($delta_max);
     }
 
-    for ($i = 0 ; $i < TABLES_N ; $i++) {
-      $this->table[$i] = Table::create($i);
-      /* OLD METHOD
-      if ($i < 12) {
-        $row = ( (((int)($i / 4)) % 2) == 0 );
-        $col = ($i % 2 == 0);
-        $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
-      }
-      else {
-        $this->table[$i]->auth_only = FALSE;
-      }
-      */
-      if ($i < TABLES_AUTH_N) 
-        $this->table[$i]->auth_only = TRUE;
-      else
-        $this->table[$i]->auth_only = FALSE;
+    function lastcheck_set($curtime)
+    {
+        $this->lastcheck = $curtime;
+    }
+}
+
+class Room
+{
+    static $delta_t;
+    
+    var $crystal_filename;
+    var $user;
+    var $table;
+    var $match;
+    var $comm; // commands for many people
+    var $step; // current step of the comm array
+    var $garbage_timeout;
+    var $shm_sz;
+    
+    var $delay_mgr;
+
+    function Room ($crystal_filename) {
+        $this->crystal_filename = $crystal_filename;
+        $this->user  = array();
+        $this->table = array();
+        $this->match = array();
+        
+        for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
+            $this->user[$i] = User::create($this, $i, "", "");
+        }
+        
+        for ($i = 0 ; $i < TABLES_N ; $i++) {
+            $this->table[$i] = Table::create($i);
+            /* OLD METHOD
+               if ($i < 12) {
+               $row = ( (((int)($i / 4)) % 2) == 0 );
+               $col = ($i % 2 == 0);
+               $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
+               }
+               else {
+               $this->table[$i]->auth_only = FALSE;
+               }
+            */
+            if ($i < TABLES_AUTH_N) 
+                $this->table[$i]->auth_only = TRUE;
+            else
+                $this->table[$i]->auth_only = FALSE;
+        }
+        $this->garbage_timeout = 0;
+        $this->shm_sz = SHM_DIMS_MIN;
+
+        $this->delay_mgr = new Delay_Manager(1.5);
     }
-    $this->garbage_timeout = 0;
-    $this->shm_sz = SHM_DIMS_MIN;
-  }
 
   function garbage_manager($force)
   {
@@ -722,14 +752,15 @@ class Room {
     log_rd2("garbage_manager START");
 
     /* Garbage collector degli utenti in timeout */
-    $curtime = time();
+    $curtime = microtime(TRUE);
+
+    $delta = $this->delay_mgr->delta_get($curtime);
 
     if (!$force && !($this->garbage_timeout < $curtime)) {
+        $this->delay_mgr->lastcheck_set($curtime);
         return ($ismod);
     }
       
-    webservers_check();
-    
     // Before all align times with table timeout
     for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
        $table_cur = $this->table[$table_idx];
@@ -750,7 +781,6 @@ class Room {
                     $bri = FALSE;
                 }
 
-
                 if ($bri != FALSE) {
                     //
                     //  SPAWN: JOIN
@@ -785,7 +815,7 @@ class Room {
                             $user_cur->bantime    = $bri_user->bantime;
                         }
                         
-                        log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME", $plist);
+                        log_legal($curtime, $user_cur->ip, $user_cur, "STAT:DESTROY_GAME", $plist);
                         
                         $this->room_join_wakeup($user_cur, FALSE, 0); 
                         $table_cur->table_token = "";
@@ -822,7 +852,7 @@ class Room {
                     for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
                         $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
                     }
-                    log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
+                    log_legal($curtime, $user_cur->ip, $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
                     
                     $this->room_join_wakeup($user_cur, TRUE, -2); 
                     $table_cur->table_token = "";
@@ -841,9 +871,9 @@ class Room {
        if ($user_cur->sess == "") 
             continue;
        
-       if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
+       if ($user_cur->lacc + EXPIRE_TIME_RD < ($curtime - $delta)) {
             // Auto logout dell'utente
-            log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
+            log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime - delta ".($curtime - $delta));
             
             if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
                 log_auth($user_cur->sess, "Autologout session.");
@@ -860,7 +890,7 @@ class Room {
             }
        }
 
-       if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
+       if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < ($curtime - $delta)) { // lo rimettiamo in piedi
             if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
                 $this->room_wakeup($user_cur);
                 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
@@ -875,6 +905,7 @@ class Room {
     $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
     $ismod = TRUE;
 
+    $this->delay_mgr->lastcheck_set($curtime);
     return ($ismod);
   }
 
@@ -902,7 +933,7 @@ class Room {
 
     $ret .= sprintf('stat = "%s";',  $user->stat);
     
-    $ret .= root_wellcome($user);
+    $ret .= root_welcome($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);
@@ -984,7 +1015,7 @@ class Room {
 
       $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
       if ($from_table && ($user_cur->table == $table_idx || $user->idx_get() == $i)) {
-       $ret .= 'gst.st_loc++; hstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
+       $ret .= 'gst.st_loc++; xstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
        // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
        log_main("DOCUMENT.index.php: from table");
       }
@@ -1219,7 +1250,7 @@ class Room {
     $only_you = FALSE;
     
     // common settings
-    $msg = substr($mesg, 6, 128);
+    $msg = mb_substr($mesg, 6, 128, "UTF-8");
     $curtime = time();
     $dt = date("H:i ", $curtime);
     $target = "";
@@ -1463,6 +1494,18 @@ class Room {
         else if (strcasecmp($st_str, "pulizie") == 0) {
           $st = USER_FLAG_S_MOP;
         }
+        else if (strcasecmp($st_str, "babbo") == 0) {
+          $st = USER_FLAG_S_BABBO;
+        }
+        else if (strcasecmp($st_str, "renna") == 0) {
+          $st = USER_FLAG_S_RENNA;
+        }
+        else if (strcasecmp($st_str, "pupazzo") == 0) {
+          $st = USER_FLAG_S_PUPAZ;
+        }
+        else if (strcasecmp($st_str, "vischio") == 0) {
+          $st = USER_FLAG_S_VISCH;
+        }
         else {
           /* MLANG: "Questo stato non esiste." */
           $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['statunkn'][$G_lang]);
@@ -1507,7 +1550,7 @@ class Room {
         $to_room = $to_user;
       }
 
-      log_legal($curtime, 'xxx', $user, 
+      log_legal($curtime, $user->ip, $user,
                ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
       
       $user->chat_lst = "$msg";
@@ -1561,46 +1604,27 @@ class Room {
     }
     
     if ($to_tabl) {
-      // FIXME BRISK4: include for each kind of table
-      require_once("${G_base}briskin5/Obj/briskin5.phh");
-      // Before all align times with table timeout
-      for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
-        $table_cur = $this->table[$table_idx];
-        // if the table is complete and exists its shared mem we get the info about users lacc
-        
-        if ($table_cur->player_n == PLAYERS_N) {
-          log_main("PLAYERS == N TABLE ".$table_idx);
-        
-          if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) { 
-            log_main("bin5 lock data success");
-            
-            $no_recovery = FALSE;
-            if (($bri = Bin5::load_data($table_idx)) != FALSE) {
-              if ($table_cur->table_token != $bri->table_token) {
-                log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
-                $bri = FALSE;
-              }
-            }
-            
-            if ($bri != FALSE) {
-              $bri_table = $bri->table[0];
-              for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
-                // stat must be "table" by definition
-                $bri_user = $bri->user[$i];
+        // FIXME BRISK4: include for each kind of table
+        require_once("${G_base}briskin5/Obj/briskin5.phh");
+        // Before all align times with table timeout
+        for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+            if (isset($this->match[$table_idx])) {
+                $bri = $this->match[$table_idx];
+
+                $bri_table = $bri->table[0];
+                for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
+                    // stat must be "table" by definition
+                    $bri_user = $bri->user[$i];
               
-                if ($target != "" && $bri_user->name != $target)
-                  continue;
-                log_main("writa: ".$user_mesg);
-                $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
-                $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
-                $bri_user->step_inc();
-              }
-              Bin5::save_data($bri);
-            }
-            Bin5::unlock_data($sem);
-          } // bri::lock_data
-        } //  if ($table_cur->player_n == PLAYERS_N) {
-      } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
+                    if ($target != "" && $bri_user->name != $target)
+                        continue;
+                    log_main("writa: ".$user_mesg);
+                    $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
+                    $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
+                    $bri_user->step_inc();
+                }
+            } // if (isset($this->match
+        } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
     } // if ($to_tabl == true ...
 
     if ($update_room) {
@@ -1736,6 +1760,7 @@ class Room {
 
       $ghost_user = $this->user[$ghost];
       $curtime = time();
+      $ghost_user->comm[$ghost_user->step % COMM_N] = "";
       $ghost_user->step_inc();
       if ($sess == "") {
         $sess = uniqid(""); 
@@ -1748,18 +1773,16 @@ class Room {
       // If user at the table we need to update the table data too
       $table_idx = $ghost_user->table;
       if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
-        // FIXME BRISK4: include for each kind of table
-        require_once("${G_base}briskin5/Obj/briskin5.phh");
-        if (($brisem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) { 
-          if (($bri = Bin5::load_data($table_idx)) != FALSE) {
-            if ($bri->the_end != TRUE) {
-              $bri->user[$ghost_user->table_pos]->step_inc();
-              $bri->user[$ghost_user->table_pos]->sess = $sess;
-              Bin5::save_data($bri);
-            }
+          require_once("${G_base}briskin5/Obj/briskin5.phh");
+          if (isset($this->match[$table_idx])) {
+              $bri = $this->match[$table_idx];
+
+              if ($bri->the_end != TRUE) {
+                  $bri->user[$ghost_user->table_pos]->comm[$bri->user[$ghost_user->table_pos]->step % COMM_N] = "";
+                  $bri->user[$ghost_user->table_pos]->step_inc();
+                  $bri->user[$ghost_user->table_pos]->sess = $sess;
+              }
           }
-          Bin5::unlock_data($brisem);
-        }
       }
 
       $idx = $ghost;
@@ -1883,10 +1906,18 @@ class Room {
   }
 
   // Static functions
-  static function create()
+  static function create($crystal_filename)
   {
-    $room = new Room();
-    
+      if (($room_ser = @file_get_contents($crystal_filename)) == FALSE ||
+          ($room = unserialize($room_ser)) == FALSE) {
+          fprintf(STDERR, "NEW ROOM\n");
+          $room = new Room($crystal_filename);
+      }
+      else {
+          fprintf(STDERR, "ROOM FROM FILE\n");
+          rename($crystal_filename, $crystal_filename.".old");
+      }
+
     return $room;
   }
   
@@ -1896,7 +1927,7 @@ class Room {
     GLOBAL $sess;
 
     do {
-      if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
+      if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1) {
        log_main("ftok failed");
        break;
       }
@@ -1961,7 +1992,7 @@ class Room {
     
     // var_dump($room);
     
-    if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) 
+    if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1) 
       return (FALSE);
     
     while ($room->shm_sz < SHM_DIMS_MAX) {
@@ -1986,7 +2017,16 @@ class Room {
     
     return (FALSE);
   }
-
+  function dump_data()
+  {
+      $room_ser = serialize($this);
+      $room_ser_len = mb_strlen($room_ser, "ASCII");
+      if (file_put_contents($this->crystal_filename, $room_ser) == $room_ser_len) {
+          return (TRUE);
+      }
+      
+      return (FALSE);
+  }
 
   function save_data($room) 
   {
@@ -1995,7 +2035,7 @@ class Room {
       $ret =   FALSE;
       $shm =   FALSE;
     
-      if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) 
+      if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1) 
           return (FALSE);
     
       // SHSPLIT: before save the $room you must save users, 
@@ -2133,31 +2173,43 @@ class Room {
     return ($ret);
   }
 
-  function request_mgr(&$s_a_p, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
+  function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
   {
       printf("NEW_SOCKET (root): %d\n", intval($new_socket));
 
+      $enc = get_encoding($header);
+      if (isset($header['User-Agent'])) {
+          if (strstr($header['User-Agent'], "MSIE")) {
+              $transp_type = "htmlfile";
+          }
+          else {
+              $transp_type = "xhr";
+          }
+      }
+      else {
+          $transp_type = "iframe";
+      }
       force_no_cache($header_out);
 
       switch ($path) {
       case "":
       case "index.php":
           ob_start();
-      index_main($this, $header_out, $addr, $get, $post, $cookie);
-      $content = ob_get_contents();
-      ob_end_clean();
+          index_main($this, $transp_type, $header_out, $addr, $get, $post, $cookie);
+          $content = ob_get_contents();
+          ob_end_clean();
 
-      $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
-      return TRUE;
+          $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
+          return TRUE;
 
-      break;
+          break;
       case "index_wr.php":
           ob_start();
           index_wr_main($this, $addr, $get, $post, $cookie);
           $content = ob_get_contents();
           ob_end_clean();
-
-          $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
+          
+          $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
           return TRUE;
 
           break;
@@ -2165,9 +2217,12 @@ class Room {
           do {
               if (!isset($cookie['sess'])
                   || (($user = $this->get_user($cookie['sess'], $idx)) == FALSE)) {
-                  $content = User::stream_fini(TRUE);
+                  if (($transp  = gpcs_var('transp', $get, $post, $cookie)) === FALSE)
+                      $transp = "iframe";
                   
-                  $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
+                  $content = User::stream_fini($transp, $s_a_p->rndstr, TRUE);
+
+                  $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
                   return TRUE;
 
                   break;
@@ -2179,11 +2234,11 @@ class Room {
                   printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
                   $user->rd_socket_set(NULL);
               }
-              
+
               $content = "";
-              $user->stream_init($header_out, $content, $get, $post, $cookie);
+              $user->stream_init($s_a_p->rndstr, $enc, $header_out, $content, $get, $post, $cookie);
               
-              $response = headers_render($header_out, -1).chunked_content($content);
+              $response = headers_render($header_out, -1).chunked_content($user->rd_zls_get(), $content);
               $response_l = mb_strlen($response, "ASCII");
               
               $wret = @fwrite($new_socket, $response, $response_l);
@@ -2211,7 +2266,7 @@ class Room {
           $subs = "briskin5/";
           $subs_l = strlen($subs);
           if (!strncmp($path, $subs, $subs_l)) {
-              $ret = Bin5::request_mgr(&$s_a_p, &$header_out, &$new_socket, substr($path, $subs_l) , $addr, $get, $post, $cookie);
+              $ret = Bin5::request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, substr($path, $subs_l) , $addr, $get, $post, $cookie);
               return ($ret);
           }
           break;
@@ -2600,8 +2655,8 @@ function log_shme($log)
 
 
 
-// function log_legal($curtime, $sess, $name, $where, $mesg) 
-function log_legal($curtime, $addr, $user, $where, $mesg) 
+// function log_legal($curtime, $sess, $name, $where, $mesg)
+function log_legal($curtime, $addr, $user, $where, $mesg)
 {
 
   if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
@@ -2651,7 +2706,7 @@ function show_notify_ex($text, $tout, $butt, $w, $h, $is_opaque, $block_time)
 }
 
 
-function root_wellcome($user)
+function root_welcome($user)
 {
   GLOBAL $root_wellarr, $G_lang;
   $ret = "";
@@ -2677,7 +2732,7 @@ function validate_sess($sess)
 
 function validate_name($name) 
 {
-  $name_new = str_replace(' ', '_', substr(trim($name),0,12));
+    $name_new = str_replace(' ', '_', mb_substr(trim($name),0,12, "UTF-8"));
 
   for ($i = 0 ; $i < strlen($name_new) ; $i++) {
     $c = $name_new[$i];