2013-06-07 20:01:12 +00:00
|
|
|
<?php
|
2016-01-21 17:45:45 +00:00
|
|
|
// Copyright (c) 2013-2016 Datenstrom, http://datenstrom.se
|
2013-06-07 20:01:12 +00:00
|
|
|
// This file may be used and distributed under the terms of the public license.
|
|
|
|
|
2015-10-06 12:19:11 +00:00
|
|
|
// Command line plugin
|
2013-12-01 11:59:07 +00:00
|
|
|
class YellowCommandline
|
2013-06-07 20:01:12 +00:00
|
|
|
{
|
2016-01-22 15:06:12 +00:00
|
|
|
const Version = "0.6.4";
|
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 $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
|
|
|
{
|
2013-07-24 10:27:13 +00:00
|
|
|
$this->yellow = $yellow;
|
2015-10-06 12:19:11 +00:00
|
|
|
$this->yellow->config->setDefault("commandlineVersionUrl", "https://github.com/datenstrom/yellow-extensions");
|
2013-06-07 20:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle command
|
|
|
|
function onCommand($args)
|
|
|
|
{
|
|
|
|
list($name, $command) = $args;
|
2013-07-24 10:27:13 +00:00
|
|
|
switch($command)
|
|
|
|
{
|
2013-12-21 13:10:15 +00:00
|
|
|
case "": $statusCode = $this->helpCommand(); break;
|
2015-10-06 12:19:11 +00:00
|
|
|
case "version": $statusCode = $this->versionCommand($args); break;
|
2013-12-21 13:10:15 +00:00
|
|
|
case "build": $statusCode = $this->buildCommand($args); break;
|
2014-07-25 10:46:58 +00:00
|
|
|
case "clean": $statusCode = $this->cleanCommand($args); break;
|
|
|
|
default: $statusCode = $this->pluginCommand($args);
|
|
|
|
}
|
|
|
|
if($statusCode == 0)
|
|
|
|
{
|
|
|
|
$statusCode = 400;
|
|
|
|
echo "Yellow $command: Command not found\n";
|
2013-07-24 10:27:13 +00:00
|
|
|
}
|
2013-06-07 20:01:12 +00:00
|
|
|
return $statusCode;
|
|
|
|
}
|
|
|
|
|
2015-04-29 07:26:48 +00:00
|
|
|
// Handle command help
|
|
|
|
function onCommandHelp()
|
|
|
|
{
|
|
|
|
$help .= "version\n";
|
|
|
|
$help .= "build [DIRECTORY LOCATION]\n";
|
|
|
|
$help .= "clean [DIRECTORY LOCATION]\n";
|
|
|
|
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
|
|
|
{
|
2015-10-06 12:19:11 +00:00
|
|
|
echo "Yellow ".YellowCore::Version."\n";
|
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
|
|
|
}
|
2013-07-24 10:27:13 +00:00
|
|
|
|
2015-10-14 15:01:09 +00:00
|
|
|
// Show software version and updates
|
2015-10-06 12:19:11 +00:00
|
|
|
function versionCommand($args)
|
2013-06-07 20:01:12 +00:00
|
|
|
{
|
2015-10-06 12:19:11 +00:00
|
|
|
$statusCode = 0;
|
2015-12-08 00:01:04 +00:00
|
|
|
$serverSoftware = $this->yellow->toolbox->getServerSoftware();
|
|
|
|
echo "Yellow ".YellowCore::Version.", PHP ".PHP_VERSION.", $serverSoftware\n";
|
2015-10-06 12:19:11 +00:00
|
|
|
$url = $this->yellow->config->get("commandlineVersionUrl");
|
|
|
|
list($dummy, $command) = $args;
|
|
|
|
list($statusCode, $versionCurrent) = $this->getPluginVersion();
|
|
|
|
list($statusCode, $versionLatest) = $this->getPluginVersion($url);
|
|
|
|
foreach($versionCurrent as $key=>$value)
|
|
|
|
{
|
|
|
|
if($versionCurrent[$key] >= $versionLatest[$key])
|
|
|
|
{
|
|
|
|
echo "$key $value\n";
|
|
|
|
} else {
|
|
|
|
echo "$key $value - Update available\n";
|
|
|
|
++$updates;
|
|
|
|
}
|
|
|
|
}
|
2015-10-08 14:52:01 +00:00
|
|
|
if($statusCode != 200) echo "ERROR checking updates at $url: $versionLatest[error]\n";
|
2015-10-06 12:19:11 +00:00
|
|
|
if($updates) echo "Yellow $command: $updates update".($updates==1 ? "":"s")." available at $url\n";
|
|
|
|
return $statusCode;
|
2013-07-29 18:49:57 +00:00
|
|
|
}
|
2015-10-06 12:19:11 +00:00
|
|
|
|
2015-11-30 23:31:57 +00:00
|
|
|
// Build static files
|
2013-12-21 13:10:15 +00:00
|
|
|
function buildCommand($args)
|
2013-07-29 18:49:57 +00:00
|
|
|
{
|
|
|
|
$statusCode = 0;
|
2013-12-21 13:10:15 +00:00
|
|
|
list($dummy, $command, $path, $location) = $args;
|
2015-01-19 19:05:51 +00:00
|
|
|
if(empty($location) || $location[0]=='/')
|
2013-07-24 10:27:13 +00:00
|
|
|
{
|
2015-11-30 23:31:57 +00:00
|
|
|
if($this->checkStaticConfig() && $this->checkStaticFilesystem())
|
2013-07-24 10:27:13 +00:00
|
|
|
{
|
2015-01-19 19:05:51 +00:00
|
|
|
$statusCode = $this->buildStatic($path, $location);
|
2013-07-24 10:27:13 +00:00
|
|
|
} else {
|
2014-04-03 13:23:07 +00:00
|
|
|
$statusCode = 500;
|
2016-01-22 15:06:12 +00:00
|
|
|
$this->files = 0; $this->errors = 1;
|
2015-11-30 23:31:57 +00:00
|
|
|
if(!$this->checkStaticFilesystem())
|
|
|
|
{
|
|
|
|
echo "ERROR building files: Static website not supported on Windows file system!\n";
|
|
|
|
} else {
|
|
|
|
$fileName = $this->yellow->config->get("configDir").$this->yellow->config->get("configFile");
|
|
|
|
echo "ERROR building files: Please configure ServerScheme, ServerName, ServerBase, ServerTime in file '$fileName'!\n";
|
|
|
|
}
|
2013-07-24 10:27:13 +00:00
|
|
|
}
|
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' : '');
|
2013-07-29 18:49:57 +00:00
|
|
|
echo ", status $statusCode\n";
|
|
|
|
} else {
|
2013-12-21 13:10:15 +00:00
|
|
|
$statusCode = 400;
|
2014-04-03 13:23:07 +00:00
|
|
|
echo "Yellow $command: Invalid arguments\n";
|
2013-07-29 18:49:57 +00:00
|
|
|
}
|
|
|
|
return $statusCode;
|
|
|
|
}
|
|
|
|
|
2016-01-22 15:06:12 +00:00
|
|
|
// Build static files and additional locations
|
2015-01-19 19:05:51 +00:00
|
|
|
function buildStatic($path, $location)
|
2013-07-29 18:49:57 +00:00
|
|
|
{
|
2013-07-30 09:31:40 +00:00
|
|
|
$this->yellow->toolbox->timerStart($time);
|
2015-01-19 19:05:51 +00:00
|
|
|
$path = rtrim(empty($path) ? $this->yellow->config->get("staticDir") : $path, '/');
|
2016-01-22 15:06:12 +00:00
|
|
|
$this->files = $this->errors = $statusCode = 0;
|
2015-04-29 07:26:48 +00:00
|
|
|
$this->locationsArgs = $this->locationsArgsPagination = array();
|
2013-07-29 18:49:57 +00:00
|
|
|
if(empty($location))
|
|
|
|
{
|
2015-01-19 19:05:51 +00:00
|
|
|
$statusCode = $this->cleanStatic($path, $location);
|
2016-01-21 17:45:45 +00:00
|
|
|
foreach($this->getContentLocations() as $location)
|
2014-06-27 17:50:21 +00:00
|
|
|
{
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCode = max($statusCode, $this->buildStaticFile($path, $location, true));
|
2014-06-27 17:50:21 +00:00
|
|
|
}
|
2015-04-29 07:26:48 +00:00
|
|
|
foreach($this->locationsArgs as $location)
|
2014-06-27 17:50:21 +00:00
|
|
|
{
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCode = max($statusCode, $this->buildStaticFile($path, $location, true));
|
2014-06-27 17:50:21 +00:00
|
|
|
}
|
2015-04-29 07:26:48 +00:00
|
|
|
foreach($this->locationsArgsPagination as $location)
|
2014-06-27 17:50:21 +00:00
|
|
|
{
|
2015-04-29 07:26:48 +00:00
|
|
|
if(substru($location, -1) != ':')
|
|
|
|
{
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCode = max($statusCode, $this->buildStaticFile($path, $location, false, true));
|
2015-04-29 07:26:48 +00:00
|
|
|
}
|
2014-06-27 17:50:21 +00:00
|
|
|
for($pageNumber=2; $pageNumber<=999; ++$pageNumber)
|
|
|
|
{
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCodeLocation = $this->buildStaticFile($path, $location.$pageNumber, false, true);
|
2014-07-25 10:46:58 +00:00
|
|
|
$statusCode = max($statusCode, $statusCodeLocation);
|
2016-01-21 17:45:45 +00:00
|
|
|
if($statusCodeLocation == 100) break;
|
2014-06-27 17:50:21 +00:00
|
|
|
}
|
|
|
|
}
|
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)
|
2014-06-27 17:50:21 +00:00
|
|
|
{
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCode = max($statusCode, $this->buildStaticFile($path, $location));
|
2014-06-27 17:50:21 +00:00
|
|
|
}
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCode = max($statusCode, $this->buildStaticFile($path, "/error", false, false, true));
|
2014-06-27 17:50:21 +00:00
|
|
|
} else {
|
2016-01-22 15:06:12 +00:00
|
|
|
$statusCode = $this->buildStaticFile($path, $location);
|
2013-07-30 13:32:08 +00:00
|
|
|
}
|
2013-07-30 09:31:40 +00:00
|
|
|
$this->yellow->toolbox->timerStop($time);
|
2013-12-01 11:59:07 +00:00
|
|
|
if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStatic time:$time ms\n";
|
2014-07-25 10:46:58 +00:00
|
|
|
return $statusCode;
|
2013-07-24 10:27:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$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))
|
2013-07-24 10:27:13 +00:00
|
|
|
{
|
2016-01-21 17:45:45 +00:00
|
|
|
ob_start();
|
|
|
|
$_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1";
|
|
|
|
$_SERVER["SERVER_NAME"] = $this->yellow->config->get("serverName");
|
|
|
|
$_SERVER["REQUEST_URI"] = $this->yellow->config->get("serverBase").$location;
|
|
|
|
$_SERVER["SCRIPT_NAME"] = $this->yellow->config->get("serverBase")."/yellow.php";
|
|
|
|
$_REQUEST = array();
|
|
|
|
$statusCode = $this->yellow->request();
|
|
|
|
if($statusCode<400 || $error)
|
2013-07-24 10:27:13 +00:00
|
|
|
{
|
2016-01-21 17:45:45 +00:00
|
|
|
$fileData = ob_get_contents();
|
|
|
|
$modified = strtotime($this->yellow->page->getHeader("Last-Modified"));
|
|
|
|
if($modified == 0) $modified = filemtime($this->yellow->page->fileName);
|
|
|
|
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'!");
|
|
|
|
}
|
2013-07-24 10:27:13 +00:00
|
|
|
}
|
2016-01-21 17:45:45 +00:00
|
|
|
ob_end_clean();
|
|
|
|
} else {
|
|
|
|
$statusCode = 200;
|
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) ||
|
|
|
|
!$this->yellow->toolbox->modifyFile($fileName, filemtime($this->yellow->page->fileName)))
|
2013-07-30 13:32:08 +00:00
|
|
|
{
|
|
|
|
$statusCode = 500;
|
2015-01-20 10:15:04 +00:00
|
|
|
$this->yellow->page->statusCode = $statusCode;
|
|
|
|
$this->yellow->page->set("pageError", "Can't write file '$fileName'!");
|
2013-07-30 13:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-22 15:06:12 +00:00
|
|
|
if($statusCode==200 && $analyse) $this->analyseStaticFile($fileData);
|
2015-04-29 07:26:48 +00:00
|
|
|
if($statusCode==404 && $error) $statusCode = 200;
|
2016-01-21 17:45:45 +00:00
|
|
|
if($statusCode==404 && $probe) $statusCode = 100;
|
2016-01-22 15:06:12 +00:00
|
|
|
if($statusCode >= 200) ++$this->files;
|
2014-02-04 14:06:48 +00:00
|
|
|
if($statusCode >= 400)
|
|
|
|
{
|
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
|
|
|
}
|
2016-01-22 15:06:12 +00:00
|
|
|
if(defined("DEBUG") && DEBUG>=1) echo "YellowCommandline::buildStaticFile status:$statusCode location:$location\n";
|
2014-01-29 22:35:44 +00:00
|
|
|
return $statusCode;
|
2013-07-29 18:49:57 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 15:06:12 +00:00
|
|
|
// Analyse static file, detect locations with arguments
|
|
|
|
function analyseStaticFile($text)
|
2014-02-04 14:06:48 +00:00
|
|
|
{
|
|
|
|
$serverName = $this->yellow->config->get("serverName");
|
|
|
|
$serverBase = $this->yellow->config->get("serverBase");
|
2014-03-16 18:32:52 +00:00
|
|
|
$pagination = $this->yellow->config->get("contentPagination");
|
2014-04-03 13:23:07 +00:00
|
|
|
preg_match_all("/<a(.*?)href=\"([^\"]+)\"(.*?)>/i", $text, $matches);
|
2014-02-04 14:06:48 +00:00
|
|
|
foreach($matches[2] as $match)
|
|
|
|
{
|
2015-11-30 23:31:57 +00:00
|
|
|
if(preg_match("/^(.*?)#(.*)$/", $match, $tokens)) $match = $tokens[1];
|
2014-02-04 14:06:48 +00:00
|
|
|
if(preg_match("/^\w+:\/+(.*?)(\/.*)$/", $match, $tokens))
|
|
|
|
{
|
|
|
|
if($tokens[1] != $serverName) continue;
|
|
|
|
$match = $tokens[2];
|
|
|
|
}
|
|
|
|
if(!$this->yellow->toolbox->isLocationArgs($match)) continue;
|
|
|
|
if(substru($match, 0, strlenu($serverBase)) != $serverBase) continue;
|
2014-03-16 18:32:52 +00:00
|
|
|
$location = rawurldecode(substru($match, strlenu($serverBase)));
|
2015-04-29 07:26:48 +00:00
|
|
|
if(!$this->yellow->toolbox->isLocationArgsPagination($location, $pagination))
|
2014-02-04 14:06:48 +00:00
|
|
|
{
|
2014-03-16 18:32:52 +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;
|
2016-01-22 15:06:12 +00:00
|
|
|
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommandline::analyseStaticFile detected location:$location\n";
|
2014-02-04 14:06:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-03-16 18:32:52 +00:00
|
|
|
$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;
|
2016-01-22 15:06:12 +00:00
|
|
|
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommandline::analyseStaticFile detected location:$location\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;
|
|
|
|
list($dummy, $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
|
|
|
{
|
2015-01-19 19:05:51 +00:00
|
|
|
$statusCode = $this->cleanStatic($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
|
2015-01-19 19:05:51 +00:00
|
|
|
function cleanStatic($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))
|
|
|
|
{
|
|
|
|
$statusCode = max($statusCode, $this->pluginCommand(array("all", "clean")));
|
|
|
|
$statusCode = max($statusCode, $this->cleanStaticDirectory($path));
|
|
|
|
} else {
|
2015-01-19 19:05:51 +00:00
|
|
|
$statusCode = $this->cleanStaticFile($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
|
|
|
{
|
2015-11-30 23:31:57 +00:00
|
|
|
if(!$this->yellow->toolbox->deleteDirectory($path, true))
|
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
|
2015-01-19 19:05:51 +00:00
|
|
|
function cleanStaticFile($path, $location)
|
2014-07-25 10:46:58 +00:00
|
|
|
{
|
|
|
|
$statusCode = 200;
|
2015-04-29 07:26:48 +00:00
|
|
|
$fileName = $this->getStaticFile($path, $location, $statusCode);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward plugin command
|
|
|
|
function pluginCommand($args)
|
|
|
|
{
|
|
|
|
$statusCode = 0;
|
|
|
|
foreach($this->yellow->plugins->plugins as $key=>$value)
|
|
|
|
{
|
|
|
|
if($key == "commandline") continue;
|
|
|
|
if(method_exists($value["obj"], "onCommand"))
|
|
|
|
{
|
|
|
|
$statusCode = $value["obj"]->onCommand($args);
|
|
|
|
if($statusCode != 0) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $statusCode;
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:32:52 +00:00
|
|
|
// Check static configuration
|
|
|
|
function checkStaticConfig()
|
2013-08-04 09:42:21 +00:00
|
|
|
{
|
2014-05-15 11:53:54 +00:00
|
|
|
$serverScheme = $this->yellow->config->get("serverScheme");
|
2014-03-16 18:32:52 +00:00
|
|
|
$serverName = $this->yellow->config->get("serverName");
|
|
|
|
$serverBase = $this->yellow->config->get("serverBase");
|
2015-01-19 19:05:51 +00:00
|
|
|
return !empty($serverScheme) && !empty($serverName) &&
|
2015-04-29 07:26:48 +00:00
|
|
|
$this->yellow->lookup->isValidLocation($serverBase) && $serverBase!="/";
|
|
|
|
}
|
|
|
|
|
2015-11-30 23:31:57 +00:00
|
|
|
// Check static filesystem
|
|
|
|
function checkStaticFilesystem()
|
|
|
|
{
|
|
|
|
return strtoupperu(substru(PHP_OS, 0, 3)) != "WIN";
|
|
|
|
}
|
|
|
|
|
2015-04-29 07:26:48 +00:00
|
|
|
// Check static directory
|
|
|
|
function checkStaticDirectory($path)
|
|
|
|
{
|
|
|
|
$ok = false;
|
|
|
|
if(!empty($path))
|
|
|
|
{
|
|
|
|
if($path == rtrim($this->yellow->config->get("staticDir"), '/')) $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;
|
2013-08-04 09:42:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:45:45 +00:00
|
|
|
// Return static file
|
|
|
|
function getStaticFile($path, $location, $statusCode)
|
|
|
|
{
|
|
|
|
if($statusCode < 400)
|
|
|
|
{
|
|
|
|
$fileName = $path.$location;
|
|
|
|
if(!$this->yellow->lookup->isFileLocation($location)) $fileName .= $this->yellow->config->get("staticDefaultFile");
|
|
|
|
} else if($statusCode == 404) {
|
|
|
|
$fileName = $path."/".$this->yellow->config->get("staticErrorFile");
|
|
|
|
}
|
|
|
|
return $fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 content locations
|
|
|
|
function getContentLocations()
|
2014-10-02 09:55:32 +00:00
|
|
|
{
|
|
|
|
$locations = array();
|
2015-01-19 19:05:51 +00:00
|
|
|
$serverScheme = $this->yellow->config->get("serverScheme");
|
|
|
|
$serverName = $this->yellow->config->get("serverName");
|
|
|
|
$serverBase = $this->yellow->config->get("serverBase");
|
|
|
|
$this->yellow->page->setRequestInformation($serverScheme, $serverName, $serverBase, "", "");
|
|
|
|
foreach($this->yellow->pages->index(true, true) as $page)
|
|
|
|
{
|
2015-09-06 15:59:14 +00:00
|
|
|
if($page->get("status")!="ignore" && $page->get("status")!="draft")
|
|
|
|
{
|
|
|
|
array_push($locations, $page->location);
|
|
|
|
}
|
2015-01-19 19:05:51 +00:00
|
|
|
}
|
2014-10-02 09:55:32 +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()
|
2013-07-24 10:27:13 +00:00
|
|
|
{
|
2016-01-21 17:45:45 +00:00
|
|
|
$locations = array();
|
2015-01-19 19:05:51 +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();
|
2015-01-19 19:05:51 +00:00
|
|
|
$fileNames = $this->yellow->toolbox->getDirectoryEntries(
|
|
|
|
$this->yellow->config->get("pluginDir"), "/\.(css|js|jpg|png|txt|woff)/", false, false);
|
|
|
|
foreach($fileNames as $fileName)
|
|
|
|
{
|
2016-01-21 17:45:45 +00:00
|
|
|
array_push($locations, $this->yellow->config->get("pluginLocation").basename($fileName));
|
2015-01-19 19:05:51 +00:00
|
|
|
}
|
|
|
|
$fileNames = $this->yellow->toolbox->getDirectoryEntries(
|
|
|
|
$this->yellow->config->get("themeDir"), "/\.(css|js|jpg|png|txt|woff)/", false, false);
|
|
|
|
foreach($fileNames as $fileName)
|
2013-07-24 10:27:13 +00:00
|
|
|
{
|
2016-01-21 17:45:45 +00:00
|
|
|
array_push($locations, $this->yellow->config->get("themeLocation").basename($fileName));
|
2015-04-29 07:26:48 +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
|
|
|
}
|
|
|
|
|
2015-10-06 12:19:11 +00:00
|
|
|
// Return plugin version
|
|
|
|
function getPluginVersion($url = "")
|
|
|
|
{
|
|
|
|
$version = array();
|
|
|
|
if(empty($url))
|
|
|
|
{
|
|
|
|
$statusCode = 200;
|
|
|
|
$version["YellowCore"] = YellowCore::Version;
|
|
|
|
foreach($this->yellow->plugins->plugins as $key=>$value) $version[$value["class"]] = $value[version];
|
|
|
|
} else {
|
|
|
|
if(extension_loaded("curl"))
|
|
|
|
{
|
|
|
|
$pluginVersionUrl = $this->getPluginVersionUrl($url);
|
|
|
|
$curlHandle = curl_init();
|
|
|
|
curl_setopt($curlHandle, CURLOPT_URL, $pluginVersionUrl);
|
|
|
|
curl_setopt($curlHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; YellowCore/".YellowCore::Version).")";
|
|
|
|
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 30);
|
|
|
|
$rawData = curl_exec($curlHandle);
|
|
|
|
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($curlHandle);
|
|
|
|
if($statusCode == 200)
|
|
|
|
{
|
|
|
|
if(defined("DEBUG") && DEBUG>=2) echo "YellowCommandline::getPluginVersion file:$pluginVersionUrl\n";
|
|
|
|
foreach($this->yellow->toolbox->getTextLines($rawData) as $line)
|
|
|
|
{
|
|
|
|
if(preg_match("/^(\w+)\s*:\s*([0-9\.]+)/", $line, $matches))
|
|
|
|
{
|
|
|
|
$version[$matches[1]] = $matches[2];
|
|
|
|
if(defined("DEBUG") && DEBUG>=3) echo "YellowCommandline::getPluginVersion $matches[1]:$matches[2]\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($statusCode == 0) $statusCode = 444;
|
|
|
|
$version["error"] = $this->yellow->toolbox->getHttpStatusFormatted($statusCode);
|
|
|
|
} else {
|
|
|
|
$statusCode = 500;
|
|
|
|
$version["error"] = "Plugin 'commandline' requires cURL library!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uksort($version, strnatcasecmp);
|
|
|
|
return array($statusCode, $version);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return plugin version URL from repository
|
|
|
|
function getPluginVersionUrl($url)
|
|
|
|
{
|
2015-10-12 16:00:26 +00:00
|
|
|
if(preg_match("#^https://github.com/(.+)$#", $url, $matches))
|
2015-10-06 12:19:11 +00:00
|
|
|
{
|
2015-10-12 16:00:26 +00:00
|
|
|
$url = "https://raw.githubusercontent.com/".$matches[1]."/master/version.ini";
|
2015-10-06 12:19:11 +00:00
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
list($command, $text) = explode(' ', $line, 2);
|
|
|
|
if(!empty($command) && is_null($data[$command])) $data[$command] = $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uksort($data, strnatcasecmp);
|
2013-07-29 18:49:57 +00:00
|
|
|
return $data;
|
2013-06-07 20:01:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 08:42:32 +00:00
|
|
|
$yellow->plugins->register("commandline", "YellowCommandline", YellowCommandline::Version);
|
2013-06-07 20:01:12 +00:00
|
|
|
?>
|