Guest User

Untitled

a guest
Apr 26th, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.26 KB | None | 0 0
  1. <?php
  2.  
  3. require_once __DIR__ . '../../../vendor/autoload.php';
  4. /**
  5. * Created by PhpStorm.
  6. * User: rounak
  7. * Date: 26/01/17
  8. * Time: 2:10 AM
  9. */
  10. class Login extends CI_Controller
  11. {
  12. public $now = "";
  13.  
  14. function __construct()
  15. {
  16. parent::__construct();
  17. //Cookie Check
  18. //Cookie Check
  19. if ($this->input->cookie('jcb_user') != "") {
  20. redirect('home');
  21. }
  22. //Load the Models
  23. $this->load->model('otpModel');
  24. $this->load->model('userModel');
  25. //Get the current timezone
  26. $this->now = new DateTime();
  27. $this->now->setTimezone(new DateTimezone('Asia/Kolkata'));
  28. $this->now = $this->now->format('Y-m-d H:i:s');
  29. }
  30.  
  31. public function index()
  32. {
  33. //Loading the required libraries
  34. $this->load->library('session');
  35. $this->load->helper('form');
  36. $fb = new Facebook\Facebook([
  37. 'app_id' => '1820844724832802',
  38. 'app_secret' => 'efa0048dac84a90f838cde1e86fc9e85',
  39. 'default_graph_version' => 'v2.5',
  40. ]);
  41. $helper = $fb->getRedirectLoginHelper();
  42.  
  43. //Get the Session Data
  44. $phoneno = $this->session->flashdata('phoneno');
  45. $finalPhoneNo = $this->input->cookie('jcb_user_login');
  46.  
  47. //If Flags are 0 that means offset data exists, if 1 then it means it does not exists
  48. $flag = $finalFlag = 0;
  49. if ($this->session->flashdata('phoneno') == false) {
  50. $flag = 1;
  51. }
  52.  
  53. //Security Check 1
  54. if ($this->input->cookie('jcb_user_login') == false) {
  55. $finalFlag = 1;
  56. }
  57.  
  58. //Security Check 1
  59. if ($flag == 1 && $finalFlag == 1) {
  60. redirect('home');
  61. }
  62.  
  63. // Get user's login information
  64.  
  65.  
  66. //User Logged In Case
  67. if ($flag == 1) {
  68.  
  69. try {
  70. // Returns a `Facebook\FacebookResponse` object
  71. $accessToken = $helper->getAccessToken();
  72. $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);
  73. } catch(Facebook\Exceptions\FacebookResponseException $e) {
  74. echo 'Graph returned an error: ' . $e->getMessage();
  75. exit;
  76. } catch(Facebook\Exceptions\FacebookSDKException $e) {
  77. echo 'Facebook SDK returned an error: ' . $e->getMessage();
  78. exit;
  79. }
  80. $user_profile = $response->getGraphUser();
  81.  
  82. //Process the Data Received from FB
  83. $fbId = $ageMin = $ageMax = $bday = $relationStatus = $religion = $significantOther = $location = $hometown = $name = $first_name = $last_name = $email = $fbTimeline = $gender = $fbDp = "";
  84. //Fb ID
  85. if (isset($user_profile['id'])) {
  86. $fbId = $user_profile['id'];
  87. }
  88. //Age Min
  89. if (isset($user_profile['age_range']['min'])) {
  90. $ageMin = $user_profile['age_range']['min'];
  91. }
  92. //Age Max
  93. if (isset($user_profile['age_range']['max'])) {
  94. $ageMax = $user_profile['age_range']['max'];
  95. }
  96. //Birthday
  97. if (isset($user_profile['birthday'])) {
  98. $bday = $user_profile['birthday'];
  99. }
  100. //Birthday
  101. if (isset($user_profile['relationship_status'])) {
  102. $relationStatus = $user_profile['relationship_status'];
  103. }
  104. //Religion
  105. if (isset($user_profile['religion'])) {
  106. $religion = $user_profile['religion'];
  107. }
  108. //Significant_Other
  109. if (isset($user_profile['significant_other'])) {
  110. $significantOther = $user_profile['significant_other'];
  111. }
  112. //Location
  113. if (isset($user_profile['location'])) {
  114. $location = $user_profile['location'];
  115. }
  116. //Home Town
  117. if (isset($user_profile['hometown'])) {
  118. $hometown = $user_profile['hometown'];
  119. }
  120. //Name
  121. if (isset($user_profile['name'])) {
  122. $name = $user_profile['name'];
  123. }
  124. //E Mail
  125. if (isset($user_profile['email'])) {
  126. $email = $user_profile['email'];
  127. }
  128. //First Name
  129. if (isset($user_profile['first_name'])) {
  130. $first_name = $user_profile['first_name'];
  131. }
  132. //Last Name
  133. if (isset($user_profile['last_name'])) {
  134. $last_name = $user_profile['last_name'];
  135. }
  136. //Link To FB Timeline
  137. if (isset($user_profile['link'])) {
  138. $fbTimeline = $user_profile['link'];
  139. }
  140. //Gender
  141. if (isset($user_profile['gender'])) {
  142. $gender = $user_profile['gender'];
  143. }
  144. //Work
  145. if (isset($user_profile['work'])) {
  146. $work = $user_profile['work'];
  147. }
  148. //FB Display Image
  149. $fbDp = 'https://graph.facebook.com/' . $user_profile['id'] . '/picture';
  150. //Note, if after Login Controller is to be customised say to Profile Page, Just Change the NEXT in the line below
  151.  
  152.  
  153. //Prepare Data for Database Update
  154. $userData = array('fbId' => $fbId,
  155. 'ageMin' => $ageMin,
  156. 'ageMax' => $ageMax,
  157. 'bDay' => $bday,
  158. 'relationship_status' => $relationStatus,
  159. 'religion' => $religion,
  160. 'significant_other' => $significantOther,
  161. 'location' => $location,
  162. 'hometown' => $hometown,
  163. 'userName' => $name,
  164. 'fname' => $first_name,
  165. 'lname' => $last_name,
  166. 'email' => $email,
  167. 'fbTimeline' => $fbTimeline,
  168. 'sex' => $gender,
  169. 'image' => $fbDp,
  170. 'date-update' => $this->now,
  171. 'verification' => "1",
  172. 'phone' => $finalPhoneNo);
  173.  
  174. //Update
  175. $this->userModel->updateUser($userData);
  176.  
  177.  
  178. //Create the Cookie
  179. $cookie = array(
  180. 'name' => 'jcb_user',
  181. 'value' => $finalPhoneNo,
  182. 'expire' => time()+86500
  183. );
  184.  
  185. set_cookie($cookie);
  186. delete_cookie('jcb_user_login');
  187. //Auto Logout From FB Access Token
  188. // No need to logout since session is common for facebook+jeeto since api v5
  189. redirect('/home');
  190. } else {
  191. //User Not Logged In Case
  192.  
  193.  
  194. $permissions = ['public_profile']; // optional
  195. $loginUrl = $helper->getLoginUrl(base_url().'login/', $permissions);
  196.  
  197. // Store users facebook login url
  198. // $data['login_url'] = $this->facebook->getLoginUrl(array('scope' => 'public_profile'));
  199. $data['login_url'] = $loginUrl;
  200. error_log($data['login_url']);
  201. //Header INIT
  202. $data['headerTab'] = "home";
  203. //Cookie Non Existent Header State
  204. $data['profileLi'] = "";
  205. $data['pofileLiMobile'] = "";
  206. //Footer INIT
  207. $data['footer'] = $this->load->view('sub-views/footer', $data, TRUE);
  208. $data['header'] = $this->load->view('sub-views/header', $data, TRUE);
  209. $data['timeline'] = $this->load->view('sub-views/facebook', $data, TRUE);
  210. $data['staticRight'] = $this->load->view('sub-views/static-right', $data, TRUE);
  211. $this->load->view('login_data', $data);
  212. //Create the Cookie
  213. $cookie = array(
  214. 'name' => 'jcb_user_login',
  215. 'value' => $phoneno,
  216. 'expire' => time() + 86500
  217. );
  218.  
  219. set_cookie($cookie);
  220.  
  221. }
  222.  
  223. }
  224.  
  225. public function otpverify()
  226. {
  227. //Get the Session Data
  228. $this->load->library('session');
  229. $phoneno = $this->session->flashdata('phoneno');
  230. //Redirect to home page if no session data is present
  231. if ($phoneno == "") {
  232. redirect(base_url());
  233. }
  234. //Get the URI Parameter
  235. $data['responseStatus'] = "";
  236. if ($this->uri->segment(3) == "wrongotp") {
  237. $data['responseStatus'] = '<script type="text/javascript">new PNotify({
  238. title: \'Uh Oh! Seems like the OTP is Incorrect\',
  239. text: \'Please provide a valid OTP to proceed.\',
  240. type: \'error\',
  241. styling: \'bootstrap3\',
  242. delay: 2000
  243. });</script>';
  244. }
  245. $this->load->helper('form');
  246. $data['failResponse'] = "";
  247. //Header INIT
  248. $data['headerTab'] = "home";
  249. //Cookie Non Existent Header State
  250. $data['profileLi'] = "";
  251. $data['pofileLiMobile'] = "";
  252. //Footer INIT
  253. $data['footer'] = $this->load->view('sub-views/footer', $data, TRUE);
  254. $data['header'] = $this->load->view('sub-views/header', $data, TRUE);
  255. $data['timeline'] = $this->load->view('sub-views/facebook', $data, TRUE);
  256. $data['staticRight'] = $this->load->view('sub-views/static-right', $data, TRUE);
  257. $data['phoneno'] = $phoneno;
  258. $this->load->view('otpverify', $data);
  259.  
  260. }
  261.  
  262. public function otp_generate()
  263. {
  264. //Initialise to Error Code & Error Message to Empty Spaces
  265. $errorCode = " ";
  266. $errorMsg = " ";
  267. $phoneNo = $this->input->post('phone');
  268. //Redirect if Phone Number is NULL
  269. if ($phoneNo == "") {
  270. redirect(base_url());
  271. }
  272. $randomNo = rand(1000, 9999);
  273. $otpMsg = $randomNo . "%20is%20your%20OTP.";
  274. //Send OTP using API
  275. $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';
  276. $this->load->library('Curl');
  277. $otpResponse_json = $this->curl->simple_get($otp_url, false, array(CURLOPT_USERAGENT => true));
  278. $otpResponse = json_decode($otpResponse_json, true);
  279. $errorCode = $otpResponse['ErrorCode'];
  280. $errorMsg = $otpResponse['ErrorMessage'];
  281.  
  282. //Log the OTP Transaction
  283. $data = array(
  284. 'phone' => $phoneNo,
  285. 'timestamp' => $this->now,
  286. 'vertical' => "OTP",
  287. 'errorCode' => $errorCode,
  288. 'errorMessage' => $errorMsg);
  289.  
  290. $this->otpModel->insertLog($data);
  291.  
  292. if ($errorCode != "000") {
  293. redirect('home/otpfail');
  294. } else {
  295. $data = array(
  296. 'phone' => $phoneNo,
  297. 'type' => "C",
  298. 'otp' => $randomNo,
  299. 'otp_time' => $this->now,
  300. 'verification' => "0");
  301.  
  302. $dataUpdate = array(
  303. 'phone' => $phoneNo,
  304. 'otp' => $randomNo,
  305. 'otp_time' => $this->now);
  306.  
  307. //Check if existing User
  308. if ($this->userModel->checkUserStatus($phoneNo) == true) {
  309. $this->userModel->updateUser($dataUpdate);
  310. } else {
  311. $this->userModel->insertUser($data);
  312. }
  313.  
  314.  
  315. //Create the OTP Session
  316. $this->load->library('session');
  317. $this->session->set_flashdata('phoneno', $phoneNo);
  318. //Redirect to OTP Verify
  319. redirect('login/otpverify');
  320. }
  321.  
  322. }
  323.  
  324. public function otp_validate()
  325. {
  326. $phoneno = $this->input->post('phoneno');
  327. $otp = $this->input->post('otp');
  328. $validationResult = $this->otpModel->otpValidate($otp, $phoneno);
  329. $this->load->library('session');
  330. $this->session->set_flashdata('phoneno', $phoneno);
  331. if ($validationResult == false) {
  332. //Create the OTP Session
  333. redirect('login/otpverify/wrongotp');
  334. } else {
  335. if ($this->otpModel->verificationValidate($phoneno) == false) {
  336. redirect('login');
  337. } else {
  338. //Create the Cookie - For Repeat User
  339. $cookie = array(
  340. 'name' => 'jcb_user',
  341. 'value' => $phoneno,
  342. 'expire' => time() + 86500
  343. );
  344.  
  345. set_cookie($cookie);
  346. redirect('home');
  347. }
  348.  
  349. }
  350. }
  351.  
  352. public function resendOtp()
  353. {
  354. //Initialise to Error Code & Error Message to Empty Spaces
  355. $errorCode = " ";
  356. $errorMsg = " ";
  357. $phoneNo = $this->uri->segment(3);
  358. //Redirect if no phone number is present
  359. if ($phoneNo == "") {
  360. redirect(base_url());
  361. }
  362. $randomNo = rand(1000, 9999);
  363. $otpMsg = $randomNo . "%20is%20your%20OTP.";
  364. //Resent OTP
  365. $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';
  366. $this->load->library('Curl');
  367. $otpResponse_json = $this->curl->simple_get($otp_url, false, array(CURLOPT_USERAGENT => true));
  368. $otpResponse = json_decode($otpResponse_json, true);
  369. $errorCode = $otpResponse['ErrorCode'];
  370. $errorMsg = $otpResponse['ErrorMessage'];
  371.  
  372. //Log the OTP Transaction
  373. $data = array(
  374. 'phone' => $phoneNo,
  375. 'timestamp' => $this->now,
  376. 'vertical' => "OTP",
  377. 'errorCode' => $errorCode,
  378. 'errorMessage' => $errorMsg);
  379.  
  380. $this->otpModel->insertLog($data);
  381.  
  382. if ($errorCode != "000") {
  383. redirect('home/otpfail');
  384. } else {
  385. $data = array(
  386. 'phone' => $phoneNo,
  387. 'otp' => $randomNo,
  388. 'otp_time' => $this->now);
  389.  
  390. $this->otpModel->updateUser_Otp($data);
  391.  
  392. //Create the OTP Session
  393. $this->load->library('session');
  394. $this->session->set_flashdata('phoneno', $phoneNo);
  395. //Redirect to OTP Verify
  396. redirect('login/otpverify');
  397. }
  398.  
  399. }
  400. }
Add Comment
Please, Sign In to add comment