Fix server, now returns files that are in the `docs` folder

This commit is contained in:
Stéphane Goetz 2015-04-27 12:47:10 +02:00
parent 36c663ac41
commit 3e374c7585
6 changed files with 155 additions and 13 deletions

View File

@ -63,6 +63,18 @@ software, even if advised of the possibility of such damage.
*/
if (php_sapi_name() === 'cli-server') {
// This file allows us to emulate Apache's "mod_rewrite"
// functionality from the built-in PHP web server.
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if ($uri !== '/' && file_exists(__DIR__ . $uri)) {
return false;
}
// When the built in server is used
// the script name is the file called
$_SERVER['SCRIPT_NAME'] = '/index.php';
}
require_once("vendor/autoload.php");
\Todaymade\Daux\Daux::initConstants();

View File

@ -12,6 +12,10 @@ class Daux
public $base_url = '';
public $host;
private $docs_path;
/**
* @var Tree\Entry
*/
public $tree;
public $options;
private $mode;

32
libs/RawPage.php Normal file
View File

@ -0,0 +1,32 @@
<?php namespace Todaymade\Daux;
use Todaymade\Daux\Server\MimeType;
class RawPage implements Page
{
private $file;
public function __construct($filename)
{
$this->file = $filename;
}
public function getContent()
{
throw new Exception("you should not use this method to show a raw content");
}
public function display()
{
header('Content-type: ' . MimeType::get($this->file));
// Transfer file in 1024 byte chunks to save memory usage.
if ($fd = fopen($this->file, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
}
}

81
libs/Server/MimeType.php Normal file
View File

@ -0,0 +1,81 @@
<?php namespace Todaymade\Daux\Server;
/**
* Class MimeType
* @package Defr
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
* @see http://www.php.net/mime_content_type
*/
class MimeType
{
/**
* @var array
*/
protected static $mimeTypes = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// Images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// Archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// Audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// Adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// MS Office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// Open Office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
/**
* @param $filename
* @return string
*/
public static function get($filename)
{
$pathInfo = pathinfo($filename);
$extension = strtolower($pathInfo['extension']);
if (array_key_exists($extension, self::$mimeTypes)) {
return self::$mimeTypes[$extension];
} elseif (function_exists('finfo_open') && is_file($filename)) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
} else {
return 'application/octet-stream';
}
}
}

View File

@ -3,8 +3,10 @@
use Todaymade\Daux\Daux;
use Todaymade\Daux\Exception;
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\RawPage;
use Todaymade\Daux\SimplePage;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Raw;
class Server
{
@ -41,8 +43,9 @@ class Server
$request = urldecode($request);
$request_type = isset($query['method']) ? $query['method'] : '';
if ($request == 'first_page') {
$request = $this->daux->tree->first_page->uri;
$request = $this->daux->tree->getFirstPage()->getUri();
}
switch ($request_type) {
case 'DauxEdit':
if (!$this->daux->options['file_editor']) {
@ -115,16 +118,21 @@ class Server
private function getPage($request)
{
$params = $this->params;
$file = $this->getFile($request);
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;
$params['request'] = $request;
$params['file_uri'] = $file->value;
if ($request !== 'index') {
$params['entry_page'] = $file->first_page;
$params['entry_page'] = $file->getFirstPage();
}
return MarkdownPage::fromFile($file, $params);
}

View File

@ -61,22 +61,27 @@ abstract class Entry
$this->index_page = $index_page;
}
/**
* @return Entry
*/
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
foreach ($this->value as $node) {
if ($node instanceof Content && $node->title != 'index') {
$this->first_page = $node;
return $node;
if ($this instanceof Directory) {
foreach ($this->value as $node) {
if ($node instanceof Content && $node->title != 'index') {
$this->first_page = $node;
return $node;
}
}
}
foreach ($this->value as $node) {
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page;
return $page;
foreach ($this->value as $node) {
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page;
return $page;
}
}
}
return false;