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,
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->elementStart('div', array('class' => 'facebook-page'));
147 * Ends a Facebook ready HTML document
153 $this->elementEnd('div');
160 * MAY overload if no notice form needed... or direct message box????
164 function showNoticeForm()
166 // don't do it for most of the Facebook pages
171 $this->elementStart('div', array('id' => 'wrap'));
175 $this->elementEnd('div');
182 function showHead($error, $success)
184 $this->showStylesheets();
185 $this->showScripts();
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');
204 // Make this into a widget later
205 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 $this->elementStart('li',
218 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
220 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
221 $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');
236 * Show primary navigation.
240 function showPrimaryNav()
242 // we don't want to show anything for this
246 * Show header of the page.
248 * Calls template methods
252 function showHeader()
254 $this->elementStart('div', array('id' => 'header'));
256 $this->showNoticeForm();
257 $this->showPrimaryNav();
258 $this->elementEnd('div');
262 * Show page, a template method.
266 function showPage($error = null, $success = null)
269 $this->showHead($error, $success);
275 function showInstructions()
278 $this->elementStart('div', array('class' => 'facebook_guide'));
280 $this->elementStart('dl', array('class' => 'system_notice'));
281 $this->element('dt', null, 'Page Notice');
283 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
284 'with your username and password. Don\'t have a username yet? ');
286 $loginmsg_part2 = _(' a new account.');
288 $this->elementStart('dd');
289 $this->elementStart('p');
290 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
292 array('href' => common_local_url('register')), _('Register'));
293 $this->text($loginmsg_part2);
294 $this->elementEnd('dd');
295 $this->elementEnd('dl');
297 $this->elementEnd('div');
302 function showLoginForm($msg = null)
305 $this->elementStart('div', array('class' => 'content'));
306 $this->element('h1', null, _('Login'));
309 $this->element('fb:error', array('message' => $msg));
312 $this->showInstructions();
314 $this->elementStart('div', array('id' => 'content_inner'));
316 $this->elementStart('form', array('method' => 'post',
317 'class' => 'form_settings',
319 'action' => 'index.php'));
321 $this->elementStart('fieldset');
323 $this->elementStart('ul', array('class' => 'form_datas'));
324 $this->elementStart('li');
325 $this->input('nickname', _('Nickname'));
326 $this->elementEnd('li');
327 $this->elementStart('li');
328 $this->password('password', _('Password'));
329 $this->elementEnd('li');
330 $this->elementEnd('ul');
332 $this->submit('submit', _('Login'));
333 $this->elementEnd('form');
335 $this->elementStart('p');
336 $this->element('a', array('href' => common_local_url('recoverpassword')),
337 _('Lost or forgotten password?'));
338 $this->elementEnd('p');
340 $this->elementEnd('div');
345 function updateProfileBox($notice)
348 // Need to include inline CSS for styling the Profile box
351 .entry-title .vcard .photo {
355 .entry-title .vcard .nickname {
359 .entry-title p.entry-content {
364 div.entry-content dl,
365 div.entry-content dt,
366 div.entry-content dd {
370 div.entry-content dt,
371 div.entry-content dd {
375 div.entry-content dl.timestamp dt {
378 div.entry-content dd a {
379 display:inline-block;
383 $this->xw->openMemory();
385 $item = new FacebookNoticeListItem($notice, $this);
388 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
389 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
391 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
393 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
395 $this->xw->openURI('php://output');
400 * Generate pagination links
402 * @param boolean $have_before is there something before?
403 * @param boolean $have_after is there something after?
404 * @param integer $page current page
405 * @param string $action current action
406 * @param array $args rest of query arguments
410 function pagination($have_before, $have_after, $page, $action, $args=null)
412 // Does a little before-after block for next/prev page
413 if ($have_before || $have_after) {
414 $this->elementStart('div', array('class' => 'pagination'));
415 $this->elementStart('dl', null);
416 $this->element('dt', null, _('Pagination'));
417 $this->elementStart('dd', null);
418 $this->elementStart('ul', array('class' => 'nav'));
421 $pargs = array('page' => $page-1);
422 $newargs = $args ? array_merge($args, $pargs) : $pargs;
423 $this->elementStart('li', array('class' => 'nav_prev'));
424 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
426 $this->elementEnd('li');
429 $pargs = array('page' => $page+1);
430 $newargs = $args ? array_merge($args, $pargs) : $pargs;
431 $this->elementStart('li', array('class' => 'nav_next'));
432 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
434 $this->elementEnd('li');
436 if ($have_before || $have_after) {
437 $this->elementEnd('ul');
438 $this->elementEnd('dd');
439 $this->elementEnd('dl');
440 $this->elementEnd('div');
444 function updateFacebookStatus($notice)
446 $prefix = $this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $this->fbuid);
447 $content = "$prefix $notice->content";
449 if ($this->facebook->api_client->users_hasAppPermission('status_update', $this->fbuid)) {
450 $this->facebook->api_client->users_setStatus($content, $this->fbuid, false, true);
454 function saveNewNotice()
457 $user = $this->flink->getUser();
459 $content = $this->trimmed('status_textarea');
462 $this->showPage(_('No notice content!'));
465 $content_shortened = common_shorten_links($content);
467 if (mb_strlen($content_shortened) > 140) {
468 common_debug("Content = '$content_shortened'", __FILE__);
469 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
470 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
475 $inter = new CommandInterpreter();
477 $cmd = $inter->handle_command($user, $content_shortened);
483 $cmd->execute(new WebChannel());
487 $replyto = $this->trimmed('inreplyto');
489 $notice = Notice::saveNew($user->id, $content,
490 'Facebook', 1, ($replyto == 'false') ? null : $replyto);
492 if (is_string($notice)) {
493 $this->showPage($notice);
497 common_broadcast_notice($notice);
499 // Also update the user's Facebook status
500 $this->updateFacebookStatus($notice);
501 $this->updateProfileBox($notice);
507 class FacebookNoticeForm extends NoticeForm
510 var $post_action = null;
515 * @param HTMLOutputter $out output channel
516 * @param string $action action to return to, if any
517 * @param string $content content to pre-fill
520 function __construct($out=null, $action=null, $content=null,
521 $post_action=null, $user=null)
523 parent::__construct($out, $action, $content, $user);
524 $this->post_action = $post_action;
530 * @return string URL of the action
535 return $this->post_action;
540 class FacebookNoticeList extends NoticeList
543 * show the list of notices
545 * "Uses up" the stream by looping through it. So, probably can't
546 * be called twice on the same list.
548 * @return int count of notices listed.
553 $this->out->elementStart('div', array('id' =>'notices_primary'));
554 $this->out->element('h2', null, _('Notices'));
555 $this->out->elementStart('ul', array('class' => 'notices'));
559 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
562 if ($cnt > NOTICES_PER_PAGE) {
566 $item = $this->newListItem($this->notice);
570 $this->out->elementEnd('ul');
571 $this->out->elementEnd('div');
577 * returns a new list item for the current notice
579 * Overridden to return a Facebook specific list item.
581 * @param Notice $notice the current notice
583 * @return FacebookNoticeListItem a list item for displaying the notice
584 * formatted for display in the Facebook App.
587 function newListItem($notice)
589 return new FacebookNoticeListItem($notice, $this);
594 class FacebookNoticeListItem extends NoticeListItem
597 * recipe function for displaying a single notice in the Facebook App.
599 * Overridden to strip out some of the controls that we don't
600 * want to be available.
609 $this->out->elementStart('div', 'entry-title');
611 $this->showContent();
612 $this->out->elementEnd('div');
614 $this->out->elementStart('div', 'entry-content');
615 $this->showNoticeLink();
616 $this->showNoticeSource();
617 $this->showReplyTo();
618 $this->out->elementEnd('div');
623 function showNoticeLink()
625 $noticeurl = common_local_url('shownotice',
626 array('notice' => $this->notice->id));
627 // XXX: we need to figure this out better. Is this right?
628 if (strcmp($this->notice->uri, $noticeurl) != 0 &&
629 preg_match('/^http/', $this->notice->uri)) {
630 $noticeurl = $this->notice->uri;
633 $this->out->elementStart('dl', 'timestamp');
634 $this->out->element('dt', null, _('Published'));
635 $this->out->elementStart('dd', null);
636 $this->out->elementStart('a', array('rel' => 'bookmark',
637 'href' => $noticeurl));
638 $dt = common_date_iso8601($this->notice->created);
639 $this->out->element('abbr', array('class' => 'published',
641 common_date_string($this->notice->created));
642 $this->out->elementEnd('a');
643 $this->out->elementEnd('dd');
644 $this->out->elementEnd('dl');