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