Fixed Resources being unable to serve static files when using more complicated URI's.
This commit is contained in:
parent
e3485fa256
commit
1a22cfb24f
@ -47,17 +47,17 @@ class ResourceServeEvent extends Event
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $resourceUrlSegments;
|
||||
public $requestURL;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $resourceFilePath;
|
||||
|
||||
public function init(string $resourceName, array $resourceUrlSegments, string $resourceFilePath)
|
||||
public function init(string $resourceName, string $requestURL, string $resourceFilePath)
|
||||
{
|
||||
$this->resourceName = $resourceName;
|
||||
$this->resourceUrlSegments = $resourceUrlSegments;
|
||||
$this->requestURL = $requestURL;
|
||||
$this->resourceFilePath = $resourceFilePath;
|
||||
}
|
||||
}
|
@ -61,10 +61,10 @@ class Resources
|
||||
$this->output = Factory::getInstance()->output;
|
||||
}
|
||||
|
||||
public function resourceExists(array $resourceUrlSegments): bool
|
||||
public function resourceExists(string $requestURL): bool
|
||||
{
|
||||
// First find the resource
|
||||
$file = $this->findResource($resourceUrlSegments);
|
||||
$file = $this->findResource($requestURL);
|
||||
|
||||
// If not found, return false;
|
||||
if (is_null($file))
|
||||
@ -77,17 +77,17 @@ class Resources
|
||||
/**
|
||||
* Serves a static file if found.
|
||||
*
|
||||
* @param array $resourceUrlSegments
|
||||
* @param string $requestURL
|
||||
* @return bool
|
||||
* @throws WebException
|
||||
*
|
||||
* @todo Bypass the Output system and use the readFile() method.
|
||||
* @todo Run as FuzeWorks pre-code, before creating the container
|
||||
*/
|
||||
public function serveResource(array $resourceUrlSegments): bool
|
||||
public function serveResource(string $requestURL): bool
|
||||
{
|
||||
// First find the resource
|
||||
$file = $this->findResource($resourceUrlSegments);
|
||||
$file = $this->findResource($requestURL);
|
||||
|
||||
// If not found return false
|
||||
if (is_null($file))
|
||||
@ -96,7 +96,7 @@ class Resources
|
||||
// If a file is found, fire a serveResourceEvent
|
||||
/** @var ResourceServeEvent $event */
|
||||
try {
|
||||
$event = Events::fireEvent('resourceServeEvent', $file['resourceName'], $file['segments'], $file['file']);
|
||||
$event = Events::fireEvent('resourceServeEvent', $file['resourceName'], $file['requestURL'], $file['file']);
|
||||
} catch (Exception\EventException $e) {
|
||||
throw new WebException("Could not serve resource. resourceServeEvent threw exception: '" . $e->getMessage() . "'");
|
||||
}
|
||||
@ -106,7 +106,7 @@ class Resources
|
||||
return false;
|
||||
|
||||
// Log the resource serving
|
||||
Logger::log("Serving static resource '/" . $file['resourceName'] . '/' . implode('/', $file['segments']) . "'");
|
||||
Logger::log("Serving static resource '/" . $file['resourceName'] . '/' . $file['requestURL'] . "'");
|
||||
|
||||
// Serve file in accordance with event
|
||||
$fileExtension = pathinfo($event->resourceFilePath, PATHINFO_EXTENSION);
|
||||
@ -118,25 +118,22 @@ class Resources
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function findResource(array $resourceUrlSegments): ?array
|
||||
protected function findResource(string $requestURL): ?array
|
||||
{
|
||||
// If too few segments provided, don't even bother
|
||||
if (count($resourceUrlSegments) < 2)
|
||||
return null;
|
||||
|
||||
// First segment should be the resourceName, check if it exists
|
||||
$resourceName = urldecode($resourceUrlSegments[1]);
|
||||
if (!isset($this->resources[$resourceName]))
|
||||
return null;
|
||||
foreach ($this->resources as $resourceName => $resourceDir)
|
||||
{
|
||||
if (substr($requestURL, 0, strlen($resourceName)) === $resourceName)
|
||||
{
|
||||
$fileURL = ltrim(substr($requestURL, strlen($resourceName)), '/');
|
||||
$fileURL = str_replace('/', DS, $fileURL);
|
||||
$file = $this->resources[$resourceName] . DS . $fileURL;
|
||||
|
||||
// If resource is found, generate file path
|
||||
$resourceUrlSegmentsBck = $resourceUrlSegments;
|
||||
array_shift($resourceUrlSegments);
|
||||
$file = $this->resources[$resourceName] . DS . implode(DS, $resourceUrlSegments);
|
||||
|
||||
// Test if file exists, if it does, return the string
|
||||
if (file_exists($file) && is_file($file))
|
||||
return ['file' => $file, 'resourceName' => $resourceName, 'segments' => $resourceUrlSegments];
|
||||
// Test if file exists, if it does, return the string
|
||||
if (file_exists($file) && is_file($file))
|
||||
return ['file' => $file, 'resourceName' => $resourceName, 'requestURL' => $fileURL];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -242,7 +242,7 @@ class WebComponent implements iComponent
|
||||
return true;
|
||||
|
||||
// Attempt to load a static resource
|
||||
if ($resources->serveResource($uri->segmentArray()))
|
||||
if ($resources->serveResource($uri->uriString()))
|
||||
return true;
|
||||
|
||||
// First test for Cross Site Request Forgery
|
||||
@ -343,14 +343,17 @@ class WebComponent implements iComponent
|
||||
/** @var Layout $layout */
|
||||
$output = Factory::getInstance()->output;
|
||||
$router = Factory::getInstance()->router;
|
||||
$layout = Factory::getInstance()->layouts;
|
||||
|
||||
// Reset the layout engine
|
||||
if (isset(Factory::getInstance()->layouts))
|
||||
{
|
||||
$layout = Factory::getInstance()->layouts;
|
||||
$layout->reset();
|
||||
}
|
||||
|
||||
// Cancel event
|
||||
$event->setCancelled(true);
|
||||
|
||||
// Reset the layout engine
|
||||
$layout->reset();
|
||||
|
||||
// Remove listener so that error pages won't be intercepted
|
||||
Events::removeListener([$this, 'callViewEventListener'], 'routerCallViewEvent',Priority::HIGHEST);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user