friendica/src/Module/Security/Login.php

212 lines
5.5 KiB
PHP
Raw Normal View History

2018-01-11 01:06:00 +01:00
<?php
2018-01-21 19:33:59 +01:00
/**
* @file src/Module/Login.php
*/
namespace Friendica\Module\Security;
2018-01-11 01:06:00 +01:00
use Friendica\BaseModule;
use Friendica\Core\Config;
use Friendica\Core\Hook;
2018-01-21 19:33:59 +01:00
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\DI;
use Friendica\Module\Register;
use Friendica\Util\Strings;
2018-01-11 01:06:00 +01:00
/**
* Login module
*
* @author Hypolite Petovan <hypolite@mrpetovan.com>
2018-01-11 01:06:00 +01:00
*/
class Login extends BaseModule
{
public static function content(array $parameters = [])
2018-01-11 01:06:00 +01:00
{
if (local_user()) {
DI::baseUrl()->redirect();
2018-01-11 01:06:00 +01:00
}
return self::form(Session::get('return_path'), intval(Config::get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
2018-01-11 01:06:00 +01:00
}
public static function post(array $parameters = [])
2018-01-11 01:06:00 +01:00
{
$return_path = Session::get('return_path');
Session::clear();
Session::set('return_path', $return_path);
2019-01-13 19:03:13 +01:00
2018-01-11 01:06:00 +01:00
// OpenId Login
if (
empty($_POST['password'])
&& (!empty($_POST['openid_url'])
|| !empty($_POST['username']))
2018-01-11 01:06:00 +01:00
) {
$openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
2018-01-11 01:06:00 +01:00
DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
}
2018-01-11 01:06:00 +01:00
if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
DI::auth()->withPassword(
DI::app(),
trim($_POST['username']),
trim($_POST['password']),
!empty($_POST['remember'])
);
}
}
2018-01-11 01:06:00 +01:00
/**
* @brief Wrapper for adding a login box.
*
2019-01-06 22:06:53 +01:00
* @param string $return_path The path relative to the base the user should be sent
* back to after login completes
* @param bool $register If $register == true provide a registration link.
* This will most always depend on the value of config.register_policy.
* @param array $hiddens optional
2018-01-11 01:06:00 +01:00
*
* @return string Returns the complete html for inserting into the page
*
2019-01-06 22:06:53 +01:00
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
2018-01-11 01:06:00 +01:00
* @hooks 'login_hook' string $o
*/
public static function form($return_path = null, $register = false, $hiddens = [])
2018-01-11 01:06:00 +01:00
{
$o = '';
2019-10-24 22:23:26 +02:00
$noid = Config::get('system', 'no_openid');
if ($noid) {
Session::remove('openid_identity');
Session::remove('openid_attributes');
}
2018-01-11 01:06:00 +01:00
$reg = false;
if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
$reg = [
'title' => L10n::t('Create a New Account'),
2019-10-24 22:23:26 +02:00
'desc' => L10n::t('Register'),
'url' => self::getRegisterURL()
];
2018-01-11 01:06:00 +01:00
}
if (is_null($return_path)) {
$return_path = DI::args()->getQueryString();
2018-01-11 01:06:00 +01:00
}
if (local_user()) {
$tpl = Renderer::getMarkupTemplate('logout.tpl');
2018-01-11 01:06:00 +01:00
} else {
DI::page()['htmlhead'] .= Renderer::replaceMacros(
Renderer::getMarkupTemplate('login_head.tpl'),
2018-01-11 01:06:00 +01:00
[
'$baseurl' => DI::baseUrl()->get(true)
2018-01-11 01:06:00 +01:00
]
);
$tpl = Renderer::getMarkupTemplate('login.tpl');
$_SESSION['return_path'] = $return_path;
2018-01-11 01:06:00 +01:00
}
2019-10-24 22:23:26 +02:00
if (!empty(Session::get('openid_identity'))) {
$openid_title = L10n::t('Your OpenID: ');
$openid_readonly = true;
$identity = Session::get('openid_identity');
$username_desc = L10n::t('Please enter your username and password to add the OpenID to your existing account.');
} else {
$openid_title = L10n::t('Or login using OpenID: ');
$openid_readonly = false;
$identity = '';
$username_desc = '';
}
$o .= Renderer::replaceMacros(
2018-01-11 01:06:00 +01:00
$tpl,
[
'$dest_url' => DI::baseUrl()->get(true) . '/login',
'$logout' => L10n::t('Logout'),
'$login' => L10n::t('Login'),
2018-01-11 01:06:00 +01:00
2019-10-24 22:23:26 +02:00
'$lname' => ['username', L10n::t('Nickname or Email: '), '', $username_desc],
'$lpassword' => ['password', L10n::t('Password: '), '', ''],
'$lremember' => ['remember', L10n::t('Remember me'), 0, ''],
2018-01-11 01:06:00 +01:00
'$openid' => !$noid,
2019-10-24 22:23:26 +02:00
'$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
2018-01-11 01:06:00 +01:00
'$hiddens' => $hiddens,
'$register' => $reg,
'$lostpass' => L10n::t('Forgot your password?'),
'$lostlink' => L10n::t('Password Reset'),
2018-01-11 01:06:00 +01:00
'$tostitle' => L10n::t('Website Terms of Service'),
'$toslink' => L10n::t('terms of service'),
2018-01-11 01:06:00 +01:00
'$privacytitle' => L10n::t('Website Privacy Policy'),
'$privacylink' => L10n::t('privacy policy'),
2018-01-11 01:06:00 +01:00
]
);
Hook::callAll('login_hook', $o);
2018-01-11 01:06:00 +01:00
return $o;
}
2019-10-24 22:23:26 +02:00
/**
* Get the URL to the register page and add OpenID parameters to it
*/
private static function getRegisterURL()
{
if (empty(Session::get('openid_identity'))) {
return 'register';
}
2019-10-28 21:38:53 +01:00
$args = [];
2019-10-29 14:26:54 +01:00
$attr = Session::get('openid_attributes', []);
2019-10-24 22:23:26 +02:00
if (is_array($attr) && count($attr)) {
foreach ($attr as $k => $v) {
if ($k === 'namePerson/friendly') {
$nick = Strings::escapeTags(trim($v));
}
if ($k === 'namePerson/first') {
$first = Strings::escapeTags(trim($v));
}
if ($k === 'namePerson') {
2019-10-28 21:38:53 +01:00
$args['username'] = Strings::escapeTags(trim($v));
2019-10-24 22:23:26 +02:00
}
if ($k === 'contact/email') {
2019-10-28 21:38:53 +01:00
$args['email'] = Strings::escapeTags(trim($v));
2019-10-24 22:23:26 +02:00
}
if ($k === 'media/image/aspect11') {
$photosq = bin2hex(trim($v));
}
if ($k === 'media/image/default') {
$photo = bin2hex(trim($v));
}
}
}
if (!empty($nick)) {
2019-10-28 21:38:53 +01:00
$args['nickname'] = $nick;
2019-10-24 22:23:26 +02:00
} elseif (!empty($first)) {
2019-10-28 21:38:53 +01:00
$args['nickname'] = $first;
2019-10-24 22:23:26 +02:00
}
if (!empty($photosq)) {
2019-10-28 21:38:53 +01:00
$args['photo'] = $photosq;
2019-10-24 22:23:26 +02:00
} elseif (!empty($photo)) {
2019-10-28 21:38:53 +01:00
$args['photo'] = $photo;
2019-10-24 22:23:26 +02:00
}
2019-10-28 21:38:53 +01:00
$args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
2019-10-24 22:23:26 +02:00
2019-10-28 21:38:53 +01:00
return 'register?' . http_build_query($args);
2019-10-24 22:23:26 +02:00
}
2018-01-11 01:06:00 +01:00
}