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
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
|
2008-12-23 19:49:23 +00:00
|
|
|
class FacebookhomeAction extends FacebookAction
|
|
|
|
{
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-23 03:16:03 +00: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-23 19:33:23 +00:00
|
|
|
function handle($args)
|
|
|
|
{
|
2009-01-23 03:16:03 +00:00
|
|
|
parent::handle($args);
|
2009-01-26 06:31:25 +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
|
2009-01-26 06:31:25 +00:00
|
|
|
if ($this->arg('skip') || $args['fb_sig_request_method'] == 'GET') {
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->facebook->api_client->data_setUserPreference(
|
2009-01-19 00:50:45 +00:00
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF, 'true');
|
|
|
|
}
|
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
if ($this->flink) {
|
2009-01-19 07:24:12 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->user = $this->flink->getUser();
|
2009-01-19 00:50:45 +00:00
|
|
|
|
|
|
|
// If this is the first time the user has started the app
|
2009-01-23 05:47:34 +00:00
|
|
|
// prompt for Facebook status update permission
|
|
|
|
if (!$this->facebook->api_client->users_hasAppPermission('status_update')) {
|
2009-01-23 03:16:03 +00:00
|
|
|
|
|
|
|
if ($this->facebook->api_client->data_getUserPreference(
|
2009-01-23 05:47:34 +00:00
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF) != 'true') {
|
|
|
|
$this->getUpdatePermission();
|
|
|
|
return;
|
2009-01-23 03:16:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the user's profile box has the lastest notice
|
|
|
|
$notice = $this->user->getCurrentNotice();
|
2009-01-23 05:47:34 +00:00
|
|
|
if ($notice) {
|
|
|
|
$this->updateProfileBox($notice);
|
|
|
|
}
|
2009-01-23 03:16:03 +00:00
|
|
|
|
2009-01-23 05:47:34 +00:00
|
|
|
if ($this->arg('status_submit') == 'Send') {
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->saveNewNotice();
|
2009-01-23 05:47:34 +00:00
|
|
|
}
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
// User is authenticated and has already been prompted once for
|
2009-01-19 00:50:45 +00:00
|
|
|
// Facebook status update permission? Then show the main page
|
|
|
|
// of the app
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->showPage();
|
|
|
|
|
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-23 03:16:03 +00:00
|
|
|
$this->login();
|
2009-01-11 07:03:59 +00:00
|
|
|
}
|
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-23 03:16:03 +00:00
|
|
|
function login()
|
2009-01-11 07:03:59 +00:00
|
|
|
{
|
2009-01-23 05:37:21 +00:00
|
|
|
|
|
|
|
$this->showStylesheets();
|
|
|
|
|
2009-01-11 07:03:59 +00:00
|
|
|
$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;
|
2009-01-23 03:16:03 +00:00
|
|
|
$flink->foreign_id = $this->fbuid;
|
2009-01-11 07:03:59 +00:00
|
|
|
$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->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()
|
|
|
|
{
|
|
|
|
// A default prefix string for notices
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->facebook->api_client->data_setUserPreference(
|
2009-01-19 00:50:45 +00:00
|
|
|
FACEBOOK_NOTICE_PREFIX, 'dented: ');
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->facebook->api_client->data_setUserPreference(
|
2009-01-19 00:50:45 +00:00
|
|
|
FACEBOOK_PROMPTED_UPDATE_PREF, 'false');
|
2009-01-12 05:25:07 +00:00
|
|
|
}
|
2009-01-23 03:16:03 +00:00
|
|
|
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
function showNoticeForm()
|
2008-12-23 19:33:23 +00:00
|
|
|
{
|
2009-01-23 03:16:03 +00: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-27 01:55:36 +00:00
|
|
|
|
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
}
|
2009-01-12 05:25:07 +00:00
|
|
|
|
2009-01-23 03:16:03 +00: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-23 19:19:07 +00:00
|
|
|
}
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
function showContent()
|
2008-12-23 19:33:23 +00:00
|
|
|
{
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$notice = $this->user->noticesWithFriends(($this->page-1) *
|
|
|
|
NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
|
|
|
|
|
|
|
|
$nl = new NoticeList($notice, $this);
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$cnt = $nl->show();
|
2008-12-06 03:01:32 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
|
|
|
|
$this->page, 'index.php', array('nickname' => $this->user->nickname));
|
2008-12-06 03:01:32 +00:00
|
|
|
|
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-23 03:16:03 +00:00
|
|
|
|
|
|
|
$nl = new NoticeList($notice, $this);
|
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() {
|
|
|
|
|
2009-01-23 05:37:21 +00:00
|
|
|
$this->showStylesheets();
|
|
|
|
|
2009-01-26 06:31:25 +00:00
|
|
|
$this->elementStart('div', array('class' => 'facebook_guide'));
|
2009-01-19 00:50:45 +00: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 03:16:03 +00:00
|
|
|
'to give it permission.'), $this->app_name);
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementStart('p');
|
|
|
|
$this->element('span', array('id' => 'permissions_notice'), $instructions);
|
|
|
|
$this->elementEnd('p');
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementStart('form', array('method' => 'post',
|
2009-01-23 08:42:40 +00:00
|
|
|
'action' => "index.php",
|
2009-01-19 00:50:45 +00:00
|
|
|
'id' => 'facebook-skip-permissions'));
|
|
|
|
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementStart('ul', array('id' => 'fb-permissions-list'));
|
|
|
|
$this->elementStart('li', array('id' => 'fb-permissions-item'));
|
2009-01-26 06:31:25 +00:00
|
|
|
|
|
|
|
$next = urlencode("$this->app_uri/index.php");
|
|
|
|
$api_key = common_config('facebook', 'apikey');
|
|
|
|
|
|
|
|
$auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
|
|
|
|
$api_key . '&v=1.0&ext_perm=status_update&next=' . $next .
|
|
|
|
'&next_cancel=' . $next . '&submit=skip';
|
|
|
|
|
|
|
|
$this->elementStart('span', array('class' => 'facebook-button'));
|
|
|
|
$this->element('a', array('href' => $auth_url),
|
|
|
|
sprintf(_('Okay, do it!'), $this->app_name));
|
|
|
|
$this->elementEnd('span');
|
|
|
|
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementEnd('li');
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementStart('li', array('id' => 'fb-permissions-item'));
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->submit('skip', _('Skip'));
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementEnd('ul');
|
2009-01-19 00:50:45 +00:00
|
|
|
|
2009-01-19 13:44:10 +00:00
|
|
|
$this->elementEnd('form');
|
|
|
|
$this->elementEnd('div');
|
2009-01-19 00:50:45 +00:00
|
|
|
|
|
|
|
}
|
2009-01-19 13:35:17 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
function saveNewNotice()
|
2009-01-19 07:24:12 +00:00
|
|
|
{
|
2009-01-19 13:35:17 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$user = $this->flink->getUser();
|
2009-01-19 07:24:12 +00:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$content = $this->trimmed('status_textarea');
|
|
|
|
|
2009-01-19 07:24:12 +00:00
|
|
|
if (!$content) {
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->showPage(_('No notice content!'));
|
2009-01-19 07:24:12 +00: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 03:16:03 +00:00
|
|
|
$this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
|
2009-01-19 07:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$inter = new CommandInterpreter();
|
|
|
|
|
|
|
|
$cmd = $inter->handle_command($user, $content_shortened);
|
|
|
|
|
|
|
|
if ($cmd) {
|
2009-01-23 03:16:03 +00:00
|
|
|
|
|
|
|
// XXX fix this
|
|
|
|
|
2009-01-19 07:24:12 +00: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 03:16:03 +00:00
|
|
|
$this->showPage($notice);
|
2009-01-19 07:24:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
common_broadcast_notice($notice);
|
2009-01-23 03:16:03 +00: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 07:24:12 +00:00
|
|
|
}
|
2009-01-23 03:16:03 +00:00
|
|
|
|
2009-01-19 13:35:17 +00:00
|
|
|
|
2008-12-06 03:01:32 +00:00
|
|
|
}
|