Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2.  
  3. class user
  4. {
  5.     public $_info = array();
  6.     public $_loggedIn = false;
  7.     public $_loggedInFB = false;
  8.    
  9.     private $_username;
  10.     private $_password;
  11.     private $_facebook_id;
  12.     private $_facebook_info = array();
  13.    
  14.     // what info do we want passed in initially? The current session information
  15.     public function __construct($username='', $password='', $facebook_id='', $facebook_info='')
  16.     {
  17.         $this->_username      = $username;
  18.         $this->_password      = $password;
  19.         $this->_facebook_id   = $facebook_id;
  20.         $this->_facebook_info = $facebook_info;
  21.         $this->_info          = $this->login();
  22.         $this->_loggedIn      = ($this->_info) ? true : false;
  23.     }
  24.    
  25.  
  26.     /**
  27.      *
  28.      *
  29.      * methid to log a user in
  30.      *
  31.      *
  32.      */
  33.     public function login()
  34.     {
  35.         if(!$_SESSION['uinfo'])
  36.         {
  37.             // if user is logging in through facebook check the database to see if they are already registered in our database
  38.             if($this->_facebook_id)
  39.             {
  40.                 $check = $this->getDBInfo('facebook');
  41.                 // if the user has a member id reister the sesssion, other wise do nothing
  42.                 if($check['mem_id'])
  43.                 {
  44.                     $this->_loggedInFB = true;
  45.                     $this->sessionRegister($check);
  46.                 } else {
  47.                    
  48.                     $this->registerFB();
  49.                     $check = $this->getDBInfo('facebook');
  50.                     $this->_loggedInFB = true;
  51.                     $this->sessionRegister($check);
  52.                    
  53.                 }
  54.                
  55.             } elseif($this->_username && $this->_password)
  56.             {
  57.                 // person is entering a username and password lets check it against the database
  58.                 $check = $this->getDBInfo('unp');
  59.                
  60.                 // if there is an id for the user let's set the session up
  61.                 if($check['mem_id'])
  62.                 {
  63.                     $this->sessionRegister($check);
  64.                 }
  65.             }
  66.            
  67.             return $this->_info;
  68.            
  69.         } else {
  70.            
  71.             $this->_loggedInFB = $_SESSION['uinfo']['loggedInFB'];
  72.             return $_SESSION['uinfo'];
  73.         }
  74.     }
  75.    
  76.    
  77.     /**
  78.      *
  79.      *
  80.      * methid to log register the user if they are using facebook to login
  81.      *
  82.      *
  83.      */
  84.     private function registerFB()
  85.     {
  86.            
  87.         // split location
  88.         $fbinfo = $this->_facebook_info;
  89.         $location = explode(',', $fbinfo['location']['name']);
  90.         $city  = addslashes(trim($location[0]));
  91.         $state = addslashes(trim(substr($location[1], 0, 3)));
  92.         $zipinfo = mysql_fetch_array(mysql_query("SELECT * FROM zip_code WHERE city = '".$city."' && state = '".$state."' LIMIT 1"));
  93.        
  94.         // facebok user not in the database, add them
  95.         mysql_query("INSERT INTO members SET
  96.                     mem_email           = '".filter_var($fbinfo['email'], FILTER_SANITIZE_EMAIL)."',
  97.                     mem_real            = '1',
  98.                     mem_ip              = '".$_SERVER['REMOTE_ADDR']."',
  99.                     mem_date_joined     = '".time()."',
  100.                     mem_last_active     = '".time()."',
  101.                     mem_firstname       = '".$this->clean($fbinfo['first_name'])."',
  102.                     mem_lastname        = '".$this->clean($fbinfo['last_name'])."',
  103.                     mem_city            = '".$zipinfo['city']."',
  104.                     mem_state           = '".$zipinfo['state']."',
  105.                     mem_zipcode         = '".$zipinfo['zip_code']."',
  106.                     mem_lat             = '".$zipinfo['lat']."',
  107.                     mem_lon             = '".$zipinfo['lon']."',
  108.                     mem_gender          = '".$this->clean($fbinfo['gender'])."',
  109.                     mem_timezone        = '".$this->clean($fbinfo['timezone'])."',
  110.                     mem_oauth_provider  = 'facebook',
  111.                     mem_oauth_uid       = '".$this->_facebook_id."'");             
  112.        
  113.     }
  114.    
  115.  
  116.     /**
  117.      *
  118.      *
  119.      * methid to log a user out
  120.      *
  121.      *
  122.      */
  123.     public function logout()
  124.     {
  125.         unset($_SESSION[uinfo]);
  126.         $this->_loggedIn = false;
  127.     }  
  128.    
  129.  
  130.     /**
  131.      *
  132.      *
  133.      * methid to register the users session
  134.      *
  135.      *
  136.      */
  137.     private function sessionRegister($memberArray)
  138.     {
  139.         $this->_info = $_SESSION['uinfo'] = array(
  140.                                                  'id'              => $memberArray['mem_id'],
  141.                                                  'email'           => $memberArray['mem_email'],
  142.                                                  'username'        => $memberArray['mem_nick'],
  143.                                                  'active'          => $memberArray['mem_active'],
  144.                                                  'level'           => $memberArray['mem_level'],
  145.                                                  'facebook_id'     => $memberArray['mem_oauth_uid'],
  146.                                                  'loggedInFB'      => $this->_loggedInFB
  147.                                                  );
  148.        
  149.         $this->_loggedIn = true;
  150.        
  151.     }
  152.    
  153.     /**
  154.      *
  155.      *
  156.      * methid to log set a cookie if user wants to be remembered
  157.      *
  158.      *
  159.      */
  160.     public function setCookie()
  161.     {
  162.         setcookie();
  163.     }
  164.    
  165.     /**
  166.      *
  167.      *
  168.      * methid to get users info from the database
  169.      *
  170.      *
  171.      */
  172.     public function getDBInfo($method)
  173.     {
  174.         if($method == 'facebook')
  175.         {
  176.             return mysql_fetch_array(mysql_query("SELECT * FROM members WHERE mem_oauth_provider = 'facebook' && mem_oauth_uid = '".$this->_facebook_id."' LIMIT 1"));
  177.            
  178.         } elseif($method == 'unp') {
  179.            
  180.             $cleanUsername = $this->clean($this->_username);
  181.             $cleanPassword = md5($this->_password);
  182.             return mysql_fetch_array(mysql_query("SELECT * FROM members WHERE mem_nick = '".$cleanUsername."' && mem_password = '".$cleanPassword."' LIMIT 1"));
  183.            
  184.         } else {
  185.            
  186.             return false;
  187.            
  188.         }
  189.     }
  190.    
  191.     /**
  192.      *
  193.      *
  194.      * method to clean information for the database
  195.      *
  196.      *
  197.      */
  198.      private function clean($textToClean)
  199.      {
  200.          return addslashes(filter_var($textToClean, FILTER_SANITIZE_STRING));
  201.      }
  202.    
  203.     /**
  204.      *
  205.      *
  206.      * end class
  207.      *
  208.      *
  209.      */
  210.    
  211. }
  212.  
  213. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement