summaryrefslogtreecommitdiff
path: root/lib/facebookaction.php
blob: bfba702e6a09b6bb5d09c95a6b2b280ad0f38ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<?php
/**
 * Laconica, the distributed open-source microblogging tool
 *
 * Low-level generator for HTML
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Faceboook
 * @package   Laconica
 * @author    Zach Copley <zach@controlyourself.ca>
 * @copyright 2008 Control Yourself, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://laconi.ca/
 */

if (!defined('LACONICA'))
{
    exit(1);
}

require_once INSTALLDIR.'/lib/facebookutil.php';
require_once INSTALLDIR.'/lib/noticeform.php';


class FacebookAction extends Action
{
    
    var $facebook = null;
    var $fbuid    = null;
    var $flink    = null;
    var $action   = null;
    var $app_uri  = null;
    var $app_name = null;
  
    /**
     * Constructor
     *
     * Just wraps the HTMLOutputter constructor.
     *
     * @param string  $output URI to output to, default = stdout
     * @param boolean $indent Whether to indent output, default true
     *
     * @see XMLOutputter::__construct
     * @see HTMLOutputter::__construct
     */
    function __construct($output='php://output', $indent=true, $facebook=null, $flink=null)
    {
        parent::__construct($output, $indent);
        
        $this->facebook = $facebook;
        $this->flink = $flink;
        
        if ($this->flink) {
            $this->fbuid = $flink->foreign_id; 
            $this->user = $flink->getUser();
        }
        
        $this->args = array();
    }
  
    function prepare($argarray)
    {        
        parent::prepare($argarray);
          
        $this->facebook = getFacebook();
        $this->fbuid = $this->facebook->require_login();
        
        $this->action = $this->trimmed('action');
        
        $app_props = $this->facebook->api_client->Admin_getAppProperties(
                array('canvas_name', 'application_name'));
        
        $this->app_uri = 'http://apps.facebook.com/' . $app_props['canvas_name'];
        $this->app_name = $app_props['application_name'];

        $this->flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
        
        return true;
        
    }
  
    function showStylesheets()
    {
        // Add a timestamp to the file so Facebook cache wont ignore our changes
        $ts = filemtime(INSTALLDIR.'/theme/base/css/display.css');
        
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/display.css', 'base') . '?ts=' . $ts));
                                     
        $theme = common_config('site', 'theme');
        
        $ts = filemtime(INSTALLDIR. '/theme/' . $theme .'/css/display.css');
                                     
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/display.css', null) . '?ts=' . $ts));
                                     
        $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.css');
        
        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => theme_path('css/facebookapp.css', 'base') . '?ts=' . $ts));
    }
  
    function showScripts()
    {
        // Add a timestamp to the file so Facebook cache wont ignore our changes
        $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
        
        $this->element('script', array('src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
    }
    
    /**
     * Start an Facebook ready HTML document
     *
     *  For Facebook we don't want to actually output any headers,
     *  DTD info, etc.
     *
     * If $type isn't specified, will attempt to do content negotiation.
     *
     * @param string $type MIME type to use; default is to do negotation.
     *
     * @return void
     */

    function startHTML($type=null) 
    {          
        $this->elementStart('div', array('class' => 'facebook-page'));
    }

    /**
    *  Ends a Facebook ready HTML document
    *
    *  @return void
    */
    function endHTML()
    {
        $this->elementEnd('div');
        $this->endXML();
    }

    /**
     * Show notice form.
     *
     * MAY overload if no notice form needed... or direct message box????
     *
     * @return nothing
     */
    function showNoticeForm()
    {
        // don't do it for most of the Facebook pages
    }

    function showBody()
    {
        $this->elementStart('div', 'wrap');
        $this->showHeader();
        $this->showCore();
        $this->showFooter();
        $this->elementEnd('div');
    }
      
    function showAside()
    {
    }

    function showHead($error, $success)
    {
        $this->showStylesheets();
        $this->showScripts();
        
        if ($error) {
            $this->element("h1", null, $error);
        }
        
        if ($success) {
            $this->element("h1", null, $success);
        }

        $this->elementStart('fb:if-section-not-added', array('section' => 'profile'));
        $this->elementStart('span', array('id' => 'add_to_profile'));
        $this->element('fb:add-section-button', array('section' => 'profile'));
        $this->elementEnd('span');
        $this->elementEnd('fb:if-section-not-added');
        
    }

    
    // Make this into a widget later
    function showLocalNav()
    {
                
        $this->elementStart('ul', array('class' => 'nav'));

        $this->elementStart('li', array('class' =>
            ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
        $this->element('a',
            array('href' => 'index.php', 'title' => _('Home')), _('Home'));
        $this->elementEnd('li');

        $this->elementStart('li',
            array('class' =>
                ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
        $this->element('a',
            array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
        $this->elementEnd('li');

        $this->elementStart('li',
            array('class' =>
                ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
        $this->element('a',
            array('href' => 'settings.php',
                'title' => _('Settings')), _('Settings'));
        $this->elementEnd('li');

        $this->elementEnd('ul');

    }     

    /**
     * Show primary navigation.
     *
     * @return nothing
     */
    function showPrimaryNav()
    {
        // we don't want to show anything for this
    }
    
    /**
     * Show header of the page.
     *
     * Calls template methods
     *
     * @return nothing
     */
    function showHeader()
    {
        $this->elementStart('div', array('id' => 'header'));
        $this->showLogo();
        $this->showNoticeForm();
        $this->showPrimaryNav();
        $this->elementEnd('div');
    }
    
    /**
     * Show page, a template method.
     *
     * @return nothing
     */
    function showPage($error = null, $success = null)
    {
        $this->startHTML();
        $this->showHead($error, $success);
        $this->showBody();
        $this->endHTML();
    }
    

    function showInstructions()
    {

        $this->elementStart('div', array('class' => 'facebook_guide'));

        $this->elementStart('dl', array('class' => 'system_notice'));
        $this->element('dt', null, 'Page Notice');

        $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
            'with your username and password. Don\'t have a username yet? ');

        $loginmsg_part2 = _(' a new account.');

        $this->elementStart('dd');
        $this->elementStart('p');
        $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
        $this->element('a',
            array('href' => common_local_url('register')), _('Register'));
        $this->text($loginmsg_part2);
        $this->elementEnd('dd');
        $this->elementEnd('dl');
        
        $this->elementEnd('div');
        
    }


    function showLoginForm($msg = null)
    {

        $this->elementStart('div', array('class' => 'content'));
        $this->element('h1', null, _('Login'));

        if ($msg) {
             $this->element('fb:error', array('message' => $msg));
        }

        $this->showInstructions();

        $this->elementStart('div', array('id' => 'content_inner'));

        $this->elementStart('form', array('method' => 'post',
                                               'class' => 'form_settings',
                                               'id' => 'login',
                                               'action' => 'index.php'));

        $this->elementStart('fieldset');

        $this->elementStart('ul', array('class' => 'form_datas'));
        $this->elementStart('li');
        $this->input('nickname', _('Nickname'));
        $this->elementEnd('li');
        $this->elementStart('li');
        $this->password('password', _('Password'));
        $this->elementEnd('li');
        $this->elementEnd('ul');

        $this->submit('submit', _('Login'));
        $this->elementEnd('form');

        $this->elementStart('p');
        $this->element('a', array('href' => common_local_url('recoverpassword')),
                       _('Lost or forgotten password?'));
        $this->elementEnd('p');

        $this->elementEnd('div');

    }
    
    
    function updateProfileBox($notice)
    {

        // Need to include inline CSS for styling the Profile box

        $style = '<style>
         .entry-title .vcard .photo {
         float:left;
         display:inline;
         }
         .entry-title .vcard .nickname {
         margin-left:5px;
         }

         .entry-title p.entry-content {
         display:inline;
         margin-left:5px;
         }

         div.entry-content dl,
         div.entry-content dt,
         div.entry-content dd {
         display:inline;
         }

         div.entry-content dt,
         div.entry-content dd {
         display:inline;
         margin-left:5px;
         }
         div.entry-content dl.timestamp dt {
         display:none;
         }
         div.entry-content dd a {
         display:inline-block;
         }
         </style>';        

        $this->xw->openMemory();

        $item = new FacebookNoticeListItem($notice, $this);
        $item->show();

        $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
        $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";

        $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";

        $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);  

        $this->xw->openURI('php://output');
    }
    
    
    /**
     * Generate pagination links
     *
     * @param boolean $have_before is there something before?
     * @param boolean $have_after  is there something after?
     * @param integer $page        current page
     * @param string  $action      current action
     * @param array   $args        rest of query arguments
     *
     * @return nothing
     */
    function pagination($have_before, $have_after, $page, $action, $args=null)
    {
        // Does a little before-after block for next/prev page
        if ($have_before || $have_after) {
            $this->elementStart('div', array('class' => 'pagination'));
            $this->elementStart('dl', null);
            $this->element('dt', null, _('Pagination'));
            $this->elementStart('dd', null);
            $this->elementStart('ul', array('class' => 'nav'));
        }
        if ($have_before) {
            $pargs   = array('page' => $page-1);
            $newargs = $args ? array_merge($args, $pargs) : $pargs;
            $this->elementStart('li', array('class' => 'nav_prev'));
            $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
                           _('After'));
            $this->elementEnd('li');
        }
        if ($have_after) {
            $pargs   = array('page' => $page+1);
            $newargs = $args ? array_merge($args, $pargs) : $pargs;
            $this->elementStart('li', array('class' => 'nav_next'));
            $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
                           _('Before'));
            $this->elementEnd('li');
        }
        if ($have_before || $have_after) {
            $this->elementEnd('ul');
            $this->elementEnd('dd');
            $this->elementEnd('dl');
            $this->elementEnd('div');
        }
    }
    
    function updateFacebookStatus($notice) 
    {
        $prefix = $this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $this->fbuid);
        $content = "$prefix $notice->content";
        
        if ($this->facebook->api_client->users_hasAppPermission('status_update', $this->fbuid)) {
            $this->facebook->api_client->users_setStatus($content, $this->fbuid, false, true);
        }
    }
    
    function saveNewNotice()
    {

        $user = $this->flink->getUser();

        $content = $this->trimmed('status_textarea');
        
        if (!$content) {
            $this->showPage(_('No notice content!'));
            return;
        } else {
            $content_shortened = common_shorten_links($content);

            if (mb_strlen($content_shortened) > 140) {
                common_debug("Content = '$content_shortened'", __FILE__);
                common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
                $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
                return;
            }
        }

        $inter = new CommandInterpreter();

        $cmd = $inter->handle_command($user, $content_shortened);

        if ($cmd) {
            
            // XXX fix this
            
            $cmd->execute(new WebChannel());
            return;
        }

        $replyto = $this->trimmed('inreplyto');

        $notice = Notice::saveNew($user->id, $content,
            'Facebook', 1, ($replyto == 'false') ? null : $replyto);

        if (is_string($notice)) {
            $this->showPage($notice);
            return;
        }

        common_broadcast_notice($notice);
        
        // Also update the user's Facebook status
        $this->updateFacebookStatus($notice);
        $this->updateProfileBox($notice);
        
    }

}

class FacebookNoticeForm extends NoticeForm 
{
    
    var $post_action = null;
    
    /**
     * Constructor
     *
     * @param HTMLOutputter $out     output channel
     * @param string        $action  action to return to, if any
     * @param string        $content content to pre-fill
     */

    function __construct($out=null, $action=null, $content=null, 
        $post_action=null, $user=null)
    {
        parent::__construct($out, $action, $content, $user);
        $this->post_action = $post_action;
    }
    
    /**
     * Action of the form
     *
     * @return string URL of the action
     */

    function action()
    {
        return $this->post_action;
    }

}

class FacebookNoticeList extends NoticeList
{
    /**
     * show the list of notices
     *
     * "Uses up" the stream by looping through it. So, probably can't
     * be called twice on the same list.
     *
     * @return int count of notices listed.
     */

    function show()
    {
        $this->out->elementStart('div', array('id' =>'notices_primary'));
        $this->out->element('h2', null, _('Notices'));
        $this->out->elementStart('ul', array('class' => 'notices'));

        $cnt = 0;

        while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
            $cnt++;

            if ($cnt > NOTICES_PER_PAGE) {
                break;
            }

            $item = $this->newListItem($this->notice);
            $item->show();
        }

        $this->out->elementEnd('ul');
        $this->out->elementEnd('div');

        return $cnt;
    }

    /**
     * returns a new list item for the current notice
     *
     * Overridden to return a Facebook specific list item.
     *
     * @param Notice $notice the current notice
     *
     * @return FacebookNoticeListItem a list item for displaying the notice
     * formatted for display in the Facebook App.
     */

    function newListItem($notice)
    {
        return new FacebookNoticeListItem($notice, $this);
    }

}

class FacebookNoticeListItem extends NoticeListItem
{    
    /**
     * recipe function for displaying a single notice in the Facebook App.
     *
     * Overridden to strip out some of the controls that we don't
     * want to be available.
     *
     * @return void
     */

    function show()
    {
        $this->showStart();

        $this->out->elementStart('div', 'entry-title');
        $this->showAuthor();
        $this->showContent();
        $this->out->elementEnd('div');

        $this->out->elementStart('div', 'entry-content');
        $this->showNoticeLink();
        $this->showNoticeSource();
        $this->showReplyTo();
        $this->out->elementEnd('div');

        $this->showEnd();
    }

    function showNoticeLink()
    {
        $noticeurl = common_local_url('shownotice',
                                      array('notice' => $this->notice->id));
        // XXX: we need to figure this out better. Is this right?
        if (strcmp($this->notice->uri, $noticeurl) != 0 &&
            preg_match('/^http/', $this->notice->uri)) {
            $noticeurl = $this->notice->uri;
        }

        $this->out->elementStart('dl', 'timestamp');
        $this->out->element('dt', null, _('Published'));
        $this->out->elementStart('dd', null);
        $this->out->elementStart('a', array('rel' => 'bookmark',
                                        'href' => $noticeurl));
        $dt = common_date_iso8601($this->notice->created);
        $this->out->element('abbr', array('class' => 'published',
                                     'title' => $dt),
        common_date_string($this->notice->created));
        $this->out->elementEnd('a');
        $this->out->elementEnd('dd');
        $this->out->elementEnd('dl');
    }

}