FusionDirectory
class_LoginPost.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2017-2018 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
24 class LoginPost extends LoginMethod
25 {
27  static $secondFactorMethods = ['SecondFactorWebAuthn','SecondFactorTotp','SecondFactorRecoveryCode'];
28 
30  static function getLabel ()
31  {
32  return _('HTML form');
33  }
34 
35  static function init ()
36  {
37  parent::init();
38 
39  /* Init second factor methods if needed */
40  foreach (static::$secondFactorMethods as $secondFactorMethod) {
41  if (!class_available($secondFactorMethod)) {
42  continue;
43  }
44  $secondFactorMethod::init();
45  }
46  }
47 
49  static function loginProcess ()
50  {
51  global $smarty, $config, $message;
52 
53  static::init();
54 
55  $smarty->assign('focusfield', 'username');
56 
57  if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['login']) && isset($_POST['username']) && isset($_POST['password'])) {
58  static::$username = $_POST['username'];
59  static::$password = $_POST['password'];
60 
61  $success = static::runSteps([
62  'validateUserInput',
63  'checkForLockingBranch',
64  'ldapLoginUser',
65  'loginAndCheckExpired',
66  'runSchemaCheck',
67  ]);
68 
69  if ($success) {
70  /* If needed redirect to second factor page */
71  $secondFactor = FALSE;
72  foreach (static::$secondFactorMethods as $secondFactorMethod) {
73  if (!class_available($secondFactorMethod)) {
74  continue;
75  }
76  $secondFactor = ($secondFactorMethod::hasSecondFactor() || $secondFactor);
77  }
78  if ($secondFactor) {
79  static::redirectSecondFactorPage();
80  }
81 
82  /* Everything went well, redirect to main.php */
83  static::redirect();
84  }
85  }
86 
87  static::displayLogin();
88  }
89 
91  static protected function redirectSecondFactorPage ()
92  {
93  session::un_set('connected');
94  header('Location: secondfactor.php');
95  exit;
96  }
97 
99  static protected function displayLogin ()
100  {
101  global $smarty,$message,$config,$ssl,$error_collector,$error_collector_mailto;
102 
103  $lang = session::get('lang');
104 
105  error_reporting(E_ALL | E_STRICT);
106 
107  /* Fill template with required values */
108  $username = '';
109  if (isset($_POST['username'])) {
110  $username = trim($_POST['username']);
111  }
112  $smarty->assign('date', gmdate('D, d M Y H:i:s'));
113  $smarty->assign('username', $username);
114  $smarty->assign('copynotice', copynotice());
115  $smarty->append('css_files', get_template_path('login.css'));
116  $smarty->assign('title', _('Sign in'));
117 
118  /* Some error to display? */
119  if (!isset($message)) {
120  $message = '';
121  }
122  $smarty->assign('message', $message);
123 
124  /* Display SSL mode warning? */
125  if (($ssl != '') && ($config->get_cfg_value('warnSSL') == 'TRUE')) {
126  $smarty->assign('ssl', sprintf(htmlescape(_('Warning: %sSession is not encrypted!%s')), '<a href="'.$ssl.'">', '</a>'));
127  } else {
128  $smarty->assign('ssl', '');
129  }
130 
131  if (!$config->check_session_lifetime()) {
132  $smarty->assign('lifetime', _('Warning: The session lifetime configured in your fusiondirectory.conf will be overridden by php.ini settings.'));
133  } else {
134  $smarty->assign('lifetime', '');
135  }
136 
137  /* Generate server list */
138  $servers = [];
139  if (isset($_POST['server'])) {
140  $selected = $_POST['server'];
141  } else {
142  $selected = $config->data['MAIN']['DEFAULT'];
143  }
144  foreach ($config->data['LOCATIONS'] as $key => $ignored) {
145  $servers[$key] = $key;
146  }
147  $smarty->assign('server_options', $servers);
148  $smarty->assign('server_id', $selected);
149 
150  /* show login screen */
151  $smarty->assign('PHPSESSID', session_id());
152  if ($error_collector != '') {
153  $smarty->assign('php_errors', preg_replace('/%BUGBODY%/', $error_collector_mailto, $error_collector).'</div>');
154  } else {
155  $smarty->assign('php_errors', '');
156  }
157  $smarty->assign('msg_dialogs', msg_dialog::get_dialogs());
158  $smarty->assign('usePrototype', 'false');
159  $smarty->assign('date', date('l, dS F Y H:i:s O'));
160  $smarty->assign('lang', preg_replace('/_.*$/', '', $lang));
161  $smarty->assign('rtl', Language::isRTL($lang));
162 
163  $smarty->display(get_template_path('headers.tpl'));
164  $smarty->assign('version', FD_VERSION);
165 
166  $smarty->display(get_template_path('login.tpl'));
167  exit();
168  }
169 
171  static function displaySecondFactorPage ()
172  {
173  global $smarty,$message,$config,$ssl,$error_collector,$error_collector_mailto;
174 
175  $lang = session::get('lang');
176 
177  error_reporting(E_ALL | E_STRICT);
178 
179  /* Fill template with required values */
180  $username = '';
181  if (isset($_POST['username'])) {
182  $username = trim($_POST['username']);
183  }
184  $smarty->assign('date', gmdate('D, d M Y H:i:s'));
185  $smarty->assign('username', $username);
186  $smarty->assign('copynotice', copynotice());
187  $smarty->append('css_files', get_template_path('login.css'));
188  $smarty->assign('title', _('Second factor'));
189 
190  /* Some error to display? */
191  if (!isset($message)) {
192  $message = '';
193  }
194  $smarty->assign('message', $message);
195 
196  /* show login screen */
197  $smarty->assign('PHPSESSID', session_id());
198  if ($error_collector != '') {
199  $smarty->assign('php_errors', preg_replace('/%BUGBODY%/', $error_collector_mailto, $error_collector).'</div>');
200  } else {
201  $smarty->assign('php_errors', '');
202  }
203  $smarty->assign('msg_dialogs', msg_dialog::get_dialogs());
204  $smarty->assign('usePrototype', 'false');
205  $smarty->assign('date', date('l, dS F Y H:i:s O'));
206  $smarty->assign('lang', preg_replace('/_.*$/', '', $lang));
207  $smarty->assign('rtl', Language::isRTL($lang));
208  $smarty->assign('CSRFtoken', CSRFProtection::getToken());
209 
210  $methodOutputs = [];
211 
212  /* Run second factor methods */
213  foreach (static::$secondFactorMethods as $secondFactorMethod) {
214  if (!class_available($secondFactorMethod)) {
215  continue;
216  }
217  $methodOutput = $secondFactorMethod::execute();
218  if ($methodOutput !== NULL) {
219  $methodOutputs[$secondFactorMethod] = $methodOutput;
220  }
221  }
222 
223  $smarty->assign('methodOutputs', $methodOutputs);
224 
225  $smarty->display(get_template_path('headers.tpl'));
226  $smarty->assign('version', FD_VERSION);
227 
228  $smarty->display(get_template_path('secondfactor.tpl'));
229  exit();
230  }
231 }
const FD_VERSION
FusionDirectory Version.
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
static redirectSecondFactorPage()
Redirect to the second factor page.
get_template_path($filename='', $plugin=FALSE, $path='')
Return themed path for specified base file.
Definition: functions.inc:174
static get($name)
Accessor of a session var.
static get_dialogs()
Accessor of the message dialog rendered HTML.
static loginProcess()
All login steps in the right order for standard POST login.
static $secondFactorMethods
List of second factor methods, may be dynamic later.
Base class for login methods.
copynotice()
Return HTML safe copyright notice.
Definition: functions.inc:149
static displaySecondFactorPage()
Display the second factor page and exit()
static un_set($name)
Unset a session.
static displayLogin()
Display the login page and exit()
static getLabel()
Displayed name.
static isRTL($lang)
Returns TRUE if $lang is a right to left language.
class_available($name)
Checks if a class is available.
Definition: functions.inc:92
Login via POST.