3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2008-2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php';
35 require_once INSTALLDIR . '/lib/noticeform.php';
37 class FacebookAction extends Action
47 function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
49 parent::__construct($output, $indent);
51 $this->facebook = $facebook;
52 $this->flink = $flink;
55 $this->fbuid = $flink->foreign_id;
56 $this->user = $flink->getUser();
59 $this->args = array();
62 function prepare($argarray)
64 parent::prepare($argarray);
66 $this->facebook = getFacebook();
67 $this->fbuid = $this->facebook->require_login();
69 $this->action = $this->trimmed('action');
71 $app_props = $this->facebook->api_client->Admin_getAppProperties(
72 array('canvas_name', 'application_name'));
74 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
75 $this->app_name = $app_props['application_name'];
77 $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
83 function showStylesheets()
85 $this->cssLink('css/display.css', 'base');
86 $this->cssLink('css/display.css', null, 'screen, projection, tv');
87 $this->cssLink('plugins/Facebook/facebookapp.css');
90 function showScripts()
92 $this->script('js/facebookapp.js');
96 * Start an Facebook ready HTML document
98 * For Facebook we don't want to actually output any headers,
99 * DTD info, etc. Just Stylesheet and JavaScript links.
101 * @param string $type MIME type to use; default is to do negotation.
106 function startHTML($type=null)
108 $this->showStylesheets();
109 $this->showScripts();
111 $this->elementStart('div', array('class' => 'facebook-page'));
115 * Ends a Facebook ready HTML document
121 $this->elementEnd('div');
130 function showNoticeForm()
132 // don't do it for most of the Facebook pages
137 $this->elementStart('div', array('id' => 'wrap'));
141 $this->elementEnd('div');
144 function showHead($error, $success)
148 $this->element("h1", null, $error);
152 $this->element("h1", null, $success);
155 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
156 $this->elementStart('span', array('id' => 'add_to_profile'));
157 $this->element('fb:add-section-button', array('section' => 'profile'));
158 $this->elementEnd('span');
159 $this->elementEnd('fb:if-section-not-added');
163 // Make this into a widget later
164 function showLocalNav()
166 $this->elementStart('ul', array('class' => 'nav'));
168 $this->elementStart('li', array('class' =>
169 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
171 array('href' => 'index.php', 'title' => _('Home')), _('Home'));
172 $this->elementEnd('li');
174 if (common_config('invite', 'enabled')) {
175 $this->elementStart('li',
177 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
179 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
180 $this->elementEnd('li');
183 $this->elementStart('li',
185 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
187 array('href' => 'settings.php',
188 'title' => _('Settings')), _('Settings'));
189 $this->elementEnd('li');
191 $this->elementEnd('ul');
195 * Show header of the page.
199 function showHeader()
201 $this->elementStart('div', array('id' => 'header'));
203 $this->showNoticeForm();
204 $this->elementEnd('div');
208 * Show page, a template method.
212 function showPage($error = null, $success = null)
215 $this->showHead($error, $success);
220 function showInstructions()
223 $this->elementStart('div', array('class' => 'facebook_guide'));
225 $this->elementStart('dl', array('class' => 'system_notice'));
226 $this->element('dt', null, 'Page Notice');
228 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
229 'with your username and password. Don\'t have a username yet? ');
230 $loginmsg_part2 = _(' a new account.');
232 $this->elementStart('dd');
233 $this->elementStart('p');
234 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
236 array('href' => common_local_url('register')), _('Register'));
237 $this->text($loginmsg_part2);
238 $this->elementEnd('p');
239 $this->elementEnd('dd');
241 $this->elementEnd('dl');
242 $this->elementEnd('div');
245 function showLoginForm($msg = null)
248 $this->elementStart('div', array('id' => 'content'));
249 $this->element('h1', null, _('Login'));
252 $this->element('fb:error', array('message' => $msg));
255 $this->showInstructions();
257 $this->elementStart('div', array('id' => 'content_inner'));
259 $this->elementStart('form', array('method' => 'post',
260 'class' => 'form_settings',
262 'action' => 'index.php'));
264 $this->elementStart('fieldset');
266 $this->elementStart('ul', array('class' => 'form_datas'));
267 $this->elementStart('li');
268 $this->input('nickname', _('Nickname'));
269 $this->elementEnd('li');
270 $this->elementStart('li');
271 $this->password('password', _('Password'));
272 $this->elementEnd('li');
273 $this->elementEnd('ul');
275 $this->submit('submit', _('Login'));
276 $this->elementEnd('fieldset');
277 $this->elementEnd('form');
279 $this->elementStart('p');
280 $this->element('a', array('href' => common_local_url('recoverpassword')),
281 _('Lost or forgotten password?'));
282 $this->elementEnd('p');
284 $this->elementEnd('div');
285 $this->elementEnd('div');
289 function updateProfileBox($notice)
292 // Need to include inline CSS for styling the Profile box
294 $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
295 $icon_url = $app_props['icon_url'];
301 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
308 .entry-title .vcard .photo {
317 .entry-title p.entry-content {
325 div.entry-content dl,
326 div.entry-content dt,
327 div.entry-content dd {
329 text-transform:lowercase;
332 div.entry-content dd,
333 div.entry-content .device dt {
337 div.entry-content dl.timestamp dt,
338 div.entry-content dl.response dt {
341 div.entry-content dd a {
342 display:inline-block;
345 #facebook_statusnet_app {
350 background:url('.$icon_url.') no-repeat 0 0;
355 $this->xw->openMemory();
357 $item = new FacebookProfileBoxNotice($notice, $this);
360 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
361 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
363 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
365 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
367 $this->xw->openURI('php://output');
371 * Generate pagination links
373 * @param boolean $have_before is there something before?
374 * @param boolean $have_after is there something after?
375 * @param integer $page current page
376 * @param string $action current action
377 * @param array $args rest of query arguments
381 function pagination($have_before, $have_after, $page, $action, $args=null)
383 // Does a little before-after block for next/prev page
384 if ($have_before || $have_after) {
385 $this->elementStart('div', array('class' => 'pagination'));
386 $this->elementStart('dl', null);
387 $this->element('dt', null, _('Pagination'));
388 $this->elementStart('dd', null);
389 $this->elementStart('ul', array('class' => 'nav'));
392 $pargs = array('page' => $page-1);
393 $newargs = $args ? array_merge($args, $pargs) : $pargs;
394 $this->elementStart('li', array('class' => 'nav_prev'));
395 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
397 $this->elementEnd('li');
400 $pargs = array('page' => $page+1);
401 $newargs = $args ? array_merge($args, $pargs) : $pargs;
402 $this->elementStart('li', array('class' => 'nav_next'));
403 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
405 $this->elementEnd('li');
407 if ($have_before || $have_after) {
408 $this->elementEnd('ul');
409 $this->elementEnd('dd');
410 $this->elementEnd('dl');
411 $this->elementEnd('div');
415 function saveNewNotice()
418 $user = $this->flink->getUser();
420 $content = $this->trimmed('status_textarea');
423 $this->showPage(_('No notice content!'));
426 $content_shortened = common_shorten_links($content);
428 if (Notice::contentTooLong($content_shortened)) {
429 $this->showPage(sprintf(_('That\'s too long. Max notice size is %d chars.'),
430 Notice::maxContent()));
435 $inter = new CommandInterpreter();
437 $cmd = $inter->handle_command($user, $content_shortened);
443 $cmd->execute(new WebChannel());
447 $replyto = $this->trimmed('inreplyto');
450 $notice = Notice::saveNew($user->id, $content,
451 'web', 1, ($replyto == 'false') ? null : $replyto);
452 } catch (Exception $e) {
453 $this->showPage($e->getMessage());
457 common_broadcast_notice($notice);
459 // Also update the user's Facebook status
460 facebookBroadcastNotice($notice);
466 class FacebookNoticeForm extends NoticeForm
469 var $post_action = null;
474 * @param HTMLOutputter $out output channel
475 * @param string $action action to return to, if any
476 * @param string $content content to pre-fill
479 function __construct($out=null, $action=null, $content=null,
480 $post_action=null, $user=null)
482 parent::__construct($out, $action, $content, $user);
483 $this->post_action = $post_action;
489 * @return string URL of the action
494 return $this->post_action;
499 class FacebookNoticeList extends NoticeList
505 * @param Notice $notice stream of notices from DB_DataObject
508 function __construct($notice, $out=null)
510 parent::__construct($notice, $out);
514 * show the list of notices
516 * "Uses up" the stream by looping through it. So, probably can't
517 * be called twice on the same list.
519 * @return int count of notices listed.
524 $this->out->elementStart('div', array('id' =>'notices_primary'));
525 $this->out->element('h2', null, _('Notices'));
526 $this->out->elementStart('ul', array('class' => 'notices'));
530 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
533 if ($cnt > NOTICES_PER_PAGE) {
537 $item = $this->newListItem($this->notice);
541 $this->out->elementEnd('ul');
542 $this->out->elementEnd('div');
548 * returns a new list item for the current notice
550 * Overridden to return a Facebook specific list item.
552 * @param Notice $notice the current notice
554 * @return FacebookNoticeListItem a list item for displaying the notice
555 * formatted for display in the Facebook App.
558 function newListItem($notice)
560 return new FacebookNoticeListItem($notice, $this);
565 class FacebookNoticeListItem extends NoticeListItem
571 * Also initializes the profile attribute.
573 * @param Notice $notice The notice we'll display
576 function __construct($notice, $out=null)
578 parent::__construct($notice, $out);
582 * recipe function for displaying a single notice in the Facebook App.
584 * Overridden to strip out some of the controls that we don't
585 * want to be available.
594 $this->showNoticeInfo();
596 // XXX: Need to update to show attachements and controls
603 class FacebookProfileBoxNotice extends FacebookNoticeListItem
609 * Also initializes the profile attribute.
611 * @param Notice $notice The notice we'll display
614 function __construct($notice, $out=null)
616 parent::__construct($notice, $out);
620 * Recipe function for displaying a single notice in the
621 * Facebook App profile notice box
629 $this->showNoticeInfo();
630 $this->showAppLink();
633 function showAppLink()
636 $this->facebook = getFacebook();
638 $app_props = $this->facebook->api_client->Admin_getAppProperties(
639 array('canvas_name', 'application_name'));
641 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
642 $this->app_name = $app_props['application_name'];
644 $this->out->elementStart('a', array('id' => 'facebook_statusnet_app',
645 'href' => $this->app_uri));
646 $this->out->text($this->app_name);
647 $this->out->elementEnd('a');