X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCore%2FAuthentication.php;h=106ba2a6021ccd542325b4c4bff87d6c477818ae;hb=0615c8438abf3519156d2ba9c0bf378c08f44737;hp=3e53a5ae9a98953b4f559e26cae2620572ac4e8d;hpb=05be2db72516ac17dc6b9cccaa6fee458501df53;p=friendica.git diff --git a/src/Core/Authentication.php b/src/Core/Authentication.php index 3e53a5ae9a..106ba2a602 100644 --- a/src/Core/Authentication.php +++ b/src/Core/Authentication.php @@ -5,17 +5,14 @@ namespace Friendica\Core; -use Friendica\Core\Addon; use Friendica\BaseObject; -use Friendica\Core\Config; -use Friendica\Util\DateTimeFormat; use Friendica\Database\DBA; -use Friendica\Core\L10n; -use Friendica\Core\PConfig; +use Friendica\Model\User; +use Friendica\Util\DateTimeFormat; /** * Handle Authentification, Session and Cookies -*/ +*/ class Authentication extends BaseObject { /** @@ -24,8 +21,9 @@ class Authentication extends BaseObject * @param array $user Record from "user" table * * @return string Hashed data + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function cookie_hash($user) + public static function getCookieHashForUser($user) { return(hash("sha256", Config::get("system", "site_prvkey") . $user["prvkey"] . @@ -35,10 +33,11 @@ class Authentication extends BaseObject /** * @brief Set the "Friendica" cookie * - * @param int $time + * @param int $time * @param array $user Record from "user" table + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function new_cookie($time, $user = []) + public static function setCookie($time, $user = []) { if ($time != 0) { $time = $time + time(); @@ -46,7 +45,7 @@ class Authentication extends BaseObject if ($user) { $value = json_encode(["uid" => $user["uid"], - "hash" => self::cookie_hash($user), + "hash" => self::getCookieHashForUser($user), "ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')]); } else { $value = ""; @@ -58,14 +57,15 @@ class Authentication extends BaseObject /** * @brief Sets the provided user's authenticated session * - * @todo Should be moved to Friendica\Core\Session once it's created + * @todo Should be moved to Friendica\Core\Session once it's created * - * @param type $user_record - * @param type $login_initial - * @param type $interactive - * @param type $login_refresh + * @param array $user_record + * @param bool $login_initial + * @param bool $interactive + * @param bool $login_refresh + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function authenticate_success($user_record, $login_initial = false, $interactive = false, $login_refresh = false) + public static function setAuthenticatedSessionForUser($user_record, $login_initial = false, $interactive = false, $login_refresh = false) { $a = self::getApp(); @@ -81,8 +81,8 @@ class Authentication extends BaseObject $a->user = $user_record; if ($interactive) { - if ($a->user['login_date'] <= NULL_DATE) { - $_SESSION['return_url'] = 'profile_photo/new'; + if ($a->user['login_date'] <= DBA::NULL_DATETIME) { + $_SESSION['return_path'] = 'profile_photo/new'; $a->module = 'profile_photo'; info(L10n::t("Welcome ") . $a->user['username'] . EOL); info(L10n::t('Please upload a profile photo.') . EOL); @@ -102,61 +102,22 @@ class Authentication extends BaseObject $a->timezone = $a->user['timezone']; } - $master_record = $a->user; + $masterUid = $user_record['uid']; - if ((x($_SESSION, 'submanage')) && intval($_SESSION['submanage'])) { - $user = DBA::selectFirst('user', [], ['uid' => $_SESSION['submanage']]); + if (!empty($_SESSION['submanage'])) { + $user = DBA::selectFirst('user', ['uid'], ['uid' => $_SESSION['submanage']]); if (DBA::isResult($user)) { - $master_record = $user; - } - } - - if ($master_record['parent-uid'] == 0) { - // First add our own entry - $a->identities = [['uid' => $master_record['uid'], - 'username' => $master_record['username'], - 'nickname' => $master_record['nickname']]]; - - // Then add all the children - $r = DBA::select('user', ['uid', 'username', 'nickname'], - ['parent-uid' => $master_record['uid'], 'account_removed' => false]); - if (DBA::isResult($r)) { - $a->identities = array_merge($a->identities, DBA::toArray($r)); - } - } else { - // Just ensure that the array is always defined - $a->identities = []; - - // First entry is our parent - $r = DBA::select('user', ['uid', 'username', 'nickname'], - ['uid' => $master_record['parent-uid'], 'account_removed' => false]); - if (DBA::isResult($r)) { - $a->identities = DBA::toArray($r); - } - - // Then add all siblings - $r = DBA::select('user', ['uid', 'username', 'nickname'], - ['parent-uid' => $master_record['parent-uid'], 'account_removed' => false]); - if (DBA::isResult($r)) { - $a->identities = array_merge($a->identities, DBA::toArray($r)); + $masterUid = $user['uid']; } } - $r = DBA::p("SELECT `user`.`uid`, `user`.`username`, `user`.`nickname` - FROM `manage` - INNER JOIN `user` ON `manage`.`mid` = `user`.`uid` - WHERE `user`.`account_removed` = 0 AND `manage`.`uid` = ?", - $master_record['uid'] - ); - if (DBA::isResult($r)) { - $a->identities = array_merge($a->identities, DBA::toArray($r)); - } + $a->identities = User::identities($masterUid); if ($login_initial) { - logger('auth_identities: ' . print_r($a->identities, true), LOGGER_DEBUG); + Logger::log('auth_identities: ' . print_r($a->identities, true), Logger::DEBUG); } if ($login_refresh) { - logger('auth_identities refresh: ' . print_r($a->identities, true), LOGGER_DEBUG); + Logger::log('auth_identities refresh: ' . print_r($a->identities, true), Logger::DEBUG); } $contact = DBA::selectFirst('contact', [], ['uid' => $_SESSION['uid'], 'self' => true]); @@ -173,7 +134,7 @@ class Authentication extends BaseObject // Set the login date for all identities of the user DBA::update('user', ['login_date' => DateTimeFormat::utcNow()], - ['parent-uid' => $master_record['uid'], 'account_removed' => false]); + ['parent-uid' => $masterUid, 'account_removed' => false]); } if ($login_initial) { @@ -184,17 +145,17 @@ class Authentication extends BaseObject * The week ensures that sessions will expire after some inactivity. */ if (!empty($_SESSION['remember'])) { - logger('Injecting cookie for remembered user ' . $a->user['nickname']); - self::new_cookie(604800, $user_record); + Logger::log('Injecting cookie for remembered user ' . $a->user['nickname']); + self::setCookie(604800, $user_record); unset($_SESSION['remember']); } } if ($login_initial) { - Addon::callHooks('logged_in', $a->user); + Hook::callAll('logged_in', $a->user); - if (($a->module !== 'home') && isset($_SESSION['return_url'])) { - goaway($a->getbaseUrl() . '/' . $_SESSION['return_url']); + if (($a->module !== 'home') && isset($_SESSION['return_path'])) { + $a->internalRedirect($_SESSION['return_path']); } } } @@ -202,12 +163,11 @@ class Authentication extends BaseObject /** * @brief Kills the "Friendica" cookie and all session data */ - public static function nuke_session() + public static function deleteSession() { - self::new_cookie(-3600); // make sure cookie is deleted on browser close, as a security measure + self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure session_unset(); session_destroy(); } } -?>