2018-10-17 14:19:58 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file /src/Core/Authentication.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
|
|
|
use Friendica\BaseObject;
|
2018-10-21 07:53:47 +02:00
|
|
|
use Friendica\Database\DBA;
|
2018-11-08 00:22:15 +01:00
|
|
|
use Friendica\Model\User;
|
2018-10-21 07:53:47 +02:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-10-17 14:19:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Authentification, Session and Cookies
|
2018-10-21 07:53:47 +02:00
|
|
|
*/
|
2018-10-17 14:19:58 +02:00
|
|
|
class Authentication extends BaseObject
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @brief Calculate the hash that is needed for the "Friendica" cookie
|
|
|
|
*
|
|
|
|
* @param array $user Record from "user" table
|
|
|
|
*
|
|
|
|
* @return string Hashed data
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-17 14:19:58 +02:00
|
|
|
*/
|
2018-10-17 21:30:41 +02:00
|
|
|
public static function getCookieHashForUser($user)
|
2018-10-17 14:19:58 +02:00
|
|
|
{
|
|
|
|
return(hash("sha256", Config::get("system", "site_prvkey") .
|
|
|
|
$user["prvkey"] .
|
|
|
|
$user["password"]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the "Friendica" cookie
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param int $time
|
2018-10-17 14:19:58 +02:00
|
|
|
* @param array $user Record from "user" table
|
2019-01-06 22:06:53 +01:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-17 14:19:58 +02:00
|
|
|
*/
|
2018-10-17 21:30:41 +02:00
|
|
|
public static function setCookie($time, $user = [])
|
2018-10-17 14:19:58 +02:00
|
|
|
{
|
|
|
|
if ($time != 0) {
|
|
|
|
$time = $time + time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$value = json_encode(["uid" => $user["uid"],
|
2018-10-17 21:30:41 +02:00
|
|
|
"hash" => self::getCookieHashForUser($user),
|
2018-10-17 14:19:58 +02:00
|
|
|
"ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]);
|
|
|
|
} else {
|
|
|
|
$value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == SSL_POLICY_FULL), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sets the provided user's authenticated session
|
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @todo Should be moved to Friendica\Core\Session once it's created
|
2018-10-17 14:19:58 +02:00
|
|
|
*
|
2019-01-06 22:06:53 +01:00
|
|
|
* @param array $user_record
|
|
|
|
* @param bool $login_initial
|
|
|
|
* @param bool $interactive
|
|
|
|
* @param bool $login_refresh
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-17 14:19:58 +02:00
|
|
|
*/
|
2018-10-17 21:30:41 +02:00
|
|
|
public static function setAuthenticatedSessionForUser($user_record, $login_initial = false, $interactive = false, $login_refresh = false)
|
2018-10-17 14:19:58 +02:00
|
|
|
{
|
|
|
|
$a = self::getApp();
|
|
|
|
|
|
|
|
$_SESSION['uid'] = $user_record['uid'];
|
|
|
|
$_SESSION['theme'] = $user_record['theme'];
|
|
|
|
$_SESSION['mobile-theme'] = PConfig::get($user_record['uid'], 'system', 'mobile_theme');
|
|
|
|
$_SESSION['authenticated'] = 1;
|
|
|
|
$_SESSION['page_flags'] = $user_record['page-flags'];
|
|
|
|
$_SESSION['my_url'] = $a->getbaseUrl() . '/profile/' . $user_record['nickname'];
|
|
|
|
$_SESSION['my_address'] = $user_record['nickname'] . '@' . substr($a->getbaseUrl(), strpos($a->getbaseUrl(), '://') + 3);
|
|
|
|
$_SESSION['addr'] = defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0');
|
|
|
|
|
|
|
|
$a->user = $user_record;
|
|
|
|
|
|
|
|
if ($interactive) {
|
2018-10-21 07:53:47 +02:00
|
|
|
if ($a->user['login_date'] <= DBA::NULL_DATETIME) {
|
2018-10-19 23:55:11 +02:00
|
|
|
$_SESSION['return_path'] = 'profile_photo/new';
|
2018-10-17 14:19:58 +02:00
|
|
|
$a->module = 'profile_photo';
|
|
|
|
info(L10n::t("Welcome ") . $a->user['username'] . EOL);
|
|
|
|
info(L10n::t('Please upload a profile photo.') . EOL);
|
|
|
|
} else {
|
|
|
|
info(L10n::t("Welcome back ") . $a->user['username'] . EOL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$member_since = strtotime($a->user['register_date']);
|
|
|
|
if (time() < ($member_since + ( 60 * 60 * 24 * 14))) {
|
|
|
|
$_SESSION['new_member'] = true;
|
|
|
|
} else {
|
|
|
|
$_SESSION['new_member'] = false;
|
|
|
|
}
|
|
|
|
if (strlen($a->user['timezone'])) {
|
|
|
|
date_default_timezone_set($a->user['timezone']);
|
|
|
|
$a->timezone = $a->user['timezone'];
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:22:15 +01:00
|
|
|
$masterUid = $user_record['uid'];
|
2018-10-17 14:19:58 +02:00
|
|
|
|
2018-11-30 15:06:22 +01:00
|
|
|
if (!empty($_SESSION['submanage'])) {
|
2018-11-08 00:22:15 +01:00
|
|
|
$user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]);
|
2018-10-17 14:19:58 +02:00
|
|
|
if (DBA::isResult($user)) {
|
2018-11-08 00:22:15 +01:00
|
|
|
$masterUid = $user['uid'];
|
2018-10-17 14:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 00:22:15 +01:00
|
|
|
$a->identities = User::identities($masterUid);
|
2018-10-17 14:19:58 +02:00
|
|
|
|
|
|
|
if ($login_initial) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('auth_identities: ' . print_r($a->identities, true), Logger::DEBUG);
|
2018-10-17 14:19:58 +02:00
|
|
|
}
|
|
|
|
if ($login_refresh) {
|
2018-10-30 14:58:45 +01:00
|
|
|
Logger::log('auth_identities refresh: ' . print_r($a->identities, true), Logger::DEBUG);
|
2018-10-17 14:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]);
|
|
|
|
if (DBA::isResult($contact)) {
|
|
|
|
$a->contact = $contact;
|
|
|
|
$a->cid = $contact['id'];
|
|
|
|
$_SESSION['cid'] = $a->cid;
|
|
|
|
}
|
|
|
|
|
|
|
|
header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] . '"');
|
|
|
|
|
|
|
|
if ($login_initial || $login_refresh) {
|
|
|
|
DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], ['uid' => $_SESSION['uid']]);
|
|
|
|
|
|
|
|
// Set the login date for all identities of the user
|
|
|
|
DBA::update('user', ['login_date' => DateTimeFormat::utcNow()],
|
2018-11-08 00:22:15 +01:00
|
|
|
['parent-uid' => $masterUid, 'account_removed' => false]);
|
2018-10-17 14:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($login_initial) {
|
|
|
|
/*
|
|
|
|
* If the user specified to remember the authentication, then set a cookie
|
|
|
|
* that expires after one week (the default is when the browser is closed).
|
|
|
|
* The cookie will be renewed automatically.
|
|
|
|
* The week ensures that sessions will expire after some inactivity.
|
|
|
|
*/
|
|
|
|
if (!empty($_SESSION['remember'])) {
|
2018-10-29 22:20:46 +01:00
|
|
|
Logger::log('Injecting cookie for remembered user ' . $a->user['nickname']);
|
2018-10-17 21:30:41 +02:00
|
|
|
self::setCookie(604800, $user_record);
|
2018-10-17 14:19:58 +02:00
|
|
|
unset($_SESSION['remember']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($login_initial) {
|
2018-12-26 07:06:24 +01:00
|
|
|
Hook::callAll('logged_in', $a->user);
|
2018-10-17 14:19:58 +02:00
|
|
|
|
2018-10-19 23:55:11 +02:00
|
|
|
if (($a->module !== 'home') && isset($_SESSION['return_path'])) {
|
|
|
|
$a->internalRedirect($_SESSION['return_path']);
|
2018-10-17 14:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Kills the "Friendica" cookie and all session data
|
|
|
|
*/
|
2018-10-17 21:30:41 +02:00
|
|
|
public static function deleteSession()
|
2018-10-17 14:19:58 +02:00
|
|
|
{
|
2018-10-17 21:30:41 +02:00
|
|
|
self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
|
2018-10-17 14:19:58 +02:00
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|