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