Skip to content

Commit c8ec7ba

Browse files
author
Stan Hutcheon
committed
Further sanity checks, output consistency fixes
commit 0ce0ef3 Author: Stan Hutcheon <[email protected]> Date: Wed Feb 18 13:04:46 2015 +0000 Add fixes for file not returning commit b4ac30f Author: Stan Hutcheon <[email protected]> Date: Wed Feb 18 12:56:08 2015 +0000 Add missing breaks in method selection commit 43f469a Author: Stan Hutcheon <[email protected]> Date: Wed Feb 18 11:52:24 2015 +0000 Add Accept-Ranges checks commit be449ec Author: Alexander Mikhailov <[email protected]> Date: Tue Feb 17 23:14:27 2015 +0300 fix php 5.3 incompatibility
1 parent 1f62a57 commit c8ec7ba

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/Stnvh/Partial/Data/PartialData.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ public function get() {
4848
switch($this->method) {
4949
case 8:
5050
$_method = 'gzinflate';
51+
break;
5152
case 12:
5253
if(!extension_loaded('bz2')){
5354
@dl((strtolower(substr(PHP_OS, 0, 3)) == 'win') ? 'php_bz2.dll' : 'bz2.so');
5455
}
5556

5657
if(extension_loaded('bz2')) {
5758
$_method = 'bzdecompress';
59+
break;
5860
} else {
5961
user_error('Unable to decompress, failed to load bz2 extension', E_USER_ERROR);
6062
exit;
@@ -110,7 +112,8 @@ public function format($raw, $map = false) {
110112

111113
$sect = substr($raw, (isset($pos[2]) ? $pos[2] : $i), $pos[0]);
112114
if($pos[1]) {
113-
$sect = unpack($pos[1], $sect)[1];
115+
$sect = unpack($pos[1], $sect);
116+
$sect = $sect[1];
114117
}
115118
$this->$name = $sect;
116119

@@ -131,7 +134,5 @@ public function format($raw, $map = false) {
131134
$this->format($this->extra, $_map);
132135
$this->compressedSize = $this->size;
133136
}
134-
135-
ob_clean(); # clean output
136137
}
137138
}

src/Stnvh/Partial/Zip.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ private function receiveData($ch, $data) {
6464
* @return void
6565
*/
6666
public function __construct($url, $file = false) {
67-
set_time_limit(15);
6867
ob_start();
6968

7069
$this->info = new Data\ZipInfo();
@@ -82,12 +81,19 @@ public function init() {
8281
$request = $this->httpRequest(array(
8382
CURLOPT_URL => $this->info->url,
8483
CURLOPT_FOLLOWLOCATION => true,
85-
CURLOPT_NOBODY => true
84+
CURLOPT_NOBODY => true,
85+
CURLOPT_HEADER => true,
86+
CURLOPT_RETURNTRANSFER => true
8687
));
8788

8889
if($request['http_code'] > 400) {
8990
user_error(sprintf('Initial request failed, got HTTP status code: %d', $request['http_code']) , E_USER_ERROR);
90-
die;
91+
exit;
92+
}
93+
94+
if(!$request['headers']['Accept-Ranges']) {
95+
user_error('Server does not support HTTP range requests', E_USER_ERROR);
96+
exit;
9197
}
9298

9399
$this->info->length = intval($request['download_content_length']);
@@ -113,7 +119,7 @@ public function init() {
113119
$this->info->centralDirectoryDesc = new Data\EOCD($_EOCD);
114120
} else {
115121
user_error('End of central directory not found', E_USER_ERROR);
116-
die;
122+
exit;
117123
}
118124

119125
if($cdEnd = $this->info->centralDirectoryDesc) {
@@ -230,6 +236,7 @@ public function get(Data\CDFile $file, $output = false) {
230236
));
231237

232238
if($output) {
239+
ob_clean(); # clean output
233240
header(sprintf('Content-Disposition: attachment; filename="%s"', $file->filename));
234241
header(sprintf('Content-Length: %d', $file->size));
235242
header('Pragma: public');
@@ -254,6 +261,17 @@ protected function httpRequest($conf) {
254261
$out = curl_exec($ch);
255262

256263
$info = curl_getinfo($ch);
264+
265+
if($conf[CURLOPT_HEADER] && preg_match_all('/(.*): (.*)\r?\n/', $out, $match)) {
266+
$_headers = substr($out, 0, $info['header_size']);
267+
$headers = array();
268+
foreach($match[1] as $i => $header) {
269+
$headers[$header] = $match[2][$i];
270+
}
271+
$info['headers'] = $headers;
272+
$out = substr($out, $info['header_size']);
273+
}
274+
257275
$info['response'] = $out;
258276

259277
curl_close($ch);

0 commit comments

Comments
 (0)