Example #1
0
 /**
  * Prepares session after waking up
  *
  * @param int $expiry
  * @param string $salt
  * @param int $cost
  * @return bool
  * @throws SessionException
  */
 public function decodeData(int $expiry, string $salt, int $cost) : bool
 {
     // Check if this session needs decoding
     if (is_array($this->data) || empty($this->encoded)) {
         throw new SessionException(__METHOD__, "Session is already decoded", 1502);
     }
     // Check validity
     $span = microtime(true) - $this->timeStamp["last"];
     if ($span >= $expiry) {
         // Session has expired
         return false;
     }
     // Checksum
     if (!hash_equals(Session::saltedHash($this->encoded, $salt, $cost), $this->hash)) {
         throw new SessionException(__METHOD__, "Session checksum failed", 1503);
     }
     // Decode data
     $this->data = unserialize($this->encoded, ["allowed_classes" => ["Comely\\IO\\Session\\ComelySession\\Bag"]]);
     if (!$this->data instanceof Bag) {
         throw new SessionException(__METHOD__, "Failed to un-serialize data bags", 1504);
     }
     // Release "encoded" property
     $this->encoded = null;
     // Successfully decoded
     return true;
 }