login = $login; $this->token = $token; $this->ip = $ip; $this->tstamp = $tstamp + CHAL_VALID_TIME; } } class Challenges { static $delta_t; var $item; var $item_n; var $mod; var $shm_sz; var $garbage_timeout; function Challenges() { $this->item = array(); $this->item_n = 0; $this->garbage_timeout = 0; $this->mod = FALSE; } function add($login, $token, $ip, $tstamp) { $chal = null; log_auth("xxx", sprintf("Challenges::add [%s]\n", $login)); // FIXME Checks here if ($login == '') { return (FALSE); } // log_auth("xxx", "LOOPI tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime); if (($chal = new Challenge($login, $token, $ip, $tstamp)) == FALSE) { return (FALSE); } $this->item[$this->item_n] = $chal; $this->item_n++; $this->mod = TRUE; return ($chal); } /* remove all istances related to $login */ function rem($login) { $ismod = FALSE; for ($i = 0 ; $i < $this->item_n ; $i++) { if ($this->item[$i]->login == $login) { $ismod = TRUE; for ($e = $i ; $e < ($this->item_n - 1) ; $e++) { $this->item[$e] = $this->item[$e + 1]; } $i--; $this->item_n--; unset($this->item[$this->item_n]); $this->mod = TRUE; } } return ($ismod); } function garbage_manager() { $curtime = time(); // FIXME remove set to 0 $this->garbage_timeout = 0; if ($this->garbage_timeout > $curtime) return (FALSE); $ismod = FALSE; for ($i = 0 ; $i < $this->item_n ; $i++) { log_auth("xxx", "LOOPI item: ".$i." tstamp: ".$this->item[$i]->tstamp." curtime: ".$curtime); if ($this->item[$i]->tstamp < $curtime) { for ($e = $i ; $e < ($this->item_n - 1) ; $e++) { $this->item[$e] = $this->item[$e + 1]; } $i--; $this->item_n--; log_auth("xxx", "LOOPI unset: ".$this->item_n); unset($this->item[$this->item_n]); $ismod = TRUE; $this->mod = TRUE; } } log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n ); $this->garbage_timeout = $curtime + CHAL_GARBAGE_TIMEOUT; return ($ismod); } function ismod() { return ($this->mod); } // Static functions static function create() { $chal =& new Challenges(); $chal->mod = TRUE; return $chal; } function load_data() { GLOBAL $sess; do { if (($tok = @ftok(FTOK_PATH."/challenges", "C")) == -1) { log_main("ftok failed"); break; } if (($shm_sz = sharedmem_sz($tok)) == -1) { log_main("shmop_open failed"); } if ($shm_sz == -1) $shm_sz = CHAL_SHM_DIMS_MIN; if ($shm = shm_attach($tok, $shm_sz)) { $chals = @shm_get_var($shm, $tok); // CHECKED BELOW log_only("challenges == ".($chals == FALSE ? "FALSE" : "TRUE")." challenges === ".($chals === FALSE ? "FALSE" : "TRUE")." challenges isset ".(isset($chals) ? "TRUE" : "FALSE")); if ($chals == FALSE) { log_only("INIT CHALLENGES DATA"); $chals =& Challenges::create(); if (@shm_put_var($shm, $tok, $chals) == FALSE) { log_only("PUT_VAR FALLITA ".strlen(serialize($chals))); log_only(serialize($chals)); } log_shme("Challenges::save_data2"); } $chals->shm_sz = $shm_sz; shm_detach($shm); } $chals->garbage_manager(); return ($chals); } while (0); return (FALSE); } function save_data($chals) { $shm = FALSE; $oldmod = $chals->mod; if (($tok = @ftok(FTOK_PATH."/challenges", "C")) == -1) return (FALSE); while ($chals->shm_sz < CHAL_SHM_DIMS_MAX) { if (($shm = shm_attach($tok, $chals->shm_sz)) == FALSE) break; if (isset($chals)) log_only("challenges count ".count($chals->item)." _n: ".$chals->item_n); $chals->mod = FALSE; if (@shm_put_var($shm, $tok, $chals) != FALSE) { log_shme("Challenges::save_data"); shm_detach($shm); return (TRUE); } $chals->mod = $oldmod; if (shm_remove($shm) === FALSE) { log_only("REMOVE FALLITA"); break; } shm_detach($shm); $chals->shm_sz += CHAL_SHM_DIMS_DLT; } if ($shm) shm_detach($shm); return (FALSE); } static function lock_data($is_exclusive) { if (($res = file_lock(FTOK_PATH."/challenges", $is_exclusive)) != FALSE) { self::$delta_t = microtime(TRUE); log_lock("LOCK challenges [".self::$delta_t."]"); return ($res); } return (FALSE); } static function unlock_data($res) { GLOBAL $sess; log_lock("UNLOCK challenges [".(microtime(TRUE) - (self::$delta_t))."]"); file_unlock($res); } } // End CLASS Challenges ?>