2019-02-03 23:10:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FuzeWorks WebComponent.
|
|
|
|
*
|
|
|
|
* The FuzeWorks PHP FrameWork
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2019 TechFuze
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author TechFuze
|
|
|
|
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
|
|
*
|
|
|
|
* @link http://techfuze.net/fuzeworks
|
|
|
|
* @since Version 1.2.0
|
|
|
|
*
|
|
|
|
* @version Version 1.2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FuzeWorks;
|
|
|
|
|
2019-03-01 10:25:18 +00:00
|
|
|
use FuzeWorks\Event\HaltExecutionEvent;
|
|
|
|
use FuzeWorks\Event\LayoutLoadEvent;
|
|
|
|
use FuzeWorks\Event\RouterCallViewEvent;
|
2019-07-22 17:53:18 +00:00
|
|
|
use FuzeWorks\Event\RouteWebRequestEvent;
|
2019-03-05 10:23:52 +00:00
|
|
|
use FuzeWorks\Exception\ConfigException;
|
2019-03-01 10:25:18 +00:00
|
|
|
use FuzeWorks\Exception\CSRFException;
|
2019-02-09 19:22:49 +00:00
|
|
|
use FuzeWorks\Exception\EventException;
|
|
|
|
use FuzeWorks\Exception\Exception;
|
2019-02-15 18:30:11 +00:00
|
|
|
use FuzeWorks\Exception\HaltException;
|
2019-02-09 19:22:49 +00:00
|
|
|
use FuzeWorks\Exception\NotFoundException;
|
2019-02-15 18:30:11 +00:00
|
|
|
use FuzeWorks\Exception\OutputException;
|
|
|
|
use FuzeWorks\Exception\RouterException;
|
2019-03-01 10:25:18 +00:00
|
|
|
use FuzeWorks\Exception\SecurityException;
|
2019-02-09 19:22:49 +00:00
|
|
|
use FuzeWorks\Exception\WebException;
|
2019-02-03 23:10:18 +00:00
|
|
|
|
|
|
|
class WebComponent implements iComponent
|
|
|
|
{
|
2019-02-09 19:22:49 +00:00
|
|
|
/**
|
|
|
|
* Whether WebComponent is configured to handle a web request
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public static $willHandleRequest = false;
|
|
|
|
|
2019-02-03 23:10:18 +00:00
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return "WebComponent";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClasses(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'web' => $this,
|
2019-02-09 19:22:49 +00:00
|
|
|
'security' => '\FuzeWorks\Security',
|
2019-02-03 23:10:18 +00:00
|
|
|
'input' => '\FuzeWorks\Input',
|
2019-02-09 19:22:49 +00:00
|
|
|
'output' => '\FuzeWorks\Output',
|
|
|
|
'uri' => '\FuzeWorks\URI',
|
2019-07-22 17:53:18 +00:00
|
|
|
'resources' => '\FuzeWorks\Resources'
|
2019-02-03 23:10:18 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAddComponent(Configurator $configurator)
|
|
|
|
{
|
|
|
|
// Add dependencies
|
|
|
|
$configurator->addComponent(new MVCRComponent());
|
|
|
|
|
|
|
|
// Add fallback config directory
|
|
|
|
$configurator->addDirectory(
|
|
|
|
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Config',
|
|
|
|
'config',
|
|
|
|
Priority::LOWEST
|
|
|
|
);
|
2019-02-09 19:22:49 +00:00
|
|
|
|
|
|
|
// If WebComponent will handle a request, add some calls to the configurator
|
|
|
|
if (self::$willHandleRequest)
|
|
|
|
{
|
|
|
|
// Invoke methods to prepare system for HTTP calls
|
|
|
|
$configurator->call('logger', 'setLoggerTemplate', null, 'logger_http');
|
|
|
|
}
|
2019-02-03 23:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onCreateContainer(Factory $container)
|
|
|
|
{
|
|
|
|
}
|
2019-02-09 19:22:49 +00:00
|
|
|
|
2019-02-15 18:30:11 +00:00
|
|
|
/**
|
|
|
|
* On initializing, Initialize UTF8 first, since it's a dependency for other componentClasses
|
|
|
|
*/
|
2019-02-09 19:22:49 +00:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
// First init UTF8
|
|
|
|
UTF8::init();
|
2019-03-01 10:25:18 +00:00
|
|
|
|
|
|
|
// Register some base events
|
|
|
|
Events::addListener([$this, 'layoutLoadEventListener'], 'layoutLoadEvent', Priority::NORMAL);
|
2019-02-09 19:22:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 18:30:11 +00:00
|
|
|
/**
|
|
|
|
* Enable the WebComponent to prepare for handling requests
|
|
|
|
*/
|
2019-02-09 19:22:49 +00:00
|
|
|
public function enableComponent()
|
|
|
|
{
|
|
|
|
self::$willHandleRequest = true;
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:30:11 +00:00
|
|
|
/**
|
|
|
|
* Disable the WebComponent so it won't prepare for handling requests
|
|
|
|
*/
|
2019-02-09 19:22:49 +00:00
|
|
|
public function disableComponent()
|
|
|
|
{
|
|
|
|
self::$willHandleRequest = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-15 18:30:11 +00:00
|
|
|
* Handle a Web request.
|
|
|
|
*
|
|
|
|
* Retrieves URI string, routes this URI using the provided routes,
|
|
|
|
* appends output and adds listener to view output on shutdown.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @throws OutputException
|
|
|
|
* @throws RouterException
|
2019-02-09 19:22:49 +00:00
|
|
|
* @throws WebException
|
2019-03-05 10:23:52 +00:00
|
|
|
* @throws EventException
|
2019-02-09 19:22:49 +00:00
|
|
|
*/
|
|
|
|
public function routeWebRequest(): bool
|
|
|
|
{
|
|
|
|
if (!self::$willHandleRequest)
|
|
|
|
throw new WebException("Could not route web request. WebComponent is not configured to handle requests");
|
|
|
|
|
|
|
|
try {
|
2019-02-15 18:30:11 +00:00
|
|
|
// Set the output to display when shutting down
|
2019-03-01 10:25:18 +00:00
|
|
|
Events::addListener(function ($event) {
|
2019-02-09 19:22:49 +00:00
|
|
|
/** @var Output $output */
|
|
|
|
Logger::logInfo("Parsing output...");
|
|
|
|
$output = Factory::getInstance()->output;
|
|
|
|
$output->display();
|
2019-07-22 09:48:45 +00:00
|
|
|
return $event;
|
2019-02-09 19:22:49 +00:00
|
|
|
}, 'coreShutdownEvent', Priority::NORMAL);
|
2019-02-15 18:30:11 +00:00
|
|
|
|
|
|
|
// Create an error 500 page when a haltEvent is fired
|
|
|
|
Events::addListener([$this, 'haltEventListener'], 'haltExecutionEvent', Priority::NORMAL);
|
2019-02-09 19:22:49 +00:00
|
|
|
} catch (EventException $e) {
|
|
|
|
throw new WebException("Could not route web request. coreShutdownEvent threw EventException: '".$e->getMessage()."'");
|
|
|
|
}
|
|
|
|
|
2019-03-05 10:23:52 +00:00
|
|
|
// Remove the X-Powered-By header, since it's a security risk
|
|
|
|
header_remove("X-Powered-By");
|
|
|
|
|
2019-02-09 19:22:49 +00:00
|
|
|
/** @var Router $router */
|
2019-02-15 18:30:11 +00:00
|
|
|
/** @var URI $uri */
|
2019-02-09 19:22:49 +00:00
|
|
|
/** @var Output $output */
|
2019-03-01 10:25:18 +00:00
|
|
|
/** @var Security $security */
|
2019-07-22 17:53:18 +00:00
|
|
|
/** @var Resources $resources */
|
2019-02-15 18:30:11 +00:00
|
|
|
$router = Factory::getInstance()->router;
|
|
|
|
$uri = Factory::getInstance()->uri;
|
2019-02-09 19:22:49 +00:00
|
|
|
$output = Factory::getInstance()->output;
|
2019-03-01 10:25:18 +00:00
|
|
|
$security = Factory::getInstance()->security;
|
2019-07-22 17:53:18 +00:00
|
|
|
$resources = Factory::getInstance()->resources;
|
2019-03-01 10:25:18 +00:00
|
|
|
|
|
|
|
// And start logging the request
|
|
|
|
Logger::newLevel("Routing web request...");
|
|
|
|
|
2019-03-05 10:23:52 +00:00
|
|
|
// First check if a cached page is available
|
|
|
|
$uriString = $uri->uriString();
|
|
|
|
if ($output->getCache($uriString))
|
|
|
|
return true;
|
|
|
|
|
2019-07-22 09:48:45 +00:00
|
|
|
// Send webRequestEvent, if no cache is found
|
2019-07-22 17:53:18 +00:00
|
|
|
/** @var RouteWebRequestEvent $event */
|
2019-07-22 09:48:45 +00:00
|
|
|
$event = Events::fireEvent('routeWebRequestEvent', $uriString);
|
|
|
|
if ($event->isCancelled())
|
|
|
|
return true;
|
|
|
|
|
2019-07-22 17:53:18 +00:00
|
|
|
// Attempt to load a static resource
|
|
|
|
if ($resources->serveResource($uri->segmentArray()))
|
|
|
|
return true;
|
|
|
|
|
2019-03-01 10:25:18 +00:00
|
|
|
// First test for Cross Site Request Forgery
|
|
|
|
try {
|
|
|
|
$security->csrf_verify();
|
|
|
|
} catch (SecurityException $exception) {
|
|
|
|
// If a SecurityException is thrown, first log it
|
|
|
|
Logger::logWarning("SecurityException thrown. Registering listener to verify handler in View");
|
|
|
|
|
|
|
|
// Register a listener
|
|
|
|
Events::addListener([$this, 'callViewEventListener'], 'routerCallViewEvent', Priority::HIGHEST, $exception);
|
|
|
|
}
|
2019-02-09 19:22:49 +00:00
|
|
|
|
|
|
|
// Attempt to load the requested page
|
|
|
|
try {
|
2019-02-15 18:30:11 +00:00
|
|
|
$viewOutput = $router->route($uriString);
|
2019-02-09 19:22:49 +00:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
Logger::logWarning("Requested page not found. Requesting Error/error404 View");
|
|
|
|
$output->setStatusHeader(404);
|
|
|
|
|
2019-03-01 10:25:18 +00:00
|
|
|
// Remove listener so that error pages won't be intercepted
|
|
|
|
Events::removeListener([$this, 'callViewEventListener'], 'routerCallViewEvent',Priority::HIGHEST);
|
|
|
|
|
2019-07-22 17:53:18 +00:00
|
|
|
// Request 404 page
|
2019-02-09 19:22:49 +00:00
|
|
|
try {
|
|
|
|
$viewOutput = $router->route('Error/error404');
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// If still resulting in an error, do something else
|
|
|
|
$viewOutput = 'ERROR 404. Page was not found.';
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Logger::exceptionHandler($e, false);
|
|
|
|
$viewOutput = 'ERROR 404. Page was not found.';
|
|
|
|
}
|
2019-03-01 10:25:18 +00:00
|
|
|
} catch (HaltException $e) {
|
|
|
|
Logger::logWarning("Requested page was denied. Requesting Error/error403 View.");
|
|
|
|
$output->setStatusHeader(403);
|
|
|
|
|
|
|
|
// Remove listener so that error pages won't be intercepted
|
|
|
|
Events::removeListener([$this, 'callViewEventListener'], 'routerCallViewEvent',Priority::HIGHEST);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$viewOutput = $router->route('Error/error403');
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// If still resulting in an error, do something else
|
|
|
|
$viewOutput = 'ERROR 403. Forbidden.';
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Logger::exceptionHandler($e, false);
|
|
|
|
$viewOutput = 'ERROR 403. Forbidden.';
|
|
|
|
}
|
2019-02-09 19:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append the output
|
|
|
|
if (!empty($viewOutput))
|
|
|
|
$output->appendOutput($viewOutput);
|
|
|
|
|
2019-03-01 10:25:18 +00:00
|
|
|
Logger::stopLevel();
|
2019-02-09 19:22:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-02-15 18:30:11 +00:00
|
|
|
|
2019-03-01 10:25:18 +00:00
|
|
|
/**
|
|
|
|
* Listener for routerCallViewEvent
|
|
|
|
*
|
|
|
|
* Fired when a SecurityException is thrown. Verifies if a securityExceptionHandler() method exists.
|
|
|
|
* If not, the calling of the view is cancelled. If yes, the calling of the view depends on the
|
|
|
|
* result of the method
|
|
|
|
*
|
|
|
|
* @param RouterCallViewEvent $event
|
|
|
|
* @param SecurityException $exception
|
|
|
|
*/
|
|
|
|
public function callViewEventListener(RouterCallViewEvent $event, SecurityException $exception)
|
|
|
|
{
|
|
|
|
/** @var RouterCallViewEvent $event */
|
|
|
|
// If the securityExceptionHandler method exists, cancel based on that methods output
|
|
|
|
if (method_exists($event->view, 'securityExceptionHandler'))
|
|
|
|
$event->setCancelled(!$event->view->securityExceptionHandler($exception));
|
|
|
|
|
|
|
|
// If not, cancel it immediately
|
|
|
|
else
|
|
|
|
$event->setCancelled(true);
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:30:11 +00:00
|
|
|
/**
|
|
|
|
* Listener for haltExecutionEvent
|
|
|
|
*
|
|
|
|
* Fired when FuzeWorks halts it's execution. Loads an error 500 page.
|
|
|
|
*
|
|
|
|
* @param $event
|
2019-03-05 10:23:52 +00:00
|
|
|
* @throws EventException
|
|
|
|
* @TODO remove FuzeWorks\Layout dependency
|
2019-02-15 18:30:11 +00:00
|
|
|
*/
|
2019-03-01 10:25:18 +00:00
|
|
|
public function haltEventListener(HaltExecutionEvent $event)
|
2019-02-15 18:30:11 +00:00
|
|
|
{
|
|
|
|
// Dependencies
|
|
|
|
/** @var Output $output */
|
|
|
|
/** @var Router $router */
|
|
|
|
/** @var Event $event */
|
2019-03-05 10:23:52 +00:00
|
|
|
/** @var Layout $layout */
|
2019-02-15 18:30:11 +00:00
|
|
|
$output = Factory::getInstance()->output;
|
|
|
|
$router = Factory::getInstance()->router;
|
2019-03-05 10:23:52 +00:00
|
|
|
$layout = Factory::getInstance()->layouts;
|
2019-02-15 18:30:11 +00:00
|
|
|
|
|
|
|
// Cancel event
|
|
|
|
$event->setCancelled(true);
|
|
|
|
|
2019-03-05 10:23:52 +00:00
|
|
|
// Reset the layout engine
|
|
|
|
$layout->reset();
|
|
|
|
|
2019-03-01 10:25:18 +00:00
|
|
|
// Remove listener so that error pages won't be intercepted
|
|
|
|
Events::removeListener([$this, 'callViewEventListener'], 'routerCallViewEvent',Priority::HIGHEST);
|
|
|
|
|
2019-02-15 18:30:11 +00:00
|
|
|
try {
|
|
|
|
// And handle consequences
|
|
|
|
Logger::logError("Execution halted. Providing error 500 page.");
|
|
|
|
$output->setStatusHeader(500);
|
|
|
|
$viewOutput = $router->route('Error/error500');
|
|
|
|
} catch (Exception $error500Exception) {
|
|
|
|
Logger::exceptionHandler($error500Exception, false);
|
|
|
|
$viewOutput = 'ERROR 500. Page could not be loaded.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally append output and shutdown
|
|
|
|
$output->appendOutput($viewOutput);
|
|
|
|
}
|
2019-03-01 10:25:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listener for layoutLoadEvent
|
|
|
|
*
|
|
|
|
* Assigns variables from the WebComponent to Layout engines.
|
|
|
|
*
|
2019-03-05 10:23:52 +00:00
|
|
|
* @param LayoutLoadEvent $event
|
|
|
|
* @throws ConfigException
|
2019-03-01 10:25:18 +00:00
|
|
|
*/
|
2019-03-05 10:23:52 +00:00
|
|
|
public function layoutLoadEventListener($event)
|
2019-03-01 10:25:18 +00:00
|
|
|
{
|
|
|
|
// Dependencies
|
|
|
|
/** @var Security $security */
|
|
|
|
/** @var Config $config */
|
|
|
|
$security = Factory::getInstance()->security;
|
|
|
|
$config = Factory::getInstance()->config;
|
|
|
|
|
|
|
|
/** @var LayoutLoadEvent $event */
|
|
|
|
$event->assign('csrfHash', $security->get_csrf_hash());
|
|
|
|
$event->assign('csrfTokenName', $security->get_csrf_token_name());
|
|
|
|
$event->assign('siteURL', $config->getConfig('web')->get('base_url'));
|
2019-07-22 09:48:45 +00:00
|
|
|
$event->assign('serverName', $config->getConfig('web')->get('serverName'));
|
2019-03-01 10:25:18 +00:00
|
|
|
|
|
|
|
Logger::logInfo("Assigned variables to TemplateEngine from WebComponent");
|
|
|
|
}
|
2019-02-03 23:10:18 +00:00
|
|
|
}
|