]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookaction.php
Merge commit 'jeff-themovie/small-fixes' into 0.8.x
[quix0rs-gnu-social.git] / lib / facebookaction.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Low-level generator for HTML
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Faceboook
23  * @package   Laconica
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/
28  */
29
30 if (!defined('LACONICA'))
31 {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/facebookutil.php';
36 require_once INSTALLDIR.'/lib/noticeform.php';
37
38
39 class FacebookAction extends Action
40 {
41
42     var $facebook = null;
43     var $fbuid    = null;
44     var $flink    = null;
45     var $action   = null;
46     var $app_uri  = null;
47     var $app_name = null;
48
49     /**
50      * Constructor
51      *
52      * Just wraps the HTMLOutputter constructor.
53      *
54      * @param string  $output URI to output to, default = stdout
55      * @param boolean $indent Whether to indent output, default true
56      *
57      * @see XMLOutputter::__construct
58      * @see HTMLOutputter::__construct
59      */
60     function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
61     {
62         parent::__construct($output, $indent);
63
64         $this->facebook = $facebook;
65         $this->flink = $flink;
66
67         if ($this->flink) {
68             $this->fbuid = $flink->foreign_id;
69             $this->user = $flink->getUser();
70         }
71
72         $this->args = array();
73     }
74
75     function prepare($argarray)
76     {
77         parent::prepare($argarray);
78
79         $this->facebook = getFacebook();
80         $this->fbuid = $this->facebook->require_login();
81
82         $this->action = $this->trimmed('action');
83
84         $app_props = $this->facebook->api_client->Admin_getAppProperties(
85                 array('canvas_name', 'application_name'));
86
87         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
88         $this->app_name = $app_props['application_name'];
89
90         $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
91
92         return true;
93
94     }
95
96     function showStylesheets()
97     {
98         // Add a timestamp to the file so Facebook cache wont ignore our changes
99         $ts = filemtime(INSTALLDIR.'/theme/base/css/display.css');
100
101     $this->element('link', array('rel' => 'stylesheet',
102                'type' => 'text/css',
103                'href' => theme_path('css/display.css', 'base') . '?ts=' . $ts));
104
105         $theme = common_config('site', 'theme');
106
107         $ts = filemtime(INSTALLDIR. '/theme/' . $theme .'/css/display.css');
108
109         $this->element('link', array('rel' => 'stylesheet',
110                                      'type' => 'text/css',
111                                      'href' => theme_path('css/display.css', null) . '?ts=' . $ts));
112
113         $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.css');
114
115         $this->element('link', array('rel' => 'stylesheet',
116                                      'type' => 'text/css',
117                                      'href' => theme_path('css/facebookapp.css', 'base') . '?ts=' . $ts));
118     }
119
120     function showScripts()
121     {
122         // Add a timestamp to the file so Facebook cache wont ignore our changes
123         $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
124
125         $this->element('script', array('src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
126     }
127
128     /**
129      * Start an Facebook ready HTML document
130      *
131      *  For Facebook we don't want to actually output any headers,
132      *  DTD info, etc.  Just Stylesheet and JavaScript links.
133      *
134      * If $type isn't specified, will attempt to do content negotiation.
135      *
136      * @param string $type MIME type to use; default is to do negotation.
137      *
138      * @return void
139      */
140
141     function startHTML($type=null)
142     {
143         $this->showStylesheets();
144         $this->showScripts();
145
146         $this->elementStart('div', array('class' => 'facebook-page'));
147     }
148
149     /**
150     *  Ends a Facebook ready HTML document
151     *
152     *  @return void
153     */
154     function endHTML()
155     {
156         $this->elementEnd('div');
157         $this->endXML();
158     }
159
160     /**
161      * Show notice form.
162      *
163      * MAY overload if no notice form needed... or direct message box????
164      *
165      * @return nothing
166      */
167     function showNoticeForm()
168     {
169         // don't do it for most of the Facebook pages
170     }
171
172     function showBody()
173     {
174         $this->elementStart('div', array('id' => 'wrap'));
175         $this->showHeader();
176         $this->showCore();
177         $this->showFooter();
178         $this->elementEnd('div');
179     }
180
181     function showAside()
182     {
183     }
184
185     function showHead($error, $success)
186     {
187
188         if ($error) {
189             $this->element("h1", null, $error);
190         }
191
192         if ($success) {
193             $this->element("h1", null, $success);
194         }
195
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');
201
202     }
203
204
205     // Make this into a widget later
206     function showLocalNav()
207     {
208         $this->elementStart('ul', array('class' => 'nav'));
209
210         $this->elementStart('li', array('class' =>
211             ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
212         $this->element('a',
213             array('href' => 'index.php', 'title' => _('Home')), _('Home'));
214         $this->elementEnd('li');
215
216         if (common_config('invite', 'enabled')) {
217             $this->elementStart('li',
218                 array('class' =>
219                     ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
220             $this->element('a',
221                 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
222             $this->elementEnd('li');
223         }
224
225         $this->elementStart('li',
226             array('class' =>
227                 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
228         $this->element('a',
229             array('href' => 'settings.php',
230                 'title' => _('Settings')), _('Settings'));
231         $this->elementEnd('li');
232
233         $this->elementEnd('ul');
234     }
235
236     /**
237      * Show header of the page.
238      *
239      * Calls template methods
240      *
241      * @return nothing
242      */
243     function showHeader()
244     {
245         $this->elementStart('div', array('id' => 'header'));
246         $this->showLogo();
247         $this->showNoticeForm();
248         $this->elementEnd('div');
249     }
250
251     /**
252      * Show page, a template method.
253      *
254      * @return nothing
255      */
256     function showPage($error = null, $success = null)
257     {
258         $this->startHTML();
259         $this->showHead($error, $success);
260         $this->showBody();
261         $this->endHTML();
262     }
263
264
265     function showInstructions()
266     {
267
268         $this->elementStart('div', array('class' => 'facebook_guide'));
269
270         $this->elementStart('dl', array('class' => 'system_notice'));
271         $this->element('dt', null, 'Page Notice');
272
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.');
276
277         $this->elementStart('dd');
278         $this->elementStart('p');
279         $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
280         $this->element('a',
281             array('href' => common_local_url('register')), _('Register'));
282         $this->text($loginmsg_part2);
283     $this->elementEnd('p');
284         $this->elementEnd('dd');
285
286         $this->elementEnd('dl');
287         $this->elementEnd('div');
288     }
289
290
291     function showLoginForm($msg = null)
292     {
293
294         $this->elementStart('div', array('id' => 'content'));
295         $this->element('h1', null, _('Login'));
296
297         if ($msg) {
298              $this->element('fb:error', array('message' => $msg));
299         }
300
301         $this->showInstructions();
302
303         $this->elementStart('div', array('id' => 'content_inner'));
304
305         $this->elementStart('form', array('method' => 'post',
306                                                'class' => 'form_settings',
307                                                'id' => 'login',
308                                                'action' => 'index.php'));
309
310         $this->elementStart('fieldset');
311
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');
320
321         $this->submit('submit', _('Login'));
322     $this->elementEnd('fieldset');
323         $this->elementEnd('form');
324
325         $this->elementStart('p');
326         $this->element('a', array('href' => common_local_url('recoverpassword')),
327                        _('Lost or forgotten password?'));
328         $this->elementEnd('p');
329
330         $this->elementEnd('div');
331         $this->elementEnd('div');
332
333     }
334
335
336     function updateProfileBox($notice)
337     {
338
339         // Need to include inline CSS for styling the Profile box
340
341     $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
342     $icon_url = $app_props['icon_url'];
343
344         $style = '<style>
345      .entry-title *,
346      .entry-content * {
347      font-size:14px;
348      font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
349      }
350      .entry-title a,
351      .entry-content a {
352      color:#002E6E;
353      }
354
355          .entry-title .vcard .photo {
356          float:left;
357          display:inline;
358      margin-right:11px;
359      margin-bottom:11px
360          }
361      .entry-title {
362      margin-bottom:11px;
363      }
364          .entry-title p.entry-content {
365          display:inline;
366      margin-left:5px;
367          }
368
369      div.entry-content {
370      clear:both;
371      }
372          div.entry-content dl,
373          div.entry-content dt,
374          div.entry-content dd {
375          display:inline;
376      text-transform:lowercase;
377          }
378
379          div.entry-content dd,
380      div.entry-content .device dt {
381      margin-left:0;
382      margin-right:5px;
383          }
384          div.entry-content dl.timestamp dt,
385      div.entry-content dl.response dt {
386          display:none;
387          }
388          div.entry-content dd a {
389          display:inline-block;
390          }
391
392      #facebook_laconica_app {
393      text-indent:-9999px;
394      height:16px;
395      width:16px;
396      display:block;
397      background:url('.$icon_url.') no-repeat 0 0;
398      float:right;
399      }
400          </style>';
401
402         $this->xw->openMemory();
403
404         $item = new FacebookProfileBoxNotice($notice, $this);
405         $item->show();
406
407         $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
408         $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
409
410         $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
411
412         $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
413
414         $this->xw->openURI('php://output');
415     }
416
417
418     /**
419      * Generate pagination links
420      *
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
426      *
427      * @return nothing
428      */
429     function pagination($have_before, $have_after, $page, $action, $args=null)
430     {
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'));
438         }
439         if ($have_before) {
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'),
444                            _('After'));
445             $this->elementEnd('li');
446         }
447         if ($have_after) {
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'),
452                            _('Before'));
453             $this->elementEnd('li');
454         }
455         if ($have_before || $have_after) {
456             $this->elementEnd('ul');
457             $this->elementEnd('dd');
458             $this->elementEnd('dl');
459             $this->elementEnd('div');
460         }
461     }
462
463     function updateFacebookStatus($notice)
464     {
465         $prefix = $this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $this->fbuid);
466         $content = "$prefix $notice->content";
467
468         if ($this->facebook->api_client->users_hasAppPermission('status_update', $this->fbuid)) {
469             $this->facebook->api_client->users_setStatus($content, $this->fbuid, false, true);
470         }
471     }
472
473     function saveNewNotice()
474     {
475
476         $user = $this->flink->getUser();
477
478         $content = $this->trimmed('status_textarea');
479
480         if (!$content) {
481             $this->showPage(_('No notice content!'));
482             return;
483         } else {
484             $content_shortened = common_shorten_links($content);
485
486             if (mb_strlen($content_shortened) > 140) {
487                 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
488                 return;
489             }
490         }
491
492         $inter = new CommandInterpreter();
493
494         $cmd = $inter->handle_command($user, $content_shortened);
495
496         if ($cmd) {
497
498             // XXX fix this
499
500             $cmd->execute(new WebChannel());
501             return;
502         }
503
504         $replyto = $this->trimmed('inreplyto');
505
506         $notice = Notice::saveNew($user->id, $content,
507             'Facebook', 1, ($replyto == 'false') ? null : $replyto);
508
509         if (is_string($notice)) {
510             $this->showPage($notice);
511             return;
512         }
513
514         common_broadcast_notice($notice);
515
516         // Also update the user's Facebook status
517         $this->updateFacebookStatus($notice);
518         $this->updateProfileBox($notice);
519
520     }
521
522 }
523
524 class FacebookNoticeForm extends NoticeForm
525 {
526
527     var $post_action = null;
528
529     /**
530      * Constructor
531      *
532      * @param HTMLOutputter $out     output channel
533      * @param string        $action  action to return to, if any
534      * @param string        $content content to pre-fill
535      */
536
537     function __construct($out=null, $action=null, $content=null,
538         $post_action=null, $user=null)
539     {
540         parent::__construct($out, $action, $content, $user);
541         $this->post_action = $post_action;
542     }
543
544     /**
545      * Action of the form
546      *
547      * @return string URL of the action
548      */
549
550     function action()
551     {
552         return $this->post_action;
553     }
554
555 }
556
557 class FacebookNoticeList extends NoticeList
558 {
559
560     /**
561      * constructor
562      *
563      * @param Notice $notice stream of notices from DB_DataObject
564      */
565
566     function __construct($notice, $out=null)
567     {
568         parent::__construct($notice, $out);
569     }
570
571     /**
572      * show the list of notices
573      *
574      * "Uses up" the stream by looping through it. So, probably can't
575      * be called twice on the same list.
576      *
577      * @return int count of notices listed.
578      */
579
580     function show()
581     {
582         $this->out->elementStart('div', array('id' =>'notices_primary'));
583         $this->out->element('h2', null, _('Notices'));
584         $this->out->elementStart('ul', array('class' => 'notices'));
585
586         $cnt = 0;
587
588         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
589             $cnt++;
590
591             if ($cnt > NOTICES_PER_PAGE) {
592                 break;
593             }
594
595             $item = $this->newListItem($this->notice);
596             $item->show();
597         }
598
599         $this->out->elementEnd('ul');
600         $this->out->elementEnd('div');
601
602         return $cnt;
603     }
604
605     /**
606      * returns a new list item for the current notice
607      *
608      * Overridden to return a Facebook specific list item.
609      *
610      * @param Notice $notice the current notice
611      *
612      * @return FacebookNoticeListItem a list item for displaying the notice
613      * formatted for display in the Facebook App.
614      */
615
616     function newListItem($notice)
617     {
618         return new FacebookNoticeListItem($notice, $this);
619     }
620
621 }
622
623 class FacebookNoticeListItem extends NoticeListItem
624 {
625
626     /**
627      * constructor
628      *
629      * Also initializes the profile attribute.
630      *
631      * @param Notice $notice The notice we'll display
632      */
633
634     function __construct($notice, $out=null)
635     {
636         parent::__construct($notice, $out);
637     }
638
639     /**
640      * recipe function for displaying a single notice in the Facebook App.
641      *
642      * Overridden to strip out some of the controls that we don't
643      * want to be available.
644      *
645      * @return void
646      */
647
648     function show()
649     {
650         $this->showStart();
651         $this->showNotice();
652         $this->showNoticeInfo();
653
654         // XXX: Need to update to show attachements and controls
655
656         $this->showEnd();
657     }
658
659 }
660
661 class FacebookProfileBoxNotice extends FacebookNoticeListItem
662 {
663
664     /**
665      * constructor
666      *
667      * Also initializes the profile attribute.
668      *
669      * @param Notice $notice The notice we'll display
670      */
671
672     function __construct($notice, $out=null)
673     {
674         parent::__construct($notice, $out);
675     }
676
677     /**
678      * Recipe function for displaying a single notice in the
679      * Facebook App profile notice box
680      *
681      * @return void
682      */
683
684     function show()
685     {
686         $this->showNotice();
687         $this->showNoticeInfo();
688         $this->showAppLink();
689     }
690
691     function showAppLink()
692     {
693
694         $this->facebook = getFacebook();
695
696         $app_props = $this->facebook->api_client->Admin_getAppProperties(
697                 array('canvas_name', 'application_name'));
698
699         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
700         $this->app_name = $app_props['application_name'];
701
702         $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
703                                             'href' => $this->app_uri));
704         $this->out->text($this->app_name);
705         $this->out->elementEnd('a');
706     }
707
708 }