Here's one more controller I'm trying to get reviewed for. Any thoughts?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('users/usersmodel');
}
public function index()
{
//Config Defaults Start
$msgBoxMsgs = array();//msgType = dl, info, warn, note, msg
$cssPageAddons = '';//If you have extra CSS for this view append it here
$jsPageAddons = '<script src='.base_url().'assets/'. $this->config->item('defaultTemplate') .'/js/validate/login.js></script>';//If you have extra JS for this view append it here
$metaAddons = '';//Sometimes there is a need for additional Meta Data such in the case of Facebook addon's
$siteTitle = '';//alter only if you need something other than the default for this view.
//Config Defaults Start
//examples of how to use the message box system (css not included).
//$msgBoxMsgs[] = array('msgType' => 'dl', 'theMsg' => 'This is a Blank Message Box...');
/**********************************************************Your Coding Logic Here, Start*/
if ($this->session->userdata('xtr') == "yes")
{
redirect('cpanel');
}
else
{
$bodyContent = $this->config->item('defaultTemplate') ."/usermanagement/forms/login_form";//which view file
$bodyType = "full";//type of template
}
/***********************************************************Your Coding Logic Here, End*/
//Double checks if any default variables have been changed, Start.
//If msgBoxMsgs array has anything in it, if so displays it in view, else does nothing.
if(count($msgBoxMsgs) !== 0)
{
$msgBoxes = $this->msgboxes->buildMsgBoxesOutput(array('display' => 'show', 'msgs' =>$msgBoxMsgs));
}
else
{
$msgBoxes = array('display' => 'none');
}
if($siteTitle == '')
{
$siteTitle = $this->metatags->SiteTitle(); //reads
}
//Double checks if any default variables have been changed, End.
$this->data['msgBoxes'] = $msgBoxes;
$this->data['cssPageAddons'] = $cssPageAddons;//if there is any additional CSS to add from above Variable this will send it to the view.
$this->data['jsPageAddons'] = $jsPageAddons;//if there is any addictional JS to add from the above variable this will send it to the view.
$this->data['siteTitle'] = $siteTitle;//defaults can be changed via models/metatags.php
$this->data['bodyType'] = $bodyType;
$this->data['bodyContent'] = $bodyContent;
$this->load->view($this->config->item('defaultTemplate') .'/usermanagement/index', $this->data);
}
function submit()
{
// Sets validation rules for the login form
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember me', 'integer');
// Checks to see if login form was submitted properly
if ($this->form_validation->run() === false)
{
$outputArray = array('error' => 'yes', 'message' => 'There was a problem submitting the form! Please refresh the window and try again!');
}
else
{
if (is_null($userData = $this->usersmodel->getUserByUsername($this->input->post('username'))))
{
// Username was not found in the database
$outputArray = array('error' => 'yes', 'message' => 'Incorrect username and password combination!');
}
else
{
// Checks to see if user has exceeded max login attempts
if ($this->auth->isMaxLoginAttemptsExceeded($userData->userID))
{
// Max was exceeded and sends email to account holder
$outputArray = array('error' => 'yes', 'message' => 'Your account is currently locked, we appologize for the inconvienence. You must wait 10 minutes before you can login again! An email was sent to the owner of this account!');
$userData = array('userID' => $userData->userID, 'firstName' => $userData->firstName, 'lastName' => $userData->lastName, 'email' => $userData->email, 'username' => $userData->username);
$this->auth->sendEmail($this->config->item('defaultTemplate'), 'maxlogins', 'KOW Manager Account Locked', $userData);
}
else
{
// Matches user's status for validity
switch($userData->usersStatusesID)
{
// Registered not validated
case 1:
$outputArray = array('error' => 'yes', 'message' => 'Sorry you must verify your account before logging in!');
break;
// Account suspended
case 3:
$outputArray = array('error' => 'yes', 'message' => 'Your account has been suspended!');
break;
// Account Banned
case 4:
$outputArray = array('error' => 'yes', 'message' => 'Your account is currently banned!');
break;
// Account Deleted
case 5:
$outputArray = array('error' => 'yes', 'message' => 'Your account has been deleted!');
break;
// Registered and validated
default:
// Checks to see if login was successful
if ($this->auth->login($this->input->post('username'), $this->input->post('password'), $this->input->post('remember')))
{
// Login was successful
$outputArray = array('success' => 'Yes', 'message' => 'Sending to control panel!');
}
else
{
// Login failed
$outputArray = array('error' => 'yes', 'message' => 'Incorrect username and password combination!');
}
}
}
}
}
echo json_encode($outputArray);
}
}
/* End of file login.php */
/* Location: ./application/controllers/login.php */
KOW Auth:
/**
* Login user on the site. Return TRUE if login is successful
* (user exists and activated, password is correct), otherwise FALSE.
*
* @param string
* @param string
* @param integer
* @return array
*/
function login($username, $password, $remember)
{
$this->ci->load->model('users/usersmodel');
if ($userData = $this->ci->usersmodel->getUserByUsername($username))
{
$regenFromPostPW = $this->ci->genfunc->reGenPassHash($password, $userData->passwordHash);
if ($regenFromPostPW == $userData->password)
{
$this->ci->session->set_userdata(array(
'xtr' => 'yes',
'userID' => $userData->userID,
'username' => $userData->username,
'role' => $userData->usersRolesID
));
if ($remember == 1)
{
//$this->create_autologin($user_data->user_id);
}
$this->clearLoginUserAttempts($userData->userID);
$this->session->all_userdata();
//$this->ci->users->insert_session($this->session->userdata('session_id'), $this->session->userdata('user_id'), $this->genfunc->getRealIpAddr(), $this->session->userdata('user_agent'));
return $userData;
}
else
{
$this->increaseLoginUserAttempt($userData->userID);
}
}
else
{
return null;
}
}