8
0
daux.io/libs/Server/Server.php

166 Zeilen
4.9 KiB
PHP

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Exception;
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\SimplePage;
2015-04-23 00:32:30 +02:00
use Todaymade\Daux\Tree\Directory;
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;
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) {
$page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams());
2015-04-22 12:23:57 +02:00
}
$page->display();
}
2015-04-23 00:32:30 +02:00
public function __construct(Daux $daux)
{
2015-04-22 12:23:57 +02:00
$this->daux = $daux;
}
2015-04-23 00:32:30 +02:00
public function handle($query = [])
{
$this->params = $this->daux->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') {
2015-04-22 12:23:57 +02:00
$request = $this->daux->tree->first_page->uri;
}
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 getFile($request)
{
$tree = $this->daux->tree;
$request = explode('/', $request);
foreach ($request as $node) {
// If the element we're in currently is not a
// directory, we failed to find the requested file
if (!$tree instanceof Directory) {
return false;
}
// if the node exists in the current request tree,
// change the $tree variable to reference the new
// node and proceed to the next url part
if (isset($tree->value[$node])) {
$tree = $tree->value[$node];
continue;
}
// At this stage, we're in a directory, but no
// sub-item matches, so the current node must
// be an index page or we failed
if ($node !== 'index' && $node !== 'index.html') {
return false;
}
2015-04-23 00:32:30 +02:00
return $tree->getIndexPage();
}
// If the entry we found is not a directory, we're done
if (!$tree instanceof Directory) {
return $tree;
}
2015-04-23 00:32:30 +02:00
if ($tree->getIndexPage()) {
return $tree->getIndexPage();
}
2015-04-23 00:32:30 +02:00
return false;
2015-04-22 12:23:57 +02:00
}
2015-04-23 00:32:30 +02:00
private function getPage($request)
{
2015-04-22 12:23:57 +02:00
$params = $this->params;
2015-04-23 00:32:30 +02:00
$file = $this->getFile($request);
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
$params['request'] = $request;
$params['file_uri'] = $file->value;
2015-04-23 00:32:30 +02:00
if ($request !== 'index') {
$params['entry_page'] = $file->first_page;
}
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
}