Exemplo n.º 1
0
 function push($audience_email, $project_id)
 {
     $st = $this->db->prepare('SELECT * FROM audience LEFT JOIN projects ON (audience.project_id=projects.PROJECT_ID) WHERE audience.audience_email=:audience_email AND projects.project_id=:project_id ');
     $st->bindValue(':audience_email', $audience_email, PDO::PARAM_STR);
     $st->bindValue(':project_id', $project_id, PDO::PARAM_INT);
     $st->execute();
     $data = $st->fetchAll(PDO::FETCH_ASSOC);
     if (!empty($data)) {
         $this->CONDUCTTR_PROJECT_ID = $data[0]['PROJECT_ID'];
         $this->CONDUCTTR_CONSUMER_KEY = $data[0]['CONSUMER_KEY'];
         $this->CONDUCTTR_CONSUMER_SECRET = $data[0]['CONSUMER_SECRET'];
         $this->CONDUCTTR_ACCESS_TOKEN = $data[0]['ACCESS_TOKEN'];
         $this->CONDUCTTR_ACCESS_TOKEN_SECRET = $data[0]['ACCESS_TOKEN_SECRET'];
         $this->REGISTRATION_REQUIRED = $data[0]['REGISTRATION_REQUIRED'];
         $this->BADGES_GROUP_ID = $data[0]['BADGES_GROUP_ID'];
         $this->ROLES_GROUP_ID = $data[0]['ROLES_GROUP_ID'];
         $this->DELAY = $data[0]['DELAY'];
         $this->audience_id = $data[0]['id'];
         $this->audience_email = $data[0]['audience_email'];
         $this->audience_phone = $data[0]['audience_phone'];
         $this->audience_first_name = $data[0]['audience_first_name'];
         $this->audience_last_name = $data[0]['audience_last_name'];
         $this->roles = $data[0]['roles'];
         $this->profile_image = $data[0]['profile_image'];
     }
     /* NEW CODE - MESSAGE FEED Based */
     if ($this->INVENTORY == false) {
         $CONDUCTTR_REQUEST_URL = "https://api.conducttr.com/v1/project/" . $this->CONDUCTTR_PROJECT_ID . "/get_communicator";
         $params = array("audience_email" => $this->audience_email);
         $options = array('consumer_key' => $this->CONDUCTTR_CONSUMER_KEY, 'consumer_secret' => $this->CONDUCTTR_CONSUMER_SECRET);
         OAuthStore::instance("2Leg", $options);
         $req = new OAuthRequestSigner($CONDUCTTR_REQUEST_URL, "GET", $params);
         $secrets = array('signature_methods' => array('HMAC-SHA1'), 'token' => $this->CONDUCTTR_ACCESS_TOKEN, 'token_secret' => $this->CONDUCTTR_ACCESS_TOKEN_SECRET, 'nonce' => $this->makeNonce(), 'timestamp' => time(), 'consumer_key' => $this->CONDUCTTR_CONSUMER_KEY, 'consumer_secret' => $this->CONDUCTTR_CONSUMER_SECRET);
         $req->sign(0, $secrets);
         $signed_url = sprintf('%s?%s', $CONDUCTTR_REQUEST_URL, $req->getQueryString(false));
         $options = array(CURLOPT_HEADER => false, CURLOPT_URL => $signed_url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false);
         $curl = curl_init();
         curl_setopt_array($curl, $options);
         $response = curl_exec($curl);
         if (!$response) {
             $response = curl_error($curl);
         }
         curl_close($curl);
         $results = json_decode($response);
         /* UNLOCKED ITEMS */
         $st = $this->db->prepare('UPDATE messages SET unlocked=:unlocked WHERE audience_id=:audience_id');
         $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
         $st->bindValue(':unlocked', false, PDO::PARAM_INT);
         $st->execute();
         for ($i = 0; $i < sizeof($results->results); $i++) {
             $total_count = 0;
             $total_question_count = 0;
             foreach ($results->results[$i] as $key => $value) {
                 for ($j = 0; $j < sizeof($value); $j++) {
                     $id = $value[$j][1];
                     $message_feed_id = $value[$j][16];
                     $name = $value[$j][5];
                     $body = $value[$j][3];
                     $type = $value[$j][17];
                     $character_name = $value[$j][15];
                     $count = 0;
                     $is_question = false;
                     /* Debug */
                     echo "<b>MESSAGE ID: </b>" . $id;
                     echo "<br>";
                     echo "<b>MESSAGE FEED ID: </b>" . $message_feed_id;
                     echo "<br>";
                     echo "<b>MESSAGE NAME: </b>" . $name;
                     echo "<br>";
                     echo "<b>Body: </b>" . $body;
                     echo "<br>";
                     echo "<b>Type: </b>" . $type;
                     echo "<br>";
                     echo "<b>Character name: </b>" . $character_name;
                     echo "<br><br>";
                     /* Debug */
                     if ($type != "Mail" && $type != "Blog") {
                         $parsed_body = preg_replace('/\\<\\/div\\>/', "</div>\n", $body);
                         $parsed_body = preg_replace("/<\\/?([a-z][a-z0-9]*)\\b[^>]*>/", "", $parsed_body);
                         $parsed_body = preg_replace("/\n+/", "\n", $parsed_body);
                         $parsed_body = preg_replace("/\\|name\\|/", $this->audience_first_name, $parsed_body);
                         $parsed_body = preg_replace("/\\|lname\\|/", $this->audience_last_name, $parsed_body);
                         $array = explode("\n", $parsed_body);
                         $count = 0;
                         for ($w = 0; $w < sizeof($array); $w++) {
                             if (!empty($array[$w])) {
                                 if ($array[$w][0] != "d" && $array[$w][0] != "q" && $array[$w][0] != "w" && $array[$w][0] != "g" && $array[$w][0] != "o") {
                                     $count++;
                                 }
                                 if (($array[$w][0] == "q" || $array[$w][0] == "w" || $array[$w][0] == "g" || $array[$w][0] == "o") && $array[$w][1] == ".") {
                                     $is_question |= true;
                                 }
                             }
                         }
                     } else {
                         preg_match("/q\\./", $body, $question);
                         if (empty($question)) {
                             $parsed_body = preg_replace("/<div.*?>/", "", $body);
                             $parsed_body = preg_replace("/<\\/div>/", "", $parsed_body);
                         } else {
                             $parsed_body = preg_replace("/<\\/?([a-z][a-z0-9]*)\\b[^>]*>/", "\n", $body);
                         }
                         $parsed_body = preg_replace("/\n+/", "\n", $parsed_body);
                         $parsed_body = preg_replace("/\\|name\\|/", $this->audience_first_name, $parsed_body);
                         $parsed_body = preg_replace("/\\|lname\\|/", $this->audience_last_name, $parsed_body);
                         $array = explode("\\[", $parsed_body);
                         $array = explode("\n", $parsed_body);
                         $count = 0;
                         for ($w = 0; $w < sizeof($array); $w++) {
                             if (!empty($array[$w])) {
                                 if ($array[$w][0] != "d" && $array[$w][0] != "q" && $array[$w][0] != "w" && $array[$w][0] != "g" && $array[$w][0] != "o") {
                                     $count++;
                                 }
                             }
                         }
                         $array = explode("\n", $parsed_body);
                         for ($w = 0; $w < sizeof($array); $w++) {
                             if (!empty($array[$w])) {
                                 if (($array[$w][0] == "q" || $array[$w][0] == "w" || $array[$w][0] == "g" || $array[$w][0] == "0") && $array[$w][1] == ".") {
                                     $is_question |= true;
                                 }
                             }
                         }
                     }
                     //$st = $this->db->prepare('INSERT IGNORE INTO messages (id, audience_id, name, type, body, message_feed_id, character_name, question, count, unlocked) VALUES (:id, :audience_id, :name, :type, :body, :message_feed_id, :character_name, :question, :count, :unlocked)');
                     $st = $this->db->prepare('INSERT INTO messages (id, audience_id, name, type, body, message_feed_id, character_name, question, count, unlocked) VALUES (:id, :audience_id, :name, :type, :body, :message_feed_id, :character_name, :question, :count, :unlocked) ON DUPLICATE KEY UPDATE unlocked=true');
                     $st->bindValue(':id', $id, PDO::PARAM_STR);
                     $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
                     $st->bindValue(':name', $name, PDO::PARAM_STR);
                     $st->bindValue(':message_feed_id', $message_feed_id, PDO::PARAM_INT);
                     $st->bindValue(':character_name', $character_name, PDO::PARAM_INT);
                     $st->bindValue(':body', $parsed_body, PDO::PARAM_STR);
                     $st->bindValue(':type', $type, PDO::PARAM_STR);
                     $st->bindValue(':question', $is_question, PDO::PARAM_BOOL);
                     $st->bindValue(':count', $count, PDO::PARAM_INT);
                     $st->bindValue(':unlocked', true, PDO::PARAM_BOOL);
                     $st->execute();
                     print_r($st->errorInfo());
                     $total_count += $count;
                     if ($is_question) {
                         $total_question_count++;
                     }
                 }
             }
             /*
             $st = $this->db->prepare('INSERT INTO message_feeds (id, audience_id, character_name, type, count, question_count) VALUES (:id, :audience_id, :character_name, :type, :count, :question_count) ON DUPLICATE KEY UPDATE count=:count,question_count=:question_count ');	
             $st->bindValue(':id', $message_feed_id, PDO::PARAM_INT);
             $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
             $st->bindValue(':character_name',$character_name, PDO::PARAM_STR);
             $st->bindValue(':type',$type, PDO::PARAM_STR);
             $st->bindValue(':count',$total_count, PDO::PARAM_INT);
             $st->bindValue(':question_count',$total_question_count, PDO::PARAM_INT);
             $st->execute();
             */
         }
     } else {
         $CONDUCTTR_REQUEST_URL = "https://api.conducttr.com/v1/project/" . $this->CONDUCTTR_PROJECT_ID . "/get_audience_inventory";
         $params = array("audience_email" => $this->audience_email);
         $options = array('consumer_key' => $this->CONDUCTTR_CONSUMER_KEY, 'consumer_secret' => $this->CONDUCTTR_CONSUMER_SECRET);
         OAuthStore::instance("2Leg", $options);
         $req = new OAuthRequestSigner($CONDUCTTR_REQUEST_URL, "GET", $params);
         $secrets = array('signature_methods' => array('HMAC-SHA1'), 'token' => $this->CONDUCTTR_ACCESS_TOKEN, 'token_secret' => $this->CONDUCTTR_ACCESS_TOKEN_SECRET, 'nonce' => $this->makeNonce(), 'timestamp' => time(), 'consumer_key' => $this->CONDUCTTR_CONSUMER_KEY, 'consumer_secret' => $this->CONDUCTTR_CONSUMER_SECRET);
         $req->sign(0, $secrets);
         $signed_url = sprintf('%s?%s', $CONDUCTTR_REQUEST_URL, $req->getQueryString(false));
         $options = array(CURLOPT_HEADER => false, CURLOPT_URL => $signed_url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false);
         $curl = curl_init();
         curl_setopt_array($curl, $options);
         $response = curl_exec($curl);
         if (!$response) {
             $response = curl_error($curl);
         }
         curl_close($curl);
         $results = json_decode($response);
         /* UNLOCKED ITEMS */
         $st = $this->db->prepare('UPDATE inventory_items SET unlocked=:unlocked WHERE audience_id=:audience_id');
         $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
         $st->bindValue(':unlocked', false, PDO::PARAM_INT);
         $st->execute();
         for ($i = 0; $i < sizeof($results->results); $i++) {
             $inventory_count = 0;
             $type = $results->results[$i]->inventory_name;
             preg_match("/\\{(.*?)\\}/", $results->results[$i]->inventory_name, $matches);
             $inventory_name = preg_replace("/\\{(.*?)\\}/", "", $type);
             if ($matches[0] == "{whatsup}" || $matches[0] == "{msngr}") {
                 $type = "Msngr";
             } else {
                 if ($matches[0] == "{cmail}" || $matches[0] == "{mail}") {
                     $type = "Mail";
                 } else {
                     if ($matches[0] == "{fakebook}" || $matches[0] == "{gosocial}") {
                         $type = "GoSocial";
                     } else {
                         if ($matches[0] == "{tuiter}" || ($matches[0] == "{tuitter}" || $matches[0] == "{microblog}")) {
                             $type = "Microblog";
                         } else {
                             if ($matches[0] == "{media}" || $matches[0] == "{file}") {
                                 $type = "Media";
                             } else {
                                 if ($matches[0] == "{blog}") {
                                     $type = "Blog";
                                 }
                             }
                         }
                     }
                 }
             }
             $st = $this->db->prepare('INSERT INTO inventory_attributes (audience_id, inventory_name, type ) VALUES (:audience_id, :inventory_name, :type) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)');
             $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
             $st->bindValue(':inventory_name', $inventory_name, PDO::PARAM_STR);
             $st->bindValue(':type', $type, PDO::PARAM_STR);
             $st->execute();
             $inventory_id = $this->db->lastInsertId();
             for ($j = 0; $j < sizeof($results->results[$i]->items); $j++) {
                 $count++;
                 if ($matches[0] == "{whatsup}" || $matches[0] == "{msngr}") {
                     $type = "msngr";
                 } else {
                     if ($matches[0] == "{cmail}" || $matches[0] == "{mail}") {
                         $type = "mail";
                     } else {
                         if ($matches[0] == "{fakebook}" || $matches[0] == "{gosocial}") {
                             $type = "gosocial";
                         } else {
                             if ($matches[0] == "{tuiter}" || ($matches[0] == "{tuitter}" || $matches[0] == "{microblog}")) {
                                 $type = "microblog";
                             } else {
                                 if ($matches[0] == "{media}" || $matches[0] == "{file}") {
                                     $type = "media";
                                 } else {
                                     if ($matches[0] == "{blog}") {
                                         $type = "blog";
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $is_question = false;
                 if ($type != "mail" && $type != "blog") {
                     //print_r($results->results[$i]->items[$j]->body);
                     $messages_array = preg_replace("/<\\/?([a-z][a-z0-9]*)\\b[^>]*>/", "\n", $results->results[$i]->items[$j]->body);
                     $messages_array = preg_replace("/\n+/", "\n", $messages_array);
                     $messages_array = preg_replace("/\\|name\\|/", $this->audience_first_name, $messages_array);
                     $messages_array = preg_replace("/\\|lname\\|/", $this->audience_last_name, $messages_array);
                     $array = explode("\n", $messages_array);
                     $count = 0;
                     for ($w = 0; $w < sizeof($array); $w++) {
                         if (!empty($array[$w])) {
                             if ($array[$w][0] != "d" && $array[$w][0] != "q" && $array[$w][0] != "w" && $array[$w][0] != "g" && $array[$w][0] != "o") {
                                 $count++;
                             }
                             if (($array[$w][0] == "q" || $array[$w][0] == "w" || $array[$w][0] == "g" || $array[$w][0] == "o") && $array[$w][1] == ".") {
                                 $is_question |= true;
                             }
                         }
                     }
                 } else {
                     preg_match("/q\\./", $results->results[$i]->items[$j]->body, $question);
                     if (empty($question)) {
                         $messages_array = preg_replace("/<div.*?>/", "", $results->results[$i]->items[$j]->body);
                         $messages_array = preg_replace("/<\\/div>/", "", $messages_array);
                     } else {
                         $messages_array = preg_replace("/<\\/?([a-z][a-z0-9]*)\\b[^>]*>/", "\n", $results->results[$i]->items[$j]->body);
                     }
                     $messages_array = preg_replace("/\n+/", "\n", $messages_array);
                     $messages_array = preg_replace("/\\|name\\|/", $this->audience_first_name, $messages_array);
                     $messages_array = preg_replace("/\\|lname\\|/", $this->audience_last_name, $messages_array);
                     $array = explode("\\[", $messages_array);
                     $count = 0;
                     for ($w = 0; $w < sizeof($array); $w++) {
                         if (!empty($array[$w])) {
                             $count++;
                         }
                     }
                     $array = explode("\n", $messages_array);
                     for ($w = 0; $w < sizeof($array); $w++) {
                         if (!empty($array[$w])) {
                             if (($array[$w][0] == "q" || $array[$w][0] == "w" || $array[$w][0] == "g") && $array[$w][1] == ".") {
                                 $is_question |= true;
                             }
                         }
                     }
                 }
                 $item_name = $results->results[$i]->items[$j]->name;
                 $insert = false;
                 $item_roles = array();
                 preg_match_all("/\\/(.*?)\\//", $results->results[$i]->items[$j]->name, $item_roles);
                 if (!empty($item_roles[1])) {
                     for ($r = 0; $r < sizeof($item_roles[1]); $r++) {
                         if (in_array($item_roles[1][$r], unserialize($this->roles))) {
                             $insert = true;
                         }
                     }
                 } else {
                     $insert = true;
                 }
                 //print_r($item_roles[1]);
                 //print_r('<br>');
                 //print_r(unserialize($this->roles));
                 //print_r('<br>');
                 //echo "Insert: ".$insert;
                 if ($insert) {
                     //print_r('<br>Insert '.$results->results[$i]->items[$j]->name.'<br>');
                     $st = $this->db->prepare('INSERT INTO inventory_items (audience_id, inventory_name, item_name, type, body, inventory_id, question, count, unlocked) VALUES (:audience_id, :inventory_name, :item_name,:type,:body,:inventory_id, :question, :count, :unlocked) ON DUPLICATE KEY UPDATE body=:body,question=:question, count=:count, unlocked=:unlocked');
                     $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
                     $st->bindValue(':inventory_name', $inventory_name, PDO::PARAM_STR);
                     $st->bindValue(':inventory_id', $inventory_id, PDO::PARAM_INT);
                     $st->bindValue(':item_name', $item_name, PDO::PARAM_STR);
                     $st->bindValue(':body', $messages_array, PDO::PARAM_STR);
                     $st->bindValue(':type', $type, PDO::PARAM_STR);
                     $st->bindValue(':question', $is_question, PDO::PARAM_BOOL);
                     $st->bindValue(':count', $count, PDO::PARAM_INT);
                     $st->bindValue(':unlocked', true, PDO::PARAM_BOOL);
                     $st->execute();
                     //print_r($st->errorInfo());
                     $inventory_count += $count;
                 }
                 //else print_r('Not inserted '.$results->results[$i]->items[$j]->name.'<br>');
             }
             $item_name = $results->results[$i]->items[$j]->name;
             $st = $this->db->prepare('UPDATE inventory_attributes SET count=:count WHERE id=:inventory_id AND audience_id=:audience_id');
             $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
             $st->bindValue(':inventory_id', $inventory_id, PDO::PARAM_INT);
             $st->bindValue(':count', $inventory_count, PDO::PARAM_INT);
             $st->execute();
             $inventory_id = $this->db->lastInsertId();
         }
         /* RESET ITEMS */
         $st = $this->db->prepare('UPDATE inventory_items SET already_read=:already_read WHERE audience_id=:audience_id AND unlocked=false');
         $st->bindValue(':audience_id', $this->audience_id, PDO::PARAM_INT);
         $st->bindValue(':already_read', false, PDO::PARAM_INT);
         $st->execute();
     }
     return array("Response" => "OK");
 }