3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Security;
24 use Friendica\BaseModule;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
29 use Friendica\Module\Register;
30 use Friendica\Util\Strings;
35 class Login extends BaseModule
37 public static function content(array $parameters = [])
39 $return_path = $_REQUEST['return_path'] ?? '' ;
42 DI::baseUrl()->redirect($return_path);
43 } elseif (!empty($return_path)) {
44 Session::set('return_path', $return_path);
47 return self::form(Session::get('return_path'), intval(DI::config()->get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
50 public static function post(array $parameters = [])
52 $return_path = Session::get('return_path');
54 Session::set('return_path', $return_path);
58 empty($_POST['password'])
59 && (!empty($_POST['openid_url'])
60 || !empty($_POST['username']))
62 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
64 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
67 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
68 DI::auth()->withPassword(
70 trim($_POST['username']),
71 trim($_POST['password']),
72 !empty($_POST['remember'])
78 * Wrapper for adding a login box.
80 * @param string $return_path The path relative to the base the user should be sent
81 * back to after login completes
82 * @param bool $register If $register == true provide a registration link.
83 * This will most always depend on the value of config.register_policy.
84 * @param array $hiddens optional
86 * @return string Returns the complete html for inserting into the page
88 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
89 * @hooks 'login_hook' string $o
91 public static function form($return_path = null, $register = false, $hiddens = [])
95 $noid = DI::config()->get('system', 'no_openid');
98 Session::remove('openid_identity');
99 Session::remove('openid_attributes');
103 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
105 'title' => DI::l10n()->t('Create a New Account'),
106 'desc' => DI::l10n()->t('Register'),
107 'url' => self::getRegisterURL()
111 if (is_null($return_path)) {
112 $return_path = DI::args()->getQueryString();
116 $tpl = Renderer::getMarkupTemplate('logout.tpl');
118 DI::page()['htmlhead'] .= Renderer::replaceMacros(
119 Renderer::getMarkupTemplate('login_head.tpl'),
121 '$baseurl' => DI::baseUrl()->get(true)
125 $tpl = Renderer::getMarkupTemplate('login.tpl');
126 $_SESSION['return_path'] = $return_path;
129 if (!empty(Session::get('openid_identity'))) {
130 $openid_title = DI::l10n()->t('Your OpenID: ');
131 $openid_readonly = true;
132 $identity = Session::get('openid_identity');
133 $username_desc = DI::l10n()->t('Please enter your username and password to add the OpenID to your existing account.');
135 $openid_title = DI::l10n()->t('Or login using OpenID: ');
136 $openid_readonly = false;
141 $o .= Renderer::replaceMacros(
144 '$dest_url' => DI::baseUrl()->get(true) . '/login',
145 '$logout' => DI::l10n()->t('Logout'),
146 '$login' => DI::l10n()->t('Login'),
148 '$lname' => ['username', DI::l10n()->t('Nickname or Email: '), '', $username_desc],
149 '$lpassword' => ['password', DI::l10n()->t('Password: '), '', ''],
150 '$lremember' => ['remember', DI::l10n()->t('Remember me'), 0, ''],
153 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
155 '$hiddens' => $hiddens,
159 '$lostpass' => DI::l10n()->t('Forgot your password?'),
160 '$lostlink' => DI::l10n()->t('Password Reset'),
162 '$tostitle' => DI::l10n()->t('Website Terms of Service'),
163 '$toslink' => DI::l10n()->t('terms of service'),
165 '$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
166 '$privacylink' => DI::l10n()->t('privacy policy'),
170 Hook::callAll('login_hook', $o);
176 * Get the URL to the register page and add OpenID parameters to it
178 private static function getRegisterURL()
180 if (empty(Session::get('openid_identity'))) {
185 $attr = Session::get('openid_attributes', []);
187 if (is_array($attr) && count($attr)) {
188 foreach ($attr as $k => $v) {
189 if ($k === 'namePerson/friendly') {
190 $nick = Strings::escapeTags(trim($v));
192 if ($k === 'namePerson/first') {
193 $first = Strings::escapeTags(trim($v));
195 if ($k === 'namePerson') {
196 $args['username'] = Strings::escapeTags(trim($v));
198 if ($k === 'contact/email') {
199 $args['email'] = Strings::escapeTags(trim($v));
201 if ($k === 'media/image/aspect11') {
202 $photosq = bin2hex(trim($v));
204 if ($k === 'media/image/default') {
205 $photo = bin2hex(trim($v));
211 $args['nickname'] = $nick;
212 } elseif (!empty($first)) {
213 $args['nickname'] = $first;
216 if (!empty($photosq)) {
217 $args['photo'] = $photosq;
218 } elseif (!empty($photo)) {
219 $args['photo'] = $photo;
222 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
224 return 'register?' . http_build_query($args);