/**
  * 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.º 2
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
  */
 private function processStream($stream, $network, $page_number)
 {
     $thinkup_posts = array();
     $total_added_posts = 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;
         if (isset($p->likes)) {
             if (is_int($p->likes)) {
                 $likes_count = $p->likes;
             } elseif (isset($p->likes->count) && is_int($p->likes->count)) {
                 $likes_count = $p->likes->count;
             }
         }
         $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->count)) {
                 if ($post_in_storage->reply_count_cache >= $p->comments->count) {
                     $must_process_comments = false;
                     $this->logger->logInfo("Already have " . $p->comments->count . " comment(s) for post " . $post_id . "; skipping comment processing", __METHOD__ . ',' . __LINE__);
                 } else {
                     $comments_difference = $p->comments->count - $post_in_storage->reply_count_cache;
                     $this->logger->logInfo($comments_difference . " new comment(s) of " . $p->comments->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)) {
                 $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, "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' => $profile->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)) {
                                     $comment_id = explode("_", $c->id);
                                     $comment_id = $comment_id[2];
                                     //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);
                                         $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->count) && $p->comments->count > $comments_captured && $must_process_comments) {
                         if (is_int($comments_difference)) {
                             $offset = $p->comments->count - $comments_difference;
                             $offset_str = "&offset=" . $offset . "&limit=" . $comments_difference;
                         } else {
                             $offset_str = "";
                         }
                         $api_call = 'https://graph.facebook.com/' . $p->from->id . '_' . $post_id . '/comments?access_token=' . $this->access_token . $offset_str;
                         //$this->logger->logInfo("API call ".$api_call, __METHOD__.','.__LINE__);
                         do {
                             $comments_stream = FacebookGraphAPIAccessor::rawApiRequest($api_call);
                             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_posts = $total_added_posts + $post_comments_added;
             }
             //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_str = "&offset=" . $offset;
                         } else {
                             $offset_str = "";
                         }
                         $api_call = 'https://graph.facebook.com/' . $p->from->id . '_' . $post_id . '/likes?access_token=' . $this->access_token . $offset_str;
                         do {
                             $likes_stream = FacebookGraphAPIAccessor::rawApiRequest($api_call);
                             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', "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" => $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_users . " user(s) and " . $total_added_likes . " like(s)", __METHOD__ . ',' . __LINE__);
 }
Ejemplo n.º 3
0
 /**
  * Take a list of likes from a page or a post, run through pagination and add a count member to the object.
  * @param  object $likes Likes Object structure from Facebook API
  * @return object
  */
 private function normalizeLikes($likes)
 {
     $output = (object) array('count' => 0, 'data' => array());
     // Just in case we get an object with the legacy layout
     if (!isset($likes->data)) {
         if (is_int($likes)) {
             $output->count = $likes;
         } elseif (is_object($likes) && isset($likes->count) && is_int($likes->count)) {
             $output->count = $likes->count;
         }
         return $output;
     }
     while ($likes !== null) {
         foreach ($likes->data as $like) {
             $output->data[] = $like;
             $output->count++;
         }
         if (!empty($likes->paging->next)) {
             $next_url = $likes->paging->next . '&access_token=' . $this->access_token;
             $likes = FacebookGraphAPIAccessor::rawApiRequest($next_url);
         } else {
             $likes = null;
         }
     }
     return $output;
 }
 /**
  * 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);
     }
 }
 /**
  * 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->from->id . '/picture', "location" => '', "description" => '', "url" => '', "is_protected" => 1, "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) {
                 $api_call = 'https://graph.facebook.com/' . $p->from->id . '_' . $post_id . '/comments?access_token=' . $this->access_token;
                 do {
                     $comments_stream = FacebookGraphAPIAccessor::rawApiRequest($api_call);
                     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[sizeof($comment_id) - 1];
                                 //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->from->id . '/picture', "location" => '', "description" => '', "url" => '', "is_protected" => 1, "follower_count" => 0, "post_count" => 0, "joined" => '', "found_in" => "Comments", "network" => 'facebook');
                                 //Users are always set to network=facebook
                                 array_push($thinkup_users, $ttu);
                             }
                         }
                         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));
             }
         }
     }
     return array("posts" => $thinkup_posts, "users" => $thinkup_users);
 }
Ejemplo n.º 6
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 processStream($stream, $network)
 {
     $thinkup_posts = array();
     $total_added_posts = 0;
     $thinkup_users = array();
     $total_added_users = 0;
     $thinkup_links = array();
     $total_links_added = 0;
     $thinkup_likes = array();
     $total_added_likes = 0;
     $profile = null;
     $post_dao = DAOFactory::getDAO('PostDAO');
     $must_process_likes = true;
     $must_process_comments = true;
     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;
         //get likes count
         $likes_count = 0;
         if (isset($p->likes)) {
             if (is_int($p->likes)) {
                 $likes_count = $p->likes;
             } elseif (isset($p->likes->count) && is_int($p->likes->count)) {
                 $likes_count = $p->likes->count;
             }
         }
         //Figure out if we have to process likes and comments
         $post_in_storage = $post_dao->getPost($post_id, $network);
         if (isset($post_in_storage)) {
             if ($post_in_storage->favlike_count_cache >= $likes_count) {
                 $must_process_likes = false;
                 $this->logger->logInfo("Already have " . $likes_count . " likes for post ID " . $post_id . "; Skipping like processing this crawler run", __METHOD__ . ',' . __LINE__);
             }
             if (isset($p->comments->count)) {
                 if ($post_in_storage->reply_count_cache >= $p->comments->count) {
                     $must_process_comments = false;
                     $this->logger->logInfo("Already have " . $p->comments->count . " comments for post ID " . $post_id . "; Skipping comments processing", __METHOD__ . ',' . __LINE__);
                 }
             }
         }
         if (isset($profile) && !isset($post_in_storage)) {
             $posts_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, "in_reply_to_user_id" => '', "in_reply_to_post_id" => '', "source" => '', 'network' => $network, 'is_protected' => $is_protected, 'location' => $profile->location);
             array_push($thinkup_posts, $posts_to_process);
             $total_added_posts = $total_added_posts + $this->storePostsAndAuthors($thinkup_posts, "Owner stream");
             //free up memory
             $thinkup_posts = array();
             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" => $link_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 : '', "network" => $network, "post_id" => $post_id));
                 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();
         }
         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)) {
                                 $comment_id = explode("_", $c->id);
                                 $comment_id = $comment_id[2];
                                 //Get posts
                                 $posts_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, $posts_to_process);
                                 $comments_captured = $comments_captured + 1;
                             }
                         }
                     }
                 }
                 $total_added_posts = $total_added_posts + $this->storePostsAndAuthors($thinkup_posts, "Post stream comments");
                 //free up memory
                 $thinkup_posts = array();
                 // collapsed comment thread
                 if (isset($p->comments->count) && $p->comments->count > $comments_captured) {
                     $api_call = 'https://graph.facebook.com/' . $p->from->id . '_' . $post_id . '/comments?access_token=' . $this->access_token;
                     do {
                         $comments_stream = FacebookGraphAPIAccessor::rawApiRequest($api_call);
                         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[sizeof($comment_id) - 1];
                                     //Get posts
                                     $posts_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, $posts_to_process);
                                 }
                             }
                             $total_added_posts = $total_added_posts + $this->storePostsAndAuthors($thinkup_posts, "Posts stream comments collapsed");
                             //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));
                 }
             }
         }
         //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
                                 $ttu = 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, $ttu);
                                 $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");
                 $total_added_likes = $total_added_likes + $this->storeLikes($thinkup_likes);
                 //free up memory
                 $thinkup_users = array();
                 $thinkup_likes = array();
                 // collapsed likes
                 if (isset($p->likes->count) && $p->likes->count > $likes_captured) {
                     $api_call = 'https://graph.facebook.com/' . $p->from->id . '_' . $post_id . '/likes?access_token=' . $this->access_token;
                     do {
                         $likes_stream = FacebookGraphAPIAccessor::rawApiRequest($api_call);
                         if (isset($likes_stream) && is_array($likes_stream->data)) {
                             foreach ($likes_stream->data as $l) {
                                 if (isset($l->name) && isset($l->id)) {
                                     //Get users
                                     $ttu = 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, $ttu);
                                     $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");
                             $total_added_likes = $total_added_likes + $this->storeLikes($thinkup_likes);
                             //free up memory
                             $thinkup_users = array();
                             $thinkup_likes = array();
                             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));
                 }
             }
             //free up memory
             $thinkup_users = array();
             $thinkup_likes = array();
         }
     }
     if ($total_added_posts > 0) {
         $this->logger->logUserSuccess("Collected {$total_added_posts} posts", __METHOD__ . ',' . __LINE__);
     } else {
         $this->logger->logUserInfo("No new posts found.", __METHOD__ . ',' . __LINE__);
     }
     if ($total_added_users > 0) {
         $this->logger->logUserSuccess("Collected {$total_added_users} users", __METHOD__ . ',' . __LINE__);
     } else {
         $this->logger->logUserInfo("No new users found.", __METHOD__ . ',' . __LINE__);
     }
     if ($total_added_likes > 0) {
         $this->logger->logUserSuccess("Collected {$total_added_likes} likes", __METHOD__ . ',' . __LINE__);
     } else {
         $this->logger->logUserInfo("No new likes found.", __METHOD__ . ',' . __LINE__);
     }
 }