2008-12-06 03:01:32 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Laconica - a distributed open-source microblogging tool
|
2009-06-21 00:00:04 +01:00
|
|
|
* Copyright (C) 2008, Control Yourself, Inc.
|
2008-12-06 03:01:32 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
var $page = null;
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
function prepare($argarray)
|
2009-06-18 19:45:48 +01:00
|
|
|
{
|
2009-01-23 03:16:03 +00:00
|
|
|
parent::prepare($argarray);
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$this->page = $this->trimmed('page');
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
if (!$this->page) {
|
|
|
|
$this->page = 1;
|
|
|
|
}
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:33:23 +00:00
|
|
|
function handle($args)
|
|
|
|
{
|
2009-06-18 19:45:48 +01:00
|
|
|
parent::handle($args);
|
|
|
|
|
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-06-18 19:45:48 +01: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();
|
2009-06-18 19:45:48 +01: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-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
|
|
|
}
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
function login()
|
2009-01-11 07:03:59 +00:00
|
|
|
{
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 05:37:21 +00:00
|
|
|
$this->showStylesheets();
|
2009-06-18 19:45:48 +01:00
|
|
|
|
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();
|
2009-05-27 00:21:10 +01:00
|
|
|
$flink->set_flags(true, false, 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-30 00:07:56 +00:00
|
|
|
$this->showFooter();
|
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()
|
|
|
|
{
|
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
|
|
|
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";
|
2009-06-18 19:45:48 +01:00
|
|
|
|
|
|
|
$notice_form = new FacebookNoticeForm($this, $post_action, null,
|
2009-01-23 03:16:03 +00:00
|
|
|
$post_action, $this->user);
|
|
|
|
$notice_form->show();
|
|
|
|
}
|
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
|
|
|
{
|
2009-06-18 19:45:48 +01:00
|
|
|
$notice = $this->user->noticeInbox(($this->page-1) * NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
|
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
$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));
|
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-06-18 19:45:48 +01: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-06-18 19:45:48 +01:00
|
|
|
|
2009-01-26 06:31:25 +00:00
|
|
|
$next = urlencode("$this->app_uri/index.php");
|
|
|
|
$api_key = common_config('facebook', 'apikey');
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-26 06:31:25 +00:00
|
|
|
$auth_url = 'http://www.facebook.com/authorize.php?api_key=' .
|
2009-06-18 19:45:48 +01:00
|
|
|
$api_key . '&v=1.0&ext_perm=status_update&next=' . $next .
|
2009-01-26 06:31:25 +00:00
|
|
|
'&next_cancel=' . $next . '&submit=skip';
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-26 06:31:25 +00:00
|
|
|
$this->elementStart('span', array('class' => 'facebook-button'));
|
2009-06-18 19:45:48 +01:00
|
|
|
$this->element('a', array('href' => $auth_url),
|
2009-01-26 06:31:25 +00:00
|
|
|
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-06-18 19:45:48 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
// Does a little before-after block for next/prev page
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
// XXX: Fix so this uses common_local_url() if possible.
|
2009-06-18 19:45:48 +01:00
|
|
|
|
2009-01-23 03:16:03 +00:00
|
|
|
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;
|
2009-06-18 19:45:48 +01:00
|
|
|
$this->elementStart('li', array('class' => 'nav_prev'));
|
2009-01-23 03:16:03 +00:00
|
|
|
$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-19 13:35:17 +00:00
|
|
|
|
2008-12-06 03:01:32 +00:00
|
|
|
}
|