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