Advertisement
Guest User

Untitled

a guest
May 14th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php
  2. class ClubFinder
  3. {
  4.     private $API_KEY = 'Neverrrrrrrrrrrrr';
  5.     private $API_SECRET = 'Neverrrrrrrrrrrrr';
  6.    
  7.     var $fb_install_permissions = array('email','read_stream','publish_stream');
  8.     var $setup_complete = false;
  9.    
  10.     var $user_info = array();
  11.     var $new_user = false;
  12.    
  13.     function setup()
  14.     {
  15.         //Initialize Facebook
  16.         if($this->setup_complete !== true)
  17.         {
  18.             LitePHP::set('Facebook',LitePHP::get('Libary')->load('facebook')->init($this->API_KEY,$this->API_SECRET));
  19.             LitePHP::get('Facebook')->require_login(implode(',',$this->fb_install_permissions));
  20.             //Make sure the frame is in
  21.         }
  22.         return $this->setup_complete = true;
  23.     }
  24.    
  25.     public function getUserInfo()
  26.     {
  27.         if(isset(LitePHP::get('Session')->user->uid))
  28.         {
  29.             $uid = LitePHP::get('Session')->user->uid;
  30.             //user is active!
  31.             if(isset($this->user_data[$uid]))
  32.             {
  33.                 return $this->user_data[$uid];
  34.             }else
  35.             {
  36.                 //Get the users info into an object
  37.                 $sql = sprintf('SELECT * FROM fb_users WHERE fb_users_uid = %d',$uid);
  38.                 LitePHP::get('Database')->query($sql);
  39.                 if(LitePHP::get('Database')->numRows() > 0)
  40.                 {
  41.                     $this->user_data[$uid] = LitePHP::get('Database')->fetchObject();
  42.                 }
  43.             }
  44.             return $this->user_data[$uid];
  45.         }
  46.     }
  47.    
  48.     public function getMutualFriends($uid = false)
  49.     {
  50.         $uid = ($uid !== false ? $uid : LitePHP::get('Facebook')->get_loggedin_user());
  51.         $fql = 'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.(int)$uid.') AND is_app_user = 1';
  52.         $_friends = LitePHP::get('Facebook')->api_client->fql_query($fql);
  53.         $friends = array();
  54.         if(is_array($_friends) && count($_friends))
  55.         {
  56.             foreach ($_friends as $friend)
  57.             {
  58.                 $friends[] = $friend['uid'];
  59.             }
  60.         }
  61.         return $friends;
  62.     }
  63.    
  64.     public function processUser($second_pass = false)
  65.     {
  66.         //check logged in
  67.         $this->setup();
  68.         $uid = LitePHP::get('Facebook')->get_loggedin_user();
  69.        
  70.         //Check against DB
  71.         $query = sprintf('SELECT fb_users_uid AS uid,fb_users_fbuid AS fbuid FROM fb_users WHERE fb_users_fbuid = %d',$uid);       
  72.         LitePHP::get('Database')->query($query);
  73.         if(LitePHP::get('Database')->numRows() == 0)
  74.         {
  75.             //User does not exists so lets grab it
  76.             if($second_pass === true)
  77.             {
  78.                 die('Second Pass should not get here!');
  79.             }
  80.            
  81.             //Check to see if theres been a removal
  82.             if(isset(LitePHP::get('Session')->user->fbuid))
  83.             {
  84.                 if(LitePHP::get('Session')->user->fbuid != $uid)
  85.                 {
  86.                     LitePHP::get('Session')->user = null;
  87.                     $this->processUser(true);
  88.                 }
  89.             }
  90.             //Get and add user info to DB and set up the variables
  91.             $user_info = LitePHP::get('Facebook')->api_client->users_getInfo($uid, 'last_name,first_name,locale,hometown_location,timezone');
  92.            
  93.             //Escape facebook data safty
  94.             $user_info = LitePHP::get('Database')->escape($user_info);
  95.            
  96.             //build the insert query
  97.             $cols = array('fb_users_fbuid','fb_users_registration_date','fb_users_firstname','fb_users_lastname','fb_users_locale','fb_users_city','fb_users_state','fb_users_country','fb_users_zip');
  98.             $query = sprintf("INSERT INTO fb_users (%s) VALUES ('%d','%d','%s','%s','%s','%s','%s','%s','%s')",
  99.             implode(',',$cols),
  100.                 $uid,time(),$user_info[0]['first_name'],$user_info[0]['last_name'],$user_info[0]['locale'],$user_info[0]['hometown_location']['city'],
  101.                 $user_info[0]['hometown_location']['state'],$user_info[0]['hometown_location']['country'],$user_info[0]['hometown_location']['zip']
  102.             );
  103.            
  104.             //Ok so lets execute the query!
  105.             LitePHP::get('Database')->query($query);
  106.            
  107.             //Run this function to log user in!
  108.             $this->processUser(true);
  109.            
  110.             //Return a notice to the caller to allow a redirect for install mode!
  111.             return $this->new_user = true;
  112.         }else
  113.         {
  114.             $userdata = LitePHP::get('Database')->fetchObject();
  115.             //Just have to make sure the sessions is ok!
  116.            
  117.             LitePHP::get('Session')->user = new stdClass();
  118.             LitePHP::get('Session')->user->logged_in = true;
  119.             LitePHP::get('Session')->user->uid = $userdata->uid;
  120.             LitePHP::get('Session')->user->fbuid = $userdata->fbuid;
  121.             return true;
  122.         }
  123.     }
  124.    
  125.     public function GetLocationId($street,$town,$county)
  126.     {
  127.         $params = array('s' => $street,'t' => $town,'c' => $county);
  128.        
  129.         //security
  130.         $params = LitePHP::get('Database')->escape($params);
  131.         $sql = "SELECT location_id FROM locations WHERE location_street = '%s' AND location_town = '%s' AND location_county = '%s' LIMIT 1";
  132.         $sql = sprintf($sql,$params['s'],$params['t'],$params['c']);
  133.         LitePHP::get('Database')->query($sql);
  134.        
  135.         if(LitePHP::get('Database')->numRows() > 0)
  136.         {
  137.             return LitePHP::get('Database')->fetchObject()->location_id;
  138.         }
  139.         //Insert Mode
  140.         $sql = "INSERT INTO locations (location_street,location_town,location_county) VALUES ('%s','%s','%s')";
  141.         $sql = sprintf($sql,$params['s'],$params['t'],$params['c']);
  142.         LitePHP::get('Database')->query($sql);
  143.         return LitePHP::get('Database')->insertId();
  144.     }
  145. }
  146.  
  147. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement