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')));
256 if (!common_config('site', 'openidonly')) {
258 array('href' => common_local_url('register')), _('Register'));
261 array('href' => common_local_url('openidlogin')), _('Register'));
263 $this->text($loginmsg_part2);
264 $this->elementEnd('p');
265 $this->elementEnd('dd');
267 $this->elementEnd('dl');
268 $this->elementEnd('div');
271 function showLoginForm($msg = null)
274 $this->elementStart('div', array('id' => 'content'));
275 $this->element('h1', null, _('Login'));
278 $this->element('fb:error', array('message' => $msg));
281 $this->showInstructions();
283 $this->elementStart('div', array('id' => 'content_inner'));
285 $this->elementStart('form', array('method' => 'post',
286 'class' => 'form_settings',
288 'action' => 'index.php'));
290 $this->elementStart('fieldset');
292 $this->elementStart('ul', array('class' => 'form_datas'));
293 $this->elementStart('li');
294 $this->input('nickname', _('Nickname'));
295 $this->elementEnd('li');
296 $this->elementStart('li');
297 $this->password('password', _('Password'));
298 $this->elementEnd('li');
299 $this->elementEnd('ul');
301 $this->submit('submit', _('Login'));
302 $this->elementEnd('fieldset');
303 $this->elementEnd('form');
305 $this->elementStart('p');
306 $this->element('a', array('href' => common_local_url('recoverpassword')),
307 _('Lost or forgotten password?'));
308 $this->elementEnd('p');
310 $this->elementEnd('div');
311 $this->elementEnd('div');
315 function updateProfileBox($notice)
318 // Need to include inline CSS for styling the Profile box
320 $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
321 $icon_url = $app_props['icon_url'];
327 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
334 .entry-title .vcard .photo {
343 .entry-title p.entry-content {
351 div.entry-content dl,
352 div.entry-content dt,
353 div.entry-content dd {
355 text-transform:lowercase;
358 div.entry-content dd,
359 div.entry-content .device dt {
363 div.entry-content dl.timestamp dt,
364 div.entry-content dl.response dt {
367 div.entry-content dd a {
368 display:inline-block;
371 #facebook_laconica_app {
376 background:url('.$icon_url.') no-repeat 0 0;
381 $this->xw->openMemory();
383 $item = new FacebookProfileBoxNotice($notice, $this);
386 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
387 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
389 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
391 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
393 $this->xw->openURI('php://output');
397 * Generate pagination links
399 * @param boolean $have_before is there something before?
400 * @param boolean $have_after is there something after?
401 * @param integer $page current page
402 * @param string $action current action
403 * @param array $args rest of query arguments
407 function pagination($have_before, $have_after, $page, $action, $args=null)
409 // Does a little before-after block for next/prev page
410 if ($have_before || $have_after) {
411 $this->elementStart('div', array('class' => 'pagination'));
412 $this->elementStart('dl', null);
413 $this->element('dt', null, _('Pagination'));
414 $this->elementStart('dd', null);
415 $this->elementStart('ul', array('class' => 'nav'));
418 $pargs = array('page' => $page-1);
419 $newargs = $args ? array_merge($args, $pargs) : $pargs;
420 $this->elementStart('li', array('class' => 'nav_prev'));
421 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
423 $this->elementEnd('li');
426 $pargs = array('page' => $page+1);
427 $newargs = $args ? array_merge($args, $pargs) : $pargs;
428 $this->elementStart('li', array('class' => 'nav_next'));
429 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
431 $this->elementEnd('li');
433 if ($have_before || $have_after) {
434 $this->elementEnd('ul');
435 $this->elementEnd('dd');
436 $this->elementEnd('dl');
437 $this->elementEnd('div');
441 function saveNewNotice()
444 $user = $this->flink->getUser();
446 $content = $this->trimmed('status_textarea');
449 $this->showPage(_('No notice content!'));
452 $content_shortened = common_shorten_links($content);
454 if (Notice::contentTooLong($content_shortened)) {
455 $this->showPage(sprintf(_('That\'s too long. Max notice size is %d chars.'),
456 Notice::maxContent()));
461 $inter = new CommandInterpreter();
463 $cmd = $inter->handle_command($user, $content_shortened);
469 $cmd->execute(new WebChannel());
473 $replyto = $this->trimmed('inreplyto');
475 $notice = Notice::saveNew($user->id, $content,
476 'web', 1, ($replyto == 'false') ? null : $replyto);
478 if (is_string($notice)) {
479 $this->showPage($notice);
483 common_broadcast_notice($notice);
485 // Also update the user's Facebook status
486 facebookBroadcastNotice($notice);
492 class FacebookNoticeForm extends NoticeForm
495 var $post_action = null;
500 * @param HTMLOutputter $out output channel
501 * @param string $action action to return to, if any
502 * @param string $content content to pre-fill
505 function __construct($out=null, $action=null, $content=null,
506 $post_action=null, $user=null)
508 parent::__construct($out, $action, $content, $user);
509 $this->post_action = $post_action;
515 * @return string URL of the action
520 return $this->post_action;
525 class FacebookNoticeList extends NoticeList
531 * @param Notice $notice stream of notices from DB_DataObject
534 function __construct($notice, $out=null)
536 parent::__construct($notice, $out);
540 * show the list of notices
542 * "Uses up" the stream by looping through it. So, probably can't
543 * be called twice on the same list.
545 * @return int count of notices listed.
550 $this->out->elementStart('div', array('id' =>'notices_primary'));
551 $this->out->element('h2', null, _('Notices'));
552 $this->out->elementStart('ul', array('class' => 'notices'));
556 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
559 if ($cnt > NOTICES_PER_PAGE) {
563 $item = $this->newListItem($this->notice);
567 $this->out->elementEnd('ul');
568 $this->out->elementEnd('div');
574 * returns a new list item for the current notice
576 * Overridden to return a Facebook specific list item.
578 * @param Notice $notice the current notice
580 * @return FacebookNoticeListItem a list item for displaying the notice
581 * formatted for display in the Facebook App.
584 function newListItem($notice)
586 return new FacebookNoticeListItem($notice, $this);
591 class FacebookNoticeListItem extends NoticeListItem
597 * Also initializes the profile attribute.
599 * @param Notice $notice The notice we'll display
602 function __construct($notice, $out=null)
604 parent::__construct($notice, $out);
608 * recipe function for displaying a single notice in the Facebook App.
610 * Overridden to strip out some of the controls that we don't
611 * want to be available.
620 $this->showNoticeInfo();
622 // XXX: Need to update to show attachements and controls
629 class FacebookProfileBoxNotice extends FacebookNoticeListItem
635 * Also initializes the profile attribute.
637 * @param Notice $notice The notice we'll display
640 function __construct($notice, $out=null)
642 parent::__construct($notice, $out);
646 * Recipe function for displaying a single notice in the
647 * Facebook App profile notice box
655 $this->showNoticeInfo();
656 $this->showAppLink();
659 function showAppLink()
662 $this->facebook = getFacebook();
664 $app_props = $this->facebook->api_client->Admin_getAppProperties(
665 array('canvas_name', 'application_name'));
667 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
668 $this->app_name = $app_props['application_name'];
670 $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
671 'href' => $this->app_uri));
672 $this->out->text($this->app_name);
673 $this->out->elementEnd('a');