Skip to content

Commit c34f418

Browse files
committed
Merge pull request JorgenPhi#25 from hisham-albeik/identify_compression
Identify compression
2 parents c5eb944 + 0a5b8ae commit c34f418

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/snapchat.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,11 @@ public function unblock($username) {
665665
*
666666
* @return mixed
667667
* The snap data or FALSE on failure.
668+
* Snap data can returned as an Array of more than one file.
669+
* array(
670+
* overlay~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => overlay_file_data,
671+
* media~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => m4v_file_data
672+
* )
668673
*/
669674
public function getMedia($id) {
670675
// Make sure we're logged in and have a valid access token.
@@ -695,6 +700,18 @@ public function getMedia($id) {
695700
if (parent::isMedia(substr($result, 0, 2))) {
696701
return $result;
697702
}
703+
704+
//When a snapchat video is sent with "text" or overlay
705+
//the overlay is a transparent PNG file Zipped together
706+
//with the M4V file.
707+
//First two bytes are "PK" x50x4B; thus the previous media check
708+
//will fail and would've returned a FALSE on an available media.
709+
if (parent::isCompressed(substr($result, 0, 2))) {
710+
//Uncompress
711+
$result = parent::unCompress($result);
712+
//Return Media and Overlay
713+
return $result;
714+
}
698715
}
699716

700717
return FALSE;

src/snapchat_agent.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,59 @@ function isMedia($data) {
189189
return FALSE;
190190
}
191191

192+
/**
193+
* Checks to see if a blob looks like a compressed file.
194+
*
195+
* @param data $data
196+
* The blob data (or just the header).
197+
*
198+
* @return bool
199+
* TRUE if the blob looks like a compressed file, FALSE otherwise.
200+
*/
201+
function isCompressed($data) {
202+
// Check for a PK header.
203+
if ($data[0] == chr(0x50) && $data[1] == chr(0x4B)) {
204+
return TRUE;
205+
}
206+
207+
return FALSE;
208+
}
209+
210+
/**
211+
* Uncompress the blob and put the data into an Array.
212+
* Array(
213+
* overlay~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => overlay_file_data,
214+
* media~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => m4v_file_data
215+
* )
216+
*
217+
* @param data $data
218+
* The blob data (or just the header).
219+
*
220+
* @return array
221+
* Array containing both file contents, or FALSE if couldn't extract.
222+
*/
223+
function unCompress($data) {
224+
if (!file_put_contents("./temp", $data)) {
225+
exit('Should have write access to own folder');
226+
}
227+
$resource = zip_open("./temp");
228+
$result = FALSE;
229+
if (is_resource($resource)) {
230+
while($zip_entry = zip_read($resource)) {
231+
$filename = zip_entry_name($zip_entry);
232+
if (zip_entry_open($resource, $zip_entry, "r")) {
233+
$result[$filename] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
234+
zip_entry_close($zip_entry);
235+
} else {
236+
return FALSE;
237+
}
238+
}
239+
zip_close($resource);
240+
}
241+
242+
return $result;
243+
}
244+
192245
/**
193246
* Performs a GET request. Currently only used for story blobs.
194247
*

0 commit comments

Comments
 (0)