mikuli.cz

:)
git clone https://git.sr.ht/~ashymad/mikuli.cz
Log | Files | Refs

commit aa395003ac3bdf756618e4f5cc2393dcafd8b6cf
parent cbb2e8ce60a64dd92723bcf0c36644da62461bb3
Author: markseu <mark2011@mayberg.se>
Date:   Wed, 29 Jan 2014 23:35:44 +0100

Better location handling (snowman remix)

Diffstat:
Msystem/core/core-commandline.php | 151+++++++++++++++++++++++++++++++++++++------------------------------------------
Msystem/core/core-markdownextra.php | 5++---
Msystem/core/core.php | 57+++++++++++++++++++++++++++++----------------------------
3 files changed, 101 insertions(+), 112 deletions(-)

diff --git a/system/core/core-commandline.php b/system/core/core-commandline.php @@ -1,20 +1,21 @@ <?php -// Copyright (c) 2013 Datenstrom, http://datenstrom.se +// Copyright (c) 2013-2014 Datenstrom, http://datenstrom.se // This file may be used and distributed under the terms of the public license. // Command line core plugin class YellowCommandline { - const Version = "0.2.2"; + const Version = "0.2.3"; var $yellow; //access to API // Initialise plugin function onLoad($yellow) { $this->yellow = $yellow; - $this->yellow->config->setDefault("commandBuildDefaultFile", "index.html"); - $this->yellow->config->setDefault("commandBuildCustomMediaExtension", ".txt"); - $this->yellow->config->setDefault("commandBuildCustomErrorFile", "error404.html"); + $this->yellow->config->setDefault("commandlineDefaultFile", "index.html"); + $this->yellow->config->setDefault("commandlineMediaFile", "(.*).txt"); + $this->yellow->config->setDefault("commandlineSystemErrorFile", "error404.html"); + $this->yellow->config->setDefault("commandlineSystemServerFile", ".htaccess"); } // Handle command help @@ -55,22 +56,20 @@ class YellowCommandline return 200; } - // Build website + // Build static pages function buildCommand($args) { $statusCode = 0; list($dummy, $command, $path, $location) = $args; - if(!empty($path) && $path!="/") + if(!empty($path) && $path!="/" && (empty($location) || $location[0]=='/')) { if($this->yellow->config->isExisting("serverName")) { - $serverName = $this->yellow->config->get("serverName"); - $serverBase = $this->yellow->config->get("serverBase"); - list($statusCode, $content, $media, $system, $error) = $this->buildStatic($serverName, $serverBase, $location, $path); + list($statusCode, $content, $media, $system, $error) = $this->buildStatic($location, $path); } else { list($statusCode, $content, $media, $system, $error) = array(500, 0, 0, 0, 1); $fileName = $this->yellow->config->get("configDir").$this->yellow->config->get("configFile"); - echo "ERROR bulding website: Please configure serverName and serverBase in file '$fileName'!\n"; + echo "ERROR bulding pages: Please configure serverName and serverBase in file '$fileName'!\n"; } echo "Yellow $command: $content content, $media media, $system system"; echo ", $error error".($error!=1 ? 's' : ''); @@ -83,71 +82,53 @@ class YellowCommandline } // Build static files - function buildStatic($serverName, $serverBase, $location, $path) + function buildStatic($location, $path) { $this->yellow->toolbox->timerStart($time); $statusCodeMax = $error = 0; if(empty($location)) { $pages = $this->yellow->pages->index(true); - $fileNamesMedia = $this->yellow->toolbox->getDirectoryEntriesrecursive( + $fileNamesMedia = $this->yellow->toolbox->getDirectoryEntriesRecursive( $this->yellow->config->get("mediaDir"), "/.*/", false, false); $fileNamesMedia = array_merge($fileNamesMedia, $this->yellow->toolbox->getDirectoryEntries( - ".", "/.*\\".$this->yellow->config->get("commandBuildCustomMediaExtension")."/", false, false)); - $fileNamesSystem = array($this->yellow->config->get("commandBuildCustomErrorFile")); + ".", "/".$this->yellow->config->get("commandlineMediaFile")."/", false, false, false)); + $fileNamesSystem = array($this->yellow->config->get("commandlineSystemErrorFile"), + $this->yellow->config->get("commandlineSystemServerFile")); } else { - if($location[0] != '/') $location = '/'.$location; $pages = new YellowPageCollection($this->yellow); $pages->append(new YellowPage($this->yellow, $location)); - $fileNamesMedia = array(); - $fileNamesSystem = array(); + $fileNamesMedia = $fileNamesSystem = array(); } foreach($pages as $page) { - $statusCode = $this->buildStaticLocation($serverName, $serverBase, $page->location, $path); - $statusCodeMax = max($statusCodeMax, $statusCode); - if($statusCode >= 400) - { - ++$error; - echo "ERROR building location '".$page->location."', ".$this->yellow->page->getStatusCode(true)."\n"; - } - if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStatic status:$statusCode location:".$page->location."\n"; + $statusCode = $this->buildStaticLocation($page->location, $path); + $statusCodeMax = max($statusCodeMax, $statusCode); if($statusCode >= 400) ++$error; } foreach($fileNamesMedia as $fileName) { - $statusCode = $this->copyStaticFile($fileName, "$path/$fileName") ? 200 : 500; - $statusCodeMax = max($statusCodeMax, $statusCode); - if($statusCode >= 400) - { - ++$error; - echo "ERROR building media file '$path/$fileName', ".$this->yellow->toolbox->getHttpStatusFormatted($statusCode)."\n"; - } - if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStatic status:$statusCode file:$fileName\n"; + $statusCode = $this->buildStaticFile($fileName, "$path/$fileName", "media file"); + $statusCodeMax = max($statusCodeMax, $statusCode); if($statusCode >= 400) ++$error; } foreach($fileNamesSystem as $fileName) { - $statusCode = $this->buildStaticError($serverName, $serverBase, "$path/$fileName", 404) ? 200 : 500; - $statusCodeMax = max($statusCodeMax, $statusCode); - if($statusCode >= 400) - { - ++$error; - echo "ERROR building system file '$path/$fileName', ".$this->yellow->toolbox->getHttpStatusFormatted($statusCode)."\n"; - } - if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStatic status:$statusCode file:$fileName\n"; + $statusCode = $this->buildStaticFile($fileName, "$path/$fileName", "system file"); + $statusCodeMax = max($statusCodeMax, $statusCode); if($statusCode >= 400) ++$error; } $this->yellow->toolbox->timerStop($time); if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStatic time:$time ms\n"; return array($statusCodeMax, count($pages), count($fileNamesMedia), count($fileNamesSystem), $error); } - // Build static location as file - function buildStaticLocation($serverName, $serverBase, $location, $path) + // Build static location + function buildStaticLocation($location, $path) { ob_start(); $_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1"; - $_SERVER["SERVER_NAME"] = $serverName; - $_SERVER["REQUEST_URI"] = $serverBase.$location; - $_SERVER["SCRIPT_NAME"] = $serverBase."yellow.php"; + $_SERVER["SERVER_NAME"] = $this->yellow->config->get("serverName"); + $_SERVER["REQUEST_URI"] = $this->yellow->config->get("serverBase").$location; + $_SERVER["SCRIPT_NAME"] = $this->yellow->config->get("serverBase")."yellow.php"; + $_REQUEST = array(); $statusCode = $this->yellow->request(); if($statusCode != 404) { @@ -160,18 +141,21 @@ class YellowCommandline $fileName = $this->getStaticFileName($location, $path); $fileData = ob_get_contents(); if($statusCode>=301 && $statusCode<=303) $fileData = $this->getStaticRedirect($this->yellow->page->getHeader("Location")); - $fileOk = $this->createStaticFile($fileName, $fileData, $modified); + $fileOk = $this->yellow->toolbox->createFile($fileName, $fileData, true) && + $this->yellow->toolbox->modifyFile($fileName, $modified); } else { if(!$this->yellow->toolbox->isFileLocation($location)) { $fileName = $this->getStaticFileName($location, $path); - $fileData = $this->getStaticRedirect("http://$serverName$serverBase$staticLocation"); - $fileOk = $this->createStaticFile($fileName, $fileData, $modified); + $fileData = $this->getStaticRedirect($staticLocation); + $fileOk = $this->yellow->toolbox->createFile($fileName, $fileData, true) && + $this->yellow->toolbox->modifyFile($fileName, $modified); if($fileOk) { $fileName = $this->getStaticFileName($staticLocation, $path); $fileData = ob_get_contents(); - $fileOk = $this->createStaticFile($fileName, $fileData, $modified); + $fileOk = $this->yellow->toolbox->createFile($fileName, $fileData, true) && + $this->yellow->toolbox->modifyFile($fileName, $modified); } } else { $statusCode = 409; @@ -185,43 +169,46 @@ class YellowCommandline } } ob_end_clean(); + if($statusCode>=400) echo "ERROR building location '$location', ".$this->yellow->page->getStatusCode(true)."\n"; + if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStaticLocation status:$statusCode location:$location\n"; return $statusCode; } - // Build static error as file - function buildStaticError($serverName, $serverBase, $fileName, $statusCodeRequest) + // Build static file + function buildStaticFile($fileNameSource, $fileNameDest, $fileType) { - ob_start(); - $_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1"; - $_SERVER["SERVER_NAME"] = $serverName; - $_SERVER["REQUEST_URI"] = $serverBase."/"; - $_SERVER["SCRIPT_NAME"] = $serverBase."yellow.php"; - $statusCode = $this->yellow->request($statusCodeRequest); - if($statusCode == $statusCodeRequest) + if($fileNameSource != $this->yellow->config->get("commandlineSystemErrorFile")) { - $modified = strtotime($this->yellow->page->getHeader("Last-Modified")); - if(!$this->createStaticFile($fileName, ob_get_contents(), $modified)) + $statusCode = $this->yellow->toolbox->copyFile($fileNameSource, $fileNameDest, true) && + $this->yellow->toolbox->modifyFile($fileNameDest, filemtime($fileNameSource)) ? 200 : 500; + } else { + ob_start(); + $_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1"; + $_SERVER["SERVER_NAME"] = $this->yellow->config->get("serverName"); + $_SERVER["REQUEST_URI"] = $this->yellow->config->get("serverBase")."/"; + $_SERVER["SCRIPT_NAME"] = $this->yellow->config->get("serverBase")."yellow.php"; + $_REQUEST = array(); + $statusCodeRequest = 404; + $statusCode = $this->yellow->request($statusCodeRequest); + if($statusCode == $statusCodeRequest) { + $statusCode = 200; + $modified = strtotime($this->yellow->page->getHeader("Last-Modified")); + if(!$this->yellow->toolbox->createFile($fileNameDest, ob_get_contents(), true) || + !$this->yellow->toolbox->modifyFile($fileNameDest, $modified)) + { + $statusCode = 500; + $this->yellow->page->error($statusCode, "Can't write file '$fileNameDest'!"); + } + } else { $statusCode = 500; - $this->yellow->page->error($statusCode, "Can't write file '$fileName'!"); + $this->yellow->page->error($statusCode, "Error $statusCodeRequest does not exist!"); } + ob_end_clean(); } - ob_end_clean(); - return $statusCode == $statusCodeRequest; - } - - // Create static file - function createStaticFile($fileName, $fileData, $modified) - { - return $this->yellow->toolbox->createFile($fileName, $fileData, true) && - $this->yellow->toolbox->modifyFile($fileName, $modified); - } - - // Copy static file - function copyStaticFile($fileNameSource, $fileNameDest) - { - return $this->yellow->toolbox->copyFile($fileNameSource, $fileNameDest, true) && - $this->yellow->toolbox->modifyFile($fileNameDest, filemtime($fileNameSource)); + if($statusCode>=400) echo "ERROR building $fileType '$fileNameSource', ".$this->yellow->toolbox->getHttpStatusFormatted($statusCode)."\n"; + if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStaticFile status:$statusCode file:$fileNameSource\n"; + return $statusCode; } // Return static location corresponding to content type @@ -255,14 +242,16 @@ class YellowCommandline $fileName = $path.$location; if(!$this->yellow->toolbox->isFileLocation($location)) { - $fileName .= $this->yellow->config->get("commandBuildDefaultFile"); + $fileName .= $this->yellow->config->get("commandlineDefaultFile"); } return $fileName; } // Return static redirect data - function getStaticRedirect($url) + function getStaticRedirect($location) { + $url = $this->yellow->toolbox->getHttpUrl($this->yellow->config->get("serverName"), + $this->yellow->config->get("serverBase"), $location); $text = "<!DOCTYPE html><html>\n"; $text .= "<head>\n"; $text .= "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n"; diff --git a/system/core/core-markdownextra.php b/system/core/core-markdownextra.php @@ -5,9 +5,8 @@ // Markdown extra core plugin class YellowMarkdownExtra { - const Version = "0.2.7"; + const Version = "0.2.8"; var $yellow; //access to API - var $textHtml; //generated text (HTML format) // Initialise plugin function onLoad($yellow) @@ -19,7 +18,7 @@ class YellowMarkdownExtra function onParse($page, $text) { $markdown = new YellowMarkdownExtraParser($this->yellow); - $this->textHtml = $markdown->transformPage($page, $text); + return $markdown->transformPage($page, $text); } } diff --git a/system/core/core.php b/system/core/core.php @@ -5,7 +5,7 @@ // Yellow main class class Yellow { - const Version = "0.2.7"; + const Version = "0.2.8"; var $page; //current page data var $pages; //current page tree from file system var $config; //configuration @@ -341,6 +341,7 @@ class YellowPage var $metaDataOffsetBytes; //meta data offset var $metaData; //meta data var $headerData; //response header + var $parserData; //content data of page var $parser; //content parser var $active; //page is active location? (boolean) var $visible; //page is visible location? (boolean) @@ -445,18 +446,18 @@ class YellowPage if(method_exists($plugin["obj"], "onParse")) { $this->parser = $plugin["obj"]; - $this->parser->onParse($this, $this->getContent(true)); + $this->parserData = $this->parser->onParse($this, $this->getContent(true)); foreach($this->yellow->plugins->plugins as $key=>$value) { if(method_exists($value["obj"], "onParseContent")) { - $output = $value["obj"]->onParseContent($this, $this->parser->textHtml); - if(!is_null($output)) { $this->parser->textHtml = $output; break; } + $output = $value["obj"]->onParseContent($this, $this->parserData); + if(!is_null($output)) { $this->parserData = $output; break; } } } if(!$this->isExisting("description")) { - $this->set("description", $this->yellow->toolbox->createTextDescription($this->parser->textHtml, 150)); + $this->set("description", $this->yellow->toolbox->createTextDescription($this->parserData, 150)); } if(!$this->isExisting("keywords")) { @@ -552,7 +553,7 @@ class YellowPage $text = substrb($this->rawData, $this->metaDataOffsetBytes); } else { $this->parseContent(); - $text = is_object($this->parser) ? $this->parser->textHtml : ""; + $text = $this->parserData; } return $text; } @@ -914,24 +915,24 @@ class YellowPages $this->yellow->config->get("contentDir"), $this->yellow->config->get("contentHomeDir"), "", ""); } $fileNames = array(); - foreach($this->yellow->toolbox->getDirectoryEntries($path, "/.*/", true) as $entry) + foreach($this->yellow->toolbox->getDirectoryEntries($path, "/.*/") as $entry) { $fileDefault = $this->yellow->config->get("contentDefaultFile"); - if(!is_file($path.$entry."/".$fileDefault)) + if(!is_file($entry."/".$fileDefault)) { $regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $fileDefault)."$/"; - foreach($this->yellow->toolbox->getDirectoryEntries($path.$entry, $regex, false, false) as $entry2) + foreach($this->yellow->toolbox->getDirectoryEntries($entry, $regex, false, false, false) as $entry2) { if($this->yellow->toolbox->normaliseName($entry2) == $fileDefault) { $fileDefault = $entry2; break; } } } - array_push($fileNames, $path.$entry."/".$fileDefault); + array_push($fileNames, $entry."/".$fileDefault); } + $fileDefault = $this->yellow->config->get("contentDefaultFile"); $regex = "/.*\\".$this->yellow->config->get("contentExtension")."/"; - foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false) as $entry) + foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false, false) as $entry) { - $token = $this->yellow->toolbox->normaliseName($entry); - if($token != $this->yellow->config->get("contentDefaultFile")) array_push($fileNames, $path.$entry); + if($this->yellow->toolbox->normaliseName($entry) != $fileDefault) array_push($fileNames, $path.$entry); } foreach($fileNames as $fileName) { @@ -1086,11 +1087,11 @@ class YellowText $regex = "/".basename($fileName)."/"; foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false) as $entry) { - $fileData = @file("$path/$entry"); + $fileData = @file($entry); if($fileData) { - if(defined("DEBUG") && DEBUG>=2) echo "YellowText::load file:$path/$entry<br/>\n"; - $this->modified = max($this->modified, filemtime("$path/$entry")); + if(defined("DEBUG") && DEBUG>=2) echo "YellowText::load file:$entry<br/>\n"; + $this->modified = max($this->modified, filemtime($entry)); $language = ""; foreach($fileData as $line) { @@ -1362,7 +1363,7 @@ class YellowToolbox $token = $tokens[$i]; if($this->normaliseName($token) != $token) $invalid = true; $regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/"; - foreach($this->getDirectoryEntries($path, $regex) as $entry) + foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry) { if($this->normaliseName($entry) == $token) { $token = $entry; break; } } @@ -1373,7 +1374,7 @@ class YellowToolbox $token = rtrim($pathHome, '/'); if($this->normaliseName($token) != $token) $invalid = true; $regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/"; - foreach($this->getDirectoryEntries($path, $regex) as $entry) + foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry) { if($this->normaliseName($entry) == $token) { $token = $entry; break; } } @@ -1383,7 +1384,7 @@ class YellowToolbox if(!empty($tokens[$i]) && $tokens[$i].$fileExtension==$fileDefault) $invalid = true; if($this->normaliseName($token) != $token) $invalid = true; $regex = "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/"; - foreach($this->getDirectoryEntries($path, $regex, false, false) as $entry) + foreach($this->getDirectoryEntries($path, $regex, false, false, false) as $entry) { if($this->normaliseName($entry) == $token) { $token = $entry; break; } } @@ -1469,12 +1470,13 @@ class YellowToolbox } // Return files and directories - function getDirectoryEntries($path, $regex = "/.*/", $sort = false, $directories = true) + function getDirectoryEntries($path, $regex = "/.*/", $sort = true, $directories = true, $includePath = true) { $entries = array(); $dirHandle = @opendir($path); if($dirHandle) { + $path = rtrim($path, '/'); while(($entry = readdir($dirHandle)) !== false) { if(substru($entry, 0, 1) == ".") continue; @@ -1482,9 +1484,9 @@ class YellowToolbox { if($directories) { - if(is_dir("$path/$entry")) array_push($entries, $entry); + if(is_dir("$path/$entry")) array_push($entries, $includePath ? "$path/$entry" : $entry); } else { - if(is_file("$path/$entry")) array_push($entries, $entry); + if(is_file("$path/$entry")) array_push($entries, $includePath ? "$path/$entry" : $entry); } } } @@ -1495,16 +1497,15 @@ class YellowToolbox } // Return files and directories recursively - function getDirectoryEntriesRecursive($path, $regex = "/.*/", $sort = false, $directories = true, $levelMax = 0) + function getDirectoryEntriesRecursive($path, $regex = "/.*/", $sort = true, $directories = true, $levelMax = 0) { - $entries = array(); - foreach($this->getDirectoryEntries($path, $regex, $sort, $directories) as $entry) array_push($entries, "$path/$entry"); --$levelMax; + $entries = $this->getDirectoryEntries($path, $regex, $sort, $directories); if($levelMax != 0) { foreach($this->getDirectoryEntries($path, "/.*/", $sort, true) as $entry) { - $entries = array_merge($entries, $this->getDirectoryEntriesRecursive("$path/$entry", $regex, $sort, $directories, $levelMax)); + $entries = array_merge($entries, $this->getDirectoryEntriesRecursive($entry, $regex, $sort, $directories, $levelMax)); } } return $entries; @@ -1711,9 +1712,9 @@ class YellowPlugins { global $yellow; $path = dirname(__FILE__); - foreach($yellow->toolbox->getDirectoryEntries($path, "/core-.*\.php/", true, false) as $entry) require_once("$path/$entry"); + foreach($yellow->toolbox->getDirectoryEntries($path, "/core-.*\.php/", true, false) as $entry) require_once($entry); $path = $yellow->config->get("pluginDir"); - foreach($yellow->toolbox->getDirectoryEntries($path, "/.*\.php/", true, false) as $entry) require_once("$path/$entry"); + foreach($yellow->toolbox->getDirectoryEntries($path, "/.*\.php/", true, false) as $entry) require_once($entry); foreach($this->plugins as $key=>$value) { $this->plugins[$key]["obj"] = new $value["class"];