yellow/system/core/core.php

2270 lines
72 KiB
PHP
Raw Normal View History

2013-04-07 18:04:09 +00:00
<?php
2014-01-08 16:58:52 +00:00
// Copyright (c) 2013-2014 Datenstrom, http://datenstrom.se
2013-04-07 18:04:09 +00:00
// This file may be used and distributed under the terms of the public license.
2013-04-07 18:04:09 +00:00
// Yellow main class
class Yellow
{
const Version = "0.4.3";
2014-03-21 09:12:56 +00:00
var $page; //current page
var $pages; //pages from file system
2013-07-11 20:33:28 +00:00
var $config; //configuration
var $text; //text strings
2013-11-29 12:16:14 +00:00
var $toolbox; //toolbox with helpers
2013-07-11 20:33:28 +00:00
var $plugins; //plugins
2013-04-07 18:04:09 +00:00
function __construct()
{
2013-12-01 11:59:07 +00:00
$this->pages = new YellowPages($this);
$this->config = new YellowConfig($this);
$this->text = new YellowText($this);
$this->toolbox = new YellowToolbox();
$this->plugins = new YellowPlugins();
2013-04-14 22:41:04 +00:00
$this->config->setDefault("sitename", "Yellow");
$this->config->setDefault("author", "Yellow");
2013-05-23 06:24:50 +00:00
$this->config->setDefault("style", "default");
2014-08-29 07:15:49 +00:00
$this->config->setDefault("template", "default");
$this->config->setDefault("language", "en");
2014-05-15 11:53:54 +00:00
$this->config->setDefault("serverScheme", $this->toolbox->getServerScheme());
2013-06-07 20:01:12 +00:00
$this->config->setDefault("serverName", $this->toolbox->getServerName());
$this->config->setDefault("serverBase", $this->toolbox->getServerBase());
2013-05-23 06:24:50 +00:00
$this->config->setDefault("styleLocation", "/media/styles/");
$this->config->setDefault("imageLocation", "/media/images/");
2014-01-08 16:58:52 +00:00
$this->config->setDefault("pluginLocation", "/media/plugins/");
2013-04-07 18:04:09 +00:00
$this->config->setDefault("systemDir", "system/");
$this->config->setDefault("configDir", "system/config/");
$this->config->setDefault("pluginDir", "system/plugins/");
$this->config->setDefault("snippetDir", "system/snippets/");
$this->config->setDefault("templateDir", "system/templates/");
$this->config->setDefault("mediaDir", "media/");
2013-05-23 06:24:50 +00:00
$this->config->setDefault("styleDir", "media/styles/");
$this->config->setDefault("imageDir", "media/images/");
2013-04-07 18:04:09 +00:00
$this->config->setDefault("contentDir", "content/");
$this->config->setDefault("contentRootDir", "default/");
$this->config->setDefault("contentHomeDir", "home/");
2013-04-14 22:41:04 +00:00
$this->config->setDefault("contentDefaultFile", "page.txt");
$this->config->setDefault("contentPagination", "page");
2013-04-07 18:04:09 +00:00
$this->config->setDefault("contentExtension", ".txt");
2013-04-14 22:41:04 +00:00
$this->config->setDefault("configExtension", ".ini");
2013-04-07 18:04:09 +00:00
$this->config->setDefault("configFile", "config.ini");
2013-04-14 22:41:04 +00:00
$this->config->setDefault("errorPageFile", "error(.*).txt");
2013-12-01 11:59:07 +00:00
$this->config->setDefault("textStringFile", "text(.*).ini");
2014-08-29 07:15:49 +00:00
$this->config->setDefault("parser", "markdownextra");
$this->config->setDefault("parserSafeMode", "0");
$this->config->setDefault("multiLanguageMode", "0");
2013-04-14 22:41:04 +00:00
$this->config->load($this->config->get("configDir").$this->config->get("configFile"));
2013-11-29 12:16:14 +00:00
$this->text->load($this->config->get("configDir").$this->config->get("textStringFile"));
$this->updateConfig();
2013-04-14 22:41:04 +00:00
}
2013-06-07 20:01:12 +00:00
// Handle request
2014-05-15 11:53:54 +00:00
function request($statusCodeRequest = 0)
2013-04-07 18:04:09 +00:00
{
$this->toolbox->timerStart($time);
ob_start();
$statusCode = 0;
2014-05-15 11:53:54 +00:00
list($serverScheme, $serverName, $base, $location, $fileName) = $this->getRequestInformation();
2014-07-25 10:46:58 +00:00
$this->page = new YellowPage($this, $serverScheme, $serverName, $base, $location, $fileName);
2013-04-14 22:41:04 +00:00
foreach($this->plugins->plugins as $key=>$value)
{
2013-04-07 18:04:09 +00:00
if(method_exists($value["obj"], "onRequest"))
2013-04-14 22:41:04 +00:00
{
$this->pages->requestHandler = $key;
2014-05-15 11:53:54 +00:00
$statusCode = $value["obj"]->onRequest($serverScheme, $serverName, $base, $location, $fileName);
if($statusCode != 0) break;
2013-04-14 22:41:04 +00:00
}
}
if($statusCode == 0)
{
$this->pages->requestHandler = "core";
2014-05-15 11:53:54 +00:00
$statusCode = $this->processRequest($serverScheme, $serverName, $base, $location, $fileName, true, $statusCode);
}
if($this->page->isError() || $statusCodeRequest>=400) $statusCode = $this->processRequestError($statusCodeRequest);
2013-06-27 17:00:03 +00:00
ob_end_flush();
$this->toolbox->timerStop($time);
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::request status:$statusCode location:$location<br/>\n";
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::request time:$time ms<br/>\n";
2013-07-11 20:33:28 +00:00
return $statusCode;
2013-04-14 22:41:04 +00:00
}
2013-06-27 17:00:03 +00:00
// Process request
2014-05-15 11:53:54 +00:00
function processRequest($serverScheme, $serverName, $base, $location, $fileName, $cacheable, $statusCode)
2013-04-14 22:41:04 +00:00
{
$handler = $this->getRequestHandler();
2013-04-14 22:41:04 +00:00
if($statusCode == 0)
{
if(is_readable($fileName))
{
if($this->toolbox->isRequestCleanUrl($location))
{
2013-10-16 21:11:24 +00:00
$statusCode = 303;
$locationArgs = $this->toolbox->getLocationArgsCleanUrl($this->config->get("contentPagination"));
2014-05-15 11:53:54 +00:00
$locationHeader = $this->toolbox->getLocationHeader($serverScheme, $serverName, $base, $location.$locationArgs);
$this->sendStatus($statusCode, $locationHeader);
} else {
$statusCode = 200;
$fileName = $this->readPage($serverScheme, $serverName, $base, $location, $fileName, $cacheable, $statusCode);
}
2013-04-14 22:41:04 +00:00
} else {
if(($this->toolbox->isFileLocation($location) && $this->isContentDirectory("$location/")) ||
($location=="/" && $this->config->get("multiLanguageMode")))
2013-04-14 22:41:04 +00:00
{
$statusCode = 301;
$location = $this->toolbox->isFileLocation($location) ? "$location/" : "/".$this->config->get("language")."/";
$locationHeader = $this->toolbox->getLocationHeader($serverScheme, $serverName, $base, $location);
2014-05-15 11:53:54 +00:00
$this->sendStatus($statusCode, $locationHeader);
2013-04-14 22:41:04 +00:00
} else {
$statusCode = 404;
2014-05-15 11:53:54 +00:00
$fileName = $this->readPage($serverScheme, $serverName, $base, $location, $fileName, $cacheable, $statusCode);
2013-04-14 22:41:04 +00:00
}
}
2013-06-27 17:00:03 +00:00
} else if($statusCode >= 400) {
2014-05-15 11:53:54 +00:00
$fileName = $this->readPage($serverScheme, $serverName, $base, $location, $fileName, $cacheable, $statusCode);
2013-04-14 22:41:04 +00:00
}
if($this->page->statusCode != 0) $statusCode = $this->sendPage();
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequest handler:$handler file:$fileName<br/>\n";
2013-04-14 22:41:04 +00:00
return $statusCode;
}
2013-07-11 20:33:28 +00:00
// Process request with error
2014-05-15 11:53:54 +00:00
function processRequestError($statusCodeRequest)
2013-04-14 22:41:04 +00:00
{
ob_clean();
$handler = $this->getRequestHandler();
2014-05-15 11:53:54 +00:00
if($statusCodeRequest >= 400) $this->page->error($statusCodeRequest, "Request error");
$fileName = $this->readPage($this->page->serverScheme, $this->page->serverName, $this->page->base, $this->page->location,
$this->page->fileName, $this->page->cacheable, $this->page->statusCode, $this->page->get("pageError"));
2013-07-11 20:33:28 +00:00
$statusCode = $this->sendPage();
if(defined("DEBUG") && DEBUG>=1) echo "Yellow::processRequestError handler:$handler file:$fileName<br/>\n";
2013-07-11 20:33:28 +00:00
return $statusCode;
}
// Read page from file
2014-05-15 11:53:54 +00:00
function readPage($serverScheme, $serverName, $base, $location, $fileName, $cacheable, $statusCode, $pageError = "")
2013-07-11 20:33:28 +00:00
{
if($statusCode >= 400)
{
$fileName = $this->config->get("configDir").$this->config->get("errorPageFile");
$fileName = strreplaceu("(.*)", $statusCode, $fileName);
$cacheable = false;
}
2013-06-27 17:00:03 +00:00
$fileHandle = @fopen($fileName, "r");
if($fileHandle)
{
$fileData = fread($fileHandle, filesize($fileName));
fclose($fileHandle);
2013-07-11 20:33:28 +00:00
}
2014-07-25 10:46:58 +00:00
$this->page = new YellowPage($this, $serverScheme, $serverName, $base, $location, $fileName);
$this->page->parseData($fileData, $cacheable, $statusCode, $pageError);
2013-10-16 21:11:24 +00:00
$this->page->setHeader("Content-Type", "text/html; charset=UTF-8");
$this->page->setHeader("Last-Modified", $this->page->getModified(true));
if(!$this->page->isCacheable()) $this->page->setHeader("Cache-Control", "no-cache, must-revalidate");
2013-06-27 17:00:03 +00:00
$this->text->setLanguage($this->page->get("language"));
2013-12-11 14:13:38 +00:00
$this->page->parseContent();
2013-06-27 17:00:03 +00:00
return $fileName;
2013-04-14 22:41:04 +00:00
}
// Send page response
2013-07-11 20:33:28 +00:00
function sendPage()
2013-04-14 22:41:04 +00:00
{
2013-07-11 20:33:28 +00:00
$this->template($this->page->get("template"));
$fileNameStyle = $this->config->get("styleDir").$this->page->get("style").".css";
if(!is_file($fileNameStyle))
{
$this->page->error(500, "Style '".$this->page->get("style")."' does not exist!");
}
2014-01-08 16:58:52 +00:00
if(!is_object($this->page->parser))
2013-07-11 20:33:28 +00:00
{
$this->page->error(500, "Parser '".$this->page->get("parser")."' does not exist!");
}
$statusCode = $this->page->statusCode;
2013-11-29 12:16:14 +00:00
if($statusCode==200 && $this->getRequestHandler()=="core" && $this->page->isExisting("redirect"))
{
$statusCode = 301;
$location = $this->toolbox->normaliseLocation($this->page->get("redirect"), $this->page->base, $this->page->location);
2014-05-15 11:53:54 +00:00
$locationHeader = $this->toolbox->getLocationHeader($this->page->serverScheme, $this->page->serverName, "", $location);
$this->page->clean($statusCode, $locationHeader);
2013-11-29 12:16:14 +00:00
$this->page->setHeader("Last-Modified", $this->page->getModified(true));
$this->page->setHeader("Cache-Control", "no-cache, must-revalidate");
}
if($statusCode==200 && $this->page->isCacheable() &&
$this->toolbox->isFileNotModified($this->page->getHeader("Last-Modified")))
2013-06-27 17:00:03 +00:00
{
$statusCode = 304;
2013-12-21 13:10:15 +00:00
if($this->page->isHeader("Cache-Control")) $responseHeader = "Cache-Control: ".$this->page->getHeader("Cache-Control");
$this->page->clean($statusCode, $responseHeader);
}
list($contentType) = explode(';', $this->page->getHeader("Content-Type"));
if($statusCode==200 && !$this->toolbox->isValidContentType($contentType, $this->page->getLocation()))
{
$statusCode = 500;
$this->page->error($statusCode, "Type '$contentType' does not match file name!");
}
2013-11-29 12:16:14 +00:00
if($this->page->isExisting("pageClean")) ob_clean();
if(PHP_SAPI != "cli")
{
@header($this->toolbox->getHttpStatusFormatted($statusCode));
2013-11-29 12:16:14 +00:00
foreach($this->page->headerData as $key=>$value) @header("$key: $value");
} else {
if($statusCode>=301 && $statusCode<=303) $this->sendStaticRedirect();
2013-07-11 20:33:28 +00:00
}
if(defined("DEBUG") && DEBUG>=1)
{
foreach($this->page->headerData as $key=>$value) echo "Yellow::sendPage $key: $value<br/>\n";
$fileNameTemplate = $this->config->get("templateDir").$this->page->get("template").".php";
$parserName = $this->page->get("parser");
echo "Yellow::sendPage template:$fileNameTemplate style:$fileNameStyle parser:$parserName<br/>\n";
2013-06-27 17:00:03 +00:00
}
return $statusCode;
}
2013-06-27 17:00:03 +00:00
// Send status response
2013-11-29 12:16:14 +00:00
function sendStatus($statusCode, $responseHeader = "")
2013-06-27 17:00:03 +00:00
{
if(PHP_SAPI != "cli")
{
@header($this->toolbox->getHttpStatusFormatted($statusCode));
2013-11-29 12:16:14 +00:00
if(!empty($responseHeader)) @header($responseHeader);
} else {
if(!empty($responseHeader))
{
$this->page->header($responseHeader);
$this->page->header("Last-Modified: ".$this->toolbox->getHttpTimeFormatted(time()));
if($statusCode>=301 && $statusCode<=303) $this->sendStaticRedirect();
}
}
}
// Send static redirect response
function sendStaticRedirect()
{
echo "<!DOCTYPE html><html>\n<head>\n";
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
echo "<meta http-equiv=\"refresh\" content=\"0;url=".htmlspecialchars($this->page->getHeader("Location"))."\" />\n";
echo "</head>\n</html>";
}
// Return request information
2014-05-15 11:53:54 +00:00
function getRequestInformation($serverScheme = "", $serverName = "", $base = "")
2013-12-21 13:10:15 +00:00
{
2014-05-15 11:53:54 +00:00
$serverScheme = empty($serverScheme) ? $this->config->get("serverScheme") : $serverScheme;
$serverName = empty($serverName) ? $this->config->get("serverName") : $serverName;
$base = empty($base) ? $this->config->get("serverBase") : $base;
$location = $this->toolbox->getLocationClean();
2014-05-15 11:53:54 +00:00
$location = substru($location, strlenu($base));
$fileName = $this->toolbox->findFileFromLocation($location, $this->config->get("contentDir"),
$this->config->get("contentRootDir"), $this->config->get("contentHomeDir"),
2013-12-21 13:10:15 +00:00
$this->config->get("contentDefaultFile"), $this->config->get("contentExtension"));
2014-05-15 11:53:54 +00:00
return array($serverScheme, $serverName, $base, $location, $fileName);
2013-12-21 13:10:15 +00:00
}
// Return request handler
function getRequestHandler()
{
return $this->pages->requestHandler;
}
2013-12-21 13:10:15 +00:00
// Check if content directory exists
function isContentDirectory($location)
{
$path = $this->toolbox->findFileFromLocation($location, $this->config->get("contentDir"),
$this->config->get("contentRootDir"), $this->config->get("contentHomeDir"), "", "");
2013-12-21 13:10:15 +00:00
return is_dir($path);
}
// Update content configuration
function updateConfig()
{
list($pathRoot, $pathHome) = $this->toolbox->findRootConfig($this->config->get("contentDir"),
$this->config->get("contentRootDir"), $this->config->get("contentHomeDir"),
$this->config->get("multiLanguageMode"));
$this->config->set("contentRootDir", $pathRoot);
$this->config->set("contentHomeDir", $pathHome);
}
2014-07-10 08:42:32 +00:00
// Execute command
function command($name, $args = NULL)
{
$statusCode = 0;
if($this->plugins->isExisting($name))
{
$plugin = $this->plugins->plugins[$name];
if(method_exists($plugin["obj"], "onCommand")) $statusCode = $plugin["obj"]->onCommand(func_get_args());
} else {
$statusCode = 500;
$this->page->error($statusCode, "Plugin '$name' does not exist!");
}
return $statusCode;
}
2013-12-11 14:13:38 +00:00
// Execute template
2013-07-11 20:33:28 +00:00
function template($name)
{
$fileNameTemplate = $this->config->get("templateDir")."$name.php";
if(is_file($fileNameTemplate))
{
global $yellow;
require($fileNameTemplate);
} else {
$this->page->error(500, "Template '$name' does not exist!");
}
}
// Execute snippet
function snippet($name, $args = NULL)
2013-04-07 18:04:09 +00:00
{
2013-07-16 16:56:27 +00:00
$this->pages->snippetArgs = func_get_args();
2013-06-27 17:00:03 +00:00
$fileNameSnippet = $this->config->get("snippetDir")."$name.php";
2013-07-11 20:33:28 +00:00
if(is_file($fileNameSnippet))
{
global $yellow;
require($fileNameSnippet);
} else {
$this->page->error(500, "Snippet '$name' does not exist!");
}
2013-04-07 18:04:09 +00:00
}
2013-04-14 22:41:04 +00:00
2013-12-11 14:13:38 +00:00
// Return snippet arguments
function getSnippetArgs()
{
2013-07-16 16:56:27 +00:00
return $this->pages->snippetArgs;
}
2013-04-07 18:04:09 +00:00
}
2013-05-01 20:16:05 +00:00
2014-03-21 09:12:56 +00:00
// Yellow page
2013-12-01 11:59:07 +00:00
class YellowPage
2013-04-07 18:04:09 +00:00
{
2013-07-11 20:33:28 +00:00
var $yellow; //access to API
2014-05-15 11:53:54 +00:00
var $serverScheme; //server scheme
var $serverName; //server name
var $base; //base location
2013-07-16 16:56:27 +00:00
var $location; //page location
2013-05-01 20:16:05 +00:00
var $fileName; //content file name
2013-06-27 17:00:03 +00:00
var $rawData; //raw data of page
2013-07-11 20:33:28 +00:00
var $metaDataOffsetBytes; //meta data offset
2013-10-16 21:11:24 +00:00
var $metaData; //meta data
var $headerData; //response header
var $parserData; //content data of page
2013-10-16 21:11:24 +00:00
var $parser; //content parser
2014-08-29 07:15:49 +00:00
var $parserSafeMode; //page is parsed in safe mode? (boolean)
2013-07-11 20:33:28 +00:00
var $active; //page is active location? (boolean)
var $visible; //page is visible location? (boolean)
var $cacheable; //page is cacheable? (boolean)
2013-10-16 21:11:24 +00:00
var $statusCode; //status code
2014-07-25 10:46:58 +00:00
function __construct($yellow, $serverScheme, $serverName, $base, $location, $fileName)
2013-04-14 22:41:04 +00:00
{
2013-07-11 20:33:28 +00:00
$this->yellow = $yellow;
2014-05-15 11:53:54 +00:00
$this->serverScheme = $serverScheme;
$this->serverName = $serverName;
$this->base = $base;
2013-07-16 16:56:27 +00:00
$this->location = $location;
2014-07-25 10:46:58 +00:00
$this->fileName = $fileName;
$this->metaData = array();
$this->headerData = array();
$this->statusCode = 0;
}
// Parse page data
2014-07-25 10:46:58 +00:00
function parseData($rawData, $cacheable, $statusCode, $pageError = "")
{
2013-07-11 20:33:28 +00:00
$this->rawData = $rawData;
2014-08-29 07:15:49 +00:00
$this->parserSafeMode = $this->yellow->config->get("parserSafeMode");
$this->active = $this->yellow->toolbox->isActiveLocation($this->location, $this->yellow->page->location,
$this->yellow->pages->getHomeLocation($this->yellow->page->location));
2014-07-25 10:46:58 +00:00
$this->visible = $this->yellow->toolbox->isVisibleLocation($this->location, $this->fileName,
2014-05-15 11:53:54 +00:00
$this->yellow->config->get("contentDir"));
2013-07-11 20:33:28 +00:00
$this->cacheable = $cacheable;
$this->statusCode = $statusCode;
if(!empty($pageError)) $this->error($statusCode, $pageError);
$this->parseMeta();
2013-04-14 22:41:04 +00:00
}
// Parse page data update
function parseDataUpdate()
{
if($this->statusCode == 0)
{
$fileHandle = @fopen($this->fileName, "r");
if($fileHandle)
{
$this->statusCode = 200;
$this->rawData = fread($fileHandle, filesize($this->fileName));
$this->metaData = array();
fclose($fileHandle);
$this->parseMeta();
}
}
}
2013-07-11 20:33:28 +00:00
// Parse page meta data
function parseMeta()
2013-04-07 18:04:09 +00:00
{
2013-10-16 21:11:24 +00:00
$fileDate = date("c", is_readable($this->fileName) ? filemtime($this->fileName) : 0);
$this->set("modified", $fileDate);
2013-07-11 20:33:28 +00:00
$this->set("title", $this->yellow->toolbox->createTextTitle($this->location));
$this->set("sitename", $this->yellow->config->get("sitename"));
2013-07-11 20:33:28 +00:00
$this->set("author", $this->yellow->config->get("author"));
$this->set("style", $this->yellow->toolbox->findNameFromFile($this->fileName,
$this->yellow->config->get("styleDir"), $this->yellow->config->get("style"), ".css"));
2014-08-29 07:15:49 +00:00
$this->set("template", $this->yellow->toolbox->findNameFromFile($this->fileName,
$this->yellow->config->get("templateDir"), $this->yellow->config->get("template"), ".php"));
$this->set("language", $this->yellow->toolbox->findLanguageFromFile($this->fileName,
$this->yellow->config->get("contentDir"), $this->yellow->config->get("contentRootDir"),
$this->yellow->config->get("language")));
2013-07-11 20:33:28 +00:00
$this->set("parser", $this->yellow->config->get("parser"));
2013-07-11 20:33:28 +00:00
if(preg_match("/^(\-\-\-[\r\n]+)(.+?)([\r\n]+\-\-\-[\r\n]+)/s", $this->rawData, $parsed))
2013-04-14 22:41:04 +00:00
{
2013-07-11 20:33:28 +00:00
$this->metaDataOffsetBytes = strlenb($parsed[0]);
2013-09-17 09:18:01 +00:00
foreach(preg_split("/[\r\n]+/", $parsed[2]) as $line)
{
preg_match("/^\s*(.*?)\s*:\s*(.*?)\s*$/", $line, $matches);
if(!empty($matches[1]) && !strempty($matches[2])) $this->set(lcfirst($matches[1]), $matches[2]);
2013-09-17 09:18:01 +00:00
}
2013-07-11 20:33:28 +00:00
} else if(preg_match("/^([^\r\n]+)([\r\n]+=+[\r\n]+)/", $this->rawData, $parsed)) {
$this->metaDataOffsetBytes = strlenb($parsed[0]);
2013-04-14 22:41:04 +00:00
$this->set("title", $parsed[1]);
}
$shortHeader = $this->location == $this->yellow->pages->getHomeLocation($this->location);
$titleHeader = $shortHeader ? $this->get("sitename") : $this->get("title")." - ".$this->get("sitename");
2013-09-17 09:18:01 +00:00
if(!$this->isExisting("titleHeader")) $this->set("titleHeader", $titleHeader);
if(!$this->isExisting("titleNavigation")) $this->set("titleNavigation", $this->get("title"));
2014-01-08 16:58:52 +00:00
if(!$this->isExisting("titleContent")) $this->set("titleContent", $this->get("title"));
2014-05-15 11:53:54 +00:00
$this->set("pageRead", $this->yellow->toolbox->getUrl(
$this->yellow->config->get("serverScheme"), $this->yellow->config->get("serverName"),
2013-12-11 14:13:38 +00:00
$this->yellow->config->get("serverBase"), $this->location));
2014-05-15 11:53:54 +00:00
$this->set("pageEdit", $this->yellow->toolbox->getUrl(
$this->yellow->config->get("webinterfaceServerScheme"), $this->yellow->config->get("webinterfaceServerName"),
2013-12-11 14:13:38 +00:00
$this->yellow->config->get("serverBase"), rtrim($this->yellow->config->get("webinterfaceLocation"), '/').$this->location));
2013-09-17 09:18:01 +00:00
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onParseMeta"))
{
$output = $value["obj"]->onParseMeta($this, $this->rawData);
2013-10-16 21:11:24 +00:00
if(!is_null($output)) break;
2013-09-17 09:18:01 +00:00
}
}
2013-10-16 21:11:24 +00:00
}
// Parse page content on demand
2013-05-01 20:16:05 +00:00
function parseContent()
{
2013-09-17 09:18:01 +00:00
if(!is_object($this->parser))
2013-05-01 20:16:05 +00:00
{
2013-09-17 09:18:01 +00:00
if($this->yellow->plugins->isExisting($this->get("parser")))
{
2013-12-21 13:10:15 +00:00
$plugin = $this->yellow->plugins->plugins[$this->get("parser")];
2014-07-10 08:42:32 +00:00
if(method_exists($plugin["obj"], "onParseContentText"))
2013-12-21 13:10:15 +00:00
{
$this->parser = $plugin["obj"];
2014-07-10 08:42:32 +00:00
$this->parserData = $this->parser->onParseContentText($this, $this->getContent(true));
2014-01-08 16:58:52 +00:00
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onParseContent"))
{
$output = $value["obj"]->onParseContent($this, $this->parserData);
if(!is_null($output)) { $this->parserData = $output; break; }
2014-01-08 16:58:52 +00:00
}
}
if(!$this->isExisting("description"))
{
$this->set("description", $this->yellow->toolbox->createTextDescription($this->parserData, 150));
2014-01-08 16:58:52 +00:00
}
if(!$this->isExisting("keywords"))
{
$this->set("keywords", $this->yellow->toolbox->createTextKeywords($this->get("title"), 10));
}
2014-01-27 11:30:39 +00:00
}
2013-05-01 20:16:05 +00:00
}
2013-12-01 11:59:07 +00:00
if(defined("DEBUG") && DEBUG>=2) echo "YellowPage::parseContent location:".$this->location."<br/>\n";
2013-05-01 20:16:05 +00:00
}
}
2013-07-11 20:33:28 +00:00
2014-07-10 08:42:32 +00:00
// Parse page custom type
2013-08-28 10:01:46 +00:00
function parseType($name, $text, $typeShortcut)
{
$output = NULL;
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onParseType"))
{
2014-07-10 08:42:32 +00:00
$output = $value["obj"]->onParseType($this, $name, $text, $typeShortcut);
2013-08-28 10:01:46 +00:00
if(!is_null($output)) break;
}
}
if(defined("DEBUG") && DEBUG>=3 && !empty($name)) echo "YellowPage::parseType name:$name shortcut:$typeShortcut<br/>\n";
2013-08-28 10:01:46 +00:00
return $output;
}
// Set page response header
function setHeader($key, $value)
{
$this->headerData[$key] = $value;
}
// Return page response header
function getHeader($key)
{
return $this->isHeader($key) ? $this->headerData[$key] : "";
}
2013-04-14 22:41:04 +00:00
// Set page meta data
2013-04-07 18:04:09 +00:00
function set($key, $value)
{
$this->metaData[$key] = $value;
}
2014-02-20 15:11:00 +00:00
2013-04-14 22:41:04 +00:00
// Return page meta data
2013-04-07 18:04:09 +00:00
function get($key)
{
return $this->isExisting($key) ? $this->metaData[$key] : "";
2013-04-07 18:04:09 +00:00
}
2013-04-14 22:41:04 +00:00
// Return page meta data, HTML encoded
2013-04-07 18:04:09 +00:00
function getHtml($key)
{
return htmlspecialchars($this->get($key));
}
2013-10-16 21:11:24 +00:00
// Return page content, HTML encoded or raw format
function getContent($rawFormat = false)
2013-04-14 22:41:04 +00:00
{
2013-10-16 21:11:24 +00:00
if($rawFormat)
{
$this->parseDataUpdate();
2013-10-16 21:11:24 +00:00
$text = substrb($this->rawData, $this->metaDataOffsetBytes);
} else {
$this->parseContent();
$text = $this->parserData;
2013-10-16 21:11:24 +00:00
}
return $text;
2013-04-14 22:41:04 +00:00
}
2014-05-29 21:33:01 +00:00
// Return page extra header, HTML encoded
function getHeaderExtra()
{
$header = "";
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onHeaderExtra")) $header .= $value["obj"]->onHeaderExtra($this);
}
return $header;
}
// Return parent page relative to current page, NULL if none
2014-02-20 15:11:00 +00:00
function getParent()
{
$parentLocation = $this->yellow->pages->getParentLocation($this->location);
2014-07-25 10:46:58 +00:00
return $this->yellow->pages->find($parentLocation);
2014-02-20 15:11:00 +00:00
}
// Return top-level page for current page, NULL if none
function getParentTop($homeFailback = true)
2014-02-20 15:11:00 +00:00
{
$parentTopLocation = $this->yellow->pages->getParentTopLocation($this->location);
if(!$this->yellow->pages->find($parentTopLocation) && $homeFailback)
{
$parentTopLocation = $this->yellow->pages->getHomeLocation($this->location);
}
2014-07-25 10:46:58 +00:00
return $this->yellow->pages->find($parentTopLocation);
2014-02-20 15:11:00 +00:00
}
// Return pages on the same level as current page
function getSiblings($showInvisible = false)
{
$parentLocation = $this->yellow->pages->getParentLocation($this->location);
return $this->yellow->pages->findChildren($parentLocation, $showInvisible);
}
// Return child pages relative to current page
function getChildren($showInvisible = false)
{
return $this->yellow->pages->findChildren($this->location, $showInvisible);
}
2013-06-27 17:00:03 +00:00
// Return absolute page location
2013-04-14 22:41:04 +00:00
function getLocation()
{
2014-05-15 11:53:54 +00:00
return $this->base.$this->location;
2013-04-14 22:41:04 +00:00
}
2014-05-15 11:53:54 +00:00
// Return page URL, with server scheme and server name
2013-11-29 12:16:14 +00:00
function getUrl()
{
2014-05-15 11:53:54 +00:00
return $this->yellow->toolbox->getUrl($this->serverScheme, $this->serverName, $this->base, $this->location);
2013-11-29 12:16:14 +00:00
}
2013-06-27 17:00:03 +00:00
// Return page modification time, Unix time
function getModified($httpFormat = false)
2013-05-01 20:16:05 +00:00
{
$modified = strtotime($this->get("modified"));
return $httpFormat ? $this->yellow->toolbox->getHttpTimeFormatted($modified) : $modified;
2013-05-01 20:16:05 +00:00
}
// Return page status code
function getStatusCode($httpFormat = false)
{
$statusCode = $this->statusCode;
if($httpFormat)
{
$statusCode = $this->yellow->toolbox->getHttpStatusFormatted($statusCode);
if($this->isExisting("pageError")) $statusCode .= ": ".$this->get("pageError");
}
return $statusCode;
}
2014-02-20 15:11:00 +00:00
// Respond with error page
function error($statusCode, $pageError = "")
{
2014-02-20 15:11:00 +00:00
if(!$this->isExisting("pageError") && $statusCode>0)
{
$this->statusCode = $statusCode;
$this->set("pageError", empty($pageError) ? "Template/snippet error!" : $pageError);
}
2013-05-01 20:16:05 +00:00
}
2014-02-20 15:11:00 +00:00
// Respond with status code, no page content
function clean($statusCode, $responseHeader = "")
2013-05-01 20:16:05 +00:00
{
2014-02-20 15:11:00 +00:00
if(!$this->isExisting("pageClean") && $statusCode>0)
{
$this->statusCode = $statusCode;
$this->headerData = array();
if(!empty($responseHeader)) $this->header($responseHeader);
$this->set("pageClean", (string)$statusCode);
}
2013-05-01 20:16:05 +00:00
}
2014-02-20 15:11:00 +00:00
// Add page response header, HTTP format
function header($responseHeader)
2013-05-01 20:16:05 +00:00
{
2014-02-20 15:11:00 +00:00
$tokens = explode(':', $responseHeader, 2);
$this->setHeader(trim($tokens[0]), trim($tokens[1]));
2013-05-01 20:16:05 +00:00
}
// Check if response header exists
function isHeader($key)
{
return !is_null($this->headerData[$key]);
}
2013-07-11 20:33:28 +00:00
2014-02-20 15:11:00 +00:00
// Check if page meta data exists
2013-04-14 22:41:04 +00:00
function isExisting($key)
{
return !is_null($this->metaData[$key]);
}
// Check if page with error
function isError()
{
return $this->isExisting("pageError");
}
2013-05-01 20:16:05 +00:00
// Check if page is within current HTTP request
2013-04-14 22:41:04 +00:00
function isActive()
{
return $this->active;
}
2013-07-11 20:33:28 +00:00
// Check if page is visible in navigation
function isVisible()
{
return $this->visible;
}
// Check if page is cacheable
function isCacheable()
2013-04-14 22:41:04 +00:00
{
2013-07-11 20:33:28 +00:00
return $this->cacheable;
2013-04-14 22:41:04 +00:00
}
2013-04-07 18:04:09 +00:00
}
2013-05-01 20:16:05 +00:00
// Yellow page collection as array
2013-12-01 11:59:07 +00:00
class YellowPageCollection extends ArrayObject
2013-04-07 18:04:09 +00:00
{
2013-07-16 16:56:27 +00:00
var $yellow; //access to API
2014-02-04 14:06:48 +00:00
var $filterValue; //current page filter value
2013-05-01 20:16:05 +00:00
var $paginationPage; //current page number in pagination
var $paginationCount; //highest page number in pagination
2013-04-14 22:41:04 +00:00
2013-10-16 21:11:24 +00:00
function __construct($yellow)
2013-04-14 22:41:04 +00:00
{
parent::__construct(array());
2013-07-16 16:56:27 +00:00
$this->yellow = $yellow;
}
2013-04-14 22:41:04 +00:00
2013-05-01 20:16:05 +00:00
// Filter page collection by meta data
function filter($key, $value, $exactMatch = true)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
if(!empty($key))
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
$array = array();
2014-02-04 14:06:48 +00:00
$value = strreplaceu(' ', '-', strtoloweru($value));
2013-05-01 20:16:05 +00:00
$valueLength = strlenu($value);
foreach($this->getArrayCopy() as $page)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
if($page->isExisting($key))
{
2014-02-04 14:06:48 +00:00
foreach(preg_split("/,\s*/", $page->get($key)) as $pageValue)
2013-05-01 20:16:05 +00:00
{
2014-02-04 14:06:48 +00:00
$pageValueLength = $exactMatch ? strlenu($pageValue) : $valueLength;
if($value == substru(strreplaceu(' ', '-', strtoloweru($pageValue)), 0, $pageValueLength))
{
$this->filterValue = substru($pageValue, 0, $pageValueLength);
2014-02-04 14:06:48 +00:00
array_push($array, $page);
}
2013-05-01 20:16:05 +00:00
}
}
2013-04-14 22:41:04 +00:00
}
2013-05-01 20:16:05 +00:00
$this->exchangeArray($array);
2013-04-14 22:41:04 +00:00
}
2013-05-01 20:16:05 +00:00
return $this;
}
// Sort page collection by meta data
function sort($key, $ascendingOrder = true)
{
$callback = function($a, $b) use ($key, $ascendingOrder)
{
return $ascendingOrder ?
strnatcasecmp($a->get($key), $b->get($key)) :
strnatcasecmp($b->get($key), $a->get($key));
};
$array = $this->getArrayCopy();
usort($array, $callback);
$this->exchangeArray($array);
return $this;
}
2013-05-01 20:16:05 +00:00
2013-07-16 16:56:27 +00:00
// Merge page collection
function merge($input)
{
$this->exchangeArray(array_merge($this->getArrayCopy(), (array)$input));
return $this;
}
2013-09-17 09:18:01 +00:00
// Append to end of page collection
function append($page)
{
parent::append($page);
return $this;
}
// Prepend to start of page collection
function prepend($page)
{
$array = $this->getArrayCopy();
array_unshift($array, $page);
$this->exchangeArray($array);
return $this;
}
// Limit the number of pages in page collection
function limit($pagesMax)
{
$this->exchangeArray(array_slice($this->getArrayCopy(), 0, $pagesMax));
return $this;
}
2013-05-01 20:16:05 +00:00
// Reverse page collection
function reverse()
2013-05-01 20:16:05 +00:00
{
$this->exchangeArray(array_reverse($this->getArrayCopy()));
return $this;
}
// Paginate page collection
function pagination($limit, $reverse = true)
{
$array = $this->getArrayCopy();
if($reverse) $array = array_reverse($array);
$this->paginationPage = 1;
$this->paginationCount = ceil($this->count() / $limit);
if($limit < $this->count())
{
$pagination = $this->yellow->config->get("contentPagination");
if(isset($_REQUEST[$pagination])) $this->paginationPage = max(1, $_REQUEST[$pagination]);
}
2013-05-01 20:16:05 +00:00
$this->exchangeArray(array_slice($array, ($this->paginationPage - 1) * $limit, $limit));
return $this;
}
// Return current page number in pagination
function getPaginationPage()
{
2013-07-11 20:33:28 +00:00
return $this->paginationPage;
2013-05-01 20:16:05 +00:00
}
// Return highest page number in pagination
function getPaginationCount()
{
return $this->paginationCount;
2013-04-14 22:41:04 +00:00
}
2013-06-27 17:00:03 +00:00
// Return absolute location for a page in pagination
2013-05-01 20:16:05 +00:00
function getLocationPage($pageNumber)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
if($pageNumber>=1 && $pageNumber<=$this->paginationCount)
2013-04-14 22:41:04 +00:00
{
$pagination = $this->yellow->config->get("contentPagination");
2013-12-21 13:10:15 +00:00
$location = $this->yellow->page->getLocation();
$locationArgs = $this->yellow->toolbox->getLocationArgsNew(
$pageNumber>1 ? "$pagination:$pageNumber" : "$pagination:", $pagination);
2013-04-14 22:41:04 +00:00
}
2013-12-21 13:10:15 +00:00
return $location.$locationArgs;
2013-04-14 22:41:04 +00:00
}
2013-06-27 17:00:03 +00:00
// Return absolute location for previous page in pagination
2013-05-01 20:16:05 +00:00
function getLocationPrevious()
{
$pageNumber = $this->paginationPage;
$pageNumber = ($pageNumber>1 && $pageNumber<=$this->paginationCount) ? $pageNumber-1 : 0;
return $this->getLocationPage($pageNumber);
}
2013-06-27 17:00:03 +00:00
// Return absolute location for next page in pagination
2013-05-01 20:16:05 +00:00
function getLocationNext()
{
$pageNumber = $this->paginationPage;
$pageNumber = ($pageNumber>=1 && $pageNumber<$this->paginationCount) ? $pageNumber+1 : 0;
return $this->getLocationPage($pageNumber);
}
2013-06-27 17:00:03 +00:00
2014-02-04 14:06:48 +00:00
// Return current page filter
function getFilter()
{
return $this->filterValue;
}
2013-06-27 17:00:03 +00:00
// 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());
2013-07-16 16:56:27 +00:00
return $httpFormat ? $this->yellow->toolbox->getHttpTimeFormatted($modified) : $modified;
2013-06-27 17:00:03 +00:00
}
// Return first page in page collection
function first()
{
return $this->offsetGet(0);
}
2013-05-01 20:16:05 +00:00
// Return last page in page collection
function last()
{
return $this->offsetGet($this->count()-1);
}
2013-05-01 20:16:05 +00:00
// Check if there is an active pagination
function isPagination()
{
return $this->paginationCount > 1;
}
}
2014-03-21 09:12:56 +00:00
// Yellow pages from file system
2013-12-01 11:59:07 +00:00
class YellowPages
2013-04-07 18:04:09 +00:00
{
var $yellow; //access to API
var $pages; //scanned pages
var $requestHandler; //request handler name
var $snippetArgs; //requested snippet arguments
2013-05-01 20:16:05 +00:00
2013-07-16 16:56:27 +00:00
function __construct($yellow)
2013-04-14 22:41:04 +00:00
{
2013-07-11 20:33:28 +00:00
$this->yellow = $yellow;
2013-11-29 12:16:14 +00:00
$this->pages = array();
2013-05-01 20:16:05 +00:00
}
2013-09-17 09:18:01 +00:00
// Return page collection with all pages from file system
function index($showInvisible = false, $showLanguages = false, $levelMax = 0)
2013-07-16 16:56:27 +00:00
{
$rootLocation = $showLanguages ? "" : $this->getRootLocation($this->yellow->page->location);
return $this->findChildrenRecursive($rootLocation, $showInvisible, $levelMax);
2013-07-16 16:56:27 +00:00
}
2013-05-01 20:16:05 +00:00
// Return page collection with top-level navigation
function top($showInvisible = false)
2013-05-01 20:16:05 +00:00
{
$rootLocation = $this->getRootLocation($this->yellow->page->location);
return $this->findChildren($rootLocation, $showInvisible);
2013-05-01 20:16:05 +00:00
}
2013-09-17 09:18:01 +00:00
// Return page collection with path ancestry
function path($location, $absoluteLocation = false)
{
2014-07-25 10:46:58 +00:00
$pages = new YellowPageCollection($this->yellow);
if($page = $this->find($location, $absoluteLocation))
2013-09-17 09:18:01 +00:00
{
2014-07-25 10:46:58 +00:00
$pages->prepend($page);
for(; $parent = $page->getParent(); $page=$parent) $pages->prepend($parent);
$home = $this->find($this->getHomeLocation($page->location));
if($home && $home->location!=$page->location) $pages->prepend($home);
2013-09-17 09:18:01 +00:00
}
return $pages;
}
// Return page collection with multiple languages
function translation($location, $absoluteLocation = false, $showInvisible = false)
{
$pages = new YellowPageCollection($this->yellow);
if($absoluteLocation) $location = substru($location, strlenu($this->yellow->page->base));
$postfix = substru($location, strlenu($this->getRootLocation($location)) - 4);
$this->scanChildren("");
foreach($this->pages[""] as $page)
{
if($content = $this->find(substru($page->location, 4).$postfix))
{
if($content->isVisible() || $showInvisible) $pages->append($content);
}
}
return $pages;
}
2014-07-25 10:46:58 +00:00
// Return empty page collection
function create()
2013-07-16 16:56:27 +00:00
{
2014-07-25 10:46:58 +00:00
return new YellowPageCollection($this->yellow);
2013-07-16 16:56:27 +00:00
}
2013-05-01 20:16:05 +00:00
// Return one page from file system, NULL if not found
function find($location, $absoluteLocation = false)
{
if(!$this->yellow->toolbox->isRootLocation($location))
{
if($absoluteLocation) $location = substru($location, strlenu($this->yellow->page->base));
$parentLocation = $this->getParentLocation($location);
$this->scanChildren($parentLocation);
foreach($this->pages[$parentLocation] as $page) if($page->location == $location) { $found = true; break; }
}
return $found ? $page : NULL;
}
2013-07-16 16:56:27 +00:00
// Find child pages
2014-02-20 15:11:00 +00:00
function findChildren($location, $showInvisible = false)
2013-05-01 20:16:05 +00:00
{
2013-12-01 11:59:07 +00:00
$pages = new YellowPageCollection($this->yellow);
$this->scanChildren($location);
foreach($this->pages[$location] as $page)
{
if($page->isVisible() || $showInvisible)
{
if(!$this->yellow->toolbox->isRootLocation($page->location)) $pages->append($page);
}
}
2013-05-01 20:16:05 +00:00
return $pages;
2013-04-14 22:41:04 +00:00
}
2013-07-16 16:56:27 +00:00
// Find child pages recursively
2014-02-20 15:11:00 +00:00
function findChildrenRecursive($location, $showInvisible = false, $levelMax = 0)
2013-04-14 22:41:04 +00:00
{
2013-07-16 16:56:27 +00:00
--$levelMax;
2013-12-01 11:59:07 +00:00
$pages = new YellowPageCollection($this->yellow);
$this->scanChildren($location);
2013-07-16 16:56:27 +00:00
foreach($this->pages[$location] as $page)
{
2014-02-20 15:11:00 +00:00
if($page->isVisible() || $showInvisible)
2013-07-16 16:56:27 +00:00
{
if(!$this->yellow->toolbox->isRootLocation($page->location)) $pages->append($page);
2013-07-16 16:56:27 +00:00
if(!$this->yellow->toolbox->isFileLocation($page->location) && $levelMax!=0)
{
2014-02-20 15:11:00 +00:00
$pages->merge($this->findChildrenRecursive($page->location, $showInvisible, $levelMax));
2013-07-16 16:56:27 +00:00
}
}
}
return $pages;
2013-05-01 20:16:05 +00:00
}
// Scan child pages on demand
function scanChildren($location)
{
if(is_null($this->pages[$location]))
{
2013-12-01 11:59:07 +00:00
if(defined("DEBUG") && DEBUG>=2) echo "YellowPages::scanChildren location:$location<br/>\n";
2013-05-01 20:16:05 +00:00
$this->pages[$location] = array();
if(empty($location))
2013-05-01 20:16:05 +00:00
{
$rootLocations = $this->yellow->toolbox->findRootLocations($this->yellow->config->get("contentDir"),
$this->yellow->config->get("contentRootDir"));
foreach($rootLocations as $rootLocation)
2013-05-01 20:16:05 +00:00
{
$page = new YellowPage($this->yellow,
$this->yellow->page->serverScheme, $this->yellow->page->serverName, $this->yellow->page->base,
$rootLocation, "");
$page->parseData("", false, 0);
array_push($this->pages[$location], $page);
}
} else {
$fileNames = $this->yellow->toolbox->findChildrenFromLocation($location, $this->yellow->config->get("contentDir"),
$this->yellow->config->get("contentRootDir"), $this->yellow->config->get("contentHomeDir"),
$this->yellow->config->get("contentDefaultFile"), $this->yellow->config->get("contentExtension"));
foreach($fileNames as $fileName)
{
$fileHandle = @fopen($fileName, "r");
if($fileHandle)
{
$fileData = fread($fileHandle, 4096);
$statusCode = filesize($fileName) <= 4096 ? 200 : 0;
fclose($fileHandle);
} else {
$fileData = "";
$statusCode = 0;
}
$page = new YellowPage($this->yellow,
$this->yellow->page->serverScheme, $this->yellow->page->serverName, $this->yellow->page->base,
$this->yellow->toolbox->findLocationFromFile($fileName, $this->yellow->config->get("contentDir"),
$this->yellow->config->get("contentRootDir"), $this->yellow->config->get("contentHomeDir"),
$this->yellow->config->get("contentDefaultFile"), $this->yellow->config->get("contentExtension")),
$fileName);
$page->parseData($fileData, false, $statusCode);
array_push($this->pages[$location], $page);
2013-05-01 20:16:05 +00:00
}
}
}
2013-04-14 22:41:04 +00:00
}
// Return root location
function getRootLocation($location)
{
$rootLocation = "root/";
if($this->yellow->config->get("multiLanguageMode"))
{
$this->scanChildren("");
foreach($this->pages[""] as $page)
{
$token = substru($page->location, 4);
if($token!="/" && substru($location, 0, strlenu($token))==$token) { $rootLocation = "root$token"; break; }
}
}
return $rootLocation;
}
// Return home location
function getHomeLocation($location)
{
return substru($this->getRootLocation($location), 4);
}
2013-07-16 16:56:27 +00:00
// Return parent location
2013-05-01 20:16:05 +00:00
function getParentLocation($location)
{
$prefix = rtrim(substru($this->getRootLocation($location), 4), '/');
if(preg_match("#^($prefix.*\/).+?$#", $location, $matches))
{
if($matches[1]!="$prefix/" || $this->yellow->toolbox->isFileLocation($location)) $parentLocation = $matches[1];
}
if(empty($parentLocation)) $parentLocation = "root$prefix/";
2013-05-01 20:16:05 +00:00
return $parentLocation;
}
// Return top-level location
function getParentTopLocation($location)
{
$prefix = rtrim(substru($this->getRootLocation($location), 4), '/');
if(preg_match("#^($prefix.+?\/)#", $location, $matches)) $parentTopLocation = $matches[1];
if(empty($parentTopLocation)) $parentTopLocation = "$prefix/";
return $parentTopLocation;
}
2013-05-01 20:16:05 +00:00
}
2013-11-29 12:16:14 +00:00
// Yellow configuration
2013-12-01 11:59:07 +00:00
class YellowConfig
2013-11-29 12:16:14 +00:00
{
var $yellow; //access to API
var $modified; //configuration modification time
var $config; //configuration
var $configDefaults; //configuration defaults
function __construct($yellow)
{
$this->yellow = $yellow;
$this->modified = 0;
$this->config = array();
$this->configDefaults = array();
}
// Load configuration from file
function load($fileName)
{
$fileData = @file($fileName);
if($fileData)
{
2013-12-01 11:59:07 +00:00
if(defined("DEBUG") && DEBUG>=2) echo "YellowConfig::load file:$fileName<br/>\n";
2013-11-29 12:16:14 +00:00
$this->modified = filemtime($fileName);
foreach($fileData as $line)
{
if(preg_match("/^\//", $line)) continue;
preg_match("/^\s*(.*?)\s*=\s*(.*?)\s*$/", $line, $matches);
if(!empty($matches[1]) && !strempty($matches[2]))
{
$this->set($matches[1], $matches[2]);
2013-12-01 11:59:07 +00:00
if(defined("DEBUG") && DEBUG>=3) echo "YellowConfig::load key:$matches[1] $matches[2]<br/>\n";
2013-11-29 12:16:14 +00:00
}
}
}
}
// Set default configuration
function setDefault($key, $value)
{
$this->configDefaults[$key] = $value;
}
// Set configuration
function set($key, $value)
{
$this->config[$key] = $value;
}
// Return configuration
function get($key)
{
return $this->isExisting($key) ? $this->config[$key] : $this->configDefaults[$key];
}
// Return configuration, HTML encoded
function getHtml($key)
{
return htmlspecialchars($this->get($key));
}
// Return configuration strings
2014-08-19 21:44:22 +00:00
function getData($filterStart = "", $filterEnd = "")
2013-11-29 12:16:14 +00:00
{
$config = array();
2014-08-19 21:44:22 +00:00
if(empty($filterStart) && empty($filterEnd))
2013-11-29 12:16:14 +00:00
{
$config = $this->config;
} else {
foreach($this->config as $key=>$value)
{
2014-08-19 21:44:22 +00:00
if(!empty($filterStart) && substru($key, 0, strlenu($filterStart))==$filterStart) $config[$key] = $value;
if(!empty($filterEnd) && substru($key, -strlenu($filterEnd))==$filterEnd) $config[$key] = $value;
2013-11-29 12:16:14 +00:00
}
}
return $config;
}
// Return configuration modification time, Unix time
function getModified($httpFormat = false)
{
return $httpFormat ? $this->yellow->toolbox->getHttpTimeFormatted($this->modified) : $this->modified;
}
// Check if configuration exists
function isExisting($key)
{
return !is_null($this->config[$key]);
}
}
// Yellow text strings
2013-12-01 11:59:07 +00:00
class YellowText
2013-11-29 12:16:14 +00:00
{
var $yellow; //access to API
var $modified; //text modification time
var $text; //text strings
var $language; //current language
function __construct($yellow)
{
$this->yellow = $yellow;
$this->modified = 0;
$this->text = array();
}
// Load text strings from file
function load($fileName)
{
$path = dirname($fileName);
2014-06-27 09:36:30 +00:00
$regex = "/^".basename($fileName)."$/";
2013-11-29 12:16:14 +00:00
foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false) as $entry)
{
$fileData = @file($entry);
2013-11-29 12:16:14 +00:00
if($fileData)
{
if(defined("DEBUG") && DEBUG>=2) echo "YellowText::load file:$entry<br/>\n";
$this->modified = max($this->modified, filemtime($entry));
2013-11-29 12:16:14 +00:00
$language = "";
foreach($fileData as $line)
{
preg_match("/^\s*(.*?)\s*=\s*(.*?)\s*$/", $line, $matches);
if($matches[1]=="language" && !strempty($matches[2])) { $language = $matches[2]; break; }
2013-11-29 12:16:14 +00:00
}
foreach($fileData as $line)
{
if(preg_match("/^\//", $line)) continue;
preg_match("/^\s*(.*?)\s*=\s*(.*?)\s*$/", $line, $matches);
if(!empty($language) && !empty($matches[1]) && !strempty($matches[2]))
{
2013-12-11 14:13:38 +00:00
$this->setText($matches[1], $matches[2], $language);
2013-12-01 11:59:07 +00:00
if(defined("DEBUG") && DEBUG>=3) echo "YellowText::load key:$matches[1] $matches[2]<br/>\n";
2013-11-29 12:16:14 +00:00
}
}
}
}
}
// Set current language
function setLanguage($language)
{
$this->language = $language;
}
// Set text string for specific language
2013-12-11 14:13:38 +00:00
function setText($key, $value, $language)
2013-11-29 12:16:14 +00:00
{
if(is_null($this->text[$language])) $this->text[$language] = array();
$this->text[$language][$key] = $value;
}
// Return text string for specific language
2013-12-11 14:13:38 +00:00
function getText($key, $language)
2013-11-29 12:16:14 +00:00
{
2013-12-11 14:13:38 +00:00
return ($this->isText($key, $language)) ? $this->text[$language][$key] : "[$key]";
2013-11-29 12:16:14 +00:00
}
// Return text string for specific language, HTML encoded
function getTextHtml($key, $language)
{
return htmlspecialchars($this->getText($key, $language));
}
2013-11-29 12:16:14 +00:00
// Return text string
function get($key)
{
return $this->isExisting($key) ? $this->text[$this->language][$key] : "[$key]";
}
// Return text string, HTML encoded
function getHtml($key)
{
return htmlspecialchars($this->get($key));
}
2013-12-11 14:13:38 +00:00
// Return text strings
function getData($filterStart = "", $language = "")
2013-11-29 12:16:14 +00:00
{
$text = array();
2013-12-11 14:13:38 +00:00
if(empty($language)) $language = $this->language;
2013-11-29 12:16:14 +00:00
if(!is_null($this->text[$language]))
{
if(empty($filterStart))
{
$text = $this->text[$language];
} else {
foreach($this->text[$language] as $key=>$value)
{
if(substru($key, 0, strlenu("language")) == "language") $text[$key] = $value;
if(substru($key, 0, strlenu($filterStart)) == $filterStart) $text[$key] = $value;
}
}
}
return $text;
}
// Return text modification time, Unix time
function getModified($httpFormat = false)
{
return $httpFormat ? $this->yellow->toolbox->getHttpTimeFormatted($this->modified) : $this->modified;
}
// Check if text string for specific language exists
2013-12-11 14:13:38 +00:00
function isText($key, $language)
2013-11-29 12:16:14 +00:00
{
return !is_null($this->text[$language]) && !is_null($this->text[$language][$key]);
}
// Check if text string exists
function isExisting($key)
{
return !is_null($this->text[$this->language]) && !is_null($this->text[$this->language][$key]);
}
}
2013-05-01 20:16:05 +00:00
// Yellow toolbox with helpers
2013-12-01 11:59:07 +00:00
class YellowToolbox
2013-05-01 20:16:05 +00:00
{
2014-05-15 11:53:54 +00:00
// Return server scheme from current HTTP request
function getServerScheme()
{
$serverScheme = "";
if(preg_match("/^HTTP\//", $_SERVER["SERVER_PROTOCOL"]))
{
$secure = isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]!="off";
$serverScheme = $secure ? "https" : "http";
}
return $serverScheme;
}
2013-06-07 20:01:12 +00:00
// Return server name from current HTTP request
2013-10-16 21:11:24 +00:00
function getServerName()
2013-04-14 22:41:04 +00:00
{
2013-06-07 20:01:12 +00:00
return $_SERVER["SERVER_NAME"];
}
// Return server base from current HTTP request
2013-10-16 21:11:24 +00:00
function getServerBase()
2013-06-07 20:01:12 +00:00
{
$serverBase = "";
2013-06-07 20:01:12 +00:00
if(preg_match("/^(.*)\//", $_SERVER["SCRIPT_NAME"], $matches)) $serverBase = $matches[1];
return $serverBase;
2013-04-14 22:41:04 +00:00
}
2013-05-01 20:16:05 +00:00
// Return location from current HTTP request
2013-10-16 21:11:24 +00:00
function getLocation()
2013-05-01 20:16:05 +00:00
{
$uri = $_SERVER["REQUEST_URI"];
2014-01-27 11:30:39 +00:00
return rawurldecode(($pos = strposu($uri, '?')) ? substru($uri, 0, $pos) : $uri);
2013-05-01 20:16:05 +00:00
}
2014-01-27 11:30:39 +00:00
// Return location from current HTTP request, remove unwanted path tokens
function getLocationClean()
2013-12-21 13:10:15 +00:00
{
2014-01-27 11:30:39 +00:00
$string = $this->getLocation();
2013-12-21 13:10:15 +00:00
$location = ($string[0]=='/') ? '' : '/';
for($pos=0; $pos<strlenb($string); ++$pos)
{
if($string[$pos] == '/')
{
if($string[$pos+1] == '/') continue;
if($string[$pos+1] == '.')
{
$posNew = $pos+1; while($string[$posNew] == '.') ++$posNew;
if($string[$posNew]=='/' || $string[$posNew]=='')
{
$pos = $posNew-1;
continue;
}
}
}
$location .= $string[$pos];
}
if(preg_match("/^(.*?\/)([^\/]+:.*)$/", $location, $matches))
{
$location = $matches[1];
foreach(explode('/', $matches[2]) as $token)
{
preg_match("/^(.*?):(.*)$/", $token, $matches);
if(!empty($matches[1]) && !strempty($matches[2]))
{
$matches[1] = strreplaceu(array("\x1c", "\x1d"), array('/', ':'), $matches[1]);
$matches[2] = strreplaceu(array("\x1c", "\x1d"), array('/', ':'), $matches[2]);
$_REQUEST[$matches[1]] = $matches[2];
}
}
}
2014-01-27 11:30:39 +00:00
if(!preg_match("/^HTTP\//", $_SERVER["SERVER_PROTOCOL"])) $_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1";
2013-12-21 13:10:15 +00:00
return $location;
}
2013-10-16 21:11:24 +00:00
// Return location arguments from current HTTP request
function getLocationArgs()
{
if(preg_match("/^(.*?\/)([^\/]+:.*)$/", $this->getLocation(), $matches)) $locationArgs = $matches[2];
return $locationArgs;
}
// Return location arguments from current HTTP request, modify an argument
function getLocationArgsNew($arg, $pagination)
{
2013-05-01 20:16:05 +00:00
preg_match("/^(.*?):(.*)$/", $arg, $args);
2014-01-27 11:30:39 +00:00
if(preg_match("/^(.*?\/)([^\/]+:.*)$/", $this->getLocation(), $matches))
2013-05-01 20:16:05 +00:00
{
foreach(explode('/', $matches[2]) as $token)
{
preg_match("/^(.*?):(.*)$/", $token, $matches);
if($matches[1] == $args[1]) { $matches[2] = $args[2]; $found = true; }
2013-10-16 21:11:24 +00:00
if(!empty($matches[1]) && !strempty($matches[2]))
2013-05-01 20:16:05 +00:00
{
if(!empty($locationArgs)) $locationArgs .= '/';
$locationArgs .= "$matches[1]:$matches[2]";
}
}
}
2013-10-16 21:11:24 +00:00
if(!$found && !empty($args[1]) && !strempty($args[2]))
2013-05-01 20:16:05 +00:00
{
if(!empty($locationArgs)) $locationArgs .= '/';
$locationArgs .= "$args[1]:$args[2]";
2013-05-01 20:16:05 +00:00
}
2013-12-21 13:10:15 +00:00
if(!empty($locationArgs))
{
if(!$this->isPaginationLocation($locationArgs, $pagination)) $locationArgs .= '/';
$locationArgs = $this->normaliseArgs($locationArgs, false, false);
2013-12-21 13:10:15 +00:00
}
2013-10-16 21:11:24 +00:00
return $locationArgs;
}
// Return location arguments from current HTTP request, convert form into clean URL
function getLocationArgsCleanUrl($pagination)
2013-10-16 21:11:24 +00:00
{
foreach(array_merge($_GET, $_POST) as $key=>$value)
2013-05-01 20:16:05 +00:00
{
2013-10-16 21:11:24 +00:00
if(!empty($key) && !strempty($value))
{
if(!empty($locationArgs)) $locationArgs .= '/';
$key = strreplaceu(array('/', ':'), array("\x1c", "\x1d"), $key);
$value = strreplaceu(array('/', ':'), array("\x1c", "\x1d"), $value);
$locationArgs .= "$key:$value";
}
2013-05-01 20:16:05 +00:00
}
2013-12-21 13:10:15 +00:00
if(!empty($locationArgs))
2013-05-01 20:16:05 +00:00
{
if(!$this->isPaginationLocation($locationArgs, $pagination)) $locationArgs .= '/';
$locationArgs = $this->normaliseArgs($locationArgs, false, false);
2013-04-14 22:41:04 +00:00
}
2013-12-21 13:10:15 +00:00
return $locationArgs;
2013-04-14 22:41:04 +00:00
}
2014-01-27 11:30:39 +00:00
2014-05-15 11:53:54 +00:00
// Return location header with URL
function getLocationHeader($serverScheme, $serverName, $base, $location)
{
return "Location: ".$this->getUrl($serverScheme, $serverName, $base, $location);
}
2014-01-27 11:30:39 +00:00
// Check if location contains location arguments
function isLocationArgs($location)
{
return preg_match("/[^\/]+:.*$/", $location);
}
// Check if location contains pagination
function isPaginationLocation($location, $pagination)
{
return preg_match("/^(.*\/)?$pagination:\d*$/", $location);
}
2013-12-21 13:10:15 +00:00
// Check if location is specifying root
function isRootLocation($location)
{
return $location[0] != "/";
}
// Check if location is specifying file or directory
function isFileLocation($location)
{
return substru($location, -1, 1) != "/";
}
2013-10-16 21:11:24 +00:00
// Check if file is unmodified since last HTTP request
function isFileNotModified($lastModified)
{
return isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && $_SERVER["HTTP_IF_MODIFIED_SINCE"]==$lastModified;
}
// Check if clean URL is requested
function isRequestCleanUrl($location)
{
return (isset($_GET["clean-url"]) || isset($_POST["clean-url"])) && !$this->isFileLocation($location);
}
// Check if content type is valid for location
function isValidContentType($contentType, $location)
2013-04-14 22:41:04 +00:00
{
$ok = false;
$extension = ($pos = strrposu($location, '.')) ? substru($location, $pos) : "";
if($contentType == "text/html")
{
if($this->isFileLocation($location))
{
if(empty($extension) || preg_match("/^\.(html|md)$/", $extension)) $ok = true;
} else {
$ok = true;
}
} else {
if($this->isFileLocation($location))
{
if(!empty($extension) && preg_match("/^.*$extension$/", $contentType)) $ok = true;
}
}
return $ok;
2013-04-14 22:41:04 +00:00
}
// Check if location is valid
2013-10-16 21:11:24 +00:00
function isValidLocation($location)
2013-07-11 20:33:28 +00:00
{
$string = "";
$tokens = explode('/', $location);
2013-12-21 13:10:15 +00:00
for($i=1; $i<count($tokens); ++$i) $string .= '/'.$this->normaliseName($tokens[$i]);
return $location == $string;
2013-07-11 20:33:28 +00:00
}
2013-04-14 22:41:04 +00:00
// Check if location is within current HTTP request
function isActiveLocation($location, $currentLocation, $homeLocation)
2013-04-14 22:41:04 +00:00
{
if($location != $homeLocation)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
$active = substru($currentLocation, 0, strlenu($location))==$location;
2013-04-14 22:41:04 +00:00
} else {
$active = $this->getDirectoryLocation($currentLocation)==$location;
2013-04-14 22:41:04 +00:00
}
return $active;
}
2013-07-11 20:33:28 +00:00
// Check if location is visible in navigation
2014-05-15 11:53:54 +00:00
function isVisibleLocation($location, $fileName, $pathBase)
2013-04-14 22:41:04 +00:00
{
2013-07-11 20:33:28 +00:00
$visible = true;
2014-09-02 11:14:34 +00:00
if(substru($fileName, 0, strlenu($pathBase)) == $pathBase)
2013-04-14 22:41:04 +00:00
{
2014-09-02 11:14:34 +00:00
$fileName = substru($fileName, strlenu($pathBase));
$tokens = explode('/', $fileName);
for($i=0; $i<count($tokens)-1; ++$i)
{
if(!preg_match("/^[\d\-\_\.]+(.*)$/", $tokens[$i])) { $visible = false; break; }
}
} else {
$visible = false;
2013-04-14 22:41:04 +00:00
}
2013-07-11 20:33:28 +00:00
return $visible;
2013-04-14 22:41:04 +00:00
}
// Return root configuration
function findRootConfig($pathBase, $pathRoot, $pathHome, $multiLanguageMode)
{
$path = $pathBase;
if(!$multiLanguageMode) $pathRoot = "";
if(!empty($pathRoot))
{
$token = $root = rtrim($pathRoot, '/');
foreach($this->getDirectoryEntries($path, "/.*/", true, true, false) as $entry)
{
if(empty($firstRoot)) { $firstRoot = $token = $entry; }
if($this->normaliseName($entry) == $root) { $token = $entry; break; }
}
$pathRoot = $this->normaliseName($token)."/";
$path .= "$firstRoot/";
}
if(!empty($pathHome))
{
$token = $home = rtrim($pathHome, '/');
foreach($this->getDirectoryEntries($path, "/.*/", true, true, false) as $entry)
{
if(empty($firstHome)) { $firstHome = $token = $entry; }
if($this->normaliseName($entry) == $home) { $token = $entry; break; }
}
$pathHome = $this->normaliseName($token)."/";
}
return array($pathRoot, $pathHome);
}
// Return root locations
function findRootLocations($pathBase, $pathRoot)
{
$locations = array("root/");
if(!empty($pathRoot))
{
$root = rtrim($pathRoot, '/');
foreach($this->getDirectoryEntries($pathBase, "/.*/", true, true, false) as $entry)
{
$token = $this->normaliseName($entry);
if($token != $root) array_push($locations, "root/$token/");
}
}
return $locations;
}
// Return location from file path
function findLocationFromFile($fileName, $pathBase, $pathRoot, $pathHome, $fileDefault, $fileExtension)
{
$location = "/";
2014-09-02 11:14:34 +00:00
if(substru($fileName, 0, strlenu($pathBase)) == $pathBase)
{
2014-09-02 11:14:34 +00:00
$fileName = substru($fileName, strlenu($pathBase));
$tokens = explode('/', $fileName);
if(!empty($pathRoot))
{
$token = $this->normaliseName($tokens[0]).'/';
if($token!=$pathRoot) $location .= $token;
array_shift($tokens);
}
2014-09-02 11:14:34 +00:00
for($i=0; $i<count($tokens)-1; ++$i)
{
$token = $this->normaliseName($tokens[$i]).'/';
if($i || $token!=$pathHome) $location .= $token;
}
$token = $this->normaliseName($tokens[$i]);
$fileFolder = $this->normaliseName($tokens[$i-1]).$fileExtension;
if($token!=$fileDefault && $token!=$fileFolder) $location .= $this->normaliseName($tokens[$i], true, true);
$extension = ($pos = strrposu($fileName, '.')) ? substru($fileName, $pos) : "";
if($extension != $fileExtension) $invalid = true;
} else {
$invalid = true;
}
if(defined("DEBUG") && DEBUG>=2)
{
$debug = ($invalid ? "INVALID" : $location)." <- $pathBase$fileName";
echo "YellowToolbox::findLocationFromFile $debug<br/>\n";
}
2014-09-02 11:14:34 +00:00
return $invalid ? "" : $location;
}
// Return file path from location
function findFileFromLocation($location, $pathBase, $pathRoot, $pathHome, $fileDefault, $fileExtension)
2013-04-14 22:41:04 +00:00
{
$path = $pathBase;
if($this->isRootLocation($location))
2013-04-14 22:41:04 +00:00
{
if(!empty($pathRoot))
2013-04-14 22:41:04 +00:00
{
$token = rtrim(substru($location, 5), '/');
if(empty($token)) $token = rtrim($pathRoot, '/');
2013-12-21 13:10:15 +00:00
if($this->normaliseName($token) != $token) $invalid = true;
2014-08-25 13:05:22 +00:00
$regex = $invalid ? "//" : "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry)
{
2013-12-21 13:10:15 +00:00
if($this->normaliseName($entry) == $token) { $token = $entry; break; }
}
$path .= "$token/";
2013-04-14 22:41:04 +00:00
}
} else {
$tokens = explode('/', $location);
if(!empty($pathRoot))
{
if(count($tokens) > 2)
{
$root = $tokens[1];
if($this->normaliseName($root) == $this->normaliseName($pathRoot)) $invalid = true;
if($this->normaliseName($root) != $root) $invalid = true;
$regex = $invalid ? "//" : "/^[\d\-\_\.]*".strreplaceu('-', '.', $root)."$/";
foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry)
{
if($this->normaliseName($entry) == $root) { $token = $entry; array_shift($tokens); break; }
}
}
if(empty($token))
{
$token = rtrim($pathRoot, '/');
if($this->normaliseName($token) != $token) $invalid = true;
$regex = $invalid ? "//" : "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry)
{
if($this->normaliseName($entry) == $token) { $token = $entry; break; }
}
}
$path .= "$token/";
}
if(count($tokens) > 2)
{
if($this->normaliseName($tokens[1]) == $this->normaliseName($pathHome)) $invalid = true;
for($i=1; $i<count($tokens)-1; ++$i)
{
$token = $tokens[$i];
if($this->normaliseName($token) != $token) $invalid = true;
$regex = $invalid ? "//" : "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry)
{
if($this->normaliseName($entry) == $token) { $token = $entry; break; }
}
$path .= "$token/";
}
} else {
$i = 1;
$token = rtrim($pathHome, '/');
if($this->normaliseName($token) != $token) $invalid = true;
2014-08-25 13:05:22 +00:00
$regex = $invalid ? "//" : "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach($this->getDirectoryEntries($path, $regex, false, true, false) as $entry)
{
if($this->normaliseName($entry) == $token) { $token = $entry; break; }
}
$path .= "$token/";
}
if(!empty($fileDefault) && !empty($fileExtension))
{
if(!empty($tokens[$i]))
{
$token = $tokens[$i].$fileExtension;
$fileFolder = $tokens[$i-1].$fileExtension;
if($token==$fileDefault || $token==$fileFolder) $invalid = true;
if($this->normaliseName($token) != $token) $invalid = true;
$regex = $invalid ? "//" : "/^[\d\-\_\.]*".strreplaceu('-', '.', $token)."$/";
foreach($this->getDirectoryEntries($path, $regex, false, false, false) as $entry)
{
if($this->normaliseName($entry) == $token) { $token = $entry; break; }
}
} else {
$token = $fileDefault;
if(!is_file($path."/".$fileDefault))
{
$fileFolder = $tokens[$i-1].$fileExtension;
$regex = "/^[\d\-\_\.]*($fileDefault|$fileFolder)$/";
foreach($this->getDirectoryEntries($path, $regex, true, false, false) as $entry)
{
if($this->normaliseName($entry) == $fileDefault) { $token = $entry; break; }
if($this->normaliseName($entry) == $fileFolder) { $token = $entry; break; }
}
}
}
$path .= $token;
if(defined("DEBUG") && DEBUG>=2)
{
$debug = "$location -> ".($invalid ? "INVALID" : $path);
echo "YellowToolbox::findFileFromLocation $debug<br/>\n";
}
}
}
return $invalid ? "" : $path;
2013-04-14 22:41:04 +00:00
}
// Return children from location
function findChildrenFromLocation($location, $pathBase, $pathRoot, $pathHome, $fileDefault, $fileExtension)
2013-04-14 22:41:04 +00:00
{
$fileNames = array();
if(!$this->isFileLocation($location))
{
$path = $this->findFileFromLocation($location, $pathBase, $pathRoot, $pathHome, "", "");
foreach($this->getDirectoryEntries($path, "/.*/", true, true, false) as $entry)
{
$token = $fileDefault;
if(!is_file($path.$entry."/".$fileDefault))
{
$fileFolder = $this->normaliseName($entry).$fileExtension;
$regex = "/^[\d\-\_\.]*($fileDefault|$fileFolder)$/";
foreach($this->getDirectoryEntries($path.$entry, $regex, true, false, false) as $entry2)
{
if($this->normaliseName($entry2) == $fileDefault) { $token = $entry2; break; }
if($this->normaliseName($entry2) == $fileFolder) { $token = $entry2; break; }
}
}
array_push($fileNames, $path.$entry."/".$token);
}
if(!$this->isRootLocation($location))
{
$fileFolder = $this->normaliseName(basename($path)).$fileExtension;
$regex = "/^.*\\".$fileExtension."$/";
foreach($this->getDirectoryEntries($path, $regex, true, false, false) as $entry)
{
if($this->normaliseName($entry) == $fileDefault) continue;
if($this->normaliseName($entry) == $fileFolder) continue;
if($this->normaliseName($entry, true, true) == "") continue;
array_push($fileNames, $path.$entry);
}
}
}
return $fileNames;
2014-02-04 14:06:48 +00:00
}
2014-08-29 07:15:49 +00:00
// Return file/style/template name from file path
function findNameFromFile($fileName, $pathBase, $nameDefault, $fileExtension, $includeFileName = false)
{
$name = "";
if(preg_match("/^.*\/(.+?)$/", dirname($fileName), $matches)) $name = $this->normaliseName($matches[1]);
if(!is_file("$pathBase$name$fileExtension")) $name = $this->normaliseName($nameDefault);
return $includeFileName ? "$pathBase$name$fileExtension" : $name;
}
// Return language from file path
function findLanguageFromFile($fileName, $pathBase, $pathRoot, $languageDefault)
{
$language = $languageDefault;
if(!empty($pathRoot))
{
$fileName = substru($fileName, strlenu($pathBase));
if(preg_match("/^(.+?)\//", $fileName, $matches)) $name = $this->normaliseName($matches[1]);
if(strlenu($name) == 2) $language = $name;
}
return $language;
}
// Return file path from title
function findFileFromTitle($titlePrefix, $titleText, $fileName, $fileDefault, $fileExtension)
{
preg_match("/^([\d\-\_\.]*)(.*)$/", $titlePrefix, $matches);
if(preg_match("/\d$/", $matches[1])) $matches[1] .= '-';
$titleText = $this->normaliseName($titleText, false, false, true);
preg_match("/^([\d\-\_\.]*)(.*)$/", $matches[1].$titleText, $matches);
$fileNamePrefix = $matches[1];
$fileNameText = empty($matches[2]) ? $fileDefault : $matches[2].$fileExtension;
return dirname($fileName)."/".$fileNamePrefix.$fileNameText;
}
// Normalise location, make absolute location
2014-08-25 13:05:22 +00:00
function normaliseLocation($location, $pageBase, $pageLocation, $filterStrict = true)
{
2014-09-02 11:14:34 +00:00
if(!preg_match("/^\w+:/", trim(html_entity_decode($location, ENT_QUOTES, "UTF-8"))))
{
2014-08-07 19:42:28 +00:00
if(!preg_match("/^\//", $location))
{
$location = $this->getDirectoryLocation($pageBase.$pageLocation).$location;
}
else if(!preg_match("#^$pageBase#", $location))
{
$location = $pageBase.$location;
}
2014-08-29 07:15:49 +00:00
} else {
if($filterStrict && !preg_match("/^(http|https|ftp|mailto):/", $location)) $location = "error-xss-filter";
}
return $location;
}
2014-08-25 13:05:22 +00:00
// Normalise location arguments
function normaliseArgs($text, $appendSlash = true, $filterStrict = true)
{
if($appendSlash) $text .= '/';
if($filterStrict) $text = strreplaceu(' ', '-', strtoloweru($text));
return strreplaceu(array('%3A','%2F'), array(':','/'), rawurlencode($text));
}
2014-07-25 10:46:58 +00:00
// Normalise file/directory/other name
function normaliseName($text, $removePrefix = true, $removeExtension = false, $filterStrict = false)
2014-07-25 10:46:58 +00:00
{
if($removeExtension) $text = ($pos = strrposu($text, '.')) ? substru($text, 0, $pos) : $text;
if($removePrefix) if(preg_match("/^[\d\-\_\.]+(.*)$/", $text, $matches)) $text = $matches[1];
2014-07-25 10:46:58 +00:00
if($filterStrict) $text = strreplaceu('.', '-', strtoloweru($text));
return preg_replace("/[^\pL\d\-\_\.]/u", "-", rtrim($text, '/'));
2014-07-25 10:46:58 +00:00
}
2014-02-04 14:06:48 +00:00
// Normalise text into UTF-8 NFC
function normaliseUnicode($text)
{
if(PHP_OS=="Darwin" && !mb_check_encoding($text, "ASCII"))
{
$utf8nfc = preg_match("//u", $text) && !preg_match('/[^\x00-\x{2FF}]/u', $text);
if(!$utf8nfc) $text = iconv("UTF-8-MAC", "UTF-8", $text);
}
return $text;
}
2013-04-14 22:41:04 +00:00
// Return human readable HTTP server status
2013-10-16 21:11:24 +00:00
function getHttpStatusFormatted($statusCode)
2013-04-14 22:41:04 +00:00
{
switch($statusCode)
{
case 0: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode No data"; break;
case 200: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode OK"; break;
case 301: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Moved permanently"; break;
case 302: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Moved temporarily"; break;
case 303: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Reload please"; break;
case 304: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Not modified"; break;
case 400: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Bad request"; break;
case 401: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Unauthorised"; break;
case 404: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Not found"; break;
2014-07-25 10:46:58 +00:00
case 424: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Not existing"; break;
case 500: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Server error"; break;
default: $text = "$_SERVER[SERVER_PROTOCOL] $statusCode Unknown status";
2013-04-14 22:41:04 +00:00
}
return $text;
}
2013-06-27 17:00:03 +00:00
// Return human readable HTTP time
2013-10-16 21:11:24 +00:00
function getHttpTimeFormatted($timestamp)
2013-06-27 17:00:03 +00:00
{
return gmdate("D, d M Y H:i:s", $timestamp)." GMT";
}
2013-04-14 22:41:04 +00:00
2014-05-15 11:53:54 +00:00
// Return URL
function getUrl($serverScheme, $serverName, $base, $location)
{
2014-05-15 11:53:54 +00:00
if(!preg_match("/^\w+:/", $location))
{
2014-05-15 11:53:54 +00:00
$url = "$serverScheme://$serverName$base$location";
} else {
2014-05-15 11:53:54 +00:00
$url = $location;
}
2013-11-29 12:16:14 +00:00
return $url;
}
2013-10-16 21:11:24 +00:00
// Return directory location
function getDirectoryLocation($location)
{
return ($pos = strrposu($location, '/')) ? substru($location, 0, $pos+1) : "/";
2013-10-16 21:11:24 +00:00
}
2013-04-14 22:41:04 +00:00
// Return files and directories
function getDirectoryEntries($path, $regex = "/.*/", $sort = true, $directories = true, $includePath = true)
2013-04-14 22:41:04 +00:00
{
$entries = array();
$dirHandle = @opendir($path);
if($dirHandle)
{
$path = rtrim($path, '/');
2013-04-14 22:41:04 +00:00
while(($entry = readdir($dirHandle)) !== false)
{
2013-05-01 20:16:05 +00:00
if(substru($entry, 0, 1) == ".") continue;
2014-02-04 14:06:48 +00:00
$entry = $this->normaliseUnicode($entry);
2013-04-14 22:41:04 +00:00
if(preg_match($regex, $entry))
{
if($directories)
{
if(is_dir("$path/$entry")) array_push($entries, $includePath ? "$path/$entry" : $entry);
2013-04-14 22:41:04 +00:00
} else {
if(is_file("$path/$entry")) array_push($entries, $includePath ? "$path/$entry" : $entry);
2013-04-14 22:41:04 +00:00
}
}
}
if($sort) natsort($entries);
closedir($dirHandle);
}
return $entries;
}
// Return files and directories recursively
function getDirectoryEntriesRecursive($path, $regex = "/.*/", $sort = true, $directories = true, $levelMax = 0)
{
--$levelMax;
$entries = $this->getDirectoryEntries($path, $regex, $sort, $directories);
if($levelMax != 0)
{
2013-12-21 13:10:15 +00:00
foreach($this->getDirectoryEntries($path, "/.*/", $sort, true) as $entry)
{
$entries = array_merge($entries, $this->getDirectoryEntriesRecursive($entry, $regex, $sort, $directories, $levelMax));
}
}
return $entries;
}
2014-07-25 10:46:58 +00:00
// Delete directory
function deleteDirectory($path, $recursive = false)
{
if($recursive)
{
$iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file)
{
if($file->isDir())
{
@rmdir($file->getRealPath());
} else {
@unlink($file->getRealPath());
}
}
}
return @rmdir($path);
}
// Create file
2013-12-21 13:10:15 +00:00
function createFile($fileName, $fileData, $mkdir = false)
{
$ok = false;
if($mkdir)
{
$path = dirname($fileName);
if(!empty($path) && !is_dir($path)) @mkdir($path, 0777, true);
}
$fileHandle = @fopen($fileName, "w");
if($fileHandle)
{
fwrite($fileHandle, $fileData);
fclose($fileHandle);
$ok = true;
}
return $ok;
}
// Copy file
function copyFile($fileNameSource, $fileNameDest, $mkdir = false)
{
if($mkdir)
{
$path = dirname($fileNameDest);
if(!empty($path) && !is_dir($path)) @mkdir($path, 0777, true);
}
return @copy($fileNameSource, $fileNameDest);
}
// Rename file
function renameFile($fileNameSource, $fileNameDest, $mkdir = false)
{
if($mkdir)
{
$path = dirname($fileNameDest);
if(!empty($path) && !is_dir($path)) @mkdir($path, 0777, true);
}
return @rename($fileNameSource, $fileNameDest);
}
2014-06-27 09:36:30 +00:00
// Set file modification time, Unix time
function modifyFile($fileName, $modified)
{
return @touch($fileName, $modified);
}
2014-07-25 10:46:58 +00:00
// Delete file
function deleteFile($fileName)
{
return @unlink($fileName);
}
2014-06-27 09:36:30 +00:00
// Return arguments from text string
function getTextArgs($text, $optional = "-")
{
$tokens = str_getcsv(trim($text), ' ', '"');
foreach($tokens as $key=>$value) if($value == $optional) $tokens[$key] = "";
return $tokens;
}
2013-05-01 20:16:05 +00:00
// Create description from text string
2013-10-16 21:11:24 +00:00
function createTextDescription($text, $lengthMax, $removeHtml = true, $endMarker = "", $endMarkerText = "")
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
if(preg_match("/<h1>.*<\/h1>(.*)/si", $text, $matches)) $text = $matches[1];
if($removeHtml)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
while(true)
2013-04-14 22:41:04 +00:00
{
2013-09-17 09:18:01 +00:00
$elementFound = preg_match("/<\s*?([\/!]?\w*)(.*?)\s*?\>/s", $text, $matches, PREG_OFFSET_CAPTURE, $offsetBytes);
2013-05-01 20:16:05 +00:00
$element = $matches[0][0];
$elementName = $matches[1][0];
2013-09-17 09:18:01 +00:00
$elementText = $matches[2][0];
2013-05-01 20:16:05 +00:00
$elementOffsetBytes = $elementFound ? $matches[0][1] : strlenb($text);
$string = html_entity_decode(substrb($text, $offsetBytes, $elementOffsetBytes - $offsetBytes), ENT_QUOTES, "UTF-8");
if(preg_match("/^(blockquote|br|div|h\d|hr|li|ol|p|pre|ul)/i", $elementName)) $string .= ' ';
if(preg_match("/^\/(code|pre)/i", $elementName)) $string = preg_replace("/^(\d+\n){2,}$/", "", $string);
2013-05-01 20:16:05 +00:00
$string = preg_replace("/\s+/s", " ", $string);
2013-11-29 12:16:14 +00:00
if(substru($string, 0, 1)==" " && (empty($output) || substru($output, -1)==' ')) $string = substru($string, 1);
2013-05-01 20:16:05 +00:00
$length = strlenu($string);
$output .= substru($string, 0, $length < $lengthMax ? $length : $lengthMax-1);
$lengthMax -= $length;
2013-09-17 09:18:01 +00:00
if(!empty($element) && $element==$endMarker) { $lengthMax = 0; $endMarkerFound = true; }
2013-05-01 20:16:05 +00:00
if($lengthMax<=0 || !$elementFound) break;
$offsetBytes = $elementOffsetBytes + strlenb($element);
2013-04-14 22:41:04 +00:00
}
2013-05-01 20:16:05 +00:00
$output = rtrim($output);
2013-09-17 09:18:01 +00:00
if($lengthMax <= 0) $output .= $endMarkerFound ? $endMarkerText : "";
2013-05-01 20:16:05 +00:00
} else {
$elementsOpen = array();
while(true)
2013-04-14 22:41:04 +00:00
{
2013-09-17 09:18:01 +00:00
$elementFound = preg_match("/&.*?\;|<\s*?([\/!]?\w*)(.*?)\s*?\>/s", $text, $matches, PREG_OFFSET_CAPTURE, $offsetBytes);
2013-05-01 20:16:05 +00:00
$element = $matches[0][0];
$elementName = $matches[1][0];
$elementText = $matches[2][0];
2013-05-01 20:16:05 +00:00
$elementOffsetBytes = $elementFound ? $matches[0][1] : strlenb($text);
$string = substrb($text, $offsetBytes, $elementOffsetBytes - $offsetBytes);
$length = strlenu($string);
$output .= substru($string, 0, $length < $lengthMax ? $length : $lengthMax-1);
$lengthMax -= $length + ($element[0]=='&' ? 1 : 0);
2013-09-17 09:18:01 +00:00
if(!empty($element) && $element==$endMarker) { $lengthMax = 0; $endMarkerFound = true; }
2013-05-01 20:16:05 +00:00
if($lengthMax<=0 || !$elementFound) break;
if(!empty($elementName) && substru($elementText, -1)!='/' &&
2013-09-17 09:18:01 +00:00
!preg_match("/^(area|br|col|hr|img|input|col|param|!)/i", $elementName))
2013-05-01 20:16:05 +00:00
{
if($elementName[0] != '/')
2013-05-01 20:16:05 +00:00
{
array_push($elementsOpen, $elementName);
2013-05-01 20:16:05 +00:00
} else {
array_pop($elementsOpen);
}
}
$output .= $element;
$offsetBytes = $elementOffsetBytes + strlenb($element);
2013-04-14 22:41:04 +00:00
}
2013-05-01 20:16:05 +00:00
$output = rtrim($output);
2013-12-21 13:10:15 +00:00
for($i=count($elementsOpen)-1; $i>=0; --$i)
{
if(!preg_match("/^(dl|ol|ul|table|tbody|thead|tfoot|tr)/i", $elementsOpen[$i])) break;
$output .= "</".$elementsOpen[$i].">";
}
2013-09-17 09:18:01 +00:00
if($lengthMax <= 0) $output .= $endMarkerFound ? $endMarkerText : "";
2013-12-21 13:10:15 +00:00
for(; $i>=0; --$i) $output .= "</".$elementsOpen[$i].">";
2013-04-14 22:41:04 +00:00
}
2013-05-01 20:16:05 +00:00
return $output;
2013-04-14 22:41:04 +00:00
}
2013-12-21 13:10:15 +00:00
2013-04-14 22:41:04 +00:00
// Create keywords from text string
2013-10-16 21:11:24 +00:00
function createTextKeywords($text, $keywordsMax)
2013-04-14 22:41:04 +00:00
{
2013-05-01 20:16:05 +00:00
$tokens = preg_split("/[,\s\(\)]/", strtoloweru($text));
foreach($tokens as $key=>$value) if(strlenu($value) < 3) unset($tokens[$key]);
2013-04-14 22:41:04 +00:00
return implode(", ", array_slice(array_unique($tokens), 0, $keywordsMax));
}
// Create title from text string
2013-10-16 21:11:24 +00:00
function createTextTitle($text)
2013-04-14 22:41:04 +00:00
{
2014-07-25 10:46:58 +00:00
if(preg_match("/^.*\/([\w\-]+)/", $text, $matches)) $text = strreplaceu('-', ' ', ucfirst($matches[1]));
2013-04-14 22:41:04 +00:00
return $text;
}
2013-04-07 18:04:09 +00:00
// Create random text for cryptography
function createSalt($length, $bcryptFormat = false)
{
$dataBuffer = $salt = "";
$dataBufferSize = $bcryptFormat ? intval(ceil($length/4) * 3) : intval(ceil($length/2));
if(empty($dataBuffer) && function_exists("mcrypt_create_iv"))
{
$dataBuffer = @mcrypt_create_iv($dataBufferSize, MCRYPT_DEV_URANDOM);
}
if(empty($dataBuffer) && function_exists("openssl_random_pseudo_bytes"))
{
$dataBuffer = @openssl_random_pseudo_bytes($dataBufferSize);
}
if(strlenb($dataBuffer) == $dataBufferSize)
{
if($bcryptFormat)
{
$salt = substrb(base64_encode($dataBuffer), 0, $length);
$base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
$bcrypt64Chars = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
$salt = strtr($salt, $base64Chars, $bcrypt64Chars);
} else {
$salt = substrb(bin2hex($dataBuffer), 0, $length);
}
}
return $salt;
}
// Create hash with random salt, bcrypt or sha256
function createHash($text, $algorithm, $cost = 0)
{
$hash = "";
switch($algorithm)
{
case "bcrypt": $prefix = sprintf("$2y$%02d$", $cost);
$salt = $this->createSalt(22, true);
$hash = crypt($text, $prefix.$salt);
if(empty($salt) || strlenb($hash)!=60) $hash = "";
break;
case "sha256": $prefix = "$5y$";
$salt = $this->createSalt(32);
$hash = "$prefix$salt".hash("sha256", $salt.$text);
if(empty($salt) || strlenb($hash)!=100) $hash = "";
break;
}
return $hash;
}
// Verify that text matches hash
function verifyHash($text, $algorithm, $hash)
{
$hashCalculated = "";
switch($algorithm)
{
case "bcrypt": if(substrb($hash, 0, 4) == "$2y$") $hashCalculated = crypt($text, $hash); break;
case "sha256": if(substrb($hash, 0, 4) == "$5y$")
{
$prefix = substrb($hash, 0, 4);
$salt = substrb($hash, 4, 32);
$hashCalculated = "$prefix$salt".hash("sha256", $salt.$text);
}
break;
}
$ok = !empty($hashCalculated) && strlenb($hashCalculated)==strlenb($hash);
if($ok) for($i=0; $i<strlenb($hashCalculated); ++$i) $ok &= $hashCalculated[$i] == $hash[$i];
return $ok;
}
2014-06-27 09:36:30 +00:00
// Detect image dimensions and type, png or jpg
function detectImageInfo($fileName)
{
$width = $height = 0;
2014-06-27 09:36:30 +00:00
$type = "";
$fileHandle = @fopen($fileName, "rb");
if($fileHandle)
{
if(substru($fileName, -3) == "png")
{
$dataSignature = fread($fileHandle, 8);
$dataHeader = fread($fileHandle, 16);
if(!feof($fileHandle) && $dataSignature=="\x89PNG\r\n\x1a\n")
{
$width = (ord($dataHeader[10])<<8) + ord($dataHeader[11]);
$height = (ord($dataHeader[14])<<8) + ord($dataHeader[15]);
2014-06-27 09:36:30 +00:00
$type = "png";
}
} else if(substru($fileName, -3) == "jpg") {
$dataBufferSize = min(filesize($fileName), 8192);
$dataBuffer = fread($fileHandle, $dataBufferSize);
$dataSignature = substrb($dataBuffer, 0, 11);
if(!feof($fileHandle) && $dataSignature=="\xff\xd8\xff\xe0\x00\x10JFIF\0")
{
for($pos=20; $pos+8<$dataBufferSize; $pos+=$length)
{
if($dataBuffer[$pos] != "\xff") break;
if($dataBuffer[$pos+1]=="\xc0" || $dataBuffer[$pos+1]=="\xc2")
{
$width = (ord($dataBuffer[$pos+7])<<8) + ord($dataBuffer[$pos+8]);
$height = (ord($dataBuffer[$pos+5])<<8) + ord($dataBuffer[$pos+6]);
2014-06-27 09:36:30 +00:00
$type = "jpg";
break;
}
$length = (ord($dataBuffer[$pos+2])<<8) + ord($dataBuffer[$pos+3]) + 2;
}
}
}
fclose($fileHandle);
}
2014-06-27 09:36:30 +00:00
return array($width, $height, $type);
}
2013-04-14 22:41:04 +00:00
// Start timer
2013-10-16 21:11:24 +00:00
function timerStart(&$time)
2013-04-14 22:41:04 +00:00
{
$time = microtime(true);
}
// Stop timer and calculate elapsed time in milliseconds
2013-10-16 21:11:24 +00:00
function timerStop(&$time)
2013-04-14 22:41:04 +00:00
{
$time = intval((microtime(true)-$time) * 1000);
}
2013-04-07 18:04:09 +00:00
}
// Yellow plugins
2013-12-01 11:59:07 +00:00
class YellowPlugins
2013-04-07 18:04:09 +00:00
{
2013-04-14 22:41:04 +00:00
var $plugins; //registered plugins
2013-04-07 18:04:09 +00:00
2013-04-14 22:41:04 +00:00
function __construct()
{
$this->plugins = array();
}
// Load plugins
function load()
2013-04-07 18:04:09 +00:00
{
global $yellow;
2013-12-21 13:10:15 +00:00
$path = dirname(__FILE__);
2014-06-27 09:36:30 +00:00
foreach($yellow->toolbox->getDirectoryEntries($path, "/^core-.*\.php$/", true, false) as $entry) require_once($entry);
2013-12-21 13:10:15 +00:00
$path = $yellow->config->get("pluginDir");
2014-06-27 09:36:30 +00:00
foreach($yellow->toolbox->getDirectoryEntries($path, "/^.*\.php$/", true, false) as $entry) require_once($entry);
2013-04-14 22:41:04 +00:00
foreach($this->plugins as $key=>$value)
{
$this->plugins[$key]["obj"] = new $value["class"];
if(defined("DEBUG") && DEBUG>=3) echo "YellowPlugins::load class:$value[class] $value[version]<br/>\n";
2013-12-01 11:59:07 +00:00
if(method_exists($this->plugins[$key]["obj"], "onLoad")) $this->plugins[$key]["obj"]->onLoad($yellow);
2013-04-07 18:04:09 +00:00
}
}
2013-04-14 22:41:04 +00:00
// Register plugin
2013-04-07 18:04:09 +00:00
function register($name, $class, $version)
{
2013-04-14 22:41:04 +00:00
if(!$this->isExisting($name))
{
$this->plugins[$name] = array();
$this->plugins[$name]["class"] = $class;
$this->plugins[$name]["version"] = $version;
}
}
// Check if plugin exists
function isExisting($name)
{
return !is_null($this->plugins[$name]);
2013-04-07 18:04:09 +00:00
}
}
2013-05-01 20:16:05 +00:00
// Unicode support for PHP
2013-05-01 20:16:05 +00:00
mb_internal_encoding("UTF-8");
2013-10-16 21:11:24 +00:00
function strempty($string) { return is_null($string) || $string===""; }
2013-05-01 20:16:05 +00:00
function strreplaceu() { return call_user_func_array("str_replace", func_get_args()); }
function strtoloweru() { return call_user_func_array("mb_strtolower", func_get_args()); }
function strtoupperu() { return call_user_func_array("mb_strtoupper", func_get_args()); }
function strlenu() { return call_user_func_array("mb_strlen", func_get_args()); }
2013-05-01 20:16:05 +00:00
function strlenb() { return call_user_func_array("strlen", func_get_args()); }
function strposu() { return call_user_func_array("mb_strpos", func_get_args()); }
2013-05-01 20:16:05 +00:00
function strposb() { return call_user_func_array("strpos", func_get_args()); }
function strrposu() { return call_user_func_array("mb_strrpos", func_get_args()); }
function strrposb() { return call_user_func_array("strrpos", func_get_args()); }
function substru() { return call_user_func_array("mb_substr", func_get_args()); }
2013-05-01 20:16:05 +00:00
function substrb() { return call_user_func_array("substr", func_get_args()); }
2013-06-07 20:01:12 +00:00
// Default timezone for PHP
2014-01-08 16:58:52 +00:00
date_default_timezone_set(@date_default_timezone_get());
// Error reporting for PHP
2013-06-07 20:01:12 +00:00
error_reporting(E_ALL ^ E_NOTICE);
2013-04-07 18:04:09 +00:00
?>