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 // Add a timestamp to the file so Facebook cache wont ignore our changes
98 $ts = filemtime(INSTALLDIR.'/theme/base/css/display.css');
100 $this->element('link', array('rel' => 'stylesheet',
101 'type' => 'text/css',
102 'href' => theme_path('css/display.css', 'base') . '?ts=' . $ts));
104 $theme = common_config('site', 'theme');
106 $ts = filemtime(INSTALLDIR. '/theme/' . $theme .'/css/display.css');
108 $this->element('link', array('rel' => 'stylesheet',
109 'type' => 'text/css',
110 'href' => theme_path('css/display.css', null) . '?ts=' . $ts));
112 $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.css');
114 $this->element('link', array('rel' => 'stylesheet',
115 'type' => 'text/css',
116 'href' => theme_path('css/facebookapp.css', 'base') . '?ts=' . $ts));
119 function showScripts()
121 // Add a timestamp to the file so Facebook cache wont ignore our changes
122 $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
124 $this->element('script', array('src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
128 * Start an Facebook ready HTML document
130 * For Facebook we don't want to actually output any headers,
131 * DTD info, etc. Just Stylesheet and JavaScript links.
133 * If $type isn't specified, will attempt to do content negotiation.
135 * @param string $type MIME type to use; default is to do negotation.
140 function startHTML($type=null)
142 $this->showStylesheets();
143 $this->showScripts();
145 $this->elementStart('div', array('class' => 'facebook-page'));
149 * Ends a Facebook ready HTML document
155 $this->elementEnd('div');
162 * MAY overload if no notice form needed... or direct message box????
166 function showNoticeForm()
168 // don't do it for most of the Facebook pages
173 $this->elementStart('div', array('id' => 'wrap'));
177 $this->elementEnd('div');
184 function showHead($error, $success)
188 $this->element("h1", null, $error);
192 $this->element("h1", null, $success);
195 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
196 $this->elementStart('span', array('id' => 'add_to_profile'));
197 $this->element('fb:add-section-button', array('section' => 'profile'));
198 $this->elementEnd('span');
199 $this->elementEnd('fb:if-section-not-added');
203 // Make this into a widget later
204 function showLocalNav()
206 $this->elementStart('ul', array('class' => 'nav'));
208 $this->elementStart('li', array('class' =>
209 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
211 array('href' => 'index.php', 'title' => _('Home')), _('Home'));
212 $this->elementEnd('li');
214 if (common_config('invite', 'enabled')) {
215 $this->elementStart('li',
217 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
219 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
220 $this->elementEnd('li');
223 $this->elementStart('li',
225 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
227 array('href' => 'settings.php',
228 'title' => _('Settings')), _('Settings'));
229 $this->elementEnd('li');
231 $this->elementEnd('ul');
235 * Show header of the page.
237 * Calls template methods
241 function showHeader()
243 $this->elementStart('div', array('id' => 'header'));
245 $this->showNoticeForm();
246 $this->elementEnd('div');
250 * Show page, a template method.
254 function showPage($error = null, $success = null)
257 $this->showHead($error, $success);
262 function showInstructions()
265 $this->elementStart('div', array('class' => 'facebook_guide'));
267 $this->elementStart('dl', array('class' => 'system_notice'));
268 $this->element('dt', null, 'Page Notice');
270 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
271 'with your username and password. Don\'t have a username yet? ');
272 $loginmsg_part2 = _(' a new account.');
274 $this->elementStart('dd');
275 $this->elementStart('p');
276 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
278 array('href' => common_local_url('register')), _('Register'));
279 $this->text($loginmsg_part2);
280 $this->elementEnd('p');
281 $this->elementEnd('dd');
283 $this->elementEnd('dl');
284 $this->elementEnd('div');
287 function showLoginForm($msg = null)
290 $this->elementStart('div', array('id' => 'content'));
291 $this->element('h1', null, _('Login'));
294 $this->element('fb:error', array('message' => $msg));
297 $this->showInstructions();
299 $this->elementStart('div', array('id' => 'content_inner'));
301 $this->elementStart('form', array('method' => 'post',
302 'class' => 'form_settings',
304 'action' => 'index.php'));
306 $this->elementStart('fieldset');
308 $this->elementStart('ul', array('class' => 'form_datas'));
309 $this->elementStart('li');
310 $this->input('nickname', _('Nickname'));
311 $this->elementEnd('li');
312 $this->elementStart('li');
313 $this->password('password', _('Password'));
314 $this->elementEnd('li');
315 $this->elementEnd('ul');
317 $this->submit('submit', _('Login'));
318 $this->elementEnd('fieldset');
319 $this->elementEnd('form');
321 $this->elementStart('p');
322 $this->element('a', array('href' => common_local_url('recoverpassword')),
323 _('Lost or forgotten password?'));
324 $this->elementEnd('p');
326 $this->elementEnd('div');
327 $this->elementEnd('div');
331 function updateProfileBox($notice)
334 // Need to include inline CSS for styling the Profile box
336 $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
337 $icon_url = $app_props['icon_url'];
343 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
350 .entry-title .vcard .photo {
359 .entry-title p.entry-content {
367 div.entry-content dl,
368 div.entry-content dt,
369 div.entry-content dd {
371 text-transform:lowercase;
374 div.entry-content dd,
375 div.entry-content .device dt {
379 div.entry-content dl.timestamp dt,
380 div.entry-content dl.response dt {
383 div.entry-content dd a {
384 display:inline-block;
387 #facebook_laconica_app {
392 background:url('.$icon_url.') no-repeat 0 0;
397 $this->xw->openMemory();
399 $item = new FacebookProfileBoxNotice($notice, $this);
402 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
403 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
405 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
407 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
409 $this->xw->openURI('php://output');
413 * Generate pagination links
415 * @param boolean $have_before is there something before?
416 * @param boolean $have_after is there something after?
417 * @param integer $page current page
418 * @param string $action current action
419 * @param array $args rest of query arguments
423 function pagination($have_before, $have_after, $page, $action, $args=null)
425 // Does a little before-after block for next/prev page
426 if ($have_before || $have_after) {
427 $this->elementStart('div', array('class' => 'pagination'));
428 $this->elementStart('dl', null);
429 $this->element('dt', null, _('Pagination'));
430 $this->elementStart('dd', null);
431 $this->elementStart('ul', array('class' => 'nav'));
434 $pargs = array('page' => $page-1);
435 $newargs = $args ? array_merge($args, $pargs) : $pargs;
436 $this->elementStart('li', array('class' => 'nav_prev'));
437 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
439 $this->elementEnd('li');
442 $pargs = array('page' => $page+1);
443 $newargs = $args ? array_merge($args, $pargs) : $pargs;
444 $this->elementStart('li', array('class' => 'nav_next'));
445 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
447 $this->elementEnd('li');
449 if ($have_before || $have_after) {
450 $this->elementEnd('ul');
451 $this->elementEnd('dd');
452 $this->elementEnd('dl');
453 $this->elementEnd('div');
457 function saveNewNotice()
460 $user = $this->flink->getUser();
462 $content = $this->trimmed('status_textarea');
465 $this->showPage(_('No notice content!'));
468 $content_shortened = common_shorten_links($content);
470 if (Notice::contentTooLong($content_shortened)) {
471 $this->showPage(sprintf(_('That\'s too long. Max notice size is %d chars.'),
472 Notice::maxContent()));
477 $inter = new CommandInterpreter();
479 $cmd = $inter->handle_command($user, $content_shortened);
485 $cmd->execute(new WebChannel());
489 $replyto = $this->trimmed('inreplyto');
491 $notice = Notice::saveNew($user->id, $content,
492 'web', 1, ($replyto == 'false') ? null : $replyto);
494 if (is_string($notice)) {
495 $this->showPage($notice);
499 common_broadcast_notice($notice);
501 // Also update the user's Facebook status
502 facebookBroadcastNotice($notice);
508 class FacebookNoticeForm extends NoticeForm
511 var $post_action = null;
516 * @param HTMLOutputter $out output channel
517 * @param string $action action to return to, if any
518 * @param string $content content to pre-fill
521 function __construct($out=null, $action=null, $content=null,
522 $post_action=null, $user=null)
524 parent::__construct($out, $action, $content, $user);
525 $this->post_action = $post_action;
531 * @return string URL of the action
536 return $this->post_action;
541 class FacebookNoticeList extends NoticeList
547 * @param Notice $notice stream of notices from DB_DataObject
550 function __construct($notice, $out=null)
552 parent::__construct($notice, $out);
556 * show the list of notices
558 * "Uses up" the stream by looping through it. So, probably can't
559 * be called twice on the same list.
561 * @return int count of notices listed.
566 $this->out->elementStart('div', array('id' =>'notices_primary'));
567 $this->out->element('h2', null, _('Notices'));
568 $this->out->elementStart('ul', array('class' => 'notices'));
572 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
575 if ($cnt > NOTICES_PER_PAGE) {
579 $item = $this->newListItem($this->notice);
583 $this->out->elementEnd('ul');
584 $this->out->elementEnd('div');
590 * returns a new list item for the current notice
592 * Overridden to return a Facebook specific list item.
594 * @param Notice $notice the current notice
596 * @return FacebookNoticeListItem a list item for displaying the notice
597 * formatted for display in the Facebook App.
600 function newListItem($notice)
602 return new FacebookNoticeListItem($notice, $this);
607 class FacebookNoticeListItem extends NoticeListItem
613 * Also initializes the profile attribute.
615 * @param Notice $notice The notice we'll display
618 function __construct($notice, $out=null)
620 parent::__construct($notice, $out);
624 * recipe function for displaying a single notice in the Facebook App.
626 * Overridden to strip out some of the controls that we don't
627 * want to be available.
636 $this->showNoticeInfo();
638 // XXX: Need to update to show attachements and controls
645 class FacebookProfileBoxNotice extends FacebookNoticeListItem
651 * Also initializes the profile attribute.
653 * @param Notice $notice The notice we'll display
656 function __construct($notice, $out=null)
658 parent::__construct($notice, $out);
662 * Recipe function for displaying a single notice in the
663 * Facebook App profile notice box
671 $this->showNoticeInfo();
672 $this->showAppLink();
675 function showAppLink()
678 $this->facebook = getFacebook();
680 $app_props = $this->facebook->api_client->Admin_getAppProperties(
681 array('canvas_name', 'application_name'));
683 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
684 $this->app_name = $app_props['application_name'];
686 $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
687 'href' => $this->app_uri));
688 $this->out->text($this->app_name);
689 $this->out->elementEnd('a');