]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookaction.php
Merge branch 'testing'
[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 saveNewNotice()
464     {
465
466         $user = $this->flink->getUser();
467
468         $content = $this->trimmed('status_textarea');
469
470         if (!$content) {
471             $this->showPage(_('No notice content!'));
472             return;
473         } else {
474             $content_shortened = common_shorten_links($content);
475
476             if (mb_strlen($content_shortened) > 140) {
477                 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
478                 return;
479             }
480         }
481
482         $inter = new CommandInterpreter();
483
484         $cmd = $inter->handle_command($user, $content_shortened);
485
486         if ($cmd) {
487
488             // XXX fix this
489
490             $cmd->execute(new WebChannel());
491             return;
492         }
493
494         $replyto = $this->trimmed('inreplyto');
495
496         $notice = Notice::saveNew($user->id, $content,
497             'web', 1, ($replyto == 'false') ? null : $replyto);
498
499         if (is_string($notice)) {
500             $this->showPage($notice);
501             return;
502         }
503
504         common_broadcast_notice($notice);
505
506         // Also update the user's Facebook status
507         facebookBroadcastNotice($notice);
508
509     }
510
511 }
512
513 class FacebookNoticeForm extends NoticeForm
514 {
515
516     var $post_action = null;
517
518     /**
519      * Constructor
520      *
521      * @param HTMLOutputter $out     output channel
522      * @param string        $action  action to return to, if any
523      * @param string        $content content to pre-fill
524      */
525
526     function __construct($out=null, $action=null, $content=null,
527         $post_action=null, $user=null)
528     {
529         parent::__construct($out, $action, $content, $user);
530         $this->post_action = $post_action;
531     }
532
533     /**
534      * Action of the form
535      *
536      * @return string URL of the action
537      */
538
539     function action()
540     {
541         return $this->post_action;
542     }
543
544 }
545
546 class FacebookNoticeList extends NoticeList
547 {
548
549     /**
550      * constructor
551      *
552      * @param Notice $notice stream of notices from DB_DataObject
553      */
554
555     function __construct($notice, $out=null)
556     {
557         parent::__construct($notice, $out);
558     }
559
560     /**
561      * show the list of notices
562      *
563      * "Uses up" the stream by looping through it. So, probably can't
564      * be called twice on the same list.
565      *
566      * @return int count of notices listed.
567      */
568
569     function show()
570     {
571         $this->out->elementStart('div', array('id' =>'notices_primary'));
572         $this->out->element('h2', null, _('Notices'));
573         $this->out->elementStart('ul', array('class' => 'notices'));
574
575         $cnt = 0;
576
577         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
578             $cnt++;
579
580             if ($cnt > NOTICES_PER_PAGE) {
581                 break;
582             }
583
584             $item = $this->newListItem($this->notice);
585             $item->show();
586         }
587
588         $this->out->elementEnd('ul');
589         $this->out->elementEnd('div');
590
591         return $cnt;
592     }
593
594     /**
595      * returns a new list item for the current notice
596      *
597      * Overridden to return a Facebook specific list item.
598      *
599      * @param Notice $notice the current notice
600      *
601      * @return FacebookNoticeListItem a list item for displaying the notice
602      * formatted for display in the Facebook App.
603      */
604
605     function newListItem($notice)
606     {
607         return new FacebookNoticeListItem($notice, $this);
608     }
609
610 }
611
612 class FacebookNoticeListItem extends NoticeListItem
613 {
614
615     /**
616      * constructor
617      *
618      * Also initializes the profile attribute.
619      *
620      * @param Notice $notice The notice we'll display
621      */
622
623     function __construct($notice, $out=null)
624     {
625         parent::__construct($notice, $out);
626     }
627
628     /**
629      * recipe function for displaying a single notice in the Facebook App.
630      *
631      * Overridden to strip out some of the controls that we don't
632      * want to be available.
633      *
634      * @return void
635      */
636
637     function show()
638     {
639         $this->showStart();
640         $this->showNotice();
641         $this->showNoticeInfo();
642
643         // XXX: Need to update to show attachements and controls
644
645         $this->showEnd();
646     }
647
648 }
649
650 class FacebookProfileBoxNotice extends FacebookNoticeListItem
651 {
652
653     /**
654      * constructor
655      *
656      * Also initializes the profile attribute.
657      *
658      * @param Notice $notice The notice we'll display
659      */
660
661     function __construct($notice, $out=null)
662     {
663         parent::__construct($notice, $out);
664     }
665
666     /**
667      * Recipe function for displaying a single notice in the
668      * Facebook App profile notice box
669      *
670      * @return void
671      */
672
673     function show()
674     {
675         $this->showNotice();
676         $this->showNoticeInfo();
677         $this->showAppLink();
678     }
679
680     function showAppLink()
681     {
682
683         $this->facebook = getFacebook();
684
685         $app_props = $this->facebook->api_client->Admin_getAppProperties(
686                 array('canvas_name', 'application_name'));
687
688         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
689         $this->app_name = $app_props['application_name'];
690
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');
695     }
696
697 }