/** * @return Forms */ public function forms() : Forms { if (!isset($this->forms)) { $this->forms = new Forms($this->sessionBag->getBag("Forms")); } return $this->forms; }
/** * @param \string[] ...$keys * @return Obfuscator */ public function setFields(string ...$keys) : self { $keysCount = count($keys); $bitsCount = $keysCount * (self::OBFUSCATE_KEY_SIZE * 4); // Get cryptographically secure random bytes $bytes = Security::randomKey($bitsCount); $bytes = str_split($bytes, self::OBFUSCATE_KEY_SIZE); // Make sure there are no duplicates some how if (count($bytes) !== count(array_unique($bytes))) { // Repeating key detected, retry return call_user_func_array([$this, "setFields"], $keys); } // Iterate through keys $count = 0; foreach ($keys as $key) { $this->obfuscated[$key] = $bytes[$count]; $count++; } // Save hash $this->hash = hash("sha1", implode(":", array_keys($this->obfuscated))); // Save to session? if (isset($this->sessionBag)) { $this->sessionBag->getBag($this->name)->set("hash", $this->hash)->set("fields", $this->obfuscated); } // Chain return $this; }
/** * @return string */ public function getToken() : string { $token = $this->sessionBag->get("token"); $expire = $this->sessionBag->get("expire"); // Check expire prop. data type and if its > 0 if (is_int($expire) && $expire > 0) { // Check if token is expired if (time() >= $expire) { $token = null; // Expired $this->sessionBag->remove("token")->remove("expire"); } } // Return token or an empty string return $token ?? ""; }
/** * @return Forms */ public function flush() : self { $this->sessionBag->flush(); return $this; }