Current File : /home/giriqfky/public_html/Barga/application/modules/sessions/controllers//Sessions.php
<?php
class Sessions extends Basecontroller
{
public function __construct()
{
parent::__construct();
$this->load->model('Sessions_model', 'Sessions');
$this->controller_name = "sessions";
$this->folder = "sessions";
}
public function index()
{
$data['title'] = "Login";
$data['company_tag'] = getSettingValue('company_tag');
if($this->session->userdata('is_login')) { redirect('dashboard','refresh'); } //Checked is logined in or not
if($this->input->post()) {
$this->form_validation->set_rules('user_name', 'Email / User Name', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() === TRUE) {
$user_name = $this->input->post('user_name');
$password = $this->input->post('password');
$query = $this->Sessions->verifyLogin($user_name, md5($password));
if($query->num_rows() > 0) {
$user_data = $query->row();
if($user_data->user_status == 1) {
// Update last login datetime
$this->Sessions->updateUserProfile(array('last_login_date' => date('Y-m-d H:i:s'), 'last_login_ip' => getUserIP()), $user_data->user_id);
$user_info = array(
'user_id' => $user_data->user_id,
'user_role_id' => $user_data->user_role_id,
'user_profile_name' => $user_data->user_profile_name,
'user_name' => $user_data->user_name,
'user_email' => $user_data->user_email,
'user_status' => $user_data->user_status,
);
$this->session->set_userdata('user_info', $user_info);
$this->session->set_userdata('is_login', true);
if($this->input->post('remember_me') == 1) {
setcookie("setuser", $user_name, time()+3600*12*30);
setcookie("setpass", $password, time()+3600*12*30);
setcookie("remember_me", $this->input->post('remember_me'),time()+3600*12*30);
}
redirect('dashboard');
} else {
$this->session->set_flashdata('user_error', getMessage("inactive_user", 3));
}
} else {
$this->session->set_flashdata('user_error', getMessage("invalid_login", 3));
}
}
}
if(isset($_COOKIE["remember_me"]) && $_COOKIE["remember_me"] == 1) {
$data['setuser'] = $_COOKIE["setuser"];
$data['setpass'] = $_COOKIE["setpass"];
$data['remember_me'] = 1;
} else {
$data['setuser'] = '';
$data['setpass'] = '';
$data['remember_me'] = '';
}
$this->load->view('login',$data);
}
public function sendResetEmail()
{
$data['title'] = "Send Reset Email";
$data['company_tag'] = getSettingValue('company_tag');
if($this->input->post()) {
$this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() === TRUE) {
$user_email = $this->input->post('user_email');
$this->db->select('*');
$this->db->where(array('user_email' => $user_email));
$query = $this->db->get(TABLE_USERS);
if($query->num_rows() == 1) {
$user = $query->row();
$company_name = getSettingValue('company_name');
$admin_email = getSettingValue('company_email');
if(EMAIL_ON == 1) {
$password_reset_link = '<a href="'.site_url('sessions/passwordResetForm/'.$user->user_id.'/'.md5($user->user_name.$user->user_phone)).'">Reset Your Password</a>';
$user_password_reset_tpl = getEmailTemplate('admin_user_password_reset_tpl');
$mail_subject = $user_password_reset_tpl->email_template_subject;
$mail_subject = str_replace("{{company_name}}", $company_name, $mail_subject);
$mail_subject = str_replace("{{profile_name}}", $user->user_profile_name, $mail_subject);
$mail_body = str_replace("{{profile_name}}", $user->user_profile_name, $user_password_reset_tpl->email_template_body);
$mail_body = str_replace("{{password_reset_link}}", $password_reset_link, $mail_body);
$this->load->library('supertronmailer', array(
'to' => $user->user_email,
'subject' => $mail_subject,
'body' => $mail_body,
'name' => $user->user_profile_name,
'from_name' => $company_name,
'from_email' => $admin_email
));
$this->supertronmailer->send();
}
$this->session->set_flashdata('msg_success', getMessage("password_reset_mail_sent", 3));
redirect($this->controller_name.'/sendResetEmail');
} else {
$this->session->set_flashdata('msg_error', getMessage("email_does_not_exists", 3));
}
}
}
$this->load->view('send_reset_email',$data);
}
public function passwordResetForm($user_id, $token)
{
if($user_id > 0 && $token != "") {
$data['title'] = "Password Reset Form";
$data['user_id'] = $user_id;
$data['token'] = $token;
if($this->input->post()) {
$this->form_validation->set_rules('user_password', 'New Password', 'trim|required');
$this->form_validation->set_rules('confirm_user_password', 'Confirm New Password', 'trim|required|matches[user_password]');
if ($this->form_validation->run() === TRUE) {
$this->db->select('user_name,user_phone,user_profile_name');
$this->db->where(array('user_id' => $user_id));
$this->db->limit(1);
$user = $this->db->get(TABLE_USERS)->row();
$new_token = md5($user->user_name.$user->user_phone);
if($new_token == $token) {
$new_user_password = $this->input->post('user_password');
$company_name = getSettingValue('company_name');
$admin_email = getSettingValue('company_email');
$this->db->where('user_id', $user_id);
$this->db->update(TABLE_USERS, array('user_password' => md5($new_user_password), 'dom' => date('Y-m-d H:i:s')));
if(EMAIL_ON == 1) {
$user_password_reset_tpl = getEmailTemplate('admin_user_password_reset_tpl');
$mail_subject = $user_password_reset_tpl->email_template_subject;
$mail_subject = str_replace("{{company_name}}", $company_name, $mail_subject);
$mail_subject = str_replace("{{profile_name}}", $user->user_profile_name, $mail_subject);
$mail_body = str_replace("{{profile_name}}", $user->user_profile_name, $user_password_reset_tpl->email_template_body);
$mail_body = str_replace("{{new_password}}", $new_user_password, $mail_body);
$this->load->library('supertronmailer', array(
'to' => $user->user_email,
'subject' => $mail_subject,
'body' => $mail_body,
'name' => $user->user_profile_name,
'from_name' => $company_name,
'from_email' => $admin_email
));
$this->supertronmailer->send();
}
$this->session->set_flashdata('msg_success', getMessage("record_updated", 3));
redirect($this->controller_name);
} else {
$this->session->set_flashdata('msg_error', getMessage("invalid_token", 3));
redirect($this->controller_name);
}
}
}
$this->load->view('password_reset_form',$data);
} else {
$this->session->set_flashdata('user_error', getMessage("invalid_request", 3));
redirect($this->controller_name);
}
}
public function logout()
{
$this->session->sess_destroy();
redirect($this->controller_name);
}
}