2018-01-11 01:06:00 +01:00
|
|
|
<?php
|
2019-10-11 01:21:41 +02:00
|
|
|
|
2018-01-21 19:33:59 +01:00
|
|
|
/**
|
|
|
|
* @file src/Module/Login.php
|
|
|
|
*/
|
2019-10-11 01:21:41 +02:00
|
|
|
|
2019-12-27 22:19:28 +01:00
|
|
|
namespace Friendica\Module\Security;
|
2018-01-11 01:06:00 +01:00
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Core\Config;
|
2018-12-26 07:06:24 +01:00
|
|
|
use Friendica\Core\Hook;
|
2018-01-21 19:33:59 +01:00
|
|
|
use Friendica\Core\L10n;
|
2018-10-31 15:35:50 +01:00
|
|
|
use Friendica\Core\Renderer;
|
2019-05-13 06:55:26 +02:00
|
|
|
use Friendica\Core\Session;
|
2019-12-15 22:34:11 +01:00
|
|
|
use Friendica\DI;
|
2019-12-27 22:19:28 +01:00
|
|
|
use Friendica\Module\Register;
|
2018-11-08 16:14:37 +01:00
|
|
|
use Friendica\Util\Strings;
|
2018-01-11 01:06:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Login module
|
|
|
|
*
|
2018-09-16 01:28:38 +02:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-01-11 01:06:00 +01:00
|
|
|
*/
|
|
|
|
class Login extends BaseModule
|
|
|
|
{
|
2019-11-05 22:48:54 +01:00
|
|
|
public static function content(array $parameters = [])
|
2018-01-11 01:06:00 +01:00
|
|
|
{
|
|
|
|
if (local_user()) {
|
2019-12-16 00:28:31 +01:00
|
|
|
DI::baseUrl()->redirect();
|
2018-01-11 01:06:00 +01:00
|
|
|
}
|
|
|
|
|
2019-05-26 22:15:38 +02: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
|
|
|
}
|
|
|
|
|
2019-11-05 22:48:54 +01:00
|
|
|
public static function post(array $parameters = [])
|
2018-01-11 01:06:00 +01:00
|
|
|
{
|
2019-05-26 22:15:38 +02:00
|
|
|
$return_path = Session::get('return_path');
|
2019-12-03 22:29:37 +01:00
|
|
|
Session::clear();
|
2019-05-26 22:15:38 +02:00
|
|
|
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 (
|
2018-02-09 06:08:01 +01:00
|
|
|
empty($_POST['password'])
|
2019-10-11 01:21:41 +02:00
|
|
|
&& (!empty($_POST['openid_url'])
|
|
|
|
|| !empty($_POST['username']))
|
2018-01-11 01:06:00 +01:00
|
|
|
) {
|
2019-10-15 15:20:32 +02:00
|
|
|
$openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
|
2018-01-11 01:06:00 +01:00
|
|
|
|
2019-12-15 23:28:01 +01:00
|
|
|
DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
|
2018-02-09 06:08:01 +01:00
|
|
|
}
|
2018-01-11 01:06:00 +01:00
|
|
|
|
2018-11-30 15:06:22 +01:00
|
|
|
if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
|
2019-12-15 23:28:01 +01:00
|
|
|
DI::auth()->withPassword(
|
2019-12-15 22:34:11 +01:00
|
|
|
DI::app(),
|
2018-02-09 06:08:01 +01:00
|
|
|
trim($_POST['username']),
|
|
|
|
trim($_POST['password']),
|
2019-12-03 22:29:37 +01:00
|
|
|
!empty($_POST['remember'])
|
2018-02-09 06:08:01 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
2018-10-19 23:55:11 +02:00
|
|
|
public static function form($return_path = null, $register = false, $hiddens = [])
|
2018-01-11 01:06:00 +01:00
|
|
|
{
|
2019-12-15 22:34:11 +01:00
|
|
|
$a = DI::app();
|
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;
|
2019-12-15 23:44:33 +01:00
|
|
|
if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
|
2018-01-15 14:05:12 +01:00
|
|
|
$reg = [
|
2018-01-22 15:54:13 +01:00
|
|
|
'title' => L10n::t('Create a New Account'),
|
2019-10-24 22:23:26 +02:00
|
|
|
'desc' => L10n::t('Register'),
|
|
|
|
'url' => self::getRegisterURL()
|
2018-01-15 14:05:12 +01:00
|
|
|
];
|
2018-01-11 01:06:00 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 23:55:11 +02:00
|
|
|
if (is_null($return_path)) {
|
|
|
|
$return_path = $a->query_string;
|
2018-01-11 01:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (local_user()) {
|
2018-10-31 15:44:06 +01:00
|
|
|
$tpl = Renderer::getMarkupTemplate('logout.tpl');
|
2018-01-11 01:06:00 +01:00
|
|
|
} else {
|
2018-10-31 15:35:50 +01:00
|
|
|
$a->page['htmlhead'] .= Renderer::replaceMacros(
|
2018-10-31 15:44:06 +01:00
|
|
|
Renderer::getMarkupTemplate('login_head.tpl'),
|
2018-01-11 01:06:00 +01:00
|
|
|
[
|
2018-10-09 19:58:58 +02:00
|
|
|
'$baseurl' => $a->getBaseURL(true)
|
2018-01-11 01:06:00 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-10-31 15:44:06 +01:00
|
|
|
$tpl = Renderer::getMarkupTemplate('login.tpl');
|
2018-10-19 23:55:11 +02:00
|
|
|
$_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 = '';
|
|
|
|
}
|
|
|
|
|
2018-10-31 15:35:50 +01:00
|
|
|
$o .= Renderer::replaceMacros(
|
2018-01-11 01:06:00 +01:00
|
|
|
$tpl,
|
|
|
|
[
|
2019-12-15 22:34:11 +01:00
|
|
|
'$dest_url' => DI::app()->getBaseURL(true) . '/login',
|
2018-01-22 15:54:13 +01:00
|
|
|
'$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],
|
2018-01-22 15:54:13 +01:00
|
|
|
'$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,
|
|
|
|
|
2018-01-22 15:54:13 +01:00
|
|
|
'$lostpass' => L10n::t('Forgot your password?'),
|
|
|
|
'$lostlink' => L10n::t('Password Reset'),
|
2018-01-11 01:06:00 +01:00
|
|
|
|
2018-01-22 15:54:13 +01:00
|
|
|
'$tostitle' => L10n::t('Website Terms of Service'),
|
|
|
|
'$toslink' => L10n::t('terms of service'),
|
2018-01-11 01:06:00 +01:00
|
|
|
|
2018-01-22 15:54:13 +01:00
|
|
|
'$privacytitle' => L10n::t('Website Privacy Policy'),
|
|
|
|
'$privacylink' => L10n::t('privacy policy'),
|
2018-01-11 01:06:00 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-12-26 07:06:24 +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
|
|
|
}
|