1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-06 06:38:00 +02:00

Merge pull request #3529 from Deltik/fix-3528

Namespaced class support!  Also: readlink() has been "polyfilled"
This commit is contained in:
Cameron
2018-10-31 16:17:04 -07:00
committed by GitHub
9 changed files with 182 additions and 108 deletions

View File

@@ -261,73 +261,6 @@ $e107 = e107::getInstance()->initCore($e107_paths, e_ROOT, $sql_info, varset($E1
e107::getSingleton('eIPHandler'); // This auto-handles bans etc
### NEW Register Autoload - do it asap
if(!function_exists('spl_autoload_register'))
{
// PHP >= 5.1.2 required
die('Fatal exception - spl_autoload_* required.');
}
// allow disable of autoloading - may be removed as e107::autoload_register() is flexible enough
if(!defset('E107_DISABLE_AUTOLOAD', false))
{
/**
* Generic autoloader. (didn't work while in e107_class.php)
* @example if your plugin calls 'use Xxxxx\Yyyyy\Zzzzz;' it will attempt to load: ./vendor/Xxxxx/Yyyyy/Zzzzz.php
*/
function autoloadPsr0($className)
{
$className = str_replace("_", "\\", $className);
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\'))
{
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
$fullPath = 'vendor'. DIRECTORY_SEPARATOR . $fileName;
if(file_exists($fullPath))
{
e107_require_once($fullPath);
}
else
{
return false;
}
}
e107::autoload_register(array('e107', 'autoload'));
// e107::autoload_register('autoloadPsr0'); // Generic 'use xxxx\yyyy\zzzz;' fix/solution for plugin developers.
}
function genericAutoload($className)
{
$className = str_replace("_", "\\", $className);
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\'))
{
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
e107_require_once($fileName);
}
/**
* NEW - system security levels
* Could be overridden by e107_config.php OR $CLASS2_INCLUDE script (if not set earlier)

View File

@@ -0,0 +1,17 @@
<?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;
class All
{
use InternalShims;
}

View File

@@ -0,0 +1,17 @@
<?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;
class Internal
{
use InternalShims;
}

View File

@@ -0,0 +1,63 @@
<?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 InternalShims
{
/**
* 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);
}
}

View File

@@ -0,0 +1,18 @@
<?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
*/
// e107 v2-style classes
namespace
{
class eShims extends \e107\Shims\All
{
}
}

View File

@@ -226,6 +226,7 @@ class e107
'eRequest' => '{e_HANDLER}application.php',
'eResponse' => '{e_HANDLER}application.php',
'eRouter' => '{e_HANDLER}application.php',
'eShims' => '{e_HANDLER}Shims/eShims.php',
'eUrl' => '{e_HANDLER}e107Url.php',
'eUrlConfig' => '{e_HANDLER}application.php',
'eUrlRule' => '{e_HANDLER}application.php',
@@ -4829,6 +4830,13 @@ class e107
*/
public static function autoload_register($function, $prepend = false)
{
### NEW Register Autoload - do it asap
if(!function_exists('spl_autoload_register'))
{
// PHP >= 5.1.2 required
die_fatal_error('Fatal exception - spl_autoload_* required.');
}
if(!$prepend || false === ($registered = spl_autoload_functions()))
{
return spl_autoload_register($function);
@@ -4886,6 +4894,14 @@ class e107
{
return;
}
// Detect namespaced class
if (strpos($className, '\\') !== false)
{
self::autoload_namespaced($className);
return;
}
$tmp = explode('_', $className);
//echo 'autoloding...'.$className.'<br />';
@@ -4947,6 +4963,27 @@ class e107
}
}
/**
* Autoloading logic for namespaced classes
*
* @param $className
* @return void
*/
private static function autoload_namespaced($className)
{
$levels = explode('\\', $className);
// Guard against classes that are not ours
if ($levels[0] != 'e107') return;
$levels[0] = e_HANDLER;
$classPath = implode('/', $levels).'.php';
if (is_file($classPath) && is_readable($classPath))
{
include($classPath);
}
}
public function __get($name)
{
switch ($name)
@@ -5125,3 +5162,5 @@ class e107
}
e107::autoload_register(array(e107::class, 'autoload'));

View File

@@ -111,7 +111,7 @@ function resize_image($source_file, $destination_file, $type = "upload", $model
{
if (($result = mimeFromFilename($source_file)) === FALSE) { return FALSE; }
header($result);
if (@readfile($source_file) === FALSE) { return FALSE; }
if (eShims::readfile($source_file) === FALSE) { return FALSE; }
}
else
{

View File

@@ -163,18 +163,6 @@ if($e107->initInstall($e107_paths, $ebase, $override)===false)
unset($e107_paths,$override,$ebase);
### NEW Register Autoload - do it asap
if(!function_exists('spl_autoload_register'))
{
// PHP >= 5.1.2 required
die_fatal_error('Fatal exception - spl_autoload_* required.');
}
// register core autoload
e107::autoload_register(array('e107', 'autoload'));
// NEW - session handler
require_once(e_HANDLER.'session_handler.php');
define('e_SECURITY_LEVEL', e_session::SECURITY_LEVEL_NONE);

View File

@@ -324,11 +324,12 @@ class e_thumbpage
$fname = e107::getParser()->thumbCacheFile($this->_src_path, $options);
if(($this->_cache === true) && is_file(e_CACHE_IMAGE.$fname) && is_readable(e_CACHE_IMAGE.$fname) && ($this->_debug !== true))
$cache_filename = e_CACHE_IMAGE . $fname;
if(($this->_cache === true) && is_file($cache_filename) && is_readable($cache_filename) && ($this->_debug !== true))
{
$thumbnfo['lmodified'] = filemtime(e_CACHE_IMAGE.$fname);
$thumbnfo['md5s'] = md5_file(e_CACHE_IMAGE.$fname);
$thumbnfo['fsize'] = filesize(e_CACHE_IMAGE.$fname);
$thumbnfo['lmodified'] = filemtime($cache_filename);
$thumbnfo['md5s'] = md5_file($cache_filename);
$thumbnfo['fsize'] = filesize($cache_filename);
// Send required headers
if($this->_debug !== true)
@@ -349,9 +350,7 @@ class e_thumbpage
// Send required headers
//$this->sendHeaders($thumbnfo);
@readfile(e_CACHE_IMAGE.$fname);
eShims::readfile($cache_filename);
//$bench->end()->logResult('thumb.php', $_GET['src'].' - retrieve cache');
exit;
@@ -443,7 +442,7 @@ class e_thumbpage
//exit;
// set cache
$thumb->save(e_CACHE_IMAGE.$fname);
$thumb->save($cache_filename);