Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class user
- {
- public $_info = array();
- public $_loggedIn = false;
- public $_loggedInFB = false;
- private $_username;
- private $_password;
- private $_facebook_id;
- private $_facebook_info = array();
- // what info do we want passed in initially? The current session information
- public function __construct($username='', $password='', $facebook_id='', $facebook_info='')
- {
- $this->_username = $username;
- $this->_password = $password;
- $this->_facebook_id = $facebook_id;
- $this->_facebook_info = $facebook_info;
- $this->_info = $this->login();
- $this->_loggedIn = ($this->_info) ? true : false;
- }
- /**
- *
- *
- * methid to log a user in
- *
- *
- */
- public function login()
- {
- if(!$_SESSION['uinfo'])
- {
- // if user is logging in through facebook check the database to see if they are already registered in our database
- if($this->_facebook_id)
- {
- $check = $this->getDBInfo('facebook');
- // if the user has a member id reister the sesssion, other wise do nothing
- if($check['mem_id'])
- {
- $this->_loggedInFB = true;
- $this->sessionRegister($check);
- } else {
- $this->registerFB();
- $check = $this->getDBInfo('facebook');
- $this->_loggedInFB = true;
- $this->sessionRegister($check);
- }
- } elseif($this->_username && $this->_password)
- {
- // person is entering a username and password lets check it against the database
- $check = $this->getDBInfo('unp');
- // if there is an id for the user let's set the session up
- if($check['mem_id'])
- {
- $this->sessionRegister($check);
- }
- }
- return $this->_info;
- } else {
- $this->_loggedInFB = $_SESSION['uinfo']['loggedInFB'];
- return $_SESSION['uinfo'];
- }
- }
- /**
- *
- *
- * methid to log register the user if they are using facebook to login
- *
- *
- */
- private function registerFB()
- {
- // split location
- $fbinfo = $this->_facebook_info;
- $location = explode(',', $fbinfo['location']['name']);
- $city = addslashes(trim($location[0]));
- $state = addslashes(trim(substr($location[1], 0, 3)));
- $zipinfo = mysql_fetch_array(mysql_query("SELECT * FROM zip_code WHERE city = '".$city."' && state = '".$state."' LIMIT 1"));
- // facebok user not in the database, add them
- mysql_query("INSERT INTO members SET
- mem_email = '".filter_var($fbinfo['email'], FILTER_SANITIZE_EMAIL)."',
- mem_real = '1',
- mem_ip = '".$_SERVER['REMOTE_ADDR']."',
- mem_date_joined = '".time()."',
- mem_last_active = '".time()."',
- mem_firstname = '".$this->clean($fbinfo['first_name'])."',
- mem_lastname = '".$this->clean($fbinfo['last_name'])."',
- mem_city = '".$zipinfo['city']."',
- mem_state = '".$zipinfo['state']."',
- mem_zipcode = '".$zipinfo['zip_code']."',
- mem_lat = '".$zipinfo['lat']."',
- mem_lon = '".$zipinfo['lon']."',
- mem_gender = '".$this->clean($fbinfo['gender'])."',
- mem_timezone = '".$this->clean($fbinfo['timezone'])."',
- mem_oauth_provider = 'facebook',
- mem_oauth_uid = '".$this->_facebook_id."'");
- }
- /**
- *
- *
- * methid to log a user out
- *
- *
- */
- public function logout()
- {
- unset($_SESSION[uinfo]);
- $this->_loggedIn = false;
- }
- /**
- *
- *
- * methid to register the users session
- *
- *
- */
- private function sessionRegister($memberArray)
- {
- $this->_info = $_SESSION['uinfo'] = array(
- 'id' => $memberArray['mem_id'],
- 'email' => $memberArray['mem_email'],
- 'username' => $memberArray['mem_nick'],
- 'active' => $memberArray['mem_active'],
- 'level' => $memberArray['mem_level'],
- 'facebook_id' => $memberArray['mem_oauth_uid'],
- 'loggedInFB' => $this->_loggedInFB
- );
- $this->_loggedIn = true;
- }
- /**
- *
- *
- * methid to log set a cookie if user wants to be remembered
- *
- *
- */
- public function setCookie()
- {
- setcookie();
- }
- /**
- *
- *
- * methid to get users info from the database
- *
- *
- */
- public function getDBInfo($method)
- {
- if($method == 'facebook')
- {
- return mysql_fetch_array(mysql_query("SELECT * FROM members WHERE mem_oauth_provider = 'facebook' && mem_oauth_uid = '".$this->_facebook_id."' LIMIT 1"));
- } elseif($method == 'unp') {
- $cleanUsername = $this->clean($this->_username);
- $cleanPassword = md5($this->_password);
- return mysql_fetch_array(mysql_query("SELECT * FROM members WHERE mem_nick = '".$cleanUsername."' && mem_password = '".$cleanPassword."' LIMIT 1"));
- } else {
- return false;
- }
- }
- /**
- *
- *
- * method to clean information for the database
- *
- *
- */
- private function clean($textToClean)
- {
- return addslashes(filter_var($textToClean, FILTER_SANITIZE_STRING));
- }
- /**
- *
- *
- * end class
- *
- *
- */
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement