PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/system/application/controllers/fb_login.php

https://github.com/ibnoe/Microweber
PHP | 209 lines | 114 code | 38 blank | 57 comment | 26 complexity | ddd7a0bac96c259c4e2aa6a4aebe660c MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter Facebook Connect Graph API Login Controller
  4. *
  5. * Author: Graham McCarthy (graham@hitsend.ca) HitSend inc. (http://hitsend.ca)
  6. *
  7. * VERSION: 1.0 (2010-09-30)
  8. * LICENSE: GNU GENERAL PUBLIC LICENSE - Version 2, June 1991
  9. *
  10. **/
  11. class Fb_login extends Controller {
  12. function __construct() {
  13. parent::Controller ();
  14. require_once (APPPATH . 'controllers/default_constructor.php');
  15. }
  16. function index() {
  17. //create blank data array to return
  18. $data = array ();
  19. session_start ();
  20. unset ( $_SESSION ['facebook'] );
  21. $this->load->library ( 'fb_connect' );
  22. $data = array ('facebook' => $this->fb_connect->fb, 'fbSession' => $this->fb_connect->fbSession, 'user' => $this->fb_connect->user, 'uid' => $this->fb_connect->user_id, 'fbLogoutURL' => $this->fb_connect->fbLogoutURL, 'fbLoginURL' => $this->fb_connect->fbLoginURL, 'base_url' => site_url ( 'fb_login' ), 'appkey' => $this->fb_connect->appkey );
  23. //p($data);
  24. if ($data ['user']) {
  25. if ($data ['user'] ["id"]) {
  26. /*if(stristr($k, 'fbs_')){
  27. //var_dump($v);
  28. //setcookie( $k);
  29. }*/
  30. $userdata_check = array ();
  31. $userdata_check ['fb_uid'] = $data ['user'] ["id"];
  32. $userdata_check = CI::model('users')->getUsers ( $userdata_check, $limit = false, $count_only = false );
  33. if ($userdata_check == false) {
  34. //p($data['user']);
  35. $to_save = array ();
  36. $to_save ['username'] = $data ['user'] ["first_name"] . $data ['user'] ["last_name"] . $data ['user'] ["id"];
  37. $to_save ['password'] = rand () . rand () . rand ();
  38. $to_save ['email'] = $data ['user'] ["email"];
  39. $to_save ['is_active'] = 'y';
  40. $to_save ['first_name'] = $data ['user'] ["first_name"];
  41. $to_save ['last_name'] = $data ['user'] ["last_name"];
  42. $to_save ['is_admin'] = 'n';
  43. $to_save ['fb_uid'] = $data ['user'] ["id"];
  44. $userdata_check = CI::model('users')->saveUser ( $to_save );
  45. $userdata_check = $to_save;
  46. } else {
  47. $userdata_check = $userdata_check [0];
  48. }
  49. if (! empty ( $userdata_check )) {
  50. //$test = $this->validate_user_facebook($userdata_check ['fb_uid']);
  51. //var_Dump($test);
  52. CI::library('session')->set_userdata ( 'the_user', $userdata_check );
  53. $user_session = array ();
  54. $user_session ['is_logged'] = 'yes';
  55. $user_session ['user_id'] = $userdata_check ['id'];
  56. CI::library('session')->set_userdata ( 'user_session', $user_session );
  57. }
  58. CI::view ( 'fb_login_done', $data );
  59. //redirect ( site_url ('dashboard') );
  60. //p ( $userdata_check );
  61. /*CI::library('session')->set_userdata ( 'the_user', $userdata_check );
  62. $user_session = array ();
  63. $user_session ['is_logged'] = 'yes';
  64. $user_session ['user_id'] = $userdata_check ['id'];
  65. CI::library('session')->set_userdata ( 'user_session', $userdata_check );*/
  66. //
  67. }
  68. } else {
  69. /*$this->template ['data'] = $data;
  70. $this->load->vars ( $this->template );
  71. $content_filename = $this->load->file ( DEFAULT_TEMPLATE_DIR . 'blocks/users/fb_login.php', true );
  72. print ($content_filename) ;
  73. exit ();*/
  74. /* p ( $data );
  75. CI::library('session')->set_userdata ( 'the_user', $userdata_check );
  76. CI::library('session')->set_userdata ( 'user_session', $user_session );*/
  77. //CI::library('session')->unset_userdata('the_user');
  78. // CI::library('session')->unset_userdata('user_session');
  79. CI::view ( 'fb_login', $data );
  80. }
  81. }
  82. //This won't destroy your facebook session
  83. function logout() {
  84. $this->load->library ( 'session' );
  85. CI::library('session')->sess_destroy ();
  86. //var_dump($_COOKIE);
  87. if (! empty ( $_COOKIE )) {
  88. foreach ( $_COOKIE as $k => $v ) {
  89. //setcookie ( $k );
  90. if (stristr ( $k, 'fbs_' )) {
  91. //var_dump($v);
  92. //setcookie( $k);
  93. }
  94. }
  95. }
  96. //remove all the variables in the session
  97. session_unset ();
  98. session_destroy ();
  99. $data ['logged_out'] = TRUE;
  100. //CI::view ( 'fb_login', $data );
  101. } // function logout()
  102. function _facebook_validate($uid = 0) {
  103. //this query basically sees if the users facebook user id is associated with a user.
  104. $bQry = CI::model('users')->validate_user_facebook ( $uid );
  105. if ($bQry) { // if the user's credentials validated...
  106. $data = array ('user_id' => $uid, 'is_logged_in' => true, 'list_type' => 'hot' );
  107. CI::library('session')->set_userdata ( $data );
  108. $uri_var = $this->uri->segment ( 3 );
  109. if (strlen ( $uri_var ) > 0) {
  110. $url_location = $uri_var;
  111. $url_location = str_replace ( '-', '/', $url_location );
  112. redirect ( $url_location );
  113. } else {
  114. redirect ( '/message/index' );
  115. }
  116. } else {
  117. // incorrect username or password
  118. $data = array ();
  119. $data ["login_failed"] = TRUE;
  120. $this->index ( $data );
  121. }
  122. }
  123. function validate_user_facebook($uid = 0) {
  124. //confirm that facebook session data is still valid and matches
  125. $this->load->library ( 'fb_connect' );
  126. //see if the facebook session is valid and the user id in the sesison is equal to the user_id you want to validate
  127. $session_uid = 'fb:' . $this->fb_connect->fbSession ['uid'];
  128. if (! $this->fb_connect->fbSession || $session_uid != $uid) {
  129. return false;
  130. }
  131. }
  132. function facebook() {
  133. //1. Check to see if the facebook session has been declared
  134. $this->load->library ( 'fb_connect' );
  135. if (! $this->fb_connect->fbSession) {
  136. //2. If No, bounce back to login
  137. $this->index ();
  138. } else {
  139. $fb_uid = $this->fb_connect->user_id;
  140. $fb_usr = $this->fb_connect->user;
  141. if ($fb_uid != false) {
  142. //3. If yes, see if the facebook id is associated with any existing account
  143. $usr = CI::model('users')->get_user_by_fb_uid ( $fb_uid );
  144. if (is_array ( $usr ) && count ( $usr ) == 1) {
  145. $usr = $usr [0]; //the model returns an object array, so get the first elemet of it which contains all of the data we need.
  146. //3.a. if yes, log the person in
  147. //echo "Logging in via facebook...";
  148. $this->_facebook_validate ( $usr->user_id );
  149. } else {
  150. //3.b. if no, register the new user.
  151. //echo "Creating a new account...";
  152. $fname = $fb_usr ["first_name"];
  153. $lname = $fb_usr ["last_name"];
  154. $fullname = $fb_usr ["name"];
  155. $pwd = ''; //left blank so user can modify this later
  156. $email = $fb_usr ["email"];
  157. $db_values = array ('user_id' => "fb:" . $fb_uid, 'fb_uid' => "fb:" . $fb_uid, 'full_name' => $fullname, 'pwd' => "" );
  158. //data ready, try to create the new user
  159. if ($query = CI::model('users')->create_user ( $db_values )) {
  160. $data ['account_created'] = true;
  161. //log user in
  162. $this->_facebook_validate ( $db_values ["user_id"] );
  163. } else {
  164. //Did not work, go back to login page
  165. $this->index ();
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }