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 = [])
31 $a->internalRedirect();
34 return self::form(Session::get('return_path'), intval(Config::get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
37 public static function post(array $parameters = [])
39 $return_path = Session::get('return_path');
41 Session::set('return_path', $return_path);
45 empty($_POST['password'])
46 && (!empty($_POST['openid_url'])
47 || !empty($_POST['username']))
49 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
51 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
54 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
55 DI::auth()->withPassword(
57 trim($_POST['username']),
58 trim($_POST['password']),
59 !empty($_POST['remember'])
65 * @brief Wrapper for adding a login box.
67 * @param string $return_path The path relative to the base the user should be sent
68 * back to after login completes
69 * @param bool $register If $register == true provide a registration link.
70 * This will most always depend on the value of config.register_policy.
71 * @param array $hiddens optional
73 * @return string Returns the complete html for inserting into the page
75 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
76 * @hooks 'login_hook' string $o
78 public static function form($return_path = null, $register = false, $hiddens = [])
83 $noid = Config::get('system', 'no_openid');
86 Session::remove('openid_identity');
87 Session::remove('openid_attributes');
91 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
93 'title' => L10n::t('Create a New Account'),
94 'desc' => L10n::t('Register'),
95 'url' => self::getRegisterURL()
99 if (is_null($return_path)) {
100 $return_path = $a->query_string;
104 $tpl = Renderer::getMarkupTemplate('logout.tpl');
106 $a->page['htmlhead'] .= Renderer::replaceMacros(
107 Renderer::getMarkupTemplate('login_head.tpl'),
109 '$baseurl' => $a->getBaseURL(true)
113 $tpl = Renderer::getMarkupTemplate('login.tpl');
114 $_SESSION['return_path'] = $return_path;
117 if (!empty(Session::get('openid_identity'))) {
118 $openid_title = L10n::t('Your OpenID: ');
119 $openid_readonly = true;
120 $identity = Session::get('openid_identity');
121 $username_desc = L10n::t('Please enter your username and password to add the OpenID to your existing account.');
123 $openid_title = L10n::t('Or login using OpenID: ');
124 $openid_readonly = false;
129 $o .= Renderer::replaceMacros(
132 '$dest_url' => DI::app()->getBaseURL(true) . '/login',
133 '$logout' => L10n::t('Logout'),
134 '$login' => L10n::t('Login'),
136 '$lname' => ['username', L10n::t('Nickname or Email: '), '', $username_desc],
137 '$lpassword' => ['password', L10n::t('Password: '), '', ''],
138 '$lremember' => ['remember', L10n::t('Remember me'), 0, ''],
141 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
143 '$hiddens' => $hiddens,
147 '$lostpass' => L10n::t('Forgot your password?'),
148 '$lostlink' => L10n::t('Password Reset'),
150 '$tostitle' => L10n::t('Website Terms of Service'),
151 '$toslink' => L10n::t('terms of service'),
153 '$privacytitle' => L10n::t('Website Privacy Policy'),
154 '$privacylink' => L10n::t('privacy policy'),
158 Hook::callAll('login_hook', $o);
164 * Get the URL to the register page and add OpenID parameters to it
166 private static function getRegisterURL()
168 if (empty(Session::get('openid_identity'))) {
173 $attr = Session::get('openid_attributes', []);
175 if (is_array($attr) && count($attr)) {
176 foreach ($attr as $k => $v) {
177 if ($k === 'namePerson/friendly') {
178 $nick = Strings::escapeTags(trim($v));
180 if ($k === 'namePerson/first') {
181 $first = Strings::escapeTags(trim($v));
183 if ($k === 'namePerson') {
184 $args['username'] = Strings::escapeTags(trim($v));
186 if ($k === 'contact/email') {
187 $args['email'] = Strings::escapeTags(trim($v));
189 if ($k === 'media/image/aspect11') {
190 $photosq = bin2hex(trim($v));
192 if ($k === 'media/image/default') {
193 $photo = bin2hex(trim($v));
199 $args['nickname'] = $nick;
200 } elseif (!empty($first)) {
201 $args['nickname'] = $first;
204 if (!empty($photosq)) {
205 $args['photo'] = $photosq;
206 } elseif (!empty($photo)) {
207 $args['photo'] = $photo;
210 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
212 return 'register?' . http_build_query($args);