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