]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Facebook/facebookaction.php
003bf8cd7e8e91a44b608c6800af407826808ddf
[quix0rs-gnu-social.git] / plugins / Facebook / facebookaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base Facebook Action
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-2009 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     exit(1);
32 }
33
34 require_once INSTALLDIR . '/plugins/Facebook/facebookutil.php';
35 require_once INSTALLDIR . '/plugins/Facebook/facebooknoticeform.php';
36
37 class FacebookAction extends Action
38 {
39     var $facebook = null;
40     var $fbuid    = null;
41     var $flink    = null;
42     var $action   = null;
43     var $app_uri  = null;
44     var $app_name = null;
45
46     function __construct($output='php://output', $indent=null, $facebook=null, $flink=null)
47     {
48         parent::__construct($output, $indent);
49
50         $this->facebook = $facebook;
51         $this->flink = $flink;
52
53         if ($this->flink) {
54             $this->fbuid = $flink->foreign_id;
55             $this->user = $flink->getUser();
56         }
57
58         $this->args = array();
59     }
60
61     function prepare($argarray)
62     {
63         parent::prepare($argarray);
64
65         $this->facebook = getFacebook();
66         $this->fbuid = $this->facebook->require_login();
67
68         $this->action = $this->trimmed('action');
69
70         $app_props = $this->facebook->api_client->Admin_getAppProperties(
71                 array('canvas_name', 'application_name'));
72
73         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
74         $this->app_name = $app_props['application_name'];
75
76         $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
77
78         return true;
79
80     }
81
82     function showStylesheets()
83     {
84         // Loading CSS via files in Facebook FBML Canvas apps is still busted as of 1/31/11
85         // See: http://bugs.developers.facebook.net/show_bug.cgi?id=10052
86         $this->cssLink('css/display.css', 'base');
87         $this->cssLink('css/display.css', null, 'screen, projection, tv');
88         $this->cssLink('plugins/Facebook/facebookapp.css');
89
90         // Also, Facebook refuses to let me do this... gar!
91 /*
92         $baseCss = file_get_contents(INSTALLDIR . '/theme/base/css/display.css');
93         $this->style($baseCss);
94
95         $facebookCss = file_get_contents(INSTALLDIR . '/plugins/Facebook/facebookapp.css');
96         $this->style($facebookCss);
97 */
98     }
99
100     function showScripts()
101     {
102         $this->script('plugins/Facebook/facebookapp.js');
103     }
104
105     /**
106      * Start an Facebook ready HTML document
107      *
108      * For Facebook we don't want to actually output any headers,
109      * DTD info, etc.  Just Stylesheet and JavaScript links.
110      *
111      * @param string $type MIME type to use; default is to do negotation.
112      *
113      * @return void
114      */
115     function startHTML($type=null)
116     {
117         $this->showStylesheets();
118         $this->showScripts();
119
120         $this->elementStart('div', array('class' => 'facebook-page'));
121     }
122
123     /**
124     *  Ends a Facebook ready HTML document
125     *
126     *  @return void
127     */
128     function endHTML()
129     {
130         $this->elementEnd('div');
131         $this->endXML();
132     }
133
134     /**
135      * Show notice form.
136      *
137      * @return nothing
138      */
139     function showNoticeForm()
140     {
141         // don't do it for most of the Facebook pages
142     }
143
144     function showBody()
145     {
146         $this->elementStart('div', array('id' => 'wrap'));
147         $this->showHeader();
148         $this->showCore();
149         $this->showFooter();
150         $this->elementEnd('div');
151     }
152
153     function showHead($error, $success)
154     {
155         if ($error) {
156             $this->element("h1", null, $error);
157         }
158
159         if ($success) {
160             $this->element("h1", null, $success);
161         }
162
163         $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
164         $this->elementStart('span', array('id' => 'add_to_profile'));
165         $this->element('fb:add-section-button', array('section' => 'profile'));
166         $this->elementEnd('span');
167         $this->elementEnd('fb:if-section-not-added');
168
169     }
170
171     // Make this into a widget later
172     function showLocalNav()
173     {
174         $this->elementStart('ul', array('class' => 'nav'));
175
176         $this->elementStart('li', array('class' =>
177             ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
178         $this->element('a',
179             // TRANS: Link description for 'Home' link that leads to a start page.
180             array('href' => 'index.php', 'title' => _m('MENU','Home')),
181             // TRANS: Tooltip for 'Home' link that leads to a start page.
182             _m('Home'));
183         $this->elementEnd('li');
184
185         if (common_config('invite', 'enabled')) {
186             $this->elementStart('li',
187                 array('class' =>
188                     ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
189             $this->element('a',
190                 // TRANS: Link description for 'Invite' link that leads to a page where friends can be invited.
191                 array('href' => 'invite.php', 'title' => _m('MENU','Invite')),
192                 // TRANS: Tooltip for 'Invite' link that leads to a page where friends can be invited.
193                 _m('Invite'));
194             $this->elementEnd('li');
195         }
196
197         $this->elementStart('li',
198             array('class' =>
199                 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
200         $this->element('a',
201             array('href' => 'settings.php',
202                 // TRANS: Link description for 'Settings' link that leads to a page user preferences can be set.
203                 'title' => _m('MENU','Settings')),
204                 // TRANS: Tooltip for 'Settings' link that leads to a page user preferences can be set.
205                 _m('Settings'));
206         $this->elementEnd('li');
207
208         $this->elementEnd('ul');
209     }
210
211     /**
212      * Show header of the page.
213      *
214      * @return nothing
215      */
216     function showHeader()
217     {
218         $this->elementStart('div', array('id' => 'header'));
219         $this->showLogo();
220         $this->showNoticeForm();
221         $this->elementEnd('div');
222     }
223
224     /**
225      * Show page, a template method.
226      *
227      * @return nothing
228      */
229     function showPage($error = null, $success = null)
230     {
231         $this->startHTML();
232         $this->showHead($error, $success);
233         $this->showBody();
234         $this->endHTML();
235     }
236
237     function showInstructions()
238     {
239         $this->elementStart('div', array('class' => 'facebook_guide'));
240
241         $this->elementStart('dl', array('class' => 'system_notice'));
242         $this->element('dt', null, 'Page Notice');
243
244         $loginmsg_part1 = _m('To use the %s Facebook Application you need to login ' .
245             'with your username and password. Don\'t have a username yet?');
246         $loginmsg_part2 = _m(' a new account.');
247
248         $this->elementStart('dd');
249         $this->elementStart('p');
250         $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
251         // @todo FIXME: Bad i18n. Patchwork message in two parts.
252         $this->element('a',
253             array('href' => common_local_url('register')), _m('Register'));
254         $this->text( " " . $loginmsg_part2);
255         $this->elementEnd('p');
256         $this->elementEnd('dd');
257
258         $this->elementEnd('dl');
259         $this->elementEnd('div');
260     }
261
262     function showLoginForm($msg = null)
263     {
264
265         $this->elementStart('div', array('id' => 'content'));
266         $this->element('h1', null, _m('Login'));
267
268         if ($msg) {
269              $this->element('fb:error', array('message' => $msg));
270         }
271
272         $this->showInstructions();
273
274         $this->elementStart('div', array('id' => 'content_inner'));
275
276         $this->elementStart('form', array('method' => 'post',
277                                                'class' => 'form_settings',
278                                                'id' => 'login',
279                                                'action' => 'index.php'));
280
281         $this->elementStart('fieldset');
282
283         $this->elementStart('ul', array('class' => 'form_datas'));
284         $this->elementStart('li');
285         $this->input('nickname', _m('Nickname'));
286         $this->elementEnd('li');
287         $this->elementStart('li');
288         $this->password('password', _m('Password'));
289         $this->elementEnd('li');
290         $this->elementEnd('ul');
291
292         // TRANS: Login button.
293         $this->submit('submit', _m('BUTTON','Login'));
294         $this->elementEnd('fieldset');
295         $this->elementEnd('form');
296
297         $this->elementStart('p');
298         $this->element('a', array('href' => common_local_url('recoverpassword')),
299                        _m('Lost or forgotten password?'));
300         $this->elementEnd('p');
301
302         $this->elementEnd('div');
303         $this->elementEnd('div');
304     }
305
306     /**
307      * Generate pagination links
308      *
309      * @param boolean $have_before is there something before?
310      * @param boolean $have_after  is there something after?
311      * @param integer $page        current page
312      * @param string  $action      current action
313      * @param array   $args        rest of query arguments
314      *
315      * @return nothing
316      */
317     function pagination($have_before, $have_after, $page, $action, $args=null)
318     {
319         // Does a little before-after block for next/prev page
320         if ($have_before || $have_after) {
321             $this->elementStart('dl', 'pagination');
322             $this->element('dt', null, _m('Pagination'));
323             $this->elementStart('dd', null);
324             $this->elementStart('ul', array('class' => 'nav'));
325         }
326         if ($have_before) {
327             $pargs   = array('page' => $page-1);
328             $newargs = $args ? array_merge($args, $pargs) : $pargs;
329             $this->elementStart('li', array('class' => 'nav_prev'));
330             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
331                            _m('After'));
332             $this->elementEnd('li');
333         }
334         if ($have_after) {
335             $pargs   = array('page' => $page+1);
336             $newargs = $args ? array_merge($args, $pargs) : $pargs;
337             $this->elementStart('li', array('class' => 'nav_next'));
338             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
339                            _m('Before'));
340             $this->elementEnd('li');
341         }
342         if ($have_before || $have_after) {
343             $this->elementEnd('ul');
344             $this->elementEnd('dd');
345             $this->elementEnd('dl');
346         }
347     }
348
349     function saveNewNotice()
350     {
351         $user = $this->flink->getUser();
352
353         $content = $this->trimmed('status_textarea');
354
355         if (!$content) {
356             $this->showPage(_m('No notice content!'));
357             return;
358         } else {
359             $content_shortened = $user->shortenLinks($content);
360
361             if (Notice::contentTooLong($content_shortened)) {
362                 // @todo FIXME: i18n: Needs plural.
363                 $this->showPage(sprintf(_m('That\'s too long. Max notice size is %d chars.'),
364                                         Notice::maxContent()));
365                 return;
366             }
367         }
368
369         $inter = new CommandInterpreter();
370
371         $cmd = $inter->handle_command($user, $content_shortened);
372
373         if ($cmd) {
374             // XXX fix this
375             $cmd->execute(new WebChannel());
376             return;
377         }
378
379         $replyto = $this->trimmed('inreplyto');
380
381         try {
382             $notice = Notice::saveNew($user->id, $content, 'web',
383                                       array('reply_to' => ($replyto == 'false') ? null : $replyto));
384
385         } catch (Exception $e) {
386             $this->showPage($e->getMessage());
387             return;
388         }
389
390     }
391 }
392
393 class FacebookNoticeList extends NoticeList
394 {
395     /**
396      * constructor
397      *
398      * @param Notice $notice stream of notices from DB_DataObject
399      */
400
401     function __construct($notice, $out=null)
402     {
403         parent::__construct($notice, $out);
404     }
405
406     /**
407      * show the list of notices
408      *
409      * "Uses up" the stream by looping through it. So, probably can't
410      * be called twice on the same list.
411      *
412      * @return int count of notices listed.
413      */
414     function show()
415     {
416         $this->out->elementStart('div', array('id' =>'notices_primary'));
417         $this->out->element('h2', null, _m('Notices'));
418         $this->out->elementStart('ul', array('class' => 'notices'));
419
420         $cnt = 0;
421
422         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
423             $cnt++;
424
425             if ($cnt > NOTICES_PER_PAGE) {
426                 break;
427             }
428
429             $item = $this->newListItem($this->notice);
430             $item->show();
431         }
432
433         $this->out->elementEnd('ul');
434         $this->out->elementEnd('div');
435
436         return $cnt;
437     }
438
439     /**
440      * returns a new list item for the current notice
441      *
442      * Overridden to return a Facebook specific list item.
443      *
444      * @param Notice $notice the current notice
445      *
446      * @return FacebookNoticeListItem a list item for displaying the notice
447      * formatted for display in the Facebook App.
448      */
449     function newListItem($notice)
450     {
451         return new FacebookNoticeListItem($notice, $this);
452     }
453 }
454
455 class FacebookNoticeListItem extends NoticeListItem
456 {
457     /**
458      * constructor
459      *
460      * Also initializes the profile attribute.
461      *
462      * @param Notice $notice The notice we'll display
463      */
464     function __construct($notice, $out=null)
465     {
466         parent::__construct($notice, $out);
467     }
468
469     /**
470      * recipe function for displaying a single notice in the Facebook App.
471      *
472      * Overridden to strip out some of the controls that we don't
473      * want to be available.
474      *
475      * @return void
476      */
477     function show()
478     {
479         $this->showStart();
480         $this->showNotice();
481         $this->showNoticeInfo();
482
483         // XXX: Need to update to show attachements and controls
484
485         $this->showEnd();
486     }
487 }
488
489 class FacebookProfileBoxNotice extends FacebookNoticeListItem
490 {
491     /**
492      * constructor
493      *
494      * Also initializes the profile attribute.
495      *
496      * @param Notice $notice The notice we'll display
497      */
498     function __construct($notice, $out=null)
499     {
500         parent::__construct($notice, $out);
501     }
502
503     /**
504      * Recipe function for displaying a single notice in the
505      * Facebook App profile notice box
506      *
507      * @return void
508      */
509     function show()
510     {
511         $this->showNotice();
512         $this->showNoticeInfo();
513         $this->showAppLink();
514     }
515
516     function showAppLink()
517     {
518         $this->facebook = getFacebook();
519
520         $app_props = $this->facebook->api_client->Admin_getAppProperties(
521                 array('canvas_name', 'application_name'));
522
523         $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
524         $this->app_name = $app_props['application_name'];
525
526         $this->out->elementStart('a', array('id' => 'facebook_statusnet_app',
527                                             'href' => $this->app_uri));
528         $this->out->text($this->app_name);
529         $this->out->elementEnd('a');
530     }
531 }