4 * @file src/Module/Login.php
7 namespace Friendica\Module\Security;
9 use Friendica\BaseModule;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\Session;
14 use Friendica\Module\Register;
15 use Friendica\Util\Strings;
20 * @author Hypolite Petovan <hypolite@mrpetovan.com>
22 class Login extends BaseModule
24 public static function content(array $parameters = [])
27 DI::baseUrl()->redirect();
30 return self::form(Session::get('return_path'), intval(DI::config()->get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
33 public static function post(array $parameters = [])
35 $return_path = Session::get('return_path');
37 Session::set('return_path', $return_path);
41 empty($_POST['password'])
42 && (!empty($_POST['openid_url'])
43 || !empty($_POST['username']))
45 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
47 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
50 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
51 DI::auth()->withPassword(
53 trim($_POST['username']),
54 trim($_POST['password']),
55 !empty($_POST['remember'])
61 * Wrapper for adding a login box.
63 * @param string $return_path The path relative to the base the user should be sent
64 * back to after login completes
65 * @param bool $register If $register == true provide a registration link.
66 * This will most always depend on the value of config.register_policy.
67 * @param array $hiddens optional
69 * @return string Returns the complete html for inserting into the page
71 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
72 * @hooks 'login_hook' string $o
74 public static function form($return_path = null, $register = false, $hiddens = [])
78 $noid = DI::config()->get('system', 'no_openid');
81 Session::remove('openid_identity');
82 Session::remove('openid_attributes');
86 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
88 'title' => DI::l10n()->t('Create a New Account'),
89 'desc' => DI::l10n()->t('Register'),
90 'url' => self::getRegisterURL()
94 if (is_null($return_path)) {
95 $return_path = DI::args()->getQueryString();
99 $tpl = Renderer::getMarkupTemplate('logout.tpl');
101 DI::page()['htmlhead'] .= Renderer::replaceMacros(
102 Renderer::getMarkupTemplate('login_head.tpl'),
104 '$baseurl' => DI::baseUrl()->get(true)
108 $tpl = Renderer::getMarkupTemplate('login.tpl');
109 $_SESSION['return_path'] = $return_path;
112 if (!empty(Session::get('openid_identity'))) {
113 $openid_title = DI::l10n()->t('Your OpenID: ');
114 $openid_readonly = true;
115 $identity = Session::get('openid_identity');
116 $username_desc = DI::l10n()->t('Please enter your username and password to add the OpenID to your existing account.');
118 $openid_title = DI::l10n()->t('Or login using OpenID: ');
119 $openid_readonly = false;
124 $o .= Renderer::replaceMacros(
127 '$dest_url' => DI::baseUrl()->get(true) . '/login',
128 '$logout' => DI::l10n()->t('Logout'),
129 '$login' => DI::l10n()->t('Login'),
131 '$lname' => ['username', DI::l10n()->t('Nickname or Email: '), '', $username_desc],
132 '$lpassword' => ['password', DI::l10n()->t('Password: '), '', ''],
133 '$lremember' => ['remember', DI::l10n()->t('Remember me'), 0, ''],
136 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
138 '$hiddens' => $hiddens,
142 '$lostpass' => DI::l10n()->t('Forgot your password?'),
143 '$lostlink' => DI::l10n()->t('Password Reset'),
145 '$tostitle' => DI::l10n()->t('Website Terms of Service'),
146 '$toslink' => DI::l10n()->t('terms of service'),
148 '$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
149 '$privacylink' => DI::l10n()->t('privacy policy'),
153 Hook::callAll('login_hook', $o);
159 * Get the URL to the register page and add OpenID parameters to it
161 private static function getRegisterURL()
163 if (empty(Session::get('openid_identity'))) {
168 $attr = Session::get('openid_attributes', []);
170 if (is_array($attr) && count($attr)) {
171 foreach ($attr as $k => $v) {
172 if ($k === 'namePerson/friendly') {
173 $nick = Strings::escapeTags(trim($v));
175 if ($k === 'namePerson/first') {
176 $first = Strings::escapeTags(trim($v));
178 if ($k === 'namePerson') {
179 $args['username'] = Strings::escapeTags(trim($v));
181 if ($k === 'contact/email') {
182 $args['email'] = Strings::escapeTags(trim($v));
184 if ($k === 'media/image/aspect11') {
185 $photosq = bin2hex(trim($v));
187 if ($k === 'media/image/default') {
188 $photo = bin2hex(trim($v));
194 $args['nickname'] = $nick;
195 } elseif (!empty($first)) {
196 $args['nickname'] = $first;
199 if (!empty($photosq)) {
200 $args['photo'] = $photosq;
201 } elseif (!empty($photo)) {
202 $args['photo'] = $photo;
205 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
207 return 'register?' . http_build_query($args);