Esempio n. 1
4
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
        $fbuid = $flink->foreign_id;
        $user = $flink->getUser();
        try {
            // Check permissions
            common_debug('FacebookPlugin - checking for publish_stream permission for user ' . "{$user->nickname} ({$user->id}), Facebook UID: {$fbuid}");
            // NOTE: $facebook->api_client->users_hasAppPermission('publish_stream', $fbuid)
            // has been returning bogus results, so we're using FQL to check for
            // publish_stream permission now
            $fql = "SELECT publish_stream FROM permissions WHERE uid = {$fbuid}";
            $result = $facebook->api_client->fql_query($fql);
            $canPublish = 0;
            if (!empty($result)) {
                $canPublish = $result[0]['publish_stream'];
            }
            if ($canPublish == 1) {
                common_debug("FacebookPlugin - {$user->nickname} ({$user->id}), Facebook UID: {$fbuid} " . 'has publish_stream permission.');
            } else {
                common_debug("FacebookPlugin - {$user->nickname} ({$user->id}), Facebook UID: {$fbuid} " . 'does NOT have publish_stream permission. Facebook ' . 'returned: ' . var_export($result, true));
            }
            common_debug('FacebookPlugin - checking for status_update permission for user ' . "{$user->nickname} ({$user->id}), Facebook UID: {$fbuid}. ");
            $canUpdate = $facebook->api_client->users_hasAppPermission('status_update', $fbuid);
            if ($canUpdate == 1) {
                common_debug("FacebookPlugin - {$user->nickname} ({$user->id}), Facebook UID: {$fbuid} " . 'has status_update permission.');
            } else {
                common_debug("FacebookPlugin - {$user->nickname} ({$user->id}), Facebook UID: {$fbuid} " . 'does NOT have status_update permission. Facebook ' . 'returned: ' . var_export($canPublish, true));
            }
            // Post to Facebook
            if ($notice->hasAttachments() && $canPublish == 1) {
                publishStream($notice, $user, $fbuid);
            } elseif ($canUpdate == 1 || $canPublish == 1) {
                statusUpdate($notice, $user, $fbuid);
            } else {
                $msg = "FacebookPlugin - Not sending notice {$notice->id} to Facebook " . "because user {$user->nickname} has not 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 ($canPublish == 1 || $canUpdate == 1) {
                updateProfileBox($facebook, $flink, $notice, $user);
            }
        } catch (FacebookRestClientException $e) {
            return handleFacebookError($e, $notice, $flink);
        }
    }
    return true;
}
Esempio n. 2
0
function facebookBroadcastNotice($notice)
{
    $facebook = getFacebook();
    $flink = Foreign_link::getByUserID($notice->profile_id, FACEBOOK_SERVICE);
    $fbuid = $flink->foreign_id;
    if (isFacebookBound($notice, $flink)) {
        $status = null;
        // Get the status 'verb' (prefix) the user has set
        try {
            $prefix = $facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX, $fbuid);
            $status = "{$prefix} {$notice->content}";
        } catch (FacebookRestClientException $e) {
            common_log(LOG_ERR, $e->getMessage());
            return false;
        }
        // Okay, we're good to go!
        try {
            $facebook->api_client->users_setStatus($status, $fbuid, false, true);
            updateProfileBox($facebook, $flink, $notice);
        } catch (FacebookRestClientException $e) {
            common_log(LOG_ERR, $e->getMessage());
            return false;
            // Should we remove flink if this fails?
        }
    }
    return true;
}
Esempio n. 3
0
 /**
  * For initializing members of the class.
  *
  * @param array $argarray misc. arguments
  *
  * @return boolean true
  */
 function prepare($args)
 {
     parent::prepare($args);
     $this->facebook = new Facebook(array('appId' => common_config('facebook', 'appid'), 'secret' => common_config('facebook', 'secret'), 'cookie' => true));
     $this->user = common_current_user();
     $this->flink = Foreign_link::getByUserID($this->user->id, FACEBOOK_SERVICE);
     return true;
 }
 protected function doPreparation()
 {
     try {
         $this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
         $this->fuser = $this->flink->getForeignUser();
     } catch (NoResultException $e) {
         // No foreign link found for this user!
     }
 }
Esempio n. 5
0
/**
 *
 * @param User $user 
 * @return TwitterOAuthClient
 */
function twitterAuthForUser(User $user)
{
    $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
    $token = TwitterOAuthClient::unpackToken($flink->credentials);
    if (!$token) {
        throw new ServerException("No Twitter OAuth credentials for this user.");
    }
    return new TwitterOAuthClient($token->key, $token->secret);
}
 /**
  * Handler method
  *
  * @param array $args is ignored since it's now passed in in prepare()
  *
  * @return nothing
  */
 function handle($args)
 {
     parent::handle($args);
     if (common_logged_in()) {
         $user = common_current_user();
         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
         // If there's already a foreign link record and a foreign user
         // it means the accounts are already linked, and this is unecessary.
         // So go back.
         if (isset($flink)) {
             $fuser = $flink->getForeignUser();
             if (!empty($fuser)) {
                 common_redirect(common_local_url('twittersettings'));
             }
         }
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         // User was not logged in to StatusNet before
         $this->twuid = $this->trimmed('twuid');
         $this->tw_fields = array('screen_name' => $this->trimmed('tw_fields_screen_name'), 'fullname' => $this->trimmed('tw_fields_fullname'));
         $this->access_token = new OAuthToken($this->trimmed('access_token_key'), $this->trimmed('access_token_secret'));
         $token = $this->trimmed('token');
         if (!$token || $token != common_session_token()) {
             // TRANS: Client error displayed when the session token does not match or is not given.
             $this->showForm(_m('There was a problem with your session token. Try again, please.'));
             return;
         }
         if ($this->arg('create')) {
             if (!$this->boolean('license')) {
                 // TRANS: Form validation error displayed when the checkbox to agree to the license has not been checked.
                 $this->showForm(_m('You cannot register if you do not agree to the license.'), $this->trimmed('newname'));
                 return;
             }
             $this->createNewUser();
         } else {
             if ($this->arg('connect')) {
                 $this->connectNewUser();
             } else {
                 common_debug('Twitter bridge - ' . print_r($this->args, true));
                 // TRANS: Form validation error displayed when an unhandled error occurs.
                 $this->showForm(_m('Something weird happened.'), $this->trimmed('newname'));
             }
         }
     } else {
         // $this->oauth_token is only populated once Twitter authorizes our
         // request token. If it's empty we're at the beginning of the auth
         // process
         if (empty($this->oauth_token)) {
             $this->authorizeRequestToken();
         } else {
             $this->saveAccessToken();
         }
     }
 }
function broadcast_twitter($notice)
{
    $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
    if (is_twitter_bound($notice, $flink)) {
        if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
            return broadcast_oauth($notice, $flink);
        } else {
            return broadcast_basicauth($notice, $flink);
        }
    }
    return true;
}
 /**
  *
  * @param Notice $notice the notice to manipulate
  * @param Profile $profile local user to act as; if left empty, the notice's poster will be used.
  */
 function __construct($notice, $profile = null)
 {
     $this->facebook = self::getFacebook();
     if (empty($this->facebook)) {
         throw new FacebookApiException("Could not create Facebook client! Bad application ID or secret?");
     }
     $this->notice = $notice;
     $profile_id = $profile ? $profile->id : $notice->profile_id;
     $this->flink = Foreign_link::getByUserID($profile_id, FACEBOOK_SERVICE);
     if (!empty($this->flink)) {
         $this->user = $this->flink->getUser();
     }
 }
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();
            $msg = "Facebook returned error code {$code}: " . $e->getMessage() . ' - ' . "Unable to update Facebook status (notice {$notice->id}) " . "for {$user->nickname} (user id: {$user->id})!";
            common_log(LOG_WARNING, $msg);
            if ($code == 100 || $code == 200 || $code == 250) {
                // 100 The account is 'inactive' (probably - this is not well documented)
                // 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;
}
Esempio n. 10
0
 /**
  *
  * @param Notice $notice the notice to manipulate
  * @param Profile $profile local user to act as; if left empty, the notice's poster will be used.
  */
 function __construct($notice, $profile = null)
 {
     $this->facebook = self::getFacebook();
     if (empty($this->facebook)) {
         throw new FacebookApiException("Could not create Facebook client! Bad application ID or secret?");
     }
     $this->notice = $notice;
     $profile_id = $profile ? $profile->id : $notice->profile_id;
     try {
         $this->flink = Foreign_link::getByUserID($profile_id, FACEBOOK_SERVICE);
         $this->user = $this->flink->getUser();
     } catch (NoResultException $e) {
         // at least $this->flink could've gotten set to something,
         // but the logic that was here before didn't care, so let's not care either
     }
 }
 protected function doPreparation()
 {
     $this->oauth_token = $this->arg('oauth_token');
     $this->verifier = $this->arg('oauth_verifier');
     if ($this->scoped instanceof Profile) {
         try {
             $flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
             $fuser = $flink->getForeignUser();
             // If there's already a foreign link record and a foreign user
             // (no exceptions were thrown when fetching either of them...)
             // it means the accounts are already linked, and this is unecessary.
             // So go back.
             common_redirect(common_local_url('twittersettings'));
         } catch (NoResultException $e) {
             // but if we don't have a foreign user linked, let's continue authorization procedure.
         }
     }
 }
 function onEndConnectSettingsNav($action)
 {
     if ($this->hasApplication()) {
         $action_name = $action->trimmed('action');
         // CurrentUserDesignAction stores the current user in $cur
         $user = $action->getCurrentUser();
         $flink = null;
         if (!empty($user)) {
             $flink = Foreign_link::getByUserID($user->id, FACEBOOK_SERVICE);
         }
         if (!empty($flink)) {
             $action->menuItem(common_local_url('facebooksettings'), _m('MENU', 'Facebook'), _m('Facebook settings'), $action_name === 'facebooksettings');
         }
     }
 }
Esempio n. 13
0
 protected function doPreparation()
 {
     $this->facebook = new Facebook(array('appId' => common_config('facebook', 'appid'), 'secret' => common_config('facebook', 'secret'), 'cookie' => true));
     $this->flink = Foreign_link::getByUserID($this->scoped->getID(), FACEBOOK_SERVICE);
 }
Esempio n. 14
0
 /**
  * Notify remote users when their notices get de-favorited.
  *
  * @param Profile $profile Profile person doing the de-faving
  * @param Notice  $notice  Notice being favored
  *
  * @return hook return value
  */
 function onEndDisfavorNotice(Profile $profile, Notice $notice)
 {
     $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
     // twitter service
     if (empty($flink)) {
         return true;
     }
     if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
         $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
         return true;
     }
     $status_id = twitter_status_id($notice);
     if (empty($status_id)) {
         return true;
     }
     $token = TwitterOAuthClient::unpackToken($flink->credentials);
     $client = new TwitterOAuthClient($token->key, $token->secret);
     $client->favoritesDestroy($status_id);
     return true;
 }
Esempio n. 15
0
 /**
  * Save user's Twitter-bridging preferences
  *
  * @return void
  */
 function savePreferences()
 {
     $noticesend = $this->boolean('noticesend');
     $noticerecv = $this->boolean('noticerecv');
     $friendsync = $this->boolean('friendsync');
     $replysync = $this->boolean('replysync');
     $user = common_current_user();
     $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
     if (empty($flink)) {
         common_log_db_error($flink, 'SELECT', __FILE__);
         $this->showForm(_m('Couldn\'t save Twitter preferences.'));
         return;
     }
     $original = clone $flink;
     $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
     $result = $flink->update($original);
     if ($result === false) {
         common_log_db_error($flink, 'UPDATE', __FILE__);
         $this->showForm(_m('Couldn\'t save Twitter preferences.'));
         return;
     }
     $this->showForm(_m('Twitter preferences saved.'), true);
 }
 public function onEndShowHeadElements(Action $action)
 {
     if ($action instanceof ShowNoticeAction) {
         // Showing a notice
         $notice = Notice::getKV('id', $action->arg('notice'));
         try {
             $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
             $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
         } catch (NoResultException $e) {
             return true;
         }
         $statusId = twitter_status_id($notice);
         if ($notice instanceof Notice && $notice->isLocal() && $statusId) {
             $tweetUrl = 'https://twitter.com/' . $fuser->nickname . '/status/' . $statusId;
             $action->element('link', array('rel' => 'syndication', 'href' => $tweetUrl));
         }
     }
     if (!$action instanceof AttachmentAction) {
         return true;
     }
     /* Twitter card support. See https://dev.twitter.com/docs/cards */
     /* @fixme: should we display twitter cards only for attachments posted
      *         by local users ? Seems mandatory to display twitter:creator
      *
      * Author: jbfavre
      */
     switch ($action->attachment->mimetype) {
         case 'image/pjpeg':
         case 'image/jpeg':
         case 'image/jpg':
         case 'image/png':
         case 'image/gif':
             $action->element('meta', array('name' => 'twitter:card', 'content' => 'photo'), null);
             $action->element('meta', array('name' => 'twitter:url', 'content' => common_local_url('attachment', array('attachment' => $action->attachment->id))), null);
             $action->element('meta', array('name' => 'twitter:image', 'content' => $action->attachment->url));
             $action->element('meta', array('name' => 'twitter:title', 'content' => $action->attachment->title));
             $ns = new AttachmentNoticeSection($action);
             $notices = $ns->getNotices();
             $noticeArray = $notices->fetchAll();
             // Should not have more than 1 notice for this attachment.
             if (count($noticeArray) != 1) {
                 break;
             }
             $post = $noticeArray[0];
             try {
                 $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE);
                 $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
                 $action->element('meta', array('name' => 'twitter:creator', 'content' => '@' . $fuser->nickname));
             } catch (NoResultException $e) {
                 // no foreign link and/or user for Twitter on this profile ID
             }
             break;
         default:
             break;
     }
     return true;
 }
Esempio n. 17
0
function broadcast_twitter($notice)
{
    $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
    // Don't bother with basic auth, since it's no longer allowed
    if (!empty($flink) && TwitterOAuthClient::isPackedToken($flink->credentials)) {
        if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) {
            $retweet = retweet_notice($flink, Notice::staticGet('id', $notice->repeat_of));
            if (!empty($retweet)) {
                Notice_to_status::saveNew($notice->id, $retweet->id);
            }
        } else {
            if (is_twitter_bound($notice, $flink)) {
                return broadcast_oauth($notice, $flink);
            }
        }
    }
    return true;
}
 /**
  * Handle post
  *
  * Disconnects the current Facebook user from the current user's account
  *
  * @return void
  */
 function handlePost()
 {
     // CSRF protection
     $token = $this->trimmed('token');
     if (!$token || $token != common_session_token()) {
         $this->showForm(_m('There was a problem with your session token. ' . 'Try again, please.'));
         return;
     }
     if ($this->arg('disconnect')) {
         $user = common_current_user();
         $flink = Foreign_link::getByUserID($user->id, FACEBOOK_CONNECT_SERVICE);
         $result = $flink->delete();
         if ($result === false) {
             common_log_db_error($user, 'DELETE', __FILE__);
             $this->serverError(_m('Couldn\'t delete link to Facebook.'));
             return;
         }
         try {
             // Clear FB Connect cookies out
             $facebook = getFacebook();
             $facebook->clear_cookie_state();
         } catch (Exception $e) {
             common_log(LOG_WARNING, 'Facebook Connect Plugin - ' . 'Couldn\'t clear Facebook cookies: ' . $e->getMessage());
         }
         $this->showForm(_m('You have disconnected from Facebook.'), true);
     } else {
         $this->showForm(_m('Not sure what you\'re trying to do.'));
         return;
     }
 }
Esempio n. 19
0
 /**
  * Save user's Twitter-bridging preferences
  *
  * @return void
  */
 function savePreferences()
 {
     $noticesend = $this->boolean('noticesend');
     $noticerecv = $this->boolean('noticerecv');
     $friendsync = $this->boolean('friendsync');
     $replysync = $this->boolean('replysync');
     $user = common_current_user();
     $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
     if (empty($flink)) {
         common_log_db_error($flink, 'SELECT', __FILE__);
         // @todo FIXME: Shouldn't this be a serverError()?
         // TRANS: Server error displayed when saving Twitter integration preferences fails.
         $this->showForm(_m('Could not save Twitter preferences.'));
         return;
     }
     $original = clone $flink;
     $wasReceiving = (bool) ($original->noticesync & FOREIGN_NOTICE_RECV);
     $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
     $result = $flink->update($original);
     if ($result === false) {
         common_log_db_error($flink, 'UPDATE', __FILE__);
         // @todo FIXME: Shouldn't this be a serverError()?
         // TRANS: Server error displayed when saving Twitter integration preferences fails.
         $this->showForm(_m('Could not save Twitter preferences.'));
         return;
     }
     if ($wasReceiving xor $noticerecv) {
         $this->notifyDaemon($flink->foreign_id, $noticerecv);
     }
     // TRANS: Success message after saving Twitter integration preferences.
     $this->showForm(_m('Twitter preferences saved.'), true);
 }
Esempio n. 20
0
 public function onEndShowHeadElements(Action $action)
 {
     if (!$action instanceof AttachmentAction) {
         return true;
     }
     /* Twitter card support. See https://dev.twitter.com/docs/cards */
     /* @fixme: should we display twitter cards only for attachments posted
      *         by local users ? Seems mandatory to display twitter:creator
      *
      * Author: jbfavre
      */
     switch ($action->attachment->mimetype) {
         case 'image/pjpeg':
         case 'image/jpeg':
         case 'image/jpg':
         case 'image/png':
         case 'image/gif':
             $action->element('meta', array('name' => 'twitter:card', 'content' => 'photo'), null);
             $action->element('meta', array('name' => 'twitter:url', 'content' => common_local_url('attachment', array('attachment' => $action->attachment->id))), null);
             $action->element('meta', array('name' => 'twitter:image', 'content' => $action->attachment->url));
             $action->element('meta', array('name' => 'twitter:title', 'content' => $action->attachment->title));
             $ns = new AttachmentNoticeSection($action);
             $notices = $ns->getNotices();
             $noticeArray = $notices->fetchAll();
             // Should not have more than 1 notice for this attachment.
             if (count($noticeArray) != 1) {
                 break;
             }
             $post = $noticeArray[0];
             $flink = Foreign_link::getByUserID($post->profile_id, TWITTER_SERVICE);
             if ($flink) {
                 // Our local user has registered Twitter Gateway
                 $fuser = Foreign_user::getForeignUser($flink->foreign_id, TWITTER_SERVICE);
                 if ($fuser) {
                     // Got nickname for local user's Twitter account
                     $action->element('meta', array('name' => 'twitter:creator', 'content' => '@' . $fuser->nickname));
                 }
             }
             break;
         default:
             break;
     }
     return true;
 }
Esempio n. 21
0
function broadcast_twitter($notice)
{
    $success = true;
    $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
    // XXX: Not sure WHERE to check whether a notice should go to
    // Twitter. Should we even put in the queue if it shouldn't? --Zach
    if (!is_null($flink) && is_twitter_bound($notice, $flink)) {
        $fuser = $flink->getForeignUser();
        $twitter_user = $fuser->nickname;
        $twitter_password = $flink->credentials;
        $uri = 'http://www.twitter.com/statuses/update.json';
        // XXX: Hack to get around PHP cURL's use of @ being a a meta character
        $statustxt = preg_replace('/^@/', ' @', $notice->content);
        $options = array(CURLOPT_USERPWD => "{$twitter_user}:{$twitter_password}", CURLOPT_POST => true, CURLOPT_POSTFIELDS => array('status' => $statustxt, 'source' => common_config('integration', 'source')), CURLOPT_RETURNTRANSFER => true, CURLOPT_FAILONERROR => true, CURLOPT_HEADER => false, CURLOPT_FOLLOWLOCATION => true, CURLOPT_USERAGENT => "Laconica", CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_HTTPHEADER => array('Expect:'));
        $ch = curl_init($uri);
        curl_setopt_array($ch, $options);
        $data = curl_exec($ch);
        $errmsg = curl_error($ch);
        if ($errmsg) {
            common_debug("cURL error: {$errmsg} - " . "trying to send notice for {$twitter_user}.", __FILE__);
            $success = false;
        }
        curl_close($ch);
        if (!$data) {
            common_debug("No data returned by Twitter's " . "API trying to send update for {$twitter_user}", __FILE__);
            $success = false;
        }
        // Twitter should return a status
        $status = json_decode($data);
        if (!$status->id) {
            common_debug("Unexpected data returned by Twitter " . " API trying to send update for {$twitter_user}", __FILE__);
            $success = false;
        }
    }
    return $success;
}
Esempio n. 22
0
 /**
  * Notify remote users when their notices get de-favorited.
  *
  * @param Profile $profile Profile person doing the de-faving
  * @param Notice  $notice  Notice being favored
  *
  * @return hook return value
  */
 function onEndDisfavorNotice(Profile $profile, Notice $notice)
 {
     $flink = Foreign_link::getByUserID($profile->id, TWITTER_SERVICE);
     // twitter service
     if (empty($flink)) {
         return true;
     }
     if (!TwitterOAuthClient::isPackedToken($flink->credentials)) {
         $this->log(LOG_INFO, "Skipping fave processing for {$profile->id} since link is not OAuth.");
         return true;
     }
     $status_id = twitter_status_id($notice);
     if (empty($status_id)) {
         return true;
     }
     try {
         $token = TwitterOAuthClient::unpackToken($flink->credentials);
         $client = new TwitterOAuthClient($token->key, $token->secret);
         $client->favoritesDestroy($status_id);
     } catch (Exception $e) {
         common_log(LOG_ERR, "Error attempting to unfavorite bridged notice on Twitter: " . $e->getMessage());
     }
     return true;
 }
 /**
  * Add links in the user's profile block to their Facebook profile URL.
  *
  * @param Profile $profile The profile being shown
  * @param Array   &$links  Writeable array of arrays (href, text, image).
  *
  * @return boolean hook value (true)
  */
 function onOtherAccountProfiles($profile, &$links)
 {
     $fuser = null;
     $flink = Foreign_link::getByUserID($profile->id, FACEBOOK_SERVICE);
     if (!empty($flink)) {
         $fuser = $this->getFacebookUser($flink->foreign_id);
         if (!empty($fuser)) {
             $links[] = array("href" => $fuser->link, "text" => sprintf(_("%s on Facebook"), $fuser->name), "image" => $this->path("images/f_logo.png"));
         }
     }
     return true;
 }
Esempio n. 24
0
/**
 * Check if we need to broadcast a notice over the Twitter bridge, and
 * do so if necessary. Will determine whether to do a straight post or
 * a repeat/retweet
 *
 * This function is meant to be called directly from TwitterQueueHandler.
 *
 * @param Notice $notice
 * @return boolean true if complete or successful, false if we should retry
 */
function broadcast_twitter($notice)
{
    $flink = Foreign_link::getByUserID($notice->profile_id, TWITTER_SERVICE);
    // Don't bother with basic auth, since it's no longer allowed
    if (!empty($flink) && TwitterOAuthClient::isPackedToken($flink->credentials)) {
        if (is_twitter_bound($notice, $flink)) {
            if (!empty($notice->repeat_of) && is_twitter_notice($notice->repeat_of)) {
                $retweet = retweet_notice($flink, Notice::staticGet('id', $notice->repeat_of));
                if (is_object($retweet)) {
                    Notice_to_status::saveNew($notice->id, twitter_id($retweet));
                    return true;
                } else {
                    // Our error processing will have decided if we need to requeue
                    // this or can discard safely.
                    return $retweet;
                }
            } else {
                return broadcast_oauth($notice, $flink);
            }
        }
    }
    return true;
}
Esempio n. 25
0
 /**
  * Save user's Twitter-bridging preferences
  *
  * @return void
  */
 function savePreferences()
 {
     $noticesync = $this->boolean('noticesync');
     $friendsync = $this->boolean('friendsync');
     $replysync = $this->boolean('replysync');
     $user = common_current_user();
     $flink = Foreign_link::getByUserID($user->id, 1);
     if (!$flink) {
         common_log_db_error($flink, 'SELECT', __FILE__);
         $this->showForm(_('Couldn\'t save Twitter preferences.'));
         return;
     }
     $twitter_id = $flink->foreign_id;
     $password = $flink->credentials;
     $fuser = $flink->getForeignUser();
     if (!$fuser) {
         common_log_db_error($fuser, 'SELECT', __FILE__);
         $this->showForm(_('Couldn\'t save Twitter preferences.'));
         return;
     }
     $screen_name = $fuser->nickname;
     $original = clone $flink;
     $flink->set_flags($noticesync, $replysync, $friendsync);
     $result = $flink->update($original);
     if ($result === false) {
         common_log_db_error($flink, 'UPDATE', __FILE__);
         $this->showForm(_('Couldn\'t save Twitter preferences.'));
         return;
     }
     if ($friendsync) {
         save_twitter_friends($user, $flink->foreign_id, $screen_name, $password);
     }
     $this->showForm(_('Twitter preferences saved.'), true);
 }
Esempio n. 26
0
 function checkForExistingLink()
 {
     // User is already logged in, are her accounts already linked?
     $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_SERVICE);
     if (!empty($flink)) {
         // User already has a linked Facebook account and shouldn't be here!
         $this->clientError(_m('There is already a local account linked with that Facebook account.'));
         return;
     }
     $cur = common_current_user();
     $flink = Foreign_link::getByUserID($cur->id, FACEBOOK_SERVICE);
     if (!empty($flink)) {
         // There's already a local user linked to this Facebook account.
         $this->clientError(_m('There is already a local account linked with that Facebook account.'));
         return;
     }
 }
Esempio n. 27
0
 function onStartLogout($action)
 {
     if ($this->hasApplication()) {
         $cur = common_current_user();
         $flink = Foreign_link::getByUserID($cur->id, FACEBOOK_SERVICE);
         if (!empty($flink)) {
             $this->facebook->setAccessToken($flink->credentials);
             if (common_config('singleuser', 'enabled')) {
                 $user = User::singleUser();
                 $destination = common_local_url('showstream', array('nickname' => $user->nickname));
             } else {
                 $destination = common_local_url('public');
             }
             $logoutUrl = $this->facebook->getLogoutUrl(array('next' => $destination));
             common_log(LOG_INFO, sprintf("Logging user out of Facebook (fbuid = %s)", $fbuid), __FILE__);
             $action->logout();
             common_redirect($logoutUrl, 303);
             return false;
             // probably never get here, but hey
         }
         return true;
     }
 }