yellow/system/plugins/command.php

660 lines
22 KiB
PHP
Raw Normal View History

2013-06-07 20:01:12 +00:00
<?php
// Command plugin, https://github.com/datenstrom/yellow-plugins/tree/master/command
// Copyright (c) 2013-2018 Datenstrom, https://datenstrom.se
2013-06-07 20:01:12 +00:00
// This file may be used and distributed under the terms of the public license.
class YellowCommand
2013-06-07 20:01:12 +00:00
{
2018-08-08 19:11:40 +00:00
const VERSION = "0.7.8";
2015-04-29 07:26:48 +00:00
var $yellow; //access to API
2016-01-22 15:06:12 +00:00
var $files; //number of files
var $links; //number of links
2016-01-22 15:06:12 +00:00
var $errors; //number of errors
2015-04-29 07:26:48 +00:00
var $locationsArgs; //locations with location arguments detected
var $locationsArgsPagination; //locations with pagination arguments detected
2014-02-04 14:06:48 +00:00
2015-04-29 07:26:48 +00:00
// Handle initialisation
2013-12-01 11:59:07 +00:00
function onLoad($yellow)
2013-06-07 20:01:12 +00:00
{
$this->yellow = $yellow;
2013-06-07 20:01:12 +00:00
}
// Handle command
function onCommand($args)
{
2016-06-27 13:28:10 +00:00
list($command) = $args;
switch($command)
{
2013-12-21 13:10:15 +00:00
case "": $statusCode = $this->helpCommand(); break;
case "build": $statusCode = $this->buildCommand($args); break;
case "check": $statusCode = $this->checkCommand($args); break;
2014-07-25 10:46:58 +00:00
case "clean": $statusCode = $this->cleanCommand($args); break;
2016-06-27 13:28:10 +00:00
case "version": $statusCode = $this->versionCommand($args); break;
default: $statusCode = 0;
}
2013-06-07 20:01:12 +00:00
return $statusCode;
}
2015-04-29 07:26:48 +00:00
// Handle command help
function onCommandHelp()
{
2018-08-08 19:11:40 +00:00
$help .= "build [directory location]\n";
$help .= "check [directory location]\n";
$help .= "clean [directory location]\n";
2016-06-27 13:28:10 +00:00
$help .= "version\n";
2015-04-29 07:26:48 +00:00
return $help;
}
2013-06-07 20:01:12 +00:00
// Show available commands
2013-12-21 13:10:15 +00:00
function helpCommand()
2013-06-07 20:01:12 +00:00
{
echo "Datenstrom Yellow ".YellowCore::VERSION."\n";
2016-07-12 10:30:18 +00:00
$lineCounter = 0;
2013-12-21 13:10:15 +00:00
foreach($this->getCommandHelp() as $line) echo (++$lineCounter>1 ? " " : "Syntax: ")."yellow.php $line\n";
return 200;
2013-06-07 20:01:12 +00:00
}
// Build static website
2013-12-21 13:10:15 +00:00
function buildCommand($args)
{
$statusCode = 0;
2016-06-27 13:28:10 +00:00
list($command, $path, $location) = $args;
2015-01-19 19:05:51 +00:00
if(empty($location) || $location[0]=='/')
{
2016-02-20 17:34:06 +00:00
if($this->checkStaticConfig())
{
$statusCode = $this->buildStaticFiles($path, $location);
} else {
$statusCode = 500;
2016-01-22 15:06:12 +00:00
$this->files = 0; $this->errors = 1;
2016-06-27 13:28:10 +00:00
$fileName = $this->yellow->config->get("configDir").$this->yellow->config->get("configFile");
2017-02-17 13:52:55 +00:00
echo "ERROR building files: Please configure StaticUrl in file '$fileName'!\n";
}
2016-01-22 15:06:12 +00:00
echo "Yellow $command: $this->files file".($this->files!=1 ? 's' : '');
echo ", $this->errors error".($this->errors!=1 ? 's' : '')."\n";
} else {
2013-12-21 13:10:15 +00:00
$statusCode = 400;
echo "Yellow $command: Invalid arguments\n";
}
return $statusCode;
}
// Build static files
function buildStaticFiles($path, $locationFilter)
{
2015-01-19 19:05:51 +00:00
$path = rtrim(empty($path) ? $this->yellow->config->get("staticDir") : $path, '/');
$this->files = $this->errors = 0;
2015-04-29 07:26:48 +00:00
$this->locationsArgs = $this->locationsArgsPagination = array();
$statusCode = empty($locationFilter) ? $this->cleanStaticFiles($path, $locationFilter) : 200;
$staticUrl = $this->yellow->config->get("staticUrl");
list($scheme, $address, $base) = $this->yellow->lookup->getUrlInformation($staticUrl);
foreach($this->getContentLocations() as $location)
{
if(!preg_match("#^$base$locationFilter#", "$base$location")) continue;
$statusCode = max($statusCode, $this->buildStaticFile($path, $location, true));
}
foreach($this->locationsArgs as $location)
{
if(!preg_match("#^$base$locationFilter#", "$base$location")) continue;
$statusCode = max($statusCode, $this->buildStaticFile($path, $location, true));
}
foreach($this->locationsArgsPagination as $location)
{
if(!preg_match("#^$base$locationFilter#", "$base$location")) continue;
if(substru($location, -1)!=$this->yellow->toolbox->getLocationArgsSeparator())
{
$statusCode = max($statusCode, $this->buildStaticFile($path, $location, false, true));
}
for($pageNumber=2; $pageNumber<=999; ++$pageNumber)
{
$statusCodeLocation = $this->buildStaticFile($path, $location.$pageNumber, false, true);
$statusCode = max($statusCode, $statusCodeLocation);
if($statusCodeLocation==100) break;
}
}
if(empty($locationFilter))
{
2016-01-21 17:45:45 +00:00
foreach($this->getMediaLocations() as $location)
2014-02-04 14:06:48 +00:00
{
2016-01-22 15:06:12 +00:00
$statusCode = max($statusCode, $this->buildStaticFile($path, $location));
2014-02-04 14:06:48 +00:00
}
2016-01-21 17:45:45 +00:00
foreach($this->getSystemLocations() as $location)
{
2016-01-22 15:06:12 +00:00
$statusCode = max($statusCode, $this->buildStaticFile($path, $location));
}
2016-01-26 17:11:07 +00:00
$statusCode = max($statusCode, $this->buildStaticFile($path, "/error/", false, false, true));
2013-07-30 13:32:08 +00:00
}
2014-07-25 10:46:58 +00:00
return $statusCode;
}
2016-01-22 15:06:12 +00:00
// Build static file
function buildStaticFile($path, $location, $analyse = false, $probe = false, $error = false)
2016-01-21 17:45:45 +00:00
{
2016-01-26 17:11:07 +00:00
$this->yellow->pages = new YellowPages($this->yellow);
2016-01-21 17:45:45 +00:00
$this->yellow->page = new YellowPage($this->yellow);
2016-01-22 15:06:12 +00:00
$this->yellow->page->fileName = substru($location, 1);
if(!is_readable($this->yellow->page->fileName))
{
2016-01-21 17:45:45 +00:00
ob_start();
2017-02-17 13:52:55 +00:00
$staticUrl = $this->yellow->config->get("staticUrl");
list($scheme, $address, $base) = $this->yellow->lookup->getUrlInformation($staticUrl);
$statusCode = $this->requestStaticFile($scheme, $address, $base, $location);
2016-01-21 17:45:45 +00:00
if($statusCode<400 || $error)
{
2016-01-21 17:45:45 +00:00
$fileData = ob_get_contents();
$modified = strtotime($this->yellow->page->getHeader("Last-Modified"));
2016-08-18 19:57:07 +00:00
if($modified==0) $modified = $this->yellow->toolbox->getFileModified($this->yellow->page->fileName);
2016-01-21 17:45:45 +00:00
if($statusCode>=301 && $statusCode<=303)
{
$fileData = $this->getStaticRedirect($this->yellow->page->getHeader("Location"));
$modified = time();
}
$fileName = $this->getStaticFile($path, $location, $statusCode);
if(!$this->yellow->toolbox->createFile($fileName, $fileData, true) ||
!$this->yellow->toolbox->modifyFile($fileName, $modified))
{
$statusCode = 500;
$this->yellow->page->statusCode = $statusCode;
$this->yellow->page->set("pageError", "Can't write file '$fileName'!");
}
}
2016-01-21 17:45:45 +00:00
ob_end_clean();
} else {
$statusCode = 200;
2016-08-18 19:57:07 +00:00
$modified = $this->yellow->toolbox->getFileModified($this->yellow->page->fileName);
2015-04-29 07:26:48 +00:00
$fileName = $this->getStaticFile($path, $location, $statusCode);
2016-01-22 15:06:12 +00:00
if(!$this->yellow->toolbox->copyFile($this->yellow->page->fileName, $fileName, true) ||
2016-08-18 19:57:07 +00:00
!$this->yellow->toolbox->modifyFile($fileName, $modified))
2013-07-30 13:32:08 +00:00
{
$statusCode = 500;
$this->yellow->page->statusCode = $statusCode;
$this->yellow->page->set("pageError", "Can't write file '$fileName'!");
2013-07-30 13:32:08 +00:00
}
}
2017-02-17 13:52:55 +00:00
if($statusCode==200 && $analyse) $this->analyseStaticFile($scheme, $address, $base, $fileData);
2016-01-21 17:45:45 +00:00
if($statusCode==404 && $probe) $statusCode = 100;
2017-02-17 13:52:55 +00:00
if($statusCode==404 && $error) $statusCode = 200;
2016-07-19 22:12:05 +00:00
if($statusCode>=200) ++$this->files;
if($statusCode>=400)
2014-02-04 14:06:48 +00:00
{
2016-01-22 15:06:12 +00:00
++$this->errors;
2016-01-21 17:45:45 +00:00
echo "ERROR building location '$location', ".$this->yellow->page->getStatusCode(true)."\n";
2014-02-04 14:06:48 +00:00
}
if(defined("DEBUG") && DEBUG>=1) echo "YellowCommand::buildStaticFile status:$statusCode location:$location<br/>\n";
return $statusCode;
}
2016-07-13 10:11:56 +00:00
// Request static file
2017-02-17 13:52:55 +00:00
function requestStaticFile($scheme, $address, $base, $location)
2016-07-13 10:11:56 +00:00
{
2017-02-17 13:52:55 +00:00
list($serverName, $serverPort) = explode(':', $address);
if(is_null($serverPort)) $serverPort = $scheme=="https" ? 443 : 80;
$_SERVER["HTTPS"] = $scheme=="https" ? "on" : "off";
2016-07-13 10:11:56 +00:00
$_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1";
2017-02-17 13:52:55 +00:00
$_SERVER["SERVER_NAME"] = $serverName;
$_SERVER["SERVER_PORT"] = $serverPort;
$_SERVER["REQUEST_METHOD"] = "GET";
2017-02-17 13:52:55 +00:00
$_SERVER["REQUEST_URI"] = $base.$location;
$_SERVER["SCRIPT_NAME"] = $base."/yellow.php";
2016-07-13 10:11:56 +00:00
$_SERVER["REMOTE_ADDR"] = "127.0.0.1";
$_REQUEST = array();
return $this->yellow->request();
}
2016-01-22 15:06:12 +00:00
// Analyse static file, detect locations with arguments
2017-02-17 13:52:55 +00:00
function analyseStaticFile($scheme, $address, $base, $rawData)
2014-02-04 14:06:48 +00:00
{
$pagination = $this->yellow->config->get("contentPagination");
2016-07-10 18:44:02 +00:00
preg_match_all("/<(.*?)href=\"([^\"]+)\"(.*?)>/i", $rawData, $matches);
2014-02-04 14:06:48 +00:00
foreach($matches[2] as $match)
{
2017-02-17 13:52:55 +00:00
$location = rawurldecode($match);
if(preg_match("/^(.*?)#(.*)$/", $location, $tokens)) $location = $tokens[1];
if(preg_match("/^(\w+):\/\/([^\/]+)(.*)$/", $location, $tokens))
2014-02-04 14:06:48 +00:00
{
2017-02-17 13:52:55 +00:00
if($tokens[1]!=$scheme) continue;
if($tokens[2]!=$address) continue;
$location = $tokens[3];
2014-02-04 14:06:48 +00:00
}
2017-02-17 13:52:55 +00:00
if(substru($location, 0, strlenu($base))!=$base) continue;
$location = substru($location, strlenu($base));
if(!$this->yellow->toolbox->isLocationArgs($location)) continue;
2015-04-29 07:26:48 +00:00
if(!$this->yellow->toolbox->isLocationArgsPagination($location, $pagination))
2014-02-04 14:06:48 +00:00
{
$location = rtrim($location, '/').'/';
2015-04-29 07:26:48 +00:00
if(is_null($this->locationsArgs[$location]))
2014-02-04 14:06:48 +00:00
{
2015-04-29 07:26:48 +00:00
$this->locationsArgs[$location] = $location;
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommand::analyseStaticFile detected location:$location<br/>\n";
2014-02-04 14:06:48 +00:00
}
} else {
$location = rtrim($location, "0..9");
2015-04-29 07:26:48 +00:00
if(is_null($this->locationsArgsPagination[$location]))
2014-02-04 14:06:48 +00:00
{
2015-04-29 07:26:48 +00:00
$this->locationsArgsPagination[$location] = $location;
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommand::analyseStaticFile detected location:$location<br/>\n";
2014-02-04 14:06:48 +00:00
}
}
}
}
// Check static files for broken links
function checkCommand($args)
{
$statusCode = 0;
list($command, $path, $location) = $args;
if(empty($location) || $location[0]=='/')
{
if($this->checkStaticConfig())
{
$statusCode = $this->checkStaticFiles($path, $location);
} else {
$statusCode = 500;
$this->files = $this->links = 0;
$fileName = $this->yellow->config->get("configDir").$this->yellow->config->get("configFile");
echo "ERROR checking files: Please configure StaticUrl in file '$fileName'!\n";
}
echo "Yellow $command: $this->files file".($this->files!=1 ? 's' : '');
echo ", $this->links link".($this->links!=1 ? 's' : '')."\n";
} else {
$statusCode = 400;
echo "Yellow $command: Invalid arguments\n";
}
return $statusCode;
}
// Check static files
function checkStaticFiles($path, $locationFilter)
{
$path = rtrim(empty($path) ? $this->yellow->config->get("staticDir") : $path, '/');
$this->files = $this->links = 0;
$regex = "/^[^.]+$|".$this->yellow->config->get("staticDefaultFile")."$/";
$fileNames = $this->yellow->toolbox->getDirectoryEntriesRecursive($path, $regex, false, false);
list($statusCodeFiles, $links) = $this->analyseStaticFiles($path, $locationFilter, $fileNames);
list($statusCodeLinks, $broken, $redirect) = $this->analyseLinks($path, $links);
if($statusCodeLinks!=200)
{
$this->showLinks($broken, "Broken links");
$this->showLinks($redirect, "Redirect links");
}
return max($statusCodeFiles, $statusCodeLinks);
}
// Analyse static files, detect links
function analyseStaticFiles($path, $locationFilter, $fileNames)
{
$statusCode = 200;
$links = array();
if(!empty($fileNames))
{
$staticUrl = $this->yellow->config->get("staticUrl");
list($scheme, $address, $base) = $this->yellow->lookup->getUrlInformation($staticUrl);
foreach($fileNames as $fileName)
{
if(is_readable($fileName))
{
$locationSource = $this->getStaticLocation($path, $fileName);
if(!preg_match("#^$base$locationFilter#", "$base$locationSource")) continue;
$fileData = $this->yellow->toolbox->readFile($fileName);
preg_match_all("/<(.*?)href=\"([^\"]+)\"(.*?)>/i", $fileData, $matches);
foreach($matches[2] as $match)
{
$location = rawurldecode($match);
if(preg_match("/^(.*?)#(.*)$/", $location, $tokens)) $location = $tokens[1];
if(preg_match("/^(\w+):\/\/([^\/]+)(.*)$/", $location, $matches))
{
$url = $location.(empty($matches[3]) ? "/" : "");
if(!is_null($links[$url])) $links[$url] .= ",";
$links[$url] .= $locationSource;
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommand::analyseStaticFiles detected url:$url<br/>\n";
} else if($location[0]=='/') {
$url = "$scheme://$address$location";
if(!is_null($links[$url])) $links[$url] .= ",";
$links[$url] .= $locationSource;
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommand::analyseStaticFiles detected url:$url<br/>\n";
}
}
++$this->files;
} else {
$statusCode = 500;
echo "ERROR reading files: Can't read file '$fileName'!\n";
}
}
$this->links = count($links);
} else {
$statusCode = 500;
echo "ERROR reading files: Can't find files in directory '$path'!\n";
}
return array($statusCode, $links);
}
// Analyse links, detect status
function analyseLinks($path, $links)
{
$statusCode = 200;
$broken = $redirect = $data = array();
2018-07-07 12:21:21 +00:00
$staticUrl = $this->yellow->config->get("staticUrl");
$staticUrlLength = strlenu(rtrim($staticUrl, '/'));
2018-07-07 12:21:21 +00:00
list($scheme, $address, $base) = $this->yellow->lookup->getUrlInformation($staticUrl);
$staticLocations = $this->getContentLocations(true);
uksort($links, "strnatcasecmp");
foreach($links as $url=>$value)
{
2018-07-07 12:21:21 +00:00
if(defined("DEBUG") && DEBUG>=1) echo "YellowCommand::analyseLinks url:$url\n";
if(preg_match("#^$staticUrl#", $url))
{
$location = substru($url, $staticUrlLength);
$fileName = $path.substru($url, $staticUrlLength);
2018-07-07 12:21:21 +00:00
if(is_readable($fileName)) continue;
if(in_array($location, $staticLocations)) continue;
}
if(preg_match("/^(http|https):/", $url))
{
$referer = "$scheme://$address$base".(($pos = strposu($value, ',')) ? substru($value, 0, $pos) : $value);
2018-07-07 12:21:21 +00:00
$statusCodeUrl = $this->getLinkStatus($url, $referer);
if($statusCodeUrl!=200)
{
2018-07-07 12:21:21 +00:00
$statusCode = max($statusCode, $statusCodeUrl);
$data[$url] = "$statusCodeUrl,$value";
}
}
2018-07-07 12:21:21 +00:00
}
foreach($data as $url=>$value)
{
$locations = preg_split("/\s*,\s*/", $value);
$statusCodeUrl = array_shift($locations);
foreach($locations as $location)
{
2018-07-07 12:21:21 +00:00
if($statusCodeUrl==302) continue;
if($statusCodeUrl>=300 && $statusCodeUrl<=399) {
$redirect["$scheme://$address$base$location -> $url - ".$this->getStatusFormatted($statusCodeUrl)] = $statusCodeUrl;
2018-07-07 12:21:21 +00:00
} else {
$broken["$scheme://$address$base$location -> $url - ".$this->getStatusFormatted($statusCodeUrl)] = $statusCodeUrl;
}
}
}
return array($statusCode, $broken, $redirect);
}
// Show links
function showLinks($data, $text)
{
if(!empty($data))
{
echo "$text\n\n";
uksort($data, "strnatcasecmp");
$data = array_slice($data, 0, 99);
foreach($data as $key=>$value) echo "- $key\n";
echo "\n";
}
}
2014-02-04 14:06:48 +00:00
2015-11-30 23:31:57 +00:00
// Clean static files
2014-07-25 10:46:58 +00:00
function cleanCommand($args)
{
$statusCode = 0;
2016-06-27 13:28:10 +00:00
list($command, $path, $location) = $args;
2015-01-19 19:05:51 +00:00
if(empty($location) || $location[0]=='/')
2014-07-25 10:46:58 +00:00
{
$statusCode = $this->cleanStaticFiles($path, $location);
2015-11-30 23:31:57 +00:00
echo "Yellow $command: Static file".(empty($location) ? "s" : "")." ".($statusCode!=200 ? "not " : "")."cleaned\n";
2014-07-25 10:46:58 +00:00
} else {
$statusCode = 400;
echo "Yellow $command: Invalid arguments\n";
}
return $statusCode;
}
2016-01-21 17:45:45 +00:00
// Clean static files and directories
function cleanStaticFiles($path, $location)
2014-07-25 10:46:58 +00:00
{
$statusCode = 200;
2015-01-19 19:05:51 +00:00
$path = rtrim(empty($path) ? $this->yellow->config->get("staticDir") : $path, '/');
2014-07-25 10:46:58 +00:00
if(empty($location))
{
2016-07-15 16:35:11 +00:00
$statusCode = max($statusCode, $this->commandBroadcast("clean", "all"));
2014-07-25 10:46:58 +00:00
$statusCode = max($statusCode, $this->cleanStaticDirectory($path));
} else {
if($this->yellow->lookup->isFileLocation($location))
{
$fileName = $this->getStaticFile($path, $location, $statusCode);
$statusCode = $this->cleanStaticFile($fileName);
} else {
$statusCode = $this->cleanStaticDirectory($path.$location);
}
2014-07-25 10:46:58 +00:00
}
return $statusCode;
}
// Clean static directory
function cleanStaticDirectory($path)
{
$statusCode = 200;
2015-11-30 23:31:57 +00:00
if(is_dir($path) && $this->checkStaticDirectory($path))
2014-07-25 10:46:58 +00:00
{
2016-08-07 10:51:23 +00:00
if(!$this->yellow->toolbox->deleteDirectory($path))
2014-07-25 10:46:58 +00:00
{
$statusCode = 500;
2015-11-30 23:31:57 +00:00
echo "ERROR cleaning files: Can't delete directory '$path'!\n";
2014-07-25 10:46:58 +00:00
}
}
return $statusCode;
}
// Clean static file
function cleanStaticFile($fileName)
2014-07-25 10:46:58 +00:00
{
$statusCode = 200;
2015-04-29 07:26:48 +00:00
if(is_file($fileName))
2014-07-25 10:46:58 +00:00
{
2015-11-30 23:31:57 +00:00
if(!$this->yellow->toolbox->deleteFile($fileName))
2014-07-25 10:46:58 +00:00
{
2015-04-29 07:26:48 +00:00
$statusCode = 500;
2015-11-30 23:31:57 +00:00
echo "ERROR cleaning files: Can't delete file '$fileName'!\n";
2014-07-25 10:46:58 +00:00
}
}
return $statusCode;
}
2016-07-15 16:35:11 +00:00
// Broadcast command to other plugins
function commandBroadcast($args)
2014-07-25 10:46:58 +00:00
{
$statusCode = 0;
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if($key=="command") continue;
2016-07-15 16:35:11 +00:00
if(method_exists($value["obj"], "onCommand"))
2014-07-25 10:46:58 +00:00
{
2016-06-27 13:28:10 +00:00
$statusCode = $value["obj"]->onCommand(func_get_args());
2016-07-19 22:12:05 +00:00
if($statusCode!=0) break;
2014-07-25 10:46:58 +00:00
}
2016-06-27 13:28:10 +00:00
}
return $statusCode;
}
// Show software version and updates
function versionCommand($args)
{
2017-02-17 13:52:55 +00:00
$serverVersion = $this->yellow->toolbox->getServerVersion();
echo "Datenstrom Yellow ".YellowCore::VERSION.", PHP ".PHP_VERSION.", $serverVersion\n";
2016-06-27 13:28:10 +00:00
list($statusCode, $dataCurrent) = $this->getSoftwareVersion();
list($statusCode, $dataLatest) = $this->getSoftwareVersion(true);
foreach($dataCurrent as $key=>$value)
{
2016-07-19 22:12:05 +00:00
if(strnatcasecmp($dataCurrent[$key], $dataLatest[$key])>=0)
2016-06-27 13:28:10 +00:00
{
echo "$key $value\n";
} else {
2016-08-16 08:16:13 +00:00
echo "$key $dataLatest[$key] - Update available\n";
2016-06-27 13:28:10 +00:00
}
2014-07-25 10:46:58 +00:00
}
2016-07-19 22:12:05 +00:00
if($statusCode!=200) echo "ERROR checking updates: ".$this->yellow->page->get("pageError")."\n";
2014-07-25 10:46:58 +00:00
return $statusCode;
}
// Check static configuration
function checkStaticConfig()
{
2017-02-17 13:52:55 +00:00
$staticUrl = $this->yellow->config->get("staticUrl");
return !empty($staticUrl);
2015-04-29 07:26:48 +00:00
}
// Check static directory
function checkStaticDirectory($path)
{
$ok = false;
if(!empty($path))
{
2016-07-19 22:12:05 +00:00
if($path==rtrim($this->yellow->config->get("staticDir"), '/')) $ok = true;
if($path==rtrim($this->yellow->config->get("trashDir"), '/')) $ok = true;
2015-11-30 23:31:57 +00:00
if(is_file("$path/".$this->yellow->config->get("staticDefaultFile"))) $ok = true;
2015-04-29 07:26:48 +00:00
if(is_file("$path/yellow.php")) $ok = false;
}
return $ok;
}
2016-01-21 17:45:45 +00:00
// Return static file
function getStaticFile($path, $location, $statusCode)
{
2016-07-19 22:12:05 +00:00
if($statusCode<400)
2016-01-21 17:45:45 +00:00
{
$fileName = $path.$location;
if(!$this->yellow->lookup->isFileLocation($location)) $fileName .= $this->yellow->config->get("staticDefaultFile");
2016-07-19 22:12:05 +00:00
} else if($statusCode==404) {
2016-01-21 17:45:45 +00:00
$fileName = $path."/".$this->yellow->config->get("staticErrorFile");
}
return $fileName;
}
// Return static location
function getStaticLocation($path, $fileName)
{
$location = substru($fileName, strlenu($path));
if(basename($location)==$this->yellow->config->get("staticDefaultFile"))
{
$defaultFileLength = strlenu($this->yellow->config->get("staticDefaultFile"));
$location = substru($location, 0, -$defaultFileLength);
}
return $location;
}
2016-01-21 17:45:45 +00:00
// Return static redirect
function getStaticRedirect($location)
{
$output = "<!DOCTYPE html><html>\n<head>\n";
$output .= "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
$output .= "<meta http-equiv=\"refresh\" content=\"0;url=".htmlspecialchars($location)."\" />\n";
$output .= "</head>\n</html>";
return $output;
}
// Return human readable status
function getStatusFormatted($statusCode)
{
return $this->yellow->toolbox->getHttpStatusFormatted($statusCode, true);
}
2016-01-21 17:45:45 +00:00
// Return content locations
function getContentLocations($includeAll = false)
{
$locations = array();
2017-02-17 13:52:55 +00:00
$staticUrl = $this->yellow->config->get("staticUrl");
list($scheme, $address, $base) = $this->yellow->lookup->getUrlInformation($staticUrl);
$this->yellow->page->setRequestInformation($scheme, $address, $base, "", "");
2015-01-19 19:05:51 +00:00
foreach($this->yellow->pages->index(true, true) as $page)
{
if(($page->get("status")!="ignore" && $page->get("status")!="draft") || $includeAll)
2015-09-06 15:59:14 +00:00
{
array_push($locations, $page->location);
}
2015-01-19 19:05:51 +00:00
}
if(!$this->yellow->pages->find("/") && $this->yellow->config->get("multiLanguageMode")) array_unshift($locations, "/");
return $locations;
}
2016-01-21 17:45:45 +00:00
// Return media locations
function getMediaLocations()
{
2016-01-21 17:45:45 +00:00
$locations = array();
2016-08-18 19:57:07 +00:00
$fileNames = $this->yellow->toolbox->getDirectoryEntriesRecursive($this->yellow->config->get("mediaDir"), "/.*/", false, false);
2016-01-21 17:45:45 +00:00
foreach($fileNames as $fileName)
{
array_push($locations, "/".$fileName);
}
return $locations;
2015-01-19 19:05:51 +00:00
}
2016-01-21 17:45:45 +00:00
// Return system locations
function getSystemLocations()
2015-01-19 19:05:51 +00:00
{
2016-01-21 17:45:45 +00:00
$locations = array();
2017-06-26 13:19:49 +00:00
$regex = "/\.(css|gif|ico|js|jpg|png|svg|txt|woff|woff2)$/";
2017-01-03 13:11:44 +00:00
$pluginDirLength = strlenu($this->yellow->config->get("pluginDir"));
$fileNames = $this->yellow->toolbox->getDirectoryEntriesRecursive($this->yellow->config->get("pluginDir"), $regex, false, false);
2015-01-19 19:05:51 +00:00
foreach($fileNames as $fileName)
{
2017-01-03 13:11:44 +00:00
array_push($locations, $this->yellow->config->get("pluginLocation").substru($fileName, $pluginDirLength));
2015-01-19 19:05:51 +00:00
}
2017-01-03 13:11:44 +00:00
$themeDirLength = strlenu($this->yellow->config->get("themeDir"));
$fileNames = $this->yellow->toolbox->getDirectoryEntriesRecursive($this->yellow->config->get("themeDir"), $regex, false, false);
2015-01-19 19:05:51 +00:00
foreach($fileNames as $fileName)
{
2017-01-03 13:11:44 +00:00
array_push($locations, $this->yellow->config->get("themeLocation").substru($fileName, $themeDirLength));
2016-08-18 19:57:07 +00:00
}
2016-01-21 17:45:45 +00:00
array_push($locations, "/".$this->yellow->config->get("robotsFile"));
return $locations;
2015-04-29 07:26:48 +00:00
}
2013-12-21 13:10:15 +00:00
// Return command help
function getCommandHelp()
{
$data = array();
foreach($this->yellow->plugins->plugins as $key=>$value)
{
if(method_exists($value["obj"], "onCommandHelp"))
{
foreach(preg_split("/[\r\n]+/", $value["obj"]->onCommandHelp()) as $line)
{
2016-07-11 14:42:16 +00:00
list($command) = explode(' ', $line);
2013-12-21 13:10:15 +00:00
if(!empty($command) && is_null($data[$command])) $data[$command] = $line;
}
}
}
2017-11-30 23:31:43 +00:00
uksort($data, "strnatcasecmp");
return $data;
2013-06-07 20:01:12 +00:00
}
2016-06-27 13:28:10 +00:00
// Return software version
function getSoftwareVersion($latest = false)
{
$data = array();
if($this->yellow->plugins->isExisting("update"))
{
list($statusCode, $data) = $this->yellow->plugins->get("update")->getSoftwareVersion($latest);
} else {
$statusCode = 200;
2017-01-03 13:11:44 +00:00
$data = array_merge($this->yellow->plugins->getData(), $this->yellow->themes->getData());
2016-06-27 13:28:10 +00:00
}
return array($statusCode, $data);
}
// Return link status
function getLinkStatus($url, $referer)
{
2018-07-07 12:21:21 +00:00
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_REFERER, $referer);
curl_setopt($curlHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; DatenstromYellow/".YellowCore::VERSION."; LinkChecker)");
curl_setopt($curlHandle, CURLOPT_NOBODY, 1);
curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 30);
curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle);
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommand::getLinkStatus status:$statusCode url:$url<br/>\n";
return $statusCode;
}
2013-06-07 20:01:12 +00:00
}
$yellow->plugins->register("command", "YellowCommand", YellowCommand::VERSION);
?>