From 7908f463c8a7c36514ac7af1577f51fa0c48eb9e Mon Sep 17 00:00:00 2001 From: mgp25 Date: Thu, 17 Sep 2015 23:52:52 +0200 Subject: [PATCH] No updates, only rewrite --- src/snapchat.php | 207 +++++++++++++++++++++-------------------- src/snapchat_agent.php | 7 +- 2 files changed, 110 insertions(+), 104 deletions(-) diff --git a/src/snapchat.php b/src/snapchat.php index c822ea2..bed47dd 100644 --- a/src/snapchat.php +++ b/src/snapchat.php @@ -36,25 +36,25 @@ class Snapchat extends SnapchatAgent { /** * Snap statuses. */ - const STATUS_NONE = -1; - const STATUS_SENT = 0; - const STATUS_DELIVERED = 1; - const STATUS_OPENED = 2; + const STATUS_NONE = -1; + const STATUS_SENT = 0; + const STATUS_DELIVERED = 1; + const STATUS_OPENED = 2; const STATUS_SCREENSHOT = 3; /** * Friend statuses. */ - const FRIEND_CONFIRMED = 0; - const FRIEND_UNCONFIRMED = 1; - const FRIEND_BLOCKED = 2; - const FRIEND_DELETED = 3; + const FRIEND_CONFIRMED = 0; + const FRIEND_UNCONFIRMED = 1; + const FRIEND_BLOCKED = 2; + const FRIEND_DELETED = 3; /** * Privacy settings. */ - const PRIVACY_EVERYONE = 0; - const PRIVACY_FRIENDS = 1; + const PRIVACY_EVERYONE = 0; + const PRIVACY_FRIENDS = 1; const DATA_FOLDER = 'authData'; @@ -467,7 +467,7 @@ public function login($password, $force = FALSE) return $auth; } parent::setGAuth($auth); - $attestation = $this->getAttestation($password, $timestamp); + $attestation = $this->getAttestation($password, $timestamp); $clientAuthToken = $this->getClientAuthToken($this->username, $password, $timestamp); $result = parent::post( @@ -846,21 +846,21 @@ public function getConversationAuth($to) return $result; } - - public function getConversationInfo($tos){ - if(!is_array($tos)) $tos = array($tos); + public function getConversationInfo($tos) + { + if(!is_array($tos)) $tos = array($tos); $messagesArray = array(); $authArray = array(); - foreach($tos as $to){ - $authInfo = $this->getConversationAuth($to); - //if user is even a friend - if(!property_exists($authInfo["data"], "messaging_auth")) continue; - $authArray[$to] = $authInfo['data']; - $payload = $authInfo["data"]->messaging_auth->payload; - $mac = $authInfo["data"]->messaging_auth->mac; - $genID = md5(uniqid()); - $id = strtoupper(sprintf('%08s-%04s-%04x-%04x-%12s', substr($genID, 0, 8), substr($genID, 8, 4), substr($genID, 12, 4), substr($genID, 16, 4), substr($genID, 20, 12))); - $messagesArray[] = array( + foreach($tos as $to){ + $authInfo = $this->getConversationAuth($to); + //if user is even a friend + if(!property_exists($authInfo["data"], "messaging_auth")) continue; + $authArray[$to] = $authInfo['data']; + $payload = $authInfo["data"]->messaging_auth->payload; + $mac = $authInfo["data"]->messaging_auth->mac; + $genID = md5(uniqid()); + $id = strtoupper(sprintf('%08s-%04s-%04x-%04x-%12s', substr($genID, 0, 8), substr($genID, 8, 4), substr($genID, 12, 4), substr($genID, 16, 4), substr($genID, 20, 12))); + $messagesArray[] = array( "presences" => array( $this->username => true, $to => false @@ -907,83 +907,84 @@ public function getConversationInfo($tos){ "type" => "presence" ); } - $messages = json_encode($messagesArray); - $timestamp = parent::timestamp(); - $result = parent::post( - '/loq/conversation_post_messages', - array( - 'auth_token' => $this->auth_token, - 'messages' => $messages, - 'timestamp' => $timestamp, - 'username' => $this->username, - ), - array( - $this->auth_token, - $timestamp, - ), - $multipart = false, - $debug = $this->debug - ); - $resultsf = array(); - foreach($result['data']->conversations as $convo){ - $split = explode("~", $convo->id); - $un = (strtolower($split[0]) != strtolower($this->username)) ? $split[0] : $split[1]; - $resultsf[$un] = $convo; - } + $messages = json_encode($messagesArray); + $timestamp = parent::timestamp(); + $result = parent::post( + '/loq/conversation_post_messages', + array( + 'auth_token' => $this->auth_token, + 'messages' => $messages, + 'timestamp' => $timestamp, + 'username' => $this->username, + ), + array( + $this->auth_token, + $timestamp, + ), + $multipart = false, + $debug = $this->debug + ); + $resultsf = array(); + foreach($result['data']->conversations as $convo){ + $split = explode("~", $convo->id); + $un = (strtolower($split[0]) != strtolower($this->username)) ? $split[0] : $split[1]; + $resultsf[$un] = $convo; + } return array($resultsf,$authArray); } - public function sendMessage($tos, $text){ - if(!is_array($tos)) $tos = array($tos); - $convoInfo = $this->getConversationInfo($tos); - $messagesArray = array(); - foreach($tos as $to){ - if(!array_key_exists($to, $convoInfo[1])){ //check if user can be sent a message - echo "\nYou have to add {$to} as a friend first!"; - continue; - } - if(!array_key_exists($to, $convoInfo[0])){ //new convo - $payload = $convoInfo[1][$to]->messaging_auth->payload; - $mac = $convoInfo[1][$to]->messaging_auth->mac; - $seq_num = 0; - $conv_id = implode('~', array($to, $this->username)); - }else{ //conversation already exists - $payload = $convoInfo[0][$to]->conversation_messages->messaging_auth->payload; - $mac = $convoInfo[0][$to]->conversation_messages->messaging_auth->mac; - $name = $this->username; - $seq_num = $convoInfo[0][$to]->conversation_state->user_sequences->$name; - $conv_id = $convoInfo[0][$to]->id; - } - $genID = md5(uniqid()); - $chatID = strtoupper(sprintf('%08s-%04s-%04x-%04x-%12s', substr($genID, 0, 8), substr($genID, 8, 4), substr($genID, 12, 4), substr($genID, 16, 4), substr($genID, 20, 12))); - $genID = md5(uniqid()); - $id = strtoupper(sprintf('%08s-%04s-%04x-%04x-%12s', substr($genID, 0, 8), substr($genID, 8, 4), substr($genID, 12, 4), substr($genID, 16, 4), substr($genID, 20, 12))); - $timestamp = parent::timestamp(); - $messagesArray[] = - array( - 'body' => array( - 'text' => $text, - 'type' => 'text' - ), - 'chat_message_id' => $chatID, - 'seq_num' => $seq_num + 1, - 'timestamp' => $timestamp, - 'header' => array( - 'auth' => array( - 'mac' => $mac, - 'payload' => $payload - ), - 'to' => array($to), - 'conv_id' => $conv_id, - 'from' => $this->username, - 'conn_seq_num' => 1 + public function sendMessage($tos, $text) + { + if(!is_array($tos)) $tos = array($tos); + $convoInfo = $this->getConversationInfo($tos); + $messagesArray = array(); + foreach($tos as $to){ + if(!array_key_exists($to, $convoInfo[1])){ //check if user can be sent a message + echo "\nYou have to add {$to} as a friend first!"; + continue; + } + if(!array_key_exists($to, $convoInfo[0])){ //new convo + $payload = $convoInfo[1][$to]->messaging_auth->payload; + $mac = $convoInfo[1][$to]->messaging_auth->mac; + $seq_num = 0; + $conv_id = implode('~', array($to, $this->username)); + }else{ //conversation already exists + $payload = $convoInfo[0][$to]->conversation_messages->messaging_auth->payload; + $mac = $convoInfo[0][$to]->conversation_messages->messaging_auth->mac; + $name = $this->username; + $seq_num = $convoInfo[0][$to]->conversation_state->user_sequences->$name; + $conv_id = $convoInfo[0][$to]->id; + } + $genID = md5(uniqid()); + $chatID = strtoupper(sprintf('%08s-%04s-%04x-%04x-%12s', substr($genID, 0, 8), substr($genID, 8, 4), substr($genID, 12, 4), substr($genID, 16, 4), substr($genID, 20, 12))); + $genID = md5(uniqid()); + $id = strtoupper(sprintf('%08s-%04s-%04x-%04x-%12s', substr($genID, 0, 8), substr($genID, 8, 4), substr($genID, 12, 4), substr($genID, 16, 4), substr($genID, 20, 12))); + $timestamp = parent::timestamp(); + $messagesArray[] = + array( + 'body' => array( + 'text' => $text, + 'type' => 'text' + ), + 'chat_message_id' => $chatID, + 'seq_num' => $seq_num + 1, + 'timestamp' => $timestamp, + 'header' => array( + 'auth' => array( + 'mac' => $mac, + 'payload' => $payload ), - 'retried' => false, - 'id' => $id, - 'type' => 'chat_message' - ); - } - if(count($messagesArray) <= 0) return null; + 'to' => array($to), + 'conv_id' => $conv_id, + 'from' => $this->username, + 'conn_seq_num' => 1 + ), + 'retried' => false, + 'id' => $id, + 'type' => 'chat_message' + ); + } + if(count($messagesArray) <= 0) return null; $messages = json_encode($messagesArray); $timestamp = parent::timestamp(); $result = parent::post( @@ -1243,10 +1244,13 @@ function getFriendStories($save = FALSE) * An array of phone numbers. * FORMATTING: array("name" => "number") !! VERY IMPORTANT !! * + * @param string $country + * Country, i.e: US, ES, GE... + * * @return mixed * An array of user objects or FALSE on failure. */ - public function findFriends($numbers) + public function findFriends($numbers, $country) { $updates = $this->getUpdates(); @@ -1259,8 +1263,6 @@ public function findFriends($numbers) if (!$itsVerified) { $batches = array_chunk(array_flip($numbers), 30, TRUE); - $country = $updates['data']->updates_response->country_code; - // Make sure we're logged in and have a valid access token. if(!$this->auth_token || !$this->username) { @@ -1295,7 +1297,7 @@ public function findFriends($numbers) return $results; } else if ($this->debug) - echo 'DEBUG: You need to verify your phone number'; + echo 'DEBUG: You need to verify your phone number'; } public function searchFriend($friend) @@ -1506,7 +1508,8 @@ public function deleteFriends($usernames) { ), $multipart = false, $debug = $this->debug); - return $result; + + return $result; } /** * Adds multiple friends. diff --git a/src/snapchat_agent.php b/src/snapchat_agent.php index 4b52d86..d238596 100644 --- a/src/snapchat_agent.php +++ b/src/snapchat_agent.php @@ -441,11 +441,14 @@ public function post($endpoint, $data, $params, $multipart = FALSE, $debug = FAL $jsonResult = json_decode($result); echo 'RESULT: ' . print_r($jsonResult) . "\n"; if (property_exists($jsonResult, "status") && $jsonResult->status == '-103') - exit(); + exit(); } else { - echo 'RESULT: ' . $result . "\n"; + if (strpos($result,'400 BAD_REQUEST') !== false) + echo "\nRESULT: 400 BAD REQUEST\n"; + else + echo 'RESULT: ' . $result . "\n"; } if($endpoint == '/loq/register_username' || $endpoint == '/loq/register')