From 7c16727c5893611fdc246cc8e2a082e5c32e095e Mon Sep 17 00:00:00 2001 From: Jd Daniel Date: Fri, 14 Jun 2013 09:49:45 -0700 Subject: [PATCH 01/14] Dictionary file for PHPStorm --- snaphix.dic | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 snaphix.dic diff --git a/snaphix.dic b/snaphix.dic new file mode 100644 index 0000000..260f115 --- /dev/null +++ b/snaphix.dic @@ -0,0 +1,7 @@ +dechex +multipart +repr +rjindael +snapchat +snaphix +urlencode \ No newline at end of file From c9bedbb65e9b292750a471ae6b38171c2ac24517 Mon Sep 17 00:00:00 2001 From: Jd Daniel Date: Fri, 14 Jun 2013 09:53:44 -0700 Subject: [PATCH 02/14] Dictionary file for PHPStorm, altered directory. --- .phpstorm/spelling/snaphix.dic | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .phpstorm/spelling/snaphix.dic diff --git a/.phpstorm/spelling/snaphix.dic b/.phpstorm/spelling/snaphix.dic new file mode 100644 index 0000000..260f115 --- /dev/null +++ b/.phpstorm/spelling/snaphix.dic @@ -0,0 +1,7 @@ +dechex +multipart +repr +rjindael +snapchat +snaphix +urlencode \ No newline at end of file From 45ffff3ab1cd5e2e58c1cc506103bdd646ead852 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:05:01 -0700 Subject: [PATCH 03/14] Spelling error --- .phpstorm/spelling/snaphax.dic | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .phpstorm/spelling/snaphax.dic diff --git a/.phpstorm/spelling/snaphax.dic b/.phpstorm/spelling/snaphax.dic new file mode 100644 index 0000000..260f115 --- /dev/null +++ b/.phpstorm/spelling/snaphax.dic @@ -0,0 +1,7 @@ +dechex +multipart +repr +rjindael +snapchat +snaphix +urlencode \ No newline at end of file From ee1bb87a1bfc9c63a2fd255853d5792cc49afdeb Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:05:36 -0700 Subject: [PATCH 04/14] Cleanup --- .idea/scopes/scope_settings.xml | 5 + .idea/workspace.xml | 281 ++++++++++++++++++++++++++++++++ 2 files changed, 286 insertions(+) create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..7c7dbb0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,281 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1371228341549 + 1371228341549 + + + 1371228585565 + 1371228585565 + + + 1371228824075 + 1371228824075 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 0d66378e306941c1a85bdbcda2794ec0dbbfda2f Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:07:38 -0700 Subject: [PATCH 05/14] Removed file --- snaphix.dic | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 snaphix.dic diff --git a/snaphix.dic b/snaphix.dic deleted file mode 100644 index 260f115..0000000 --- a/snaphix.dic +++ /dev/null @@ -1,7 +0,0 @@ -dechex -multipart -repr -rjindael -snapchat -snaphix -urlencode \ No newline at end of file From 83256db84fa5c38339313e86a922203e35ef74af Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:19:32 -0700 Subject: [PATCH 06/14] Added items to dictionary. Commented possible bug with Content-type not being compatible with declaration --- .phpstorm/spelling/snaphax.dic | 9 +++++++-- .phpstorm/spelling/snaphix.dic | 7 ------- snaphax.php | 2 +- snaphix.dic | 7 ------- 4 files changed, 8 insertions(+), 17 deletions(-) delete mode 100644 .phpstorm/spelling/snaphix.dic delete mode 100644 snaphix.dic diff --git a/.phpstorm/spelling/snaphax.dic b/.phpstorm/spelling/snaphax.dic index 260f115..e2e4108 100644 --- a/.phpstorm/spelling/snaphax.dic +++ b/.phpstorm/spelling/snaphax.dic @@ -1,7 +1,12 @@ +appspot dechex +feelinsonice +lackner +mcrypt multipart +noninfringement repr -rjindael +rijndael snapchat -snaphix +snaphax urlencode \ No newline at end of file diff --git a/.phpstorm/spelling/snaphix.dic b/.phpstorm/spelling/snaphix.dic deleted file mode 100644 index 260f115..0000000 --- a/.phpstorm/spelling/snaphix.dic +++ /dev/null @@ -1,7 +0,0 @@ -dechex -multipart -repr -rjindael -snapchat -snaphix -urlencode \ No newline at end of file diff --git a/snaphax.php b/snaphax.php index e3f8933..5e12cf4 100644 --- a/snaphax.php +++ b/snaphax.php @@ -134,7 +134,7 @@ function upload($file_data, $type, $recipients, $time=8) { $this->auth_token, $ts, 0, - array("Content-type: multipart/form-data") + array('Content-type: multipart/form-data; boundary=AaB03x') // not compatible with declaration error noted. ); $this->api->debug('upload result', $result); diff --git a/snaphix.dic b/snaphix.dic deleted file mode 100644 index 260f115..0000000 --- a/snaphix.dic +++ /dev/null @@ -1,7 +0,0 @@ -dechex -multipart -repr -rjindael -snapchat -snaphix -urlencode \ No newline at end of file From 3c126ead81e509a46b5d9fa1f3e6da584cbf9e7b Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:25:31 -0700 Subject: [PATCH 07/14] Easier module checking --- snaphax.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/snaphax.php b/snaphax.php index 5e12cf4..2bd3217 100644 --- a/snaphax.php +++ b/snaphax.php @@ -1,4 +1,4 @@ - 'Snaphax 4.0.1 (iPad; iPhone OS 6.0; en_US)' ); - if (!function_exists('curl_init')) { - throw new Exception('Snaphax needs the CURL PHP extension.'); - } - if (!function_exists('json_decode')) { - throw new Exception('Snaphax needs the JSON PHP extension.'); - } - if (!function_exists('mcrypt_decrypt')) { - throw new Exception('Snaphax needs the mcrypt PHP extension.'); - } + $requirements = array( + 'curl_init' => 'CURL', + 'json_decode' => 'JSON', + 'mcrypt_decrypt' => 'MCRYPT', + ); + + foreach ($requirements AS $module => $extension) + { + if (!function_exists($module)) Throw New Exception("Snaphax needs the {$extension} PHP extension."); + } + class Snaphax { // High level class to perform actions on Snapchat From 2d2bd3c6b734d989d3481a9f6883c1e0d71ac3c3 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:27:27 -0700 Subject: [PATCH 08/14] Stubout for Configuration INI, saner way than passing Global Array variables around everywhere. --- configuration/config.ini.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 configuration/config.ini.php diff --git a/configuration/config.ini.php b/configuration/config.ini.php new file mode 100644 index 0000000..e69de29 From 81cc23f8dc2b4c0546d96a8fe17fc038ee5e1694 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:55:27 -0700 Subject: [PATCH 09/14] Added IniParser submodule to remove GLOBAL dependancy --- .gitmodules | 3 ++ .idea/workspace.xml | 83 ++++++++++++++++++++++++++++++++++---------- snaphax.php | 41 +++++++++++++++------- submodules/IniParser | 1 + 4 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 .gitmodules create mode 160000 submodules/IniParser diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8442cc7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "submodules/IniParser"] + path = submodules/IniParser + url = git@github.com:ehime/IniParser.git diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 7c7dbb0..5bb3e24 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,13 @@ - + + - + + @@ -120,11 +135,11 @@ - + - + @@ -147,6 +162,7 @@ + - - - - - + - - - + + + + + + + + - @@ -248,7 +276,7 @@ - + + + + + + + + - + + + + + + + + + + diff --git a/snaphax.php b/snaphax.php index 2bd3217..3ae7b98 100644 --- a/snaphax.php +++ b/snaphax.php @@ -45,21 +45,31 @@ if (!function_exists($module)) Throw New Exception("Snaphax needs the {$extension} PHP extension."); } - - class Snaphax { + Class Snaphax + { // High level class to perform actions on Snapchat - const STATUS_NEW = 1; - const MEDIA_IMAGE = 0; - const MEDIA_VIDEO = 1; + const STATUS_NEW = 1; + const MEDIA_IMAGE = 0; + const MEDIA_VIDEO = 1; + + private $_configFile = 'configuration/config.ini.php'; + + public function __construct(Array $options = array()) + { + $this->options = array_merge($this->_getOptionsIni(), $options); + $this->api = New SnaphaxApi($this->options); + $$this->_auth_token = false; + } function Snaphax($options) { global $SNAPHAX_DEFAULT_OPTIONS; $this->options = array_merge($SNAPHAX_DEFAULT_OPTIONS, $options); $this->api = new SnaphaxApi($this->options); - $this->auth_token = false; + $$this->_auth_token = false; } + function login() { $ts = $this->api->time(); $out = $this->api->postCall( @@ -74,12 +84,12 @@ function login() { ); if (is_array($out) && !empty($out['auth_token'])) { - $this->auth_token = $out['auth_token']; + $$this->_auth_token = $out['auth_token']; } return $out; } function fetch($id) { - if (!$this->auth_token) { + if (!$$this->_auth_token) { throw new Exception('no auth token'); } $ts = $this->api->time(); @@ -88,7 +98,7 @@ function fetch($id) { 'id' => $id, 'timestamp' => $ts, 'username' => urlencode($this->options['username']) - ), $this->auth_token, $ts, 0); + ), $$this->_auth_token, $ts, 0); $this->api->debug('blob result', $result); // some blobs are not encrypted @@ -112,7 +122,7 @@ function reqToken($param1, $param2) { function upload($file_data, $type, $recipients, $time=8) { if ($type != self::MEDIA_IMAGE && $type != self::MEDIA_VIDEO) throw new Exception('Snaphax: upload type must be MEDIA_IMAGE or MEDIA_VIDEO'); - if (!$this->auth_token) { + if (!$$this->_auth_token) { throw new Exception('no auth token'); } if (!is_array($recipients)) @@ -133,7 +143,7 @@ function upload($file_data, $type, $recipients, $time=8) { 'data' => '@/tmp/blah.jpg;filename=file', 'media_id' => $media_id ), - $this->auth_token, + $$this->_auth_token, $ts, 0, array('Content-type: multipart/form-data; boundary=AaB03x') // not compatible with declaration error noted. @@ -151,7 +161,7 @@ function upload($file_data, $type, $recipients, $time=8) { 'media_id' => $media_id, 'time' => $time ), - $this->auth_token, + $$this->_auth_token, $ts, 0 ); @@ -160,6 +170,13 @@ function upload($file_data, $type, $recipients, $time=8) { return $media_id; } + + private function getOptionsIni () + { + if (!file_exists($this->_configFile)) Throw New Exception ('missing instantiating INI file, please make sure this exists.'); + + } + } class SnaphaxApi { diff --git a/submodules/IniParser b/submodules/IniParser new file mode 160000 index 0000000..e48dddd --- /dev/null +++ b/submodules/IniParser @@ -0,0 +1 @@ +Subproject commit e48dddd232e96213bc81ca9747db7f3f412a51ad From d80edfd350611dd34221dcd479bb839d779a9a90 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 10:58:25 -0700 Subject: [PATCH 10/14] Disallow INI reading by compiler halting if file is not saved above a doc-root level --- configuration/config.ini.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/configuration/config.ini.php b/configuration/config.ini.php index e69de29..a511248 100644 --- a/configuration/config.ini.php +++ b/configuration/config.ini.php @@ -0,0 +1,4 @@ +; \ No newline at end of file From 3a3b9e31a9445aac53d879ce1b596bb855928d35 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 11:12:22 -0700 Subject: [PATCH 11/14] Bug and language fixes --- .idea/workspace.xml | 209 ++++++++++++++++++++++++++++++--- .phpstorm/spelling/snaphax.dic | 1 + configuration/config.ini.php | 13 ++ snaphax.php | 36 +++--- 4 files changed, 221 insertions(+), 38 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5bb3e24..af2e4c2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,13 +2,17 @@ + + - - + + + + - + - + @@ -39,10 +43,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -67,17 +107,40 @@ + + + + + + + + + + Spelling + + + + + SpellCheckingInspection + + + + + @@ -122,6 +185,92 @@ @@ -224,10 +377,10 @@ - + - + @@ -276,7 +429,7 @@ + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - - - - + + diff --git a/.phpstorm/spelling/snaphax.dic b/.phpstorm/spelling/snaphax.dic index e2e4108..a64c2ba 100644 --- a/.phpstorm/spelling/snaphax.dic +++ b/.phpstorm/spelling/snaphax.dic @@ -6,6 +6,7 @@ mcrypt multipart noninfringement repr +requirements rijndael snapchat snaphax diff --git a/configuration/config.ini.php b/configuration/config.ini.php index a511248..c9f9af2 100644 --- a/configuration/config.ini.php +++ b/configuration/config.ini.php @@ -1,4 +1,17 @@ ; \ No newline at end of file diff --git a/snaphax.php b/snaphax.php index 3ae7b98..31e9984 100644 --- a/snaphax.php +++ b/snaphax.php @@ -23,27 +23,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - - $SNAPHAX_DEFAULT_OPTIONS = array( - 'blob_enc_key' => 'M02cnQ51Ji97vwT4', - 'debug' => false, - 'pattern' => '0001110111101110001111010101111011010001001110011000110001000110', - 'secret' => 'iEk21fuwZApXlz93750dmW22pw389dPwOk', - 'static_token' => 'm198sOkJEn37DjqZ32lpRu76xmw288xSQ9', - 'url' => 'https://feelinsonice.appspot.com', - 'user_agent' => 'Snaphax 4.0.1 (iPad; iPhone OS 6.0; en_US)' - ); - - $requirements = array( - 'curl_init' => 'CURL', - 'json_decode' => 'JSON', - 'mcrypt_decrypt' => 'MCRYPT', - ); - - foreach ($requirements AS $module => $extension) - { - if (!function_exists($module)) Throw New Exception("Snaphax needs the {$extension} PHP extension."); - } Class Snaphax { @@ -57,6 +36,8 @@ public function __construct(Array $options = array()) { + $this->_checkRequirements(); + $this->options = array_merge($this->_getOptionsIni(), $options); $this->api = New SnaphaxApi($this->options); $$this->_auth_token = false; @@ -171,10 +152,23 @@ function upload($file_data, $type, $recipients, $time=8) { return $media_id; } + private function _checkRequirements() + { + foreach ($requirements AS $module => $extension) + { + if (!function_exists($module)) Throw New Exception("Snaphax needs the {$extension} PHP extension."); + } + + } + private function getOptionsIni () { if (!file_exists($this->_configFile)) Throw New Exception ('missing instantiating INI file, please make sure this exists.'); + require_once 'submodules/IniParser/src/IniParser.php'; + + $config = New \IniParser(); + } } From e4c78efb19d44f9a1c59a26a2df037a26d860fae Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 11:34:44 -0700 Subject: [PATCH 12/14] Bug fixes and cleanup. Added bigger dictionary --- .phpstorm/spelling/snaphax.dic | 22 ++ snaphax.php | 587 ++++++++++++++++++--------------- 2 files changed, 338 insertions(+), 271 deletions(-) diff --git a/.phpstorm/spelling/snaphax.dic b/.phpstorm/spelling/snaphax.dic index a64c2ba..502b3eb 100644 --- a/.phpstorm/spelling/snaphax.dic +++ b/.phpstorm/spelling/snaphax.dic @@ -1,13 +1,35 @@ +adamcaudill appspot +austinehyde +autoload +barbaz +caudill dechex +doesnt +ehime +fdata feelinsonice +fname +helloworld +incase +iniparser +jsonp +klampaeckel lackner mcrypt multipart noninfringement +php's +prepending repr requirements rijndael +screenshot +settype snapchat +snapchat'ssnaphax snaphax +submodule +submodules +tlack urlencode \ No newline at end of file diff --git a/snaphax.php b/snaphax.php index 31e9984..da7322a 100644 --- a/snaphax.php +++ b/snaphax.php @@ -1,290 +1,335 @@ - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - */ - - Class Snaphax +/* + SnapHax: a library for communicating with Snaphax + Implements a subset of the Snaphax API + + (c) Copyright 2012 Thomas Lackner + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + + require 'submodules/IniParser/src/IniParser.php'; + + Class Snaphax + { + // High level class to perform actions on Snapchat + + const STATUS_NEW = 1; + const MEDIA_IMAGE = 0; + const MEDIA_VIDEO = 1; + + /** + * @var string + */ + private $_configFile = 'configuration/config.ini.php'; + + + /** + * @param array $options + */ + public function __construct(Array $options = array()) { - // High level class to perform actions on Snapchat + $this->_checkRequirements(); - const STATUS_NEW = 1; - const MEDIA_IMAGE = 0; - const MEDIA_VIDEO = 1; + $this->options = array_merge($this->_getOptionsIni(), $options); + $this->api = New SnaphaxApi($this->options); + $$this->_auth_token = false; + } - private $_configFile = 'configuration/config.ini.php'; - - public function __construct(Array $options = array()) + /** + * @return array|bool|mixed + */ + public function login() + { + $ts = $this->api->time(); + $out = $this->api->postCall( + '/ph/login', + array( + 'username' => $this->options['username'], + 'password' => $this->options['password'], + 'timestamp' => $ts + ), + $this->options['static_token'], + $ts + ); + + if (is_array($out) && !empty($out['auth_token'])) { - $this->_checkRequirements(); + $$this->_auth_token = $out['auth_token']; + } + return $out; + } + + /** + * @param $id + * @return bool|mixed|string + * @throws Exception + */ + public function fetch($id) + { + if (!$$this->_auth_token) + { + Throw New Exception('no auth token'); + } + + $ts = $this->api->time(); + $url = "/ph/blob"; - $this->options = array_merge($this->_getOptionsIni(), $options); - $this->api = New SnaphaxApi($this->options); - $$this->_auth_token = false; + $result = $this->api->postCall($url, array( + 'id' => $id, + 'timestamp' => $ts, + 'username' => urlencode($this->options['username']) + ), $$this->_auth_token, $ts, 0); + + $this->api->debug('blob result', $result); + + // some blobs are not encrypted + if ($this->api->isValidBlobHeader(substr($result, 0, 256))) + { + $this->api->debug('blob not encrypted'); + return $result; } - function Snaphax($options) { - global $SNAPHAX_DEFAULT_OPTIONS; - - $this->options = array_merge($SNAPHAX_DEFAULT_OPTIONS, $options); - $this->api = new SnaphaxApi($this->options); - $$this->_auth_token = false; - } - - function login() { - $ts = $this->api->time(); - $out = $this->api->postCall( - '/ph/login', - array( - 'username' => $this->options['username'], - 'password' => $this->options['password'], - 'timestamp' => $ts - ), - $this->options['static_token'], - $ts - ); - if (is_array($out) && - !empty($out['auth_token'])) { - $$this->_auth_token = $out['auth_token']; - } - return $out; - } - function fetch($id) { - if (!$$this->_auth_token) { - throw new Exception('no auth token'); - } - $ts = $this->api->time(); - $url = "/ph/blob"; - $result = $this->api->postCall($url, array( - 'id' => $id, - 'timestamp' => $ts, - 'username' => urlencode($this->options['username']) - ), $$this->_auth_token, $ts, 0); - $this->api->debug('blob result', $result); - - // some blobs are not encrypted - if ($this->api->isValidBlobHeader(substr($result, 0, 256))) { - $this->api->debug('blob not encrypted'); - return $result; - } - - $result_decoded = $this->api->decrypt($result); - $this->api->debug('decoded snap', $result_decoded); - if ($this->api->isValidBlobHeader(substr($result_decoded, 0, 256))) { - return $result_decoded; - } else { - $this->api->debug('invalid image/video data header'); - return false; - } - } - function reqToken($param1, $param2) { - return $this->api->hash($param1, $param2); - } - function upload($file_data, $type, $recipients, $time=8) { - if ($type != self::MEDIA_IMAGE && $type != self::MEDIA_VIDEO) - throw new Exception('Snaphax: upload type must be MEDIA_IMAGE or MEDIA_VIDEO'); - if (!$$this->_auth_token) { - throw new Exception('no auth token'); - } - if (!is_array($recipients)) - $recipients = array($recipients); - $ts = $this->api->time(); - $media_id = strtoupper($this->options['username']).time(); - $this->api->debug('upload snap data', $file_data); - $file_data_encrypted = $this->api->encrypt($file_data); - $this->api->debug('upload snap data encrypted', $file_data_encrypted); - file_put_contents('/tmp/blah.jpg', $file_data_encrypted); - $result = $this->api->postCall( - '/ph/upload', - array( - 'username' => $this->options['username'], - 'timestamp' => $ts, - 'type' => $type, - // 'data' => urlencode($file_data_encrypted).'; filename="file"', - 'data' => '@/tmp/blah.jpg;filename=file', - 'media_id' => $media_id - ), - $$this->_auth_token, - $ts, - 0, - array('Content-type: multipart/form-data; boundary=AaB03x') // not compatible with declaration error noted. - ); - $this->api->debug('upload result', $result); - - foreach ($recipients as $recipient) { - $ts = $this->api->time(); - $result = $this->api->postCall( - '/ph/send', - array( - 'username' => $this->options['username'], - 'timestamp' => $ts, - 'recipient' => $recipient, - 'media_id' => $media_id, - 'time' => $time - ), - $$this->_auth_token, - $ts, - 0 - ); - $this->api->debug("send to $recipient: " . $result); - } - - return $media_id; - } - - private function _checkRequirements() + $result_decoded = $this->api->decrypt($result); + $this->api->debug('decoded snap', $result_decoded); + + if ($this->api->isValidBlobHeader(substr($result_decoded, 0, 256))) return $result_decoded; + + $this->api->debug('invalid image/video data header'); + return false; + } + + /** + * @param $param1 + * @param $param2 + * @return string + */ + public function reqToken($param1, $param2) + { + return $this->api->hash($param1, $param2); + } + + /** + * @param $file_data + * @param $type + * @param $recipients + * @param int $time + * @return string + * @throws Exception + */ + public function upload($file_data, $type, $recipients, $time=8) + { + if ($type != self::MEDIA_IMAGE && $type != self::MEDIA_VIDEO) Throw New Exception('Snaphax: upload type must be MEDIA_IMAGE or MEDIA_VIDEO'); + + if (!$$this->_auth_token) Throw New Exception('no auth token'); + + if (!is_array($recipients)) { - foreach ($requirements AS $module => $extension) - { - if (!function_exists($module)) Throw New Exception("Snaphax needs the {$extension} PHP extension."); - } + $recipients = array($recipients); + } + $ts = $this->api->time(); + $media_id = strtoupper($this->options['username']).time(); + $this->api->debug('upload snap data', $file_data); + + $file_data_encrypted = $this->api->encrypt($file_data); + $this->api->debug('upload snap data encrypted', $file_data_encrypted); + + file_put_contents('/tmp/blah.jpg', $file_data_encrypted); + + $result = $this->api->postCall( + '/ph/upload', + array( + 'username' => $this->options['username'], + 'timestamp' => $ts, + 'type' => $type, + // 'data' => urlencode($file_data_encrypted).'; filename="file"', + 'data' => '@/tmp/blah.jpg;filename=file', + 'media_id' => $media_id + ), + $$this->_auth_token, + $ts, + 0, + array('Content-type: multipart/form-data; boundary=AaB03x') // not compatible with declaration error noted. + ); + $this->api->debug('upload result', $result); + + foreach ($recipients as $recipient) { + $ts = $this->api->time(); + $result = $this->api->postCall( + '/ph/send', + array( + 'username' => $this->options['username'], + 'timestamp' => $ts, + 'recipient' => $recipient, + 'media_id' => $media_id, + 'time' => $time + ), + $$this->_auth_token, + $ts, + 0 + ); + $this->api->debug("send to $recipient: " . $result); } - private function getOptionsIni () - { - if (!file_exists($this->_configFile)) Throw New Exception ('missing instantiating INI file, please make sure this exists.'); + return $media_id; + } - require_once 'submodules/IniParser/src/IniParser.php'; + /** + * @returns void + * @throws Exception + */ + private function _checkRequirements() + { + $requirements = New \IniParser($this->_configFile); + $requirements->parse('REQUIREMENTS'); - $config = New \IniParser(); + foreach ($requirements->getArray() AS $module => $extension) + { + if (!function_exists($module)) Throw New Exception("Snaphax needs the {$extension} PHP extension."); + } + } + /** + * @return array + * @throws Exception + */ + private function _getOptionsIni () + { + if (!file_exists($this->_configFile)) Throw New Exception ('missing instantiating INI file, please make sure this exists.'); + + $config = New \IniParser($this->_configFile); + $config->parse('SNAPHAX_DEFAULT_OPTIONS'); + return $config->getArray(); + } +} + +class SnaphaxApi { + // Low level code to communicate with Snapchat via HTTP + + function SnaphaxApi($options) { + $this->options = $options; + } + + function debug($text, $binary = false) { + if ($this->options['debug']) { + echo "SNAPHAX DEBUG: $text"; + if ($binary !== false) { + // shortened hex repr of binary + $len = strlen($binary); + $tmp = " hex ($len bytes): "; + $tmp.= join(' ', array_map('dechex', array_map('ord', str_split(substr($binary, 0, 16))))); + $tmp.= ' ... '; + $tmp.= join(' ', array_map('dechex', array_map('ord', str_split(substr($binary, -16))))); + echo $tmp; + } + echo "\n"; + } + } + + function isValidBlobHeader($header) { + if (($header[0] == chr(00) && // mp4 + $header[0] == chr(00)) || + ($header[0] == chr(0xFF) && // jpg + $header[1] == chr(0xD8))) + return true; + else + return false; + } + + function decrypt($data) { + return mcrypt_decrypt('rijndael-128', $this->options['blob_enc_key'], $data, 'ecb'); + } + + function encrypt($data) { + return mcrypt_encrypt('rijndael-128', $this->options['blob_enc_key'], $data, 'ecb'); + } + + public function postCall($endpoint, $post_data, $param1, $param2, $json=1, $headers=false) { + $ch = curl_init(); + + // set the url, number of POST vars, POST data + curl_setopt($ch,CURLOPT_URL, $this->options['url'].$endpoint); + curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch,CURLOPT_USERAGENT, $this->options['user_agent']); + + if ($headers && is_array($headers)) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } - } - - class SnaphaxApi { - // Low level code to communicate with Snapchat via HTTP - - function SnaphaxApi($options) { - $this->options = $options; - } - - function debug($text, $binary = false) { - if ($this->options['debug']) { - echo "SNAPHAX DEBUG: $text"; - if ($binary !== false) { - // shortened hex repr of binary - $len = strlen($binary); - $tmp = " hex ($len bytes): "; - $tmp.= join(' ', array_map('dechex', array_map('ord', str_split(substr($binary, 0, 16))))); - $tmp.= ' ... '; - $tmp.= join(' ', array_map('dechex', array_map('ord', str_split(substr($binary, -16))))); - echo $tmp; - } - echo "\n"; - } - } - - function isValidBlobHeader($header) { - if (($header[0] == chr(00) && // mp4 - $header[0] == chr(00)) || - ($header[0] == chr(0xFF) && // jpg - $header[1] == chr(0xD8))) - return true; - else - return false; - } - - function decrypt($data) { - return mcrypt_decrypt('rijndael-128', $this->options['blob_enc_key'], $data, 'ecb'); - } - - function encrypt($data) { - return mcrypt_encrypt('rijndael-128', $this->options['blob_enc_key'], $data, 'ecb'); - } - - public function postCall($endpoint, $post_data, $param1, $param2, $json=1, $headers=false) { - $ch = curl_init(); - - // set the url, number of POST vars, POST data - curl_setopt($ch,CURLOPT_URL, $this->options['url'].$endpoint); - curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch,CURLOPT_USERAGENT, $this->options['user_agent']); - - if ($headers && is_array($headers)) { - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } - - $post_data['req_token'] = $this->hash($param1, $param2); - curl_setopt($ch, CURLOPT_POST, count($post_data)); - if (!$headers) - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); - else - curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data); - $this->debug('POST params: ' . json_encode($post_data)); - $result = curl_exec($ch); - if ($result === false) { - $this->debug('CURL error: '.curl_error($ch)); - return false; - } - $this->debug('HTTP response code' . curl_getinfo($ch, CURLINFO_HTTP_CODE)); - $this->debug('POST return ' . $result); - - // close connection - curl_close($ch); - - if ($json) - return json_decode(utf8_encode($result), true); - else - return $result; - } - - function hash($param1, $param2) { - $this->debug("p1: $param1"); - $this->debug("p2: $param2"); - - $s1 = $this->options['secret'] . $param1; - $this->debug("s1: $s1"); - $s2 = $param2 . $this->options['secret']; - $this->debug("s2: $s2"); - - $hash = hash_init('sha256'); - hash_update($hash, $s1); - $s3 = hash_final($hash, false); - $this->debug("s3: $s3"); - - $hash = hash_init('sha256'); - hash_update($hash, $s2); - $s4 = hash_final($hash, false); - $this->debug("s4: $s4"); - - $out = ''; - for ($i = 0; $i < strlen($this->options['pattern']); $i++) { - $c = $this->options['pattern'][$i]; - if ($c == '0') - $out .= $s3[$i]; - else - $out .= $s4[$i]; - } - $this->debug("out: $out"); - return $out; - } - - function time() { - return round(microtime(true) * 1000); - } - - } + $post_data['req_token'] = $this->hash($param1, $param2); + curl_setopt($ch, CURLOPT_POST, count($post_data)); + if (!$headers) + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data)); + else + curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data); + $this->debug('POST params: ' . json_encode($post_data)); + $result = curl_exec($ch); + if ($result === false) { + $this->debug('CURL error: '.curl_error($ch)); + return false; + } + $this->debug('HTTP response code' . curl_getinfo($ch, CURLINFO_HTTP_CODE)); + $this->debug('POST return ' . $result); + + // close connection + curl_close($ch); + + if ($json) + return json_decode(utf8_encode($result), true); + else + return $result; + } + + function hash($param1, $param2) { + $this->debug("p1: $param1"); + $this->debug("p2: $param2"); + + $s1 = $this->options['secret'] . $param1; + $this->debug("s1: $s1"); + $s2 = $param2 . $this->options['secret']; + $this->debug("s2: $s2"); + + $hash = hash_init('sha256'); + hash_update($hash, $s1); + $s3 = hash_final($hash, false); + $this->debug("s3: $s3"); + + $hash = hash_init('sha256'); + hash_update($hash, $s2); + $s4 = hash_final($hash, false); + $this->debug("s4: $s4"); + + $out = ''; + for ($i = 0; $i < strlen($this->options['pattern']); $i++) { + $c = $this->options['pattern'][$i]; + if ($c == '0') + $out .= $s3[$i]; + else + $out .= $s4[$i]; + } + $this->debug("out: $out"); + return $out; + } + function time() { + return round(microtime(true) * 1000); + } + } From 3b95d7a32e83a49d6a6e0fb3169b787738190b55 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 11:35:36 -0700 Subject: [PATCH 13/14] Added larger dicitonary, bug fixes and cleanup. removed global dependancy --- .idea/workspace.xml | 171 +++++++++----------------------------------- 1 file changed, 35 insertions(+), 136 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index af2e4c2..3118435 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,13 @@ - - - + - - + + @@ -185,92 +165,6 @@ @@ -376,11 +274,11 @@ - - - + + + - + @@ -429,7 +327,7 @@ - + - + - + - - - - + + - + - + - - + + + + - + - + - + From eb068834bbf88fd8dcf884c9c7a6e3c596b9af31 Mon Sep 17 00:00:00 2001 From: ehime Date: Fri, 14 Jun 2013 11:52:25 -0700 Subject: [PATCH 14/14] whitespaces --- .idea/workspace.xml | 28 ++++++++++++++-------------- submodules/IniParser | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3118435..aea36d5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -10,7 +10,7 @@ - + @@ -275,10 +275,10 @@ - + - + @@ -353,13 +353,6 @@ - - - - - - - @@ -390,9 +383,16 @@ + + + + + + + - + diff --git a/submodules/IniParser b/submodules/IniParser index e48dddd..cf602c2 160000 --- a/submodules/IniParser +++ b/submodules/IniParser @@ -1 +1 @@ -Subproject commit e48dddd232e96213bc81ca9747db7f3f412a51ad +Subproject commit cf602c2e4eef7d05015f0f2f1a1fab8280e783ad