Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ClubFinder
- {
- private $API_KEY = 'Neverrrrrrrrrrrrr';
- private $API_SECRET = 'Neverrrrrrrrrrrrr';
- var $fb_install_permissions = array('email','read_stream','publish_stream');
- var $setup_complete = false;
- var $user_info = array();
- var $new_user = false;
- function setup()
- {
- //Initialize Facebook
- if($this->setup_complete !== true)
- {
- LitePHP::set('Facebook',LitePHP::get('Libary')->load('facebook')->init($this->API_KEY,$this->API_SECRET));
- LitePHP::get('Facebook')->require_login(implode(',',$this->fb_install_permissions));
- //Make sure the frame is in
- }
- return $this->setup_complete = true;
- }
- public function getUserInfo()
- {
- if(isset(LitePHP::get('Session')->user->uid))
- {
- $uid = LitePHP::get('Session')->user->uid;
- //user is active!
- if(isset($this->user_data[$uid]))
- {
- return $this->user_data[$uid];
- }else
- {
- //Get the users info into an object
- $sql = sprintf('SELECT * FROM fb_users WHERE fb_users_uid = %d',$uid);
- LitePHP::get('Database')->query($sql);
- if(LitePHP::get('Database')->numRows() > 0)
- {
- $this->user_data[$uid] = LitePHP::get('Database')->fetchObject();
- }
- }
- return $this->user_data[$uid];
- }
- }
- public function getMutualFriends($uid = false)
- {
- $uid = ($uid !== false ? $uid : LitePHP::get('Facebook')->get_loggedin_user());
- $fql = 'SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1='.(int)$uid.') AND is_app_user = 1';
- $_friends = LitePHP::get('Facebook')->api_client->fql_query($fql);
- $friends = array();
- if(is_array($_friends) && count($_friends))
- {
- foreach ($_friends as $friend)
- {
- $friends[] = $friend['uid'];
- }
- }
- return $friends;
- }
- public function processUser($second_pass = false)
- {
- //check logged in
- $this->setup();
- $uid = LitePHP::get('Facebook')->get_loggedin_user();
- //Check against DB
- $query = sprintf('SELECT fb_users_uid AS uid,fb_users_fbuid AS fbuid FROM fb_users WHERE fb_users_fbuid = %d',$uid);
- LitePHP::get('Database')->query($query);
- if(LitePHP::get('Database')->numRows() == 0)
- {
- //User does not exists so lets grab it
- if($second_pass === true)
- {
- die('Second Pass should not get here!');
- }
- //Check to see if theres been a removal
- if(isset(LitePHP::get('Session')->user->fbuid))
- {
- if(LitePHP::get('Session')->user->fbuid != $uid)
- {
- LitePHP::get('Session')->user = null;
- $this->processUser(true);
- }
- }
- //Get and add user info to DB and set up the variables
- $user_info = LitePHP::get('Facebook')->api_client->users_getInfo($uid, 'last_name,first_name,locale,hometown_location,timezone');
- //Escape facebook data safty
- $user_info = LitePHP::get('Database')->escape($user_info);
- //build the insert query
- $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');
- $query = sprintf("INSERT INTO fb_users (%s) VALUES ('%d','%d','%s','%s','%s','%s','%s','%s','%s')",
- implode(',',$cols),
- $uid,time(),$user_info[0]['first_name'],$user_info[0]['last_name'],$user_info[0]['locale'],$user_info[0]['hometown_location']['city'],
- $user_info[0]['hometown_location']['state'],$user_info[0]['hometown_location']['country'],$user_info[0]['hometown_location']['zip']
- );
- //Ok so lets execute the query!
- LitePHP::get('Database')->query($query);
- //Run this function to log user in!
- $this->processUser(true);
- //Return a notice to the caller to allow a redirect for install mode!
- return $this->new_user = true;
- }else
- {
- $userdata = LitePHP::get('Database')->fetchObject();
- //Just have to make sure the sessions is ok!
- LitePHP::get('Session')->user = new stdClass();
- LitePHP::get('Session')->user->logged_in = true;
- LitePHP::get('Session')->user->uid = $userdata->uid;
- LitePHP::get('Session')->user->fbuid = $userdata->fbuid;
- return true;
- }
- }
- public function GetLocationId($street,$town,$county)
- {
- $params = array('s' => $street,'t' => $town,'c' => $county);
- //security
- $params = LitePHP::get('Database')->escape($params);
- $sql = "SELECT location_id FROM locations WHERE location_street = '%s' AND location_town = '%s' AND location_county = '%s' LIMIT 1";
- $sql = sprintf($sql,$params['s'],$params['t'],$params['c']);
- LitePHP::get('Database')->query($sql);
- if(LitePHP::get('Database')->numRows() > 0)
- {
- return LitePHP::get('Database')->fetchObject()->location_id;
- }
- //Insert Mode
- $sql = "INSERT INTO locations (location_street,location_town,location_county) VALUES ('%s','%s','%s')";
- $sql = sprintf($sql,$params['s'],$params['t'],$params['c']);
- LitePHP::get('Database')->query($sql);
- return LitePHP::get('Database')->insertId();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement