From 78e5a5980a21c04cfdacb993ca3c37bf65d21783 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Tue, 20 Oct 2009 16:32:30 -0700 Subject: Extract out Facebook app stuff into a plugin --- plugins/Facebook/facebookutil.php | 260 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 260 insertions(+) create mode 100644 plugins/Facebook/facebookutil.php (limited to 'plugins/Facebook/facebookutil.php') diff --git a/plugins/Facebook/facebookutil.php b/plugins/Facebook/facebookutil.php new file mode 100644 index 000000000..9817837f7 --- /dev/null +++ b/plugins/Facebook/facebookutil.php @@ -0,0 +1,260 @@ +. + */ + +require_once INSTALLDIR . '/extlib/facebook/facebook.php'; +require_once INSTALLDIR . '/plugins/Facebook/facebookaction.php'; +require_once INSTALLDIR . '/lib/noticelist.php'; + +define("FACEBOOK_SERVICE", 2); // Facebook is foreign_service ID 2 +define("FACEBOOK_NOTICE_PREFIX", 1); +define("FACEBOOK_PROMPTED_UPDATE_PREF", 2); + +function getFacebook() +{ + static $facebook = null; + + $apikey = common_config('facebook', 'apikey'); + $secret = common_config('facebook', 'secret'); + + if ($facebook === null) { + $facebook = new Facebook($apikey, $secret); + } + + if (empty($facebook)) { + common_log(LOG_ERR, 'Could not make new Facebook client obj!', + __FILE__); + } + + return $facebook; +} + +function isFacebookBound($notice, $flink) { + + if (empty($flink)) { + return false; + } + + // Avoid a loop + + if ($notice->source == 'Facebook') { + common_log(LOG_INFO, "Skipping notice $notice->id because its " . + 'source is Facebook.'); + return false; + } + + // If the user does not want to broadcast to Facebook, move along + + if (!($flink->noticesync & FOREIGN_NOTICE_SEND == FOREIGN_NOTICE_SEND)) { + common_log(LOG_INFO, "Skipping notice $notice->id " . + 'because user has FOREIGN_NOTICE_SEND bit off.'); + return false; + } + + // If it's not a reply, or if the user WANTS to send @-replies, + // then, yeah, it can go to Facebook. + + if (!preg_match('/@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) || + ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY)) { + return true; + } + + return false; + +} + +function facebookBroadcastNotice($notice) +{ + $facebook = getFacebook(); + $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE); + + if (isFacebookBound($notice, $flink)) { + + // Okay, we're good to go, update the FB status + + $status = null; + $fbuid = $flink->foreign_id; + $user = $flink->getUser(); + $attachments = $notice->attachments(); + + try { + + // Get the status 'verb' (prefix) the user has set + + // XXX: Does this call count against our per user FB request limit? + // If so we should consider storing verb elsewhere or not storing + + $prefix = trim($facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, + $fbuid)); + + $status = "$prefix $notice->content"; + + $can_publish = $facebook->api_client->users_hasAppPermission('publish_stream', + $fbuid); + + $can_update = $facebook->api_client->users_hasAppPermission('status_update', + $fbuid); + if (!empty($attachments) && $can_publish == 1) { + $fbattachment = format_attachments($attachments); + $facebook->api_client->stream_publish($status, $fbattachment, + null, null, $fbuid); + common_log(LOG_INFO, + "Posted notice $notice->id w/attachment " . + "to Facebook user's stream (fbuid = $fbuid)."); + } elseif ($can_update == 1 || $can_publish == 1) { + $facebook->api_client->users_setStatus($status, $fbuid, false, true); + common_log(LOG_INFO, + "Posted notice $notice->id to Facebook " . + "as a status update (fbuid = $fbuid)."); + } else { + $msg = "Not sending notice $notice->id to Facebook " . + "because user $user->nickname hasn't given the " . + 'Facebook app \'status_update\' or \'publish_stream\' permission.'; + common_log(LOG_WARNING, $msg); + } + + // Finally, attempt to update the user's profile box + + if ($can_publish == 1 || $can_update == 1) { + updateProfileBox($facebook, $flink, $notice); + } + + } catch (FacebookRestClientException $e) { + + $code = $e->getCode(); + + common_log(LOG_WARNING, 'Facebook returned error code ' . + $code . ': ' . $e->getMessage()); + common_log(LOG_WARNING, + 'Unable to update Facebook status for ' . + "$user->nickname (user id: $user->id)!"); + + if ($code == 200 || $code == 250) { + + // 200 The application does not have permission to operate on the passed in uid parameter. + // 250 Updating status requires the extended permission status_update or publish_stream. + // see: http://wiki.developers.facebook.com/index.php/Users.setStatus#Example_Return_XML + + remove_facebook_app($flink); + + } else { + + // Try sending again later. + + return false; + } + + } + } + + return true; + +} + +function updateProfileBox($facebook, $flink, $notice) { + $fbaction = new FacebookAction($output = 'php://output', + $indent = true, $facebook, $flink); + $fbaction->updateProfileBox($notice); +} + +function format_attachments($attachments) +{ + $fbattachment = array(); + $fbattachment['media'] = array(); + + foreach($attachments as $attachment) + { + if($enclosure = $attachment->getEnclosure()){ + $fbmedia = get_fbmedia_for_attachment($enclosure); + }else{ + $fbmedia = get_fbmedia_for_attachment($attachment); + } + if($fbmedia){ + $fbattachment['media'][]=$fbmedia; + }else{ + $fbattachment['name'] = ($attachment->title ? + $attachment->title : $attachment->url); + $fbattachment['href'] = $attachment->url; + } + } + if(count($fbattachment['media'])>0){ + unset($fbattachment['name']); + unset($fbattachment['href']); + } + return $fbattachment; +} + +/** +* given an File objects, returns an associative array suitable for Facebook media +*/ +function get_fbmedia_for_attachment($attachment) +{ + $fbmedia = array(); + + if (strncmp($attachment->mimetype, 'image/', strlen('image/')) == 0) { + $fbmedia['type'] = 'image'; + $fbmedia['src'] = $attachment->url; + $fbmedia['href'] = $attachment->url; + } else if ($attachment->mimetype == 'audio/mpeg') { + $fbmedia['type'] = 'mp3'; + $fbmedia['src'] = $attachment->url; + }else if ($attachment->mimetype == 'application/x-shockwave-flash') { + $fbmedia['type'] = 'flash'; + + // http://wiki.developers.facebook.com/index.php/Attachment_%28Streams%29 + // says that imgsrc is required... but we have no value to put in it + // $fbmedia['imgsrc']=''; + + $fbmedia['swfsrc'] = $attachment->url; + }else{ + return false; + } + return $fbmedia; +} + +function remove_facebook_app($flink) +{ + + $user = $flink->getUser(); + + common_log(LOG_INFO, 'Removing Facebook App Foreign link for ' . + "user $user->nickname (user id: $user->id)."); + + $result = $flink->delete(); + + if (empty($result)) { + common_log(LOG_ERR, 'Could not remove Facebook App ' . + "Foreign_link for $user->nickname (user id: $user->id)!"); + common_log_db_error($flink, 'DELETE', __FILE__); + } + + // Notify the user that we are removing their FB app access + + $result = mail_facebook_app_removed($user); + + if (!$result) { + + $msg = 'Unable to send email to notify ' . + "$user->nickname (user id: $user->id) " . + 'that their Facebook app link was ' . + 'removed!'; + + common_log(LOG_WARNING, $msg); + } + +} -- cgit v1.2.3-54-g00ecf From 3e5c8f28c362e945ca7d89e7aaff1e1304a9192a Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Wed, 21 Oct 2009 17:29:54 +0000 Subject: Fix references --- plugins/Facebook/FBConnectAuth.php | 2 +- plugins/Facebook/FBConnectLogin.php | 3 ++- plugins/Facebook/facebookutil.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'plugins/Facebook/facebookutil.php') diff --git a/plugins/Facebook/FBConnectAuth.php b/plugins/Facebook/FBConnectAuth.php index 647d5def8..b909a4977 100644 --- a/plugins/Facebook/FBConnectAuth.php +++ b/plugins/Facebook/FBConnectAuth.php @@ -27,7 +27,7 @@ * @link http://status.net/ */ -require_once INSTALLDIR . '/plugins/FBConnect/FBConnectPlugin.php'; +require_once INSTALLDIR . '/plugins/Facebook/FacebookPlugin.php'; class FBConnectauthAction extends Action { diff --git a/plugins/Facebook/FBConnectLogin.php b/plugins/Facebook/FBConnectLogin.php index 5696d8848..f146bef7d 100644 --- a/plugins/Facebook/FBConnectLogin.php +++ b/plugins/Facebook/FBConnectLogin.php @@ -21,7 +21,8 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } -require_once INSTALLDIR . '/plugins/FBConnect/FBConnectPlugin.php'; + +require_once INSTALLDIR . '/plugins/Facebook/FacebookPlugin.php'; class FBConnectLoginAction extends Action { diff --git a/plugins/Facebook/facebookutil.php b/plugins/Facebook/facebookutil.php index 9817837f7..da53f35e2 100644 --- a/plugins/Facebook/facebookutil.php +++ b/plugins/Facebook/facebookutil.php @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -require_once INSTALLDIR . '/extlib/facebook/facebook.php'; +require_once INSTALLDIR . '/plugins/Facebook/facebook/facebook.php'; require_once INSTALLDIR . '/plugins/Facebook/facebookaction.php'; require_once INSTALLDIR . '/lib/noticelist.php'; -- cgit v1.2.3-54-g00ecf From 6d6de3c1c7b359815aa0381b4cde3fcc8258cc80 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 23 Oct 2009 23:38:45 +0000 Subject: Move Twitter and Facebook-specific mail notifications to their respective plugins --- lib/mail.php | 72 --------------------------------------- plugins/Facebook/facebookutil.php | 35 +++++++++++++++++++ plugins/TwitterBridge/twitter.php | 37 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 72 deletions(-) (limited to 'plugins/Facebook/facebookutil.php') diff --git a/lib/mail.php b/lib/mail.php index 5bf4d7425..5218059e9 100644 --- a/lib/mail.php +++ b/lib/mail.php @@ -640,75 +640,3 @@ function mail_notify_attn($user, $notice) mail_to_user($user, $subject, $body); } -/** - * Send a mail message to notify a user that her Twitter bridge link - * has stopped working, and therefore has been removed. This can - * happen when the user changes her Twitter password, or otherwise - * revokes access. - * - * @param User $user user whose Twitter bridge link has been removed - * - * @return boolean success flag - */ - -function mail_twitter_bridge_removed($user) -{ - common_init_locale($user->language); - - $profile = $user->getProfile(); - - $subject = sprintf(_('Your Twitter bridge has been disabled.')); - - $site_name = common_config('site', 'name'); - - $body = sprintf(_('Hi, %1$s. We\'re sorry to inform you that your ' . - 'link to Twitter has been disabled. We no longer seem to have ' . - 'permission to update your Twitter status. (Did you revoke ' . - '%3$s\'s access?)' . "\n\n" . - 'You can re-enable your Twitter bridge by visiting your ' . - "Twitter settings page:\n\n\t%2\$s\n\n" . - "Regards,\n%3\$s\n"), - $profile->getBestName(), - common_local_url('twittersettings'), - common_config('site', 'name')); - - common_init_locale(); - return mail_to_user($user, $subject, $body); -} - -/** - * Send a mail message to notify a user that her Facebook Application - * access has been removed. - * - * @param User $user user whose Facebook app link has been removed - * - * @return boolean success flag - */ - -function mail_facebook_app_removed($user) -{ - common_init_locale($user->language); - - $profile = $user->getProfile(); - - $site_name = common_config('site', 'name'); - - $subject = sprintf( - _('Your %1$s Facebook application access has been disabled.', - $site_name)); - - $body = sprintf(_("Hi, %1\$s. We're sorry to inform you that we are " . - 'unable to update your Facebook status from %2$s, and have disabled ' . - 'the Facebook application for your account. This may be because ' . - 'you have removed the Facebook application\'s authorization, or ' . - 'have deleted your Facebook account. You can re-enable the ' . - 'Facebook application and automatic status updating by ' . - "re-installing the %2\$s Facebook application.\n\nRegards,\n\n%2\$s"), - $user->nickname, $site_name); - - common_init_locale(); - return mail_to_user($user, $subject, $body); - -} - - diff --git a/plugins/Facebook/facebookutil.php b/plugins/Facebook/facebookutil.php index da53f35e2..6f50c173a 100644 --- a/plugins/Facebook/facebookutil.php +++ b/plugins/Facebook/facebookutil.php @@ -258,3 +258,38 @@ function remove_facebook_app($flink) } } + +/** + * Send a mail message to notify a user that her Facebook Application + * access has been removed. + * + * @param User $user user whose Facebook app link has been removed + * + * @return boolean success flag + */ + +function mail_facebook_app_removed($user) +{ + common_init_locale($user->language); + + $profile = $user->getProfile(); + + $site_name = common_config('site', 'name'); + + $subject = sprintf( + _('Your %1$s Facebook application access has been disabled.', + $site_name)); + + $body = sprintf(_("Hi, %1\$s. We're sorry to inform you that we are " . + 'unable to update your Facebook status from %2$s, and have disabled ' . + 'the Facebook application for your account. This may be because ' . + 'you have removed the Facebook application\'s authorization, or ' . + 'have deleted your Facebook account. You can re-enable the ' . + 'Facebook application and automatic status updating by ' . + "re-installing the %2\$s Facebook application.\n\nRegards,\n\n%2\$s"), + $user->nickname, $site_name); + + common_init_locale(); + return mail_to_user($user, $subject, $body); + +} diff --git a/plugins/TwitterBridge/twitter.php b/plugins/TwitterBridge/twitter.php index ac1f49c36..1a5248a9b 100644 --- a/plugins/TwitterBridge/twitter.php +++ b/plugins/TwitterBridge/twitter.php @@ -312,3 +312,40 @@ function remove_twitter_link($flink) } } + +/** + * Send a mail message to notify a user that her Twitter bridge link + * has stopped working, and therefore has been removed. This can + * happen when the user changes her Twitter password, or otherwise + * revokes access. + * + * @param User $user user whose Twitter bridge link has been removed + * + * @return boolean success flag + */ + +function mail_twitter_bridge_removed($user) +{ + common_init_locale($user->language); + + $profile = $user->getProfile(); + + $subject = sprintf(_('Your Twitter bridge has been disabled.')); + + $site_name = common_config('site', 'name'); + + $body = sprintf(_('Hi, %1$s. We\'re sorry to inform you that your ' . + 'link to Twitter has been disabled. We no longer seem to have ' . + 'permission to update your Twitter status. (Did you revoke ' . + '%3$s\'s access?)' . "\n\n" . + 'You can re-enable your Twitter bridge by visiting your ' . + "Twitter settings page:\n\n\t%2\$s\n\n" . + "Regards,\n%3\$s\n"), + $profile->getBestName(), + common_local_url('twittersettings'), + common_config('site', 'name')); + + common_init_locale(); + return mail_to_user($user, $subject, $body); +} + -- cgit v1.2.3-54-g00ecf