XTA2 /application/controllers/auth_other.php

Language PHP Lines 398
MD5 Hash 352afe21af7229f30aba503da52c81ae
Repository https://github.com/domjwdavis/XTA2.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php

class auth_other extends CI_Controller 
{	
	function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
		$this->load->model('tank_auth/users');

		// for google open id
        parse_str($_SERVER['QUERY_STRING'],$_GET);		
	}
	
	// handle when users log in using facebook account
	function fb_signin()
	{
		// load facebook library
		//$this->load->library('facebook'); // this has been loaded in autoload.php
		
		// get the facebook user and save in the session
		$fb_user = $this->facebook->getUser();
		if( isset($fb_user))
		{
			$this->session->set_userdata('facebook_id', $fb_user['id']);
			$user = $this->user_model->get_user_by_sm(array('facebook_id' => $fb_user['id']), 'facebook_id');
			if( sizeof($user) == 0) 
			{ 
				redirect('auth_other/fill_user_info', 'refresh'); 
			}
			else
			{
				// simulate what happens in the tank auth
				$this->session->set_userdata(array(	'user_id' => $user[0]->id, 'username' => $user[0]->username,
													'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
				//$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing FB
				$this->users->update_login_info( $user[0]->id, $this->config->item('login_record_ip', 'tank_auth'), 
												 $this->config->item('login_record_time', 'tank_auth'));
				redirect('auth', 'refresh');
			}
		}
		else 
		{ 
			echo 'cannot find the Facebook user'; 
		}
	}
	
	// function to allow users to log in via twitter
	function twitter_signin()
	{
		
		if ($this->input->get('denied')) {
			
			echo 'User denied twitter authorization';
			// Possibly redirect to home page or show generic deneied message
			
		}else{
		
			// It really is best to auto-load this library!
			//$this->load->library('tweet'); // automatically loaded in the autoload!
			
			// Enabling debug will show you any errors in the calls you're making, e.g:
			$this->tweet->enable_debug(TRUE);
			
			// If you already have a token saved for your user
			// (In a db for example) - See line #37
			// 
			// You can set these tokens before calling logged_in to try using the existing tokens.
			// $tokens = array('oauth_token' => 'foo', 'oauth_token_secret' => 'bar');
			// $this->tweet->set_tokens($tokens);
			if ( !$this->tweet->logged_in() )
			{
				// This is where the url will go to after auth.
				// ( Callback url )
				
				$this->tweet->set_callback(site_url('auth_other/twitter_signin'));
				
				// Send the user off for login!
				$this->tweet->login();
			}
			else
			{
				// You can get the tokens for the active logged in user:
				// $tokens = $this->tweet->get_tokens();
				
				// 
				// These can be saved in a db alongside a user record
				// if you already have your own auth system.
				
				// get the user id from twitter authentication and save to session
				$user = $this->tweet->call('get', 'account/verify_credentials');
				$twitter_id = $user->id;
				$this->session->set_userdata('twitter_id', $twitter_id);	
				
				// now see if the user exists with the given twitter id	
				$user = $this->user_model->get_user_by_sm(array('twitter_id' => $twitter_id), 'twitter_id');
				if( sizeof($user) == 0 ) 
				{ 
					redirect('auth_other/fill_user_info', 'refresh'); 
				}
				else
				{
					// simulate what happens in the tank auth
					$this->session->set_userdata(array(	'user_id' => $user[0]->id, 'username' => $user[0]->username,
														'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
					//$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing twitter
					redirect('auth', 'refresh');	
				}			
			}
		
		}
	}
	
	// handle when users log in using google friend connect
	function gfc_signin()
	{
		// we passed the user id in the URL, let's get it!
		$gfc_id = $this->uri->segment(3);
		if( !is_null($gfc_id))
		{
			$this->session->set_userdata('gfc_id', $gfc_id);
			$user = $this->user_model->get_user_by_sm(array('gfc_id' => $gfc_id), 'gfc_id');
			if( sizeof($user) == 0) 
			{ 
				redirect('auth_other/fill_user_info', 'refresh'); 
			}
			else
			{
				// simulate what happens in the tank auth
				$this->session->set_userdata(array(	'user_id' => $user[0]->id, 'username' => $user[0]->username,
													'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
				//$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing FB
				$this->users->update_login_info( $user[0]->id, $this->config->item('login_record_ip', 'tank_auth'), 
												 $this->config->item('login_record_time', 'tank_auth'));
				redirect('auth', 'refresh');
			}
		}
		else 
		{ 
			echo 'cannot find the Google Friend Connect ID!'; 
		}
	}

	// function for logging in via google open id
    function google_openid_signin()
    {
    	//$this->load->library('lightopenid'); // loaded in aotoload.php
    	
    	// these are some of the attributes we can get from open id
    	// refer to 
    	// http://code.google.com/p/lightopenid/wiki/GettingMoreInformation
    	// for more
    	$required_attr = array('namePerson/friendly', 'contact/email', 
    						   'namePerson/first', 'namePerson/last', 
    						   'contact/country/home', 'contact/email', 'pref/language');
    	try 
    	{
			if(!isset($_GET['openid_mode'])) 
    		{
    			$lightopenid = new Lightopenid;
    			$lightopenid->identity = 'https://www.google.com/accounts/o8/id';
    			$lightopenid->required = $required_attr;
    			redirect($lightopenid->authUrl(), 'refresh');
    		}
    		elseif($_GET['openid_mode'] == 'cancel')
    		{
    			echo 'User has cancelled authentication!';
    		}
    		else 
    		{
    			$lightopenid = new Lightopenid;
    			$lightopenid->required = $required_attr;
    			if($lightopenid->validate())
    			{
    				#Here goes the code that gets interpreted after successful login!!!
    				//print_r($lightopenid);
    				//echo '<br/>';
    				//print_r($lightopenid->identity);
    				//print_r($lightopenid->getAttributes());
    				
    				
					$google_open_id = $lightopenid->identity;
					$this->session->set_userdata('google_open_id', $google_open_id);	
					    				
    				// does this user exist?
					$user = $this->user_model->get_user_by_sm(array('google_open_id' => $google_open_id), 'google_open_id');
					if( sizeof($user) == 0 ) 
					{ 
						redirect('auth_other/fill_user_info', 'refresh'); 
					}
					else
					{
						// simulate what happens in the tank auth
						$this->session->set_userdata(array(	'user_id' => $user[0]->id, 'username' => $user[0]->username,
															'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
						//$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing twitter
						redirect('auth', 'refresh');	
					}
    			}
    			else 
    			{
    				echo 'User has not logged in.';
    			}
    		}
    	}
    	catch(ErrorException $e) 
    	{
    		echo $e->getMessage();
    	}
    }
    
	// function for logging in via google open id
    function yahoo_openid_signin()
    {
    	//$this->load->library('lightopenid'); // loaded in aotoload.php
    	
    	// these are some of the attributes we can get from open id
    	// refer to 
    	// http://code.google.com/p/lightopenid/wiki/GettingMoreInformation
    	// for more
    	$required_attr = array('namePerson/friendly', 'contact/email', 
    						   'namePerson/first', 'namePerson/last', 
    						   'contact/country/home', 'contact/email', 'pref/language');
    	try 
    	{
			if(!isset($_GET['openid_mode'])) 
    		{
    			$lightopenid = new Lightopenid;
    			$lightopenid->identity = 'http://me.yahoo.com';
    			$lightopenid->required = $required_attr;
    			redirect($lightopenid->authUrl(), 'refresh');
    		}
    		elseif($_GET['openid_mode'] == 'cancel')
    		{
    			echo 'User has cancelled authentication!';
    		}
    		else 
    		{
    			$lightopenid = new Lightopenid;
    			$lightopenid->required = $required_attr;
    			if($lightopenid->validate())
    			{
    				#Here goes the code that gets interpreted after successful login!!!
    				//print_r($lightopenid);
    				//echo '<br/>';
    				//print_r($lightopenid->identity);
    				//print_r($lightopenid->getAttributes());
    				
    				$yahoo_open_id = $lightopenid->identity;
					$this->session->set_userdata('yahoo_open_id', $yahoo_open_id);	
					    				
    				// does this user exist?
					$user = $this->user_model->get_user_by_sm(array('yahoo_open_id' => $yahoo_open_id), 'yahoo_open_id');
					if( sizeof($user) == 0 ) 
					{ 
						redirect('auth_other/fill_user_info', 'refresh'); 
					}
					else
					{
						// simulate what happens in the tank auth
						$this->session->set_userdata(array(	'user_id' => $user[0]->id, 'username' => $user[0]->username,
															'status' => ($user[0]->activated == 1) ? STATUS_ACTIVATED : STATUS_NOT_ACTIVATED));
						//$this->tank_auth->clear_login_attempts($user[0]->email); can't run this when doing twitter
						redirect('auth', 'refresh');	
					}
    			}
    			else 
    			{
    				echo 'User has not logged in.';
    			}
    		}
    	}
    	catch(ErrorException $e) 
    	{
    		echo $e->getMessage();
    	}
    }    
	
	// called when user logs in via facebook/twitter for the first time
	function fill_user_info()
	{
		// load validation library and rules
		$this->load->config('tank_auth', TRUE);
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|min_length['.$this->config->item('username_min_length', 'tank_auth').']|callback_username_check');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email|callback_email_check');
		
		// Run the validation
		if ($this->form_validation->run() == false ) 
		{
			$this->load->view('auth_other/fill_user_info'); 
		}
		else
		{
			$username   =   $this->db->escape_str($this->input->post('username'));
			$email      =   $this->db->escape_str($this->input->post('email'));
			
			/*
			 * We now must create a new user in tank auth with a random password in order
			 * to insert this user and also into user profile table with tank auth id
			 */
			$password = $this->generate_password(9, 8);
			$data = $this->tank_auth->create_user($username, $email, $password, false);
			$user_id = $data['user_id'];
			if( $this->session->userdata('facebook_id')) 
			{ 
				$this->user_model->update_user_profile($user_id, array('facebook_id' => $this->session->userdata('facebook_id')));
			}
			else if( $this->session->userdata('twitter_id'))
			{
				$this->user_model->update_user_profile($user_id, array('twitter_id' => $this->session->userdata('twitter_id')));
			}
			else if( $this->session->userdata('gfc_id'))
			{
				$this->user_model->update_user_profile($user_id, array('gfc_id' => $this->session->userdata('gfc_id')));
			}
			else if( $this->session->userdata('google_open_id'))
			{
				$this->user_model->update_user_profile($user_id, array('google_open_id' => $this->session->userdata('google_open_id')));
			}
			else if( $this->session->userdata('yahoo_open_id'))
			{
				$this->user_model->update_user_profile($user_id, array('yahoo_open_id' => $this->session->userdata('yahoo_open_id')));
			}			
			// let the user login via tank auth
			$this->tank_auth->login($email, $password, false, false, true);
			redirect('auth', 'refresh');
		}
	}
	
	// a logout function for 3rd party
	function logout()
	{
		$redirect = site_url('auth/logout');
		if( $this->session->userdata('gfc_id') && $this->session->userdata('gfc_id') != '') { $redirect = null; }
		
		// set all user data to empty
		$this->session->set_userdata(array('facebook_id' => '', 
										   'twitter_id' => '', 
										   'gfc_id' => '',
										   'google_open_id' => '',
										   'yahoo_open_id' => ''));
		if( $redirect ) { redirect($redirect, 'refresh'); } 
		else { $this->load->view('gfc_logout'); }
	}
		
	// function to validate the email input field
	function email_check($email)
	{
		$user = $this->users->get_user_by_email($email);
		if ( sizeof($user) > 0) 
		{
			$this->form_validation->set_message('email_check', 'This %s is already registered.');
			return false;
		}
		else { return true; }
	}
	function username_check($username)
	{
		$user = $this->users->get_user_by_username($username);
		if ( sizeof($user) > 0) 
		{
			$this->form_validation->set_message('username_check', 'This %s is already registered.');
			return false;
		}
		else { return true; }
	}
	// generates a random password for the user
	function generate_password($length=9, $strength=0) 
	{
		$vowels = 'aeuy';
		$consonants = 'bdghjmnpqrstvz';
		if ($strength & 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; }
		if ($strength & 2) { $vowels .= "AEUY"; }
		if ($strength & 4) { $consonants .= '23456789'; }
		if ($strength & 8) { $consonants .= '@#$%'; }
	 
		$password = '';
		$alt = time() % 2;
		for ($i = 0; $i < $length; $i++) 
		{
			if ($alt == 1) 
			{
				$password .= $consonants[(rand() % strlen($consonants))];
				$alt = 0;
			} 
			else 
			{
				$password .= $vowels[(rand() % strlen($vowels))];
				$alt = 1;
			}
		}
		return $password;
	}
}

/* End of file main.php */
/* Location: ./freally_app/controllers/main.php */
Back to Top