3 * @copyright Copyright (C) 2010-2022, 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;
34 class Login extends BaseModule
36 protected function content(array $request = []): string
38 $return_path = $_REQUEST['return_path'] ?? '' ;
41 DI::baseUrl()->redirect($return_path);
42 } elseif (!empty($return_path)) {
43 Session::set('return_path', $return_path);
46 return self::form(Session::get('return_path'), intval(DI::config()->get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
49 protected function post(array $request = [])
51 $return_path = Session::get('return_path');
53 Session::set('return_path', $return_path);
57 empty($_POST['password'])
58 && (!empty($_POST['openid_url'])
59 || !empty($_POST['username']))
61 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
63 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
66 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
67 DI::auth()->withPassword(
69 trim($_POST['username']),
70 trim($_POST['password']),
71 !empty($_POST['remember'])
77 * Wrapper for adding a login box.
79 * @param string $return_path The path relative to the base the user should be sent
80 * back to after login completes
81 * @param bool $register If $register == true provide a registration link.
82 * This will most always depend on the value of config.register_policy.
83 * @param array $hiddens optional
85 * @return string Returns the complete html for inserting into the page
87 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
88 * @hooks 'login_hook' string $o
90 public static function form($return_path = null, $register = false, $hiddens = [])
94 $noid = DI::config()->get('system', 'no_openid');
97 Session::remove('openid_identity');
98 Session::remove('openid_attributes');
102 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
104 'title' => DI::l10n()->t('Create a New Account'),
105 'desc' => DI::l10n()->t('Register'),
106 'url' => self::getRegisterURL()
110 if (is_null($return_path)) {
111 $return_path = DI::args()->getQueryString();
115 $tpl = Renderer::getMarkupTemplate('logout.tpl');
117 DI::page()['htmlhead'] .= Renderer::replaceMacros(
118 Renderer::getMarkupTemplate('login_head.tpl'),
120 '$baseurl' => DI::baseUrl()->get(true)
124 $tpl = Renderer::getMarkupTemplate('login.tpl');
125 $_SESSION['return_path'] = $return_path;
128 if (!empty(Session::get('openid_identity'))) {
129 $openid_title = DI::l10n()->t('Your OpenID: ');
130 $openid_readonly = true;
131 $identity = Session::get('openid_identity');
132 $username_desc = DI::l10n()->t('Please enter your username and password to add the OpenID to your existing account.');
134 $openid_title = DI::l10n()->t('Or login using OpenID: ');
135 $openid_readonly = false;
140 $o .= Renderer::replaceMacros(
143 '$dest_url' => DI::baseUrl()->get(true) . '/login',
144 '$logout' => DI::l10n()->t('Logout'),
145 '$login' => DI::l10n()->t('Login'),
147 '$lname' => ['username', DI::l10n()->t('Nickname or Email: '), '', $username_desc],
148 '$lpassword' => ['password', DI::l10n()->t('Password: '), '', ''],
149 '$lremember' => ['remember', DI::l10n()->t('Remember me'), 0, ''],
152 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
154 '$hiddens' => $hiddens,
158 '$lostpass' => DI::l10n()->t('Forgot your password?'),
159 '$lostlink' => DI::l10n()->t('Password Reset'),
161 '$tostitle' => DI::l10n()->t('Website Terms of Service'),
162 '$toslink' => DI::l10n()->t('terms of service'),
164 '$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
165 '$privacylink' => DI::l10n()->t('privacy policy'),
169 Hook::callAll('login_hook', $o);
175 * Get the URL to the register page and add OpenID parameters to it
177 private static function getRegisterURL()
179 if (empty(Session::get('openid_identity'))) {
184 $attr = Session::get('openid_attributes', []);
186 if (is_array($attr) && count($attr)) {
187 foreach ($attr as $k => $v) {
188 if ($k === 'namePerson/friendly') {
191 if ($k === 'namePerson/first') {
194 if ($k === 'namePerson') {
195 $args['username'] = trim($v);
197 if ($k === 'contact/email') {
198 $args['email'] = trim($v);
200 if ($k === 'media/image/aspect11') {
201 $photosq = bin2hex(trim($v));
203 if ($k === 'media/image/default') {
204 $photo = bin2hex(trim($v));
210 $args['nickname'] = $nick;
211 } elseif (!empty($first)) {
212 $args['nickname'] = $first;
215 if (!empty($photosq)) {
216 $args['photo'] = $photosq;
217 } elseif (!empty($photo)) {
218 $args['photo'] = $photo;
221 $args['openid_url'] = trim(Session::get('openid_identity'));
223 return 'register?' . http_build_query($args);