3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
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.
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.
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/>.
20 require_once INSTALLDIR.'/extlib/facebook/facebook.php';
21 require_once INSTALLDIR.'/lib/facebookaction.php';
22 require_once INSTALLDIR.'/lib/noticelist.php';
24 define("FACEBOOK_SERVICE", 2); // Facebook is foreign_service ID 2
25 define("FACEBOOK_NOTICE_PREFIX", 1);
26 define("FACEBOOK_PROMPTED_UPDATE_PREF", 2);
28 function getFacebook()
30 static $facebook = null;
32 $apikey = common_config('facebook', 'apikey');
33 $secret = common_config('facebook', 'secret');
35 if ($facebook === null) {
36 $facebook = new Facebook($apikey, $secret);
40 common_log(LOG_ERR, 'Could not make new Facebook client obj!',
47 function updateProfileBox($facebook, $flink, $notice) {
48 $fbaction = new FacebookAction($output='php://output', $indent=true, $facebook, $flink);
49 $fbaction->updateProfileBox($notice);
52 function isFacebookBound($notice, $flink) {
58 // If the user does not want to broadcast to Facebook, move along
59 if (!($flink->noticesync & FOREIGN_NOTICE_SEND == FOREIGN_NOTICE_SEND)) {
60 common_log(LOG_INFO, "Skipping notice $notice->id " .
61 'because user has FOREIGN_NOTICE_SEND bit off.');
67 // If it's not a reply, or if the user WANTS to send @-replies...
68 if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
69 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
73 // The two condition below are deal breakers:
76 if ($notice->source == 'Facebook') {
77 common_log(LOG_INFO, "Skipping notice $notice->id because its " .
78 'source is Facebook.');
82 $facebook = getFacebook();
83 $fbuid = $flink->foreign_id;
87 // Check to see if the user has given the FB app status update perms
88 $result = $facebook->api_client->
89 users_hasAppPermission('publish_stream', $fbuid);
92 $result = $facebook->api_client->
93 users_hasAppPermission('status_update', $fbuid);
96 $user = $flink->getUser();
97 $msg = "Not sending notice $notice->id to Facebook " .
98 "because user $user->nickname hasn't given the " .
99 'Facebook app \'status_update\' or \'publish_stream\' permission.';
104 } catch(FacebookRestClientException $e){
105 common_log(LOG_ERR, $e->getMessage());
115 function facebookBroadcastNotice($notice)
117 $facebook = getFacebook();
118 $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
120 if (isFacebookBound($notice, $flink)) {
123 $fbuid = $flink->foreign_id;
125 $user = $flink->getUser();
127 // Get the status 'verb' (prefix) the user has set
130 $prefix = $facebook->api_client->
131 data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid);
133 $status = "$prefix $notice->content";
135 } catch(FacebookRestClientException $e) {
136 common_log(LOG_WARNING, $e->getMessage());
137 common_log(LOG_WARNING,
138 'Unable to get the status verb setting from Facebook ' .
139 "for $user->nickname (user id: $user->id).");
142 // Okay, we're good to go, update the FB status
145 $result = $facebook->api_client->
146 users_hasAppPermission('publish_stream', $fbuid);
148 // authorized to use the stream api, so use it
149 $fbattachment = null;
150 $attachments = $notice->attachments();
152 $fbattachment=array();
153 $fbattachment['media']=array();
154 //facebook only supports one attachment per item
155 $attachment = $attachments[0];
157 if(strncmp($attachment->mimetype,'image/',strlen('image/'))==0){
158 $fbmedia['type']='image';
159 $fbmedia['src']=$attachment->url;
160 $fbmedia['href']=$attachment->url;
161 $fbattachment['media'][]=$fbmedia;
162 /* Video doesn't seem to work. The notice never makes it to facebook, and no error is reported.
163 }else if(strncmp($attachment->mimetype,'video/',strlen('image/'))==0 || $attachment->mimetype="application/ogg"){
164 $fbmedia['type']='video';
165 $fbmedia['video_src']=$attachment->url;
166 // http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29
167 // says that preview_img is required... but we have no value to put in it
168 // $fbmedia['preview_img']=$attachment->url;
169 if($attachment->title){
170 $fbmedia['video_title']=$attachment->title;
172 $fbmedia['video_type']=$attachment->mimetype;
173 $fbattachment['media'][]=$fbmedia;
175 }else if($attachment->mimetype=='audio/mpeg'){
176 $fbmedia['type']='mp3';
177 $fbmedia['src']=$attachment->url;
178 $fbattachment['media'][]=$fbmedia;
179 }else if($attachment->mimetype=='application/x-shockwave-flash'){
180 $fbmedia['type']='flash';
181 // http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29
182 // says that imgsrc is required... but we have no value to put in it
183 // $fbmedia['imgsrc']='';
184 $fbmedia['swfsrc']=$attachment->url;
185 $fbattachment['media'][]=$fbmedia;
187 $fbattachment['name']=($attachment->title?$attachment->title:$attachment->url);
188 $fbattachment['href']=$attachment->url;
191 $facebook->api_client->stream_publish($status, $fbattachment, null, null, $fbuid);
193 $facebook->api_client->users_setStatus($status, $fbuid, false, true);
195 } catch(FacebookRestClientException $e) {
196 common_log(LOG_ERR, $e->getMessage());
198 'Unable to update Facebook status for ' .
199 "$user->nickname (user id: $user->id)!");
201 $code = $e->getCode();
205 // 200 The application does not have permission to operate on the passed in uid parameter.
206 // 250 Updating status requires the extended permission status_update or publish_stream.
207 // see: http://wiki.developers.facebook.com/index.php/Users.setStatus#Example_Return_XML
209 remove_facebook_app($flink);
214 // Now try to update the profile box
217 updateProfileBox($facebook, $flink, $notice);
218 } catch(FacebookRestClientException $e) {
219 common_log(LOG_WARNING, $e->getMessage());
220 common_log(LOG_WARNING,
221 'Unable to update Facebook profile box for ' .
222 "$user->nickname (user id: $user->id).");
230 function remove_facebook_app($flink)
233 $user = $flink->getUser();
235 common_log(LOG_INFO, 'Removing Facebook App Foreign link for ' .
236 "user $user->nickname (user id: $user->id).");
238 $result = $flink->delete();
240 if (empty($result)) {
241 common_log(LOG_ERR, 'Could not remove Facebook App ' .
242 "Foreign_link for $user->nickname (user id: $user->id)!");
243 common_log_db_error($flink, 'DELETE', __FILE__);
246 // Notify the user that we are removing their FB app access
248 $result = mail_facebook_app_removed($user);
252 $msg = 'Unable to send email to notify ' .
253 "$user->nickname (user id: $user->id) " .
254 'that their Facebook app link was ' .
257 common_log(LOG_WARNING, $msg);