Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once __DIR__ . '../../../vendor/autoload.php';
- /**
- * Created by PhpStorm.
- * User: rounak
- * Date: 26/01/17
- * Time: 2:10 AM
- */
- class Login extends CI_Controller
- {
- public $now = "";
- function __construct()
- {
- parent::__construct();
- //Cookie Check
- //Cookie Check
- if ($this->input->cookie('jcb_user') != "") {
- redirect('home');
- }
- //Load the Models
- $this->load->model('otpModel');
- $this->load->model('userModel');
- //Get the current timezone
- $this->now = new DateTime();
- $this->now->setTimezone(new DateTimezone('Asia/Kolkata'));
- $this->now = $this->now->format('Y-m-d H:i:s');
- }
- public function index()
- {
- //Loading the required libraries
- $this->load->library('session');
- $this->load->helper('form');
- $fb = new Facebook\Facebook([
- 'app_id' => '1820844724832802',
- 'app_secret' => 'efa0048dac84a90f838cde1e86fc9e85',
- 'default_graph_version' => 'v2.5',
- ]);
- $helper = $fb->getRedirectLoginHelper();
- //Get the Session Data
- $phoneno = $this->session->flashdata('phoneno');
- $finalPhoneNo = $this->input->cookie('jcb_user_login');
- //If Flags are 0 that means offset data exists, if 1 then it means it does not exists
- $flag = $finalFlag = 0;
- if ($this->session->flashdata('phoneno') == false) {
- $flag = 1;
- }
- //Security Check 1
- if ($this->input->cookie('jcb_user_login') == false) {
- $finalFlag = 1;
- }
- //Security Check 1
- if ($flag == 1 && $finalFlag == 1) {
- redirect('home');
- }
- // Get user's login information
- //User Logged In Case
- if ($flag == 1) {
- try {
- // Returns a `Facebook\FacebookResponse` object
- $accessToken = $helper->getAccessToken();
- $response = $fb->get('/me/?fields=id,age_range,birthday,relationship_status,religion,significant_other,location,hometown,name,email,first_name,last_name,link,gender,work', $accessToken);
- } catch(Facebook\Exceptions\FacebookResponseException $e) {
- echo 'Graph returned an error: ' . $e->getMessage();
- exit;
- } catch(Facebook\Exceptions\FacebookSDKException $e) {
- echo 'Facebook SDK returned an error: ' . $e->getMessage();
- exit;
- }
- $user_profile = $response->getGraphUser();
- //Process the Data Received from FB
- $fbId = $ageMin = $ageMax = $bday = $relationStatus = $religion = $significantOther = $location = $hometown = $name = $first_name = $last_name = $email = $fbTimeline = $gender = $fbDp = "";
- //Fb ID
- if (isset($user_profile['id'])) {
- $fbId = $user_profile['id'];
- }
- //Age Min
- if (isset($user_profile['age_range']['min'])) {
- $ageMin = $user_profile['age_range']['min'];
- }
- //Age Max
- if (isset($user_profile['age_range']['max'])) {
- $ageMax = $user_profile['age_range']['max'];
- }
- //Birthday
- if (isset($user_profile['birthday'])) {
- $bday = $user_profile['birthday'];
- }
- //Birthday
- if (isset($user_profile['relationship_status'])) {
- $relationStatus = $user_profile['relationship_status'];
- }
- //Religion
- if (isset($user_profile['religion'])) {
- $religion = $user_profile['religion'];
- }
- //Significant_Other
- if (isset($user_profile['significant_other'])) {
- $significantOther = $user_profile['significant_other'];
- }
- //Location
- if (isset($user_profile['location'])) {
- $location = $user_profile['location'];
- }
- //Home Town
- if (isset($user_profile['hometown'])) {
- $hometown = $user_profile['hometown'];
- }
- //Name
- if (isset($user_profile['name'])) {
- $name = $user_profile['name'];
- }
- //E Mail
- if (isset($user_profile['email'])) {
- $email = $user_profile['email'];
- }
- //First Name
- if (isset($user_profile['first_name'])) {
- $first_name = $user_profile['first_name'];
- }
- //Last Name
- if (isset($user_profile['last_name'])) {
- $last_name = $user_profile['last_name'];
- }
- //Link To FB Timeline
- if (isset($user_profile['link'])) {
- $fbTimeline = $user_profile['link'];
- }
- //Gender
- if (isset($user_profile['gender'])) {
- $gender = $user_profile['gender'];
- }
- //Work
- if (isset($user_profile['work'])) {
- $work = $user_profile['work'];
- }
- //FB Display Image
- $fbDp = 'https://graph.facebook.com/' . $user_profile['id'] . '/picture';
- //Note, if after Login Controller is to be customised say to Profile Page, Just Change the NEXT in the line below
- //Prepare Data for Database Update
- $userData = array('fbId' => $fbId,
- 'ageMin' => $ageMin,
- 'ageMax' => $ageMax,
- 'bDay' => $bday,
- 'relationship_status' => $relationStatus,
- 'religion' => $religion,
- 'significant_other' => $significantOther,
- 'location' => $location,
- 'hometown' => $hometown,
- 'userName' => $name,
- 'fname' => $first_name,
- 'lname' => $last_name,
- 'email' => $email,
- 'fbTimeline' => $fbTimeline,
- 'sex' => $gender,
- 'image' => $fbDp,
- 'date-update' => $this->now,
- 'verification' => "1",
- 'phone' => $finalPhoneNo);
- //Update
- $this->userModel->updateUser($userData);
- //Create the Cookie
- $cookie = array(
- 'name' => 'jcb_user',
- 'value' => $finalPhoneNo,
- 'expire' => time()+86500
- );
- set_cookie($cookie);
- delete_cookie('jcb_user_login');
- //Auto Logout From FB Access Token
- // No need to logout since session is common for facebook+jeeto since api v5
- redirect('/home');
- } else {
- //User Not Logged In Case
- $permissions = ['public_profile']; // optional
- $loginUrl = $helper->getLoginUrl(base_url().'login/', $permissions);
- // Store users facebook login url
- // $data['login_url'] = $this->facebook->getLoginUrl(array('scope' => 'public_profile'));
- $data['login_url'] = $loginUrl;
- error_log($data['login_url']);
- //Header INIT
- $data['headerTab'] = "home";
- //Cookie Non Existent Header State
- $data['profileLi'] = "";
- $data['pofileLiMobile'] = "";
- //Footer INIT
- $data['footer'] = $this->load->view('sub-views/footer', $data, TRUE);
- $data['header'] = $this->load->view('sub-views/header', $data, TRUE);
- $data['timeline'] = $this->load->view('sub-views/facebook', $data, TRUE);
- $data['staticRight'] = $this->load->view('sub-views/static-right', $data, TRUE);
- $this->load->view('login_data', $data);
- //Create the Cookie
- $cookie = array(
- 'name' => 'jcb_user_login',
- 'value' => $phoneno,
- 'expire' => time() + 86500
- );
- set_cookie($cookie);
- }
- }
- public function otpverify()
- {
- //Get the Session Data
- $this->load->library('session');
- $phoneno = $this->session->flashdata('phoneno');
- //Redirect to home page if no session data is present
- if ($phoneno == "") {
- redirect(base_url());
- }
- //Get the URI Parameter
- $data['responseStatus'] = "";
- if ($this->uri->segment(3) == "wrongotp") {
- $data['responseStatus'] = '<script type="text/javascript">new PNotify({
- title: \'Uh Oh! Seems like the OTP is Incorrect\',
- text: \'Please provide a valid OTP to proceed.\',
- type: \'error\',
- styling: \'bootstrap3\',
- delay: 2000
- });</script>';
- }
- $this->load->helper('form');
- $data['failResponse'] = "";
- //Header INIT
- $data['headerTab'] = "home";
- //Cookie Non Existent Header State
- $data['profileLi'] = "";
- $data['pofileLiMobile'] = "";
- //Footer INIT
- $data['footer'] = $this->load->view('sub-views/footer', $data, TRUE);
- $data['header'] = $this->load->view('sub-views/header', $data, TRUE);
- $data['timeline'] = $this->load->view('sub-views/facebook', $data, TRUE);
- $data['staticRight'] = $this->load->view('sub-views/static-right', $data, TRUE);
- $data['phoneno'] = $phoneno;
- $this->load->view('otpverify', $data);
- }
- public function otp_generate()
- {
- //Initialise to Error Code & Error Message to Empty Spaces
- $errorCode = " ";
- $errorMsg = " ";
- $phoneNo = $this->input->post('phone');
- //Redirect if Phone Number is NULL
- if ($phoneNo == "") {
- redirect(base_url());
- }
- $randomNo = rand(1000, 9999);
- $otpMsg = $randomNo . "%20is%20your%20OTP.";
- //Send OTP using API
- $otp_url = 'http://bulksms.logicsolution.net.in/api/mt/SendSMS?user=utkarsh13&password=utkarsh13&senderid=JCBOTP&channel=Trans&DCS=0&flashsms=0&number=' . $phoneNo . '&text=' . $otpMsg . '&route=14';
- $this->load->library('Curl');
- $otpResponse_json = $this->curl->simple_get($otp_url, false, array(CURLOPT_USERAGENT => true));
- $otpResponse = json_decode($otpResponse_json, true);
- $errorCode = $otpResponse['ErrorCode'];
- $errorMsg = $otpResponse['ErrorMessage'];
- //Log the OTP Transaction
- $data = array(
- 'phone' => $phoneNo,
- 'timestamp' => $this->now,
- 'vertical' => "OTP",
- 'errorCode' => $errorCode,
- 'errorMessage' => $errorMsg);
- $this->otpModel->insertLog($data);
- if ($errorCode != "000") {
- redirect('home/otpfail');
- } else {
- $data = array(
- 'phone' => $phoneNo,
- 'type' => "C",
- 'otp' => $randomNo,
- 'otp_time' => $this->now,
- 'verification' => "0");
- $dataUpdate = array(
- 'phone' => $phoneNo,
- 'otp' => $randomNo,
- 'otp_time' => $this->now);
- //Check if existing User
- if ($this->userModel->checkUserStatus($phoneNo) == true) {
- $this->userModel->updateUser($dataUpdate);
- } else {
- $this->userModel->insertUser($data);
- }
- //Create the OTP Session
- $this->load->library('session');
- $this->session->set_flashdata('phoneno', $phoneNo);
- //Redirect to OTP Verify
- redirect('login/otpverify');
- }
- }
- public function otp_validate()
- {
- $phoneno = $this->input->post('phoneno');
- $otp = $this->input->post('otp');
- $validationResult = $this->otpModel->otpValidate($otp, $phoneno);
- $this->load->library('session');
- $this->session->set_flashdata('phoneno', $phoneno);
- if ($validationResult == false) {
- //Create the OTP Session
- redirect('login/otpverify/wrongotp');
- } else {
- if ($this->otpModel->verificationValidate($phoneno) == false) {
- redirect('login');
- } else {
- //Create the Cookie - For Repeat User
- $cookie = array(
- 'name' => 'jcb_user',
- 'value' => $phoneno,
- 'expire' => time() + 86500
- );
- set_cookie($cookie);
- redirect('home');
- }
- }
- }
- public function resendOtp()
- {
- //Initialise to Error Code & Error Message to Empty Spaces
- $errorCode = " ";
- $errorMsg = " ";
- $phoneNo = $this->uri->segment(3);
- //Redirect if no phone number is present
- if ($phoneNo == "") {
- redirect(base_url());
- }
- $randomNo = rand(1000, 9999);
- $otpMsg = $randomNo . "%20is%20your%20OTP.";
- //Resent OTP
- $otp_url = 'http://bulksms.logicsolution.net.in/api/mt/SendSMS?user=utkarsh13&password=utkarsh13&senderid=JCBOTP&channel=Trans&DCS=0&flashsms=0&number=' . $phoneNo . '&text=' . $otpMsg . '&route=14';
- $this->load->library('Curl');
- $otpResponse_json = $this->curl->simple_get($otp_url, false, array(CURLOPT_USERAGENT => true));
- $otpResponse = json_decode($otpResponse_json, true);
- $errorCode = $otpResponse['ErrorCode'];
- $errorMsg = $otpResponse['ErrorMessage'];
- //Log the OTP Transaction
- $data = array(
- 'phone' => $phoneNo,
- 'timestamp' => $this->now,
- 'vertical' => "OTP",
- 'errorCode' => $errorCode,
- 'errorMessage' => $errorMsg);
- $this->otpModel->insertLog($data);
- if ($errorCode != "000") {
- redirect('home/otpfail');
- } else {
- $data = array(
- 'phone' => $phoneNo,
- 'otp' => $randomNo,
- 'otp_time' => $this->now);
- $this->otpModel->updateUser_Otp($data);
- //Create the OTP Session
- $this->load->library('session');
- $this->session->set_flashdata('phoneno', $phoneNo);
- //Redirect to OTP Verify
- redirect('login/otpverify');
- }
- }
- }
Add Comment
Please, Sign In to add comment