<?php
/**
* StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2010, StatusNet, Inc.
+ * Copyright (C) 2010-2011, StatusNet, Inc.
*
* A plugin for integrating Facebook with StatusNet. Includes single-sign-on
* and publishing notices to Facebook using Facebook's Graph API.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * @category Pugin
+ * @category Plugin
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2011 StatusNet, Inc.
* @category Plugin
* @package StatusNet
* @author Zach Copley <zach@status.net>
- * @copyright 2010 StatusNet, Inc.
+ * @copyright 2010-2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
{
$dir = dirname(__FILE__);
- //common_debug("class = " . $cls);
-
switch ($cls)
{
case 'Facebook': // Facebook PHP SDK
+ include_once $dir . '/extlib/base_facebook.php';
include_once $dir . '/extlib/facebook.php';
return false;
case 'FacebookloginAction':
$action->script('https://connect.facebook.net/en_US/all.js');
$script = <<<ENDOFSCRIPT
-FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
+function setCookie(name, value) {
+ var date = new Date();
+ date.setTime(date.getTime() + (5 * 60 * 1000)); // 5 mins
+ var expires = "; expires=" + date.toGMTString();
+ document.cookie = name + "=" + value + expires + "; path=/";
+}
+
+FB.init({appId: %1\$s, status: true, cookie: true, xfbml: true, oauth: true});
$('#facebook_button').bind('click', function(event) {
event.preventDefault();
FB.login(function(response) {
- if (response.session && response.perms) {
- window.location.href = '%3\$s';
+ if (response.authResponse) {
+ // put the access token in a cookie for the next step
+ setCookie('fb_access_token', response.authResponse.accessToken);
+ window.location.href = '%2\$s';
} else {
// NOP (user cancelled login)
}
- }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
+ }, {scope:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
});
ENDOFSCRIPT;
$action->inlineScript(
- sprintf($script,
+ sprintf(
+ $script,
json_encode($this->facebook->getAppId()),
- json_encode($this->facebook->getSession()),
common_local_url('facebookfinishlogin')
)
);
*
* @param Action action the current action
*/
- function onEndLogout($action)
+ function onStartLogout($action)
{
if ($this->hasApplication()) {
- $session = $this->facebook->getSession();
- $fbuser = null;
- $fbuid = null;
-
- if ($session) {
- try {
- $fbuid = $this->facebook->getUser();
- $fbuser = $this->facebook->api('/me');
- } catch (FacebookApiException $e) {
- common_log(LOG_ERROR, $e, __FILE__);
- }
- }
- if (!empty($fbuser)) {
+ $cur = common_current_user();
+ $flink = Foreign_link::getByUserID($cur->id, FACEBOOK_SERVICE);
+
+ if (!empty($flink)) {
+
+ $this->facebook->setAccessToken($flink->credentials);
+
+ if (common_config('singleuser', 'enabled')) {
+ $user = User::singleUser();
+
+ $destination = common_local_url(
+ 'showstream',
+ array('nickname' => $user->nickname)
+ );
+ } else {
+ $destination = common_local_url('public');
+ }
$logoutUrl = $this->facebook->getLogoutUrl(
- array('next' => common_local_url('public'))
+ array('next' => $destination)
);
common_log(
),
__FILE__
);
- common_debug("LOGOUT URL = $logoutUrl");
+
+ $action->logout();
+
common_redirect($logoutUrl, 303);
+ return false; // probably never get here, but hey
}
+
+ return true;
}
}
* @category Plugin
* @package StatusNet
* @author Zach Copley <zach@status.net>
- * @copyright 2010 StatusNet, Inc.
+ * @copyright 2010-2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class FacebookfinishloginAction extends Action
{
- private $facebook = null; // Facebook client
- private $fbuid = null; // Facebook user ID
- private $fbuser = null; // Facebook user object (JSON)
+ private $fbuid = null; // Facebook user ID
+ private $fbuser = null; // Facebook user object (JSON)
+ private $accessToken = null; // Access token provided by Facebook JS API
function prepare($args) {
parent::prepare($args);
- $this->facebook = new Facebook(
- array(
- 'appId' => common_config('facebook', 'appid'),
- 'secret' => common_config('facebook', 'secret'),
- 'cookie' => true,
- )
- );
-
- // Check for a Facebook user session
-
- $session = $this->facebook->getSession();
- $me = null;
+ // Check cookie for a valid access_token
- if ($session) {
- try {
- $this->fbuid = $this->facebook->getUser();
- $this->fbuser = $this->facebook->api('/me');
- } catch (FacebookApiException $e) {
- common_log(LOG_ERROR, $e, __FILE__);
+ if (isset($_COOKIE['fb_access_token'])) {
+ $this->accessToken = $_COOKIE['fb_access_token'];
+ if (empty($this->accessToken)) {
+ $this->clientError(_m("Unable to authenticate you with Facebook."));
+ return false;
}
}
+ $graphUrl = 'https://graph.facebook.com/me?access_token=' . urlencode($this->accessToken);
+ $this->fbuser = json_decode(file_get_contents($graphUrl));
+
if (!empty($this->fbuser)) {
+ $this->fbuid = $this->fbuser->id;
// OKAY, all is well... proceed to register
-
- common_debug("Found a valid Facebook user.", __FILE__);
+ return true;
} else {
- // This shouldn't happen in the regular course of things
+ // log badness
list($proxy, $ip) = common_client_ip();
);
}
- return true;
+ return false;
}
function handle($args)
if (common_is_real_login()) {
- // User is already logged in, are her accounts already linked?
+ // This will throw a client exception if the user already
+ // has some sort of foreign_link to Facebook.
- $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
+ $this->checkForExistingLink();
- if (!empty($flink)) {
+ // Possibly reconnect an existing account
- // User already has a linked Facebook account and shouldn't be here!
+ $this->connectUser();
- common_debug(
- sprintf(
- 'There\'s already a local user %d linked with Facebook user %s.',
- $flink->user_id,
- $this->fbuid
- )
- );
+ } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+ $this->handlePost();
+ } else {
+ $this->tryLogin();
+ }
+ }
- $this->clientError(
- // TRANS: Client error displayed when trying to connect to a Facebook account that is already linked
- // TRANS: in the same StatusNet site.
- _m('There is already a local account linked with that Facebook account.')
- );
+ function checkForExistingLink() {
- } else {
+ // User is already logged in, are her accounts already linked?
- // Possibly reconnect an existing account
+ $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
- $this->connectUser();
- }
+ if (!empty($flink)) {
- } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $this->handlePost();
- } else {
- $this->tryLogin();
+ // User already has a linked Facebook account and shouldn't be here!
+
+ $this->clientError(
+ // TRANS: Client error displayed when trying to connect to a Facebook account that is already linked
+ // TRANS: in the same StatusNet site.
+ _m('There is already a local account linked with that Facebook account.')
+ );
+
+ return;
+ }
+
+ $cur = common_current_user();
+ $flink = Foreign_link::getByUserID($cur->id, FACEBOOK_SERVICE);
+
+ if (!empty($flink)) {
+
+ // There's already a local user linked to this Facebook account.
+
+ $this->clientError(
+ // TRANS: Client error displayed when trying to connect to a Facebook account that is already linked
+ // TRANS: in the same StatusNet site.
+ _m('There is already a local account linked with that Facebook account.')
+ );
+
+ return;
}
}
{
$token = $this->trimmed('token');
+ // CSRF protection
if (!$token || $token != common_session_token()) {
$this->showForm(
// TRANS: Client error displayed when the session token does not match or is not given.
}
$args = array(
- 'nickname' => $nickname,
- 'fullname' => $this->fbuser['first_name']
- . ' ' . $this->fbuser['last_name'],
- 'homepage' => $this->fbuser['website'],
- 'bio' => $this->fbuser['about'],
- 'location' => $this->fbuser['location']['name']
+ 'nickname' => $nickname,
+ 'fullname' => $this->fbuser->name,
+ 'homepage' => $this->fbuser->website,
+ 'location' => $this->fbuser->location->name
);
// It's possible that the email address is already in our
// DB. It's a unique key, so we need to check
- if ($this->isNewEmail($this->fbuser['email'])) {
- $args['email'] = $this->fbuser['email'];
- $args['email_confirmed'] = true;
+ if ($this->isNewEmail($this->fbuser->email)) {
+ $args['email'] = $this->fbuser->email;
+ if (isset($this->fuser->verified) && $this->fuser->verified == true) {
+ $args['email_confirmed'] = true;
+ }
}
if (!empty($invite)) {
'Registered new user %s (%d) from Facebook user %s, (fbuid %d)',
$user->nickname,
$user->id,
- $this->fbuser['name'],
+ $this->fbuser->name,
$this->fbuid
),
__FILE__
*/
function setAvatar($user)
{
- $picUrl = sprintf(
- 'http://graph.facebook.com/%s/picture?type=large',
- $this->fbuid
- );
-
- // fetch the picture from Facebook
- $client = new HTTPClient();
-
- // fetch the actual picture
- $response = $client->get($picUrl);
+ try {
+ $picUrl = sprintf(
+ 'http://graph.facebook.com/%d/picture?type=large',
+ $this->fbuser->id
+ );
- if ($response->isOk()) {
+ // fetch the picture from Facebook
+ $client = new HTTPClient();
- $finalUrl = $client->getUrl();
+ // fetch the actual picture
+ $response = $client->get($picUrl);
- // Make sure the filename is unique becuase it's possible for a user
- // to deauthorize our app, and then come back in as a new user but
- // have the same Facebook picture (avatar URLs have a unique index
- // and their URLs are based on the filenames).
- $filename = 'facebook-' . common_good_rand(4) . '-'
- . substr(strrchr($finalUrl, '/'), 1);
+ if ($response->isOk()) {
- $ok = file_put_contents(
- Avatar::path($filename),
- $response->getBody()
- );
+ // seems to always be jpeg, but not sure
+ $tmpname = "facebook-avatar-tmp-" . common_good_rand(4);
- if (!$ok) {
- common_log(
- LOG_WARNING,
- sprintf(
- 'Couldn\'t save Facebook avatar %s',
- $tmp
- ),
- __FILE__
+ $ok = file_put_contents(
+ Avatar::path($tmpname),
+ $response->getBody()
);
- } else {
-
- // save it as an avatar
- $profile = $user->getProfile();
-
- if ($profile->setOriginal($filename)) {
- common_log(
- LOG_INFO,
- sprintf(
- 'Saved avatar for %s (%d) from Facebook picture for '
- . '%s (fbuid %d), filename = %s',
- $user->nickname,
- $user->id,
- $this->fbuser['name'],
- $this->fbuid,
- $filename
- ),
- __FILE__
- );
+ if (!$ok) {
+ common_log(LOG_WARNING, 'Couldn\'t save tmp Facebook avatar: ' . $tmpname, __FILE__);
+ } else {
+ // save it as an avatar
+
+ $file = new ImageFile($user->id, Avatar::path($tmpname));
+ $filename = $file->resize(180); // size of the biggest img we get from Facebook
+
+ $profile = $user->getProfile();
+
+ if ($profile->setOriginal($filename)) {
+ common_log(
+ LOG_INFO,
+ sprintf(
+ 'Saved avatar for %s (%d) from Facebook picture for '
+ . '%s (fbuid %d), filename = %s',
+ $user->nickname,
+ $user->id,
+ $this->fbuser->name,
+ $this->fbuid,
+ $filename
+ ),
+ __FILE__
+ );
+
+ // clean up tmp file
+ @unlink(Avatar::path($tmpname));
+ }
+
}
}
+ } catch (Exception $e) {
+ common_log(LOG_WARNING, 'Couldn\'t save Facebook avatar: ' . $e->getMessage(), __FILE__);
+ // error isn't fatal, continue
}
}
$user = User::staticGet('nickname', $nickname);
- if (!empty($user)) {
- common_debug(
- sprintf(
- 'Found a legit user to connect to Facebook: %s (%d)',
- $user->nickname,
- $user->id
- ),
- __FILE__
- );
- }
-
$this->tryLinkUser($user);
common_set_user($user);
common_real_login(true);
+ // clear out the stupid cookie
+ setcookie('fb_access_token', '', time() - 3600); // one hour ago
+
$this->goHome($user->nickname);
}
{
$user = common_current_user();
$this->tryLinkUser($user);
+
+ // clear out the stupid cookie
+ setcookie('fb_access_token', '', time() - 3600); // one hour ago
common_redirect(common_local_url('facebookfinishlogin'), 303);
}
$this->serverError(_m('Error connecting user to Facebook.'));
return;
}
-
- common_debug(
- sprintf(
- 'Connected Facebook user %s (fbuid %d) to local user %s (%d)',
- $this->fbuser['name'],
- $this->fbuid,
- $user->nickname,
- $user->id
- ),
- __FILE__
- );
}
function tryLogin()
{
- common_debug(
- sprintf(
- 'Trying login for Facebook user %s',
- $this->fbuid
- ),
- __FILE__
- );
-
$flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
if (!empty($flink)) {
common_set_user($user);
common_real_login(true);
+
+ // clear out the stupid cookie
+ setcookie('fb_access_token', '', time() - 3600); // one hour ago
+
$this->goHome($user->nickname);
}
} else {
-
- common_debug(
- sprintf(
- 'No flink found for fbuid: %s - new user',
- $this->fbuid
- ),
- __FILE__
- );
-
$this->showForm(null, $this->bestNewNickname());
}
}
function flinkUser($user_id, $fbuid)
{
$flink = new Foreign_link();
- $flink->user_id = $user_id;
- $flink->foreign_id = $fbuid;
- $flink->service = FACEBOOK_SERVICE;
-
- // Pull the access token from the Facebook cookies
- $flink->credentials = $this->facebook->getAccessToken();
- $flink->created = common_sql_now();
+ $flink->user_id = $user_id;
+ $flink->foreign_id = $fbuid;
+ $flink->service = FACEBOOK_SERVICE;
+ $flink->credentials = $this->accessToken;
+ $flink->created = common_sql_now();
$flink_id = $flink->insert();
function bestNewNickname()
{
- if (!empty($this->fbuser['name'])) {
- $nickname = $this->nicknamize($this->fbuser['name']);
+ if (!empty($this->fbuser->username)) {
+ $nickname = $this->nicknamize($this->fbuser->username);
if ($this->isNewNickname($nickname)) {
return $nickname;
}
// Try the full name
- $fullname = trim($this->fbuser['first_name'] .
- ' ' . $this->fbuser['last_name']);
+ $fullname = $this->fbuser->name;
if (!empty($fullname)) {
$fullname = $this->nicknamize($fullname);
$result = User::staticGet('email', $email);
if (empty($result)) {
- common_debug("XXXXXXXXXXXXXXXXXX We've never seen this email before!!!");
return true;
}
- common_debug("XXXXXXXXXXXXXXXXXX dupe email address!!!!");
return false;
}