Source for file Login.php
Documentation is available at Login.php
* This module can be used by other modules as follows:-
* - Copy module file to module directory
* - Adjust configuration file as needed (see further below on configuration file requirements)
* - In any action that requires the user to be logged in, run:
* - $this->exec_module_action('Login', 'check_login')
* After login and logout, the module executes the default action specified in the configuration.
* The Login module requires the following entries in the configuration file.
* mode = database or file : Authenticate against a 'database' or 'file'
* key_field = key_field_name : Name of the field that contains the primary key
* user_field = user_field_name : Name of the field that contains the user name
* pass_field = password_field_name : Name of the field that contains the password (encrypted using PASSWORD())
* table = login_table_name : Name of the table in the database that contains login information
* file = path/to/file : Location of the password file
* type = ini : Type of password file
* The defaults are as below:
* file = "ini/password.ini"
* This module requires the Config module when file based authentication is used.
* @var string $mode Authentication mode: 'database' or 'file'
* @var string $table Name of the field that contains the primary key
* @var string $table Name of the field that contains the user name
* @var string $table Name of the field that contains the password (encrypted using PASSWORD())
* @var string $table Name of the table in the database that contains login information
* @var string $file Location of the password file.
* Default: 'ini/password.ini'
var $file =
'ini/password.ini';
* @var string $type Type of password file: 'ini'
* Constructor for Login module
* Register exceptions in the constructor
"New passwords do not match. Please try again.", "ERROR");
"Current password incorrect. Please try again.", "ERROR");
"Your new password is too short. At least 8 characters are required. Please try again.", "ERROR");
"Password file does not exist: '%s'.", "ERROR");
"Password file is empty: '%s'.", "ERROR");
* Get the configuration from the config file
// Load values from config file if applicable
$keys =
explode(' ', 'mode key_field user_field pass_field table file type');
$this->$key =
$this->application->config['login'][$key];
// Check if password file exists if mode = 'file'
$this->error->display_error('ERROR_LOGIN_PASSWORD_FILE_DOES_NOT_EXIST', $this->file);
* Use the following parameters if login processing is invoked from another
* @param string $module Name of the module to process the login form (default: Login)
* @param string $action Name of the action to process the login form (default: login_user)
* @param string $params Parameters required for this module:action combination in "param=value¶m=value" format. (default: '')
* @return mixed If a module:action is specified, this action returns true (for login success) or the login form HTML (for login failure)
function login_user($module=
'Login', $action=
'login_user', $params=
'')
// Load the configuration
// Check if login form was filled
if ($this->mode ==
'database') {
// Check if login/password matches
$rows =
& $this->sql->select_query('SQL_SELECT',
sleep(5); // Sleep to avoid abuse
// Save some of the values in the PHP session
$_SESSION['session_user_id'] =
$row[$this->key_field];
$_SESSION['session_user'] =
$_POST[$this->user_field];
$_SESSION['session_authenticated'] =
1;
} else if ($this->mode ==
'file') {
// Load passwords from file
$this->error->display_error('ERROR_LOGIN_PASSWORD_FILE_EMPTY', $this->file);
// Check if login/password matches
foreach ($passwords as $password) {
$_SESSION['session_user_id'] =
$password[$this->key_field];
$_SESSION['session_user'] =
$_POST[$this->user_field];
$_SESSION['session_authenticated'] =
1;
sleep(5); // Sleep to avoid abuse
// Execute default action or return
if ($module ==
'Login' &&
$action ==
'login_user') {
// Run the default action
$_SERVER['QUERY_STRING'] =
'';
* Log out the current user. This is done by deleting the session
* The default or the specified module:action is executed after logging out.
* @param string $module Name of the module to execute after logout
* @param string $action Name of the action to execute after logout
* @param string $params Parameters required for this module:action combination in "param=value¶m=value" format. (default: '')
function logout_user($module=
'', $action=
'', $params=
'')
// Delete all the saved session data
if ($module ==
'' &&
$action ==
'') {
// Run the default action
$_SERVER['QUERY_STRING'] =
'';
$_SERVER['QUERY_STRING'] =
"module=$module&action=$action";
$_SERVER['QUERY_STRING'] .=
$params;
* @param string $module Name of the module to process the login form
* @param string $action Name of the action to process the login form
* @param string $params Parameters required for this module:action combination in "param=value¶m=value" format. (default: '')
* @param bool $failed Set to true if previous login attempt failed.
// Load the configuration
$view =
new View("Login");
$this->output .=
$view->get_data();
$view->set_data("User name:");
$user_label =
$view->get_data();
$view->set_properties(array("size" =>
33, "maxlength" =>
60, "required" =>
"required"));
$user_textbox =
$view->get_data();
$view->set_data("Password:");
$pass_label =
$view->get_data();
$view->set_properties(array("required" =>
"required"));
$pass_textbox =
$view->get_data();
// Submit and cancel buttons
$user_label =>
$user_textbox,
$pass_label =>
$pass_textbox,
$view->table_two_column_associative();
$view->set_properties(array(
"action" =>
$this->controller->encode_url($module, $action, $params),
"onsubmit" =>
"javascript: return form.validate(this);"));
$this->output .=
$view->get_data();
$view->set_data("Login failed!");
$view->set_properties(array("color" =>
"red"));
$this->output .=
$view->get_data();
* Check if a user is logged in
* @param bool $forward Forward the user to the login form if true (default: true)
// Load the configuration
!isset
($_SESSION['session_authenticated']) ||
$_SESSION['session_authenticated'] !=
1) {
* This function updates the login of the currently logged in user
* Function requires the user to be logged in in order to change their login. It changes the login of the
* @param string $new_login A string containing the updated login. String is escaped by the function using addslashes()
// Verify that we are logged in
// Escape the provided string
// Update login only if it has changed
if ($new_login !=
$_SESSION['session_user']) {
if ($this->mode ==
'database') {
// Update the database row
'SQL_UPDATE', $this->table,
$this->user_field.
"='".
$_SESSION['session_user'].
"'", "nocheck");
} else if ($this->mode ==
'file') {
// Get passwords from file
$this->error->display_error('ERROR_LOGIN_PASSWORD_FILE_EMPTY', $this->file);
foreach ($passwords as $user =>
$password) {
if ($passwords[$user][$this->user_field] ==
$_SESSION['session_user']) {
$passwords[$user][$this->user_field] =
$new_login;
// Update the session variable
$_SESSION['session_user'] =
$new_login;
* This function creates a form to update the password of the currently logged in user
* Function requires the user to be logged in in order to change their login. It changes the login of the
* active user. It should be directly invoked.
// Verify that we are logged in
$current_password =
addslashes($_POST['current_password']);
$new_password =
addslashes($_POST['new_password']);
$new_password_repeat =
addslashes($_POST['new_password_repeat']);
// Check if current password is correct
$rows =
& $this->sql->select_query('SQL_SELECT',
$this->table, "where ".
$this->user_field.
"='".
$_SESSION['session_user'].
"' and ".
$this->pass_field.
"=PASSWORD('".
$current_password.
"')");
sleep(5); // Sleep to avoid abuse
unset
($_POST['current_password']);
unset
($_POST['new_password']);
unset
($_POST['new_password_repeat']);
$this->error->display_error('ERROR_LOGIN_CURRENT_PASSWORD_INCORRECT');
if ($new_password !=
$new_password_repeat) {
unset
($_POST['current_password']);
unset
($_POST['new_password']);
unset
($_POST['new_password_repeat']);
$this->error->display_error('ERROR_LOGIN_NEW_PASSWORD_MISMATCH');
if (strlen($new_password) <
8) {
unset
($_POST['current_password']);
unset
($_POST['new_password']);
unset
($_POST['new_password_repeat']);
$this->error->display_error('ERROR_LOGIN_NEW_PASSWORD_TOO_SHORT');
// Update the password field
$this->sql->update_query('SQL_UPDATE', $this->table,
$this->pass_field.
"=PASSWORD('".
$new_password.
"')",
$this->user_field.
"='".
$_SESSION['session_user'].
"'", "nocheck");
// Run the default action
$_SERVER['QUERY_STRING'] =
'';
// Display the change password form
$view =
new View("Current password");
$view->label("current_password");
$label_op =
$view->get_data();
$view->set_data("New password");
$view->label("new_password");
$label_np =
$view->get_data();
$view->set_data("New password (repeat)");
$view->label("new_password_repeat");
$label_npr =
$view->get_data();
// Create the password fields
$view->set_properties(array('required' =>
'required'));
$view->input_password("current_password");
$view->set_properties(array('required' =>
'required'));
$view->input_password("new_password");
$view->set_properties(array('required' =>
'required'));
$view->input_password("new_password_repeat");