apollodec/Apollo/api/application/models/Login_model.php
venbatechnologies@gmail.com c8441e5a09 otp mail
2019-01-07 17:30:19 +05:30

515 lines
19 KiB
PHP
Executable File

<?php
/**
* Created by PhpStorm.
* User: karthi
* Date: 11/9/17
* Time: 5:28 PM
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_model extends CI_Model
{
/*
* get user login details
* params:email,password
* created by karthi
* */
/*
* Default login abc123 -> e99a18c428cb38d5f260853678922e03
*/
public function login($mobile = null, $password = null, $accessFrom = null)
{
$this->db->select('t1.MobileNumber, t1.Password,t1.ListCode,t1.UpdatedOn,t1.OTP,t1.StaffID');
$this->db->from('' . LOGIN . ' as t1');
$this->db->where('t1.MobileNumber', $mobile);
$this->db->or_where('t1.StaffID', $mobile);
// $this->db->join('' . STAFF . ' as t2', 't1.StaffID = t2.StaffID', 'LEFT');
$this->db->where('t1.IsActive', 1);
//$this->db->where('t2.IsActive', 1);
$loginDetails = $this->db->get(LOGIN)->first_row();
// print_r($this->db->last_query());die();
// print_r($loginDetails);
if($loginDetails) {
$mobileno= $loginDetails->MobileNumber;
$mobileno= $loginDetails->MobileNumber;
if($loginDetails->ListCode == STUDENT){
$start = date_create($loginDetails->UpdatedOn);
$end = date_create();
$diff = date_diff($end,$start);
if($diff->i <= 10){
if($password == $loginDetails->OTP){
if($accessFrom == 'Web'){
$this->db->select('LO.MobileNumber, LO.ListCode, LO.ID,ST.BranchCode, ST.LoginAccess');
$this->db->from('' . LOGIN . ' as LO');
$this->db->join('' . STUDENTS . ' as ST', 'LO.StaffID = ST.StudentID');
$this->db->join('' . STUDENTCOURSE . ' as SC', 'SC.StudentID = ST.StudentID');
$this->db->join('' . BRANCH . ' as BR', 'BR.BranchCode = SC.BranchID');
$this->db->where('LO.MobileNumber', $mobile);
$this->db->where('ST.IsActive', 1);
$this->db->where('BR.Is_StudentWebAccessActive', 1);
$responseDetails = $this->db->get()->first_row();
// print_r($this->db->last_query());die();
if($responseDetails){
$save['OTP'] = '';
$this->db->where('MobileNumber',$mobile);
$this->db->where('ListCode',STUDENT);
$this->db->update('T_Login',$save);
if($responseDetails->LoginAccess == 1){
$result['loginStatus'] = true;
$result['details'] = $responseDetails;
} else {
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
}
else{
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
} else {
$this->db->select('LO.MobileNumber, LO.ListCode, LO.ID,ST.BranchCode, ST.LoginAccess');
$this->db->from('' . LOGIN . ' as LO');
$this->db->join('' . STUDENTS . ' as ST', 'LO.StaffID = ST.StudentID');
$this->db->join('' . STUDENTCOURSE . ' as SC', 'SC.StudentID = ST.StudentID');
$this->db->join('' . BRANCH . ' as BR', 'BR.BranchCode = SC.BranchID');
$this->db->where('LO.MobileNumber', $mobile);
$this->db->where('ST.IsActive', 1);
$this->db->where('BR.Is_StudentMobileAccessActive', 1);
$responseDetails = $this->db->get()->first_row();
if($responseDetails){
$save['OTP'] = '';
$this->db->where('MobileNumber',$mobile);
$this->db->where('ListCode',STUDENT);
$this->db->update('T_Login',$save);
if($responseDetails->LoginAccess == 1){
$result['loginStatus'] = true;
$result['details'] = $responseDetails;
} else {
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
}
else{
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
}
}else{
$result['loginStatus'] = false;
$result['message'] = "Please Enter Valid OTP !!";
}
}else{
$result['loginStatus'] = false;
$result['message'] = "OTP is Expired !!";
$save['OTP'] = '';
$this->db->where('MobileNumber',$mobile);
$this->db->update('T_Login',$save);
}
}
else
{
// print_r($loginDetails->ListCode);
if($loginDetails->ListCode != SUPER_ADMIN)
{
//echo 'NO_SUPER_ADMIN';die();
if ($mobile == $loginDetails->MobileNumber || strtoupper($mobile) == strtoupper($loginDetails->StaffID))
{
// echo 'no _if';die();
// print_r($loginDetails);die();
$this->db->select('LO.MobileNumber, LO.ListCode, LO.ID, ST.IsActive,LO.StaffID,ST.BranchCode,SC.CallModuleAccess');
$this->db->from('' . LOGIN . ' as LO');
$this->db->join('' . STAFF . ' as ST', 'LO.StaffID = ST.StaffID');
$this->db->join('' . STAFF_BRANCH . ' as SC', 'SC.StaffID = ST.StaffID');
$this->db->join('' . BRANCH . ' as BR', 'BR.BranchCode = SC.BranchCode');
$this->db->where('LO.MobileNumber', $mobile);
$this->db->where('LO.OTP',$password);
$this->db->where('ST.IsActive', 1);
// $this->db->where('BR.Is_StudentWebAccessActive', 1);
$responseDetails = $this->db->get()->first_row();
// print_r($responseDetails);die();
//print_r($this->db->last_query());die();
if($responseDetails)
{
$save['OTP'] = '';
$this->db->where('MobileNumber',$mobile);
$this->db->where('StaffID',$mobile);
// $this->db->where('ListCode',ADMIN);
// $this->db->or_where('ListCode',EMPLOYEE);
// $this->db->or_where('ListCode',TRAINEE);
$this->db->update('T_Login',$save);//print_r($this->db->last_query());die();
if($responseDetails->IsActive == 1)
{
$result['loginStatus'] = true;
$result['details'] = $responseDetails;
}
else
{
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
}
else
{
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
}
else
{ // echo 'no _else';die();
$result['loginStatus'] = false;
$result['message'] = "Please Enter Register Mobile Number !!";
}
}
else if($loginDetails->ListCode ==SUPER_ADMIN)
{
if ($password == $loginDetails->Password) {
/// echo 'SUPER_ADMIN';die();
$superadminpwd= md5($password);
$this->db->select('t1.MobileNumber, t1.ListCode, t1.ID, t2.BranchCode, t2.FinanceModuleAccess,t2.CallModuleAccess');
$this->db->from('' . LOGIN . ' as t1');
$this->db->join('' . STAFF . ' as t2', 't1.StaffID = t2.StaffID', 'LEFT');
$this->db->where('t1.MobileNumber', $mobile);
$this->db->where('t2.IsActive', 1);
$responseDetails = $this->db->get()->first_row();// print_r($this->db->last_query());die();
if($responseDetails)
{
if ($responseDetails->ListCode == SUPER_ADMIN)
{
$this->db->select('t1.BranchCode');
$this->db->from('' . BRANCH . ' as t1');
$this->db->order_by('t1.CreatedOn', 'ASC');
$this->db->limit(1);
$getBranch = $this->db->get()->first_row();
if($getBranch)
{
// For First time Super admin login, will set all branch detail
$result['loginStatus'] = true;
$responseDetails->BranchCode = $getBranch->BranchCode;
$result['details'] = $responseDetails;
}
else
{
// For First time Super admin login, will set all branch detail
$result['loginStatus'] = true;
$responseDetails->BranchCode = 'All';
$result['details'] = $responseDetails;
}
}
else
{
$result['loginStatus'] = true;
$result['details'] = $responseDetails;
}
}
else
{
$result['loginStatus'] = false;
$result['message'] = "Please Enter The Correct OTP.";
}
}
else
{
$result['loginStatus'] = false;
$result['message'] = "Please Enter Valid Password !!";
}
}
}
}
else
{
$result['loginStatus'] = false;
$result['message'] = "Your login is de-activated or don't have login.";
}
/*$this->db->join('areas a', 'a.id = e.id_area', 'inner');
$this->db->join('horario h', 'h.id = e.id_horario', 'inner');
$this->db->join('dia d', 'd.id = e.id_data', 'inner');
$this->db->join('tipo_evento t', 't.id = e.id_tipo', 'inner');
$this->db->join('campus c', 'c.id = e.id_unidade', 'inner');
if ( $id != null ){
$this->db->where('e.id', $id);
return $this->db->get('eventos')->first_row();
} else {
return $this->db->get('eventos')->result();
}*/
return $result;
}
public function change_Password( $userId, $newPassword, $oldPassword, $updateOn) {
$this->db->select('Password');
$this->db->from(LOGIN);
$this->db->where('ID', $userId);
$loginDetails = $this->db->get()->first_row();
if ($loginDetails->Password == $oldPassword) {
$sql = "UPDATE ".LOGIN." SET Password='$newPassword', UpdatedBy='$userId', UpdatedOn='$updateOn' WHERE ID='$userId'";
if($this->db->query($sql) == '1'){
$result['changePassStatus'] = true;
$result['message'] = "Your Password Successfully changed !!";
} else {
$result['changePassStatus'] = false;
$result['message'] = "Please try Again !!";
}
} else {
$result['changePassStatus'] = false;
$result['message'] = "Your Current Password miss match, Please try Again !!";
}
return $result;
}
/**
* Studnet Check function*
* created_by : sriram
*/
public function checkstudnetnumber($mobile){
$this->db->select('t1.MobileNumber, t1.Password,t1.ListCode');
$this->db->from('' . LOGIN . ' as t1');
$this->db->where('t1.MobileNumber', $mobile);
$this->db->where('t1.IsActive', 1);
$loginDetails = $this->db->get(LOGIN)->first_row();
// print_r($loginDetails);die;
if($loginDetails){
if($loginDetails->ListCode == STUDENT)
{
$result['loginstudent'] = true;
$this->db->select('LO.MobileNumber, LO.ListCode, LO.ID,ST.BranchCode, ST.LoginAccess,ST.EmailID,ST.Firstname');
$this->db->from('' . LOGIN . ' as LO');
$this->db->join('' . STUDENTS . ' as ST', 'LO.StaffID = ST.StudentID');
$this->db->where('LO.MobileNumber', $mobile);
$this->db->where('ST.IsActive', 1);
$responseDetails = $this->db->get()->first_row();
if($responseDetails->LoginAccess ==1){
$EmailID = $responseDetails->EmailID;
$name = $responseDetails->Firstname;
$string2 = str_shuffle('1234567890');
$otp = substr($string2,0,6);
//$otp = '431528';
// //echo $otp;die;
$save['OTP'] = $otp;
$save['UpdatedOn'] = date('Y-m-d H:i:s');
// print_r($save);die;
$this->db->where('MobileNumber',$mobile);
$this->db->where('ListCode',STUDENT);
$query = $this->db->update('T_Login',$save);
// $query =1;
if($query == 1){
$sms = $this->smssend($mobile,$otp);
$mail = $this->mailsend($EmailID,$otp,$name);
$result['checkstudent'] = true;
}else{
$result['checkstudent'] = false;
}
}else {
$result['loginStatus'] = false;
$result['message'] = "Your login is de-activated.";
}
}
else if(($loginDetails->ListCode == ADMIN)||($loginDetails->ListCode == EMPLOYEE)||(($loginDetails->ListCode == TRAINEE)))
{
$result['loginstudent'] = true;
$this->db->select('LO.MobileNumber, LO.ListCode, LO.ID,ST.BranchCode, ST.IsActive,LO.StaffID,ST.MobileNumber as Mobile');
$this->db->from('' . LOGIN . ' as LO');
$this->db->join('' . STAFF . ' as ST', 'LO.StaffID = ST.StaffID');
$this->db->where('LO.MobileNumber', $mobile);
$this->db->where('ST.IsActive', 1);
$responseDetails = $this->db->get()->first_row();
//print_r($responseDetails);die();
if($responseDetails->IsActive ==1){
$string2 = str_shuffle('1234567890');
$otp = substr($string2,0,6);
//$otp = '431528';
// //echo $otp;die;
$save['OTP'] = $otp;
$save['UpdatedOn'] = date('Y-m-d H:i:s');
// print_r($save);die;
$this->db->where('MobileNumber',$mobile);
//$this->db->where('ListCode',STUDENT);
$query = $this->db->update('T_Login',$save);
$mobileno = $responseDetails->Mobile;
// $query =1;
if($query == 1){
$sms = $this->smssend($mobileno,$otp);
$result['checkstudent'] = true;
}else{
$result['checkstudent'] = false;
}
}else {
$result['loginStatus'] = false;
$result['message'] = "Your login is de-activated.";
}
}
else{
$result['loginstudent'] = false;
}
}
else
{
$result['checkstudent'] = false;
$result['message'] = "Your login is de-activated or don't have login.";
}
//print_r($result);die;
return $result;
}
public function smssend($mobile,$OTP) {
$message ="Your One Time Password to Login to apollodec.com is $OTP. This is valid for 10 minutes.";
$mobile = "91$mobile";
$message = urlencode($message);
// echo $message;die;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://smsapi.24x7sms.com/api_2.0/SendSMS.aspx?APIKEY=xegCdYUIMf3&MobileNo=" . $mobile . "&SenderID=APODEC&Message=" . $message . "&ServiceName=TEMPLATE_BASED");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function mailsend($EmailID,$otp,$name) {
$emails = array();
$list = $EmailID;
//Load email library
$this->load->library('email');
//SMTP & mail configuration
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.sendgrid.net',
'smtp_port' => 587,
'smtp_user' => 'apikey',
'smtp_pass' => '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',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
// print_r($this->email);exit();
//Email content
// $htmlContent = '<h1>Sending email via SMTP server</h1>';
$htmlContent = 'Dear '.$name.', Your OTP for login ApolloDEC is '.$otp;
// $htmlContent = $msg;
// Email body load the html template
// $body = $this->load->view('emails/anillabs.php',$data,TRUE);
$this->email->to($list);
$this->email->from('noreply@apollodec.com','Apollo');
$subj = 'OTP For Login';
$this->email->subject($subj);
$this->email->message($htmlContent);
//Send email 1
$result = $this->email->send();
$time = date('Y-m-d H:i:s');
$dateTime = $time;
$msgGroup = date('YmdHis');
$data[] = array('MobileNumber' => $list, 'MsgBody' => $htmlContent, 'Status' => 'success', 'CreatedOn' => $dateTime,'MsgGroup' => $msgGroup);
$this->db->insert_batch('T_BroadcastStatus', $data);
// echo $this->email->print_debugger(); exit();
return $result;
}
/**
* End the function*
*/
}