(Grav GitSync) Automatic Commit from smokephil

This commit is contained in:
smokephil 2024-03-22 22:42:22 +01:00 committed by GitSync
commit 4267db646d
2765 changed files with 462171 additions and 0 deletions

View file

@ -0,0 +1,61 @@
<?php
namespace Grav\Plugin\Problems;
use Grav\Plugin\Problems\Base\Problem;
/**
* Class Apache
* @package Grav\Plugin\Problems
*/
class Apache extends Problem
{
public function __construct()
{
$this->id = 'Apache Modules';
$this->class = get_class($this);
$this->order = 1;
$this->level = Problem::LEVEL_CRITICAL;
$this->status = true;
$this->help = 'https://learn.getgrav.org/basics/requirements#apache-requirements';
}
/**
* @return $this
*/
public function process()
{
// Perform some Apache checks
if (function_exists('apache_get_modules') && strpos(PHP_SAPI, 'apache') !== false) {
$require_apache_modules = ['mod_rewrite'];
$apache_modules = apache_get_modules();
$apache_errors = [];
$apache_success = [];
foreach ($require_apache_modules as $module) {
if (in_array($module, $apache_modules, true)) {
$apache_success[$module] = 'module required but not enabled';
} else {
$apache_errors[$module] = 'module is not installed or enabled';
}
}
if (empty($apache_errors)) {
$this->status = true;
$this->msg = 'All modules look good!';
} else {
$this->status = false;
$this->msg = 'There were problems with required modules:';
}
$this->details = ['errors' => $apache_errors, 'success' => $apache_success];
} else {
$this->msg = 'Apache not installed, skipping...';
}
return $this;
}
}

View file

@ -0,0 +1,141 @@
<?php
namespace Grav\Plugin\Problems\Base;
use JsonSerializable;
/**
* Class Problem
* @package Grav\Plugin\Problems\Base
*/
class Problem implements JsonSerializable
{
const LEVEL_CRITICAL = 'critical';
const LEVEL_WARNING = 'warning';
const LEVEL_NOTICE = 'notice';
/** @var string */
protected $id = '';
/** @var int */
protected $order = 0;
/** @var string */
protected $level = '';
/** @var bool */
protected $status = false;
/** @var string */
protected $msg = '';
/** @var array */
protected $details = [];
/** @var string */
protected $help = '';
/** @var string */
protected $class = '';
/**
* @param array $data
* @return void
*/
public function load(array $data): void
{
$this->set_object_vars($data);
}
/**
* @return $this
*/
public function process()
{
return $this;
}
/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
/**
* @return int
*/
public function getOrder(): int
{
return $this->order;
}
/**
* @return string
*/
public function getLevel(): string
{
return $this->level;
}
/**
* @return bool
*/
public function getStatus(): bool
{
return $this->status;
}
/**
* @return string
*/
public function getMsg(): string
{
return $this->msg;
}
/**
* @return array
*/
public function getDetails(): array
{
return $this->details;
}
/**
* @return string
*/
public function getHelp(): string
{
return $this->help;
}
/**
* @return string
*/
public function getClass(): string
{
return $this->class;
}
/**
* @return array
*/
public function toArray(): array
{
return get_object_vars($this);
}
/**
* @return array
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* @param array $vars
*/
protected function set_object_vars(array $vars): void
{
$has = get_object_vars($this);
foreach ($has as $name => $oldValue) {
$this->{$name} = $vars[$name] ?? null;
}
}
}

View file

@ -0,0 +1,159 @@
<?php
namespace Grav\Plugin\Problems\Base;
use Grav\Common\Cache;
use Grav\Common\Grav;
use RocketTheme\Toolbox\Event\Event;
/**
* Class ProblemChecker
* @package Grav\Plugin\Problems\Base
*/
class ProblemChecker
{
/** @var string */
const PROBLEMS_PREFIX = 'problem-check-';
/** @var array */
protected $problems = [];
/** @var string */
protected $status_file;
public function __construct()
{
/** @var Cache $cache */
$cache = Grav::instance()['cache'];
$this->status_file = CACHE_DIR . $this::PROBLEMS_PREFIX . $cache->getKey() . '.json';
}
/**
* @return bool
*/
public function load(): bool
{
if ($this->statusFileExists()) {
$json = file_get_contents($this->status_file) ?: '';
$data = json_decode($json, true);
if (!is_array($data)) {
return false;
}
foreach ($data as $problem) {
$class = $problem['class'];
$this->problems[] = new $class($problem);
}
}
return true;
}
/**
* @return string
*/
public function getStatusFile():string
{
return $this->status_file;
}
/**
* @return bool
*/
public function statusFileExists(): bool
{
return file_exists($this->status_file);
}
/**
* @return void
*/
public function storeStatusFile(): void
{
$problems = $this->getProblemsSerializable();
$json = json_encode($problems);
file_put_contents($this->status_file, $json);
}
/**
* @param string|null $problems_dir
* @return bool
*/
public function check($problems_dir = null): bool
{
$problems_dir = $problems_dir ?: dirname(__DIR__);
$problems = [];
$problems_found = false;
$iterator = new \DirectoryIterator($problems_dir);
foreach ($iterator as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}
$classname = 'Grav\\Plugin\\Problems\\' . $file->getBasename('.php');
if (class_exists($classname)) {
/** @var Problem $problem */
$problem = new $classname();
$problems[$problem->getId()] = $problem;
}
}
// Fire event to allow other plugins to add problems
Grav::instance()->fireEvent('onProblemsInitialized', new Event(['problems' => $problems]));
// Get the problems in order
usort($problems, function($a, $b) {
/** @var Problem $a */
/** @var Problem $b */
return $b->getOrder() - $a->getOrder();
});
// run the process methods in new order
foreach ($problems as $problem) {
$problem->process();
if ($problem->getStatus() === false && $problem->getLevel() === Problem::LEVEL_CRITICAL) {
$problems_found = true;
}
}
$this->problems = $problems;
return $problems_found;
}
/**
* @return array
*/
public function getProblems(): array
{
if (empty($this->problems)) {
$this->check();
}
$problems = $this->problems;
// Put the failed ones first
usort($problems, function($a, $b) {
/** @var Problem $a */
/** @var Problem $b */
return $a->getStatus() - $b->getStatus();
});
return $problems;
}
/**
* @return array
*/
public function getProblemsSerializable(): array
{
if (empty($this->problems)) {
$this->getProblems();
}
$problems = [];
foreach ($this->problems as $problem) {
$problems[] = $problem->toArray();
}
return $problems;
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Grav\Plugin\Problems;
use Grav\Plugin\Problems\Base\Problem;
/**
* Class EssentialFolders
* @package Grav\Plugin\Problems
*/
class EssentialFolders extends Problem
{
public function __construct()
{
$this->id = 'Essential Folders';
$this->class = get_class($this);
$this->order = 100;
$this->level = Problem::LEVEL_CRITICAL;
$this->status = false;
$this->help = 'https://learn.getgrav.org/basics/folder-structure';
}
/**
* @return $this
*/
public function process()
{
$essential_folders = [
GRAV_ROOT => false,
GRAV_ROOT . '/vendor' => false,
GRAV_SYSTEM_PATH => false,
GRAV_CACHE_PATH => true,
GRAV_LOG_PATH => true,
GRAV_TMP_PATH => true,
GRAV_BACKUP_PATH => true,
GRAV_WEBROOT => false,
GRAV_WEBROOT . '/images' => true,
GRAV_WEBROOT . '/assets' => true,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/accounts' => true,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/data' => true,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/pages' => false,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/config' => false,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/plugins/error' => false,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/plugins' => false,
GRAV_WEBROOT . '/' . GRAV_USER_PATH .'/themes' => false,
];
// Check for essential files & perms
$file_errors = [];
$file_success = [];
foreach ($essential_folders as $file => $check_writable) {
$file_path = (!preg_match('`^(/|[a-z]:[\\\/])`ui', $file) ? GRAV_ROOT . '/' : '') . $file;
if (!is_dir($file_path)) {
$file_errors[$file_path] = 'does not exist';
} elseif (!$check_writable) {
$file_success[$file_path] = 'exists';
} elseif (!is_writable($file_path)) {
$file_errors[$file_path] = 'exists but is <strong>not writeable</strong>';
} else {
$file_success[$file_path] = 'exists and is writable';
}
}
if (empty($file_errors)) {
$this->status = true;
$this->msg = 'All folders look good!';
} else {
$this->status = false;
$this->msg = 'There were problems with required folders:';
}
$this->details = ['errors' => $file_errors, 'success' => $file_success];
return $this;
}
}

View file

@ -0,0 +1,148 @@
<?php
namespace Grav\Plugin\Problems;
use Grav\Common\Grav;
use Grav\Plugin\Problems\Base\Problem;
/**
* Class PHPModules
* @package Grav\Plugin\Problems
*/
class PHPModules extends Problem
{
public function __construct()
{
$this->id = 'PHP Modules';
$this->class = get_class($this);
$this->order = 101;
$this->level = Problem::LEVEL_CRITICAL;
$this->status = false;
$this->help = 'https://learn.getgrav.org/basics/requirements#php-requirements';
}
/**
* @return $this
*/
public function process()
{
$modules_errors = [];
$modules_success = [];
// Check for PHP CURL library
$msg = 'PHP Curl (Data Transfer Library) is %s installed';
if (function_exists('curl_version')) {
$modules_success['curl'] = sprintf($msg, 'successfully');
} else {
$modules_errors['curl'] = sprintf($msg, 'required but not');
}
// Check for PHP Ctype library
$msg = 'PHP Ctype is %s installed';
if (function_exists('ctype_print')) {
$modules_success['ctype'] = sprintf($msg, 'successfully');
} else {
$modules_errors['ctype'] = sprintf($msg, 'required but not');
}
// Check for PHP Dom library
$msg = 'PHP DOM is %s installed';
if (class_exists('DOMDocument')) {
$modules_success['dom'] = sprintf($msg, 'successfully');
} else {
$modules_errors['dom'] = sprintf($msg, 'required but not');
}
// Check for GD library
$msg = 'PHP GD (Image Manipulation Library) is %s installed';
if (defined('GD_VERSION') && function_exists('gd_info')) {
$msg = $modules_success['gd'] = sprintf($msg, 'successfully');
// Extra checks for Image support
$ginfo = gd_info();
$gda = array('PNG Support', 'JPEG Support', 'FreeType Support', 'GIF Read Support');
$gda_msg = '';
$problems_found = false;
foreach ($gda as $image_type) {
if (!$ginfo[$image_type]) {
$problems_found = true;
$gda_msg = "missing $image_type, but is ";
break;
}
}
if ($problems_found) {
$msg .= ' but missing ' . $gda_msg;
}
$modules_success['gd'] = $msg;
} else {
$modules_errors['gd'] = sprintf($msg, 'required but not');
}
// Check for PHP MbString library
$msg = 'PHP Mbstring (Multibyte String Library) is %s installed';
if (extension_loaded('mbstring')) {
$modules_success['mbstring'] = sprintf($msg, 'successfully');
} else {
$modules_errors['mbstring'] = sprintf($msg, 'required but not');
}
// Check for PHP Open SSL library
$msg = 'PHP OpenSSL (Secure Sockets Library) is %s installed';
if (defined('OPENSSL_VERSION_TEXT') && extension_loaded('openssl')) {
$modules_success['openssl'] = sprintf($msg, 'successfully');
} else {
$modules_errors['openssl'] = sprintf($msg, 'required but not');
}
// Check for PHP XML library
$msg = 'PHP JSON Library is %s installed';
if (extension_loaded('json')) {
$modules_success['json'] = sprintf($msg, 'successfully');
} else {
$modules_errors['json'] = sprintf($msg, 'required but not');
}
// Check for PHP XML library
$msg = 'PHP XML Library is %s installed';
if (extension_loaded('xml')) {
$modules_success['xml'] = sprintf($msg, 'successfully');
} else {
$modules_errors['xml'] = sprintf($msg, 'required but not');
}
// Check for PHP Zip library
$msg = 'PHP Zip extension is %s installed';
if (extension_loaded('zip')) {
$modules_success['zip'] = sprintf($msg, 'successfully');
} else {
$modules_errors['zip'] = sprintf($msg, 'required but not');
}
// Check Exif if enabled
if (Grav::instance()['config']->get('system.media.auto_metadata_exif')) {
$msg = 'PHP Exif (Exchangeable Image File Format) is %s installed';
if (extension_loaded('exif')) {
$modules_success['exif'] = sprintf($msg, 'successfully');
} else {
$modules_errors['exif'] = sprintf($msg, 'required but not');
}
}
if (empty($modules_errors)) {
$this->status = true;
$this->msg = 'All modules look good!';
} else {
$this->status = false;
$this->msg = 'There were problems with required modules:';
}
$this->details = ['errors' => $modules_errors, 'success' => $modules_success];
return $this;
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace Grav\Plugin\Problems;
use Grav\Plugin\Problems\Base\Problem;
/**
* Class PHPVersion
* @package Grav\Plugin\Problems
*/
class PHPVersion extends Problem
{
public function __construct()
{
$this->id = 'PHP Minimum Version';
$this->class = get_class($this);
$this->order = 102;
$this->level = Problem::LEVEL_CRITICAL;
$this->status = false;
$this->help = 'https://getgrav.org/blog/raising-php-requirements-2018';
}
/**
* @return $this
*/
public function process()
{
$min_php_version = defined('GRAV_PHP_MIN') ? GRAV_PHP_MIN : '5.6.4';
$your_php_version = PHP_VERSION;
$msg = 'Your PHP <strong>%s</strong> is %s than the minimum of <strong>%s</strong> required';
// Check PHP version
if (version_compare($your_php_version, $min_php_version, '<')) {
$this->msg = sprintf($msg, $your_php_version, 'less', $min_php_version);
} else {
$this->msg = sprintf($msg, $your_php_version, 'greater', $min_php_version);
$this->status = true;
}
return $this;
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace Grav\Plugin\Problems;
use Grav\Plugin\Problems\Base\Problem;
/**
* Class Permissions
* @package Grav\Plugin\Problems
*/
class Permissions extends Problem
{
public function __construct()
{
$this->id = 'Permissions Setup';
$this->class = get_class($this);
$this->order = -1;
$this->level = Problem::LEVEL_WARNING;
$this->status = false;
$this->help = 'https://learn.getgrav.org/troubleshooting/permissions';
}
/**
* @return $this
*/
public function process()
{
umask($umask = umask(022));
$msg = 'Your default file umask is <strong>%s</strong> which %s';
if (($umask & 2) !== 2) {
$this->msg = sprintf($msg, decoct($umask), 'is potentially dangerous');
$this->status = false;
} else {
$this->msg = sprintf($msg, decoct($umask), 'looks good!');
$this->status = true;
}
return $this;
}
}