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