2008-12-06 03:01:32 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Laconica - a distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2008, Controlez-Vous, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
2008-12-23 19:19:07 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-12-06 03:01:32 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2008-12-23 19:19:07 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-06 03:01:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('LACONICA')) { exit(1); }
|
|
|
|
|
2009-01-18 01:31:39 +00:00
|
|
|
require_once INSTALLDIR.'/lib/facebookaction.php';
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2008-12-23 19:49:23 +00:00
|
|
|
class FacebookhomeAction extends FacebookAction
|
|
|
|
{
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2008-12-23 19:33:23 +00:00
|
|
|
function handle($args)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
parent::handle($args);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-05 01:04:07 +00:00
|
|
|
$facebook = get_facebook();
|
2008-12-23 19:19:07 +00:00
|
|
|
$fbuid = $facebook->require_login();
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
// Check to see whether there's already a Facebook link for this user
|
|
|
|
$flink = Foreign_link::getByForeignID($fbuid, FACEBOOK_SERVICE);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-19 00:50:45 +00:00
|
|
|
// If the user has opted not to initially allow the app to have
|
|
|
|
// Facebook status update permission, store that preference. Only
|
|
|
|
// promt the user the first time she uses the app
|
|
|
|
if ($this->arg('skip')) {
|
|
|
|
$facebook->api_client->data_setUserPreference(
|
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($flink) {
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 13:35:17 +00:00
|
|
|
if ($_POST['submit'] == 'Send') {
|
2009-01-19 07:24:12 +00:00
|
|
|
$this->saveNewNotice($flink);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-01-18 01:31:39 +00:00
|
|
|
$user = $flink->getUser();
|
|
|
|
common_set_user($user);
|
2009-01-19 00:50:45 +00:00
|
|
|
|
|
|
|
// If this is the first time the user has started the app
|
|
|
|
// prompt for Facebook status update permission
|
|
|
|
if (!$facebook->api_client->users_hasAppPermission('status_update')) {
|
|
|
|
|
|
|
|
if ($facebook->api_client->data_getUserPreference(
|
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
|
|
|
|
$this->getUpdatePermission();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use is authenticated and has already been prompted once for
|
|
|
|
// Facebook status update permission? Then show the main page
|
|
|
|
// of the app
|
2009-01-11 07:03:59 +00:00
|
|
|
$this->showHome($flink, null);
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
} else {
|
2009-01-19 00:50:45 +00:00
|
|
|
|
|
|
|
// User hasn't authenticated yet, prompt for creds
|
2009-01-11 07:03:59 +00:00
|
|
|
$this->login($fbuid);
|
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
function login($fbuid)
|
|
|
|
{
|
|
|
|
$nickname = common_canonical_nickname($this->trimmed('nickname'));
|
|
|
|
$password = $this->arg('password');
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
$msg = null;
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
if ($nickname) {
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
if (common_check_user($nickname, $password)) {
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
$user = User::staticGet('nickname', $nickname);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
if (!$user) {
|
|
|
|
$this->showLoginForm(_("Server error - couldn't get user!"));
|
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
$flink = DB_DataObject::factory('foreign_link');
|
|
|
|
$flink->user_id = $user->id;
|
|
|
|
$flink->foreign_id = $fbuid;
|
|
|
|
$flink->service = FACEBOOK_SERVICE;
|
|
|
|
$flink->created = common_sql_now();
|
|
|
|
$flink->set_flags(true, false, false);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
$flink_id = $flink->insert();
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
// XXX: Do some error handling here
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-12 05:25:07 +00:00
|
|
|
$this->setDefaults();
|
2009-01-19 00:50:45 +00:00
|
|
|
//$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-19 00:50:45 +00:00
|
|
|
$this->getUpdatePermission();
|
2009-01-16 20:19:49 +00:00
|
|
|
return;
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
} else {
|
|
|
|
$msg = _('Incorrect username or password.');
|
|
|
|
}
|
2008-12-23 19:19:07 +00:00
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
$this->showLoginForm($msg);
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-12 05:25:07 +00:00
|
|
|
function setDefaults()
|
|
|
|
{
|
|
|
|
$facebook = get_facebook();
|
|
|
|
|
|
|
|
// A default prefix string for notices
|
2009-01-19 00:50:45 +00:00
|
|
|
$facebook->api_client->data_setUserPreference(
|
|
|
|
FACEBOOK_NOTICE_PREFIX, 'dented: ');
|
|
|
|
$facebook->api_client->data_setUserPreference(
|
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
|
2009-01-12 05:25:07 +00:00
|
|
|
}
|
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
function showHome($flink, $msg)
|
2008-12-23 19:33:23 +00:00
|
|
|
{
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-11 08:33:28 +00:00
|
|
|
$facebook = get_facebook();
|
|
|
|
$fbuid = $facebook->require_login();
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-11 08:33:28 +00:00
|
|
|
$user = $flink->getUser();
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-11 08:33:28 +00:00
|
|
|
$notice = $user->getCurrentNotice();
|
|
|
|
update_profile_box($facebook, $fbuid, $user, $notice);
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-19 13:35:17 +00:00
|
|
|
$this->showHeader($msg);
|
2009-01-19 07:24:12 +00:00
|
|
|
$this->showNoticeForm($user);
|
|
|
|
$this->showNav('Home');
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
echo $this->showNotices($user);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-18 01:31:39 +00:00
|
|
|
$this->showFooter();
|
2008-12-23 19:19:07 +00:00
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
function showNotices($user)
|
2008-12-23 19:33:23 +00:00
|
|
|
{
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$page = $this->trimmed('page');
|
|
|
|
if (!$page) {
|
|
|
|
$page = 1;
|
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$notice = $user->noticesWithFriends(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
$cnt = $this->showNoticeList($notice);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
facebookPagination($page > 1, $cnt > NOTICES_PER_PAGE,
|
|
|
|
$page, 'all', array('nickname' => $user->nickname));
|
2009-01-11 07:03:59 +00:00
|
|
|
}
|
2008-12-07 23:59:25 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
function showNoticeList($notice)
|
2009-01-11 07:03:59 +00:00
|
|
|
{
|
2009-01-18 01:31:39 +00:00
|
|
|
$nl = new FacebookNoticeList($notice);
|
2009-01-11 07:03:59 +00:00
|
|
|
return $nl->show();
|
2008-12-23 19:19:07 +00:00
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-19 00:50:45 +00:00
|
|
|
function getUpdatePermission() {
|
|
|
|
|
|
|
|
$facebook = get_facebook();
|
|
|
|
$fbuid = $facebook->require_login();
|
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
startFBML();
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
$this->showStylesheets();
|
|
|
|
$this->showScripts();
|
2009-01-19 00:50:45 +00:00
|
|
|
|
|
|
|
$this->showLogo();
|
|
|
|
|
|
|
|
common_element_start('div', array('class' => 'content'));
|
|
|
|
|
|
|
|
// Figure what the URL of our app is.
|
|
|
|
$app_props = $facebook->api_client->Admin_getAppProperties(
|
|
|
|
array('canvas_name', 'application_name'));
|
|
|
|
$app_url = 'http://apps.facebook.com/' . $app_props['canvas_name'] . '/index.php';
|
|
|
|
$app_name = $app_props['application_name'];
|
|
|
|
|
|
|
|
$instructions = sprintf(_('If you would like the %s app to automatically update ' .
|
|
|
|
'your Facebook status with your latest notice, you need ' .
|
|
|
|
'to give it permission.'), $app_name);
|
|
|
|
|
|
|
|
common_element_start('p');
|
|
|
|
common_element('span', array('id' => 'permissions_notice'), $instructions);
|
|
|
|
common_element_end('p');
|
|
|
|
|
|
|
|
common_element_start('form', array('method' => 'post',
|
|
|
|
'action' => $app_url,
|
|
|
|
'id' => 'facebook-skip-permissions'));
|
|
|
|
|
|
|
|
common_element_start('ul', array('id' => 'fb-permissions-list'));
|
|
|
|
common_element_start('li', array('id' => 'fb-permissions-item'));
|
|
|
|
common_element_start('fb:prompt-permission', array('perms' => 'status_update',
|
|
|
|
'next_fbjs' => 'document.setLocation(\'' . $app_url . '\')'));
|
|
|
|
common_element('span', array('class' => 'facebook-button'),
|
|
|
|
_('Allow Identi.ca to update my Facebook status'));
|
|
|
|
common_element_end('fb:prompt-permission');
|
|
|
|
common_element_end('li');
|
|
|
|
|
|
|
|
common_element_start('li', array('id' => 'fb-permissions-item'));
|
|
|
|
common_submit('skip', _('Skip'));
|
|
|
|
common_element_end('li');
|
|
|
|
common_element_end('ul');
|
|
|
|
|
|
|
|
common_element_end('form');
|
|
|
|
common_element_end('div');
|
|
|
|
|
|
|
|
common_end_xml();
|
|
|
|
|
|
|
|
}
|
2009-01-19 13:35:17 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
function saveNewNotice($flink)
|
|
|
|
{
|
2009-01-19 13:35:17 +00:00
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
$user = $flink->getUser();
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 13:35:17 +00:00
|
|
|
$content = $_POST['status_textarea'];
|
2009-01-19 07:24:12 +00:00
|
|
|
|
|
|
|
if (!$content) {
|
|
|
|
$this->showHome($flink, _('No content!'));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
$content_shortened = common_shorten_links($content);
|
|
|
|
|
|
|
|
if (mb_strlen($content_shortened) > 140) {
|
|
|
|
common_debug("Content = '$content_shortened'", __FILE__);
|
|
|
|
common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
|
|
|
|
$this->showHome($flink, _('That\'s too long. Max notice size is 140 chars.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$inter = new CommandInterpreter();
|
|
|
|
|
|
|
|
$cmd = $inter->handle_command($user, $content_shortened);
|
|
|
|
|
|
|
|
if ($cmd) {
|
|
|
|
$cmd->execute(new WebChannel());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$replyto = $this->trimmed('inreplyto');
|
|
|
|
|
|
|
|
$notice = Notice::saveNew($user->id, $content,
|
|
|
|
'Facebook', 1, ($replyto == 'false') ? null : $replyto);
|
|
|
|
|
|
|
|
if (is_string($notice)) {
|
|
|
|
$this->showHome($flink, 'Error!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
common_broadcast_notice($notice);
|
|
|
|
$this->showHome($flink, 'Success!');
|
|
|
|
}
|
2009-01-19 13:35:17 +00:00
|
|
|
|
2008-12-06 03:01:32 +00:00
|
|
|
}
|