mirror of
https://github.com/e107inc/e107.git
synced 2025-04-22 05:31:58 +02:00
Moved readfile() shim to its own trait
This commit is contained in:
parent
e1e8a8b627
commit
f93ab61372
e107_handlers/Shims
64
e107_handlers/Shims/Internal/ReadfileTrait.php
Normal file
64
e107_handlers/Shims/Internal/ReadfileTrait.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2020 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
* Shims for PHP internal functions
|
||||
* readfile()
|
||||
*/
|
||||
|
||||
namespace e107\Shims\Internal;
|
||||
|
||||
trait ReadfileTrait
|
||||
{
|
||||
/**
|
||||
* Outputs a file
|
||||
*
|
||||
* Resilient replacement for PHP internal readfile()
|
||||
*
|
||||
* @see https://github.com/e107inc/e107/issues/3528 Why this method was implemented
|
||||
* @param string $filename The filename being read.
|
||||
* @param bool $use_include_path You can use the optional second parameter and set it to TRUE,
|
||||
* if you want to search for the file in the include_path, too.
|
||||
* @param resource $context A context stream resource.
|
||||
* @return int|bool Returns the number of bytes read from the file.
|
||||
* If an error occurs, FALSE is returned.
|
||||
*/
|
||||
public static function readfile($filename, $use_include_path = FALSE, $context = NULL)
|
||||
{
|
||||
$output = @readfile($filename, $use_include_path, $context);
|
||||
if ($output === NULL)
|
||||
{
|
||||
return self::readfile_alt($filename, $use_include_path, $context);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a file
|
||||
*
|
||||
* Alternative implementation using file streams
|
||||
*
|
||||
* @param $filename
|
||||
* @param bool $use_include_path
|
||||
* @param resource $context
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function readfile_alt($filename, $use_include_path = FALSE, $context = NULL)
|
||||
{
|
||||
// fopen() silently returns false if there is no context
|
||||
if (!is_resource($context)) $context = stream_context_create();
|
||||
|
||||
$handle = @fopen($filename, 'rb', $use_include_path, $context);
|
||||
if ($handle === FALSE) return FALSE;
|
||||
while (!feof($handle))
|
||||
{
|
||||
echo(fread($handle, 8192));
|
||||
}
|
||||
fclose($handle);
|
||||
return filesize($filename);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
/**
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2018 e107 Inc (e107.org)
|
||||
* Copyright (C) 2008-2020 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
|
@ -2,7 +2,7 @@
|
||||
/**
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2018 e107 Inc (e107.org)
|
||||
* Copyright (C) 2008-2020 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
@ -13,51 +13,5 @@ namespace e107\Shims;
|
||||
|
||||
trait InternalShimsTrait
|
||||
{
|
||||
/**
|
||||
* Outputs a file
|
||||
*
|
||||
* Resilient replacement for PHP internal readfile()
|
||||
*
|
||||
* @see https://github.com/e107inc/e107/issues/3528 Why this method was implemented
|
||||
* @param string $filename The filename being read.
|
||||
* @param bool $use_include_path You can use the optional second parameter and set it to TRUE,
|
||||
* if you want to search for the file in the include_path, too.
|
||||
* @param resource $context A context stream resource.
|
||||
* @return int|bool Returns the number of bytes read from the file.
|
||||
* If an error occurs, FALSE is returned.
|
||||
*/
|
||||
public static function readfile($filename, $use_include_path = FALSE, $context = NULL)
|
||||
{
|
||||
$output = @readfile($filename, $use_include_path, $context);
|
||||
if ($output === NULL)
|
||||
{
|
||||
return self::readfile_alt($filename, $use_include_path, $context);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a file
|
||||
*
|
||||
* Alternative implementation using file streams
|
||||
*
|
||||
* @param $filename
|
||||
* @param bool $use_include_path
|
||||
* @param resource $context
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function readfile_alt($filename, $use_include_path = FALSE, $context = NULL)
|
||||
{
|
||||
// fopen() silently returns false if there is no context
|
||||
if (!is_resource($context)) $context = stream_context_create();
|
||||
|
||||
$handle = @fopen($filename, 'rb', $use_include_path, $context);
|
||||
if ($handle === FALSE) return FALSE;
|
||||
while (!feof($handle))
|
||||
{
|
||||
echo(fread($handle, 8192));
|
||||
}
|
||||
fclose($handle);
|
||||
return filesize($filename);
|
||||
}
|
||||
use Internal\ReadfileTrait;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user