yellow/system/core/core-webinterface.php

699 lines
26 KiB
PHP
Raw Normal View History

2013-04-14 22:41:04 +00:00
<?php
2014-01-27 11:30:39 +00:00
// Copyright (c) 2013-2014 Datenstrom, http://datenstrom.se
2013-04-14 22:41:04 +00:00
// This file may be used and distributed under the terms of the public license.
// Web interface core plugin
2013-12-01 11:59:07 +00:00
class YellowWebinterface
2013-04-14 22:41:04 +00:00
{
const Version = "0.4.2";
2013-07-11 20:33:28 +00:00
var $yellow; //access to API
var $users; //web interface users
2014-05-15 11:53:54 +00:00
var $active; //web interface is active? (boolean)
2014-07-25 10:46:58 +00:00
var $userLoginFailed; //web interface login failed? (boolean)
var $userPermission; //web interface can modify page? (boolean)
var $rawDataSource; //raw data of page for comparison
var $rawDataEdit; //raw data of page for editing
2013-04-14 22:41:04 +00:00
2014-05-29 21:33:01 +00:00
// Handle plugin initialisation
2013-12-01 11:59:07 +00:00
function onLoad($yellow)
2013-04-14 22:41:04 +00:00
{
$this->yellow = $yellow;
2013-06-27 17:00:03 +00:00
$this->yellow->config->setDefault("webinterfaceLocation", "/edit/");
2014-08-19 10:06:09 +00:00
$this->yellow->config->setDefault("webinterfaceServerScheme", "http");
2014-05-15 11:53:54 +00:00
$this->yellow->config->setDefault("webinterfaceServerName", $this->yellow->config->get("serverName"));
$this->yellow->config->setDefault("webinterfaceUserHashAlgorithm", "bcrypt");
$this->yellow->config->setDefault("webinterfaceUserHashCost", "10");
$this->yellow->config->setDefault("webinterfaceUserFile", "user.ini");
$this->yellow->config->setDefault("webinterfaceEmail", "");
$this->yellow->config->setDefault("webinterfacePassword", "");
$this->yellow->config->setDefault("webinterfaceNewPage", "default");
$this->yellow->config->setDefault("webinterfaceFilePrefix", "published");
2013-12-21 13:10:15 +00:00
$this->users = new YellowWebinterfaceUsers($yellow);
2013-04-14 22:41:04 +00:00
$this->users->load($this->yellow->config->get("configDir").$this->yellow->config->get("webinterfaceUserFile"));
}
2014-05-29 21:33:01 +00:00
// Handle request
2014-05-15 11:53:54 +00:00
function onRequest($serverScheme, $serverName, $base, $location, $fileName)
2013-04-14 22:41:04 +00:00
{
$statusCode = 0;
2014-05-15 11:53:54 +00:00
if($this->checkRequest($location))
2013-04-14 22:41:04 +00:00
{
2014-07-25 10:46:58 +00:00
list($serverScheme, $serverName, $base, $location, $fileName) = $this->updateRequestInformation();
$statusCode = $this->processRequest($serverScheme, $serverName, $base, $location, $fileName);
2013-04-14 22:41:04 +00:00
} else {
2014-05-15 11:53:54 +00:00
$activeLocation = $this->yellow->config->get("webinterfaceLocation");
if(rtrim($location, '/') == rtrim($activeLocation, '/'))
2013-04-14 22:41:04 +00:00
{
$statusCode = 301;
2014-05-15 11:53:54 +00:00
$locationHeader = $this->yellow->toolbox->getLocationHeader(
$this->yellow->config->get("webinterfaceServerScheme"),
2014-07-25 10:46:58 +00:00
$this->yellow->config->get("webinterfaceServerName"), $base, $activeLocation);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2013-04-14 22:41:04 +00:00
}
}
return $statusCode;
}
2014-01-27 11:30:39 +00:00
// Handle page meta data parsing
function onParseMeta($page, $text)
{
if($this->isActive() && $this->isUser())
2014-01-27 11:30:39 +00:00
{
if($page == $this->yellow->page)
{
2014-07-25 10:46:58 +00:00
if(empty($this->rawDataSource)) $this->rawDataSource = $page->rawData;
if(empty($this->rawDataEdit)) $this->rawDataEdit = $page->rawData;
if($page->statusCode == 424)
{
$title = $this->yellow->toolbox->createTextTitle($page->location);
$this->rawDataEdit = $this->getDataNew($title);
}
2014-01-27 11:30:39 +00:00
}
}
}
2013-09-17 09:18:01 +00:00
// Handle page content parsing
function onParseContent($page, $text)
2013-07-11 20:33:28 +00:00
{
2013-08-28 10:01:46 +00:00
$output = NULL;
if($this->isActive() && $this->isUser())
2013-07-11 20:33:28 +00:00
{
2014-01-27 11:30:39 +00:00
$serverBase = $this->yellow->config->get("serverBase");
2014-05-15 11:53:54 +00:00
$activePath = trim($this->yellow->config->get("webinterfaceLocation"), '/');
$callback = function($matches) use ($serverBase, $activePath)
2014-01-27 11:30:39 +00:00
{
2014-05-15 11:53:54 +00:00
$matches[2] = preg_replace("#^$serverBase/(?!$activePath)(.*)$#", "$serverBase/$activePath/$1", $matches[2]);
2014-01-27 11:30:39 +00:00
return "<a$matches[1]href=\"$matches[2]\"$matches[3]>";
};
$output = preg_replace_callback("/<a(.*?)href=\"([^\"]+)\"(.*?)>/i", $callback, $text);
2013-07-11 20:33:28 +00:00
}
2013-08-28 10:01:46 +00:00
return $output;
2013-07-11 20:33:28 +00:00
}
2014-05-29 21:33:01 +00:00
// Handle page extra header
function onHeaderExtra($page)
2013-04-14 22:41:04 +00:00
{
$header = "";
if($this->isActive())
2013-04-14 22:41:04 +00:00
{
$location = $this->yellow->config->getHtml("serverBase").$this->yellow->config->getHtml("pluginLocation");
2014-10-14 14:42:36 +00:00
$header .= "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{$location}core-webinterface.css\" />\n";
2013-12-01 11:59:07 +00:00
$header .= "<script type=\"text/javascript\" src=\"{$location}core-webinterface.js\"></script>\n";
2013-04-14 22:41:04 +00:00
$header .= "<script type=\"text/javascript\">\n";
$header .= "// <![CDATA[\n";
if($this->isUser())
{
2014-08-29 07:15:49 +00:00
$header .= "yellow.page.userPermission = ".json_encode($this->userPermission).";\n";
2014-07-25 10:46:58 +00:00
$header .= "yellow.page.rawDataSource = ".json_encode($this->rawDataSource).";\n";
$header .= "yellow.page.rawDataEdit = ".json_encode($this->rawDataEdit).";\n";
$header .= "yellow.page.rawDataNew = ".json_encode($this->getDataNew()).";\n";
2014-08-29 07:15:49 +00:00
$header .= "yellow.page.parserSafeMode = ".json_encode($page->parserSafeMode).";\n";
2014-07-25 10:46:58 +00:00
$header .= "yellow.page.statusCode = ".json_encode($page->statusCode).";\n";
2013-04-14 22:41:04 +00:00
}
2014-08-19 21:44:22 +00:00
$header .= "yellow.config = ".json_encode($this->getDataConfig()).";\n";
2014-05-29 21:33:01 +00:00
$language = $this->isUser() ? $this->users->getLanguage() : $page->get("language");
2013-12-11 14:13:38 +00:00
$header .= "yellow.text = ".json_encode($this->yellow->text->getData("webinterface", $language)).";\n";
2013-04-14 22:41:04 +00:00
if(defined("DEBUG")) $header .= "yellow.debug = ".json_encode(DEBUG).";\n";
$header .= "// ]]>\n";
$header .= "</script>\n";
}
return $header;
}
2013-12-21 13:10:15 +00:00
// Handle command help
function onCommandHelp()
{
return "user EMAIL PASSWORD [NAME LANGUAGE HOME]\n";
2013-12-21 13:10:15 +00:00
}
// Handle command
function onCommand($args)
{
list($name, $command) = $args;
switch($command)
{
case "user": $statusCode = $this->userCommand($args); break;
default: $statusCode = 0;
}
return $statusCode;
}
// Create or update user account
2013-12-21 13:10:15 +00:00
function userCommand($args)
{
$statusCode = 0;
list($dummy, $command, $email, $password, $name, $language, $home) = $args;
if(!empty($email) && !empty($password) && (empty($home) || $home[0]=='/'))
2013-12-21 13:10:15 +00:00
{
$fileName = $this->yellow->config->get("configDir").$this->yellow->config->get("webinterfaceUserFile");
$algorithm = $this->yellow->config->get("webinterfaceUserHashAlgorithm");
$cost = $this->yellow->config->get("webinterfaceUserHashCost");
$hash = $this->yellow->toolbox->createHash($password, $algorithm, $cost);
if(empty($hash))
{
$statusCode = 500;
echo "ERROR creating hash: Algorithm '$algorithm' not supported!\n";
} else {
2014-07-25 10:46:58 +00:00
$statusCode = $this->users->createUser($fileName, $email, $hash, $name, $language, $home) ? 200 : 500;
if($statusCode != 200) echo "ERROR updating configuration: Can't write file '$fileName'!\n";
}
2013-12-21 13:10:15 +00:00
echo "Yellow $command: User account ".($statusCode!=200 ? "not " : "");
echo ($this->users->isExisting($email) ? "updated" : "created")."\n";
} else {
echo "Yellow $command: Invalid arguments\n";
$statusCode = 400;
}
return $statusCode;
}
2014-07-25 10:46:58 +00:00
// Process request
function processRequest($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
if($this->checkUser($location, $fileName))
{
switch($_POST["action"])
{
case "": $statusCode = $this->processRequestShow($serverScheme, $serverName, $base, $location, $fileName); break;
case "create": $statusCode = $this->processRequestCreate($serverScheme, $serverName, $base, $location, $fileName); break;
case "edit": $statusCode = $this->processRequestEdit($serverScheme, $serverName, $base, $location, $fileName); break;
case "delete": $statusCode = $this->processRequestDelete($serverScheme, $serverName, $base, $location, $fileName); break;
case "login": $statusCode = $this->processRequestLogin($serverScheme, $serverName, $base, $location, $fileName); break;
case "logout": $statusCode = $this->processRequestLogout($serverScheme, $serverName, $base, $location, $fileName); break;
}
}
if($statusCode == 0)
{
$statusCode = $this->userLoginFailed ? 401 : 0;
$statusCode = $this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
2014-07-25 10:46:58 +00:00
}
return $statusCode;
}
// Process request to show page
function processRequestShow($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
if(is_readable($fileName))
{
$statusCode = $this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, 0, false);
2014-07-25 10:46:58 +00:00
} else {
if($this->yellow->toolbox->isFileLocation($location) && $this->yellow->isContentDirectory("$location/"))
{
$statusCode = 301;
$locationHeader = $this->yellow->toolbox->getLocationHeader($serverScheme, $serverName, $base, "$location/");
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2014-07-25 10:46:58 +00:00
} else {
$statusCode = $this->userPermission ? 424 : 404;
$this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
2014-07-25 10:46:58 +00:00
}
}
return $statusCode;
}
// Process request to create page
function processRequestCreate($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
if($this->userPermission && !empty($_POST["rawdataedit"]))
2014-07-25 10:46:58 +00:00
{
$this->rawDataSource = $this->rawDataEdit = stripcslashes($_POST["rawdatasource"]);
$page = $this->getPageNew($serverScheme, $serverName, $base, $location, $fileName, stripcslashes($_POST["rawdataedit"]));
if(!$page->isError())
2014-07-25 10:46:58 +00:00
{
if($this->yellow->toolbox->createFile($page->fileName, $page->rawData))
{
$statusCode = 303;
$locationHeader = $this->yellow->toolbox->getLocationHeader($serverScheme, $serverName, $base, $page->location);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
} else {
$statusCode = 500;
$this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
$this->yellow->page->error($statusCode, "Can't write file '$page->fileName'!");
}
2014-07-25 10:46:58 +00:00
} else {
$statusCode = 500;
$this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
$this->yellow->page->error($statusCode, $page->get("pageError"));
2014-07-25 10:46:58 +00:00
}
}
return $statusCode;
}
// Process request to edit page
function processRequestEdit($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
if($this->userPermission && !empty($_POST["rawdataedit"]))
{
$this->rawDataSource = stripcslashes($_POST["rawdatasource"]);
$this->rawDataEdit = stripcslashes($_POST["rawdataedit"]);
$page = $this->getPageUpdate($serverScheme, $serverName, $base, $location, $fileName, $this->rawDataSource, $this->rawDataEdit);
if(!$page->isError())
2014-07-25 10:46:58 +00:00
{
if($this->yellow->toolbox->renameFile($fileName, $page->fileName) &&
$this->yellow->toolbox->createFile($page->fileName, $page->rawData))
2014-07-25 10:46:58 +00:00
{
$statusCode = 303;
$locationHeader = $this->yellow->toolbox->getLocationHeader($serverScheme, $serverName, $base, $page->location);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2014-07-25 10:46:58 +00:00
} else {
$statusCode = 500;
$this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
$this->yellow->page->error($statusCode, "Can't write file '$page->fileName'!");
2014-07-25 10:46:58 +00:00
}
} else {
$statusCode = 500;
$this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
$this->yellow->page->error($statusCode, $page->get("pageError"));
2014-07-25 10:46:58 +00:00
}
}
return $statusCode;
}
// Process request to delete page
function processRequestDelete($serverScheme, $serverName, $base, $location, $fileName)
2013-04-14 22:41:04 +00:00
{
$statusCode = 0;
2014-07-25 10:46:58 +00:00
if($this->userPermission)
2013-04-14 22:41:04 +00:00
{
2014-07-25 10:46:58 +00:00
$this->rawDataSource = $this->rawDataEdit = stripcslashes($_POST["rawdatasource"]);
if(!is_file($fileName) || $this->yellow->toolbox->deleteFile($fileName))
{
$statusCode = 303;
$locationHeader = $this->yellow->toolbox->getLocationHeader($serverScheme, $serverName, $base, $location);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2014-07-25 10:46:58 +00:00
} else {
$statusCode = 500;
$this->yellow->processRequest($serverScheme, $serverName, $base, $location, $fileName, $statusCode, false);
2014-07-25 10:46:58 +00:00
$this->yellow->page->error($statusCode, "Can't delete file '$fileName'!");
}
2013-04-14 22:41:04 +00:00
}
return $statusCode;
}
2014-07-25 10:46:58 +00:00
// Process request for user login
function processRequestLogin($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
$home = $this->users->getHome();
if(substru($location, 0, strlenu($home)) == $home)
{
$statusCode = 303;
$locationHeader = $this->yellow->toolbox->getLocationHeader($serverScheme, $serverName, $base, $location);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2014-07-25 10:46:58 +00:00
} else {
$statusCode = 302;
$locationHeader = $this->yellow->toolbox->getLocationHeader($serverScheme, $serverName, $base, $home);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2014-07-25 10:46:58 +00:00
}
return $statusCode;
}
// Process request for user logout
function processRequestLogout($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 302;
$this->users->destroyCookie("login");
$this->users->email = "";
$locationHeader = $this->yellow->toolbox->getLocationHeader(
$this->yellow->config->get("serverScheme"),
$this->yellow->config->get("serverName"),
$this->yellow->config->get("serverBase"), $location);
$this->yellow->sendStatus($statusCode, false, $locationHeader);
2014-07-25 10:46:58 +00:00
return $statusCode;
}
2013-04-14 22:41:04 +00:00
2014-07-25 10:46:58 +00:00
// Merge text
function mergeText($location, $textSource, $textLocal, $fileName)
{
$fileHandle = @fopen($fileName, "r");
if($fileHandle)
{
$fileData = fread($fileHandle, filesize($fileName));
fclose($fileHandle);
}
if(!empty($fileData) && $fileData!=$textSource && $fileData!=$textLocal)
{
$output = NULL;
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onMergeText"))
{
$output = $value["obj"]->onMergeText($location, $textSource, $textLocal, $fileData);
if(!is_null($output)) break;
}
}
} else {
$output = $textLocal;
}
return $output;
}
2014-05-15 11:53:54 +00:00
// Check web interface request
function checkRequest($location)
2013-04-14 22:41:04 +00:00
{
2014-05-15 11:53:54 +00:00
if($this->yellow->toolbox->getServerScheme()==$this->yellow->config->get("webinterfaceServerScheme") &&
$this->yellow->toolbox->getServerName()==$this->yellow->config->get("webinterfaceServerName"))
{
$locationLength = strlenu($this->yellow->config->get("webinterfaceLocation"));
$this->active = substru($location, 0, $locationLength) == $this->yellow->config->get("webinterfaceLocation");
}
return $this->isActive();
2013-04-14 22:41:04 +00:00
}
// Check web interface user
2014-07-25 10:46:58 +00:00
function checkUser($location, $fileName)
2013-04-14 22:41:04 +00:00
{
if($_POST["action"] == "login")
{
2013-12-05 22:30:45 +00:00
$email = $_POST["email"];
2013-04-14 22:41:04 +00:00
$password = $_POST["password"];
if($this->users->checkUser($email, $password))
{
$this->users->createCookie("login", $email);
$this->users->email = $email;
2014-07-25 10:46:58 +00:00
$this->userPermission = $this->getUserPermission($location, $fileName);
2013-04-14 22:41:04 +00:00
} else {
2014-07-25 10:46:58 +00:00
$this->userLoginFailed = true;
2013-04-14 22:41:04 +00:00
}
} else if(isset($_COOKIE["login"])) {
list($email, $session) = $this->users->getCookieInformation($_COOKIE["login"]);
if($this->users->checkCookie($email, $session))
2013-04-14 22:41:04 +00:00
{
$this->users->email = $email;
2014-07-25 10:46:58 +00:00
$this->userPermission = $this->getUserPermission($location, $fileName);
2013-04-14 22:41:04 +00:00
} else {
2014-07-25 10:46:58 +00:00
$this->userLoginFailed = true;
2013-04-14 22:41:04 +00:00
}
}
return $this->isUser();
}
2014-07-25 10:46:58 +00:00
// Return permission to modify page
function getUserPermission($location, $fileName)
2013-04-14 22:41:04 +00:00
{
2014-07-25 10:46:58 +00:00
$userPermission = true;
foreach($this->yellow->plugins->plugins as $key=>$value)
{
2014-07-25 10:46:58 +00:00
if(method_exists($value["obj"], "onUserPermission"))
{
2014-07-25 10:46:58 +00:00
$userPermission = $value["obj"]->onUserPermission($location, $fileName, $this->users);
if(!$userPermission) break;
}
}
2014-07-25 10:46:58 +00:00
$userPermission &= is_dir(dirname($fileName)) && strlenu(basename($fileName))<128;
return $userPermission;
2013-04-14 22:41:04 +00:00
}
2014-07-25 10:46:58 +00:00
// Update request information
function updateRequestInformation()
2013-04-14 22:41:04 +00:00
{
2014-05-15 11:53:54 +00:00
$serverScheme = $this->yellow->config->get("webinterfaceServerScheme");
$serverName = $this->yellow->config->get("webinterfaceServerName");
$base = rtrim($this->yellow->config->get("serverBase").$this->yellow->config->get("webinterfaceLocation"), '/');
2014-07-25 10:46:58 +00:00
$this->yellow->page->base = $base;
2014-05-15 11:53:54 +00:00
return $this->yellow->getRequestInformation($serverScheme, $serverName, $base);
2013-04-14 22:41:04 +00:00
}
2014-07-25 10:46:58 +00:00
// Update page data with title
function updateDataTitle($rawData, $title)
{
foreach(preg_split("/([\r\n]+)/", $rawData, -1, PREG_SPLIT_DELIM_CAPTURE) as $line)
{
if(preg_match("/^(\s*Title\s*:\s*)(.*?)(\s*)$/i", $line, $matches)) $line = $matches[1].$title.$matches[3];
$rawDataNew .= $line;
}
return $rawDataNew;
}
// Return new page
function getPageNew($serverScheme, $serverName, $base, $location, $fileName, $rawData)
{
$page = new YellowPage($this->yellow, $serverScheme, $serverName, $base, $location, $fileName);
$page->parseData($rawData, 0, false);
$page->fileName = $this->yellow->toolbox->findFileFromTitle(
$page->get($this->yellow->config->get("webinterfaceFilePrefix")), $page->get("title"), $fileName,
2014-07-25 10:46:58 +00:00
$this->yellow->config->get("contentDefaultFile"), $this->yellow->config->get("contentExtension"));
$page->location = $this->yellow->toolbox->findLocationFromFile(
$page->fileName, $this->yellow->config->get("contentDir"),
$this->yellow->config->get("contentRootDir"), $this->yellow->config->get("contentHomeDir"),
2014-07-25 10:46:58 +00:00
$this->yellow->config->get("contentDefaultFile"), $this->yellow->config->get("contentExtension"));
if($this->yellow->pages->find($page->location))
{
preg_match("/^(.*?)(\d*)$/", $page->get("title"), $matches);
$titleText = $matches[1];
$titleNumber = $matches[2];
if(strempty($titleNumber)) { $titleNumber = 2; $titleText = $titleText.' '; }
for(; $titleNumber<=999; ++$titleNumber)
2014-07-25 10:46:58 +00:00
{
$page->rawData = $this->updateDataTitle($rawData, $titleText.$titleNumber);
$page->fileName = $this->yellow->toolbox->findFileFromTitle(
$page->get($this->yellow->config->get("webinterfaceFilePrefix")), $titleText.$titleNumber, $fileName,
$this->yellow->config->get("contentDefaultFile"), $this->yellow->config->get("contentExtension"));
$page->location = $this->yellow->toolbox->findLocationFromFile(
$page->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"));
if(!$this->yellow->pages->find($page->location)) { $ok = true; break; }
2014-07-25 10:46:58 +00:00
}
if(!$ok) $page->error(500, "Page '".$page->get("title")."' can not be created!");
}
if(!$this->getUserPermission($page->location, $page->fileName)) $page->error(500, "Page '".$page->get("title")."' is not allowed!");
return $page;
}
// Return modified page
function getPageUpdate($serverScheme, $serverName, $base, $location, $fileName, $rawDataSource, $rawDataEdit)
{
$page = new YellowPage($this->yellow, $serverScheme, $serverName, $base, $location, $fileName);
$page->parseData($this->mergeText($location, $rawDataSource, $rawDataEdit, $fileName), 0, false);
if(empty($page->rawData)) $page->error(500, "Page has been modified by someone else!");
if($this->yellow->toolbox->isFileLocation($location) && !$page->isError())
{
$pageSource = new YellowPage($this->yellow, $serverScheme, $serverName, $base, $location, $fileName);
$pageSource->parseData($rawDataSource, 0, false);
$prefix = $this->yellow->config->get("webinterfaceFilePrefix");
if($pageSource->get($prefix)!=$page->get($prefix) || $pageSource->get("title")!=$page->get("title"))
2014-07-25 10:46:58 +00:00
{
$page->fileName = $this->yellow->toolbox->findFileFromTitle(
$page->get($prefix), $page->get("title"), $fileName,
2014-07-25 10:46:58 +00:00
$this->yellow->config->get("contentDefaultFile"), $this->yellow->config->get("contentExtension"));
$page->location = $this->yellow->toolbox->findLocationFromFile(
$page->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"));
if($pageSource->location!=$page->location && $this->yellow->pages->find($page->location))
{
$page->error(500, "Page '".$page->get("title")."' already exists!");
}
2014-07-25 10:46:58 +00:00
}
}
if(!$this->getUserPermission($page->location, $page->fileName)) $page->error(500, "Page '".$page->get("title")."' is not allowed!");
2014-07-25 10:46:58 +00:00
return $page;
}
// Return content data for new page
2014-07-25 10:46:58 +00:00
function getDataNew($title = "")
{
$fileName = $this->yellow->toolbox->findFileFromLocation(
$this->yellow->page->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"));
$fileName = $this->yellow->toolbox->findNameFromFile($fileName,
$this->yellow->config->get("configDir"), $this->yellow->config->get("webinterfaceNewPage"),
$this->yellow->config->get("contentExtension"), true);
$fileHandle = @fopen($fileName, "r");
if($fileHandle)
{
$fileData = fread($fileHandle, filesize($fileName));
2014-07-25 10:46:58 +00:00
if(!empty($title)) $fileData = $this->updateDataTitle($fileData, $title);
fclose($fileHandle);
}
return $fileData;
}
// Return configuration data including information of current user
2014-07-25 10:46:58 +00:00
function getDataConfig()
2013-04-14 22:41:04 +00:00
{
2014-08-19 21:44:22 +00:00
$data = $this->yellow->config->getData("", "Location");
if($this->isUser())
{
$data["userEmail"] = $this->users->email;
$data["userName"] = $this->users->getName();
$data["userLanguage"] = $this->users->getLanguage();
$data["userHome"] = $this->users->getHome();
$data["serverScheme"] = $this->yellow->config->get("serverScheme");
$data["serverName"] = $this->yellow->config->get("serverName");
$data["serverBase"] = $this->yellow->config->get("serverBase");
} else {
$data["webinterfaceEmail"] = $this->yellow->config->get("webinterfaceEmail");
$data["webinterfacePassword"] = $this->yellow->config->get("webinterfacePassword");
2014-08-19 21:44:22 +00:00
}
return $data;
2013-04-14 22:41:04 +00:00
}
2014-05-15 11:53:54 +00:00
// Check if web interface request
function isActive()
{
return $this->active;
}
// Check if user is logged in
function isUser()
{
return !empty($this->users->email);
}
2013-04-14 22:41:04 +00:00
}
// Yellow web interface users
2013-12-01 11:59:07 +00:00
class YellowWebinterfaceUsers
2013-04-14 22:41:04 +00:00
{
2013-12-21 13:10:15 +00:00
var $yellow; //access to API
2013-04-14 22:41:04 +00:00
var $users; //registered users
var $email; //current user
2013-04-14 22:41:04 +00:00
2013-12-21 13:10:15 +00:00
function __construct($yellow)
2013-04-14 22:41:04 +00:00
{
2013-12-21 13:10:15 +00:00
$this->yellow = $yellow;
2013-04-14 22:41:04 +00:00
$this->users = array();
}
// Load users from file
function load($fileName)
{
$fileData = @file($fileName);
if($fileData)
{
foreach($fileData as $line)
{
if(preg_match("/^\//", $line)) continue;
preg_match("/^(.*?),\s*(.*?),\s*(.*?),\s*(.*?),\s*(.*?)\s*$/", $line, $matches);
2013-05-01 20:16:05 +00:00
if(!empty($matches[1]) && !empty($matches[2]) && !empty($matches[3]) && !empty($matches[4]))
2013-04-14 22:41:04 +00:00
{
$this->set($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
2013-12-01 11:59:07 +00:00
if(defined("DEBUG") && DEBUG>=3) echo "YellowWebinterfaceUsers::load email:$matches[1] $matches[3]<br/>\n";
2013-04-14 22:41:04 +00:00
}
}
}
}
2013-12-21 13:10:15 +00:00
// Set user data
function set($email, $hash, $name, $language, $home)
{
$this->users[$email] = array();
$this->users[$email]["email"] = $email;
$this->users[$email]["hash"] = $hash;
$this->users[$email]["name"] = $name;
$this->users[$email]["language"] = $language;
$this->users[$email]["home"] = $home;
}
2013-12-21 13:10:15 +00:00
// Create or update user in file
function createUser($fileName, $email, $hash, $name, $language, $home)
2013-12-21 13:10:15 +00:00
{
$email = strreplaceu(',', '-', $email);
$hash = strreplaceu(',', '-', $hash);
2013-12-21 13:10:15 +00:00
$fileData = @file($fileName);
if($fileData)
{
foreach($fileData as $line)
{
preg_match("/^(.*?),\s*(.*?),\s*(.*?),\s*(.*?),\s*(.*?)\s*$/", $line, $matches);
2013-12-21 13:10:15 +00:00
if(!empty($matches[1]) && !empty($matches[2]) && !empty($matches[3]) && !empty($matches[4]))
{
if($matches[1] == $email)
{
$name = strreplaceu(',', '-', empty($name) ? $matches[3] : $name);
$language = strreplaceu(',', '-', empty($language) ? $matches[4] : $language);
$home = strreplaceu(',', '-', empty($home) ? $matches[5] : $home);
$fileDataNew .= "$email,$hash,$name,$language,$home\n";
2014-07-25 10:46:58 +00:00
$found = true;
2013-12-21 13:10:15 +00:00
continue;
}
}
$fileDataNew .= $line;
}
}
2014-07-25 10:46:58 +00:00
if(!$found)
2013-12-21 13:10:15 +00:00
{
$name = strreplaceu(',', '-', empty($name) ? $this->yellow->config->get("sitename") : $name);
$language = strreplaceu(',', '-', empty($language) ? $this->yellow->config->get("language") : $language);
$home = strreplaceu(',', '-', empty($home) ? "/" : $home);
$fileDataNew .= "$email,$hash,$name,$language,$home\n";
2013-12-21 13:10:15 +00:00
}
return $this->yellow->toolbox->createFile($fileName, $fileDataNew);
}
2013-04-14 22:41:04 +00:00
// Check user login
function checkUser($email, $password)
{
$algorithm = $this->yellow->config->get("webinterfaceUserHashAlgorithm");
return $this->isExisting($email) && $this->yellow->toolbox->verifyHash($password, $algorithm, $this->users[$email]["hash"]);
2013-04-14 22:41:04 +00:00
}
// Create browser cookie
function createCookie($cookieName, $email)
{
if($this->isExisting($email))
{
$location = $this->yellow->config->get("serverBase").$this->yellow->config->get("webinterfaceLocation");
$session = $this->yellow->toolbox->createHash($this->users[$email]["hash"], "sha256");
if(empty($session)) $session = "error-hash-algorithm-sha256";
2014-05-15 11:53:54 +00:00
setcookie($cookieName, "$email,$session", time()+60*60*24*30*365, $location,
$this->yellow->config->get("webinterfaceServerName"),
$this->yellow->config->get("webinterfaceServerScheme")=="https");
2013-04-14 22:41:04 +00:00
}
}
// Destroy browser cookie
function destroyCookie($cookieName)
{
$location = $this->yellow->config->get("serverBase").$this->yellow->config->get("webinterfaceLocation");
2014-05-15 11:53:54 +00:00
setcookie($cookieName, "", time()-3600,
$location, $this->yellow->config->get("webinterfaceServerName"),
$this->yellow->config->get("webinterfaceServerScheme")=="https");
2013-04-14 22:41:04 +00:00
}
// Return information from browser cookie
function getCookieInformation($cookie)
2013-04-14 22:41:04 +00:00
{
return explode(',', $cookie, 2);
2013-04-14 22:41:04 +00:00
}
// Check user login from browser cookie
function checkCookie($email, $session)
2013-04-14 22:41:04 +00:00
{
return $this->isExisting($email) && $this->yellow->toolbox->verifyHash($this->users[$email]["hash"], "sha256", $session);
2013-04-14 22:41:04 +00:00
}
// Return user name
function getName($email = "")
2013-04-14 22:41:04 +00:00
{
if(empty($email)) $email = $this->email;
2013-04-14 22:41:04 +00:00
return $this->isExisting($email) ? $this->users[$email]["name"] : "";
}
// Return user language
function getLanguage($email = "")
2013-04-14 22:41:04 +00:00
{
if(empty($email)) $email = $this->email;
2013-04-14 22:41:04 +00:00
return $this->isExisting($email) ? $this->users[$email]["language"] : "";
}
// Return user home
function getHome($email = "")
{
if(empty($email)) $email = $this->email;
return $this->isExisting($email) ? $this->users[$email]["home"] : "";
}
2013-04-14 22:41:04 +00:00
// Check if user exists
function isExisting($email)
{
return !is_null($this->users[$email]);
}
}
2014-07-10 08:42:32 +00:00
$yellow->plugins->register("webinterface", "YellowWebinterface", YellowWebinterface::Version);
2013-04-14 22:41:04 +00:00
?>