Browse Source

Simplification de code, utiliser la fonction spip_livrer_fichier() du core

master
Cerdic 7 months ago
parent
commit
d985b16e5b
  1. 159
      action/api_docrestreint.php
  2. 22
      action/autoriser.php

159
action/api_docrestreint.php

@ -250,166 +250,15 @@ function accesrestreint_afficher_document(Accesrestreint_document $Document) {
* @param string $fileName
* @param string $contentType
* @param false $attachment
* @param int $expire
* @param int $expires
* @throws Exception
*/
function accesrestreint_envoyer_fichier($fileName, $contentType = 'application/octet-stream', $attachment = false, $expire = 3600) {
accesrestreint_envoyer_fichier_entetes($fileName, $contentType, $attachment, $expire);
function accesrestreint_envoyer_fichier($fileName, $contentType = 'application/octet-stream', $attachment = false, $expires = 3600) {
if (isset($_SERVER['HTTP_RANGE'])) {
accesrestreint_envoyer_fichier_partie($fileName, $contentType);
}
else {
accesrestreint_envoyer_fichier_entier($fileName, $contentType);
}
}
/**
* Envoyer les entetes du fichier, sauf ce qui est lie au mode d'envoi (entier ou par parties)
* @see accesrestreint_envoyer_fichier()
* @param string $fileName
* @param string $contentType
* @param false $attachment
* @param int $expire
*/
function accesrestreint_envoyer_fichier_entetes($fileName, $contentType = 'application/octet-stream', $attachment = false, $expire = 3600) {
// toujours envoyer un content type, meme vide !
header('Accept-Ranges: bytes');
header('Content-Type: ' . $contentType);
if ($attachment) {
$f = basename($fileName);
// ce content-type est necessaire pour eviter des corruptions de zip dans ie6
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$f\";");
header('Content-Transfer-Encoding: binary');
// fix for IE catching or PHP bug issue
header('Pragma: public');
header('Expires: 0'); // set expiration time
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
}
else {
$f = basename($fileName);
header("Content-Disposition: inline; filename=\"$f\";");
header('Expires: ' . intval($expire)); // set expiration time
}
}
/**
* Envoyer les contenu entier du fichier
* @param string $fileName
* @param string $contentType
*/
function accesrestreint_envoyer_fichier_entier($fileName, $contentType = 'application/octet-stream') {
if ($size = filesize($fileName)) {
header('Content-Length: ' . $size);
}
readfile($fileName);
include_spip('inc/livrer_fichier');
spip_livrer_fichier($fileName, $contentType, ['attachment' => $attachment, 'expires' => $expires]);
}
/**
* Envoyer une partie du fichier
* Prendre en charge l'entete Range:bytes=0-456 utilise par les player medias
* source : https://github.com/pomle/php-serveFilePartial/blob/master/ServeFilePartial.inc.php
*
* @param string $fileName
* @param string $contentType
* @throws Exception
*/
function accesrestreint_envoyer_fichier_partie($fileName, $contentType = 'application/octet-stream') {
if (!file_exists($fileName)) {
throw new \Exception(sprintf('File not found: %s', $fileName));
}
if (!is_readable($fileName)) {
throw new \Exception(sprintf('File not readable: %s', $fileName));
}
### Remove headers that might unnecessarily clutter up the output
header_remove('Cache-Control');
header_remove('Pragma');
### Default to send entire file
$byteOffset = 0;
$byteLength = $fileSize = filesize($fileName);
//header('Accept-Ranges: bytes', true);
//header(sprintf('Content-Type: %s', $contentType), true);
### Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451"
if (isset($_SERVER['HTTP_RANGE']) && preg_match('%bytes=(\d+)-(\d+)?%i', $_SERVER['HTTP_RANGE'], $match)) {
### Offset signifies where we should begin to read the file
$byteOffset = (int)$match[1];
### Length is for how long we should read the file according to the browser, and can never go beyond the file size
if (isset($match[2])) {
$finishBytes = (int)$match[2];
$byteLength = $finishBytes + 1;
} else {
$finishBytes = $fileSize - 1;
}
$cr_header = sprintf('Content-Range: bytes %d-%d/%d', $byteOffset, $finishBytes, $fileSize);
header('HTTP/1.1 206 Partial content');
header($cr_header); ### Decrease by 1 on byte-length since this definition is zero-based index of bytes being sent
}
$byteRange = $byteLength - $byteOffset;
header(sprintf('Content-Length: %d', $byteRange));
header(sprintf('Expires: %s', date('D, d M Y H:i:s', time() + 60 * 60 * 24 * 90) . ' GMT'));
$buffer = ''; ### Variable containing the buffer
$bufferSize = 512 * 16; ### Just a reasonable buffer size
$bytePool = $byteRange; ### Contains how much is left to read of the byteRange
if (!$handle = fopen($fileName, 'r')) {
throw new \Exception(sprintf('Could not get handle for file %s', $fileName));
}
if (fseek($handle, $byteOffset, SEEK_SET) == -1) {
throw new \Exception(sprintf('Could not seek to byte offset %d', $byteOffset));
}
while ($bytePool > 0) {
$chunkSizeRequested = min($bufferSize, $bytePool); ### How many bytes we request on this iteration
### Try readin $chunkSizeRequested bytes from $handle and put data in $buffer
$buffer = fread($handle, $chunkSizeRequested);
### Store how many bytes were actually read
$chunkSizeActual = strlen($buffer);
### If we didn't get any bytes that means something unexpected has happened since $bytePool should be zero already
if ($chunkSizeActual == 0) {
### For production servers this should go in your php error log, since it will break the output
trigger_error('Chunksize became 0', E_USER_WARNING);
break;
}
### Decrease byte pool with amount of bytes that were read during this iteration
$bytePool -= $chunkSizeActual;
### Write the buffer to output
print $buffer;
### Try to output the data to the client immediately
flush();
}
exit();
}
/**

22
action/autoriser.php

@ -32,26 +32,8 @@ function action_autoriser_dist() {
spip_log('Acces refuse (restreint) au document ' . $arg . ': ' . $file, 'accesrestreint');
redirige_par_entete('./?page=404');
} else {
if (!function_exists('mime_content_type')) {
// https://code.spip.net/@mime_content_type
function mime_content_type($f) {
preg_match('/\.(\w+)/', $f, $r);
return $r[1];
}
}
$ct = mime_content_type($file);
$cl = filesize($file);
$filename = basename($file);
header('Content-Type: ' . $ct);
header('Content-Disposition: attachment; filename="' . $filename . ';');
if ($dcc) {
header('Content-Description: ' . $dcc);
}
if ($cl) {
header('Content-Length: ' . $cl);
}
header('Content-Transfer-Encoding: binary');
readfile($file);
include_spip('inc/livrer_fichier');
spip_livrer_fichier($file, mime_content_type($file), ['attachment' => true]);
}
}

Loading…
Cancel
Save