]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookutil.php
A tag cloud section for groups
[quix0rs-gnu-social.git] / lib / facebookutil.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 require_once INSTALLDIR.'/extlib/facebook/facebook.php';
21 require_once INSTALLDIR.'/lib/noticelist.php';
22
23 define("FACEBOOK_SERVICE", 2); // Facebook is foreign_service ID 2
24 define("FACEBOOK_NOTICE_PREFIX", 1);
25 define("FACEBOOK_PROMPTED_UPDATE_PREF", 2);
26
27 // Gets all the notices from users with a Facebook link since a given ID
28 function get_facebook_notices($since)
29 {
30     $qry = 'SELECT notice.* ' .
31         'FROM notice ' .
32         'JOIN foreign_link ' .
33         'WHERE notice.profile_id = foreign_link.user_id ' .
34         'AND foreign_link.service = 2';
35
36     // XXX: What should the limit be?
37     return Notice::getStreamDirect($qry, 0, 100, 0, 0, null, $since);
38 }
39
40 function get_facebook()
41 {
42     $apikey = common_config('facebook', 'apikey');
43     $secret = common_config('facebook', 'secret');
44     return new Facebook($apikey, $secret);
45 }
46
47 function startFBML($indent = true)
48 {
49     global $xw;
50     $xw = new XMLWriter();
51     $xw->openURI('php://output');
52     $xw->setIndent($indent);
53 }
54
55 function update_profile_box($facebook, $fbuid, $user, $notice)
56 {
57
58     // Need to include inline CSS for styling the Profile box
59
60     $style = '<style>
61      #notices {
62          clear: both;
63          margin: 0 auto;
64          padding: 0;
65          list-style-type: none;
66          width: 600px;
67          border-top: 1px solid #dec5b5;
68      }
69      #notices a:hover {
70          text-decoration: underline;
71      }
72      .notice_single {
73          clear: both;
74          display: block;
75          margin: 0;
76          padding: 5px 5px 5px 0;
77          min-height: 48px;
78          font-family: Georgia, "Times New Roman", Times, serif;
79          font-size: 13px;
80          line-height: 16px;
81          border-bottom: 1px solid #dec5b5;
82          background-color:#FCFFF5;
83          opacity:1;
84      }
85      .notice_single:hover {
86          background-color: #f7ebcc;
87      }
88      .notice_single p {
89          display: inline;
90          margin: 0;
91          padding: 0;
92      }
93      </style>';
94
95     global $xw;
96     $xw = new XMLWriter();
97     $xw->openMemory();
98
99     $item = new FacebookNoticeListItem($notice);
100     $item->show();
101
102     $fbml = "<fb:wide>$style " . $xw->outputMemory(false) . "</fb:wide>";
103     $fbml .= "<fb:narrow>$style " . $xw->outputMemory(false) . "</fb:narrow>";
104
105     $fbml_main = "<fb:narrow>$style " . $xw->outputMemory(false) . "</fb:narrow>";
106
107     $facebook->api_client->profile_setFBML(null, $fbuid, $fbml, null, null, $fbml_main);
108 }
109
110 function getFacebookBaseCSS()
111 {
112     # Add a timestamp to the CSS file so Facebook cache wont ignore our changes
113     $ts = filemtime(INSTALLDIR.'/theme/base/css/facebookapp.base.css');
114     $cssurl = INSTALLDIR.'/theme/base/css/facebookapp.base.css' . "?ts=$ts";
115     return $cssurl;
116 }
117
118 function getFacebookThemeCSS() 
119 {
120     # Add a timestamp to the CSS file so Facebook cache wont ignore our changes
121     $ts = filemtime(theme_file('css/facebookapp.theme.css'));
122     $cssurl = theme_path('css/facebookapp.theme.css') . "?ts=$ts";
123     return $cssurl;   
124 }
125
126 function getFacebookJS() {
127
128     # Add a timestamp to the FBJS file so Facebook cache wont ignore our changes
129     $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
130     $jsurl = common_path('js/facebookapp.js') . "?ts=$ts";
131     return $jsurl;
132 }
133
134
135 // Does a little before-after block for next/prev page
136
137 function facebookPagination($have_before, $have_after, $page, $action, $args=null)
138 {
139
140     if ($have_before || $have_after) {
141         common_element_start('div', array('id' => 'pagination'));
142         common_element_start('ul', array('id' => 'nav_pagination'));
143     }
144
145     if ($have_before) {
146         $pargs = array('page' => $page-1);
147         $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
148
149         common_element_start('li', 'before');
150         common_element('a', array('href' => "index.php?page=$newargs[page]", 'rel' => 'prev'),
151                        _('« After'));
152         common_element_end('li');
153     }
154
155     if ($have_after) {
156         $pargs = array('page' => $page+1);
157         $newargs = ($args) ? array_merge($args,$pargs) : $pargs;
158         common_element_start('li', 'after');
159         common_element('a', array('href' => "index.php?page=$newargs[page]", 'rel' => 'next'),
160                        _('Before »'));
161         common_element_end('li');
162     }
163
164     if ($have_before || $have_after) {
165         common_element_end('ul');
166         common_element_end('div');
167     }
168 }
169
170
171 class FacebookNoticeList extends NoticeList
172 {
173     /**
174      * show the list of notices
175      *
176      * "Uses up" the stream by looping through it. So, probably can't
177      * be called twice on the same list.
178      *
179      * @return int count of notices listed.
180      */
181
182     function show()
183     {
184         common_element_start('div', array('id' =>'notices_primary'));
185         common_element('h2', null, _('Notices'));
186         common_element_start('ul', array('class' => 'notices'));
187
188         $cnt = 0;
189
190         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
191             $cnt++;
192
193             if ($cnt > NOTICES_PER_PAGE) {
194                 break;
195             }
196
197             $item = $this->newListItem($this->notice);
198             $item->show();
199         }
200
201         common_element_end('ul');
202         common_element_end('div');
203
204         return $cnt;
205     }
206
207     /**
208      * returns a new list item for the current notice
209      *
210      * Overridden to return a Facebook specific list item.
211      *
212      * @param Notice $notice the current notice
213      *
214      * @return FacebookNoticeListItem a list item for displaying the notice
215      * formatted for display in the Facebook App.
216      */
217
218     function newListItem($notice)
219     {
220         return new FacebookNoticeListItem($notice);
221     }
222
223 }
224
225 class FacebookNoticeListItem extends NoticeListItem
226 {
227     /**
228      * recipe function for displaying a single notice in the Facebook App.
229      *
230      * Overridden to strip out some of the controls that we don't
231      * want to be available.
232      *
233      * @return void
234      */
235
236     function show()
237     {
238         $this->showStart();
239
240         common_element_start('div', 'entry-title');
241         $this->showAuthor();
242         $this->showContent();
243         common_element_end('div');
244
245         common_element_start('div', 'entry-content');
246         $this->showNoticeLink();
247         $this->showNoticeSource();
248         $this->showReplyTo();
249         common_element_end('div');
250
251         $this->showEnd();
252     }
253
254     function showStart()
255     {
256         // XXX: RDFa
257         // TODO: add notice_type class e.g., notice_video, notice_image
258         common_element_start('li', array('class' => 'hentry notice',
259                                          'id' => 'notice-' . $this->notice->id));
260     }
261
262     function showNoticeLink()
263     {
264         $noticeurl = common_local_url('shownotice',
265                                       array('notice' => $this->notice->id));
266         // XXX: we need to figure this out better. Is this right?
267         if (strcmp($this->notice->uri, $noticeurl) != 0 &&
268             preg_match('/^http/', $this->notice->uri)) {
269             $noticeurl = $this->notice->uri;
270         }
271
272         common_element_start('dl', 'timestamp');
273         common_element('dt', null, _('Published'));
274         common_element_start('dd', null);
275         common_element_start('a', array('rel' => 'bookmark',
276                                         'href' => $noticeurl));
277         $dt = common_date_iso8601($this->notice->created);
278         common_element('abbr', array('class' => 'published',
279                                      'title' => $dt),
280         common_date_string($this->notice->created));
281         common_element_end('a');
282         common_element_end('dd');
283         common_element_end('dl');
284     }
285
286 }
287