4 * @file src/Module/Login.php
7 namespace Friendica\Module\Security;
9 use Friendica\BaseModule;
10 use Friendica\App\Authentication;
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\Session;
17 use Friendica\Module\Register;
18 use Friendica\Util\Strings;
23 * @author Hypolite Petovan <hypolite@mrpetovan.com>
25 class Login extends BaseModule
27 public static function content(array $parameters = [])
32 $a->internalRedirect();
35 return self::form(Session::get('return_path'), intval(Config::get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
38 public static function post(array $parameters = [])
40 $return_path = Session::get('return_path');
42 Session::set('return_path', $return_path);
46 empty($_POST['password'])
47 && (!empty($_POST['openid_url'])
48 || !empty($_POST['username']))
50 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
52 /** @var Authentication $authentication */
53 $authentication = self::getClass(Authentication::class);
54 $authentication->withOpenId($openid_url, !empty($_POST['remember']));
57 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
58 /** @var Authentication $authentication */
59 $authentication = self::getClass(Authentication::class);
60 $authentication->withPassword(
62 trim($_POST['username']),
63 trim($_POST['password']),
64 !empty($_POST['remember'])
70 * @brief Wrapper for adding a login box.
72 * @param string $return_path The path relative to the base the user should be sent
73 * back to after login completes
74 * @param bool $register If $register == true provide a registration link.
75 * This will most always depend on the value of config.register_policy.
76 * @param array $hiddens optional
78 * @return string Returns the complete html for inserting into the page
80 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
81 * @hooks 'login_hook' string $o
83 public static function form($return_path = null, $register = false, $hiddens = [])
88 $noid = Config::get('system', 'no_openid');
91 Session::remove('openid_identity');
92 Session::remove('openid_attributes');
96 if ($register && intval($a->getConfig()->get('config', 'register_policy')) !== Register::CLOSED) {
98 'title' => L10n::t('Create a New Account'),
99 'desc' => L10n::t('Register'),
100 'url' => self::getRegisterURL()
104 if (is_null($return_path)) {
105 $return_path = $a->query_string;
109 $tpl = Renderer::getMarkupTemplate('logout.tpl');
111 $a->page['htmlhead'] .= Renderer::replaceMacros(
112 Renderer::getMarkupTemplate('login_head.tpl'),
114 '$baseurl' => $a->getBaseURL(true)
118 $tpl = Renderer::getMarkupTemplate('login.tpl');
119 $_SESSION['return_path'] = $return_path;
122 if (!empty(Session::get('openid_identity'))) {
123 $openid_title = L10n::t('Your OpenID: ');
124 $openid_readonly = true;
125 $identity = Session::get('openid_identity');
126 $username_desc = L10n::t('Please enter your username and password to add the OpenID to your existing account.');
128 $openid_title = L10n::t('Or login using OpenID: ');
129 $openid_readonly = false;
134 $o .= Renderer::replaceMacros(
137 '$dest_url' => DI::app()->getBaseURL(true) . '/login',
138 '$logout' => L10n::t('Logout'),
139 '$login' => L10n::t('Login'),
141 '$lname' => ['username', L10n::t('Nickname or Email: '), '', $username_desc],
142 '$lpassword' => ['password', L10n::t('Password: '), '', ''],
143 '$lremember' => ['remember', L10n::t('Remember me'), 0, ''],
146 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
148 '$hiddens' => $hiddens,
152 '$lostpass' => L10n::t('Forgot your password?'),
153 '$lostlink' => L10n::t('Password Reset'),
155 '$tostitle' => L10n::t('Website Terms of Service'),
156 '$toslink' => L10n::t('terms of service'),
158 '$privacytitle' => L10n::t('Website Privacy Policy'),
159 '$privacylink' => L10n::t('privacy policy'),
163 Hook::callAll('login_hook', $o);
169 * Get the URL to the register page and add OpenID parameters to it
171 private static function getRegisterURL()
173 if (empty(Session::get('openid_identity'))) {
178 $attr = Session::get('openid_attributes', []);
180 if (is_array($attr) && count($attr)) {
181 foreach ($attr as $k => $v) {
182 if ($k === 'namePerson/friendly') {
183 $nick = Strings::escapeTags(trim($v));
185 if ($k === 'namePerson/first') {
186 $first = Strings::escapeTags(trim($v));
188 if ($k === 'namePerson') {
189 $args['username'] = Strings::escapeTags(trim($v));
191 if ($k === 'contact/email') {
192 $args['email'] = Strings::escapeTags(trim($v));
194 if ($k === 'media/image/aspect11') {
195 $photosq = bin2hex(trim($v));
197 if ($k === 'media/image/default') {
198 $photo = bin2hex(trim($v));
204 $args['nickname'] = $nick;
205 } elseif (!empty($first)) {
206 $args['nickname'] = $first;
209 if (!empty($photosq)) {
210 $args['photo'] = $photosq;
211 } elseif (!empty($photo)) {
212 $args['photo'] = $photo;
215 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
217 return 'register?' . http_build_query($args);