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 $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');
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);
263 function showInstructions()
266 $this->elementStart('div', array('class' => 'facebook_guide'));
268 $this->elementStart('dl', array('class' => 'system_notice'));
269 $this->element('dt', null, 'Page Notice');
271 $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
272 'with your username and password. Don\'t have a username yet? ');
273 $loginmsg_part2 = _(' a new account.');
275 $this->elementStart('dd');
276 $this->elementStart('p');
277 $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
279 array('href' => common_local_url('register')), _('Register'));
280 $this->text($loginmsg_part2);
281 $this->elementEnd('p');
282 $this->elementEnd('dd');
284 $this->elementEnd('dl');
285 $this->elementEnd('div');
289 function showLoginForm($msg = null)
292 $this->elementStart('div', array('id' => 'content'));
293 $this->element('h1', null, _('Login'));
296 $this->element('fb:error', array('message' => $msg));
299 $this->showInstructions();
301 $this->elementStart('div', array('id' => 'content_inner'));
303 $this->elementStart('form', array('method' => 'post',
304 'class' => 'form_settings',
306 'action' => 'index.php'));
308 $this->elementStart('fieldset');
310 $this->elementStart('ul', array('class' => 'form_datas'));
311 $this->elementStart('li');
312 $this->input('nickname', _('Nickname'));
313 $this->elementEnd('li');
314 $this->elementStart('li');
315 $this->password('password', _('Password'));
316 $this->elementEnd('li');
317 $this->elementEnd('ul');
319 $this->submit('submit', _('Login'));
320 $this->elementEnd('fieldset');
321 $this->elementEnd('form');
323 $this->elementStart('p');
324 $this->element('a', array('href' => common_local_url('recoverpassword')),
325 _('Lost or forgotten password?'));
326 $this->elementEnd('p');
328 $this->elementEnd('div');
329 $this->elementEnd('div');
334 function updateProfileBox($notice)
337 // Need to include inline CSS for styling the Profile box
339 $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
340 $icon_url = $app_props['icon_url'];
346 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
353 .entry-title .vcard .photo {
362 .entry-title p.entry-content {
370 div.entry-content dl,
371 div.entry-content dt,
372 div.entry-content dd {
374 text-transform:lowercase;
377 div.entry-content dd,
378 div.entry-content .device dt {
382 div.entry-content dl.timestamp dt,
383 div.entry-content dl.response dt {
386 div.entry-content dd a {
387 display:inline-block;
390 #facebook_laconica_app {
395 background:url('.$icon_url.') no-repeat 0 0;
400 $this->xw->openMemory();
402 $item = new FacebookProfileBoxNotice($notice, $this);
405 $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
406 $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
408 $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
410 $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
412 $this->xw->openURI('php://output');
417 * Generate pagination links
419 * @param boolean $have_before is there something before?
420 * @param boolean $have_after is there something after?
421 * @param integer $page current page
422 * @param string $action current action
423 * @param array $args rest of query arguments
427 function pagination($have_before, $have_after, $page, $action, $args=null)
429 // Does a little before-after block for next/prev page
430 if ($have_before || $have_after) {
431 $this->elementStart('div', array('class' => 'pagination'));
432 $this->elementStart('dl', null);
433 $this->element('dt', null, _('Pagination'));
434 $this->elementStart('dd', null);
435 $this->elementStart('ul', array('class' => 'nav'));
438 $pargs = array('page' => $page-1);
439 $newargs = $args ? array_merge($args, $pargs) : $pargs;
440 $this->elementStart('li', array('class' => 'nav_prev'));
441 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
443 $this->elementEnd('li');
446 $pargs = array('page' => $page+1);
447 $newargs = $args ? array_merge($args, $pargs) : $pargs;
448 $this->elementStart('li', array('class' => 'nav_next'));
449 $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
451 $this->elementEnd('li');
453 if ($have_before || $have_after) {
454 $this->elementEnd('ul');
455 $this->elementEnd('dd');
456 $this->elementEnd('dl');
457 $this->elementEnd('div');
461 function updateFacebookStatus($notice)
463 $prefix = $this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $this->fbuid);
464 $content = "$prefix $notice->content";
466 if ($this->facebook->api_client->users_hasAppPermission('status_update', $this->fbuid)) {
467 $this->facebook->api_client->users_setStatus($content, $this->fbuid, false, true);
471 function saveNewNotice()
474 $user = $this->flink->getUser();
476 $content = $this->trimmed('status_textarea');
479 $this->showPage(_('No notice content!'));
482 $content_shortened = common_shorten_links($content);
484 if (mb_strlen($content_shortened) > 140) {
485 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
490 $inter = new CommandInterpreter();
492 $cmd = $inter->handle_command($user, $content_shortened);
498 $cmd->execute(new WebChannel());
502 $replyto = $this->trimmed('inreplyto');
504 $notice = Notice::saveNew($user->id, $content,
505 'Facebook', 1, ($replyto == 'false') ? null : $replyto);
507 if (is_string($notice)) {
508 $this->showPage($notice);
512 common_broadcast_notice($notice);
514 // Also update the user's Facebook status
515 $this->updateFacebookStatus($notice);
516 $this->updateProfileBox($notice);
522 class FacebookNoticeForm extends NoticeForm
525 var $post_action = null;
530 * @param HTMLOutputter $out output channel
531 * @param string $action action to return to, if any
532 * @param string $content content to pre-fill
535 function __construct($out=null, $action=null, $content=null,
536 $post_action=null, $user=null)
538 parent::__construct($out, $action, $content, $user);
539 $this->post_action = $post_action;
545 * @return string URL of the action
550 return $this->post_action;
555 class FacebookNoticeList extends NoticeList
561 * @param Notice $notice stream of notices from DB_DataObject
564 function __construct($notice, $out=null)
566 parent::__construct($notice, $out);
570 * show the list of notices
572 * "Uses up" the stream by looping through it. So, probably can't
573 * be called twice on the same list.
575 * @return int count of notices listed.
580 $this->out->elementStart('div', array('id' =>'notices_primary'));
581 $this->out->element('h2', null, _('Notices'));
582 $this->out->elementStart('ul', array('class' => 'notices'));
586 while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
589 if ($cnt > NOTICES_PER_PAGE) {
593 $item = $this->newListItem($this->notice);
597 $this->out->elementEnd('ul');
598 $this->out->elementEnd('div');
604 * returns a new list item for the current notice
606 * Overridden to return a Facebook specific list item.
608 * @param Notice $notice the current notice
610 * @return FacebookNoticeListItem a list item for displaying the notice
611 * formatted for display in the Facebook App.
614 function newListItem($notice)
616 return new FacebookNoticeListItem($notice, $this);
621 class FacebookNoticeListItem extends NoticeListItem
627 * Also initializes the profile attribute.
629 * @param Notice $notice The notice we'll display
632 function __construct($notice, $out=null)
634 parent::__construct($notice, $out);
638 * recipe function for displaying a single notice in the Facebook App.
640 * Overridden to strip out some of the controls that we don't
641 * want to be available.
650 $this->out->elementStart('div', 'entry-title');
652 $this->showContent();
653 $this->out->elementEnd('div');
655 $this->out->elementStart('div', 'entry-content');
656 $this->showNoticeLink();
657 $this->showNoticeSource();
658 $this->showReplyTo();
659 $this->out->elementEnd('div');
664 function showNoticeLink()
666 $noticeurl = common_local_url('shownotice',
667 array('notice' => $this->notice->id));
668 // XXX: we need to figure this out better. Is this right?
669 if (strcmp($this->notice->uri, $noticeurl) != 0 &&
670 preg_match('/^http/', $this->notice->uri)) {
671 $noticeurl = $this->notice->uri;
674 $this->out->elementStart('dl', 'timestamp');
675 $this->out->element('dt', null, _('Published'));
676 $this->out->elementStart('dd', null);
677 $this->out->elementStart('a', array('rel' => 'bookmark',
678 'href' => $noticeurl));
679 $dt = common_date_iso8601($this->notice->created);
680 $this->out->element('abbr', array('class' => 'published',
682 common_date_string($this->notice->created));
683 $this->out->elementEnd('a');
684 $this->out->elementEnd('dd');
685 $this->out->elementEnd('dl');
691 class FacebookProfileBoxNotice extends FacebookNoticeListItem
697 * Also initializes the profile attribute.
699 * @param Notice $notice The notice we'll display
702 function __construct($notice, $out=null)
704 parent::__construct($notice, $out);
708 * Recipe function for displaying a single notice in the
709 * Facebook App's Profile
717 $this->out->elementStart('div', 'entry-title');
719 $this->showContent();
720 $this->out->elementEnd('div');
722 $this->out->elementStart('div', 'entry-content');
724 $this->showNoticeLink();
725 $this->showNoticeSource();
726 $this->showReplyTo();
727 $this->out->elementEnd('div');
729 $this->showAppLink();
733 function showAppLink()
736 $this->facebook = getFacebook();
738 $app_props = $this->facebook->api_client->Admin_getAppProperties(
739 array('canvas_name', 'application_name'));
741 $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
742 $this->app_name = $app_props['application_name'];
744 $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
745 'href' => $this->app_uri));
746 $this->out->text($this->app_name);
747 $this->out->elementEnd('a');