daux.io/libs/Server/Server.php

181 lignes
5.4 KiB
PHP
Brut Vue normale Historique

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\MarkdownPage;
use Todaymade\Daux\Format\HTML\RawPage;
use Todaymade\Daux\Format\HTML\SimplePage;
2015-04-23 00:32:30 +02:00
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Raw;
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-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 .= '/';
}
}
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-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);
$request_type = isset($query['method']) ? $query['method'] : '';
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-04-22 12:23:57 +02:00
switch ($request_type) {
case 'DauxEdit':
if (!$this->daux->options['file_editor']) {
throw new Exception('Editing is currently disabled in config');
}
$content = isset($query['markdown']) ? $query['markdown'] : '';
2015-04-23 00:32:30 +02:00
return $this->saveFile($request, $content);
2015-04-22 12:23:57 +02:00
default:
2015-04-23 00:32:30 +02:00
return $this->getPage($request);
2015-04-22 12:23:57 +02:00
}
}
2015-04-23 00:32:30 +02:00
private function saveFile($request, $content)
{
$file = $this->getFile($request);
2015-04-22 12:23:57 +02:00
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.');
}
2015-04-22 12:23:57 +02:00
2015-04-23 00:32:30 +02:00
if (!$file->write($content)) {
throw new Exception('The file you wish to write to is not writable.');
}
2015-04-22 12:23:57 +02:00
return new SimplePage('Success', 'Successfully Edited');
}
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.');
}
if ($file instanceof Raw) {
return new RawPage($file->getPath());
}
$params = $this->params;
2015-04-22 12:23:57 +02:00
$params['request'] = $request;
$params['file_uri'] = $file->value;
2015-04-23 00:32:30 +02:00
if ($request !== 'index') {
$params['entry_page'] = $file->getFirstPage();
2015-04-23 00:32:30 +02:00
}
2015-04-22 12:23:57 +02:00
return MarkdownPage::fromFile($file, $params);
}
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
}