Hello command line (file extension update)

This commit is contained in:
markseu 2013-08-04 11:42:21 +02:00
parent 968213b658
commit 026f23139c
8 changed files with 116 additions and 89 deletions

View file

@ -5,7 +5,7 @@
// Yellow main class
class Yellow
{
const Version = "0.1.10";
const Version = "0.1.11";
var $page; //current page data
var $pages; //current page tree from file system
var $toolbox; //toolbox with helpers
@ -26,7 +26,6 @@ class Yellow
$this->config->setDefault("template", "default");
$this->config->setDefault("style", "default");
$this->config->setDefault("parser", "markdown");
$this->config->setDefault("yellowVersion", Yellow::Version);
$this->config->setDefault("serverName", $this->toolbox->getServerName());
$this->config->setDefault("serverBase", $this->toolbox->getServerBase());
$this->config->setDefault("styleLocation", "/media/styles/");
@ -57,21 +56,26 @@ class Yellow
{
$this->toolbox->timerStart($time);
ob_start();
$statusCode = 0;
$serverName = $this->config->get("serverName");
$serverBase = $this->config->get("serverBase");
$location = $this->getRelativeLocation($serverBase);
$fileName = $this->getContentFileName($location);
$statusCode = 0;
$this->page = new Yellow_Page($this, $location);
foreach($this->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onRequest"))
{
$this->pages->requestHandler = $key;
$statusCode = $value["obj"]->onRequest($serverName, $serverBase, $location, $fileName);
if($statusCode) break;
if($statusCode != 0) break;
}
}
if($statusCode == 0) $statusCode = $this->processRequest($serverName, $serverBase, $location, $fileName, true, $statusCode);
if($statusCode == 0)
{
$this->pages->requestHandler = "core";
$statusCode = $this->processRequest($serverName, $serverBase, $location, $fileName, true, $statusCode);
}
if($statusCodeRequest > 200) $this->page->error($statusCodeRequest, "Request error");
if($this->isRequestError())
{
@ -88,13 +92,14 @@ class Yellow
// Process request
function processRequest($serverName, $serverBase, $location, $fileName, $cacheable, $statusCode)
{
$handler = $this->getRequestHandler();
if($statusCode == 0)
{
if(is_readable($fileName))
{
$statusCode = 200;
$fileName = $this->readPage($serverBase, $location, $fileName, $cacheable, $statusCode);
if($this->page->isExisting("redirect") && $cacheable)
if($this->page->isExisting("redirect") && $handler=="core")
{
$statusCode = 301;
$locationHeader = $this->toolbox->getHttpLocationHeader($serverName, $serverBase, $this->page->get("redirect"));
@ -116,18 +121,19 @@ class Yellow
$fileName = $this->readPage($serverBase, $location, $fileName, $cacheable, $statusCode);
}
if($this->page->statusCode != 0) $statusCode = $this->sendPage();
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequest base:$serverBase file:$fileName<br>\n";
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequest handler:$handler base:$serverBase file:$fileName<br>\n";
return $statusCode;
}
// Process request with error
function processRequestError()
{
$handler = $this->getRequestHandler();
$serverBase = $this->pages->serverBase;
$fileName = $this->readPage($serverBase, $this->page->location, $this->page->fileName, $this->page->cacheable,
$this->page->statusCode, $this->page->get("pageError"));
$statusCode = $this->sendPage();
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequestError base:$serverBase file:$fileName<br>\n";
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequestError handler:$handler base:$serverBase file:$fileName<br>\n";
return $statusCode;
}
@ -199,6 +205,12 @@ class Yellow
}
}
// Return name of request handler
function getRequestHandler()
{
return $this->pages->requestHandler;
}
// Check for request error
function isRequestError()
{
@ -391,7 +403,7 @@ class Yellow_Page
{
if(method_exists($value["obj"], "onParseContent")) $text = $value["obj"]->onParseContent($text, $this->statusCode);
}
$this->setContent($text);
$this->parser->textHtml = $text;
if(!$this->isExisting("description"))
{
$this->set("description", $this->yellow->toolbox->createTextDescription($this->getContent(), 150));
@ -449,18 +461,12 @@ class Yellow_Page
{
return $this->getHtml("title");
}
// Set page content, HTML encoded
function setContent($html)
{
$this->parser->html = $html;
}
// Return page content, HTML encoded
function getContent()
{
$this->parseContent();
return $this->parser->html;
return $this->parser->textHtml;
}
// Return absolute page location
@ -662,10 +668,11 @@ class Yellow_PageCollection extends ArrayObject
// Yellow page tree from file system
class Yellow_Pages
{
var $yellow; //access to API
var $pages; //scanned pages
var $serverBase; //requested server base
var $snippetArgs; //requested snippet arguments
var $yellow; //access to API
var $pages; //scanned pages
var $requestHandler; //request handler
var $serverBase; //requested server base
var $snippetArgs; //requested snippet arguments
function __construct($yellow)
{
@ -923,14 +930,14 @@ class Yellow_Toolbox
{
for($i=1; $i<count($tokens)-1; ++$i)
{
$tokenFound = $tokens[$i];
if(self::normaliseName($tokens[$i]) != $tokens[$i]) $invalid = true;
$regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $tokens[$i])."$/";
$token = $tokens[$i];
if(self::normaliseName($token) != $token) $invalid = true;
$regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach(self::getDirectoryEntries($path, $regex) as $entry)
{
if(self::normaliseName($entry) == $tokens[$i]) { $tokenFound = $entry; break; }
if(self::normaliseName($entry) == $tokens[$i]) { $token = $entry; break; }
}
$path .= "$tokenFound/";
$path .= "$token/";
}
if($path == $pathBase.$pathHome) $invalid = true;
} else {
@ -939,14 +946,14 @@ class Yellow_Toolbox
}
if($tokens[$i] != "")
{
$tokenFound = $tokens[$i];
if(self::normaliseName($tokens[$i]) != $tokens[$i]) $invalid = true;
$regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $tokens[$i]).$fileExtension."$/";
$token = $tokens[$i].$fileExtension;
if(self::normaliseName($token) != $token) $invalid = true;
$regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach(self::getDirectoryEntries($path, $regex, false, false) as $entry)
{
if(self::normaliseName($entry, true) == $tokens[$i]) { $tokenFound = $entry; break; }
if(self::normaliseName($entry, true) == $tokens[$i]) { $token = $entry; break; }
}
$path .= $tokenFound;
$path .= $token;
} else {
$path .= $fileDefault;
}
@ -970,7 +977,7 @@ class Yellow_Toolbox
{
if(preg_match("/^[\d\-\_\.]+(.*)$/", $text, $matches)) $text = $matches[1];
if($removeExtension) $text = ($pos = strrposu($text, '.')) ? substru($text, 0, $pos) : $text;
$text = preg_replace("/[^\pL\d\-\_]/u", "-", $text);
$text = preg_replace("/[^\pL\d\-\_\.]/u", "-", $text);
return $text;
}
@ -1061,7 +1068,7 @@ class Yellow_Toolbox
if($mkdir)
{
$path = dirname($fileName);
if(!empty($path) && !is_dir($path)) mkdir($path, 0777, true);
if(!empty($path) && !is_dir($path)) @mkdir($path, 0777, true);
}
$fileHandle = @fopen($fileName, "w");
if($fileHandle)
@ -1079,7 +1086,7 @@ class Yellow_Toolbox
if($mkdir)
{
$path = dirname($fileNameDest);
if(!empty($path) && !is_dir($path)) mkdir($path, 0777, true);
if(!empty($path) && !is_dir($path)) @mkdir($path, 0777, true);
}
return @copy($fileNameSource, $fileNameDest);
}
@ -1424,7 +1431,7 @@ class Yellow_Plugins
{
global $yellow;
require_once("core_markdown.php");
require_once("core_rawhtml.php");
require_once("core_plaintext.php");
require_once("core_commandline.php");
require_once("core_webinterface.php");
foreach($yellow->toolbox->getDirectoryEntries($yellow->config->get("pluginDir"), "/.*\.php/", true, false) as $entry)

View file

@ -5,7 +5,7 @@
// Command line core plugin
class Yellow_Commandline
{
const Version = "0.1.3";
const Version = "0.1.4";
var $yellow; //access to API
// Initialise plugin
@ -141,31 +141,34 @@ class Yellow_Commandline
$statusCode = $this->yellow->request();
if($statusCode != 404)
{
$ok = false;
$fileOk = true;
$modified = strtotime($this->yellow->page->getHeader("Last-Modified"));
if(preg_match("/^(\w+)\/(\w+)/", $this->yellow->page->getHeader("Content-Type"), $matches))
{
$contentType = "$matches[1]/$matches[2]";
$locationExtension = $this->getStaticLocation($location, ".$matches[2]");
}
if(empty($contentType) || $contentType=="text/html")
list($contentType, $contentEncoding) = explode(';', $this->yellow->page->getHeader("Content-Type"), 2);
$staticLocation = $this->getStaticLocation($location, $contentType);
if($location == $staticLocation)
{
$fileName = $this->getStaticFileName($location, $path);
$fileData = ob_get_contents();
if($statusCode == 301) $fileData = $this->getStaticRedirect($this->yellow->page->getHeader("Location"));
$ok = $this->makeStaticFile($fileName, $fileData, $modified);
$fileOk = $this->makeStaticFile($fileName, $fileData, $modified);
} else {
$fileName = $this->getStaticFileName($location, $path);
$fileData = $this->getStaticRedirect("http://$serverName$serverBase$locationExtension");
$ok = $this->makeStaticFile($fileName, $fileData, $modified);
if($ok)
if(!$this->yellow->toolbox->isFileLocation($location))
{
$fileName = $this->getStaticFileName($locationExtension, $path);
$fileData = ob_get_contents();
$ok = $this->makeStaticFile($fileName, $fileData, $modified);
$fileName = $this->getStaticFileName($location, $path);
$fileData = $this->getStaticRedirect("http://$serverName$serverBase$staticLocation");
$fileOk = $this->makeStaticFile($fileName, $fileData, $modified);
if($fileOk)
{
$fileName = $this->getStaticFileName($staticLocation, $path);
$fileData = ob_get_contents();
$fileOk = $this->makeStaticFile($fileName, $fileData, $modified);
}
} else {
$statusCode = 500;
$this->yellow->page->error($statusCode, "Invalid file name for type '$contentType'!");
}
}
if(!$ok)
if(!$fileOk)
{
$statusCode = 500;
$this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
@ -186,10 +189,8 @@ class Yellow_Commandline
$statusCode = $this->yellow->request($statusCodeRequest);
if($statusCode == $statusCodeRequest)
{
$fileData = ob_get_contents();
$modified = strtotime($this->yellow->page->getHeader("Last-Modified"));
$ok = $this->makeStaticFile($fileName, $fileData, $modified);
if(!$ok)
if(!$this->makeStaticFile($fileName, ob_get_contents(), $modified))
{
$statusCode = 500;
$this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
@ -213,6 +214,28 @@ class Yellow_Commandline
$this->yellow->toolbox->modifyFile($fileNameDest, filemtime($fileNameSource));
}
// Return static location corresponding to content type
function getStaticLocation($location, $contentType)
{
$extension = ($pos = strrposu($location, '.')) ? substru($location, $pos) : "";
if($contentType == "text/html")
{
if($this->yellow->toolbox->isFileLocation($location))
{
if(!empty($extension) && $extension!=".html") $location .= ".html";
}
} else {
if($this->yellow->toolbox->isFileLocation($location))
{
if(empty($extension)) $location .= ".unknown";
} else {
if(preg_match("/^(\w+)\/(\w+)/", $contentType, $matches)) $extension = ".$matches[2]";
$location .= "index$extension";
}
}
return $location;
}
// Return static file name from location
function getStaticFileName($location, $path)
{
@ -224,13 +247,6 @@ class Yellow_Commandline
return $fileName;
}
// Return static location with file extension
function getStaticLocation($location, $extension)
{
if(!$this->yellow->toolbox->isFileLocation($location)) $location .= "index";
return $location.$extension;
}
// Return static redirect data
function getStaticRedirect($url)
{

View file

@ -7,7 +7,7 @@ class Yellow_Markdown
{
const Version = "0.1.5";
var $markdown; //markdown parser
var $html; //generated HTML
var $textHtml; //generated text (HTML format)
// Initialise plugin
function initPlugin($yellow)
@ -18,7 +18,7 @@ class Yellow_Markdown
// Parse text
function parse($text)
{
return $this->html = $this->markdown->transform($text);
return $this->textHtml = $this->markdown->transform($text);
}
}

21
system/core/core_plaintext.php Executable file
View file

@ -0,0 +1,21 @@
<?php
// Copyright (c) 2013 Datenstrom, http://datenstrom.se
// This file may be used and distributed under the terms of the public license.
// Plain text parser core plugin
class Yellow_Plaintext
{
const Version = "0.1.1";
var $text; //plain text
var $textHtml; //generated text (HTML format)
// Parse text, dummy transformation
function parse($text)
{
$this->text = $text;
return $textHtml;
}
}
$yellow->registerPlugin("plaintext", "Yellow_Plaintext", Yellow_Plaintext::Version);
?>

View file

@ -1,19 +0,0 @@
<?php
// Copyright (c) 2013 Datenstrom, http://datenstrom.se
// This file may be used and distributed under the terms of the public license.
// Raw HTML parser core plugin
class Yellow_RawHtml
{
const Version = "0.1.1";
var $html; //generated HTML
// Parse text, dummy transformation
function parse($text)
{
return $this->html = $text;
}
}
$yellow->registerPlugin("rawhtml", "Yellow_RawHtml", Yellow_RawHtml::Version);
?>

View file

@ -5,7 +5,7 @@
// Web interface core plugin
class Yellow_Webinterface
{
const Version = "0.1.6";
const Version = "0.1.7";
var $yellow; //access to API
var $users; //web interface users
var $activeLocation; //web interface location? (boolean)
@ -182,7 +182,7 @@ class Yellow_Webinterface
function checkUserPermissions($location, $fileName)
{
$path = dirname($fileName);
return is_dir($path);
return is_dir($path) && strlenu(basename($fileName))<128;
}
// Check if web interface location

5
system/snippets/content.php Executable file
View file

@ -0,0 +1,5 @@
<?php list($name, $title, $text) = $yellow->getSnippetArgs(); ?>
<div class="content">
<h1><?php echo $title ?></h1>
<?php echo $text ?>
</div>

View file

@ -1,7 +1,4 @@
<?php $yellow->snippet("header") ?>
<?php $yellow->snippet("navigation") ?>
<div class="content">
<h1><?php echo $yellow->page->getTitle() ?></h1>
<?php echo $yellow->page->getContent() ?>
</div>
<?php $yellow->snippet("content", $yellow->page->getTitle(), $yellow->page->getContent()) ?>
<?php $yellow->snippet("footer") ?>