Better dynamic content handling

This commit is contained in:
markseu 2013-06-27 19:00:03 +02:00 committed by Dominik Wilkowski
parent c956825e24
commit cd51c357bb
4 changed files with 123 additions and 88 deletions

View file

@ -15,9 +15,10 @@ With Yellow you don't get a lot of extra stuff, there are [Yellow extensions on
How to make a website? How to make a website?
---------------------- ----------------------
You already have everything you need, start by editing the default pages. You already have everything you need, start by editing the default pages.
For more information please see [Yellow documentation](https://github.com/markseu/yellowcms-extensions/tree/master/documentation). For more information please see [Yellow documentation](https://github.com/markseu/yellowcms-extensions/blob/master/documentation/README.md).
Need help? Have a question? Need help? Have a question?
--------------------------- ---------------------------
Looking for something, then get in touch with others.
Visit [Yellow on Reddit](http://www.reddit.com/r/yellowcms/), Visit [Yellow on Reddit](http://www.reddit.com/r/yellowcms/),
follow [Yellow on Twitter](https://twitter.com/yellowcms). follow [Yellow on Twitter](https://twitter.com/yellowcms).

View file

@ -2,7 +2,7 @@
Title: Home Title: Home
--- ---
Yes, your Yellow installation works! Yes, your Yellow installation works!
You can now [edit this page](@baselocation/wiki/) or use your favorite text editor. You can now [edit this page](@baselocation/edit/) or use your favorite text editor.
See [Yellow documentation](https://github.com/markseu/yellowcms-extensions/tree/master/documentation) for more. Information for web designers are in the [documentation](https://github.com/markseu/yellowcms-extensions/blob/master/documentation/README.md).
Have fun making your website. Have fun making your website.

View file

@ -5,7 +5,7 @@
// Yellow main class // Yellow main class
class Yellow class Yellow
{ {
const Version = "0.1.4"; const Version = "0.1.5";
var $page; //current page data var $page; //current page data
var $pages; //current page tree from file system var $pages; //current page tree from file system
var $toolbox; //toolbox with helpers var $toolbox; //toolbox with helpers
@ -62,6 +62,7 @@ class Yellow
// Process request // Process request
function processRequest() function processRequest()
{ {
ob_start();
$statusCode = 0; $statusCode = 0;
$baseLocation = $this->config->get("baseLocation"); $baseLocation = $this->config->get("baseLocation");
$location = $this->getRelativeLocation($baseLocation); $location = $this->getRelativeLocation($baseLocation);
@ -76,8 +77,9 @@ class Yellow
} }
if($statusCode == 0) $statusCode = $this->processRequestFile($baseLocation, $location, $fileName, $statusCode); if($statusCode == 0) $statusCode = $this->processRequestFile($baseLocation, $location, $fileName, $statusCode);
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequest status:$statusCode location:$location<br>\n"; if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequest status:$statusCode location:$location<br>\n";
ob_end_flush();
} }
// Process request for a file // Process request for a file
function processRequestFile($baseLocation, $location, $fileName, $statusCode, $cache = true) function processRequestFile($baseLocation, $location, $fileName, $statusCode, $cache = true)
{ {
@ -85,91 +87,104 @@ class Yellow
{ {
if(is_readable($fileName)) if(is_readable($fileName))
{ {
$time = gmdate("D, d M Y H:i:s", filemtime($fileName))." GMT"; $statusCode = 200;
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && $_SERVER["HTTP_IF_MODIFIED_SINCE"]==$time && $cache) $fileName = $this->readPage($baseLocation, $location, $fileName, $statusCode);
{
$statusCode = 304;
$this->sendStatus($statusCode);
} else {
$statusCode = 200;
header("Content-Type: text/html; charset=UTF-8");
if($cache)
{
header("Last-Modified: ".$time);
} else {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");
}
$fileHandle = @fopen($fileName, "r");
if($fileHandle)
{
$fileData = fread($fileHandle, filesize($fileName));
fclose($fileHandle);
} else {
die("Server problem: Can't read file '$fileName'!");
}
}
} else { } else {
if($this->toolbox->isFileLocation($location) && is_dir($this->getContentDirectory("$location/"))) if($this->toolbox->isFileLocation($location) && is_dir($this->getContentDirectory("$location/")))
{ {
$statusCode = 301; $statusCode = 301;
$serverName = $this->config->get("serverName"); $this->sendStatus($statusCode, "Location: http://".$this->config->get("serverName")."$baseLocation$location/");
$this->sendStatus($statusCode, "Location: http://$serverName$baseLocation$location/");
} else { } else {
$statusCode = 404; $statusCode = 404;
$fileName = $this->readPage($baseLocation, $location, $fileName, $statusCode);
} }
} }
} else if($statusCode >= 400) {
$fileName = $this->readPage($baseLocation, $location, $fileName, $statusCode);
} }
if($statusCode >= 400) if(is_object($this->page))
{ {
header($this->toolbox->getHttpStatusFormated($statusCode)); $statusCode = $this->sendPage($statusCode, $cache);
header("Content-Type: text/html; charset=UTF-8"); }
$fileName = strreplaceu("(.*)", $statusCode, $this->config->get("configDir").$this->config->get("errorPageFile")); if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequestFile base:$baseLocation file:$fileName<br>\n";
$fileHandle = @fopen($fileName, "r"); return $statusCode;
if($fileHandle) }
// Read page
function readPage($baseLocation, $location, $fileName, $statusCode)
{
if($statusCode >= 400) $fileName = strreplaceu("(.*)", $statusCode, $this->config->get("configDir").$this->config->get("errorPageFile"));
$fileHandle = @fopen($fileName, "r");
if($fileHandle)
{
$fileData = fread($fileHandle, filesize($fileName));
fclose($fileHandle);
} else {
die("Server problem: Can't read file '$fileName'!");
}
$this->pages = new Yellow_Pages($baseLocation, $this->toolbox, $this->config, $this->plugins);
$this->page = new Yellow_Page($baseLocation, $location, $fileName, $fileData, $this->pages, true);
$this->text->setLanguage($this->page->get("language"));
return $fileName;
}
// Send page response
function sendPage($statusCode, $cache)
{
$fileNameStyle = $this->config->get("styleDir").$this->page->get("style").".css";
if(!is_file($fileNameStyle)) die("Style '".$this->page->get("style")."' does not exist!");
$fileNameTemplate = $this->config->get("templateDir").$this->page->get("template").".php";
if(!is_file($fileNameTemplate)) die("Template '".$this->page->get("template")."' does not exist!");
global $yellow;
require($fileNameTemplate);
$pageHeaders["last-modified"] = $this->page->getModified(true);
$pageHeaders["content-type"] = "text/html; charset=UTF-8";
foreach(headers_list() as $header)
{
$tokens = explode(':', $header, 2);
$key = strtoloweru($tokens[0]);
if(!is_null($pageHeaders[$key]))
{ {
$fileData = fread($fileHandle, filesize($fileName)); $pageHeaders[$key] = trim($tokens[1]);
fclose($fileHandle); header_remove($tokens[0]);
} else {
die("Configuration problem: Can't open file '$fileName'!");
} }
} }
if(!empty($fileData)) $this->sendPage($baseLocation, $location, $fileName, $fileData, $statusCode); if($statusCode==200 && $cache && isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) &&
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequestFile base:$baseLocation file:$fileName<br>\n"; $_SERVER["HTTP_IF_MODIFIED_SINCE"]==$pageHeaders["last-modified"])
{
$statusCode = 304;
$this->sendStatus($statusCode);
} else {
header("Content-Type: ".$pageHeaders["content-type"]);
if($cache)
{
header("Last-Modified:". $pageHeaders["last-modified"]);
} else {
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Expires: 0");
}
}
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::sendFile template:$fileNameTemplate style:$fileNameStyle<br>\n";
return $statusCode; return $statusCode;
} }
// Send status response // Send status response
function sendStatus($statusCode, $text = "") function sendStatus($statusCode, $text = "")
{ {
header($this->toolbox->getHttpStatusFormated($statusCode)); header($this->toolbox->getHttpStatusFormatted($statusCode));
if(!empty($text)) header($text); if(!empty($text)) header($text);
} }
// Send page response
function sendPage($baseLocation, $location, $fileName, $fileData, $statusCode)
{
$this->pages = new Yellow_Pages($baseLocation, $this->toolbox, $this->config, $this->plugins);
$this->page = new Yellow_Page($baseLocation, $location, $fileName, $fileData, $this->pages, true);
$this->text->setLanguage($this->page->get("language"));
$fileName = $this->config->get("styleDir").$this->page->get("style").".css";
if(!is_file($fileName)) die("Style '".$this->page->get("style")."' does not exist!");
$fileName = $this->config->get("templateDir").$this->page->get("template").".php";
if(!is_file($fileName)) die("Template '".$this->page->get("template")."' does not exist!");
global $yellow;
require($fileName);
}
// Execute a template snippet // Execute a template snippet
function snippet($name, $args = NULL) function snippet($name, $args = NULL)
{ {
$this->page->args = func_get_args(); $this->page->args = func_get_args();
$fileName = $this->config->get("snippetDir")."$name.php"; $fileNameSnippet = $this->config->get("snippetDir")."$name.php";
if(!is_file($fileName)) die("Snippet '$name' does not exist!"); if(!is_file($fileNameSnippet)) die("Snippet '$name' does not exist!");
global $yellow; global $yellow;
require($fileName); require($fileNameSnippet);
} }
// Return template snippet arguments // Return template snippet arguments
@ -211,7 +226,7 @@ class Yellow
return $this->toolbox->findFileFromLocation($location, return $this->toolbox->findFileFromLocation($location,
$this->config->get("contentDir"), $this->config->get("contentHomeDir"), "", ""); $this->config->get("contentDir"), $this->config->get("contentHomeDir"), "", "");
} }
// Execute a plugin command // Execute a plugin command
function plugin($name, $args = NULL) function plugin($name, $args = NULL)
{ {
@ -237,13 +252,13 @@ class Yellow_Page
var $fileName; //content file name var $fileName; //content file name
var $parser; //content parser var $parser; //content parser
var $metaData; //meta data of page var $metaData; //meta data of page
var $rawData; //raw data of page (unparsed) var $rawData; //raw data of page
var $rawTextOffsetBytes; //raw text of page (unparsed) var $rawTextOffsetBytes; //raw text of page
var $args; //arguments for template snippet var $args; //arguments for template snippet
var $pages; //access to file system var $pages; //access to file system
var $active; //page is active? var $active; //page is active? (boolean)
var $hidden; //page is hidden in navigation? var $hidden; //page is hidden in navigation? (boolean)
function __construct($baseLocation, $location, $fileName, $rawData, $pages, $parseContent = false) function __construct($baseLocation, $location, $fileName, $rawData, $pages, $parseContent = false)
{ {
$this->baseLocation = $baseLocation; $this->baseLocation = $baseLocation;
@ -354,16 +369,18 @@ class Yellow_Page
return substrb($this->rawData, $this->rawTextOffsetBytes); return substrb($this->rawData, $this->rawTextOffsetBytes);
} }
// Return absolut page location // Return absolute page location
function getLocation() function getLocation()
{ {
return $this->baseLocation.$this->location; return $this->baseLocation.$this->location;
} }
// Return page modification time (Unix time UTC) // Return page modification time, Unix time
function getModified() function getModified($httpFormat = false)
{ {
return filemtime($this->fileName); $modified = is_readable($this->fileName) ? filemtime($this->fileName) : "";
if($this->IsExisting("modified")) $modified = strtotime($this->get("modified"));
return $httpFormat ? $this->pages->toolbox->getHttpTimeFormatted($modified) : $modified;
} }
// Return child pages relative to current page // Return child pages relative to current page
@ -477,7 +494,7 @@ class Yellow_PageCollection extends ArrayObject
return $this->paginationCount; return $this->paginationCount;
} }
// Return absolut location for a page in pagination // Return absolute location for a page in pagination
function getLocationPage($pageNumber) function getLocationPage($pageNumber)
{ {
if($pageNumber>=1 && $pageNumber<=$this->paginationCount) if($pageNumber>=1 && $pageNumber<=$this->paginationCount)
@ -488,7 +505,7 @@ class Yellow_PageCollection extends ArrayObject
return $location; return $location;
} }
// Return absolut location for previous page in pagination // Return absolute location for previous page in pagination
function getLocationPrevious() function getLocationPrevious()
{ {
$pageNumber = $this->paginationPage; $pageNumber = $this->paginationPage;
@ -496,13 +513,21 @@ class Yellow_PageCollection extends ArrayObject
return $this->getLocationPage($pageNumber); return $this->getLocationPage($pageNumber);
} }
// Return absolut location for next page in pagination // Return absolute location for next page in pagination
function getLocationNext() function getLocationNext()
{ {
$pageNumber = $this->paginationPage; $pageNumber = $this->paginationPage;
$pageNumber = ($pageNumber>=1 && $pageNumber<$this->paginationCount) ? $pageNumber+1 : 0; $pageNumber = ($pageNumber>=1 && $pageNumber<$this->paginationCount) ? $pageNumber+1 : 0;
return $this->getLocationPage($pageNumber); return $this->getLocationPage($pageNumber);
} }
// Return last modification time for page collection, Unix time
function getModified($httpFormat = false)
{
$modified = 0;
foreach($this->getIterator() as $page) $modified = max($modified, $page->getModified());
return $httpFormat ? $this->toolbox->getHttpTimeFormatted($modified) : $modified;
}
// Check if there is an active pagination // Check if there is an active pagination
function isPagination() function isPagination()
@ -789,7 +814,7 @@ class Yellow_Toolbox
} }
// Return human readable HTTP server status // Return human readable HTTP server status
static function getHttpStatusFormated($statusCode) static function getHttpStatusFormatted($statusCode)
{ {
switch($statusCode) switch($statusCode)
{ {
@ -800,10 +825,17 @@ class Yellow_Toolbox
case 401: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Unauthorised"; break; case 401: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Unauthorised"; break;
case 404: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Not found"; break; case 404: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Not found"; break;
case 424: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Does not exist"; break; case 424: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Does not exist"; break;
case 500: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Server error"; break;
default: die("Unknown HTTP status $statusCode!"); default: die("Unknown HTTP status $statusCode!");
} }
return $text; return $text;
} }
// Return human readable HTTP time
static function getHttpTimeFormatted($timestamp)
{
return gmdate("D, d M Y H:i:s", $timestamp)." GMT";
}
// Return files and directories // Return files and directories
static function getDirectoryEntries($path, $regex = "/.*/", $sort = false, $directories = true) static function getDirectoryEntries($path, $regex = "/.*/", $sort = false, $directories = true)
@ -1158,8 +1190,8 @@ class Yellow_Plugins
require_once("core_webinterface.php"); require_once("core_webinterface.php");
foreach($yellow->toolbox->getDirectoryEntries($yellow->config->get("pluginDir"), "/.*\.php/", true, false) as $entry) foreach($yellow->toolbox->getDirectoryEntries($yellow->config->get("pluginDir"), "/.*\.php/", true, false) as $entry)
{ {
$fileName = $yellow->config->get("pluginDir")."/$entry"; $fileNamePlugin = $yellow->config->get("pluginDir")."/$entry";
require_once($fileName); require_once($fileNamePlugin);
} }
foreach($this->plugins as $key=>$value) foreach($this->plugins as $key=>$value)
{ {

View file

@ -5,18 +5,18 @@
// Web interface core plugin // Web interface core plugin
class Yellow_Webinterface class Yellow_Webinterface
{ {
const Version = "0.1.1"; const Version = "0.1.2";
var $yellow; //access to API var $yellow; //access to API
var $users; //web interface users var $users; //web interface users
var $activeLocation; //web interface location? (boolean) var $activeLocation; //web interface location? (boolean)
var $activeUserFail; //web interface login failed (boolean) var $activeUserFail; //web interface login failed? (boolean)
var $activeUserEmail; //web interface user currently logged in var $activeUserEmail; //web interface user currently logged in
// Initialise plugin // Initialise plugin
function initPlugin($yellow) function initPlugin($yellow)
{ {
$this->yellow = $yellow; $this->yellow = $yellow;
$this->yellow->config->setDefault("webinterfaceLocation", "/wiki/"); $this->yellow->config->setDefault("webinterfaceLocation", "/edit/");
$this->yellow->config->setDefault("webinterfaceUserFile", "user.ini"); $this->yellow->config->setDefault("webinterfaceUserFile", "user.ini");
$this->users = new Yellow_WebinterfaceUsers(); $this->users = new Yellow_WebinterfaceUsers();
$this->users->load($this->yellow->config->get("configDir").$this->yellow->config->get("webinterfaceUserFile")); $this->users->load($this->yellow->config->get("configDir").$this->yellow->config->get("webinterfaceUserFile"));
@ -38,7 +38,8 @@ class Yellow_Webinterface
if($this->yellow->config->get("webinterfaceLocation") == "$location/") if($this->yellow->config->get("webinterfaceLocation") == "$location/")
{ {
$statusCode = 301; $statusCode = 301;
$this->yellow->sendStatus($statusCode, "Location: http://$_SERVER[SERVER_NAME]$baseLocation$location/"); $serverName = $this->yellow->config->get("serverName");
$this->yellow->sendStatus($statusCode, "Location: http://$serverName$baseLocation$location/");
} }
} }
return $statusCode; return $statusCode;
@ -104,6 +105,7 @@ class Yellow_Webinterface
function processRequestAction($baseLocation, $location, $fileName) function processRequestAction($baseLocation, $location, $fileName)
{ {
$statusCode = 0; $statusCode = 0;
$serverName = $this->yellow->config->get("serverName");
if($_POST["action"] == "edit") if($_POST["action"] == "edit")
{ {
if(!empty($_POST["rawdata"])) if(!empty($_POST["rawdata"]))
@ -117,24 +119,24 @@ class Yellow_Webinterface
die("Configuration problem: Can't write page '$fileName'!"); die("Configuration problem: Can't write page '$fileName'!");
} }
$statusCode = 303; $statusCode = 303;
$this->yellow->sendStatus($statusCode, "Location: http://$_SERVER[SERVER_NAME]$baseLocation$location"); $this->yellow->sendStatus($statusCode, "Location: http://$serverName$baseLocation$location");
} }
} else if($_POST["action"]== "login") { } else if($_POST["action"]== "login") {
$statusCode = 303; $statusCode = 303;
$this->yellow->sendStatus($statusCode, "Location: http://$_SERVER[SERVER_NAME]$baseLocation$location"); $this->yellow->sendStatus($statusCode, "Location: http://$serverName$baseLocation$location");
} else if($_POST["action"]== "logout") { } else if($_POST["action"]== "logout") {
$this->users->destroyCookie("login"); $this->users->destroyCookie("login");
$this->activeUserEmail = ""; $this->activeUserEmail = "";
$statusCode = 302; $statusCode = 302;
$newLocation = $this->yellow->config->getHtml("baseLocation").$location; $newLocation = $this->yellow->config->getHtml("baseLocation").$location;
$this->yellow->sendStatus($statusCode, "Location: http://$_SERVER[SERVER_NAME]$newLocation"); $this->yellow->sendStatus($statusCode, "Location: http://$serverName$newLocation");
} else { } else {
if(!is_readable($fileName)) if(!is_readable($fileName))
{ {
if($this->yellow->toolbox->isFileLocation($location) && is_dir($this->yellow->getContentDirectory("$location/"))) if($this->yellow->toolbox->isFileLocation($location) && is_dir($this->yellow->getContentDirectory("$location/")))
{ {
$statusCode = 301; $statusCode = 301;
$this->yellow->sendStatus($statusCode, "Location: http://$_SERVER[SERVER_NAME]$baseLocation$location/"); $this->yellow->sendStatus($statusCode, "Location: http://$serverName$baseLocation$location/");
} else { } else {
$statusCode = $this->checkUserPermissions($location, $fileName) ? 424 : 404; $statusCode = $this->checkUserPermissions($location, $fileName) ? 424 : 404;
$this->yellow->processRequestFile($baseLocation, $location, $fileName, $statusCode, false); $this->yellow->processRequestFile($baseLocation, $location, $fileName, $statusCode, false);