function check_import()
 {
     $this->lf_core->Livefyre_Logger->add("Livefyre: Checking on an import.");
     if ($this->ext->detect_default_comment() && $this->ext->get_option('livefyre_import_status', 'uninitialized') == 'uninitialized') {
         $this->ext->update_option('livefyre_import_status', 'complete');
         $this->ext->delete_option('livefyre_v3_notify_installed');
         return;
     }
     // Make sure we're allowed to import comments
     if (!isset($_GET['livefyre_comment_import']) || !isset($_GET['offset'])) {
         return;
     }
     // Get the decoded sig values from the $_POST object
     $sig = $_POST['sig'];
     $sig_created = urldecode($_POST['sig_created']);
     // Check the signature
     $this->lf_core->Livefyre_Logger->add('comment import req received from livefyre');
     $key = $this->ext->get_option('livefyre_site_key');
     $string = 'import|' . $_GET['offset'] . '|' . $sig_created;
     $this->lf_core->Livefyre_Logger->add(' -comment import req sig inputs: ' . $string . ' input sig:' . $sig);
     if (getHmacsha1Signature(base64_decode($key), $string) != $sig || abs($sig_created - time()) > 259200) {
         $this->lf_core->Livefyre_Logger->add(' -sig failed');
         echo 'sig-failure';
         exit;
     } else {
         $this->lf_core->Livefyre_Logger->add(' -sig correct, rendering');
         $siteId = $this->ext->get_option('livefyre_site_id', '');
         if ($siteId != '') {
             $response = $this->extract_xml($siteId, intval($_GET['offset']));
             echo $response;
             exit;
         } else {
             $this->lf_core->Livefyre_Logger->add(' -tried to render, but no blogid');
             echo 'missing-blog-id';
             exit;
         }
     }
 }
 function check_import()
 {
     if ($this->detect_default_comment() && get_option('livefyre_apps-livefyre_import_status', 'uninitialized') == 'uninitialized') {
         update_option('livefyre_apps-livefyre_import_status', 'complete');
         $this->ext->delete_option('livefyre_v3_notify_installed');
         return;
     }
     // Make sure we're allowed to import comments
     if (!isset($_GET['livefyre_comment_import']) || !isset($_GET['offset'])) {
         return;
     }
     // Get the decoded sig values from the $_POST object
     $sig = $_POST['sig'];
     $sig_created = urldecode($_POST['sig_created']);
     // Check the signature
     $key = get_option('livefyre_apps-livefyre_site_key');
     $string = 'import|' . sanitize_text_field($_GET['offset']) . '|' . $sig_created;
     if (getHmacsha1Signature(base64_decode($key), $string) != $sig || abs($sig_created - time()) > 259200) {
         echo 'sig-failure';
         exit;
     } else {
         $siteId = get_option('livefyre_apps-livefyre_site_id', '');
         if ($siteId != '') {
             $response = $this->extract_xml($siteId, intval(sanitize_text_field($_GET['offset'])));
             echo esc_html($response);
             exit;
         } else {
             echo 'missing-blog-id';
             exit;
         }
     }
 }
 function do_sync()
 {
     $this->lf_core->Livefyre_Logger->add("Livefyre: Running a site sync.");
     /*
         Fetch comments from the livefyre server, providing last activity id we have.
         Schedule the next sync if we got >50 or the server says "more-data".
         If there are no more comments, schedule a sync for several hrs out.
     */
     $result = array('status' => 'ok', 'message' => 'The sync process completed successfully.', 'last-message-type' => null, 'activities-handled' => 0);
     $inserts_remaining = LF_SYNC_MAX_INSERTS;
     $max_activity = $this->ext->get_option('livefyre_activity_id', '0');
     if ($max_activity == '0') {
         $final_path_seg = '';
     } else {
         $final_path_seg = $max_activity . '/';
     }
     $url = $this->site_rest_url() . '/sync/' . $final_path_seg;
     $qstring = 'page_size=' . $inserts_remaining . '&plugin_version=' . LF_PLUGIN_VERSION . '&sig_created=' . time();
     $key = $this->ext->get_option('livefyre_site_key');
     $url .= '?' . $qstring . '&sig=' . urlencode(getHmacsha1Signature(base64_decode($key), $qstring));
     $http_result = $this->lf_core->lf_domain_object->http->request($url, array('timeout' => 120));
     if (is_array($http_result) && isset($http_result['response']) && $http_result['response']['code'] == 200) {
         $str_comments = $http_result['body'];
     } else {
         $str_comments = '';
     }
     $json_array = json_decode($str_comments);
     if (!is_array($json_array)) {
         $this->schedule_sync(LF_SYNC_LONG_TIMEOUT);
         $error_message = 'Error during do_sync: Invalid response ( not a valid json array ) from sync request to url: ' . $url . ' it responded with: ' . $str_comments;
         $this->lf_core->Livefyre_Logger->add("Livefyre: Invalid response ( not a valid json array) from sync request.");
         $this->livefyre_report_error($error_message);
         return array_merge($result, array('status' => 'error', 'message' => $error_message));
     }
     $data = array();
     // What to record for the "latest" id we know about, when done inserting
     $last_activity_id = 0;
     // By default, we don't queue an other near-term sync unless we discover the need to
     $timeout = LF_SYNC_LONG_TIMEOUT;
     $first = true;
     foreach ($json_array as $json) {
         $mtype = $json->message_type;
         if ($mtype == LF_SYNC_ERROR) {
             // An error was encountered, don't schedule next sync for near-term
             $timeout = LF_SYNC_LONG_TIMEOUT;
             break;
         }
         if ($mtype == LF_SYNC_MORE) {
             // There is more data we need to sync, schedule next sync soon
             $timeout = LF_SYNC_SHORT_TIMEOUT;
             break;
         }
         if ($mtype == LF_SYNC_ACTIVITY) {
             $last_activity_id = $json->activity_id;
             $inserts_remaining--;
             $comment_date = (int) $json->created;
             $comment_date = get_date_from_gmt(date('Y-m-d H:i:s', $comment_date));
             $data = array('lf_activity_id' => $json->activity_id, 'lf_action_type' => $json->activity_type, 'comment_post_ID' => $json->article_identifier, 'comment_author' => $json->author, 'comment_author_email' => $json->author_email, 'comment_author_url' => $json->author_url, 'comment_type' => '', 'lf_comment_id' => $json->lf_comment_id, 'user_id' => null, 'comment_author_IP' => $json->author_ip, 'comment_agent' => 'Livefyre, Inc .  Comments Agent', 'comment_date' => $comment_date, 'lf_state' => $json->state);
             if ($first) {
                 $first_id_msg = 'Livefyre: Processing activity page starting with ' . $data['lf_activity_id'];
                 $this->lf_core->Livefyre_Logger->add($first_id_msg);
                 $first = false;
             }
             if (isset($json->lf_parent_comment_id)) {
                 $data['lf_comments_parent'] = $json->lf_parent_comment_id;
             }
             if (isset($json->body_text)) {
                 $data['comment_content'] = $json->body_text;
             }
             $this->livefyre_insert_activity($data);
             if (!$inserts_remaining) {
                 $timeout = LF_SYNC_SHORT_TIMEOUT;
                 break;
             }
         }
     }
     $result['last-message-type'] = $mtype;
     $result['activities-handled'] = LF_SYNC_MAX_INSERTS - $inserts_remaining;
     $result['last-activity-id'] = $last_activity_id;
     if ($last_activity_id) {
         $activity_update = $this->ext->update_option('livefyre_activity_id', $last_activity_id);
         if (!$activity_update) {
             $this->lf_core->Livefyre_Logger->add('Livefyre: Activity ID failed to be rewritten');
         }
         $last_id_msg = 'Livefyre: Set last activity ID processed to ' . $last_activity_id;
         $this->lf_core->Livefyre_Logger->add($last_id_msg);
     }
     $this->schedule_sync($timeout);
     return $result;
 }