function facebook_login(&$vars) { extract($vars); $app_id = environment('facebookAppId'); $consumer_key = environment('facebookKey'); $consumer_secret = environment('facebookSecret'); $agent = environment('facebookAppName') . " (curl)"; add_include_path(library_path()); add_include_path(library_path() . 'facebook-platform/php'); add_include_path(library_path() . 'facebook_stream'); require_once "facebook.php"; require_once "FacebookStream.php"; require_once "Services/Facebook.php"; $fb = new Facebook($consumer_key, $consumer_secret, true); $_SESSION['fb_session'] = (string) $fb->api_client->session_key; $_SESSION['fb_userid'] = (string) $fb->user; $fs = new FacebookStream($consumer_key, $consumer_secret, $agent); $fieldlist = array('last_name', 'first_name', 'pic_small', 'profile_blurb', 'profile_url', 'locale', 'name', 'proxied_email'); $fields = implode(',', $fieldlist); $user = $fs->GetInfo($app_id, $_SESSION['fb_session'], $_SESSION['fb_userid'], $fields); $values = array(); $values[] = str_replace(' ', '', strtolower((string) $user->user->name)); $values[] = (string) $user->user->pic_small; $values[] = (string) $user->user->name; $values[] = (string) $user->user->profile_blurb; $values[] = (string) $user->user->profile_url; $values[] = (string) $user->user->locale; $Identity =& $db->model('Identity'); $Person =& $db->model('Person'); $FacebookUser =& $db->model('FacebookUser'); if (empty($prefix) && in_array('invites', $db->tables)) { $Invite =& $db->model('Invite'); $result = $Invite->find_by('nickname', (string) $user->user->name); if (!$result) { trigger_error('Sorry, you have not been invited yet ' . environment('email_from'), E_USER_ERROR); } } $faceuser = $FacebookUser->find_by('facebook_id', $_SESSION['fb_userid']); // a) facebook user exists, does not have a profile_id // b) facebook user exists, HAS a profile_id // c) facebook user does not exist if ($faceuser) { if (!$faceuser->profile_id) { $i = make_identity($values); if (!$i) { trigger_error('sorry I was unable to create an identity', E_USER_ERROR); } $faceuser->set_value('profile_id', $i->id); $faceuser->save_changes(); if (!$faceuser) { trigger_error('sorry I was unable to create a facebook user', E_USER_ERROR); } } else { // b $i = $Identity->find($faceuser->profile_id); if (!$i) { trigger_error('sorry I was unable to find the identity', E_USER_ERROR); } } } else { // c $i = make_identity($values); if (!$i) { trigger_error('sorry I was unable to create an identity', E_USER_ERROR); } $faceuser = make_fb_user($user, $i->id); if (!$faceuser) { trigger_error('sorry I was unable to create a facebook user', E_USER_ERROR); } } $_SESSION['fb_person_id'] = $i->person_id; redirect_to($request->base); //$fs->setStatus("nerding out with the latest Facebook API tools",$_SESSION['fb_userid']); //$fs->StreamRequest( $app_id, $_SESSION['fb_session'], $_SESSION['fb_userid'] ); }
function add_facebook_user($data) { global $db; $Identity =& $db->model('Identity'); $Person =& $db->model('Person'); $FacebookUser =& $db->model('FacebookUser'); $faceuser = $FacebookUser->find_by('facebook_id', $_SESSION['fb_userid']); if ($faceuser) { if (!$faceuser->profile_id) { $i = make_identity($values); if (!$i) { trigger_error('sorry I was unable to create an identity', E_USER_ERROR); } $faceuser->set_value('profile_id', $i->id); $faceuser->save_changes(); if (!$faceuser) { trigger_error('sorry I was unable to create a facebook user', E_USER_ERROR); } } else { $i = $Identity->find($faceuser->profile_id); if (!$i) { trigger_error('sorry I was unable to find the identity', E_USER_ERROR); } } } else { $i = make_identity($values); if (!$i) { trigger_error('sorry I was unable to create an identity', E_USER_ERROR); } $faceuser = make_fb_user($user, $i->id); } }