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