protected function processPageActions($fb_user)
 {
     $messages = array("error" => '', "success" => '', "info" => '');
     //authorize user
     if (isset($_GET["session"])) {
         $session_data = json_decode(str_replace("\\", "", $_GET["session"]));
         if (!isset($fb_user) && !isset($fb_user['name'])) {
             $user = FacebookGraphAPIAccessor::apiRequest('/me', $session_data->access_token);
             $fb_username = $user->name;
         } else {
             $fb_username = $fb_user['name'];
         }
         $messages['success'] = $this->saveAccessToken($session_data->uid, $session_data->access_token, $fb_username);
     }
     //insert pages
     if (isset($_GET["action"]) && $_GET["action"] == "add page" && isset($_GET["facebook_page_id"]) && isset($_GET["viewer_id"]) && isset($_GET["owner_id"]) && isset($_GET["instance_id"])) {
         //get access token
         $oid = DAOFactory::getDAO('OwnerInstanceDAO');
         $tokens = $oid->getOAuthTokens($_GET["instance_id"]);
         $access_token = $tokens['oauth_access_token'];
         $page_data = FacebookGraphAPIAccessor::apiRequest('/' . $_GET["facebook_page_id"], $access_token);
         $messages = self::insertPage($page_data->id, $_GET["viewer_id"], $_GET["instance_id"], $page_data->name, $page_data->picture, $messages);
     }
     return $messages;
 }
 /**
  * Process actions based on $_GET parameters. Authorize FB user or add FB page.
  * @param arr $options Facebook plugin options
  * @param Facebook $facebook Facebook object
  */
 protected function processPageActions($options, Facebook $facebook)
 {
     //authorize user
     if (isset($_GET["code"]) && isset($_GET["state"])) {
         //validate state to avoid CSRF attacks
         if ($_GET["state"] == SessionCache::get('facebook_auth_csrf')) {
             //Prepare API request
             //First, prep redirect URI
             $redirect_uri = urlencode(Utils::getApplicationURL() . 'account/?p=facebook');
             //Build API request URL
             $api_req = 'https://graph.facebook.com/oauth/access_token?client_id=' . $options['facebook_app_id']->option_value . '&client_secret=' . $options['facebook_api_secret']->option_value . '&redirect_uri=' . $redirect_uri . '&state=' . SessionCache::get('facebook_auth_csrf') . '&code=' . $_GET["code"];
             $access_token_response = FacebookGraphAPIAccessor::rawApiRequest($api_req, false);
             parse_str($access_token_response);
             if (isset($access_token)) {
                 /**
                  * Swap in short-term token for long-lived token as per
                  * https://developers.facebook.com/docs/facebook-login/access-tokens/#extending
                  */
                 $api_req = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=' . $options['facebook_app_id']->option_value . '&client_secret=' . $options['facebook_api_secret']->option_value . '&fb_exchange_token=' . $access_token;
                 $access_token_response = FacebookGraphAPIAccessor::rawApiRequest($api_req, false);
                 parse_str($access_token_response);
                 $facebook->setAccessToken($access_token);
                 $fb_user_profile = $facebook->api('/me');
                 $fb_username = $fb_user_profile['name'];
                 $fb_user_id = $fb_user_profile['id'];
                 if (empty($fb_username)) {
                     $error = 'Sorry, ThinkUp does not support business accounts.';
                     $this->addErrorMessage($error, 'authorization');
                 } else {
                     $this->addSuccessMessage($this->saveAccessToken($fb_user_id, $access_token, $fb_username), 'authorization');
                 }
             } else {
                 $error_msg = "Problem authorizing your Facebook account! Please correct your plugin settings.";
                 $error_object = json_decode($access_token_response);
                 if (isset($error_object) && isset($error_object->error->type) && isset($error_object->error->message)) {
                     $error_msg = $error_msg . "<br>Facebook says: \"" . $error_object->error->type . ": " . $error_object->error->message . "\"";
                 } else {
                     $error_msg = $error_msg . "<br>Facebook's response: \"" . $access_token_response . "\"";
                 }
                 $this->addErrorMessage($error_msg, 'authorization', true);
             }
         } else {
             $this->addErrorMessage("Could not authenticate Facebook account due to invalid CSRF token.", 'authorization');
         }
     }
     //insert pages
     if (isset($_GET["action"]) && $_GET["action"] == "add page" && isset($_GET["facebook_page_id"]) && isset($_GET["viewer_id"]) && isset($_GET["owner_id"]) && isset($_GET["instance_id"])) {
         //get access token
         $oid = DAOFactory::getDAO('OwnerInstanceDAO');
         $tokens = $oid->getOAuthTokens($_GET["instance_id"]);
         $access_token = $tokens['oauth_access_token'];
         $page_data = FacebookGraphAPIAccessor::apiRequest('/' . $_GET["facebook_page_id"], $access_token, "id,name,picture");
         self::insertPage($page_data->id, $_GET["viewer_id"], $_GET["instance_id"], $page_data->name, $page_data->picture->data->url);
     }
 }
Ejemplo n.º 3
0
 /**
  * Convert parsed JSON of a profile or page's posts into ThinkUp posts and users
  * @param Object $stream
  * @param str $source The network for the post, either 'facebook' or 'facebook page'
  * @param int Page number being processed
  * @return int $total_added_posts How many posts (excluding comments) got added to the data store
  */
 private function processStream($stream, $network, $page_number)
 {
     $thinkup_posts = array();
     $total_added_posts = 0;
     $total_added_comments = 0;
     $thinkup_users = array();
     $total_added_users = 0;
     $thinkup_links = array();
     $total_links_added = 0;
     $thinkup_likes = array();
     $total_added_likes = 0;
     $profiles = array();
     //efficiency control vars
     $must_process_likes = true;
     $must_process_comments = true;
     $post_comments_added = 0;
     $post_likes_added = 0;
     $comments_difference = false;
     $likes_difference = false;
     $post_dao = DAOFactory::getDAO('PostDAO');
     foreach ($stream->data as $index => $p) {
         $post_id = explode("_", $p->id);
         $post_id = $post_id[1];
         $this->logger->logInfo("Beginning to process " . $post_id . ", post " . ($index + 1) . " of " . count($stream->data) . " on page " . $page_number, __METHOD__ . ',' . __LINE__);
         // stream can contain posts from multiple users.  get profile for this post
         $profile = null;
         if (!empty($profiles[$p->from->id])) {
             $profile = $profiles[$p->from->id];
         } else {
             $profile = $this->fetchUser($p->from->id, 'Post stream', true);
             $profiles[$p->from->id] = $profile;
         }
         //Assume profile comments are private and page posts are public
         $is_protected = $network == 'facebook' ? 1 : 0;
         //Get likes count
         $likes_count = 0;
         //Normalize likes to be one array
         if (isset($p->likes)) {
             $likes_count = $p->likes->summary->total_count;
             $p->likes = $this->normalizeLikes($p->likes);
         }
         // Normalize comments to be one array
         $comments_count = 0;
         if (isset($p->comments)) {
             $comments_count = $p->comments->summary->total_count;
             $p->comments = $this->normalizeComments($p->comments);
         }
         $post_in_storage = $post_dao->getPost($post_id, $network);
         //Figure out if we have to process likes and comments
         if (isset($post_in_storage)) {
             $this->logger->logInfo("Post " . $post_id . " already in storage", __METHOD__ . ',' . __LINE__);
             if ($post_in_storage->favlike_count_cache >= $likes_count) {
                 $must_process_likes = false;
                 $this->logger->logInfo("Already have " . $likes_count . " like(s) for post " . $post_id . " in storage; skipping like processing", __METHOD__ . ',' . __LINE__);
             } else {
                 $likes_difference = $likes_count - $post_in_storage->favlike_count_cache;
                 $this->logger->logInfo($likes_difference . " new like(s) to process for post " . $post_id, __METHOD__ . ',' . __LINE__);
             }
             if (isset($p->comments->summary->total_count)) {
                 if ($post_in_storage->reply_count_cache >= $p->comments->summary->total_count) {
                     $must_process_comments = false;
                     $this->logger->logInfo("Already have " . $post_in_storage->reply_count_cache . " comment(s) for post " . $post_id . "; skipping comment processing", __METHOD__ . ',' . __LINE__);
                 } else {
                     $comments_difference = $p->comments->summary->total_count - $post_in_storage->reply_count_cache;
                     $this->logger->logInfo($comments_difference . " new comment(s) of " . $p->comments->summary->total_count . " total to process for post " . $post_id, __METHOD__ . ',' . __LINE__);
                 }
             }
         } else {
             $this->logger->logInfo("Post " . $post_id . " not in storage", __METHOD__ . ',' . __LINE__);
         }
         if (!isset($profile)) {
             $this->logger->logError("No profile set", __METHOD__ . ',' . __LINE__);
         } else {
             if (!isset($post_in_storage)) {
                 $this->logger->logInfo("Post " . $post_id . " has " . $comments_count . " comments", __METHOD__ . ',' . __LINE__);
                 $post_to_process = array("post_id" => $post_id, "author_username" => $profile->username, "author_fullname" => $profile->username, "author_avatar" => $profile->avatar, "author_user_id" => $p->from->id, "post_text" => isset($p->message) ? $p->message : '', "pub_date" => $p->created_time, "favlike_count_cache" => $likes_count, "reply_count_cache" => $comments_count, "in_reply_to_user_id" => isset($p->to->data[0]->id) ? $p->to->data[0]->id : '', "in_reply_to_post_id" => '', "source" => '', 'network' => $network, 'is_protected' => $is_protected, 'location' => '');
                 $new_post_key = $this->storePostAndAuthor($post_to_process, "Owner stream");
                 if ($new_post_key !== false) {
                     $total_added_posts++;
                 }
                 if (isset($p->source) || isset($p->link)) {
                     // there's a link to store
                     $link_url = isset($p->source) ? $p->source : $p->link;
                     $link = new Link(array("url" => $link_url, "expanded_url" => '', "image_src" => isset($p->picture) ? $p->picture : '', "caption" => isset($p->caption) ? $p->caption : '', "description" => isset($p->description) ? $p->description : '', "title" => isset($p->name) ? $p->name : '', "post_key" => $new_post_key));
                     array_push($thinkup_links, $link);
                 }
                 $total_links_addded = $total_links_added + $this->storeLinks($thinkup_links);
                 if ($total_links_added > 0) {
                     $this->logger->logUserSuccess("Collected {$total_links_added} new links", __METHOD__ . ',' . __LINE__);
                 }
                 //free up memory
                 $thinkup_links = array();
             } else {
                 // post already exists in storage
                 if ($must_process_likes) {
                     //update its like count only
                     $post_dao->updateFavLikeCount($post_id, $network, $likes_count);
                     $this->logger->logInfo("Updated Like count for post " . $post_id . " to " . $likes_count, __METHOD__ . ',' . __LINE__);
                 }
             }
             if ($must_process_comments) {
                 if (isset($p->comments)) {
                     $comments_captured = 0;
                     if (isset($p->comments->data)) {
                         $post_comments = $p->comments->data;
                         $post_comments_count = isset($post_comments) ? sizeof($post_comments) : 0;
                         if (is_array($post_comments) && sizeof($post_comments) > 0) {
                             foreach ($post_comments as $c) {
                                 if (isset($c->from)) {
                                     // Sometimes the id is parent_poster_postId
                                     // sometimes it's just parent_postId
                                     $comment_id = explode("_", $c->id);
                                     if (count($comment_id) == 3) {
                                         $comment_id = $comment_id[2];
                                     } else {
                                         $comment_id = $comment_id[1];
                                     }
                                     //only add to queue if not already in storage
                                     $comment_in_storage = $post_dao->getPost($comment_id, $network);
                                     if (!isset($comment_in_storage)) {
                                         $comment_to_process = array("post_id" => $comment_id, "author_username" => $c->from->name, "author_fullname" => $c->from->name, "author_gender" => $c->from->gender, "author_birthday" => $c->from->birthday, "author_avatar" => 'https://graph.facebook.com/' . $c->from->id . '/picture', "author_user_id" => $c->from->id, "post_text" => $c->message, "pub_date" => $c->created_time, "in_reply_to_user_id" => $profile->user_id, "in_reply_to_post_id" => $post_id, "source" => '', 'network' => $network, 'is_protected' => $is_protected, 'location' => '');
                                         array_push($thinkup_posts, $comment_to_process);
                                         $comments_captured = $comments_captured + 1;
                                     }
                                 }
                             }
                         }
                     }
                     $post_comments_added = $post_comments_added + $this->storePostsAndAuthors($thinkup_posts, "Post stream comments");
                     //free up memory
                     $thinkup_posts = array();
                     if (is_int($comments_difference) && $post_comments_added >= $comments_difference) {
                         $must_process_comments = false;
                         if (isset($comments_stream->paging->next)) {
                             $this->logger->logInfo("Caught up on post " . $post_id . "'s balance of " . $comments_difference . " comments; stopping comment processing", __METHOD__ . ',' . __LINE__);
                         }
                     }
                     // collapsed comment thread
                     if (isset($p->comments->summary->total_count) && $p->comments->summary->total_count > $comments_captured && $must_process_comments) {
                         if (is_int($comments_difference)) {
                             $offset = $p->comments->summary->total_count - $comments_difference;
                             $offset_arr = array('offset' => $offset, 'limit' => $comments_difference);
                         } else {
                             $offset_arr = null;
                         }
                         $api_call = $p->from->id . '_' . $post_id . '/comments';
                         do {
                             $comments_stream = FacebookGraphAPIAccessor::apiRequest($api_call, $this->access_token, $offset_arr);
                             if (isset($comments_stream) && isset($comments_stream->data) && is_array($comments_stream->data)) {
                                 foreach ($comments_stream->data as $c) {
                                     if (isset($c->from)) {
                                         $comment_id = explode("_", $c->id);
                                         $comment_id = $comment_id[sizeof($comment_id) - 1];
                                         //only add to queue if not already in storage
                                         $comment_in_storage = $post_dao->getPost($comment_id, $network);
                                         if (!isset($comment_in_storage)) {
                                             $comment_to_process = array("post_id" => $comment_id, "author_username" => $c->from->name, "author_fullname" => $c->from->name, "author_avatar" => 'https://graph.facebook.com/' . $c->from->id . '/picture', "author_user_id" => $c->from->id, "post_text" => $c->message, "pub_date" => $c->created_time, "in_reply_to_user_id" => $profile->user_id, "in_reply_to_post_id" => $post_id, "source" => '', 'network' => $network, 'is_protected' => $is_protected, 'location' => '');
                                             array_push($thinkup_posts, $comment_to_process);
                                         }
                                     }
                                 }
                                 $post_comments_added = $post_comments_added + $this->storePostsAndAuthors($thinkup_posts, "Posts stream comments collapsed");
                                 if (is_int($comments_difference) && $post_comments_added >= $comments_difference) {
                                     $must_process_comments = false;
                                     if (isset($comments_stream->paging->next)) {
                                         $this->logger->logInfo("Caught up on post " . $post_id . "'s balance of " . $comments_difference . " comments; stopping comment processing", __METHOD__ . ',' . __LINE__);
                                     }
                                 }
                                 //free up memory
                                 $thinkup_posts = array();
                                 if (isset($comments_stream->paging->next)) {
                                     $api_call = str_replace('\\u00257C', '|', $comments_stream->paging->next);
                                 }
                             } else {
                                 // no comments (pun intended)
                                 break;
                             }
                         } while (isset($comments_stream->paging->next) && $must_process_comments);
                     }
                 }
                 if ($post_comments_added > 0) {
                     //let user know
                     $this->logger->logUserSuccess("Added " . $post_comments_added . " comment(s) for post " . $post_id, __METHOD__ . ',' . __LINE__);
                 } else {
                     $this->logger->logInfo("Added " . $post_comments_added . " comment(s) for post " . $post_id, __METHOD__ . ',' . __LINE__);
                 }
                 $total_added_comments = $total_added_comments + $post_comments_added;
             }
             //Inserting comments also increments the original post's reply_count_cache; reset it here
             $post_dao->updateReplyCount($post_id, $network, $comments_count);
             //process "likes"
             if ($must_process_likes) {
                 if (isset($p->likes)) {
                     $likes_captured = 0;
                     if (isset($p->likes->data)) {
                         $post_likes = $p->likes->data;
                         $post_likes_count = isset($post_likes) ? sizeof($post_likes) : 0;
                         if (is_array($post_likes) && sizeof($post_likes) > 0) {
                             foreach ($post_likes as $l) {
                                 if (isset($l->name) && isset($l->id)) {
                                     //Get users
                                     $user_to_add = array("user_name" => $l->name, "full_name" => $l->name, "user_id" => $l->id, "avatar" => 'https://graph.facebook.com/' . $l->id . '/picture', "location" => '', "description" => '', "url" => '', "is_protected" => 1, "follower_count" => 0, "post_count" => 0, "joined" => '', "found_in" => "Likes", "network" => 'facebook');
                                     //Users are always set to network=facebook
                                     array_push($thinkup_users, $user_to_add);
                                     $fav_to_add = array("favoriter_id" => $l->id, "network" => $network, "author_user_id" => $profile->user_id, "post_id" => $post_id);
                                     array_push($thinkup_likes, $fav_to_add);
                                     $likes_captured = $likes_captured + 1;
                                 }
                             }
                         }
                     }
                     $total_added_users = $total_added_users + $this->storeUsers($thinkup_users, "Likes");
                     $post_likes_added = $post_likes_added + $this->storeLikes($thinkup_likes);
                     //free up memory
                     $thinkup_users = array();
                     $thinkup_likes = array();
                     if (is_int($likes_difference) && $post_likes_added >= $likes_difference) {
                         $must_process_likes = false;
                         if (isset($likes_stream->paging->next)) {
                             $this->logger->logInfo("Caught up on post " . $post_id . "'s balance of " . $likes_difference . " likes; stopping like processing", __METHOD__ . ',' . __LINE__);
                         }
                     }
                     // collapsed likes
                     if (isset($p->likes->count) && $p->likes->count > $likes_captured && $must_process_likes) {
                         if (is_int($likes_difference)) {
                             $offset = $p->likes->count - $likes_difference;
                             $offset_arr = array('offset' => $offset);
                         } else {
                             $offset_arr = null;
                         }
                         $api_call = $p->from->id . '_' . $post_id . '/likes';
                         do {
                             $likes_stream = FacebookGraphAPIAccessor::apiRequest($api_call, $this->access_token, $offset_arr);
                             if (isset($likes_stream) && is_array($likes_stream->data)) {
                                 foreach ($likes_stream->data as $l) {
                                     if (isset($l->name) && isset($l->id)) {
                                         //Get users
                                         $user_to_add = array("user_name" => $l->name, "full_name" => $l->name, "user_id" => $l->id, "avatar" => 'https://graph.facebook.com/' . $l->id . '/picture', "is_protected" => 1, "location" => '', "description" => '', "url" => '', "follower_count" => 0, "post_count" => 0, "joined" => '', "found_in" => "Likes", "network" => 'facebook');
                                         //Users are always set to network=facebook
                                         array_push($thinkup_users, $user_to_add);
                                         $fav_to_add = array("favoriter_id" => $l->id, "network" => $network, "author_user_id" => $p->from->id, "post_id" => $post_id);
                                         array_push($thinkup_likes, $fav_to_add);
                                         $likes_captured = $likes_captured + 1;
                                     }
                                 }
                                 $total_added_users = $total_added_users + $this->storeUsers($thinkup_users, "Likes");
                                 $post_likes_added = $post_likes_added + $this->storeLikes($thinkup_likes);
                                 //free up memory
                                 $thinkup_users = array();
                                 $thinkup_likes = array();
                                 if (is_int($likes_difference) && $post_likes_added >= $likes_difference) {
                                     $must_process_likes = false;
                                     if (isset($likes_stream->paging->next)) {
                                         $this->logger->logInfo("Caught up on post " . $post_id . "'s balance of " . $likes_difference . " likes; stopping like processing", __METHOD__ . ',' . __LINE__);
                                     }
                                 }
                                 if (isset($likes_stream->paging->next)) {
                                     $api_call = str_replace('\\u00257C', '|', $likes_stream->paging->next);
                                 }
                             } else {
                                 // no likes
                                 break;
                             }
                         } while (isset($likes_stream->paging->next) && $must_process_likes);
                     }
                 }
                 $this->logger->logInfo("Added " . $post_likes_added . " like(s) for post " . $post_id, __METHOD__ . ',' . __LINE__);
                 $total_added_likes = $total_added_likes + $post_likes_added;
             }
             //free up memory
             $thinkup_users = array();
             $thinkup_likes = array();
         }
         //reset control vars for next post
         $must_process_likes = true;
         $must_process_comments = true;
         $post_comments_added = 0;
         $post_likes_added = 0;
         $comments_difference = false;
         $likes_difference = false;
     }
     $this->logger->logUserSuccess("On page " . $page_number . ", captured " . $total_added_posts . " post(s), " . $total_added_comments . " comment(s), " . $total_added_users . " user(s) and " . $total_added_likes . " like(s)", __METHOD__ . ',' . __LINE__);
     return $total_added_posts;
 }
 /**
  * Process actions based on $_GET parameters. Authorize FB user or add FB page.
  * @param arr $options Facebook plugin options
  */
 protected function processPageActions($options)
 {
     //authorize user
     if (isset($_GET["code"]) && isset($_GET["state"])) {
         //validate state to avoid CSRF attacks
         if ($_GET["state"] == SessionCache::get('facebook_auth_csrf')) {
             //Prepare API request
             //First, prep redirect URI
             $redirect_uri = Utils::getApplicationURL() . 'account/?p=facebook';
             //Build API request URL
             $api_req = 'oauth/access_token';
             $api_req_params = array('client_id' => $options['facebook_app_id']->option_value, 'client_secret' => $options['facebook_api_secret']->option_value, 'redirect_uri' => $redirect_uri, 'state' => SessionCache::get('facebook_auth_csrf'), 'code' => $_GET["code"]);
             $access_token_response = FacebookGraphAPIAccessor::apiRequest($api_req, null, $api_req_params, null);
             //DEBUG
             // Logger::getInstance()->logInfo("Access token response: "
             //     .Utils::varDumpToString($access_token_response), __METHOD__.','.__LINE__);
             if (isset($access_token_response->error)) {
                 $this->addErrorMessage("There was a problem. Facebook says: " . $access_token_response->error->message . " Please try again.", 'user_add');
                 $logger->logInfo("Added error message ", __METHOD__ . ',' . __LINE__);
                 return;
             }
             $access_token = $access_token_response->access_token;
             if (isset($access_token)) {
                 /**
                  * Swap in short-term token for long-lived token as per
                  * https://developers.facebook.com/docs/facebook-login/access-tokens/#extending
                  */
                 $api_req = 'oauth/access_token';
                 $api_req_params = array('grant_type' => 'fb_exchange_token', 'client_id' => $options['facebook_app_id']->option_value, 'client_secret' => $options['facebook_api_secret']->option_value, 'fb_exchange_token' => $access_token);
                 $access_token_response = FacebookGraphAPIAccessor::apiRequest($api_req, null, $api_req_params);
                 // DEBUG
                 // Logger::getInstance()->logInfo("Exchanged access token response: "
                 //     .Utils::varDumpToString($access_token_response), __METHOD__.','.__LINE__);
                 $access_token = $access_token_response->access_token;
                 $fb_user_profile = FacebookGraphAPIAccessor::apiRequest('me', $access_token, 'name,id');
                 //DEBUG
                 // Logger::getInstance()->logInfo("FB user profile: ".Utils::varDumpToString($fb_user_profile),
                 //     __METHOD__.','.__LINE__);
                 if (isset($fb_user_profile->error)) {
                     $error_msg = "Problem authorizing your Facebook account!";
                     $error_object = $access_token_response;
                     if (isset($error_object) && isset($error_object->error->type) && isset($error_object->error->message)) {
                         $error_msg = $error_msg . "<br>Facebook says: \"" . $error_object->error->type . ": " . $error_object->error->message . "\"";
                     } else {
                         $error_msg = $error_msg . "<br>Facebook's response: \"" . $access_token_response . "\"";
                     }
                     $this->addErrorMessage($error_msg, 'user_add', true);
                 } else {
                     $fb_username = isset($fb_user_profile->name) ? $fb_user_profile->name : '';
                     $fb_user_id = isset($fb_user_profile->id) ? $fb_user_profile->id : '';
                     if (empty($fb_username)) {
                         $error = 'Sorry, ThinkUp does not support business accounts.';
                         $this->addErrorMessage($error, 'user_add');
                     } else {
                         $this->saveAccessToken($fb_user_id, $access_token, $fb_username);
                     }
                 }
             } else {
                 $error_msg = "Problem authorizing your Facebook account! Please correct your plugin settings.";
                 $error_object = $access_token_response;
                 if (isset($error_object) && isset($error_object->error->type) && isset($error_object->error->message)) {
                     $error_msg = $error_msg . "<br>Facebook says: \"" . $error_object->error->type . ": " . $error_object->error->message . "\"";
                 } else {
                     $error_msg = $error_msg . "<br>Facebook's response: \"" . $access_token_response . "\"";
                 }
                 $this->addErrorMessage($error_msg, 'user_add', true);
             }
         } else {
             $this->addErrorMessage("Could not authenticate Facebook account due to invalid CSRF token.", 'user_add');
         }
     }
 }
Ejemplo n.º 5
0
 private function storeFriends()
 {
     if ($this->instance->network != 'facebook') {
         return;
     }
     //Retrieve friends via the Facebook API
     $user_id = $this->instance->network_user_id;
     $access_token = $this->access_token;
     $network = $user_id == $this->instance->network_user_id ? $this->instance->network : 'facebook';
     $friends = FacebookGraphAPIAccessor::apiRequest('/' . $user_id . '/friends', $access_token);
     if (isset($friends->data)) {
         //store relationships in follows table
         $follows_dao = DAOFactory::getDAO('FollowDAO');
         $follower_count_dao = DAOFactory::getDAO('FollowerCountDAO');
         $user_dao = DAOFactory::getDAO('UserDAO');
         foreach ($friends->data as $friend) {
             $follower_id = $friend->id;
             if ($follows_dao->followExists($user_id, $follower_id, $network)) {
                 // follow relationship already exists
                 $follows_dao->update($user_id, $follower_id, $network);
             } else {
                 // follow relationship does not exist yet
                 $follows_dao->insert($user_id, $follower_id, $network);
             }
             //and users in users table.
             $follower_details = FacebookGraphAPIAccessor::apiRequest('/' . $follower_id, $this->access_token);
             $follower_details->network = $network;
             $follower = $this->parseUserDetails($follower_details);
             $follower_object = new User($follower);
             $user_dao->updateUser($follower_object);
         }
         //totals in follower_count table
         $follower_count_dao->insert($user_id, $network, count($friends->data));
     }
 }
 /**
  * Process actions based on $_GET parameters. Authorize FB user or add FB page.
  * @param arr $options Facebook plugin options
  * @param Facebook $facebook Facebook object
  */
 protected function processPageActions($options, Facebook $facebook)
 {
     //authorize user
     if (isset($_GET["code"]) && isset($_GET["state"])) {
         //validate state to avoid CSRF attacks
         if ($_GET["state"] == SessionCache::get('facebook_auth_csrf')) {
             //Prepare API request
             //First, prep redirect URI
             $config = Config::getInstance();
             $site_root_path = $config->getValue('site_root_path');
             $redirect_uri = urlencode(sprintf('%s://%s%s%s', !empty($_SERVER['HTTPS']) ? 'https' : 'http', empty($_SERVER['SERVER_NAME']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'], $site_root_path, 'account/?p=facebook'));
             //Build API request URL
             $api_req = 'https://graph.facebook.com/oauth/access_token?client_id=' . $options['facebook_app_id']->option_value . '&client_secret=' . $options['facebook_api_secret']->option_value . '&redirect_uri=' . $redirect_uri . '&state=' . SessionCache::get('facebook_auth_csrf') . '&code=' . $_GET["code"];
             $access_token_response = FacebookGraphAPIAccessor::rawApiRequest($api_req, false);
             parse_str($access_token_response);
             if (isset($access_token)) {
                 $facebook->setAccessToken($access_token);
                 $fb_user_profile = $facebook->api('/me');
                 $fb_username = $fb_user_profile['name'];
                 $fb_user_id = $fb_user_profile['id'];
                 $this->addSuccessMessage($this->saveAccessToken($fb_user_id, $access_token, $fb_username), 'authorization');
             } else {
                 $error_msg = "Problem authorizing your Facebook account! Please correct your plugin settings.";
                 $error_object = json_decode($access_token_response);
                 if (isset($error_object) && isset($error_object->error->type) && isset($error_object->error->message)) {
                     $error_msg = $error_msg . "<br>Facebook says: \"" . $error_object->error->type . ": " . $error_object->error->message . "\"";
                 } else {
                     $error_msg = $error_msg . "<br>Facebook's response: \"" . $access_token_response . "\"";
                 }
                 $this->addErrorMessage($error_msg, 'authorization');
             }
         } else {
             $this->addErrorMessage("Could not authenticate Facebook account due to invalid CSRF token.", 'authorization');
         }
     }
     //insert pages
     if (isset($_GET["action"]) && $_GET["action"] == "add page" && isset($_GET["facebook_page_id"]) && isset($_GET["viewer_id"]) && isset($_GET["owner_id"]) && isset($_GET["instance_id"])) {
         //get access token
         $oid = DAOFactory::getDAO('OwnerInstanceDAO');
         $tokens = $oid->getOAuthTokens($_GET["instance_id"]);
         $access_token = $tokens['oauth_access_token'];
         $page_data = FacebookGraphAPIAccessor::apiRequest('/' . $_GET["facebook_page_id"], $access_token);
         self::insertPage($page_data->id, $_GET["viewer_id"], $_GET["instance_id"], $page_data->name, $page_data->picture);
     }
 }
Ejemplo n.º 7
0
 /**
  * Convert parsed JSON of a profile or page's posts into ThinkUp posts and users
  * @param Object $stream
  * @param str $source The network for the post; by default 'facebook'
  */
 private function parseStream($stream, $network)
 {
     $thinkup_posts = array();
     $thinkup_users = array();
     $profile = null;
     foreach ($stream->data as $p) {
         $post_id = explode("_", $p->id);
         $post_id = $post_id[1];
         if ($profile == null) {
             $profile = $this->fetchUserInfo($p->from->id, $network, 'Post stream');
         }
         //assume profile comments are private and page posts are public
         $is_protected = $network == 'facebook' ? 1 : 0;
         $ttp = array("post_id" => $post_id, "author_username" => $profile->username, "author_fullname" => $profile->username, "author_avatar" => $profile->avatar, "author_user_id" => $profile->user_id, "post_text" => isset($p->message) ? $p->message : '', "pub_date" => $p->created_time, "in_reply_to_user_id" => '', "in_reply_to_post_id" => '', "source" => '', 'network' => $network, 'is_protected' => $is_protected);
         array_push($thinkup_posts, $ttp);
         if (isset($p->comments)) {
             $comments_captured = 0;
             if (isset($p->comments->data)) {
                 $post_comments = $p->comments->data;
                 $post_comments_count = isset($post_comments) ? sizeof($post_comments) : 0;
                 if (is_array($post_comments) && sizeof($post_comments) > 0) {
                     foreach ($post_comments as $c) {
                         if (isset($c->from)) {
                             $comment_id = explode("_", $c->id);
                             $comment_id = $comment_id[2];
                             //Get posts
                             $ttp = array("post_id" => $comment_id, "author_username" => $c->from->name, "author_fullname" => $c->from->name, "author_avatar" => 'https://graph.facebook.com/' . $c->from->id . '/picture', "author_user_id" => $c->from->id, "post_text" => $c->message, "pub_date" => $c->created_time, "in_reply_to_user_id" => $profile->user_id, "in_reply_to_post_id" => $post_id, "source" => '', 'network' => $network, 'is_protected' => $is_protected);
                             array_push($thinkup_posts, $ttp);
                             //Get users
                             $ttu = array("user_name" => $c->from->name, "full_name" => $c->from->name, "user_id" => $c->from->id, "avatar" => 'https://graph.facebook.com/' . $c->id . '/picture', "location" => '', "description" => '', "url" => '', "is_protected" => 'true', "follower_count" => 0, "post_count" => 0, "joined" => '', "found_in" => "Comments", "network" => 'facebook');
                             //Users are always set to network=facebook
                             array_push($thinkup_users, $ttu);
                             $comments_captured = $comments_captured + 1;
                         }
                     }
                 }
             }
             // collapsed comment thread
             if (isset($p->comments->count) && $p->comments->count > $comments_captured) {
                 $comments_stream = FacebookGraphAPIAccessor::apiRequest('/' . $p->from->id . '_' . $post_id . '/comments', $this->access_token);
                 if (isset($comments_stream) && is_array($comments_stream->data)) {
                     foreach ($comments_stream->data as $c) {
                         if (isset($c->from)) {
                             $comment_id = explode("_", $c->id);
                             $comment_id = $comment_id[2];
                             //Get posts
                             $ttp = array("post_id" => $comment_id, "author_username" => $c->from->name, "author_fullname" => $c->from->name, "author_avatar" => 'https://graph.facebook.com/' . $c->from->id . '/picture', "author_user_id" => $c->from->id, "post_text" => $c->message, "pub_date" => $c->created_time, "in_reply_to_user_id" => $profile->user_id, "in_reply_to_post_id" => $post_id, "source" => '', 'network' => $network, 'is_protected' => $is_protected);
                             array_push($thinkup_posts, $ttp);
                             //Get users
                             $ttu = array("user_name" => $c->from->name, "full_name" => $c->from->name, "user_id" => $c->from->id, "avatar" => 'https://graph.facebook.com/' . $c->id . '/picture', "location" => '', "description" => '', "url" => '', "is_protected" => 'true', "follower_count" => 0, "post_count" => 0, "joined" => '', "found_in" => "Comments", "network" => 'facebook');
                             //Users are always set to network=facebook
                             array_push($thinkup_users, $ttu);
                         }
                     }
                 }
             }
         }
     }
     return array("posts" => $thinkup_posts, "users" => $thinkup_users);
 }
 /**
  * Fetch a save the posts and replies on a Facebook page.
  * @param int $pid Page ID
  */
 public function fetchPagePostsAndReplies($pid)
 {
     $stream = FacebookGraphAPIAccessor::apiRequest('/' . $pid . '/posts', $this->access_token);
     if (isset($stream->data) && is_array($stream->data) && sizeof($stream->data > 0)) {
         $this->logger->logSuccess(sizeof($stream->data) . " Facebook posts found for page ID {$pid}.", __METHOD__ . ',' . __LINE__);
         $thinkup_data = $this->parseStream($stream, 'facebook page');
         $posts = $thinkup_data["posts"];
         $post_dao = DAOFactory::getDAO('PostDAO');
         $added_posts = 0;
         foreach ($posts as $post) {
             if ($post['author_username'] == "" && isset($post['author_user_id'])) {
                 $commenter_object = $this->fetchUserInfo($post['author_user_id'], 'facebook', 'Facebook page comments');
                 if (isset($commenter_object)) {
                     $post["author_username"] = $commenter_object->full_name;
                     $post["author_fullname"] = $commenter_object->full_name;
                     $post["author_avatar"] = $commenter_object->avatar;
                 }
             }
             $added_posts = $added_posts + $post_dao->addPost($post);
             $this->logger->logInfo("Added post ID " . $post["post_id"] . " on " . $post["network"] . " for " . $post["author_username"] . ":" . $post["post_text"], __METHOD__ . ',' . __LINE__);
         }
         $added_users = 0;
         $users = $thinkup_data["users"];
         if (count($users) > 0) {
             foreach ($users as $user) {
                 $user["post_count"] = $post_dao->getTotalPostsByUser($user['user_name'], $user['network']);
                 $found_in = 'Facebook page stream';
                 $user_object = new User($user, $found_in);
                 $user_dao = DAOFactory::getDAO('UserDAO');
                 $user_dao->updateUser($user_object);
                 $added_users = $added_users + 1;
             }
         }
         if ($added_posts > 0 || $added_users > 0) {
             $this->logger->logUserSuccess($added_posts . " post(s) added; " . $added_users . " user(s) updated.", __METHOD__ . ',' . __LINE__);
         } else {
             $this->logger->logUserInfo("No new page posts found.", __METHOD__ . ',' . __LINE__);
         }
     } else {
         $this->logger->logInfo("No Facebook posts found for page ID {$pid}", __METHOD__ . ',' . __LINE__);
     }
 }
Ejemplo n.º 9
0
 /**
  * Fetch a save the posts and replies on a user's profile or page.
  * @param int $id Facebook user or page ID.
  * @param bool $is_page If true then this is a Facebook page, else it's a user profile
  */
 public function fetchPostsAndReplies($id, $is_page)
 {
     $stream = FacebookGraphAPIAccessor::apiRequest('/' . $id . '/posts', $this->access_token);
     if (isset($stream->data) && is_array($stream->data) && sizeof($stream->data > 0)) {
         $this->logger->logInfo(sizeof($stream->data) . " Facebook posts found.", __METHOD__ . ',' . __LINE__);
         $thinkup_data = $this->processStream($stream, $is_page ? 'facebook page' : 'facebook');
     } else {
         $this->logger->logInfo("No Facebook posts found for ID {$id}", __METHOD__ . ',' . __LINE__);
     }
 }