strlen to mb_strlen with ASCII charset to avoid binary string wrong length calculation
[brisk.git] / web / briskin5 / Obj / briskin5.phh
index 1d8d0d9..718fabd 100644 (file)
@@ -509,22 +509,7 @@ class Bin5_user extends User {
     {
         $this->step = $step & 0x7fffffff;
         
-        do {
-            if (validate_sess($this->sess) == FALSE)
-                break;
-            if (file_exists(BIN5_PROXY_PATH) == FALSE)
-                mkdir(BIN5_PROXY_PATH, 0775, TRUE);
-            if (($fp = @fopen(BIN5_PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
-                break;
-            fwrite($fp, pack("LL",$this->step, $this->idx));
-            fclose($fp);
-            
-            log_main("step_set [".$this->sess. "] [".$this->step."]"); 
-            
-            return (TRUE);
-        } while (0);
-        
-        return (FALSE);
+        return TRUE;
     }
     
     function step_inc($delta = 1) {
@@ -532,22 +517,10 @@ class Bin5_user extends User {
         /* modularization because unpack() not manage unsigned 32bit int correctly */
         $this->step &= 0x7fffffff;
         
-        if (validate_sess($this->sess)) {
-            if (file_exists(BIN5_PROXY_PATH) == FALSE)
-                mkdir(BIN5_PROXY_PATH);
-            $fp = fopen(BIN5_PROXY_PATH."/".$this->sess.".step", 'w');
-            fwrite($fp, pack("LL", $this->step, $this->idx));
-            fclose($fp);
-            
-            log_main("step_inc [".$this->sess. "] [".$this->step."]"); 
-            
-            return (TRUE);
-        }
-        
-        return (FALSE);
+        return (TRUE);
     }
-    
-    static function step_get($sess)
+
+    static function load_step($sess)
     {
         $fp = FALSE;
         do {
@@ -560,7 +533,7 @@ class Bin5_user extends User {
                 break;
             if (($s = fread($fp, 8)) == FALSE)
                 break;
-            if (strlen($s) != 8)
+            if (mb_strlen($s, "ASCII") != 8)
                 break;
             $arr = unpack('Ls/Li', $s);
             fclose($fp);
@@ -576,13 +549,31 @@ class Bin5_user extends User {
         
         return (FALSE);
     }
+
+    function save_step() 
+    {
+        do {
+            if (validate_sess($this->sess) == FALSE)
+                break;
+            if (file_exists(BIN5_PROXY_PATH) == FALSE)
+                mkdir(BIN5_PROXY_PATH, 0775, TRUE);
+            if (($fp = @fopen(BIN5_PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
+                break;
+            fwrite($fp, pack("LL",$this->step, $this->idx));
+            fclose($fp);
+            
+            log_main("step_set [".$this->sess. "] [".$this->step."]"); 
+            
+            return (TRUE);
+        } while (0);
+        
+        return (FALSE);
+    }
+    
     
     static function load_data($tab_id, $id, $sess) 
     {
-        // error_log("Bin5_user::load_data BEGIN", 0);
-        error_log("Bin5_user::load_data BEGIN id [".$id."]", 0);
-        
-        log_main("load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
+        log_main("Bin5_user::load_data: tab_id [".$tab_id."] id [".$id."] sess [".($sess == FALSE ? "FALSE" : $sess)."] ");
         
         $doexit = FALSE;
         do {
@@ -607,8 +598,6 @@ class Bin5_user extends User {
                     break;
                 }
                 log_only("user ==  ".($user == FALSE ?   "FALSE" : "TRUE")."  user ===  ".($user === FALSE ? "FALSE" : "TRUE")."  user isset ".(isset($user) ?   "TRUE" : "FALSE"));
-                if (isset($user)) 
-                    log_only("bri count ".count($user));
                 
                 if ($user == FALSE) {
                     log_only("INIT MAIN DATA");
@@ -620,6 +609,19 @@ class Bin5_user extends User {
                         log_only(serialize($user));
                     }
                 }
+                else {
+                    if ($sess != FALSE) {
+                        // This part isn't strictly required but is good to verify
+                        // the coerence of cached and User class saved value of step field.
+                        $old_step = $user->step;
+                        $arr = Bin5_user::load_step($sess);
+                        $user->step = $arr['s'];
+                        if ($old_step != $user->step) {
+                            log_crit("Bin5:: steps are diffetents User->step ".$user->step." Old_step: ".$old_step);
+                        }
+
+                    }
+                }
                 
                 $user->shm_sz = $shm_sz;
                 
@@ -644,9 +646,6 @@ class Bin5_user extends User {
       
       $shm =   FALSE;
       
-      // var_dump($user);
-      error_log("Bin5_user::save_data BEGIN id [".$id."]", 0);
-      
       if (($tok = @ftok(FTOK_PATH."/bin5/table".$tab_id."/user".$id, "B")) == -1) {
           return (FALSE);
       }
@@ -657,6 +656,7 @@ class Bin5_user extends User {
           // log_only("PUT_VAR DI ".strlen(serialize($user)));
           if (shm_put_var($shm, $tok, $user) != FALSE) {
               shm_detach($shm);
+              $user->save_step();
               log_main("User[".$id."] saved.");
               return (TRUE);
           }
@@ -810,7 +810,7 @@ class Bin5 {
     $doexit = FALSE;
     $shm = FALSE;
 
-    log_wr("TABLE_IDX ".FTOK_PATH."/bin5/table".$table_idx."/table");
+    log_wr("TABLE_FTOK ".FTOK_PATH."/bin5/table".$table_idx."/table");
     
     do {
       if (($tok = @ftok(FTOK_PATH."/bin5/table".$table_idx."/table", "B")) == -1) {
@@ -1158,7 +1158,7 @@ class Bin5 {
               break;
           if (($s = fread($fp, 4)) == FALSE)
               break;
-          if (strlen($s) != 4)
+          if (mb_strlen($s, "ASCII") != 4)
               break;
           $arr = unpack('Le', $s);
           if ($arr['e'] > $tm)