Skip to content

Commit

Permalink
Issue 5421: Autoenroll for already registered users
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-koppula committed Jan 16, 2014
1 parent e8315aa commit e539005
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 260 deletions.
13 changes: 10 additions & 3 deletions include/html/auto_enroll_courses.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@
if (count($rows_courses) > 0) $_SESSION['enroll'] = AT_ENROLL_YES;

$course_names="";
$course_registered_names = "";
foreach($rows_courses as $row_courses){
$course_names.='<li>'.$row_courses["title"].'</li>';
$course = $row_courses["course_id"];
$sql = "SELECT access, member_id FROM %scourses WHERE course_id=%d";
$course_info = queryDB($sql, array(TABLE_PREFIX, $course), TRUE);

$check_already_registered = "SELECT * FROM %scourse_enrollment WHERE course_id=%d AND member_id=%d";
$chk_registered_result = queryDB($check_already_registered, array(TABLE_PREFIX, $course, $member_id));
if (count($chk_registered_result) > 0) {
$course_registered_names.='<li>' . $row_courses["title"] . '</li>';
}
if (count($chk_registered_result) == 0) {
if ($course_info['access'] == 'private')
{

$sql = "INSERT INTO %scourse_enrollment VALUES (%d, %d, 'n', 0, '"._AT('student')."', 0)";
$result = queryDB($sql, array(TABLE_PREFIX, $member_id, $course));

$course_names.='<li>' . $row_courses["title"] . '</li>';
// send the email - if needed
if ($system_courses[$course]['notify'] == 1) {
$mail_list = array(); //initialize an array to store all the pending emails
Expand Down Expand Up @@ -97,8 +103,9 @@

$sql = "INSERT INTO %scourse_enrollment VALUES (%d, %d, 'y', 0, '"._AT('student')."', 0)";
$result = queryDB($sql, array(TABLE_PREFIX,$member_id, $course));
$course_names.='<li>' . $row_courses["title"] . '</li>';
}
}
}
}
?>
2 changes: 2 additions & 0 deletions include/install/db/atutor_language_text.sql
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ INSERT INTO `language_text` VALUES ('en', '_module', 'bbb_admin_setup', 'BigBlue
('en', '_msgs', 'AT_INFOS_ALREADY_ENROLLED', 'You have already made a request to enroll in this course and you have not yet been approved by the instructor. You will be notifed when your request has been approved.', '2004-11-05 10:29:01', ''),
('en', '_msgs', 'AT_INFOS_APPROVAL_PENDING', 'Your request has been made. You will be notified when your request has been approved.<br /><br />Return to <a href=\"users/index.php\">My Courses</a>.', '2009-09-17 14:48:30', ''),
('en', '_msgs', 'AT_INFOS_ASSIGNMENT_FS_SUBMISSIONS', 'Assignments can be submitted using the Assignment Dropbox or through My Files in the File Storage tool. Assign an assignment to all students, or create a group and enable File Storage for that group, to add an assignment drop-box.', '2011-05-30 10:08:46', ''),
('en', '_msgs', 'AT_INFOS_AUTO_ENROLL_ALREADY_ENROLLED', 'You are already enrolled to the following courses: <ul> %s </ul>', '2013-12-16 10:39:43', ''),
('en', '_msgs', 'AT_INFOS_CANNOT_CONNECT_GITHUB_SERVER', 'Failed to connect to GitHub server to determine if the files that will be modified by the Patcher differ from the original versions of those files. So, to protect any customizations you have made, your local files are assumed to be customized. You may choose to ignore and continue, or create backups of your file just in case.', '2013-12-10 18:01:45', ''),
('en', '_msgs', 'AT_INFOS_CANNOT_CONNECT_MOD_SERVER', 'Unable to connect to module server. Be sure your Internet connection is functioning, and your firewall is not blocking access.', '2009-12-16 18:45:05', ''),
('en', '_msgs', 'AT_INFOS_CANNOT_CONNECT_PATCH_SERVER', 'Failed to connect to patch server: %s so patches provided by the patch server can not be listed. Please proceed with installing private patch.', '2008-10-08 12:51:54', ''),
Expand Down Expand Up @@ -949,6 +950,7 @@ INSERT INTO `language_text` VALUES ('en', '_module', 'bbb_admin_setup', 'BigBlue
('en', '_template', 'all_questions_on_page', 'All questions on one page', '2007-08-20 15:46:22', 'create/edit test'),
('en', '_template', 'all_students', 'All Students', '2006-03-23 13:39:11', ''),
('en', '_template', 'all_users', 'All Users', '2005-03-10 15:24:33', ''),
('en', '_template', 'already_registered', 'If already registered click here', '2014-01-16 15:24:33', ''),
('en', '_template', 'alter', 'Alter', '2008-04-21 15:11:31', ''),
('en', '_template', 'alternate_text', 'Alternate Text', '2004-12-08 12:39:27', 'filemanager, image inset alt field'),
('en', '_template', 'alternatives', 'Alternatives', '2010-03-09 16:16:55', 'adapted content'),
Expand Down
226 changes: 226 additions & 0 deletions include/login_functions.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php
/***********************************************************************/
/* ATutor */
/***********************************************************************/
/* Copyright (c) 2002-2010 */
/* Inclusive Design Institute */
/* http://atutor.ca */
/* */
/* This program is free software. You can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation. */
/***********************************************************************/
// $Id$



// For security reasons the token has to be generated anew before each login attempt.
// The entropy of SHA-1 input should be comparable to that of its output; in other words, the more randomness you feed it the better.

/***
* Remove comments below to enable a remote login form.
*/
if (isset($_POST['token']))
{
$_SESSION['token'] = $_POST['token'];
}
else
{
if (!isset($_SESSION['token']))
$_SESSION['token'] = sha1(mt_rand() . microtime(TRUE));
}

/***
* Add comments 2 lines below to enable a remote login form.
*/
//if (!isset($_SESSION['token']))
// $_SESSION['token'] = sha1(mt_rand() . microtime(TRUE));

if (isset($_GET['course'])) {
$_GET['course'] = intval($_GET['course']);
} else {
$_GET['course'] = 0;
}

// check if we have a cookie
if (!$msg->containsFeedbacks()) {
if (isset($_COOKIE['ATLogin'])) {
$cookie_login = $_COOKIE['ATLogin'];
}
if (isset($_COOKIE['ATPass'])) {
$cookie_pass = $_COOKIE['ATPass'];
}
}

//garbage collect for maximum login attempts table
if (rand(1, 100) == 1){
queryDB("DELETE FROM %smember_login_attempt WHERE expiry < '%s'", array(TABLE_PREFIX, time()));
}

if (isset($cookie_login, $cookie_pass) && !isset($_POST['submit'])) {
/* auto login */
$this_login = $cookie_login;
$this_password = $cookie_pass;
$auto_login = 1;
$used_cookie = true;
} else if (isset($_POST['submit'])) {
/* form post login */
$this_password = $_POST['form_password_hidden'];
$this_login = $_POST['form_login'];
$auto_login = isset($_POST['auto']) ? intval($_POST['auto']) : 0;
$used_cookie = false;
} else if (isset($_POST['submit1'])) {
/* form post login on autoenroll registration*/
$this_password = $_POST['form1_password_hidden'];
$this_login = $_POST['form1_login'];
$auto_login = isset($_POST['auto']) ? intval($_POST['auto']) : 0;
$used_cookie = false;
}

if (isset($this_login, $this_password)) {
if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
session_regenerate_id(TRUE);
}


if ($_GET['course']) {
$_POST['form_course_id'] = intval($_GET['course']);
} else {
$_POST['form_course_id'] = intval($_POST['form_course_id']);
}
$this_login = $addslashes($this_login);
$this_password = $addslashes($this_password);

//Check if this account has exceeded maximum attempts
$rows = queryDB("SELECT login, attempt, expiry FROM %smember_login_attempt WHERE login='%s'", array(TABLE_PREFIX, $this_login));

if ($rows && count($rows) > 0){
list($attempt_login_name, $attempt_login, $attempt_expiry) = $rows[0];
} else {
$attempt_login_name = '';
$attempt_login = 0;
$attempt_expiry = 0;
}
if($attempt_expiry > 0 && $attempt_expiry < time()){
//clear entry if it has expired
queryDB("DELETE FROM %smember_login_attempt WHERE login='%s'", array(TABLE_PREFIX, $this_login));
$attempt_login = 0;
$attempt_expiry = 0;
}

if ($used_cookie) {
#4775: password now store with salt
$rows = queryDB("SELECT password, last_login FROM %smembers WHERE login='%s'", array(TABLE_PREFIX, $this_login));
$cookieRow = $rows[0];
$saltedPassword = hash('sha512', $cookieRow['password'] . hash('sha512', $cookieRow['last_login']));
$rows = queryDB("SELECT member_id, login, first_name, second_name, last_name, preferences,password AS pass, language, status, last_login FROM %smembers WHERE login='%s' AND '%s'='%s'", array(TABLE_PREFIX, $this_login, $saltedPassword, $this_password));
} else {
$rows = queryDB("SELECT member_id, login, first_name, second_name, last_name, preferences, language, status, password AS pass, last_login FROM %smembers WHERE (login='%s' OR email='%s') AND SHA1(CONCAT(password, '%s'))='%s'", array(TABLE_PREFIX, $this_login, $this_login, $_SESSION[token], $this_password));
}
$row = $rows[0];

if($_config['max_login'] > 0 && $attempt_login >= $_config['max_login']){
$msg->addError('MAX_LOGIN_ATTEMPT');
} else if ($row['status'] == AT_STATUS_UNCONFIRMED) {
$msg->addError('NOT_CONFIRMED');
} else if ($row && $row['status'] == AT_STATUS_DISABLED) {
$msg->addError('ACCOUNT_DISABLED');
} else if ($row) {
$_SESSION['valid_user'] = true;
$_SESSION['member_id'] = intval($row['member_id']);
$_SESSION['login'] = $row['login'];
if ($row['preferences'] == "")
assign_session_prefs(unserialize(stripslashes($_config["pref_defaults"])), 1);
else
assign_session_prefs(unserialize(stripslashes($row['preferences'])), 1);
$_SESSION['is_guest'] = 0;
$_SESSION['lang'] = $row['language'];
$_SESSION['course_id'] = 0;
$now = date('Y-m-d H:i:s');

if ($auto_login == 1) {
$parts = parse_url($_base_href);
// update the cookie.. increment to another 2 days
$cookie_expire = time()+172800;
// #4775, also look at pref_tab_functions.inc.php setAutoLoginCookie(). Same technique.
$saltedPassword = hash('sha512', $row['pass'] . hash('sha512', $now));
ATutor.setcookie('ATLogin', $this_login, $cookie_expire, $parts['path']);
ATutor.setcookie('ATPass', $saltedPassword, $cookie_expire, $parts['path']);
}

$_SESSION['first_login'] = false;
if ($row['last_login'] == null || $row['last_login'] == '' || $row['last_login'] == '0000-00-00 00:00:00'
|| $_SESSION['prefs']['PREF_MODIFIED']!==1) {
$_SESSION['first_login'] = true;
}

queryDB("UPDATE %smembers SET creation_date=creation_date, last_login='%s' WHERE member_id=%d", array(TABLE_PREFIX, $now, $_SESSION['member_id']));

//clear login attempt on successful login
queryDB("DELETE FROM %smember_login_attempt WHERE login='%s'", array(TABLE_PREFIX, $this_login));

//if page variable is set, bring them there.
if (isset($_POST['p']) && $_POST['p']!=''){
header ('Location: '.urldecode($_POST['p']));
exit;
}

$msg->addFeedback('LOGIN_SUCCESS');
if(!isset($_REQUEST['en_id'])) {
header('Location: bounce.php?course='.$_POST['form_course_id']);
exit;
}
} else {
// check if it's an admin login.
$rows = queryDB("SELECT login, `privileges`, language FROM %sadmins WHERE login='%s' AND SHA1(CONCAT(password, '%s'))='%s' AND `privileges`>0", array(TABLE_PREFIX, $this_login, $_SESSION['token'], $this_password));

if ($row = $rows[0]) {
$sql = "UPDATE %sadmins SET last_login=NOW() WHERE login='%s'";
$num_login = queryDB($sql, array(TABLE_PREFIX, $this_login));

$_SESSION['login'] = $row['login'];
$_SESSION['valid_user'] = true;
$_SESSION['course_id'] = -1;
$_SESSION['privileges'] = intval($row['privileges']);
$_SESSION['lang'] = $row['language'];

$sql = "UPDATE ".TABLE_PREFIX."admins SET last_login=NOW() WHERE login='$this_login'";
write_to_log(AT_ADMIN_LOG_UPDATE, 'admins', $num_login, $sql);

//clear login attempt on successful login
queryDB("DELETE FROM %smember_login_attempt WHERE login='%s'", array(TABLE_PREFIX, $this_login));

$msg->addFeedback('LOGIN_SUCCESS');

header('Location: admin/index.php');
exit;

} else {
$expiry_stmt = '';
$attempt_login++;
if ($attempt_expiry==0){
$expiry = (time() + LOGIN_ATTEMPT_LOCKED_TIME * 60); //an hour from now
} else {
$expiry = $attempt_expiry;
}
queryDB("REPLACE INTO %smember_login_attempt SET attempt='%s', expiry='%s', login='%s'", array(TABLE_PREFIX, $attempt_login, $expiry, $this_login));
}
//Different error messages depend on the number of login failure.
if ($_config['max_login'] > 0 && ($_config['max_login']-$attempt_login)==2){
$msg->addError('MAX_LOGIN_ATTEMPT_2');
} elseif ($_config['max_login'] > 0 && ($_config['max_login']-$attempt_login)==1){
$msg->addError('MAX_LOGIN_ATTEMPT_1');
} elseif ($_config['max_login'] > 0 && ($_config['max_login']-$attempt_login)==0){
$msg->addError('MAX_LOGIN_ATTEMPT');
} else {
$msg->addError('INVALID_LOGIN');
}
}
}

if (isset($_SESSION['member_id'])) {
queryDB("DELETE FROM %susers_online WHERE member_id=%d", array(TABLE_PREFIX, $_SESSION['member_id']));
}

$_SESSION['prefs']['PREF_FORM_FOCUS'] = 1;
?>
Loading

0 comments on commit e539005

Please sign in to comment.