- if (empty($_SESSION['authenticated'])) {
- return false;
- }
-
- return $_SESSION['authenticated'];
- }
-
- /**
- * @brief Calculate the hash that is needed for the "Friendica" cookie
- *
- * @param array $user Record from "user" table
- *
- * @return string Hashed data
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- private static function getCookieHashForUser($user)
- {
- return hash_hmac(
- "sha256",
- hash_hmac("sha256", $user["password"], $user["prvkey"]),
- Config::get("system", "site_prvkey")
- );
- }
-
- /**
- * @brief Set the "Friendica" cookie
- *
- * @param int $time
- * @param array $user Record from "user" table
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- public static function setCookie($time, $user = [])
- {
- if ($time != 0) {
- $time = $time + time();
- }
-
- if ($user) {
- $value = json_encode([
- "uid" => $user["uid"],
- "hash" => self::getCookieHashForUser($user),
- "ip" => ($_SERVER['REMOTE_ADDR'] ?? '') ?: '0.0.0.0'
- ]);
- } else {
- $value = "";
- }
-
- setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL), true);
- }
-
- /**
- * @brief Checks if the "Friendica" cookie is set
- *
- * @param string $hash
- * @param array $user Record from "user" table
- *
- * @return boolean True, if the cookie is set
- *
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- */
- public static function checkCookie(string $hash, array $user)
- {
- return hash_equals(
- self::getCookieHashForUser($user),
- $hash
- );
- }