Completed
Push — master ( 64322c...99e1b2 )
by cam
57s
created
ecrire/inc/livrer_fichier.php 1 patch
Indentation   +125 added lines, -125 removed lines patch added patch discarded remove patch
@@ -16,7 +16,7 @@  discard block
 block discarded – undo
16 16
  * @package SPIP\Core\Fichier
17 17
  **/
18 18
 if (!defined('_ECRIRE_INC_VERSION')) {
19
-	return;
19
+    return;
20 20
 }
21 21
 
22 22
 /**
@@ -36,28 +36,28 @@  discard block
 block discarded – undo
36 36
  */
37 37
 function spip_livrer_fichier($fichier, $content_type = 'application/octet-stream', $options = []) {
38 38
 
39
-	$defaut = [
40
-		'attachment' => false,
41
-		'expires' => 3600,
42
-		'range' => null
43
-	];
44
-	$options = array_merge($defaut, $options);
45
-	if (is_numeric($options['expires']) and $options['expires'] > 0) {
46
-		$options['expires'] = gmdate('D, d M Y H:i:s', time() + $options['expires']) . ' GMT';
47
-	}
48
-
49
-	if (is_null($options) and isset($_SERVER['HTTP_RANGE'])) {
50
-		$options['range'] = $_SERVER['HTTP_RANGE'];
51
-	}
52
-
53
-	spip_livrer_fichier_entetes($fichier, $content_type, $options['attachment'] && !$options['range'], $options['expires']);
54
-
55
-	if (!is_null($options['range'])) {
56
-		spip_livrer_fichier_partie($fichier, $options['range']);
57
-	}
58
-	else {
59
-		spip_livrer_fichier_entier($fichier);
60
-	}
39
+    $defaut = [
40
+        'attachment' => false,
41
+        'expires' => 3600,
42
+        'range' => null
43
+    ];
44
+    $options = array_merge($defaut, $options);
45
+    if (is_numeric($options['expires']) and $options['expires'] > 0) {
46
+        $options['expires'] = gmdate('D, d M Y H:i:s', time() + $options['expires']) . ' GMT';
47
+    }
48
+
49
+    if (is_null($options) and isset($_SERVER['HTTP_RANGE'])) {
50
+        $options['range'] = $_SERVER['HTTP_RANGE'];
51
+    }
52
+
53
+    spip_livrer_fichier_entetes($fichier, $content_type, $options['attachment'] && !$options['range'], $options['expires']);
54
+
55
+    if (!is_null($options['range'])) {
56
+        spip_livrer_fichier_partie($fichier, $options['range']);
57
+    }
58
+    else {
59
+        spip_livrer_fichier_entier($fichier);
60
+    }
61 61
 }
62 62
 
63 63
 /**
@@ -70,28 +70,28 @@  discard block
 block discarded – undo
70 70
  * @param int|string $expires
71 71
  */
72 72
 function spip_livrer_fichier_entetes($fichier, $content_type = 'application/octet-stream', $attachment = false, $expires = 0) {
73
-	// toujours envoyer un content type, meme vide !
74
-	header('Accept-Ranges: bytes');
75
-	header('Content-Type: ' . $content_type);
76
-
77
-	if ($attachment) {
78
-		$f = basename($fichier);
79
-		// ce content-type est necessaire pour eviter des corruptions de zip dans ie6
80
-		header('Content-Type: application/octet-stream');
81
-
82
-		header("Content-Disposition: attachment; filename=\"$f\";");
83
-		header('Content-Transfer-Encoding: binary');
84
-
85
-		// fix for IE caching or PHP bug issue
86
-		header('Expires: 0'); // set expiration time
87
-		header('Pragma: public');
88
-		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
89
-	}
90
-	else {
91
-		$f = basename($fichier);
92
-		header("Content-Disposition: inline; filename=\"$f\";");
93
-		header('Expires: ' . $expires); // set expiration time
94
-	}
73
+    // toujours envoyer un content type, meme vide !
74
+    header('Accept-Ranges: bytes');
75
+    header('Content-Type: ' . $content_type);
76
+
77
+    if ($attachment) {
78
+        $f = basename($fichier);
79
+        // ce content-type est necessaire pour eviter des corruptions de zip dans ie6
80
+        header('Content-Type: application/octet-stream');
81
+
82
+        header("Content-Disposition: attachment; filename=\"$f\";");
83
+        header('Content-Transfer-Encoding: binary');
84
+
85
+        // fix for IE caching or PHP bug issue
86
+        header('Expires: 0'); // set expiration time
87
+        header('Pragma: public');
88
+        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
89
+    }
90
+    else {
91
+        $f = basename($fichier);
92
+        header("Content-Disposition: inline; filename=\"$f\";");
93
+        header('Expires: ' . $expires); // set expiration time
94
+    }
95 95
 }
96 96
 
97 97
 /**
@@ -99,20 +99,20 @@  discard block
 block discarded – undo
99 99
  * @param string $fichier
100 100
  */
101 101
 function spip_livrer_fichier_entier($fichier) {
102
-	if (!file_exists($fichier)) {
103
-		throw new \Exception(sprintf('File not found: %s', $fichier));
104
-	}
102
+    if (!file_exists($fichier)) {
103
+        throw new \Exception(sprintf('File not found: %s', $fichier));
104
+    }
105 105
 
106
-	if (!is_readable($fichier)) {
107
-		throw new \Exception(sprintf('File not readable: %s', $fichier));
108
-	}
106
+    if (!is_readable($fichier)) {
107
+        throw new \Exception(sprintf('File not readable: %s', $fichier));
108
+    }
109 109
 
110
-	if ($size = filesize($fichier)) {
111
-		header(sprintf('Content-Length: %d', $size));
112
-	}
110
+    if ($size = filesize($fichier)) {
111
+        header(sprintf('Content-Length: %d', $size));
112
+    }
113 113
 
114
-	readfile($fichier);
115
-	exit();
114
+    readfile($fichier);
115
+    exit();
116 116
 }
117 117
 
118 118
 /**
@@ -125,98 +125,98 @@  discard block
 block discarded – undo
125 125
  * @throws Exception
126 126
  */
127 127
 function spip_livrer_fichier_partie($fichier, $range = null) {
128
-	if (!file_exists($fichier)) {
129
-		throw new \Exception(sprintf('File not found: %s', $fichier));
130
-	}
128
+    if (!file_exists($fichier)) {
129
+        throw new \Exception(sprintf('File not found: %s', $fichier));
130
+    }
131 131
 
132
-	if (!is_readable($fichier)) {
133
-		throw new \Exception(sprintf('File not readable: %s', $fichier));
134
-	}
132
+    if (!is_readable($fichier)) {
133
+        throw new \Exception(sprintf('File not readable: %s', $fichier));
134
+    }
135 135
 
136 136
 
137
-	// Par defaut on envoie tout
138
-	$byteOffset = 0;
139
-	$byteLength = $fileSize = filesize($fichier);
137
+    // Par defaut on envoie tout
138
+    $byteOffset = 0;
139
+    $byteLength = $fileSize = filesize($fichier);
140 140
 
141 141
 
142
-	// Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451"
143
-	if ($range and preg_match('%bytes=(\d+)-(\d+)?%i', $range, $match)) {
144
-		### Offset signifies where we should begin to read the file
145
-		$byteOffset = (int)$match[1];
142
+    // Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451"
143
+    if ($range and preg_match('%bytes=(\d+)-(\d+)?%i', $range, $match)) {
144
+        ### Offset signifies where we should begin to read the file
145
+        $byteOffset = (int)$match[1];
146 146
 
147 147
 
148
-		### Length is for how long we should read the file according to the browser, and can never go beyond the file size
149
-		if (isset($match[2])) {
150
-			$finishBytes = (int)$match[2];
151
-			$byteLength = $finishBytes + 1;
152
-		} else {
153
-			$finishBytes = $fileSize - 1;
154
-		}
148
+        ### Length is for how long we should read the file according to the browser, and can never go beyond the file size
149
+        if (isset($match[2])) {
150
+            $finishBytes = (int)$match[2];
151
+            $byteLength = $finishBytes + 1;
152
+        } else {
153
+            $finishBytes = $fileSize - 1;
154
+        }
155 155
 
156
-		$cr_header = sprintf('Content-Range: bytes %d-%d/%d', $byteOffset, $finishBytes, $fileSize);
157
-	}
158
-	else {
159
-		// si pas de range valide, on delegue a la methode d'envoi complet
160
-		spip_livrer_fichier_entier($fichier);
161
-		// redondant, mais facilite la comprehension du code
162
-		exit();
163
-	}
156
+        $cr_header = sprintf('Content-Range: bytes %d-%d/%d', $byteOffset, $finishBytes, $fileSize);
157
+    }
158
+    else {
159
+        // si pas de range valide, on delegue a la methode d'envoi complet
160
+        spip_livrer_fichier_entier($fichier);
161
+        // redondant, mais facilite la comprehension du code
162
+        exit();
163
+    }
164 164
 
165
-	// Remove headers that might unnecessarily clutter up the output
166
-	header_remove('Cache-Control');
167
-	header_remove('Pragma');
165
+    // Remove headers that might unnecessarily clutter up the output
166
+    header_remove('Cache-Control');
167
+    header_remove('Pragma');
168 168
 
169
-	// partial content
170
-	header('HTTP/1.1 206 Partial content');
171
-	header($cr_header);  ### Decrease by 1 on byte-length since this definition is zero-based index of bytes being sent
169
+    // partial content
170
+    header('HTTP/1.1 206 Partial content');
171
+    header($cr_header);  ### Decrease by 1 on byte-length since this definition is zero-based index of bytes being sent
172 172
 
173 173
 
174
-	$byteRange = $byteLength - $byteOffset;
174
+    $byteRange = $byteLength - $byteOffset;
175 175
 
176
-	header(sprintf('Content-Length: %d', $byteRange));
176
+    header(sprintf('Content-Length: %d', $byteRange));
177 177
 
178
-	// Variable containing the buffer
179
-	$buffer = '';
180
-	// Just a reasonable buffer size
181
-	$bufferSize = 512 * 16;
182
-	// Contains how much is left to read of the byteRange
183
-	$bytePool = $byteRange;
178
+    // Variable containing the buffer
179
+    $buffer = '';
180
+    // Just a reasonable buffer size
181
+    $bufferSize = 512 * 16;
182
+    // Contains how much is left to read of the byteRange
183
+    $bytePool = $byteRange;
184 184
 
185
-	if (!$handle = fopen($fichier, 'r')) {
186
-		throw new \Exception(sprintf('Could not get handle for file %s', $fichier));
187
-	}
185
+    if (!$handle = fopen($fichier, 'r')) {
186
+        throw new \Exception(sprintf('Could not get handle for file %s', $fichier));
187
+    }
188 188
 
189
-	if (fseek($handle, $byteOffset, SEEK_SET) == -1) {
190
-		throw new \Exception(sprintf('Could not seek to byte offset %d', $byteOffset));
191
-	}
189
+    if (fseek($handle, $byteOffset, SEEK_SET) == -1) {
190
+        throw new \Exception(sprintf('Could not seek to byte offset %d', $byteOffset));
191
+    }
192 192
 
193 193
 
194
-	while ($bytePool > 0) {
195
-		// How many bytes we request on this iteration
196
-		$chunkSizeRequested = min($bufferSize, $bytePool);
194
+    while ($bytePool > 0) {
195
+        // How many bytes we request on this iteration
196
+        $chunkSizeRequested = min($bufferSize, $bytePool);
197 197
 
198
-		// Try readin $chunkSizeRequested bytes from $handle and put data in $buffer
199
-		$buffer = fread($handle, $chunkSizeRequested);
198
+        // Try readin $chunkSizeRequested bytes from $handle and put data in $buffer
199
+        $buffer = fread($handle, $chunkSizeRequested);
200 200
 
201
-		// Store how many bytes were actually read
202
-		$chunkSizeActual = strlen($buffer);
201
+        // Store how many bytes were actually read
202
+        $chunkSizeActual = strlen($buffer);
203 203
 
204
-		// If we didn't get any bytes that means something unexpected has happened since $bytePool should be zero already
205
-		if ($chunkSizeActual == 0) {
206
-			// For production servers this should go in your php error log, since it will break the output
207
-			trigger_error('Chunksize became 0', E_USER_WARNING);
208
-			break;
209
-		}
204
+        // If we didn't get any bytes that means something unexpected has happened since $bytePool should be zero already
205
+        if ($chunkSizeActual == 0) {
206
+            // For production servers this should go in your php error log, since it will break the output
207
+            trigger_error('Chunksize became 0', E_USER_WARNING);
208
+            break;
209
+        }
210 210
 
211
-		// Decrease byte pool with amount of bytes that were read during this iteration
212
-		$bytePool -= $chunkSizeActual;
211
+        // Decrease byte pool with amount of bytes that were read during this iteration
212
+        $bytePool -= $chunkSizeActual;
213 213
 
214
-		// Write the buffer to output
215
-		print $buffer;
214
+        // Write the buffer to output
215
+        print $buffer;
216 216
 
217
-		// Try to output the data to the client immediately
218
-		flush();
219
-	}
217
+        // Try to output the data to the client immediately
218
+        flush();
219
+    }
220 220
 
221
-	exit();
221
+    exit();
222 222
 }
Please login to merge, or discard this patch.