src/AppserverIo/WebServer/Modules/DirectoryModule.php
<?php
/**
* \AppserverIo\WebServer\Modules\DirectoryModule
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* PHP version 5
*
* @author Johann Zelger <jz@appserver.io>
* @copyright 2015 TechDivision GmbH <info@appserver.io>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/appserver-io/webserver
* @link http://www.appserver.io/
*/
namespace AppserverIo\WebServer\Modules;
use AppserverIo\Psr\HttpMessage\Protocol;
use AppserverIo\Psr\HttpMessage\RequestInterface;
use AppserverIo\Psr\HttpMessage\ResponseInterface;
use AppserverIo\WebServer\Interfaces\HttpModuleInterface;
use AppserverIo\Http\HttpResponseStates;
use AppserverIo\Server\Dictionaries\ModuleHooks;
use AppserverIo\Server\Exceptions\ModuleException;
use AppserverIo\Server\Interfaces\RequestContextInterface;
use AppserverIo\Server\Interfaces\ServerContextInterface;
use AppserverIo\Server\Dictionaries\ServerVars;
/**
* Class DirectoryModule
*
* @author Johann Zelger <jz@appserver.io>
* @copyright 2015 TechDivision GmbH <info@appserver.io>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://github.com/appserver-io/webserver
* @link http://www.appserver.io/
*/
class DirectoryModule implements HttpModuleInterface
{
/**
* Defines the module name
*
* @var string
*/
const MODULE_NAME = 'directory';
/**
* Holds the server context instance
*
* @var \AppserverIo\Server\Interfaces\ServerContextInterface
*/
protected $serverContext;
/**
* Returns the request instance
*
* @return \AppserverIo\Psr\HttpMessage\RequestInterface The request instance
*/
public function getRequest()
{
return $this->request;
}
/**
* Returns the response instance
*
* @return \AppserverIo\Psr\HttpMessage\ResponseInterface The response instance;
*/
public function getResponse()
{
return $this->response;
}
/**
* Initiates the module
*
* @param \AppserverIo\Server\Interfaces\ServerContextInterface $serverContext The server's context instance
*
* @return bool
* @throws \AppserverIo\Server\Exceptions\ModuleException
*/
public function init(ServerContextInterface $serverContext)
{
$this->serverContext = $serverContext;
// save directory index as array got space separated from config
$this->directoryIndex = explode(' ', $serverContext->getServerConfig()->getDirectoryIndex());
}
/**
* Return's the server context instance
*
* @return \AppserverIo\Server\Interfaces\ServerContextInterface
*/
public function getServerContext()
{
return $this->serverContext;
}
/**
* Implement's module logic for given hook
*
* @param \AppserverIo\Psr\HttpMessage\RequestInterface $request A request object
* @param \AppserverIo\Psr\HttpMessage\ResponseInterface $response A response object
* @param \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext A requests context instance
* @param int $hook The current hook to process logic for
*
* @return bool
* @throws \AppserverIo\Server\Exceptions\ModuleException
*/
public function process(RequestInterface $request, ResponseInterface $response, RequestContextInterface $requestContext, $hook)
{
// In php an interface is, by definition, a fixed contract. It is immutable.
// So we have to declair the right ones afterwards...
/**
* @var $request \AppserverIo\Psr\HttpMessage\RequestInterface
*/
/**
* @var $response \AppserverIo\Psr\HttpMessage\ResponseInterface
*/
// if false hook is comming do nothing
if (ModuleHooks::REQUEST_POST !== $hook) {
return;
}
// set req and res object internally
$this->request = $request;
$this->response = $response;
// get document root
$documentRoot = $requestContext->getServerVar(ServerVars::DOCUMENT_ROOT);
// get url
$url = parse_url($requestContext->getServerVar(ServerVars::X_REQUEST_URI), PHP_URL_PATH);
// get query string with asterisk
$queryString = strstr($requestContext->getServerVar(ServerVars::X_REQUEST_URI), '?');
// get read path to requested uri
$realPath = $documentRoot . $url;
// check if it's a dir
if (is_dir($realPath) || $url === '/') {
// check if uri has trailing slash
if (substr($url, - 1) !== '/') {
// set enhance uri with trailing slash to response
$response->addHeader(Protocol::HEADER_LOCATION, $url . '/' . $queryString);
// send redirect status
$response->setStatusCode(301);
// set response state to be dispatched after this without calling other modules process
$response->setState(HttpResponseStates::DISPATCH);
} else {
// check directory index definitions
foreach ($this->getDirectoryIndex() as $index) {
// check if defined index files are found in directory
if (is_file($realPath . $index)) {
// reset uri with indexed filename
$requestContext->setServerVar(ServerVars::X_REQUEST_URI, $url . $index . $queryString);
// break out if index file was found
return true;
}
}
}
}
return true;
}
/**
* Return's the directory index as array
*
* @return array
*/
public function getDirectoryIndex()
{
return $this->directoryIndex;
}
/**
* Return's an array of module names which should be executed first
*
* @return array The array of module names
*/
public function getDependencies()
{
return array();
}
/**
* Returns the module name
*
* @return string The module name
*/
public function getModuleName()
{
return self::MODULE_NAME;
}
/**
* Prepares the module for upcoming request in specific context
*
* @return bool
* @throws \AppserverIo\Server\Exceptions\ModuleException
*/
public function prepare()
{
// nothing to prepare for this module
}
}