]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookaction.php
trac750 Better permissions workflow and styling hooks
[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('type' => 'text/javascript',
126                                        'src' => common_path('js/facebookapp.js') . '?ts=' . $ts));
127     }
128     
129     /**
130      * Start an Facebook ready HTML document
131      *
132      *  For Facebook we don't want to actually output any headers,
133      *  DTD info, etc.
134      *
135      * If $type isn't specified, will attempt to do content negotiation.
136      *
137      * @param string $type MIME type to use; default is to do negotation.
138      *
139      * @return void
140      */
141
142     function startHTML($type=null) 
143     {          
144         $this->elementStart('div', array('class' => 'facebook-page'));
145     }
146
147     /**
148     *  Ends a Facebook ready HTML document
149     *
150     *  @return void
151     */
152     function endHTML()
153     {
154         $this->elementEnd('div');
155         $this->endXML();
156     }
157
158     /**
159      * Show notice form.
160      *
161      * MAY overload if no notice form needed... or direct message box????
162      *
163      * @return nothing
164      */
165     function showNoticeForm()
166     {
167         // don't do it for most of the Facebook pages
168     }
169
170     function showBody()
171     {
172         $this->elementStart('div', 'wrap');
173         $this->showHeader();
174         $this->showCore();
175         $this->showFooter();
176         $this->elementEnd('div');
177     }
178       
179     function showAside()
180     {
181     }
182
183     function showHead($error, $success)
184     {
185         $this->showStylesheets();
186         $this->showScripts();
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                 
209         $this->elementStart('ul', array('class' => 'nav'));
210
211         $this->elementStart('li', array('class' =>
212             ($this->action == 'facebookhome') ? 'current' : 'facebook_home'));
213         $this->element('a',
214             array('href' => 'index.php', 'title' => _('Home')), _('Home'));
215         $this->elementEnd('li');
216
217         $this->elementStart('li',
218             array('class' =>
219                 ($this->action == 'facebookinvite') ? 'current' : 'facebook_invite'));
220         $this->element('a',
221             array('href' => 'invite.php', 'title' => _('Invite')), _('Invite'));
222         $this->elementEnd('li');
223
224         $this->elementStart('li',
225             array('class' =>
226                 ($this->action == 'facebooksettings') ? 'current' : 'facebook_settings'));
227         $this->element('a',
228             array('href' => 'settings.php',
229                 'title' => _('Settings')), _('Settings'));
230         $this->elementEnd('li');
231
232         $this->elementEnd('ul');
233
234     }     
235
236     /**
237      * Show primary navigation.
238      *
239      * @return nothing
240      */
241     function showPrimaryNav()
242     {
243         // we don't want to show anything for this
244     }
245     
246     /**
247      * Show header of the page.
248      *
249      * Calls template methods
250      *
251      * @return nothing
252      */
253     function showHeader()
254     {
255         $this->elementStart('div', array('id' => 'header'));
256         $this->showLogo();
257         $this->showNoticeForm();
258         $this->showPrimaryNav();
259         $this->elementEnd('div');
260     }
261     
262     /**
263      * Show page, a template method.
264      *
265      * @return nothing
266      */
267     function showPage($error = null, $success = null)
268     {
269         $this->startHTML();
270         $this->showHead($error, $success);
271         $this->showBody();
272         $this->endHTML();
273     }
274     
275
276     function showInstructions()
277     {
278
279         $this->elementStart('div', array('class' => 'facebook_guide'));
280
281         $this->elementStart('dl', array('class' => 'system_notice'));
282         $this->element('dt', null, 'Page Notice');
283
284         $loginmsg_part1 = _('To use the %s Facebook Application you need to login ' .
285             'with your username and password. Don\'t have a username yet? ');
286
287         $loginmsg_part2 = _(' a new account.');
288
289         $this->elementStart('dd');
290         $this->elementStart('p');
291         $this->text(sprintf($loginmsg_part1, common_config('site', 'name')));
292         $this->element('a',
293             array('href' => common_local_url('register')), _('Register'));
294         $this->text($loginmsg_part2);
295         $this->elementEnd('dd');
296         $this->elementEnd('dl');
297         
298         $this->elementEnd('div');
299         
300     }
301
302
303     function showLoginForm($msg = null)
304     {
305
306         $this->elementStart('div', array('class' => 'content'));
307         $this->element('h1', null, _('Login'));
308
309         if ($msg) {
310              $this->element('fb:error', array('message' => $msg));
311         }
312
313         $this->showInstructions();
314
315         $this->elementStart('div', array('id' => 'content_inner'));
316
317         $this->elementStart('form', array('method' => 'post',
318                                                'class' => 'form_settings',
319                                                'id' => 'login',
320                                                'action' => 'index.php'));
321
322         $this->elementStart('fieldset');
323
324         $this->elementStart('ul', array('class' => 'form_datas'));
325         $this->elementStart('li');
326         $this->input('nickname', _('Nickname'));
327         $this->elementEnd('li');
328         $this->elementStart('li');
329         $this->password('password', _('Password'));
330         $this->elementEnd('li');
331         $this->elementEnd('ul');
332
333         $this->submit('submit', _('Login'));
334         $this->elementEnd('form');
335
336         $this->elementStart('p');
337         $this->element('a', array('href' => common_local_url('recoverpassword')),
338                        _('Lost or forgotten password?'));
339         $this->elementEnd('p');
340
341         $this->elementEnd('div');
342
343     }
344     
345     
346     function updateProfileBox($notice)
347     {
348
349         // Need to include inline CSS for styling the Profile box
350
351         $style = '<style>
352          .entry-title .vcard .photo {
353          float:left;
354          display:inline;
355          }
356          .entry-title .vcard .nickname {
357          margin-left:5px;
358          }
359
360          .entry-title p.entry-content {
361          display:inline;
362          margin-left:5px;
363          }
364
365          div.entry-content dl,
366          div.entry-content dt,
367          div.entry-content dd {
368          display:inline;
369          }
370
371          div.entry-content dt,
372          div.entry-content dd {
373          display:inline;
374          margin-left:5px;
375          }
376          div.entry-content dl.timestamp dt {
377          display:none;
378          }
379          div.entry-content dd a {
380          display:inline-block;
381          }
382          </style>';        
383
384         $this->xw->openMemory();
385
386         $item = new FacebookNoticeListItem($notice, $this);
387         $item->show();
388
389         $fbml = "<fb:wide>$style " . $this->xw->outputMemory(false) . "</fb:wide>";
390         $fbml .= "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
391
392         $fbml_main = "<fb:narrow>$style " . $this->xw->outputMemory(false) . "</fb:narrow>";
393
394         $this->facebook->api_client->profile_setFBML(null, $this->fbuid, $fbml, null, null, $fbml_main);  
395
396         $this->xw->openURI('php://output');
397     }
398     
399     
400     /**
401      * Generate pagination links
402      *
403      * @param boolean $have_before is there something before?
404      * @param boolean $have_after  is there something after?
405      * @param integer $page        current page
406      * @param string  $action      current action
407      * @param array   $args        rest of query arguments
408      *
409      * @return nothing
410      */
411     function pagination($have_before, $have_after, $page, $action, $args=null)
412     {
413         // Does a little before-after block for next/prev page
414         if ($have_before || $have_after) {
415             $this->elementStart('div', array('class' => 'pagination'));
416             $this->elementStart('dl', null);
417             $this->element('dt', null, _('Pagination'));
418             $this->elementStart('dd', null);
419             $this->elementStart('ul', array('class' => 'nav'));
420         }
421         if ($have_before) {
422             $pargs   = array('page' => $page-1);
423             $newargs = $args ? array_merge($args, $pargs) : $pargs;
424             $this->elementStart('li', array('class' => 'nav_prev'));
425             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'prev'),
426                            _('After'));
427             $this->elementEnd('li');
428         }
429         if ($have_after) {
430             $pargs   = array('page' => $page+1);
431             $newargs = $args ? array_merge($args, $pargs) : $pargs;
432             $this->elementStart('li', array('class' => 'nav_next'));
433             $this->element('a', array('href' => "$this->app_uri/$action?page=$newargs[page]", 'rel' => 'next'),
434                            _('Before'));
435             $this->elementEnd('li');
436         }
437         if ($have_before || $have_after) {
438             $this->elementEnd('ul');
439             $this->elementEnd('dd');
440             $this->elementEnd('dl');
441             $this->elementEnd('div');
442         }
443     }
444     
445
446 }
447
448 class FacebookNoticeForm extends NoticeForm 
449 {
450     
451     var $post_action = null;
452     
453     /**
454      * Constructor
455      *
456      * @param HTMLOutputter $out     output channel
457      * @param string        $action  action to return to, if any
458      * @param string        $content content to pre-fill
459      */
460
461     function __construct($out=null, $action=null, $content=null, 
462         $post_action=null, $user=null)
463     {
464         parent::__construct($out, $action, $content, $user);
465         $this->post_action = $post_action;
466     }
467     
468     /**
469      * Action of the form
470      *
471      * @return string URL of the action
472      */
473
474     function action()
475     {
476         return $this->post_action;
477     }
478
479 }
480
481 class FacebookNoticeList extends NoticeList
482 {
483     /**
484      * show the list of notices
485      *
486      * "Uses up" the stream by looping through it. So, probably can't
487      * be called twice on the same list.
488      *
489      * @return int count of notices listed.
490      */
491
492     function show()
493     {
494         $this->out->elementStart('div', array('id' =>'notices_primary'));
495         $this->out->element('h2', null, _('Notices'));
496         $this->out->elementStart('ul', array('class' => 'notices'));
497
498         $cnt = 0;
499
500         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
501             $cnt++;
502
503             if ($cnt > NOTICES_PER_PAGE) {
504                 break;
505             }
506
507             $item = $this->newListItem($this->notice);
508             $item->show();
509         }
510
511         $this->out->elementEnd('ul');
512         $this->out->elementEnd('div');
513
514         return $cnt;
515     }
516
517     /**
518      * returns a new list item for the current notice
519      *
520      * Overridden to return a Facebook specific list item.
521      *
522      * @param Notice $notice the current notice
523      *
524      * @return FacebookNoticeListItem a list item for displaying the notice
525      * formatted for display in the Facebook App.
526      */
527
528     function newListItem($notice)
529     {
530         return new FacebookNoticeListItem($notice, $this);
531     }
532
533 }
534
535 class FacebookNoticeListItem extends NoticeListItem
536 {    
537     /**
538      * recipe function for displaying a single notice in the Facebook App.
539      *
540      * Overridden to strip out some of the controls that we don't
541      * want to be available.
542      *
543      * @return void
544      */
545
546     function show()
547     {
548         $this->showStart();
549
550         $this->out->elementStart('div', 'entry-title');
551         $this->showAuthor();
552         $this->showContent();
553         $this->out->elementEnd('div');
554
555         $this->out->elementStart('div', 'entry-content');
556         $this->showNoticeLink();
557         $this->showNoticeSource();
558         $this->showReplyTo();
559         $this->out->elementEnd('div');
560
561         $this->showEnd();
562     }
563
564     function showNoticeLink()
565     {
566         $noticeurl = common_local_url('shownotice',
567                                       array('notice' => $this->notice->id));
568         // XXX: we need to figure this out better. Is this right?
569         if (strcmp($this->notice->uri, $noticeurl) != 0 &&
570             preg_match('/^http/', $this->notice->uri)) {
571             $noticeurl = $this->notice->uri;
572         }
573
574         $this->out->elementStart('dl', 'timestamp');
575         $this->out->element('dt', null, _('Published'));
576         $this->out->elementStart('dd', null);
577         $this->out->elementStart('a', array('rel' => 'bookmark',
578                                         'href' => $noticeurl));
579         $dt = common_date_iso8601($this->notice->created);
580         $this->out->element('abbr', array('class' => 'published',
581                                      'title' => $dt),
582         common_date_string($this->notice->created));
583         $this->out->elementEnd('a');
584         $this->out->elementEnd('dd');
585         $this->out->elementEnd('dl');
586     }
587
588 }