3 * @copyright Copyright (C) 2020, Friendica
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 = [])
40 DI::baseUrl()->redirect();
43 return self::form(Session::get('return_path'), intval(DI::config()->get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
46 public static function post(array $parameters = [])
48 $return_path = Session::get('return_path');
50 Session::set('return_path', $return_path);
54 empty($_POST['password'])
55 && (!empty($_POST['openid_url'])
56 || !empty($_POST['username']))
58 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
60 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
63 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
64 DI::auth()->withPassword(
66 trim($_POST['username']),
67 trim($_POST['password']),
68 !empty($_POST['remember'])
74 * Wrapper for adding a login box.
76 * @param string $return_path The path relative to the base the user should be sent
77 * back to after login completes
78 * @param bool $register If $register == true provide a registration link.
79 * This will most always depend on the value of config.register_policy.
80 * @param array $hiddens optional
82 * @return string Returns the complete html for inserting into the page
84 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
85 * @hooks 'login_hook' string $o
87 public static function form($return_path = null, $register = false, $hiddens = [])
91 $noid = DI::config()->get('system', 'no_openid');
94 Session::remove('openid_identity');
95 Session::remove('openid_attributes');
99 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
101 'title' => DI::l10n()->t('Create a New Account'),
102 'desc' => DI::l10n()->t('Register'),
103 'url' => self::getRegisterURL()
107 if (is_null($return_path)) {
108 $return_path = DI::args()->getQueryString();
112 $tpl = Renderer::getMarkupTemplate('logout.tpl');
114 DI::page()['htmlhead'] .= Renderer::replaceMacros(
115 Renderer::getMarkupTemplate('login_head.tpl'),
117 '$baseurl' => DI::baseUrl()->get(true)
121 $tpl = Renderer::getMarkupTemplate('login.tpl');
122 $_SESSION['return_path'] = $return_path;
125 if (!empty(Session::get('openid_identity'))) {
126 $openid_title = DI::l10n()->t('Your OpenID: ');
127 $openid_readonly = true;
128 $identity = Session::get('openid_identity');
129 $username_desc = DI::l10n()->t('Please enter your username and password to add the OpenID to your existing account.');
131 $openid_title = DI::l10n()->t('Or login using OpenID: ');
132 $openid_readonly = false;
137 $o .= Renderer::replaceMacros(
140 '$dest_url' => DI::baseUrl()->get(true) . '/login',
141 '$logout' => DI::l10n()->t('Logout'),
142 '$login' => DI::l10n()->t('Login'),
144 '$lname' => ['username', DI::l10n()->t('Nickname or Email: '), '', $username_desc],
145 '$lpassword' => ['password', DI::l10n()->t('Password: '), '', ''],
146 '$lremember' => ['remember', DI::l10n()->t('Remember me'), 0, ''],
149 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
151 '$hiddens' => $hiddens,
155 '$lostpass' => DI::l10n()->t('Forgot your password?'),
156 '$lostlink' => DI::l10n()->t('Password Reset'),
158 '$tostitle' => DI::l10n()->t('Website Terms of Service'),
159 '$toslink' => DI::l10n()->t('terms of service'),
161 '$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
162 '$privacylink' => DI::l10n()->t('privacy policy'),
166 Hook::callAll('login_hook', $o);
172 * Get the URL to the register page and add OpenID parameters to it
174 private static function getRegisterURL()
176 if (empty(Session::get('openid_identity'))) {
181 $attr = Session::get('openid_attributes', []);
183 if (is_array($attr) && count($attr)) {
184 foreach ($attr as $k => $v) {
185 if ($k === 'namePerson/friendly') {
186 $nick = Strings::escapeTags(trim($v));
188 if ($k === 'namePerson/first') {
189 $first = Strings::escapeTags(trim($v));
191 if ($k === 'namePerson') {
192 $args['username'] = Strings::escapeTags(trim($v));
194 if ($k === 'contact/email') {
195 $args['email'] = Strings::escapeTags(trim($v));
197 if ($k === 'media/image/aspect11') {
198 $photosq = bin2hex(trim($v));
200 if ($k === 'media/image/default') {
201 $photo = bin2hex(trim($v));
207 $args['nickname'] = $nick;
208 } elseif (!empty($first)) {
209 $args['nickname'] = $first;
212 if (!empty($photosq)) {
213 $args['photo'] = $photosq;
214 } elseif (!empty($photo)) {
215 $args['photo'] = $photo;
218 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
220 return 'register?' . http_build_query($args);