4 * @file src/Module/Login.php
7 namespace Friendica\Module\Security;
9 use Friendica\BaseModule;
10 use Friendica\Core\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\Session;
16 use Friendica\Module\Register;
17 use Friendica\Util\Strings;
22 * @author Hypolite Petovan <hypolite@mrpetovan.com>
24 class Login extends BaseModule
26 public static function content(array $parameters = [])
29 DI::baseUrl()->redirect();
32 return self::form(Session::get('return_path'), intval(Config::get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
35 public static function post(array $parameters = [])
37 $return_path = Session::get('return_path');
39 Session::set('return_path', $return_path);
43 empty($_POST['password'])
44 && (!empty($_POST['openid_url'])
45 || !empty($_POST['username']))
47 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
49 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
52 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
53 DI::auth()->withPassword(
55 trim($_POST['username']),
56 trim($_POST['password']),
57 !empty($_POST['remember'])
63 * @brief Wrapper for adding a login box.
65 * @param string $return_path The path relative to the base the user should be sent
66 * back to after login completes
67 * @param bool $register If $register == true provide a registration link.
68 * This will most always depend on the value of config.register_policy.
69 * @param array $hiddens optional
71 * @return string Returns the complete html for inserting into the page
73 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
74 * @hooks 'login_hook' string $o
76 public static function form($return_path = null, $register = false, $hiddens = [])
81 $noid = Config::get('system', 'no_openid');
84 Session::remove('openid_identity');
85 Session::remove('openid_attributes');
89 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
91 'title' => L10n::t('Create a New Account'),
92 'desc' => L10n::t('Register'),
93 'url' => self::getRegisterURL()
97 if (is_null($return_path)) {
98 $return_path = DI::args()->getQueryString();
102 $tpl = Renderer::getMarkupTemplate('logout.tpl');
104 $a->page['htmlhead'] .= Renderer::replaceMacros(
105 Renderer::getMarkupTemplate('login_head.tpl'),
107 '$baseurl' => DI::baseUrl()->get(true)
111 $tpl = Renderer::getMarkupTemplate('login.tpl');
112 $_SESSION['return_path'] = $return_path;
115 if (!empty(Session::get('openid_identity'))) {
116 $openid_title = L10n::t('Your OpenID: ');
117 $openid_readonly = true;
118 $identity = Session::get('openid_identity');
119 $username_desc = L10n::t('Please enter your username and password to add the OpenID to your existing account.');
121 $openid_title = L10n::t('Or login using OpenID: ');
122 $openid_readonly = false;
127 $o .= Renderer::replaceMacros(
130 '$dest_url' => DI::baseUrl()->get(true) . '/login',
131 '$logout' => L10n::t('Logout'),
132 '$login' => L10n::t('Login'),
134 '$lname' => ['username', L10n::t('Nickname or Email: '), '', $username_desc],
135 '$lpassword' => ['password', L10n::t('Password: '), '', ''],
136 '$lremember' => ['remember', L10n::t('Remember me'), 0, ''],
139 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
141 '$hiddens' => $hiddens,
145 '$lostpass' => L10n::t('Forgot your password?'),
146 '$lostlink' => L10n::t('Password Reset'),
148 '$tostitle' => L10n::t('Website Terms of Service'),
149 '$toslink' => L10n::t('terms of service'),
151 '$privacytitle' => L10n::t('Website Privacy Policy'),
152 '$privacylink' => L10n::t('privacy policy'),
156 Hook::callAll('login_hook', $o);
162 * Get the URL to the register page and add OpenID parameters to it
164 private static function getRegisterURL()
166 if (empty(Session::get('openid_identity'))) {
171 $attr = Session::get('openid_attributes', []);
173 if (is_array($attr) && count($attr)) {
174 foreach ($attr as $k => $v) {
175 if ($k === 'namePerson/friendly') {
176 $nick = Strings::escapeTags(trim($v));
178 if ($k === 'namePerson/first') {
179 $first = Strings::escapeTags(trim($v));
181 if ($k === 'namePerson') {
182 $args['username'] = Strings::escapeTags(trim($v));
184 if ($k === 'contact/email') {
185 $args['email'] = Strings::escapeTags(trim($v));
187 if ($k === 'media/image/aspect11') {
188 $photosq = bin2hex(trim($v));
190 if ($k === 'media/image/default') {
191 $photo = bin2hex(trim($v));
197 $args['nickname'] = $nick;
198 } elseif (!empty($first)) {
199 $args['nickname'] = $first;
202 if (!empty($photosq)) {
203 $args['photo'] = $photosq;
204 } elseif (!empty($photo)) {
205 $args['photo'] = $photo;
208 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
210 return 'register?' . http_build_query($args);