daux.io/libs/Server/Server.php

163 regels
4.6 KiB
PHP

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
2015-04-22 12:23:57 +02:00
use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\HTML\Generator;
use Todaymade\Daux\Format\HTML\RawPage;
2015-04-22 12:23:57 +02:00
2015-04-23 00:32:30 +02:00
class Server
{
2015-04-22 12:23:57 +02:00
private $daux;
private $params;
private $host;
private $base_url;
2015-04-22 12:23:57 +02:00
2015-07-17 23:38:06 +02:00
/**
* Serve the documentation
*
* @throws Exception
*/
2015-04-23 00:32:30 +02:00
public static function serve()
{
2015-04-22 12:23:57 +02:00
$daux = new Daux(Daux::LIVE_MODE);
2015-04-23 00:32:30 +02:00
try {
2015-04-22 12:23:57 +02:00
$daux->initialize();
$server = new static($daux);
2015-04-23 00:32:30 +02:00
$page = $server->handle($_REQUEST);
} catch (NotFoundException $e) {
http_response_code(404);
2015-04-23 00:32:30 +02:00
$page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams());
2015-04-22 12:23:57 +02:00
}
if ($page instanceof RawPage) {
header('Content-type: ' . MimeType::get($page->getFile()));
// Transfer file in 1024 byte chunks to save memory usage.
if ($fd = fopen($page->getFile(), 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
return;
}
header('Content-type: text/html; charset=utf-8');
echo $page->getContent();
2015-04-22 12:23:57 +02:00
}
2015-04-23 00:32:30 +02:00
public function __construct(Daux $daux)
{
2015-04-22 12:23:57 +02:00
$this->daux = $daux;
$this->host = $_SERVER['HTTP_HOST'];
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$t = strrpos($this->base_url, '/index.php');
if ($t != false) {
$this->base_url = substr($this->base_url, 0, $t);
}
if (substr($this->base_url, -1) !== '/') {
$this->base_url .= '/';
}
}
2015-07-17 23:38:06 +02:00
/**
* @return \Todaymade\Daux\Config
*/
public function getParams()
{
$params = $this->daux->getParams();
$params['index_key'] = 'index';
$params['host'] = $this->host;
$params['base_page'] = $params['base_url'] = '//' . $this->base_url;
if (!$this->daux->options['clean_urls']) {
$params['base_page'] .= 'index.php/';
}
if ($params['image'] !== '') {
$params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
}
$params['theme'] = DauxHelper::getTheme($params, $this->base_url);
return $params;
2015-04-22 12:23:57 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* Handle an incoming request
*
* @param array $query
* @return \Todaymade\Daux\Tree\Entry
* @throws Exception
* @throws NotFoundException
*/
2015-04-23 00:32:30 +02:00
public function handle($query = [])
{
$this->params = $this->getParams();
2015-04-22 12:23:57 +02:00
2015-04-23 21:04:00 +02:00
$request = $this->getRequest();
2015-04-22 12:23:57 +02:00
$request = urldecode($request);
2015-04-23 00:32:30 +02:00
if ($request == 'first_page') {
$request = $this->daux->tree->getFirstPage()->getUri();
2015-04-22 12:23:57 +02:00
}
2015-07-18 00:56:53 +02:00
return $this->getPage($request);
2015-04-22 12:23:57 +02:00
}
2015-07-17 23:38:06 +02:00
/**
* @param string $request
* @return \Todaymade\Daux\Tree\Entry
* @throws NotFoundException
*/
2015-04-23 00:32:30 +02:00
private function getPage($request)
{
$file = DauxHelper::getFile($this->daux->tree, $request);
2015-04-23 00:32:30 +02:00
if ($file === false) {
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}
// TODO :: make it possible to replace the generator in live code
$generator = new Generator($this->daux);
return $generator->generateOne($file, $this->params);
2015-04-22 12:23:57 +02:00
}
2015-04-23 21:04:00 +02:00
public function getRequest()
{
if (isset($_SERVER['PATH_INFO'])) {
$uri = $_SERVER['PATH_INFO'];
} elseif (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
} elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) {
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
if (strncmp($uri, '?/', 2) === 0) {
$uri = substr($uri, 2);
}
$parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0];
if (isset($parts[1])) {
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($_SERVER['QUERY_STRING'], $_GET);
} else {
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
}
$uri = parse_url($uri, PHP_URL_PATH);
} else {
return false;
}
$uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
if ($uri == "") {
$uri = "first_page";
}
return $uri;
}
2015-04-22 12:23:57 +02:00
}