3 * Laconica, the distributed open-source microblogging tool
5 * Low-level generator for HTML
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@controlyourself.ca>
25 * @copyright 2008 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA'))
35 require_once INSTALLDIR.'/lib/facebookutil.php';
36 require_once INSTALLDIR.'/lib/noticeform.php';
38 class FacebookAction extends Action
51 * Just wraps the HTMLOutputter constructor.
53 * @param string $output URI to output to, default = stdout
54 * @param boolean $indent Whether to indent output, default true
56 * @see XMLOutputter::__construct
57 * @see HTMLOutputter::__construct
59 function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
61 parent::__construct($output, $indent);
63 $this->facebook = $facebook;
64 $this->flink = $flink;
67 $this->fbuid = $flink->foreign_id;
68 $this->user = $flink->getUser();
71 $this->args = array();
74 function prepare($argarray)
76 parent::prepare($argarray);
78 $this->facebook = getFacebook();
79 $this->fbuid = $this->facebook->require_login();
81 $this->action = $this->trimmed('action');
83 $app_props = $this->facebook->api_client->Admin_getAppProperties(
84 array('canvas_name', 'application_name'));
86 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
87 $this->app_name = $app_props['application_name'];
89 $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
95 function showStylesheets()
97 $this->cssLink('css/display.css', 'base');
98 $this->cssLink('css/facebookapp.css', 'base');
101 function showScripts()
103 $this->script('js/facebookapp.js');
107 * Start an Facebook ready HTML document
109 * For Facebook we don't want to actually output any headers,
110 * DTD info, etc. Just Stylesheet and JavaScript links.
112 * If $type isn't specified, will attempt to do content negotiation.
114 * @param string $type MIME type to use; default is to do negotation.
119 function startHTML($type=null)
121 $this->showStylesheets();
122 $this->showScripts();
124 $this->elementStart('div', array('class' => 'facebook-page'));
128 * Ends a Facebook ready HTML document
134 $this->elementEnd('div');
141 * MAY overload if no notice form needed... or direct message box????
145 function showNoticeForm()
147 // don't do it for most of the Facebook pages
152 $this->elementStart('div', array('id' => 'wrap'));
156 $this->elementEnd('div');
163 function showHead($error, $success)
167 $this->element("h1", null, $error);
171 $this->element("h1", null, $success);
174 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
175 $this->elementStart('span', array('id' => 'add_to_profile'));
176 $this->element('fb:add-section-button', array('section' => 'profile'));
177 $this->elementEnd('span');
178 $this->elementEnd('fb:if-section-not-added');
182 // Make this into a widget later
183 function showLocalNav()
185 $this->elementStart('ul', array('class' => 'nav'));
187 $this->elementStart('li', array('class' =>
188 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
190 array('href' => 'index.php', 'title' => _('Home')), _('Home'));
191 $this->elementEnd('li');
193 if (common_config('invite', 'enabled')) {
194 $this->elementStart('li',
196 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
198 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
199 $this->elementEnd('li');
202 $this->elementStart('li',
204 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
206 array('href' => 'settings.php',
207 'title' => _('Settings')), _('Settings'));
208 $this->elementEnd('li');
210 $this->elementEnd('ul');
214 * Show header of the page.
216 * Calls template methods
220 function showHeader()
222 $this->elementStart('div', array('id' => 'header'));
224 $this->showNoticeForm();
225 $this->elementEnd('div');
229 * Show page, a template method.
233 function showPage($error = null, $success = null)
236 $this->showHead($error, $success);
241 function showInstructions()
244 $this->elementStart('div', array('class' => 'facebook_guide'));
246 $this->elementStart('dl', array('class' => 'system_notice'));
247 $this->element('dt', null, 'Page Notice');
249 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
250 'with your username and password. Don\'t have a username yet? ');
251 $loginmsg_part2 = _(' a new account.');
253 $this->elementStart('dd');
254 $this->elementStart('p');
255 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
257 array('href' => common_local_url('register')), _('Register'));
258 $this->text($loginmsg_part2);
259 $this->elementEnd('p');
260 $this->elementEnd('dd');
262 $this->elementEnd('dl');
263 $this->elementEnd('div');
266 function showLoginForm($msg = null)
269 $this->elementStart('div', array('id' => 'content'));
270 $this->element('h1', null, _('Login'));
273 $this->element('fb:error', array('message' => $msg));
276 $this->showInstructions();
278 $this->elementStart('div', array('id' => 'content_inner'));
280 $this->elementStart('form', array('method' => 'post',
281 'class' => 'form_settings',
283 'action' => 'index.php'));
285 $this->elementStart('fieldset');
287 $this->elementStart('ul', array('class' => 'form_datas'));
288 $this->elementStart('li');
289 $this->input('nickname', _('Nickname'));
290 $this->elementEnd('li');
291 $this->elementStart('li');
292 $this->password('password', _('Password'));
293 $this->elementEnd('li');
294 $this->elementEnd('ul');
296 $this->submit('submit', _('Login'));
297 $this->elementEnd('fieldset');
298 $this->elementEnd('form');
300 $this->elementStart('p');
301 $this->element('a', array('href' => common_local_url('recoverpassword')),
302 _('Lost or forgotten password?'));
303 $this->elementEnd('p');
305 $this->elementEnd('div');
306 $this->elementEnd('div');
310 function updateProfileBox($notice)
313 // Need to include inline CSS for styling the Profile box
315 $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
316 $icon_url = $app_props['icon_url'];
322 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
329 .entry-title .vcard .photo {
338 .entry-title p.entry-content {
346 div.entry-content dl,
347 div.entry-content dt,
348 div.entry-content dd {
350 text-transform:lowercase;
353 div.entry-content dd,
354 div.entry-content .device dt {
358 div.entry-content dl.timestamp dt,
359 div.entry-content dl.response dt {
362 div.entry-content dd a {
363 display:inline-block;
366 #facebook_laconica_app {
371 background:url('.$icon_url.') no-repeat 0 0;
376 $this->xw->openMemory();
378 $item = new FacebookProfileBoxNotice($notice, $this);
381 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
382 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
384 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
386 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
388 $this->xw->openURI('php://output');
392 * Generate pagination links
394 * @param boolean $have_before is there something before?
395 * @param boolean $have_after is there something after?
396 * @param integer $page current page
397 * @param string $action current action
398 * @param array $args rest of query arguments
402 function pagination($have_before, $have_after, $page, $action, $args=null)
404 // Does a little before-after block for next/prev page
405 if ($have_before || $have_after) {
406 $this->elementStart('div', array('class' => 'pagination'));
407 $this->elementStart('dl', null);
408 $this->element('dt', null, _('Pagination'));
409 $this->elementStart('dd', null);
410 $this->elementStart('ul', array('class' => 'nav'));
413 $pargs = array('page' => $page-1);
414 $newargs = $args ? array_merge($args, $pargs) : $pargs;
415 $this->elementStart('li', array('class' => 'nav_prev'));
416 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
418 $this->elementEnd('li');
421 $pargs = array('page' => $page+1);
422 $newargs = $args ? array_merge($args, $pargs) : $pargs;
423 $this->elementStart('li', array('class' => 'nav_next'));
424 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
426 $this->elementEnd('li');
428 if ($have_before || $have_after) {
429 $this->elementEnd('ul');
430 $this->elementEnd('dd');
431 $this->elementEnd('dl');
432 $this->elementEnd('div');
436 function saveNewNotice()
439 $user = $this->flink->getUser();
441 $content = $this->trimmed('status_textarea');
444 $this->showPage(_('No notice content!'));
447 $content_shortened = common_shorten_links($content);
449 if (Notice::contentTooLong($content_shortened)) {
450 $this->showPage(sprintf(_('That\'s too long. Max notice size is %d chars.'),
451 Notice::maxContent()));
456 $inter = new CommandInterpreter();
458 $cmd = $inter->handle_command($user, $content_shortened);
464 $cmd->execute(new WebChannel());
468 $replyto = $this->trimmed('inreplyto');
470 $notice = Notice::saveNew($user->id, $content,
471 'web', 1, ($replyto == 'false') ? null : $replyto);
473 if (is_string($notice)) {
474 $this->showPage($notice);
478 common_broadcast_notice($notice);
480 // Also update the user's Facebook status
481 facebookBroadcastNotice($notice);
487 class FacebookNoticeForm extends NoticeForm
490 var $post_action = null;
495 * @param HTMLOutputter $out output channel
496 * @param string $action action to return to, if any
497 * @param string $content content to pre-fill
500 function __construct($out=null, $action=null, $content=null,
501 $post_action=null, $user=null)
503 parent::__construct($out, $action, $content, $user);
504 $this->post_action = $post_action;
510 * @return string URL of the action
515 return $this->post_action;
520 class FacebookNoticeList extends NoticeList
526 * @param Notice $notice stream of notices from DB_DataObject
529 function __construct($notice, $out=null)
531 parent::__construct($notice, $out);
535 * show the list of notices
537 * "Uses up" the stream by looping through it. So, probably can't
538 * be called twice on the same list.
540 * @return int count of notices listed.
545 $this->out->elementStart('div', array('id' =>'notices_primary'));
546 $this->out->element('h2', null, _('Notices'));
547 $this->out->elementStart('ul', array('class' => 'notices'));
551 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
554 if ($cnt > NOTICES_PER_PAGE) {
558 $item = $this->newListItem($this->notice);
562 $this->out->elementEnd('ul');
563 $this->out->elementEnd('div');
569 * returns a new list item for the current notice
571 * Overridden to return a Facebook specific list item.
573 * @param Notice $notice the current notice
575 * @return FacebookNoticeListItem a list item for displaying the notice
576 * formatted for display in the Facebook App.
579 function newListItem($notice)
581 return new FacebookNoticeListItem($notice, $this);
586 class FacebookNoticeListItem extends NoticeListItem
592 * Also initializes the profile attribute.
594 * @param Notice $notice The notice we'll display
597 function __construct($notice, $out=null)
599 parent::__construct($notice, $out);
603 * recipe function for displaying a single notice in the Facebook App.
605 * Overridden to strip out some of the controls that we don't
606 * want to be available.
615 $this->showNoticeInfo();
617 // XXX: Need to update to show attachements and controls
624 class FacebookProfileBoxNotice extends FacebookNoticeListItem
630 * Also initializes the profile attribute.
632 * @param Notice $notice The notice we'll display
635 function __construct($notice, $out=null)
637 parent::__construct($notice, $out);
641 * Recipe function for displaying a single notice in the
642 * Facebook App profile notice box
650 $this->showNoticeInfo();
651 $this->showAppLink();
654 function showAppLink()
657 $this->facebook = getFacebook();
659 $app_props = $this->facebook->api_client->Admin_getAppProperties(
660 array('canvas_name', 'application_name'));
662 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
663 $this->app_name = $app_props['application_name'];
665 $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
666 'href' => $this->app_uri));
667 $this->out->text($this->app_name);
668 $this->out->elementEnd('a');