]> 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  * 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         $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         $this->element('a',
260             array('href' => common_local_url('register')), _('Register'));
261         $this->text($loginmsg_part2);
262     $this->elementEnd('p');
263         $this->elementEnd('dd');
264
265         $this->elementEnd('dl');
266         $this->elementEnd('div');
267     }
268
269
270     function showLoginForm($msg = null)
271     {
272
273         $this->elementStart('div', array('id' => 'content'));
274         $this->element('h1', null, _('Login'));
275
276         if ($msg) {
277              $this->element('fb:error', array('message' => $msg));
278         }
279
280         $this->showInstructions();
281
282         $this->elementStart('div', array('id' => 'content_inner'));
283
284         $this->elementStart('form', array('method' => 'post',
285                                                'class' => 'form_settings',
286                                                'id' => 'login',
287                                                'action' => 'index.php'));
288
289         $this->elementStart('fieldset');
290
291         $this->elementStart('ul', array('class' => 'form_datas'));
292         $this->elementStart('li');
293         $this->input('nickname', _('Nickname'));
294         $this->elementEnd('li');
295         $this->elementStart('li');
296         $this->password('password', _('Password'));
297         $this->elementEnd('li');
298         $this->elementEnd('ul');
299
300         $this->submit('submit', _('Login'));
301     $this->elementEnd('fieldset');
302         $this->elementEnd('form');
303
304         $this->elementStart('p');
305         $this->element('a', array('href' => common_local_url('recoverpassword')),
306                        _('Lost or forgotten password?'));
307         $this->elementEnd('p');
308
309         $this->elementEnd('div');
310         $this->elementEnd('div');
311
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     /**
398      * Generate pagination links
399      *
400      * @param boolean $have_before is there something before?
401      * @param boolean $have_after  is there something after?
402      * @param integer $page        current page
403      * @param string  $action      current action
404      * @param array   $args        rest of query arguments
405      *
406      * @return nothing
407      */
408     function pagination($have_before, $have_after, $page, $action, $args=null)
409     {
410         // Does a little before-after block for next/prev page
411         if ($have_before || $have_after) {
412             $this->elementStart('div', array('class' => 'pagination'));
413             $this->elementStart('dl', null);
414             $this->element('dt', null, _('Pagination'));
415             $this->elementStart('dd', null);
416             $this->elementStart('ul', array('class' => 'nav'));
417         }
418         if ($have_before) {
419             $pargs   = array('page' => $page-1);
420             $newargs = $args ? array_merge($args, $pargs) : $pargs;
421             $this->elementStart('li', array('class' => 'nav_prev'));
422             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
423                            _('After'));
424             $this->elementEnd('li');
425         }
426         if ($have_after) {
427             $pargs   = array('page' => $page+1);
428             $newargs = $args ? array_merge($args, $pargs) : $pargs;
429             $this->elementStart('li', array('class' => 'nav_next'));
430             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
431                            _('Before'));
432             $this->elementEnd('li');
433         }
434         if ($have_before || $have_after) {
435             $this->elementEnd('ul');
436             $this->elementEnd('dd');
437             $this->elementEnd('dl');
438             $this->elementEnd('div');
439         }
440     }
441
442     function saveNewNotice()
443     {
444
445         $user = $this->flink->getUser();
446
447         $content = $this->trimmed('status_textarea');
448
449         if (!$content) {
450             $this->showPage(_('No notice content!'));
451             return;
452         } else {
453             $content_shortened = common_shorten_links($content);
454
455             if (mb_strlen($content_shortened) > 140) {
456                 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
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 }