2008-12-06 12:01:32 +09: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-24 04:19:07 +09:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-12-06 12:01:32 +09:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2008-12-24 04:19:07 +09:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-12-06 12:01:32 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('LACONICA')) { exit(1); }
|
|
|
|
|
2009-01-18 10:31:39 +09:00
|
|
|
require_once INSTALLDIR.'/lib/facebookaction.php';
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
|
2008-12-24 04:49:23 +09:00
|
|
|
class FacebookhomeAction extends FacebookAction
|
|
|
|
{
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
var $page = null;
|
|
|
|
|
|
|
|
function prepare($argarray)
|
|
|
|
{
|
|
|
|
parent::prepare($argarray);
|
|
|
|
|
|
|
|
$this->page = $this->trimmed('page');
|
|
|
|
|
|
|
|
if (!$this->page) {
|
|
|
|
$this->page = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-24 04:33:23 +09:00
|
|
|
function handle($args)
|
|
|
|
{
|
2009-01-23 12:16:03 +09:00
|
|
|
parent::handle($args);
|
|
|
|
|
2009-01-19 09:50:45 +09: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')) {
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->facebook->api_client->data_setUserPreference(
|
2009-01-19 09:50:45 +09:00
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
|
|
|
|
}
|
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
if ($this->flink) {
|
2009-01-19 16:24:12 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->user = $this->flink->getUser();
|
2009-01-19 09:50:45 +09:00
|
|
|
|
|
|
|
// If this is the first time the user has started the app
|
2009-01-23 14:47:34 +09:00
|
|
|
// prompt for Facebook status update permission
|
|
|
|
if (!$this->facebook->api_client->users_hasAppPermission('status_update')) {
|
2009-01-23 12:16:03 +09:00
|
|
|
|
|
|
|
if ($this->facebook->api_client->data_getUserPreference(
|
2009-01-23 14:47:34 +09:00
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
|
|
|
|
$this->getUpdatePermission();
|
|
|
|
return;
|
2009-01-23 12:16:03 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the user's profile box has the lastest notice
|
|
|
|
$notice = $this->user->getCurrentNotice();
|
2009-01-23 14:47:34 +09:00
|
|
|
if ($notice) {
|
|
|
|
$this->updateProfileBox($notice);
|
|
|
|
}
|
2009-01-23 12:16:03 +09:00
|
|
|
|
2009-01-23 14:47:34 +09:00
|
|
|
if ($this->arg('status_submit') == 'Send') {
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->saveNewNotice();
|
2009-01-23 14:47:34 +09:00
|
|
|
}
|
2009-01-19 09:50:45 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
// User is authenticated and has already been prompted once for
|
2009-01-19 09:50:45 +09:00
|
|
|
// Facebook status update permission? Then show the main page
|
|
|
|
// of the app
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->showPage();
|
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
} else {
|
2009-01-19 09:50:45 +09:00
|
|
|
|
|
|
|
// User hasn't authenticated yet, prompt for creds
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->login();
|
2009-01-11 16:03:59 +09:00
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
function login()
|
2009-01-11 16:03:59 +09:00
|
|
|
{
|
2009-01-23 14:37:21 +09:00
|
|
|
|
|
|
|
$this->showStylesheets();
|
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
$nickname = common_canonical_nickname($this->trimmed('nickname'));
|
|
|
|
$password = $this->arg('password');
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
$msg = null;
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
if ($nickname) {
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
if (common_check_user($nickname, $password)) {
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
$user = User::staticGet('nickname', $nickname);
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
if (!$user) {
|
|
|
|
$this->showLoginForm(_("Server error - couldn't get user!"));
|
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
$flink = DB_DataObject::factory('foreign_link');
|
|
|
|
$flink->user_id = $user->id;
|
2009-01-23 12:16:03 +09:00
|
|
|
$flink->foreign_id = $this->fbuid;
|
2009-01-11 16:03:59 +09:00
|
|
|
$flink->service = FACEBOOK_SERVICE;
|
|
|
|
$flink->created = common_sql_now();
|
|
|
|
$flink->set_flags(true, false, false);
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
$flink_id = $flink->insert();
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
// XXX: Do some error handling here
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-12 14:25:07 +09:00
|
|
|
$this->setDefaults();
|
2009-01-19 09:50:45 +09:00
|
|
|
//$this->showHome($flink, _('You can now use Identi.ca from Facebook!'));
|
2009-01-12 14:25:07 +09:00
|
|
|
|
2009-01-19 09:50:45 +09:00
|
|
|
$this->getUpdatePermission();
|
2009-01-17 05:19:49 +09:00
|
|
|
return;
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
} else {
|
|
|
|
$msg = _('Incorrect username or password.');
|
|
|
|
}
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
$this->showLoginForm($msg);
|
2009-01-19 09:50:45 +09:00
|
|
|
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-12 14:25:07 +09:00
|
|
|
function setDefaults()
|
|
|
|
{
|
|
|
|
// A default prefix string for notices
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->facebook->api_client->data_setUserPreference(
|
2009-01-19 09:50:45 +09:00
|
|
|
FACEBOOK_NOTICE_PREFIX, 'dented: ');
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->facebook->api_client->data_setUserPreference(
|
2009-01-19 09:50:45 +09:00
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
|
2009-01-12 14:25:07 +09:00
|
|
|
}
|
2009-01-23 12:16:03 +09:00
|
|
|
|
2009-01-12 14:25:07 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
function showNoticeForm()
|
2008-12-24 04:33:23 +09:00
|
|
|
{
|
2009-01-23 12:16:03 +09:00
|
|
|
|
|
|
|
$post_action = "$this->app_uri/index.php";
|
|
|
|
|
|
|
|
$notice_form = new FacebookNoticeForm($this, $post_action, null,
|
|
|
|
$post_action, $this->user);
|
|
|
|
$notice_form->show();
|
|
|
|
}
|
2009-01-12 14:25:07 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
function title()
|
|
|
|
{
|
|
|
|
if ($this->page > 1) {
|
|
|
|
return sprintf(_("%s and friends, page %d"), $this->user->nickname, $this->page);
|
|
|
|
} else {
|
|
|
|
return sprintf(_("%s and friends"), $this->user->nickname);
|
|
|
|
}
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
function showContent()
|
2008-12-24 04:33:23 +09:00
|
|
|
{
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
$notice = $this->user->noticesWithFriends(($this->page-1) *
|
|
|
|
NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
|
|
|
|
|
|
|
|
$nl = new NoticeList($notice, $this);
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
$cnt = $nl->show();
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
|
|
|
|
$this->page, 'index.php', array('nickname' => $this->user->nickname));
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-11 16:03:59 +09:00
|
|
|
}
|
2008-12-08 08:59:25 +09:00
|
|
|
|
2009-01-19 16:24:12 +09:00
|
|
|
function showNoticeList($notice)
|
2009-01-11 16:03:59 +09:00
|
|
|
{
|
2009-01-23 12:16:03 +09:00
|
|
|
|
|
|
|
$nl = new NoticeList($notice, $this);
|
2009-01-11 16:03:59 +09:00
|
|
|
return $nl->show();
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-12-06 12:01:32 +09:00
|
|
|
|
2009-01-19 09:50:45 +09:00
|
|
|
function getUpdatePermission() {
|
|
|
|
|
2009-01-23 14:37:21 +09:00
|
|
|
$this->showStylesheets();
|
|
|
|
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementStart('div', array('class' => 'content'));
|
2009-01-19 09:50:45 +09:00
|
|
|
|
|
|
|
$instructions = sprintf(_('If you would like the %s app to automatically update ' .
|
|
|
|
'your Facebook status with your latest notice, you need ' .
|
2009-01-23 12:16:03 +09:00
|
|
|
'to give it permission.'), $this->app_name);
|
2009-01-19 09:50:45 +09:00
|
|
|
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementStart('p');
|
|
|
|
$this->element('span', array('id' => 'permissions_notice'), $instructions);
|
|
|
|
$this->elementEnd('p');
|
2009-01-19 09:50:45 +09:00
|
|
|
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementStart('form', array('method' => 'post',
|
2009-01-23 16:09:00 +09:00
|
|
|
'action' => "$app_url/index.php",
|
2009-01-19 09:50:45 +09:00
|
|
|
'id' => 'facebook-skip-permissions'));
|
|
|
|
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementStart('ul', array('id' => 'fb-permissions-list'));
|
|
|
|
$this->elementStart('li', array('id' => 'fb-permissions-item'));
|
|
|
|
$this->elementStart('fb:prompt-permission', array('perms' => 'status_update',
|
2009-01-23 16:09:00 +09:00
|
|
|
'next_fbjs' => 'document.setLocation(\'' . "$this->app_uri/index.php" . '\')'));
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->element('span', array('class' => 'facebook-button'),
|
2009-01-23 12:16:03 +09:00
|
|
|
sprintf(_('Allow %s to update my Facebook status'), $this->app_name));
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementEnd('fb:prompt-permission');
|
|
|
|
$this->elementEnd('li');
|
2009-01-19 09:50:45 +09:00
|
|
|
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementStart('li', array('id' => 'fb-permissions-item'));
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->submit('skip', _('Skip'));
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementEnd('ul');
|
2009-01-19 09:50:45 +09:00
|
|
|
|
2009-01-19 22:44:10 +09:00
|
|
|
$this->elementEnd('form');
|
|
|
|
$this->elementEnd('div');
|
2009-01-19 09:50:45 +09:00
|
|
|
|
|
|
|
}
|
2009-01-19 22:35:17 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
function saveNewNotice()
|
2009-01-19 16:24:12 +09:00
|
|
|
{
|
2009-01-19 22:35:17 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
$user = $this->flink->getUser();
|
2009-01-19 16:24:12 +09:00
|
|
|
|
2009-01-23 12:16:03 +09:00
|
|
|
$content = $this->trimmed('status_textarea');
|
|
|
|
|
2009-01-19 16:24:12 +09:00
|
|
|
if (!$content) {
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->showPage(_('No notice content!'));
|
2009-01-19 16:24:12 +09:00
|
|
|
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__);
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
|
2009-01-19 16:24:12 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$inter = new CommandInterpreter();
|
|
|
|
|
|
|
|
$cmd = $inter->handle_command($user, $content_shortened);
|
|
|
|
|
|
|
|
if ($cmd) {
|
2009-01-23 12:16:03 +09:00
|
|
|
|
|
|
|
// XXX fix this
|
|
|
|
|
2009-01-19 16:24:12 +09:00
|
|
|
$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)) {
|
2009-01-23 12:16:03 +09:00
|
|
|
$this->showPage($notice);
|
2009-01-19 16:24:12 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
common_broadcast_notice($notice);
|
2009-01-23 12:16:03 +09:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate pagination links
|
|
|
|
*
|
|
|
|
* @param boolean $have_before is there something before?
|
|
|
|
* @param boolean $have_after is there something after?
|
|
|
|
* @param integer $page current page
|
|
|
|
* @param string $action current action
|
|
|
|
* @param array $args rest of query arguments
|
|
|
|
*
|
|
|
|
* @return nothing
|
|
|
|
*/
|
|
|
|
function pagination($have_before, $have_after, $page, $action, $args=null)
|
|
|
|
{
|
|
|
|
|
|
|
|
// Does a little before-after block for next/prev page
|
|
|
|
|
|
|
|
// XXX: Fix so this uses common_local_url() if possible.
|
|
|
|
|
|
|
|
if ($have_before || $have_after) {
|
|
|
|
$this->elementStart('div', array('class' => 'pagination'));
|
|
|
|
$this->elementStart('dl', null);
|
|
|
|
$this->element('dt', null, _('Pagination'));
|
|
|
|
$this->elementStart('dd', null);
|
|
|
|
$this->elementStart('ul', array('class' => 'nav'));
|
|
|
|
}
|
|
|
|
if ($have_before) {
|
|
|
|
$pargs = array('page' => $page-1);
|
|
|
|
$newargs = $args ? array_merge($args, $pargs) : $pargs;
|
|
|
|
$this->elementStart('li', array('class' => 'nav_prev'));
|
|
|
|
$this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'prev'),
|
|
|
|
_('After'));
|
|
|
|
$this->elementEnd('li');
|
|
|
|
}
|
|
|
|
if ($have_after) {
|
|
|
|
$pargs = array('page' => $page+1);
|
|
|
|
$newargs = $args ? array_merge($args, $pargs) : $pargs;
|
|
|
|
$this->elementStart('li', array('class' => 'nav_next'));
|
|
|
|
$this->element('a', array('href' => "$action?page=$newargs[page]", 'rel' => 'next'),
|
|
|
|
_('Before'));
|
|
|
|
$this->elementEnd('li');
|
|
|
|
}
|
|
|
|
if ($have_before || $have_after) {
|
|
|
|
$this->elementEnd('ul');
|
|
|
|
$this->elementEnd('dd');
|
|
|
|
$this->elementEnd('dl');
|
|
|
|
$this->elementEnd('div');
|
|
|
|
}
|
2009-01-19 16:24:12 +09:00
|
|
|
}
|
2009-01-23 12:16:03 +09:00
|
|
|
|
2009-01-19 22:35:17 +09:00
|
|
|
|
2008-12-06 12:01:32 +09:00
|
|
|
}
|