/
authentication.php
374 lines (330 loc) · 15 KB
/
authentication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
*
*/
/**
* Authentication Controller
*
* This controller performs authentication
*
* @package LimeSurvey
* @subpackage Backend
*/
class Authentication extends Admin_Controller {
/**
* Constructor
*/
function __construct()
{
parent::__construct();
}
/**
* Default Controller Action
*/
function index()
{
redirect('/admin', 'refresh');
}
/**
* Show login screen and parse login data
*/
function login()
{
if(!$this->session->userdata("loginID"))
{
//from login_check.php
$sIp = $this->session->userdata("ip_address");
$this->load->model("failed_login_attempts_model");
$query = $this->failed_login_attempts_model->getAllRecords(array("ip"=>$sIp));
//$query = "SELECT * FROM ".db_table_name('failed_login_attempts'). " WHERE ip='$sIp';";
$bCannotLogin = false;
$intNthAttempt = 0;
//if ($result!==false && $result->RecordCount() >= 1)
if ($query->num_rows() > 0)
{
$field = $query->row_array();//$result->FetchRow();
$intNthAttempt = $field['number_attempts'];
if ($intNthAttempt>=$this->config->item("maxLoginAttempt")){
$bCannotLogin = true;
}
$iLastAttempt = strtotime($field['last_attempt']);
if (time() > $iLastAttempt + $this->config->item("timeOutTime")){
$bCannotLogin = false;
//$query = "DELETE FROM ".db_table_name('failed_login_attempts'). " WHERE ip='$sIp';";
$this->failed_login_attempts_model->deleteAttempts($sIp);
}
}
if (!$bCannotLogin)
{
if($this->input->post('action'))
{
self::_doLogin($sIp, $intNthAttempt);
}
else
{
self::_showLoginForm();
}
}
else
{
// wrong or unknown username
$data['errormsg']="";
$data['maxattempts']=sprintf($this->limesurvey_lang->gT("You have exceeded you maximum login attempts. Please wait %d minutes before trying again"),($this->config->item("timeOutTime")/60))."<br />";
$data['clang']=$this->limesurvey_lang;
parent::_getAdminHeader();
$this->load->view('admin/Authentication/error', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
}
else
{
redirect('/admin', 'refresh');
}
}
/**
* Logout user
*/
function logout()
{
killSession();
self::_showLoginForm('<p>'.$this->limesurvey_lang->gT("Logout successful."));
}
/**
* Forgot Password screen
*/
function forgotpassword()
{
$clang = $this->limesurvey_lang;
if(!$this->input->post("action"))
{
$data['clang'] = $this->limesurvey_lang;
parent::_getAdminHeader();
$this->load->view('admin/Authentication/forgotpassword', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
else
{
$postuser = $this->input->post("user");
$emailaddr = $this->input->post("email");
//$query = "SELECT users_name, password, uid FROM ".db_table_name('users')." WHERE users_name=".$connect->qstr($postuser)." AND email=".$connect->qstr($emailaddr);
//$result = db_select_limit_assoc($query, 1) or safe_die ($query."<br />".$connect->ErrorMsg()); // Checked
$this->load->model("Users_model");
$query = $this->Users_model->getSomeRecords(array("users_name, password, uid"),array("users_name"=>$postuser,"email"=>$emailaddr));
if ($query->num_rows() < 1)
{
// wrong or unknown username and/or email
$data['errormsg']=$this->limesurvey_lang->gT("User name and/or email not found!");
$data['maxattempts']="";
$data['clang']=$this->limesurvey_lang;
parent::_getAdminHeader();
$this->load->view('admin/Authentication/error', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
else
{
//$fields = $result->FetchRow();
$fields = $query->row_array();
// send Mail
$new_pass = createPassword();
$body = sprintf($clang->gT("Your user data for accessing %s"),$this->config->item("sitename")). "<br />\n";;
$body .= $clang->gT("Username") . ": " . $fields['users_name'] . "<br />\n";
$body .= $clang->gT("New password") . ": " . $new_pass . "<br />\n";
$this->load->config("email");
$subject = $clang->gT("User data","unescaped");
$to = $emailaddr;
$from = $this->config->item("siteadminemail");
$sitename = $this->config->item("siteadminname");
if(SendEmailMessage($body, $subject, $to, $from, $this->config->item("sitename"), false,$this->config->item("siteadminbounce")))
{
//$query = "UPDATE ".db_table_name('users')." SET password='".SHA256::hashing($new_pass)."' WHERE uid={$fields['uid']}";
//$connect->Execute($query); //Checked
$this->Users_model->updatePassword($fields['uid'], $this->sha256->hashing($new_pass));
$data['clang'] = $clang;
$data['message'] = "<br />".$clang->gT("Username").": {$fields['users_name']}<br />".$clang->gT("Email").": {$emailaddr}<br />
<br />".$clang->gT("An email with your login data was sent to you.");
parent::_getAdminHeader();
$this->load->view('admin/Authentication/message', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
else
{
$tmp = str_replace("{NAME}", "<strong>".$fields['users_name']."</strong>", $clang->gT("Email to {NAME} ({EMAIL}) failed."));
$data['clang'] = $clang;
$data['message'] = "<br />".str_replace("{EMAIL}", $emailaddr, $tmp) . "<br />";
parent::_getAdminHeader();
$this->load->view('admin/Authentication/message', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
}
}
}
/**
* Show login screen
* @param optional message
*/
function _showLoginForm($logoutsummary="")
{
$refererargs=''; // If this is a direct access to admin.php, no args are given
// If we are called from a link with action and other args set, get them
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'])
{
$refererargs = html_escape($_SERVER['QUERY_STRING']);
}
$data['refererargs'] = $refererargs;
$data['clang'] = $this->limesurvey_lang;
if ($logoutsummary=="")
{
$data['summary'] = $this->limesurvey_lang->gT("You have to login first.");
}
else
{
$data['summary'] = $logoutsummary;
}
$lan=array();
$this->load->helper("surveytranslator");
foreach (getlanguagedata(true) as $langkey=>$languagekind)
{
array_push($lan,$langkey);
}
parent::_getAdminHeader();
$this->load->view('admin/Authentication/login', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
/**
* Parse login data
* @param user ip
* @param login attempts
*/
function _doLogin($sIp,$bLoginAttempted)
{
$clang = $this->limesurvey_lang;
$postuser = sanitize_user($this->input->post("user"));
//$query = "SELECT * FROM ".db_table_name('users')." WHERE users_name=".$connect->qstr($postuser);
$this->load->model("Users_model");
$query = $this->Users_model->getAllRecords(array("users_name"=>$postuser));
//var_dump($query->row_array());
//$result = $connect->SelectLimit($query, 1) or safe_die ($query."<br />".$connect->ErrorMsg());
if ($query->num_rows() < 1)
{
//$query = fGetLoginAttemptUpdateQry($bLoginAttempted,$sIp);
$this->load->model("failed_login_attempts_model");
$query = $this->failed_login_attempts_model->addAttempt($bLoginAttempted,$sIp);
//$result = $connect->Execute($query) or safe_die ($query."<br />".$connect->ErrorMsg());;
if ($query)
{
// wrong or unknown username
$data['errormsg']=$clang->gT("Incorrect username and/or password!");
$data['maxattempts']="";
if ($bLoginAttempted+1>=$this->config->item("maxLoginAttempt"))
$data['maxattempts']=sprintf($clang->gT("You have exceeded you maximum login attempts. Please wait %d minutes before trying again"),($this->config->item("timeOutTime")/60))."<br />";
$data['clang']=$clang;
parent::_getAdminHeader();
$this->load->view('admin/Authentication/error', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
}
else
{
$fields = $query->row_array();//$result->FetchRow();
$this->load->library('admin/sha256','sha256');
if ($this->sha256->hashing($this->input->post('password')) == $fields['password'])
{
// Anmeldung ERFOLGREICH
if (strtolower($this->input->post('password'))=='password')
{
$this->session->set_userdata('pw_notify',true);
$this->session->set_userdata('flashmessage',$clang->gT("Warning: You are still using the default password ('password'). Please change your password and re-login again."));
}
else
{
$this->session->set_userdata('pw_notify',false);
} // Check if the user has changed his default password
/*if ($sessionhandler=='db')
{
adodb_session_regenerate_id();
}
else
{
session_regenerate_id();
}*/
$this->session->set_userdata('loginID',intval($fields['uid']));
$this->session->set_userdata('user',$fields['users_name']);
$this->session->set_userdata('full_name',$fields['full_name']);
$this->session->set_userdata('htmleditormode',$fields['htmleditormode']);
$this->session->set_userdata('templateeditormode',$fields['templateeditormode']);
$this->session->set_userdata('questionselectormode',$fields['questionselectormode']);
$this->session->set_userdata('dateformat',$fields['dateformat']);
// Compute a checksession random number to test POSTs
$this->session->set_userdata('checksessionpost',sRandomChars(10));
$postloginlang=sanitize_languagecode($this->input->post('loginlang'));
if (isset($postloginlang) && $postloginlang!='default')
{
$this->session->set_userdata('adminlang',$postloginlang);
//$this->load->library('Limesurvey_lang',array("langcode"=>$postloginlang));
$this->limesurvey_lang->limesurvey_lang(array("langcode"=>$postloginlang));
$clang = $this->limesurvey_lang;
//$uquery = "UPDATE {$dbprefix}users "
//. "SET lang='{$postloginlang}' "
//. "WHERE uid={$_SESSION['loginID']}";
//$uresult = $connect->Execute($uquery); // Checked
$this->Users_model->updateLang($this->session->userdata("loginID"),$postloginlang);
}
else
{
$this->session->set_userdata('adminlang',$fields['lang']);
//$clang = new limesurvey_lang($fields['lang']);
$this->load->library('Limesurvey_lang',array("langcode"=>$fields['lang']));
$clang = $this->limesurvey_lang;
}
$login = true;
$loginsummary = "<br />".sprintf($clang->gT("Welcome %s!"),$this->session->userdata['full_name'])."<br /> ";
if ($this->input->post('refererargs') && strpos($this->input->post('refererargs'), "action=logout") === FALSE)
{
//require_once("../classes/inputfilter/class.inputfilter_clean.php");
$myFilter = new InputFilter('','',1,1,1);
// Prevent XSS attacks
//$sRefererArg=$myFilter->process($_POST['refererargs']);
$sRefererArg = $this->input->post('refererargs',true);
$this->session->set_userdata('metaHeader',"<meta http-equiv=\"refresh\""
. " content=\"1;URL={$scriptname}?".$sRefererArg."\" />");
$loginsummary = "<p><font size='1'><i>".$clang->gT("Reloading screen. Please wait.")."</i></font>\n";
}
self::_GetSessionUserRights($this->session->userdata('loginID'));
// self::_showMessageBox($clang->gT("Logged in"), $loginsummary);
$this->session->set_userdata("just_logged_in",true);
$this->session->set_userdata('loginsummary',$loginsummary);
redirect(site_url('/admin'));
}
else
{
//$query = fGetLoginAttemptUpdateQry($bLoginAttempted,$sIp);
$this->load->model("failed_login_attempts_model");
$query = $this->failed_login_attempts_model->addAttempt($bLoginAttempted,$sIp);
//$result = $connect->Execute($query) or safe_die ($query."<br />".$connect->ErrorMsg());;
if ($query)
{
// wrong or unknown username
$data['errormsg']=$clang->gT("Incorrect username and/or password!");
$data['maxattempts']="";
if ($bLoginAttempted+1>=$this->config->item("maxLoginAttempt"))
$data['maxattempts']=sprintf($clang->gT("You have exceeded you maximum login attempts. Please wait %d minutes before trying again"),($this->config->item("timeOutTime")/60))."<br />";
$data['clang']=$clang;
parent::_getAdminHeader();
$this->load->view('admin/Authentication/error', $data);
parent::_getAdminFooter("http://docs.limesurvey.org", $this->limesurvey_lang->gT("LimeSurvey online manual"));
}
}
}
}
}