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\Renderer;
13 use Friendica\Core\Session;
15 use Friendica\Module\Register;
16 use Friendica\Util\Strings;
21 * @author Hypolite Petovan <hypolite@mrpetovan.com>
23 class Login extends BaseModule
25 public static function content(array $parameters = [])
28 DI::baseUrl()->redirect();
31 return self::form(Session::get('return_path'), intval(Config::get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
34 public static function post(array $parameters = [])
36 $return_path = Session::get('return_path');
38 Session::set('return_path', $return_path);
42 empty($_POST['password'])
43 && (!empty($_POST['openid_url'])
44 || !empty($_POST['username']))
46 $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
48 DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
51 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
52 DI::auth()->withPassword(
54 trim($_POST['username']),
55 trim($_POST['password']),
56 !empty($_POST['remember'])
62 * Wrapper for adding a login box.
64 * @param string $return_path The path relative to the base the user should be sent
65 * back to after login completes
66 * @param bool $register If $register == true provide a registration link.
67 * This will most always depend on the value of config.register_policy.
68 * @param array $hiddens optional
70 * @return string Returns the complete html for inserting into the page
72 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
73 * @hooks 'login_hook' string $o
75 public static function form($return_path = null, $register = false, $hiddens = [])
79 $noid = Config::get('system', 'no_openid');
82 Session::remove('openid_identity');
83 Session::remove('openid_attributes');
87 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
89 'title' => DI::l10n()->t('Create a New Account'),
90 'desc' => DI::l10n()->t('Register'),
91 'url' => self::getRegisterURL()
95 if (is_null($return_path)) {
96 $return_path = DI::args()->getQueryString();
100 $tpl = Renderer::getMarkupTemplate('logout.tpl');
102 DI::page()['htmlhead'] .= Renderer::replaceMacros(
103 Renderer::getMarkupTemplate('login_head.tpl'),
105 '$baseurl' => DI::baseUrl()->get(true)
109 $tpl = Renderer::getMarkupTemplate('login.tpl');
110 $_SESSION['return_path'] = $return_path;
113 if (!empty(Session::get('openid_identity'))) {
114 $openid_title = DI::l10n()->t('Your OpenID: ');
115 $openid_readonly = true;
116 $identity = Session::get('openid_identity');
117 $username_desc = DI::l10n()->t('Please enter your username and password to add the OpenID to your existing account.');
119 $openid_title = DI::l10n()->t('Or login using OpenID: ');
120 $openid_readonly = false;
125 $o .= Renderer::replaceMacros(
128 '$dest_url' => DI::baseUrl()->get(true) . '/login',
129 '$logout' => DI::l10n()->t('Logout'),
130 '$login' => DI::l10n()->t('Login'),
132 '$lname' => ['username', DI::l10n()->t('Nickname or Email: '), '', $username_desc],
133 '$lpassword' => ['password', DI::l10n()->t('Password: '), '', ''],
134 '$lremember' => ['remember', DI::l10n()->t('Remember me'), 0, ''],
137 '$lopenid' => ['openid_url', $openid_title, $identity, '', $openid_readonly],
139 '$hiddens' => $hiddens,
143 '$lostpass' => DI::l10n()->t('Forgot your password?'),
144 '$lostlink' => DI::l10n()->t('Password Reset'),
146 '$tostitle' => DI::l10n()->t('Website Terms of Service'),
147 '$toslink' => DI::l10n()->t('terms of service'),
149 '$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
150 '$privacylink' => DI::l10n()->t('privacy policy'),
154 Hook::callAll('login_hook', $o);
160 * Get the URL to the register page and add OpenID parameters to it
162 private static function getRegisterURL()
164 if (empty(Session::get('openid_identity'))) {
169 $attr = Session::get('openid_attributes', []);
171 if (is_array($attr) && count($attr)) {
172 foreach ($attr as $k => $v) {
173 if ($k === 'namePerson/friendly') {
174 $nick = Strings::escapeTags(trim($v));
176 if ($k === 'namePerson/first') {
177 $first = Strings::escapeTags(trim($v));
179 if ($k === 'namePerson') {
180 $args['username'] = Strings::escapeTags(trim($v));
182 if ($k === 'contact/email') {
183 $args['email'] = Strings::escapeTags(trim($v));
185 if ($k === 'media/image/aspect11') {
186 $photosq = bin2hex(trim($v));
188 if ($k === 'media/image/default') {
189 $photo = bin2hex(trim($v));
195 $args['nickname'] = $nick;
196 } elseif (!empty($first)) {
197 $args['nickname'] = $first;
200 if (!empty($photosq)) {
201 $args['photo'] = $photosq;
202 } elseif (!empty($photo)) {
203 $args['photo'] = $photo;
206 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
208 return 'register?' . http_build_query($args);