]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookaction.php
change LACONICA to STATUSNET
[quix0rs-gnu-social.git] / lib / facebookaction.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2008 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET'))
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         $this->cssLink('css/display.css', 'base');
99         $this->cssLink('css/facebookapp.css', 'base');
100     }
101
102     function showScripts()
103     {
104         $this->script('js/facebookapp.js');
105     }
106
107     /**
108      * Start an Facebook ready HTML document
109      *
110      *  For Facebook we don't want to actually output any headers,
111      *  DTD info, etc.  Just Stylesheet and JavaScript links.
112      *
113      * If $type isn't specified, will attempt to do content negotiation.
114      *
115      * @param string $type MIME type to use; default is to do negotation.
116      *
117      * @return void
118      */
119
120     function startHTML($type=null)
121     {
122         $this->showStylesheets();
123         $this->showScripts();
124
125         $this->elementStart('div', array('class' => 'facebook-page'));
126     }
127
128     /**
129     *  Ends a Facebook ready HTML document
130     *
131     *  @return void
132     */
133     function endHTML()
134     {
135         $this->elementEnd('div');
136         $this->endXML();
137     }
138
139     /**
140      * Show notice form.
141      *
142      * MAY overload if no notice form needed... or direct message box????
143      *
144      * @return nothing
145      */
146     function showNoticeForm()
147     {
148         // don't do it for most of the Facebook pages
149     }
150
151     function showBody()
152     {
153         $this->elementStart('div', array('id' => 'wrap'));
154         $this->showHeader();
155         $this->showCore();
156         $this->showFooter();
157         $this->elementEnd('div');
158     }
159
160     function showAside()
161     {
162     }
163
164     function showHead($error, $success)
165     {
166
167         if ($error) {
168             $this->element("h1", null, $error);
169         }
170
171         if ($success) {
172             $this->element("h1", null, $success);
173         }
174
175         $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
176         $this->elementStart('span', array('id' => 'add_to_profile'));
177         $this->element('fb:add-section-button', array('section' => 'profile'));
178         $this->elementEnd('span');
179         $this->elementEnd('fb:if-section-not-added');
180
181     }
182
183
184     // Make this into a widget later
185     function showLocalNav()
186     {
187         $this->elementStart('ul', array('class' => 'nav'));
188
189         $this->elementStart('li', array('class' =>
190             ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
191         $this->element('a',
192             array('href' => 'index.php', 'title' => _('Home')), _('Home'));
193         $this->elementEnd('li');
194
195         if (common_config('invite', 'enabled')) {
196             $this->elementStart('li',
197                 array('class' =>
198                     ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
199             $this->element('a',
200                 array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
201             $this->elementEnd('li');
202         }
203
204         $this->elementStart('li',
205             array('class' =>
206                 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
207         $this->element('a',
208             array('href' => 'settings.php',
209                 'title' => _('Settings')), _('Settings'));
210         $this->elementEnd('li');
211
212         $this->elementEnd('ul');
213     }
214
215     /**
216      * Show header of the page.
217      *
218      * Calls template methods
219      *
220      * @return nothing
221      */
222     function showHeader()
223     {
224         $this->elementStart('div', array('id' => 'header'));
225         $this->showLogo();
226         $this->showNoticeForm();
227         $this->elementEnd('div');
228     }
229
230     /**
231      * Show page, a template method.
232      *
233      * @return nothing
234      */
235     function showPage($error = null, $success = null)
236     {
237         $this->startHTML();
238         $this->showHead($error, $success);
239         $this->showBody();
240         $this->endHTML();
241     }
242
243
244     function showInstructions()
245     {
246
247         $this->elementStart('div', array('class' => 'facebook_guide'));
248
249         $this->elementStart('dl', array('class' => 'system_notice'));
250         $this->element('dt', null, 'Page Notice');
251
252         $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
253             'with your username and password. Don\'t have a username yet? ');
254         $loginmsg_part2 = _(' a new account.');
255
256         $this->elementStart('dd');
257         $this->elementStart('p');
258         $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
259         if (!common_config('site', 'openidonly')) {
260             $this->element('a',
261                 array('href' => common_local_url('register')), _('Register'));
262         } else {
263             $this->element('a',
264                 array('href' => common_local_url('openidlogin')), _('Register'));
265         }
266         $this->text($loginmsg_part2);
267     $this->elementEnd('p');
268         $this->elementEnd('dd');
269
270         $this->elementEnd('dl');
271         $this->elementEnd('div');
272     }
273
274
275     function showLoginForm($msg = null)
276     {
277
278         $this->elementStart('div', array('id' => 'content'));
279         $this->element('h1', null, _('Login'));
280
281         if ($msg) {
282              $this->element('fb:error', array('message' => $msg));
283         }
284
285         $this->showInstructions();
286
287         $this->elementStart('div', array('id' => 'content_inner'));
288
289         $this->elementStart('form', array('method' => 'post',
290                                                'class' => 'form_settings',
291                                                'id' => 'login',
292                                                'action' => 'index.php'));
293
294         $this->elementStart('fieldset');
295
296         $this->elementStart('ul', array('class' => 'form_datas'));
297         $this->elementStart('li');
298         $this->input('nickname', _('Nickname'));
299         $this->elementEnd('li');
300         $this->elementStart('li');
301         $this->password('password', _('Password'));
302         $this->elementEnd('li');
303         $this->elementEnd('ul');
304
305         $this->submit('submit', _('Login'));
306     $this->elementEnd('fieldset');
307         $this->elementEnd('form');
308
309         $this->elementStart('p');
310         $this->element('a', array('href' => common_local_url('recoverpassword')),
311                        _('Lost or forgotten password?'));
312         $this->elementEnd('p');
313
314         $this->elementEnd('div');
315         $this->elementEnd('div');
316
317     }
318
319
320     function updateProfileBox($notice)
321     {
322
323         // Need to include inline CSS for styling the Profile box
324
325     $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
326     $icon_url = $app_props['icon_url'];
327
328         $style = '<style>
329      .entry-title *,
330      .entry-content * {
331      font-size:14px;
332      font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
333      }
334      .entry-title a,
335      .entry-content a {
336      color:#002E6E;
337      }
338
339          .entry-title .vcard .photo {
340          float:left;
341          display:inline;
342      margin-right:11px;
343      margin-bottom:11px
344          }
345      .entry-title {
346      margin-bottom:11px;
347      }
348          .entry-title p.entry-content {
349          display:inline;
350      margin-left:5px;
351          }
352
353      div.entry-content {
354      clear:both;
355      }
356          div.entry-content dl,
357          div.entry-content dt,
358          div.entry-content dd {
359          display:inline;
360      text-transform:lowercase;
361          }
362
363          div.entry-content dd,
364      div.entry-content .device dt {
365      margin-left:0;
366      margin-right:5px;
367          }
368          div.entry-content dl.timestamp dt,
369      div.entry-content dl.response dt {
370          display:none;
371          }
372          div.entry-content dd a {
373          display:inline-block;
374          }
375
376      #facebook_laconica_app {
377      text-indent:-9999px;
378      height:16px;
379      width:16px;
380      display:block;
381      background:url('.$icon_url.') no-repeat 0 0;
382      float:right;
383      }
384          </style>';
385
386         $this->xw->openMemory();
387
388         $item = new FacebookProfileBoxNotice($notice, $this);
389         $item->show();
390
391         $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
392         $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
393
394         $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
395
396         $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
397
398         $this->xw->openURI('php://output');
399     }
400
401
402     /**
403      * Generate pagination links
404      *
405      * @param boolean $have_before is there something before?
406      * @param boolean $have_after  is there something after?
407      * @param integer $page        current page
408      * @param string  $action      current action
409      * @param array   $args        rest of query arguments
410      *
411      * @return nothing
412      */
413     function pagination($have_before, $have_after, $page, $action, $args=null)
414     {
415         // Does a little before-after block for next/prev page
416         if ($have_before || $have_after) {
417             $this->elementStart('div', array('class' => 'pagination'));
418             $this->elementStart('dl', null);
419             $this->element('dt', null, _('Pagination'));
420             $this->elementStart('dd', null);
421             $this->elementStart('ul', array('class' => 'nav'));
422         }
423         if ($have_before) {
424             $pargs   = array('page' => $page-1);
425             $newargs = $args ? array_merge($args, $pargs) : $pargs;
426             $this->elementStart('li', array('class' => 'nav_prev'));
427             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
428                            _('After'));
429             $this->elementEnd('li');
430         }
431         if ($have_after) {
432             $pargs   = array('page' => $page+1);
433             $newargs = $args ? array_merge($args, $pargs) : $pargs;
434             $this->elementStart('li', array('class' => 'nav_next'));
435             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
436                            _('Before'));
437             $this->elementEnd('li');
438         }
439         if ($have_before || $have_after) {
440             $this->elementEnd('ul');
441             $this->elementEnd('dd');
442             $this->elementEnd('dl');
443             $this->elementEnd('div');
444         }
445     }
446
447     function saveNewNotice()
448     {
449
450         $user = $this->flink->getUser();
451
452         $content = $this->trimmed('status_textarea');
453
454         if (!$content) {
455             $this->showPage(_('No notice content!'));
456             return;
457         } else {
458             $content_shortened = common_shorten_links($content);
459
460             if (mb_strlen($content_shortened) > 140) {
461                 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
462                 return;
463             }
464         }
465
466         $inter = new CommandInterpreter();
467
468         $cmd = $inter->handle_command($user, $content_shortened);
469
470         if ($cmd) {
471
472             // XXX fix this
473
474             $cmd->execute(new WebChannel());
475             return;
476         }
477
478         $replyto = $this->trimmed('inreplyto');
479
480         $notice = Notice::saveNew($user->id, $content,
481             'web', 1, ($replyto == 'false') ? null : $replyto);
482
483         if (is_string($notice)) {
484             $this->showPage($notice);
485             return;
486         }
487
488         common_broadcast_notice($notice);
489
490         // Also update the user's Facebook status
491         facebookBroadcastNotice($notice);
492
493     }
494
495 }
496
497 class FacebookNoticeForm extends NoticeForm
498 {
499
500     var $post_action = null;
501
502     /**
503      * Constructor
504      *
505      * @param HTMLOutputter $out     output channel
506      * @param string        $action  action to return to, if any
507      * @param string        $content content to pre-fill
508      */
509
510     function __construct($out=null, $action=null, $content=null,
511         $post_action=null, $user=null)
512     {
513         parent::__construct($out, $action, $content, $user);
514         $this->post_action = $post_action;
515     }
516
517     /**
518      * Action of the form
519      *
520      * @return string URL of the action
521      */
522
523     function action()
524     {
525         return $this->post_action;
526     }
527
528 }
529
530 class FacebookNoticeList extends NoticeList
531 {
532
533     /**
534      * constructor
535      *
536      * @param Notice $notice stream of notices from DB_DataObject
537      */
538
539     function __construct($notice, $out=null)
540     {
541         parent::__construct($notice, $out);
542     }
543
544     /**
545      * show the list of notices
546      *
547      * "Uses up" the stream by looping through it. So, probably can't
548      * be called twice on the same list.
549      *
550      * @return int count of notices listed.
551      */
552
553     function show()
554     {
555         $this->out->elementStart('div', array('id' =>'notices_primary'));
556         $this->out->element('h2', null, _('Notices'));
557         $this->out->elementStart('ul', array('class' => 'notices'));
558
559         $cnt = 0;
560
561         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
562             $cnt++;
563
564             if ($cnt > NOTICES_PER_PAGE) {
565                 break;
566             }
567
568             $item = $this->newListItem($this->notice);
569             $item->show();
570         }
571
572         $this->out->elementEnd('ul');
573         $this->out->elementEnd('div');
574
575         return $cnt;
576     }
577
578     /**
579      * returns a new list item for the current notice
580      *
581      * Overridden to return a Facebook specific list item.
582      *
583      * @param Notice $notice the current notice
584      *
585      * @return FacebookNoticeListItem a list item for displaying the notice
586      * formatted for display in the Facebook App.
587      */
588
589     function newListItem($notice)
590     {
591         return new FacebookNoticeListItem($notice, $this);
592     }
593
594 }
595
596 class FacebookNoticeListItem extends NoticeListItem
597 {
598
599     /**
600      * constructor
601      *
602      * Also initializes the profile attribute.
603      *
604      * @param Notice $notice The notice we'll display
605      */
606
607     function __construct($notice, $out=null)
608     {
609         parent::__construct($notice, $out);
610     }
611
612     /**
613      * recipe function for displaying a single notice in the Facebook App.
614      *
615      * Overridden to strip out some of the controls that we don't
616      * want to be available.
617      *
618      * @return void
619      */
620
621     function show()
622     {
623         $this->showStart();
624         $this->showNotice();
625         $this->showNoticeInfo();
626
627         // XXX: Need to update to show attachements and controls
628
629         $this->showEnd();
630     }
631
632 }
633
634 class FacebookProfileBoxNotice extends FacebookNoticeListItem
635 {
636
637     /**
638      * constructor
639      *
640      * Also initializes the profile attribute.
641      *
642      * @param Notice $notice The notice we'll display
643      */
644
645     function __construct($notice, $out=null)
646     {
647         parent::__construct($notice, $out);
648     }
649
650     /**
651      * Recipe function for displaying a single notice in the
652      * Facebook App profile notice box
653      *
654      * @return void
655      */
656
657     function show()
658     {
659         $this->showNotice();
660         $this->showNoticeInfo();
661         $this->showAppLink();
662     }
663
664     function showAppLink()
665     {
666
667         $this->facebook = getFacebook();
668
669         $app_props = $this->facebook->api_client->Admin_getAppProperties(
670                 array('canvas_name', 'application_name'));
671
672         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
673         $this->app_name = $app_props['application_name'];
674
675         $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
676                                             'href' => $this->app_uri));
677         $this->out->text($this->app_name);
678         $this->out->elementEnd('a');
679     }
680
681 }