3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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);
39 if (empty($facebook)) {
40 common_log(LOG_ERR, 'Could not make new Facebook client obj!',
47 function isFacebookBound($notice, $flink) {
55 if ($notice->source == 'Facebook') {
56 common_log(LOG_INFO, "Skipping notice $notice->id because its " .
57 'source is Facebook.');
61 // If the user does not want to broadcast to Facebook, move along
63 if (!($flink->noticesync & FOREIGN_NOTICE_SEND == FOREIGN_NOTICE_SEND)) {
64 common_log(LOG_INFO, "Skipping notice $notice->id " .
65 'because user has FOREIGN_NOTICE_SEND bit off.');
69 // If it's not a reply, or if the user WANTS to send @-replies,
70 // then, yeah, it can go to Facebook.
72 if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
73 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) {
81 function facebookBroadcastNotice($notice)
83 $facebook = getFacebook();
84 $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
86 if (isFacebookBound($notice, $flink)) {
88 // Okay, we're good to go, update the FB status
91 $fbuid = $flink->foreign_id;
92 $user = $flink->getUser();
93 $attachments = $notice->attachments();
97 // Get the status 'verb' (prefix) the user has set
99 // XXX: Does this call count against our per user FB request limit?
100 // If so we should consider storing verb elsewhere or not storing
102 $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX,
105 $status = "$prefix $notice->content";
107 $can_publish = $facebook->api_client->users_hasAppPermission('publish_stream',
110 $can_update = $facebook->api_client->users_hasAppPermission('status_update',
112 if (!empty($attachments) && $can_publish == 1) {
113 $fbattachment = format_attachments($attachments);
114 $facebook->api_client->stream_publish($status, $fbattachment,
117 "Posted notice $notice->id w/attachment " .
118 "to Facebook user's stream (fbuid = $fbuid).");
119 } elseif ($can_update == 1 || $can_publish == 1) {
120 $facebook->api_client->users_setStatus($status, $fbuid, false, true);
122 "Posted notice $notice->id to Facebook " .
123 "as a status update (fbuid = $fbuid).");
125 $msg = "Not sending notice $notice->id to Facebook " .
126 "because user $user->nickname hasn't given the " .
127 'Facebook app \'status_update\' or \'publish_stream\' permission.';
128 common_log(LOG_WARNING, $msg);
131 // Finally, attempt to update the user's profile box
133 if ($can_publish == 1 || $can_update == 1) {
134 updateProfileBox($facebook, $flink, $notice);
137 } catch (FacebookRestClientException $e) {
139 $code = $e->getCode();
141 common_log(LOG_WARNING, 'Facebook returned error code ' .
142 $code . ': ' . $e->getMessage());
143 common_log(LOG_WARNING,
144 'Unable to update Facebook status for ' .
145 "$user->nickname (user id: $user->id)!");
147 if ($code == 200 || $code == 250) {
149 // 200 The application does not have permission to operate on the passed in uid parameter.
150 // 250 Updating status requires the extended permission status_update or publish_stream.
151 // see: http://wiki.developers.facebook.com/index.php/Users.setStatus#Example_Return_XML
153 remove_facebook_app($flink);
157 // Try sending again later.
169 function updateProfileBox($facebook, $flink, $notice) {
170 $fbaction = new FacebookAction($output = 'php://output',
171 $indent = true, $facebook, $flink);
172 $fbaction->updateProfileBox($notice);
175 function format_attachments($attachments)
177 $fbattachment = array();
178 $fbattachment['media'] = array();
180 foreach($attachments as $attachment)
182 if($enclosure = $attachment->getEnclosure()){
183 $fbmedia = get_fbmedia_for_attachment($enclosure);
185 $fbmedia = get_fbmedia_for_attachment($attachment);
188 $fbattachment['media'][]=$fbmedia;
190 $fbattachment['name'] = ($attachment->title ?
191 $attachment->title : $attachment->url);
192 $fbattachment['href'] = $attachment->url;
195 if(count($fbattachment['media'])>0){
196 unset($fbattachment['name']);
197 unset($fbattachment['href']);
199 return $fbattachment;
203 * given an File objects, returns an associative array suitable for Facebook media
205 function get_fbmedia_for_attachment($attachment)
209 if (strncmp($attachment->mimetype, 'image/', strlen('image/')) == 0) {
210 $fbmedia['type'] = 'image';
211 $fbmedia['src'] = $attachment->url;
212 $fbmedia['href'] = $attachment->url;
213 } else if ($attachment->mimetype == 'audio/mpeg') {
214 $fbmedia['type'] = 'mp3';
215 $fbmedia['src'] = $attachment->url;
216 }else if ($attachment->mimetype == 'application/x-shockwave-flash') {
217 $fbmedia['type'] = 'flash';
219 // http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29
220 // says that imgsrc is required... but we have no value to put in it
221 // $fbmedia['imgsrc']='';
223 $fbmedia['swfsrc'] = $attachment->url;
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);