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