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