]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/facebookutil.php
74e5945923e785b899b9fb6b7a2063a6b976d627
[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 start_fbml($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 getFacebookCSS()
111 {
112     # Add a timestamp to the CSS file so Facebook cache wont ignore our changes
113     $ts = filemtime(theme_file('facebookapp.css'));
114     $cssurl = theme_path('facebookapp.css') . "?ts=$ts";
115     return $cssurl;
116 }
117
118 function getFacebookJS() {
119
120     # Add a timestamp to the FBJS file so Facebook cache wont ignore our changes
121     $ts = filemtime(INSTALLDIR.'/js/facebookapp.js');
122     $jsurl = common_path('js/facebookapp.js') . "?ts=$ts";
123     return $jsurl;
124 }
125
126
127 class FacebookNoticeList extends NoticeList
128 {
129     /**
130      * show the list of notices
131      *
132      * "Uses up" the stream by looping through it. So, probably can't
133      * be called twice on the same list.
134      *
135      * @return int count of notices listed.
136      */
137
138     function show()
139     {
140         common_element_start('div', array('id' =>'notices_primary'));
141         common_element('h2', null, _('Notices'));
142         common_element_start('ul', array('class' => 'notices'));
143
144         $cnt = 0;
145
146         while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
147             $cnt++;
148
149             if ($cnt > NOTICES_PER_PAGE) {
150                 break;
151             }
152
153             $item = $this->newListItem($this->notice);
154             $item->show();
155         }
156
157         common_element_end('ul');
158         common_element_end('div');
159
160         return $cnt;
161     }
162
163     /**
164      * returns a new list item for the current notice
165      *
166      * Overridden to return a Facebook specific list item.
167      *
168      * @param Notice $notice the current notice
169      *
170      * @return FacebookNoticeListItem a list item for displaying the notice
171      * formatted for display in the Facebook App.
172      */
173
174     function newListItem($notice)
175     {
176         return new FacebookNoticeListItem($notice);
177     }
178
179 }
180
181 class FacebookNoticeListItem extends NoticeListItem
182 {
183     /**
184      * recipe function for displaying a single notice in the Facebook App.
185      *
186      * Overridden to strip out some of the controls that we don't
187      * want to be available.
188      *
189      * @return void
190      */
191
192     function show()
193     {
194         $this->showStart();
195
196         common_element_start('div', 'entry-title');
197         $this->showAuthor();
198         $this->showContent();
199         common_element_end('div');
200
201         common_element_start('div', 'entry-content');
202         $this->showNoticeLink();
203         $this->showNoticeSource();
204         $this->showReplyTo();
205         common_element_end('div');
206
207         $this->showEnd();
208     }
209
210     function showStart()
211     {
212         // XXX: RDFa
213         // TODO: add notice_type class e.g., notice_video, notice_image
214         common_element_start('li', array('class' => 'hentry notice',
215                                          'id' => 'notice-' . $this->notice->id));
216     }
217
218     function showNoticeLink()
219     {
220         $noticeurl = common_local_url('shownotice',
221                                       array('notice' => $this->notice->id));
222         // XXX: we need to figure this out better. Is this right?
223         if (strcmp($this->notice->uri, $noticeurl) != 0 &&
224             preg_match('/^http/', $this->notice->uri)) {
225             $noticeurl = $this->notice->uri;
226         }
227
228         common_element_start('dl', 'timestamp');
229         common_element('dt', null, _('Published'));
230         common_element_start('dd', null);
231         common_element_start('a', array('rel' => 'bookmark',
232                                         'href' => $noticeurl));
233         $dt = common_date_iso8601($this->notice->created);
234         common_element('abbr', array('class' => 'published',
235                                      'title' => $dt),
236         common_date_string($this->notice->created));
237         common_element_end('a');
238         common_element_end('dd');
239         common_element_end('dl');
240     }
241
242 }
243