gnu-social/plugins/FacebookSSO/actions/facebooklogin.php

172 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* An action for logging in with Facebook
*
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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 Plugin
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
class FacebookloginAction extends Action
{
function handle($args)
{
parent::handle($args);
if (common_is_real_login()) {
2010-11-02 01:41:31 +00:00
$this->clientError(_m('Already logged in.'));
2010-11-02 01:41:31 +00:00
} else {
$facebook = new Facebook(
array(
'appId' => common_config('facebook', 'appid'),
'secret' => common_config('facebook', 'secret'),
'cookie' => true,
)
);
$session = $facebook->getSession();
$fbuser = null;
2010-11-02 01:41:31 +00:00
if ($session) {
try {
$fbuid = $facebook->getUser();
$fbuser = $facebook->api('/me');
} catch (FacebookApiException $e) {
common_log(LOG_ERROR, $e);
}
}
if (!empty($fbuser)) {
common_debug("Found a valid Facebook user", __FILE__);
// Check to see if we have a foreign link already
$flink = Foreign_link::getByForeignId($fbuid, FACEBOOK_SERVICE);
if (empty($flink)) {
// See if the user would like to register a new local
// account
common_redirect(
common_local_url('facebookregister'),
303
);
} else {
// Log our user in!
$user = $flink->getUser();
if (!empty($user)) {
common_log(
LOG_INFO,
2010-11-02 01:41:31 +00:00
sprintf(
'Logged in Facebook user %s as user %s (%s)',
$fbuid,
2010-11-02 01:41:31 +00:00
$user->id,
$user->nickname
),
__FILE__
);
common_set_user($user);
common_real_login(true);
$this->goHome($user->nickname);
}
}
}
}
2010-11-02 01:41:31 +00:00
$this->showPage();
}
2010-11-02 01:41:31 +00:00
function goHome($nickname)
{
$url = common_get_returnto();
if ($url) {
// We don't have to return to it again
common_set_returnto(null);
} else {
$url = common_local_url(
'all',
array('nickname' => $nickname)
);
}
common_redirect($url, 303);
}
function getInstructions()
{
// TRANS: Instructions.
return _m('Login with your Facebook Account');
}
function showPageNotice()
{
$instr = $this->getInstructions();
$output = common_markup_to_html($instr);
$this->elementStart('div', 'instructions');
$this->raw($output);
$this->elementEnd('div');
}
function title()
{
// TRANS: Page title.
return _m('Login with Facebook');
}
function showContent() {
$this->elementStart('fieldset');
2010-11-02 01:41:31 +00:00
$attrs = array(
//'show-faces' => 'true',
//'max-rows' => '4',
//'width' => '600',
2010-11-02 01:41:31 +00:00
'perms' => 'user_location,user_website,offline_access,publish_stream'
);
$this->element('fb:login-button', $attrs);
$this->elementEnd('fieldset');
}
function showLocalNav()
{
$nav = new LoginGroupNav($this);
$nav->show();
}
}