public function confirm_token()
 {
     // we quickly confirm our envato token by loading the my accounts page
     // we pull in the users email and picture from the account page and convert it into a shub_user
     // store this in the shub_user_id field so we can use it when rendering the UI for comments etc..
     $api = $this->get_api();
     $account_data = $api->get_token_account();
     if ($account_data && !empty($account_data['username'])) {
         // success
         $comment_user = new SupportHubUser_Envato();
         $res = $comment_user->load_by('user_username', $account_data['username']);
         if (!$res) {
             $comment_user->create_new();
             if (!$comment_user->get('user_username')) {
                 $comment_user->update('user_username', $account_data['username']);
             }
             if (!empty($account_data['image'])) {
                 $comment_user->update_user_data(array('image' => $account_data['image']));
             }
         }
         $shub_user_id = $comment_user->get('shub_user_id');
         if ($shub_user_id) {
             $this->update('shub_user_id', $shub_user_id);
         }
         return $shub_user_id;
     } else {
         echo "Failed to get account data. Please confirm the envato token is correct as per the help documentation. Without this you will be unable to post comment replies.";
         return false;
     }
 }
 private function _update_comments($data, $existing_messages)
 {
     if (is_array($data)) {
         $last_message_user_id = false;
         foreach ($data as $message) {
             if ($message['id']) {
                 // does this id exist in the db already?
                 $exists = shub_get_single('shub_message_comment', array('network_key', 'shub_message_id'), array($message['id'], $this->shub_message_id));
                 // create/update a user entry for this comments.
                 $shub_user_id = 0;
                 if (!empty($message['shub_user_id'])) {
                     $shub_user_id = $message['shub_user_id'];
                 } else {
                     if (!empty($message['username'])) {
                         $comment_user = new SupportHubUser_Envato();
                         $res = $comment_user->load_by('user_username', $message['username']);
                         if (!$res) {
                             $comment_user->create_new();
                             if (!$comment_user->get('user_username')) {
                                 $comment_user->update('user_username', $message['username']);
                             }
                             $comment_user->update_user_data(array('image' => $message['profile_image_url'], 'envato' => $message));
                         }
                         $shub_user_id = $comment_user->get('shub_user_id');
                     }
                 }
                 $shub_message_comment_id = shub_update_insert('shub_message_comment_id', $exists ? $exists['shub_message_comment_id'] : false, 'shub_message_comment', array('shub_message_id' => $this->shub_message_id, 'network_key' => $message['id'], 'time' => isset($message['created_at']) ? strtotime($message['created_at']) : (isset($message['timestamp']) ? $message['timestamp'] : 0), 'data' => json_encode($message), 'message_from' => isset($message['username']) ? json_encode(array("username" => $message['username'], "profile_image_url" => $message['profile_image_url'])) : '', 'message_to' => '', 'message_text' => isset($message['content']) ? $message['content'] : '', 'shub_user_id' => $shub_user_id));
                 $last_message_user_id = $shub_user_id;
                 if (isset($existing_messages[$shub_message_comment_id])) {
                     unset($existing_messages[$shub_message_comment_id]);
                 }
                 /*if(isset($message['comments']) && is_array($message['comments'])){
                       $existing_messages = $this->_update_messages($message['comments'], $existing_messages);
                   }*/
             }
         }
         if ($last_message_user_id) {
             if ($last_message_user_id == $this->account->get('shub_user_id')) {
                 // the last comment on this item was from the account owner.
                 // mark this item as resolves so it doesn;t show up in the inbox.
                 $this->update('shub_status', _shub_MESSAGE_STATUS_ANSWERED);
             }
         }
     }
     return $existing_messages;
 }
 public function extra_save_data($extra, $value, $network, $account_id, $message_id)
 {
     $shub_message = new shub_message(false, false, $message_id);
     $shub_user_id = !empty($_SESSION['shub_oauth_envato']['shub_user_id']) ? $_SESSION['shub_oauth_envato']['shub_user_id'] : $shub_message->get('shub_user_id');
     if (is_array($value) && !empty($value['extra_data']['valid_purchase_code'])) {
         // we're saving a previously validated (Above) purchase code.
         // create a shub user for this purchase and return success along with the purchase data to show
         $comment_user = new SupportHubUser_Envato();
         $res = false;
         if (!empty($value['extra_data']['buyer'])) {
             $res = $comment_user->load_by('user_username', $value['extra_data']['buyer']);
         }
         if (!$res) {
             $comment_user->create_new();
             $comment_user->update('user_username', $value['extra_data']['buyer']);
         }
         $user_data = $comment_user->get('user_data');
         if (!is_array($user_data)) {
             $user_data = array();
         }
         if (!isset($user_data['envato_codes'])) {
             $user_data['envato_codes'] = array();
         }
         $user_data_codes = array();
         $user_data_codes[$value['data']] = $value['extra_data'];
         $user_data['envato_codes'] = array_merge($user_data['envato_codes'], $user_data_codes);
         $comment_user->update_user_data($user_data);
         $shub_user_id = $comment_user->get('shub_user_id');
     }
     $extra->save_and_link(array('extra_value' => is_array($value) && !empty($value['data']) ? $value['data'] : $value, 'extra_data' => is_array($value) && !empty($value['extra_data']) ? $value['extra_data'] : false), $network, $account_id, $message_id, $shub_user_id);
 }
 public function load_by_network_key($network_key, $message_data, $type, $debug = false)
 {
     switch ($type) {
         case 'item_comment':
             $existing = shub_get_single('shub_message', 'network_key', $network_key);
             if ($existing) {
                 // load it up.
                 $this->load($existing['shub_message_id']);
             }
             if ($message_data && isset($message_data['id']) && $message_data['id'] == $network_key) {
                 if (!$existing) {
                     $this->create_new();
                 }
                 $this->update('shub_account_id', $this->account->get('shub_account_id'));
                 $this->update('shub_item_id', $this->item->get('shub_item_id'));
                 $comments = $message_data['conversation'];
                 $this->update('title', $comments[0]['content']);
                 $this->update('summary', $comments[count($comments) - 1]['content']);
                 $this->update('last_active', strtotime($message_data['last_comment_at']));
                 $this->update('shub_type', $type);
                 $this->update('shub_data', $message_data);
                 $this->update('shub_link', $message_data['url'] . '/' . $message_data['id']);
                 $this->update('network_key', $network_key);
                 SupportHub::getInstance()->log_data(_SUPPORT_HUB_LOG_INFO, 'envato', 'Updating message ' . $network_key . ' from API.', array('Current Comment Count: ' => count($this->get_comments()), 'New Comment Count: ' => count($comments), 'Current Status: ' => $this->get('shub_status')));
                 if ($this->get('shub_status') != _shub_MESSAGE_STATUS_HIDDEN) {
                     // we have to decide if we're updating the message status from answered to unanswered.
                     // if this message status is already answered and the existing comment count matches the new comment count then we don't update the status
                     // this is because we insert a placeholder "comment" into the db while the API does the push, and when we read again a few minutes later it overwrites this placeholder comment, so really it's not a new comment coming in just the one we posted through the API that takes a while to come back through.
                     if ($this->get('shub_status') == _shub_MESSAGE_STATUS_ANSWERED && count($comments) == count($this->get_comments())) {
                         // don't do anything
                     } else {
                         // comment count is different
                         $this->update('shub_status', _shub_MESSAGE_STATUS_UNANSWERED);
                     }
                 }
                 $this->update('comments', $comments);
                 // create/update a user entry for this comments.
                 $shub_user_id = 0;
                 $first_comment = current($comments);
                 if (!empty($first_comment['username'])) {
                     $comment_user = new SupportHubUser_Envato();
                     $res = $comment_user->load_by('envato_username', $first_comment['username']);
                     if (!$res) {
                         $res = $comment_user->load_by('user_username', $first_comment['username']);
                         if (!$res) {
                             $comment_user->create_new();
                         }
                     }
                     if (!$comment_user->get('user_username')) {
                         $comment_user->update('user_username', $first_comment['username']);
                     }
                     if (!$comment_user->get('envato_username')) {
                         $comment_user->update('envato_username', $first_comment['username']);
                     }
                     $comment_user->update_user_data(array('image' => $first_comment['profile_image_url'], 'envato' => $first_comment));
                     $shub_user_id = $comment_user->get('shub_user_id');
                 }
                 $this->update('shub_user_id', $shub_user_id);
                 return $existing;
             }
             break;
     }
 }