1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 20:58:30 +01:00
php-e107/e107_handlers/Shims/InternalShimsTrait.php
Nick Liu 9cf215a0be
Better names for eShims classes
* \e107\Shims\All → \e107\Shims\AllShims
* \e107\Shims\Internal → \e107\Shims\InternalShims
* \e107\Shims\InternalShims → \e107\Shims\InternalShimsTrait

Fixes: #3538
2018-11-03 09:24:05 -05:00

63 lines
1.8 KiB
PHP

<?php
/**
* e107 website system
*
* Copyright (C) 2008-2018 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
*/
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);
}
}