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 . '/plugins/Facebook/facebooknoticeform.php';
37 class FacebookAction extends Action
46 function __construct($output='php://output', $indent=null, $facebook=null, $flink=null)
48 parent::__construct($output, $indent);
50 $this->facebook = $facebook;
51 $this->flink = $flink;
54 $this->fbuid = $flink->foreign_id;
55 $this->user = $flink->getUser();
58 $this->args = array();
61 function prepare($argarray)
63 parent::prepare($argarray);
65 $this->facebook = getFacebook();
66 $this->fbuid = $this->facebook->require_login();
68 $this->action = $this->trimmed('action');
70 $app_props = $this->facebook->api_client->Admin_getAppProperties(
71 array('canvas_name', 'application_name'));
73 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
74 $this->app_name = $app_props['application_name'];
76 $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
82 function showStylesheets()
84 // Loading CSS via files in Facebook FBML Canvas apps is still busted as of 1/31/11
85 // See: http://bugs.developers.facebook.net/show_bug.cgi?id=10052
86 $this->cssLink('css/display.css', 'base');
87 $this->cssLink('css/display.css', null, 'screen, projection, tv');
88 $this->cssLink('plugins/Facebook/facebookapp.css');
90 // Also, Facebook refuses to let me do this... gar!
92 $baseCss = file_get_contents(INSTALLDIR . '/theme/base/css/display.css');
93 $this->style($baseCss);
95 $facebookCss = file_get_contents(INSTALLDIR . '/plugins/Facebook/facebookapp.css');
96 $this->style($facebookCss);
100 function showScripts()
102 $this->script('plugins/Facebook/facebookapp.js');
106 * Start an Facebook ready HTML document
108 * For Facebook we don't want to actually output any headers,
109 * DTD info, etc. Just Stylesheet and JavaScript links.
111 * @param string $type MIME type to use; default is to do negotation.
115 function startHTML($type=null)
117 $this->showStylesheets();
118 $this->showScripts();
120 $this->elementStart('div', array('class' => 'facebook-page'));
124 * Ends a Facebook ready HTML document
130 $this->elementEnd('div');
139 function showNoticeForm()
141 // don't do it for most of the Facebook pages
146 $this->elementStart('div', array('id' => 'wrap'));
150 $this->elementEnd('div');
153 function showHead($error, $success)
156 $this->element("h1", null, $error);
160 $this->element("h1", null, $success);
163 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
164 $this->elementStart('span', array('id' => 'add_to_profile'));
165 $this->element('fb:add-section-button', array('section' => 'profile'));
166 $this->elementEnd('span');
167 $this->elementEnd('fb:if-section-not-added');
171 // Make this into a widget later
172 function showLocalNav()
174 $this->elementStart('ul', array('class' => 'nav'));
176 $this->elementStart('li', array('class' =>
177 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
179 // TRANS: Link description for 'Home' link that leads to a start page.
180 array('href' => 'index.php', 'title' => _m('MENU','Home')),
181 // TRANS: Tooltip for 'Home' link that leads to a start page.
183 $this->elementEnd('li');
185 if (common_config('invite', 'enabled')) {
186 $this->elementStart('li',
188 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
190 // TRANS: Link description for 'Invite' link that leads to a page where friends can be invited.
191 array('href' => 'invite.php', 'title' => _m('MENU','Invite')),
192 // TRANS: Tooltip for 'Invite' link that leads to a page where friends can be invited.
194 $this->elementEnd('li');
197 $this->elementStart('li',
199 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
201 array('href' => 'settings.php',
202 // TRANS: Link description for 'Settings' link that leads to a page user preferences can be set.
203 'title' => _m('MENU','Settings')),
204 // TRANS: Tooltip for 'Settings' link that leads to a page user preferences can be set.
206 $this->elementEnd('li');
208 $this->elementEnd('ul');
212 * Show header of the page.
216 function showHeader()
218 $this->elementStart('div', array('id' => 'header'));
220 $this->showNoticeForm();
221 $this->elementEnd('div');
225 * Show page, a template method.
229 function showPage($error = null, $success = null)
232 $this->showHead($error, $success);
237 function showInstructions()
239 $this->elementStart('div', array('class' => 'facebook_guide'));
241 $this->elementStart('dl', array('class' => 'system_notice'));
242 $this->element('dt', null, 'Page Notice');
244 $loginmsg_part1 = _m('To use the %s Facebook Application you need to login ' .
245 'with your username and password. Don\'t have a username yet?');
246 $loginmsg_part2 = _m(' a new account.');
248 $this->elementStart('dd');
249 $this->elementStart('p');
250 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
251 // @todo FIXME: Bad i18n. Patchwork message in two parts.
253 array('href' => common_local_url('register')), _m('Register'));
254 $this->text( " " . $loginmsg_part2);
255 $this->elementEnd('p');
256 $this->elementEnd('dd');
258 $this->elementEnd('dl');
259 $this->elementEnd('div');
262 function showLoginForm($msg = null)
265 $this->elementStart('div', array('id' => 'content'));
266 $this->element('h1', null, _m('Login'));
269 $this->element('fb:error', array('message' => $msg));
272 $this->showInstructions();
274 $this->elementStart('div', array('id' => 'content_inner'));
276 $this->elementStart('form', array('method' => 'post',
277 'class' => 'form_settings',
279 'action' => 'index.php'));
281 $this->elementStart('fieldset');
283 $this->elementStart('ul', array('class' => 'form_datas'));
284 $this->elementStart('li');
285 $this->input('nickname', _m('Nickname'));
286 $this->elementEnd('li');
287 $this->elementStart('li');
288 $this->password('password', _m('Password'));
289 $this->elementEnd('li');
290 $this->elementEnd('ul');
292 // TRANS: Login button.
293 $this->submit('submit', _m('BUTTON','Login'));
294 $this->elementEnd('fieldset');
295 $this->elementEnd('form');
297 $this->elementStart('p');
298 $this->element('a', array('href' => common_local_url('recoverpassword')),
299 _m('Lost or forgotten password?'));
300 $this->elementEnd('p');
302 $this->elementEnd('div');
303 $this->elementEnd('div');
307 * Generate pagination links
309 * @param boolean $have_before is there something before?
310 * @param boolean $have_after is there something after?
311 * @param integer $page current page
312 * @param string $action current action
313 * @param array $args rest of query arguments
317 function pagination($have_before, $have_after, $page, $action, $args=null)
319 // Does a little before-after block for next/prev page
320 if ($have_before || $have_after) {
321 $this->elementStart('dl', 'pagination');
322 $this->element('dt', null, _m('Pagination'));
323 $this->elementStart('dd', null);
324 $this->elementStart('ul', array('class' => 'nav'));
327 $pargs = array('page' => $page-1);
328 $newargs = $args ? array_merge($args, $pargs) : $pargs;
329 $this->elementStart('li', array('class' => 'nav_prev'));
330 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
332 $this->elementEnd('li');
335 $pargs = array('page' => $page+1);
336 $newargs = $args ? array_merge($args, $pargs) : $pargs;
337 $this->elementStart('li', array('class' => 'nav_next'));
338 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
340 $this->elementEnd('li');
342 if ($have_before || $have_after) {
343 $this->elementEnd('ul');
344 $this->elementEnd('dd');
345 $this->elementEnd('dl');
349 function saveNewNotice()
351 $user = $this->flink->getUser();
353 $content = $this->trimmed('status_textarea');
356 $this->showPage(_m('No notice content!'));
359 $content_shortened = $user->shortenLinks($content);
361 if (Notice::contentTooLong($content_shortened)) {
362 // @todo FIXME: i18n: Needs plural.
363 $this->showPage(sprintf(_m('That\'s too long. Max notice size is %d chars.'),
364 Notice::maxContent()));
369 $inter = new CommandInterpreter();
371 $cmd = $inter->handle_command($user, $content_shortened);
375 $cmd->execute(new WebChannel());
379 $replyto = $this->trimmed('inreplyto');
382 $notice = Notice::saveNew($user->id, $content, 'web',
383 array('reply_to' => ($replyto == 'false') ? null : $replyto));
385 } catch (Exception $e) {
386 $this->showPage($e->getMessage());
393 class FacebookNoticeList extends NoticeList
398 * @param Notice $notice stream of notices from DB_DataObject
401 function __construct($notice, $out=null)
403 parent::__construct($notice, $out);
407 * show the list of notices
409 * "Uses up" the stream by looping through it. So, probably can't
410 * be called twice on the same list.
412 * @return int count of notices listed.
416 $this->out->elementStart('div', array('id' =>'notices_primary'));
417 $this->out->element('h2', null, _m('Notices'));
418 $this->out->elementStart('ul', array('class' => 'notices'));
422 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
425 if ($cnt > NOTICES_PER_PAGE) {
429 $item = $this->newListItem($this->notice);
433 $this->out->elementEnd('ul');
434 $this->out->elementEnd('div');
440 * returns a new list item for the current notice
442 * Overridden to return a Facebook specific list item.
444 * @param Notice $notice the current notice
446 * @return FacebookNoticeListItem a list item for displaying the notice
447 * formatted for display in the Facebook App.
449 function newListItem($notice)
451 return new FacebookNoticeListItem($notice, $this);
455 class FacebookNoticeListItem extends NoticeListItem
460 * Also initializes the profile attribute.
462 * @param Notice $notice The notice we'll display
464 function __construct($notice, $out=null)
466 parent::__construct($notice, $out);
470 * recipe function for displaying a single notice in the Facebook App.
472 * Overridden to strip out some of the controls that we don't
473 * want to be available.
481 $this->showNoticeInfo();
483 // XXX: Need to update to show attachements and controls
489 class FacebookProfileBoxNotice extends FacebookNoticeListItem
494 * Also initializes the profile attribute.
496 * @param Notice $notice The notice we'll display
498 function __construct($notice, $out=null)
500 parent::__construct($notice, $out);
504 * Recipe function for displaying a single notice in the
505 * Facebook App profile notice box
512 $this->showNoticeInfo();
513 $this->showAppLink();
516 function showAppLink()
518 $this->facebook = getFacebook();
520 $app_props = $this->facebook->api_client->Admin_getAppProperties(
521 array('canvas_name', 'application_name'));
523 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
524 $this->app_name = $app_props['application_name'];
526 $this->out->elementStart('a', array('id' => 'facebook_statusnet_app',
527 'href' => $this->app_uri));
528 $this->out->text($this->app_name);
529 $this->out->elementEnd('a');