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';
39 class FacebookAction extends Action
52 * Just wraps the HTMLOutputter constructor.
54 * @param string $output URI to output to, default = stdout
55 * @param boolean $indent Whether to indent output, default true
57 * @see XMLOutputter::__construct
58 * @see HTMLOutputter::__construct
60 function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
62 parent::__construct($output, $indent);
64 $this->facebook = $facebook;
65 $this->flink = $flink;
68 $this->fbuid = $flink->foreign_id;
69 $this->user = $flink->getUser();
72 $this->args = array();
75 function prepare($argarray)
77 parent::prepare($argarray);
79 $this->facebook = getFacebook();
80 $this->fbuid = $this->facebook->require_login();
82 $this->action = $this->trimmed('action');
84 $app_props = $this->facebook->api_client->Admin_getAppProperties(
85 array('canvas_name', 'application_name'));
87 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
88 $this->app_name = $app_props['application_name'];
90 $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
96 function showStylesheets()
98 // Add a timestamp to the file so Facebook cache wont ignore our changes
99 $ts = filemtime(INSTALLDIR.'/theme/base/css/display.css');
101 $this->element('link', array('rel' => 'stylesheet',
102 'type' => 'text/css',
103 'href' => theme_path('css/display.css', 'base') . '?ts=' . $ts));
105 $theme = common_config('site', 'theme');
107 $ts = filemtime(INSTALLDIR. '/theme/' . $theme .'/css/display.css');
109 $this->element('link', array('rel' => 'stylesheet',
110 'type' => 'text/css',
111 'href' => theme_path('css/display.css', null) . '?ts=' . $ts));
113 $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.css');
115 $this->element('link', array('rel' => 'stylesheet',
116 'type' => 'text/css',
117 'href' => theme_path('css/facebookapp.css', 'base') . '?ts=' . $ts));
120 function showScripts()
122 // Add a timestamp to the file so Facebook cache wont ignore our changes
123 $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
125 $this->element('script', array('src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
129 * Start an Facebook ready HTML document
131 * For Facebook we don't want to actually output any headers,
132 * DTD info, etc. Just Stylesheet and JavaScript links.
134 * If $type isn't specified, will attempt to do content negotiation.
136 * @param string $type MIME type to use; default is to do negotation.
141 function startHTML($type=null)
143 $this->showStylesheets();
144 $this->showScripts();
146 $this->elementStart('div', array('class' => 'facebook-page'));
150 * Ends a Facebook ready HTML document
156 $this->elementEnd('div');
163 * MAY overload if no notice form needed... or direct message box????
167 function showNoticeForm()
169 // don't do it for most of the Facebook pages
174 $this->elementStart('div', array('id' => 'wrap'));
178 $this->elementEnd('div');
185 function showHead($error, $success)
189 $this->element("h1", null, $error);
193 $this->element("h1", null, $success);
196 $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
197 $this->elementStart('span', array('id' => 'add_to_profile'));
198 $this->element('fb:add-section-button', array('section' => 'profile'));
199 $this->elementEnd('span');
200 $this->elementEnd('fb:if-section-not-added');
205 // Make this into a widget later
206 function showLocalNav()
208 $this->elementStart('ul', array('class' => 'nav'));
210 $this->elementStart('li', array('class' =>
211 ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
213 array('href' => 'index.php', 'title' => _('Home')), _('Home'));
214 $this->elementEnd('li');
216 if (common_config('invite', 'enabled')) {
217 $this->elementStart('li',
219 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
221 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
222 $this->elementEnd('li');
225 $this->elementStart('li',
227 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
229 array('href' => 'settings.php',
230 'title' => _('Settings')), _('Settings'));
231 $this->elementEnd('li');
233 $this->elementEnd('ul');
237 * Show header of the page.
239 * Calls template methods
243 function showHeader()
245 $this->elementStart('div', array('id' => 'header'));
247 $this->showNoticeForm();
248 $this->elementEnd('div');
252 * Show page, a template method.
256 function showPage($error = null, $success = null)
259 $this->showHead($error, $success);
265 function showInstructions()
268 $this->elementStart('div', array('class' => 'facebook_guide'));
270 $this->elementStart('dl', array('class' => 'system_notice'));
271 $this->element('dt', null, 'Page Notice');
273 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
274 'with your username and password. Don\'t have a username yet? ');
275 $loginmsg_part2 = _(' a new account.');
277 $this->elementStart('dd');
278 $this->elementStart('p');
279 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
281 array('href' => common_local_url('register')), _('Register'));
282 $this->text($loginmsg_part2);
283 $this->elementEnd('p');
284 $this->elementEnd('dd');
286 $this->elementEnd('dl');
287 $this->elementEnd('div');
291 function showLoginForm($msg = null)
294 $this->elementStart('div', array('id' => 'content'));
295 $this->element('h1', null, _('Login'));
298 $this->element('fb:error', array('message' => $msg));
301 $this->showInstructions();
303 $this->elementStart('div', array('id' => 'content_inner'));
305 $this->elementStart('form', array('method' => 'post',
306 'class' => 'form_settings',
308 'action' => 'index.php'));
310 $this->elementStart('fieldset');
312 $this->elementStart('ul', array('class' => 'form_datas'));
313 $this->elementStart('li');
314 $this->input('nickname', _('Nickname'));
315 $this->elementEnd('li');
316 $this->elementStart('li');
317 $this->password('password', _('Password'));
318 $this->elementEnd('li');
319 $this->elementEnd('ul');
321 $this->submit('submit', _('Login'));
322 $this->elementEnd('fieldset');
323 $this->elementEnd('form');
325 $this->elementStart('p');
326 $this->element('a', array('href' => common_local_url('recoverpassword')),
327 _('Lost or forgotten password?'));
328 $this->elementEnd('p');
330 $this->elementEnd('div');
331 $this->elementEnd('div');
336 function updateProfileBox($notice)
339 // Need to include inline CSS for styling the Profile box
341 $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
342 $icon_url = $app_props['icon_url'];
348 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
355 .entry-title .vcard .photo {
364 .entry-title p.entry-content {
372 div.entry-content dl,
373 div.entry-content dt,
374 div.entry-content dd {
376 text-transform:lowercase;
379 div.entry-content dd,
380 div.entry-content .device dt {
384 div.entry-content dl.timestamp dt,
385 div.entry-content dl.response dt {
388 div.entry-content dd a {
389 display:inline-block;
392 #facebook_laconica_app {
397 background:url('.$icon_url.') no-repeat 0 0;
402 $this->xw->openMemory();
404 $item = new FacebookProfileBoxNotice($notice, $this);
407 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
408 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
410 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
412 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
414 $this->xw->openURI('php://output');
419 * Generate pagination links
421 * @param boolean $have_before is there something before?
422 * @param boolean $have_after is there something after?
423 * @param integer $page current page
424 * @param string $action current action
425 * @param array $args rest of query arguments
429 function pagination($have_before, $have_after, $page, $action, $args=null)
431 // Does a little before-after block for next/prev page
432 if ($have_before || $have_after) {
433 $this->elementStart('div', array('class' => 'pagination'));
434 $this->elementStart('dl', null);
435 $this->element('dt', null, _('Pagination'));
436 $this->elementStart('dd', null);
437 $this->elementStart('ul', array('class' => 'nav'));
440 $pargs = array('page' => $page-1);
441 $newargs = $args ? array_merge($args, $pargs) : $pargs;
442 $this->elementStart('li', array('class' => 'nav_prev'));
443 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
445 $this->elementEnd('li');
448 $pargs = array('page' => $page+1);
449 $newargs = $args ? array_merge($args, $pargs) : $pargs;
450 $this->elementStart('li', array('class' => 'nav_next'));
451 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
453 $this->elementEnd('li');
455 if ($have_before || $have_after) {
456 $this->elementEnd('ul');
457 $this->elementEnd('dd');
458 $this->elementEnd('dl');
459 $this->elementEnd('div');
463 function saveNewNotice()
466 $user = $this->flink->getUser();
468 $content = $this->trimmed('status_textarea');
471 $this->showPage(_('No notice content!'));
474 $content_shortened = common_shorten_links($content);
476 if (mb_strlen($content_shortened) > 140) {
477 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
482 $inter = new CommandInterpreter();
484 $cmd = $inter->handle_command($user, $content_shortened);
490 $cmd->execute(new WebChannel());
494 $replyto = $this->trimmed('inreplyto');
496 $notice = Notice::saveNew($user->id, $content,
497 'web', 1, ($replyto == 'false') ? null : $replyto);
499 if (is_string($notice)) {
500 $this->showPage($notice);
504 common_broadcast_notice($notice);
506 // Also update the user's Facebook status
507 facebookBroadcastNotice($notice);
513 class FacebookNoticeForm extends NoticeForm
516 var $post_action = null;
521 * @param HTMLOutputter $out output channel
522 * @param string $action action to return to, if any
523 * @param string $content content to pre-fill
526 function __construct($out=null, $action=null, $content=null,
527 $post_action=null, $user=null)
529 parent::__construct($out, $action, $content, $user);
530 $this->post_action = $post_action;
536 * @return string URL of the action
541 return $this->post_action;
546 class FacebookNoticeList extends NoticeList
552 * @param Notice $notice stream of notices from DB_DataObject
555 function __construct($notice, $out=null)
557 parent::__construct($notice, $out);
561 * show the list of notices
563 * "Uses up" the stream by looping through it. So, probably can't
564 * be called twice on the same list.
566 * @return int count of notices listed.
571 $this->out->elementStart('div', array('id' =>'notices_primary'));
572 $this->out->element('h2', null, _('Notices'));
573 $this->out->elementStart('ul', array('class' => 'notices'));
577 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
580 if ($cnt > NOTICES_PER_PAGE) {
584 $item = $this->newListItem($this->notice);
588 $this->out->elementEnd('ul');
589 $this->out->elementEnd('div');
595 * returns a new list item for the current notice
597 * Overridden to return a Facebook specific list item.
599 * @param Notice $notice the current notice
601 * @return FacebookNoticeListItem a list item for displaying the notice
602 * formatted for display in the Facebook App.
605 function newListItem($notice)
607 return new FacebookNoticeListItem($notice, $this);
612 class FacebookNoticeListItem extends NoticeListItem
618 * Also initializes the profile attribute.
620 * @param Notice $notice The notice we'll display
623 function __construct($notice, $out=null)
625 parent::__construct($notice, $out);
629 * recipe function for displaying a single notice in the Facebook App.
631 * Overridden to strip out some of the controls that we don't
632 * want to be available.
641 $this->showNoticeInfo();
643 // XXX: Need to update to show attachements and controls
650 class FacebookProfileBoxNotice extends FacebookNoticeListItem
656 * Also initializes the profile attribute.
658 * @param Notice $notice The notice we'll display
661 function __construct($notice, $out=null)
663 parent::__construct($notice, $out);
667 * Recipe function for displaying a single notice in the
668 * Facebook App profile notice box
676 $this->showNoticeInfo();
677 $this->showAppLink();
680 function showAppLink()
683 $this->facebook = getFacebook();
685 $app_props = $this->facebook->api_client->Admin_getAppProperties(
686 array('canvas_name', 'application_name'));
688 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
689 $this->app_name = $app_props['application_name'];
691 $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
692 'href' => $this->app_uri));
693 $this->out->text($this->app_name);
694 $this->out->elementEnd('a');