PageRenderTime 960ms CodeModel.GetById 35ms RepoModel.GetById 11ms app.codeStats 0ms

/application/controllers/auth_other.php

https://github.com/domjwdavis/XTA2
PHP | 398 lines | 287 code | 35 blank | 76 comment | 59 complexity | 352afe21af7229f30aba503da52c81ae MD5 | raw file
  1. <?php
  2. class auth_other extends CI_Controller
  3. {
  4. function __construct()
  5. {
  6. parent::__construct();
  7. $this->load->model('user_model');
  8. $this->load->model('tank_auth/users');
  9. // for google open id
  10. parse_str($_SERVER['QUERY_STRING'],$_GET);
  11. }
  12. // handle when users log in using facebook account
  13. function fb_signin()
  14. {
  15. // load facebook library
  16. //$this->load->library('facebook'); // this has been loaded in autoload.php
  17. // get the facebook user and save in the session
  18. $fb_user = $this->facebook->getUser();
  19. if( isset($fb_user))
  20. {
  21. $this->session->set_userdata('facebook_id', $fb_user['id']);
  22. $user = $this->user_model->get_user_by_sm(array('facebook_id' => $fb_user['id']), 'facebook_id');
  23. if( sizeof($user) == 0)
  24. {
  25. redirect('auth_other/fill_user_info', 'refresh');
  26. }
  27. else
  28. {
  29. // simulate what happens in the tank auth
  30. $this->session->set_userdata(array( 'user_id' => $user[0]->id, 'username' => $user[0]->username,
  31. 'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
  32. //$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing FB
  33. $this->users->update_login_info( $user[0]->id, $this->config->item('login_record_ip', 'tank_auth'),
  34. $this->config->item('login_record_time', 'tank_auth'));
  35. redirect('auth', 'refresh');
  36. }
  37. }
  38. else
  39. {
  40. echo 'cannot find the Facebook user';
  41. }
  42. }
  43. // function to allow users to log in via twitter
  44. function twitter_signin()
  45. {
  46. if ($this->input->get('denied')) {
  47. echo 'User denied twitter authorization';
  48. // Possibly redirect to home page or show generic deneied message
  49. }else{
  50. // It really is best to auto-load this library!
  51. //$this->load->library('tweet'); // automatically loaded in the autoload!
  52. // Enabling debug will show you any errors in the calls you're making, e.g:
  53. $this->tweet->enable_debug(TRUE);
  54. // If you already have a token saved for your user
  55. // (In a db for example) - See line #37
  56. //
  57. // You can set these tokens before calling logged_in to try using the existing tokens.
  58. // $tokens = array('oauth_token' => 'foo', 'oauth_token_secret' => 'bar');
  59. // $this->tweet->set_tokens($tokens);
  60. if ( !$this->tweet->logged_in() )
  61. {
  62. // This is where the url will go to after auth.
  63. // ( Callback url )
  64. $this->tweet->set_callback(site_url('auth_other/twitter_signin'));
  65. // Send the user off for login!
  66. $this->tweet->login();
  67. }
  68. else
  69. {
  70. // You can get the tokens for the active logged in user:
  71. // $tokens = $this->tweet->get_tokens();
  72. //
  73. // These can be saved in a db alongside a user record
  74. // if you already have your own auth system.
  75. // get the user id from twitter authentication and save to session
  76. $user = $this->tweet->call('get', 'account/verify_credentials');
  77. $twitter_id = $user->id;
  78. $this->session->set_userdata('twitter_id', $twitter_id);
  79. // now see if the user exists with the given twitter id
  80. $user = $this->user_model->get_user_by_sm(array('twitter_id' => $twitter_id), 'twitter_id');
  81. if( sizeof($user) == 0 )
  82. {
  83. redirect('auth_other/fill_user_info', 'refresh');
  84. }
  85. else
  86. {
  87. // simulate what happens in the tank auth
  88. $this->session->set_userdata(array( 'user_id' => $user[0]->id, 'username' => $user[0]->username,
  89. 'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
  90. //$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing twitter
  91. redirect('auth', 'refresh');
  92. }
  93. }
  94. }
  95. }
  96. // handle when users log in using google friend connect
  97. function gfc_signin()
  98. {
  99. // we passed the user id in the URL, let's get it!
  100. $gfc_id = $this->uri->segment(3);
  101. if( !is_null($gfc_id))
  102. {
  103. $this->session->set_userdata('gfc_id', $gfc_id);
  104. $user = $this->user_model->get_user_by_sm(array('gfc_id' => $gfc_id), 'gfc_id');
  105. if( sizeof($user) == 0)
  106. {
  107. redirect('auth_other/fill_user_info', 'refresh');
  108. }
  109. else
  110. {
  111. // simulate what happens in the tank auth
  112. $this->session->set_userdata(array( 'user_id' => $user[0]->id, 'username' => $user[0]->username,
  113. 'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
  114. //$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing FB
  115. $this->users->update_login_info( $user[0]->id, $this->config->item('login_record_ip', 'tank_auth'),
  116. $this->config->item('login_record_time', 'tank_auth'));
  117. redirect('auth', 'refresh');
  118. }
  119. }
  120. else
  121. {
  122. echo 'cannot find the Google Friend Connect ID!';
  123. }
  124. }
  125. // function for logging in via google open id
  126. function google_openid_signin()
  127. {
  128. //$this->load->library('lightopenid'); // loaded in aotoload.php
  129. // these are some of the attributes we can get from open id
  130. // refer to
  131. // http://code.google.com/p/lightopenid/wiki/GettingMoreInformation
  132. // for more
  133. $required_attr = array('namePerson/friendly', 'contact/email',
  134. 'namePerson/first', 'namePerson/last',
  135. 'contact/country/home', 'contact/email', 'pref/language');
  136. try
  137. {
  138. if(!isset($_GET['openid_mode']))
  139. {
  140. $lightopenid = new Lightopenid;
  141. $lightopenid->identity = 'https://www.google.com/accounts/o8/id';
  142. $lightopenid->required = $required_attr;
  143. redirect($lightopenid->authUrl(), 'refresh');
  144. }
  145. elseif($_GET['openid_mode'] == 'cancel')
  146. {
  147. echo 'User has cancelled authentication!';
  148. }
  149. else
  150. {
  151. $lightopenid = new Lightopenid;
  152. $lightopenid->required = $required_attr;
  153. if($lightopenid->validate())
  154. {
  155. #Here goes the code that gets interpreted after successful login!!!
  156. //print_r($lightopenid);
  157. //echo '<br/>';
  158. //print_r($lightopenid->identity);
  159. //print_r($lightopenid->getAttributes());
  160. $google_open_id = $lightopenid->identity;
  161. $this->session->set_userdata('google_open_id', $google_open_id);
  162. // does this user exist?
  163. $user = $this->user_model->get_user_by_sm(array('google_open_id' => $google_open_id), 'google_open_id');
  164. if( sizeof($user) == 0 )
  165. {
  166. redirect('auth_other/fill_user_info', 'refresh');
  167. }
  168. else
  169. {
  170. // simulate what happens in the tank auth
  171. $this->session->set_userdata(array( 'user_id' => $user[0]->id, 'username' => $user[0]->username,
  172. 'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
  173. //$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing twitter
  174. redirect('auth', 'refresh');
  175. }
  176. }
  177. else
  178. {
  179. echo 'User has not logged in.';
  180. }
  181. }
  182. }
  183. catch(ErrorException $e)
  184. {
  185. echo $e->getMessage();
  186. }
  187. }
  188. // function for logging in via google open id
  189. function yahoo_openid_signin()
  190. {
  191. //$this->load->library('lightopenid'); // loaded in aotoload.php
  192. // these are some of the attributes we can get from open id
  193. // refer to
  194. // http://code.google.com/p/lightopenid/wiki/GettingMoreInformation
  195. // for more
  196. $required_attr = array('namePerson/friendly', 'contact/email',
  197. 'namePerson/first', 'namePerson/last',
  198. 'contact/country/home', 'contact/email', 'pref/language');
  199. try
  200. {
  201. if(!isset($_GET['openid_mode']))
  202. {
  203. $lightopenid = new Lightopenid;
  204. $lightopenid->identity = 'http://me.yahoo.com';
  205. $lightopenid->required = $required_attr;
  206. redirect($lightopenid->authUrl(), 'refresh');
  207. }
  208. elseif($_GET['openid_mode'] == 'cancel')
  209. {
  210. echo 'User has cancelled authentication!';
  211. }
  212. else
  213. {
  214. $lightopenid = new Lightopenid;
  215. $lightopenid->required = $required_attr;
  216. if($lightopenid->validate())
  217. {
  218. #Here goes the code that gets interpreted after successful login!!!
  219. //print_r($lightopenid);
  220. //echo '<br/>';
  221. //print_r($lightopenid->identity);
  222. //print_r($lightopenid->getAttributes());
  223. $yahoo_open_id = $lightopenid->identity;
  224. $this->session->set_userdata('yahoo_open_id', $yahoo_open_id);
  225. // does this user exist?
  226. $user = $this->user_model->get_user_by_sm(array('yahoo_open_id' => $yahoo_open_id), 'yahoo_open_id');
  227. if( sizeof($user) == 0 )
  228. {
  229. redirect('auth_other/fill_user_info', 'refresh');
  230. }
  231. else
  232. {
  233. // simulate what happens in the tank auth
  234. $this->session->set_userdata(array( 'user_id' => $user[0]->id, 'username' => $user[0]->username,
  235. 'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
  236. //$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing twitter
  237. redirect('auth', 'refresh');
  238. }
  239. }
  240. else
  241. {
  242. echo 'User has not logged in.';
  243. }
  244. }
  245. }
  246. catch(ErrorException $e)
  247. {
  248. echo $e->getMessage();
  249. }
  250. }
  251. // called when user logs in via facebook/twitter for the first time
  252. function fill_user_info()
  253. {
  254. // load validation library and rules
  255. $this->load->config('tank_auth', TRUE);
  256. $this->load->library('form_validation');
  257. $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|callback_username_check');
  258. $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_email_check');
  259. // Run the validation
  260. if ($this->form_validation->run() == false )
  261. {
  262. $this->load->view('auth_other/fill_user_info');
  263. }
  264. else
  265. {
  266. $username = $this->db->escape_str($this->input->post('username'));
  267. $email = $this->db->escape_str($this->input->post('email'));
  268. /*
  269. * We now must create a new user in tank auth with a random password in order
  270. * to insert this user and also into user profile table with tank auth id
  271. */
  272. $password = $this->generate_password(9, 8);
  273. $data = $this->tank_auth->create_user($username, $email, $password, false);
  274. $user_id = $data['user_id'];
  275. if( $this->session->userdata('facebook_id'))
  276. {
  277. $this->user_model->update_user_profile($user_id, array('facebook_id' => $this->session->userdata('facebook_id')));
  278. }
  279. else if( $this->session->userdata('twitter_id'))
  280. {
  281. $this->user_model->update_user_profile($user_id, array('twitter_id' => $this->session->userdata('twitter_id')));
  282. }
  283. else if( $this->session->userdata('gfc_id'))
  284. {
  285. $this->user_model->update_user_profile($user_id, array('gfc_id' => $this->session->userdata('gfc_id')));
  286. }
  287. else if( $this->session->userdata('google_open_id'))
  288. {
  289. $this->user_model->update_user_profile($user_id, array('google_open_id' => $this->session->userdata('google_open_id')));
  290. }
  291. else if( $this->session->userdata('yahoo_open_id'))
  292. {
  293. $this->user_model->update_user_profile($user_id, array('yahoo_open_id' => $this->session->userdata('yahoo_open_id')));
  294. }
  295. // let the user login via tank auth
  296. $this->tank_auth->login($email, $password, false, false, true);
  297. redirect('auth', 'refresh');
  298. }
  299. }
  300. // a logout function for 3rd party
  301. function logout()
  302. {
  303. $redirect = site_url('auth/logout');
  304. if( $this->session->userdata('gfc_id') && $this->session->userdata('gfc_id') != '') { $redirect = null; }
  305. // set all user data to empty
  306. $this->session->set_userdata(array('facebook_id' => '',
  307. 'twitter_id' => '',
  308. 'gfc_id' => '',
  309. 'google_open_id' => '',
  310. 'yahoo_open_id' => ''));
  311. if( $redirect ) { redirect($redirect, 'refresh'); }
  312. else { $this->load->view('gfc_logout'); }
  313. }
  314. // function to validate the email input field
  315. function email_check($email)
  316. {
  317. $user = $this->users->get_user_by_email($email);
  318. if ( sizeof($user) > 0)
  319. {
  320. $this->form_validation->set_message('email_check', 'This %s is already registered.');
  321. return false;
  322. }
  323. else { return true; }
  324. }
  325. function username_check($username)
  326. {
  327. $user = $this->users->get_user_by_username($username);
  328. if ( sizeof($user) > 0)
  329. {
  330. $this->form_validation->set_message('username_check', 'This %s is already registered.');
  331. return false;
  332. }
  333. else { return true; }
  334. }
  335. // generates a random password for the user
  336. function generate_password($length=9, $strength=0)
  337. {
  338. $vowels = 'aeuy';
  339. $consonants = 'bdghjmnpqrstvz';
  340. if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; }
  341. if ($strength & 2) { $vowels .= "AEUY"; }
  342. if ($strength & 4) { $consonants .= '23456789'; }
  343. if ($strength & 8) { $consonants .= '@#$%'; }
  344. $password = '';
  345. $alt = time() % 2;
  346. for ($i = 0; $i < $length; $i++)
  347. {
  348. if ($alt == 1)
  349. {
  350. $password .= $consonants[(rand() % strlen($consonants))];
  351. $alt = 0;
  352. }
  353. else
  354. {
  355. $password .= $vowels[(rand() % strlen($vowels))];
  356. $alt = 1;
  357. }
  358. }
  359. return $password;
  360. }
  361. }
  362. /* End of file main.php */
  363. /* Location: ./freally_app/controllers/main.php */