]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookaction.php
Changed error and facebookaction wrap selector from @class to @id.
[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.
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->elementStart('div', array('class' => 'facebook-page'));
144     }
145
146     /**
147     *  Ends a Facebook ready HTML document
148     *
149     *  @return void
150     */
151     function endHTML()
152     {
153         $this->elementEnd('div');
154         $this->endXML();
155     }
156
157     /**
158      * Show notice form.
159      *
160      * MAY overload if no notice form needed... or direct message box????
161      *
162      * @return nothing
163      */
164     function showNoticeForm()
165     {
166         // don't do it for most of the Facebook pages
167     }
168
169     function showBody()
170     {
171         $this->elementStart('div', array('id' => 'wrap'));
172         $this->showHeader();
173         $this->showCore();
174         $this->showFooter();
175         $this->elementEnd('div');
176     }
177       
178     function showAside()
179     {
180     }
181
182     function showHead($error, $success)
183     {
184         $this->showStylesheets();
185         $this->showScripts();
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     
204     // Make this into a widget later
205     function showLocalNav()
206     {
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     /**
236      * Show primary navigation.
237      *
238      * @return nothing
239      */
240     function showPrimaryNav()
241     {
242         // we don't want to show anything for this
243     }
244     
245     /**
246      * Show header of the page.
247      *
248      * Calls template methods
249      *
250      * @return nothing
251      */
252     function showHeader()
253     {
254         $this->elementStart('div', array('id' => 'header'));
255         $this->showLogo();
256         $this->showNoticeForm();
257         $this->showPrimaryNav();
258         $this->elementEnd('div');
259     }
260     
261     /**
262      * Show page, a template method.
263      *
264      * @return nothing
265      */
266     function showPage($error = null, $success = null)
267     {
268         $this->startHTML();
269         $this->showHead($error, $success);
270         $this->showBody();
271         $this->endHTML();
272     }
273     
274
275     function showInstructions()
276     {
277
278         $this->elementStart('div', array('class' => 'facebook_guide'));
279
280         $this->elementStart('dl', array('class' => 'system_notice'));
281         $this->element('dt', null, 'Page Notice');
282
283         $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
284             'with your username and password. Don\'t have a username yet? ');
285
286         $loginmsg_part2 = _(' a new account.');
287
288         $this->elementStart('dd');
289         $this->elementStart('p');
290         $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
291         $this->element('a',
292             array('href' => common_local_url('register')), _('Register'));
293         $this->text($loginmsg_part2);
294         $this->elementEnd('dd');
295         $this->elementEnd('dl');
296         
297         $this->elementEnd('div');
298         
299     }
300
301
302     function showLoginForm($msg = null)
303     {
304
305         $this->elementStart('div', array('class' => 'content'));
306         $this->element('h1', null, _('Login'));
307
308         if ($msg) {
309              $this->element('fb:error', array('message' => $msg));
310         }
311
312         $this->showInstructions();
313
314         $this->elementStart('div', array('id' => 'content_inner'));
315
316         $this->elementStart('form', array('method' => 'post',
317                                                'class' => 'form_settings',
318                                                'id' => 'login',
319                                                'action' => 'index.php'));
320
321         $this->elementStart('fieldset');
322
323         $this->elementStart('ul', array('class' => 'form_datas'));
324         $this->elementStart('li');
325         $this->input('nickname', _('Nickname'));
326         $this->elementEnd('li');
327         $this->elementStart('li');
328         $this->password('password', _('Password'));
329         $this->elementEnd('li');
330         $this->elementEnd('ul');
331
332         $this->submit('submit', _('Login'));
333         $this->elementEnd('form');
334
335         $this->elementStart('p');
336         $this->element('a', array('href' => common_local_url('recoverpassword')),
337                        _('Lost or forgotten password?'));
338         $this->elementEnd('p');
339
340         $this->elementEnd('div');
341
342     }
343     
344     
345     function updateProfileBox($notice)
346     {
347
348         // Need to include inline CSS for styling the Profile box
349
350         $style = '<style>
351          .entry-title .vcard .photo {
352          float:left;
353          display:inline;
354          }
355          .entry-title .vcard .nickname {
356          margin-left:5px;
357          }
358
359          .entry-title p.entry-content {
360          display:inline;
361          margin-left:5px;
362          }
363
364          div.entry-content dl,
365          div.entry-content dt,
366          div.entry-content dd {
367          display:inline;
368          }
369
370          div.entry-content dt,
371          div.entry-content dd {
372          display:inline;
373          margin-left:5px;
374          }
375          div.entry-content dl.timestamp dt {
376          display:none;
377          }
378          div.entry-content dd a {
379          display:inline-block;
380          }
381          </style>';        
382
383         $this->xw->openMemory();
384
385         $item = new FacebookNoticeListItem($notice, $this);
386         $item->show();
387
388         $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
389         $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
390
391         $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
392
393         $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);  
394
395         $this->xw->openURI('php://output');
396     }
397     
398     
399     /**
400      * Generate pagination links
401      *
402      * @param boolean $have_before is there something before?
403      * @param boolean $have_after  is there something after?
404      * @param integer $page        current page
405      * @param string  $action      current action
406      * @param array   $args        rest of query arguments
407      *
408      * @return nothing
409      */
410     function pagination($have_before, $have_after, $page, $action, $args=null)
411     {
412         // Does a little before-after block for next/prev page
413         if ($have_before || $have_after) {
414             $this->elementStart('div', array('class' => 'pagination'));
415             $this->elementStart('dl', null);
416             $this->element('dt', null, _('Pagination'));
417             $this->elementStart('dd', null);
418             $this->elementStart('ul', array('class' => 'nav'));
419         }
420         if ($have_before) {
421             $pargs   = array('page' => $page-1);
422             $newargs = $args ? array_merge($args, $pargs) : $pargs;
423             $this->elementStart('li', array('class' => 'nav_prev'));
424             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
425                            _('After'));
426             $this->elementEnd('li');
427         }
428         if ($have_after) {
429             $pargs   = array('page' => $page+1);
430             $newargs = $args ? array_merge($args, $pargs) : $pargs;
431             $this->elementStart('li', array('class' => 'nav_next'));
432             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
433                            _('Before'));
434             $this->elementEnd('li');
435         }
436         if ($have_before || $have_after) {
437             $this->elementEnd('ul');
438             $this->elementEnd('dd');
439             $this->elementEnd('dl');
440             $this->elementEnd('div');
441         }
442     }
443     
444     function updateFacebookStatus($notice) 
445     {
446         $prefix = $this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $this->fbuid);
447         $content = "$prefix $notice->content";
448         
449         if ($this->facebook->api_client->users_hasAppPermission('status_update', $this->fbuid)) {
450             $this->facebook->api_client->users_setStatus($content, $this->fbuid, false, true);
451         }
452     }
453     
454     function saveNewNotice()
455     {
456
457         $user = $this->flink->getUser();
458
459         $content = $this->trimmed('status_textarea');
460         
461         if (!$content) {
462             $this->showPage(_('No notice content!'));
463             return;
464         } else {
465             $content_shortened = common_shorten_links($content);
466
467             if (mb_strlen($content_shortened) > 140) {
468                 common_debug("Content = '$content_shortened'", __FILE__);
469                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
470                 $this->showPage(_('That\'s too long. Max notice size is 140 chars.'));
471                 return;
472             }
473         }
474
475         $inter = new CommandInterpreter();
476
477         $cmd = $inter->handle_command($user, $content_shortened);
478
479         if ($cmd) {
480             
481             // XXX fix this
482             
483             $cmd->execute(new WebChannel());
484             return;
485         }
486
487         $replyto = $this->trimmed('inreplyto');
488
489         $notice = Notice::saveNew($user->id, $content,
490             'Facebook', 1, ($replyto == 'false') ? null : $replyto);
491
492         if (is_string($notice)) {
493             $this->showPage($notice);
494             return;
495         }
496
497         common_broadcast_notice($notice);
498         
499         // Also update the user's Facebook status
500         $this->updateFacebookStatus($notice);
501         $this->updateProfileBox($notice);
502         
503     }
504
505 }
506
507 class FacebookNoticeForm extends NoticeForm 
508 {
509     
510     var $post_action = null;
511     
512     /**
513      * Constructor
514      *
515      * @param HTMLOutputter $out     output channel
516      * @param string        $action  action to return to, if any
517      * @param string        $content content to pre-fill
518      */
519
520     function __construct($out=null, $action=null, $content=null, 
521         $post_action=null, $user=null)
522     {
523         parent::__construct($out, $action, $content, $user);
524         $this->post_action = $post_action;
525     }
526     
527     /**
528      * Action of the form
529      *
530      * @return string URL of the action
531      */
532
533     function action()
534     {
535         return $this->post_action;
536     }
537
538 }
539
540 class FacebookNoticeList extends NoticeList
541 {
542     /**
543      * show the list of notices
544      *
545      * "Uses up" the stream by looping through it. So, probably can't
546      * be called twice on the same list.
547      *
548      * @return int count of notices listed.
549      */
550
551     function show()
552     {
553         $this->out->elementStart('div', array('id' =>'notices_primary'));
554         $this->out->element('h2', null, _('Notices'));
555         $this->out->elementStart('ul', array('class' => 'notices'));
556
557         $cnt = 0;
558
559         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
560             $cnt++;
561
562             if ($cnt > NOTICES_PER_PAGE) {
563                 break;
564             }
565
566             $item = $this->newListItem($this->notice);
567             $item->show();
568         }
569
570         $this->out->elementEnd('ul');
571         $this->out->elementEnd('div');
572
573         return $cnt;
574     }
575
576     /**
577      * returns a new list item for the current notice
578      *
579      * Overridden to return a Facebook specific list item.
580      *
581      * @param Notice $notice the current notice
582      *
583      * @return FacebookNoticeListItem a list item for displaying the notice
584      * formatted for display in the Facebook App.
585      */
586
587     function newListItem($notice)
588     {
589         return new FacebookNoticeListItem($notice, $this);
590     }
591
592 }
593
594 class FacebookNoticeListItem extends NoticeListItem
595 {    
596     /**
597      * recipe function for displaying a single notice in the Facebook App.
598      *
599      * Overridden to strip out some of the controls that we don't
600      * want to be available.
601      *
602      * @return void
603      */
604
605     function show()
606     {
607         $this->showStart();
608
609         $this->out->elementStart('div', 'entry-title');
610         $this->showAuthor();
611         $this->showContent();
612         $this->out->elementEnd('div');
613
614         $this->out->elementStart('div', 'entry-content');
615         $this->showNoticeLink();
616         $this->showNoticeSource();
617         $this->showReplyTo();
618         $this->out->elementEnd('div');
619
620         $this->showEnd();
621     }
622
623     function showNoticeLink()
624     {
625         $noticeurl = common_local_url('shownotice',
626                                       array('notice' => $this->notice->id));
627         // XXX: we need to figure this out better. Is this right?
628         if (strcmp($this->notice->uri, $noticeurl) != 0 &&
629             preg_match('/^http/', $this->notice->uri)) {
630             $noticeurl = $this->notice->uri;
631         }
632
633         $this->out->elementStart('dl', 'timestamp');
634         $this->out->element('dt', null, _('Published'));
635         $this->out->elementStart('dd', null);
636         $this->out->elementStart('a', array('rel' => 'bookmark',
637                                         'href' => $noticeurl));
638         $dt = common_date_iso8601($this->notice->created);
639         $this->out->element('abbr', array('class' => 'published',
640                                      'title' => $dt),
641         common_date_string($this->notice->created));
642         $this->out->elementEnd('a');
643         $this->out->elementEnd('dd');
644         $this->out->elementEnd('dl');
645     }
646
647 }