]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookaction.php
Merge branch '0.8.x' into 0.9.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 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         if (!common_config('site', 'openidonly')) {
257             $this->element('a',
258                 array('href' => common_local_url('register')), _('Register'));
259         } else {
260             $this->element('a',
261                 array('href' => common_local_url('openidlogin')), _('Register'));
262         }
263         $this->text($loginmsg_part2);
264     $this->elementEnd('p');
265         $this->elementEnd('dd');
266
267         $this->elementEnd('dl');
268         $this->elementEnd('div');
269     }
270
271     function showLoginForm($msg = null)
272     {
273
274         $this->elementStart('div', array('id' => 'content'));
275         $this->element('h1', null, _('Login'));
276
277         if ($msg) {
278              $this->element('fb:error', array('message' => $msg));
279         }
280
281         $this->showInstructions();
282
283         $this->elementStart('div', array('id' => 'content_inner'));
284
285         $this->elementStart('form', array('method' => 'post',
286                                                'class' => 'form_settings',
287                                                'id' => 'login',
288                                                'action' => 'index.php'));
289
290         $this->elementStart('fieldset');
291
292         $this->elementStart('ul', array('class' => 'form_datas'));
293         $this->elementStart('li');
294         $this->input('nickname', _('Nickname'));
295         $this->elementEnd('li');
296         $this->elementStart('li');
297         $this->password('password', _('Password'));
298         $this->elementEnd('li');
299         $this->elementEnd('ul');
300
301         $this->submit('submit', _('Login'));
302     $this->elementEnd('fieldset');
303         $this->elementEnd('form');
304
305         $this->elementStart('p');
306         $this->element('a', array('href' => common_local_url('recoverpassword')),
307                        _('Lost or forgotten password?'));
308         $this->elementEnd('p');
309
310         $this->elementEnd('div');
311         $this->elementEnd('div');
312
313     }
314
315     function updateProfileBox($notice)
316     {
317
318         // Need to include inline CSS for styling the Profile box
319
320     $app_props = $this->facebook->api_client->Admin_getAppProperties(array('icon_url'));
321     $icon_url = $app_props['icon_url'];
322
323         $style = '<style>
324      .entry-title *,
325      .entry-content * {
326      font-size:14px;
327      font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
328      }
329      .entry-title a,
330      .entry-content a {
331      color:#002E6E;
332      }
333
334          .entry-title .vcard .photo {
335          float:left;
336          display:inline;
337      margin-right:11px;
338      margin-bottom:11px
339          }
340      .entry-title {
341      margin-bottom:11px;
342      }
343          .entry-title p.entry-content {
344          display:inline;
345      margin-left:5px;
346          }
347
348      div.entry-content {
349      clear:both;
350      }
351          div.entry-content dl,
352          div.entry-content dt,
353          div.entry-content dd {
354          display:inline;
355      text-transform:lowercase;
356          }
357
358          div.entry-content dd,
359      div.entry-content .device dt {
360      margin-left:0;
361      margin-right:5px;
362          }
363          div.entry-content dl.timestamp dt,
364      div.entry-content dl.response dt {
365          display:none;
366          }
367          div.entry-content dd a {
368          display:inline-block;
369          }
370
371      #facebook_laconica_app {
372      text-indent:-9999px;
373      height:16px;
374      width:16px;
375      display:block;
376      background:url('.$icon_url.') no-repeat 0 0;
377      float:right;
378      }
379          </style>';
380
381         $this->xw->openMemory();
382
383         $item = new FacebookProfileBoxNotice($notice, $this);
384         $item->show();
385
386         $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
387         $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
388
389         $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
390
391         $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);
392
393         $this->xw->openURI('php://output');
394     }
395
396     /**
397      * Generate pagination links
398      *
399      * @param boolean $have_before is there something before?
400      * @param boolean $have_after  is there something after?
401      * @param integer $page        current page
402      * @param string  $action      current action
403      * @param array   $args        rest of query arguments
404      *
405      * @return nothing
406      */
407     function pagination($have_before, $have_after, $page, $action, $args=null)
408     {
409         // Does a little before-after block for next/prev page
410         if ($have_before || $have_after) {
411             $this->elementStart('div', array('class' => 'pagination'));
412             $this->elementStart('dl', null);
413             $this->element('dt', null, _('Pagination'));
414             $this->elementStart('dd', null);
415             $this->elementStart('ul', array('class' => 'nav'));
416         }
417         if ($have_before) {
418             $pargs   = array('page' => $page-1);
419             $newargs = $args ? array_merge($args, $pargs) : $pargs;
420             $this->elementStart('li', array('class' => 'nav_prev'));
421             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
422                            _('After'));
423             $this->elementEnd('li');
424         }
425         if ($have_after) {
426             $pargs   = array('page' => $page+1);
427             $newargs = $args ? array_merge($args, $pargs) : $pargs;
428             $this->elementStart('li', array('class' => 'nav_next'));
429             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
430                            _('Before'));
431             $this->elementEnd('li');
432         }
433         if ($have_before || $have_after) {
434             $this->elementEnd('ul');
435             $this->elementEnd('dd');
436             $this->elementEnd('dl');
437             $this->elementEnd('div');
438         }
439     }
440
441     function saveNewNotice()
442     {
443
444         $user = $this->flink->getUser();
445
446         $content = $this->trimmed('status_textarea');
447
448         if (!$content) {
449             $this->showPage(_('No notice content!'));
450             return;
451         } else {
452             $content_shortened = common_shorten_links($content);
453
454             if (Notice::contentTooLong($content_shortened)) {
455                 $this->showPage(sprintf(_('That\'s too long. Max notice size is %d chars.'),
456                                         Notice::maxContent()));
457                 return;
458             }
459         }
460
461         $inter = new CommandInterpreter();
462
463         $cmd = $inter->handle_command($user, $content_shortened);
464
465         if ($cmd) {
466
467             // XXX fix this
468
469             $cmd->execute(new WebChannel());
470             return;
471         }
472
473         $replyto = $this->trimmed('inreplyto');
474
475         $notice = Notice::saveNew($user->id, $content,
476             'web', 1, ($replyto == 'false') ? null : $replyto);
477
478         if (is_string($notice)) {
479             $this->showPage($notice);
480             return;
481         }
482
483         common_broadcast_notice($notice);
484
485         // Also update the user's Facebook status
486         facebookBroadcastNotice($notice);
487
488     }
489
490 }
491
492 class FacebookNoticeForm extends NoticeForm
493 {
494
495     var $post_action = null;
496
497     /**
498      * Constructor
499      *
500      * @param HTMLOutputter $out     output channel
501      * @param string        $action  action to return to, if any
502      * @param string        $content content to pre-fill
503      */
504
505     function __construct($out=null, $action=null, $content=null,
506         $post_action=null, $user=null)
507     {
508         parent::__construct($out, $action, $content, $user);
509         $this->post_action = $post_action;
510     }
511
512     /**
513      * Action of the form
514      *
515      * @return string URL of the action
516      */
517
518     function action()
519     {
520         return $this->post_action;
521     }
522
523 }
524
525 class FacebookNoticeList extends NoticeList
526 {
527
528     /**
529      * constructor
530      *
531      * @param Notice $notice stream of notices from DB_DataObject
532      */
533
534     function __construct($notice, $out=null)
535     {
536         parent::__construct($notice, $out);
537     }
538
539     /**
540      * show the list of notices
541      *
542      * "Uses up" the stream by looping through it. So, probably can't
543      * be called twice on the same list.
544      *
545      * @return int count of notices listed.
546      */
547
548     function show()
549     {
550         $this->out->elementStart('div', array('id' =>'notices_primary'));
551         $this->out->element('h2', null, _('Notices'));
552         $this->out->elementStart('ul', array('class' => 'notices'));
553
554         $cnt = 0;
555
556         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
557             $cnt++;
558
559             if ($cnt > NOTICES_PER_PAGE) {
560                 break;
561             }
562
563             $item = $this->newListItem($this->notice);
564             $item->show();
565         }
566
567         $this->out->elementEnd('ul');
568         $this->out->elementEnd('div');
569
570         return $cnt;
571     }
572
573     /**
574      * returns a new list item for the current notice
575      *
576      * Overridden to return a Facebook specific list item.
577      *
578      * @param Notice $notice the current notice
579      *
580      * @return FacebookNoticeListItem a list item for displaying the notice
581      * formatted for display in the Facebook App.
582      */
583
584     function newListItem($notice)
585     {
586         return new FacebookNoticeListItem($notice, $this);
587     }
588
589 }
590
591 class FacebookNoticeListItem extends NoticeListItem
592 {
593
594     /**
595      * constructor
596      *
597      * Also initializes the profile attribute.
598      *
599      * @param Notice $notice The notice we'll display
600      */
601
602     function __construct($notice, $out=null)
603     {
604         parent::__construct($notice, $out);
605     }
606
607     /**
608      * recipe function for displaying a single notice in the Facebook App.
609      *
610      * Overridden to strip out some of the controls that we don't
611      * want to be available.
612      *
613      * @return void
614      */
615
616     function show()
617     {
618         $this->showStart();
619         $this->showNotice();
620         $this->showNoticeInfo();
621
622         // XXX: Need to update to show attachements and controls
623
624         $this->showEnd();
625     }
626
627 }
628
629 class FacebookProfileBoxNotice extends FacebookNoticeListItem
630 {
631
632     /**
633      * constructor
634      *
635      * Also initializes the profile attribute.
636      *
637      * @param Notice $notice The notice we'll display
638      */
639
640     function __construct($notice, $out=null)
641     {
642         parent::__construct($notice, $out);
643     }
644
645     /**
646      * Recipe function for displaying a single notice in the
647      * Facebook App profile notice box
648      *
649      * @return void
650      */
651
652     function show()
653     {
654         $this->showNotice();
655         $this->showNoticeInfo();
656         $this->showAppLink();
657     }
658
659     function showAppLink()
660     {
661
662         $this->facebook = getFacebook();
663
664         $app_props = $this->facebook->api_client->Admin_getAppProperties(
665                 array('canvas_name', 'application_name'));
666
667         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
668         $this->app_name = $app_props['application_name'];
669
670         $this->out->elementStart('a', array('id' => 'facebook_laconica_app',
671                                             'href' => $this->app_uri));
672         $this->out->text($this->app_name);
673         $this->out->elementEnd('a');
674     }
675
676 }