]> git.mxchange.org Git - friendica.git/blob - src/Module/Security/Login.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git] / src / Module / Security / Login.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Security;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\DI;
29 use Friendica\Module\Register;
30 use Friendica\Util\Strings;
31
32 /**
33  * Login module
34  */
35 class Login extends BaseModule
36 {
37         public static function content(array $parameters = [])
38         {
39                 if (local_user()) {
40                         DI::baseUrl()->redirect();
41                 }
42
43                 return self::form(Session::get('return_path'), intval(DI::config()->get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED);
44         }
45
46         public static function post(array $parameters = [])
47         {
48                 $return_path = Session::get('return_path');
49                 Session::clear();
50                 Session::set('return_path', $return_path);
51
52                 // OpenId Login
53                 if (
54                         empty($_POST['password'])
55                         && (!empty($_POST['openid_url'])
56                                 || !empty($_POST['username']))
57                 ) {
58                         $openid_url = trim(($_POST['openid_url'] ?? '') ?: $_POST['username']);
59
60                         DI::auth()->withOpenId($openid_url, !empty($_POST['remember']));
61                 }
62
63                 if (!empty($_POST['auth-params']) && $_POST['auth-params'] === 'login') {
64                         DI::auth()->withPassword(
65                                 DI::app(),
66                                 trim($_POST['username']),
67                                 trim($_POST['password']),
68                                 !empty($_POST['remember'])
69                         );
70                 }
71         }
72
73         /**
74          * Wrapper for adding a login box.
75          *
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
81          *
82          * @return string Returns the complete html for inserting into the page
83          *
84          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
85          * @hooks 'login_hook' string $o
86          */
87         public static function form($return_path = null, $register = false, $hiddens = [])
88         {
89                 $o = '';
90
91                 $noid = DI::config()->get('system', 'no_openid');
92
93                 if ($noid) {
94                         Session::remove('openid_identity');
95                         Session::remove('openid_attributes');
96                 }
97
98                 $reg = false;
99                 if ($register && intval(DI::config()->get('config', 'register_policy')) !== Register::CLOSED) {
100                         $reg = [
101                                 'title' => DI::l10n()->t('Create a New Account'),
102                                 'desc' => DI::l10n()->t('Register'),
103                                 'url' => self::getRegisterURL()
104                         ];
105                 }
106
107                 if (is_null($return_path)) {
108                         $return_path = DI::args()->getQueryString();
109                 }
110
111                 if (local_user()) {
112                         $tpl = Renderer::getMarkupTemplate('logout.tpl');
113                 } else {
114                         DI::page()['htmlhead'] .= Renderer::replaceMacros(
115                                 Renderer::getMarkupTemplate('login_head.tpl'),
116                                 [
117                                         '$baseurl' => DI::baseUrl()->get(true)
118                                 ]
119                         );
120
121                         $tpl = Renderer::getMarkupTemplate('login.tpl');
122                         $_SESSION['return_path'] = $return_path;
123                 }
124
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.');
130                 } else {
131                         $openid_title = DI::l10n()->t('Or login using OpenID: ');
132                         $openid_readonly = false;
133                         $identity = '';
134                         $username_desc = '';
135                 }
136
137                 $o .= Renderer::replaceMacros(
138                         $tpl,
139                         [
140                                 '$dest_url'     => DI::baseUrl()->get(true) . '/login',
141                                 '$logout'       => DI::l10n()->t('Logout'),
142                                 '$login'        => DI::l10n()->t('Login'),
143
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,  ''],
147
148                                 '$openid'       => !$noid,
149                                 '$lopenid'      => ['openid_url', $openid_title, $identity, '', $openid_readonly],
150
151                                 '$hiddens'      => $hiddens,
152
153                                 '$register'     => $reg,
154
155                                 '$lostpass'     => DI::l10n()->t('Forgot your password?'),
156                                 '$lostlink'     => DI::l10n()->t('Password Reset'),
157
158                                 '$tostitle'     => DI::l10n()->t('Website Terms of Service'),
159                                 '$toslink'      => DI::l10n()->t('terms of service'),
160
161                                 '$privacytitle' => DI::l10n()->t('Website Privacy Policy'),
162                                 '$privacylink'  => DI::l10n()->t('privacy policy'),
163                         ]
164                 );
165
166                 Hook::callAll('login_hook', $o);
167
168                 return $o;
169         }
170
171         /**
172          * Get the URL to the register page and add OpenID parameters to it
173          */
174         private static function getRegisterURL()
175         {
176                 if (empty(Session::get('openid_identity'))) {
177                         return 'register';
178                 }
179
180                 $args = [];
181                 $attr = Session::get('openid_attributes', []);
182
183                 if (is_array($attr) && count($attr)) {
184                         foreach ($attr as $k => $v) {
185                                 if ($k === 'namePerson/friendly') {
186                                         $nick = Strings::escapeTags(trim($v));
187                                 }
188                                 if ($k === 'namePerson/first') {
189                                         $first = Strings::escapeTags(trim($v));
190                                 }
191                                 if ($k === 'namePerson') {
192                                         $args['username'] = Strings::escapeTags(trim($v));
193                                 }
194                                 if ($k === 'contact/email') {
195                                         $args['email'] = Strings::escapeTags(trim($v));
196                                 }
197                                 if ($k === 'media/image/aspect11') {
198                                         $photosq = bin2hex(trim($v));
199                                 }
200                                 if ($k === 'media/image/default') {
201                                         $photo = bin2hex(trim($v));
202                                 }
203                         }
204                 }
205
206                 if (!empty($nick)) {
207                         $args['nickname'] = $nick;
208                 } elseif (!empty($first)) {
209                         $args['nickname'] = $first;
210                 }
211
212                 if (!empty($photosq)) {
213                         $args['photo'] = $photosq;
214                 } elseif (!empty($photo)) {
215                         $args['photo'] = $photo;
216                 }
217
218                 $args['openid_url'] = Strings::escapeTags(trim(Session::get('openid_identity')));
219
220                 return 'register?' . http_build_query($args);
221         }
222 }