mirror of
https://github.com/getgrav/grav.git
synced 2025-10-26 07:56:07 +01:00
Updates for problem checker plugin
This commit is contained in:
committed by
Djamil Legato
parent
f439e71ad0
commit
8c4d6c9982
@@ -1,6 +1,10 @@
|
||||
<?php
|
||||
namespace Grav\Common;
|
||||
|
||||
if (version_compare($ver = PHP_VERSION, $req = '5.4.0', '<')) {
|
||||
throw new \RuntimeException(sprintf('You are running PHP %s, but Grav needs at least <strong>PHP %s</strong> to run.', $ver, $req));
|
||||
}
|
||||
|
||||
use Tracy\Debugger;
|
||||
|
||||
// Register system libraries to the auto-loader.
|
||||
|
||||
34
user/plugins/problems/html/problems.html
Normal file
34
user/plugins/problems/html/problems.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Grav Problems</title>
|
||||
<meta name="description" content="Grav is an easy to use, yet powerful, open source flat-file CMS">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="icon" type="image/png" href="%%THEME_URL%%/images/favicon.png">
|
||||
<link rel="stylesheet" href="%%THEME_URL%%/css-compiled/nucleus.css" type="text/css" />
|
||||
<link rel="stylesheet" href="%%THEME_URL%%/css-compiled/template.css" type="text/css" />
|
||||
<!--[if (gte IE 8)&(lte IE 9)]>
|
||||
<script type="text/javascript" src="%%THEME_URL%%/js/html5shiv-printshiv.min.js"></script>
|
||||
<link rel="stylesheet" href="%%THEME_URL%%/css/nucleus-ie9.css" type="text/css" />
|
||||
<link rel="stylesheet" href="%%THEME_URL%%/css/pure-0.5.0/grids-min.css" type="text/css" />
|
||||
<![endif]-->
|
||||
<link rel="stylesheet" href="%%PROBLEMS_URL%%/problems.css" type="text/css" />
|
||||
</head>
|
||||
<body id="top" class="">
|
||||
<div id="container">
|
||||
<section id="body" class="">
|
||||
<h1>Issues Found</h1>
|
||||
<h2>Please <strong>Review</strong> and <strong>Resolve</strong> before continuing...</h2>
|
||||
|
||||
<p class="center">
|
||||
<a href="%%BASE_URL%%" class="button big"><i class="fa fa-refresh"></i> Reload Page</a>
|
||||
</p>
|
||||
|
||||
<ul class="problems">
|
||||
%%PROBLEMS%%
|
||||
</ul>
|
||||
</section>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,3 +1,7 @@
|
||||
section#body {
|
||||
padding-top: 3rem;
|
||||
}
|
||||
|
||||
ul.problems {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
|
||||
@@ -15,10 +15,14 @@ class ProblemsPlugin extends Plugin
|
||||
|
||||
protected $results = array();
|
||||
|
||||
public function onFatalException($e)
|
||||
{
|
||||
// Run through potential issues
|
||||
if ($this->problemChecker()) {
|
||||
$this->renderProblems();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable sitemap only if url matches to the configuration.
|
||||
*/
|
||||
public function onAfterInitPlugins()
|
||||
{
|
||||
$cache = Registry::get('Cache');
|
||||
@@ -28,11 +32,8 @@ class ProblemsPlugin extends Plugin
|
||||
|
||||
if(!file_exists($this->check)) {
|
||||
|
||||
// Run through potential issues
|
||||
$this->active = $this->problemChecker();
|
||||
|
||||
// If no issues remain, save a state file in the cache
|
||||
if (!$this->active) {
|
||||
if (!$this->problemChecker()) {
|
||||
// delete any exising validated files
|
||||
foreach (glob(CACHE_DIR . $validated_prefix. '*') as $filename) {
|
||||
unlink($filename);
|
||||
@@ -40,6 +41,9 @@ class ProblemsPlugin extends Plugin
|
||||
|
||||
// create a file in the cache dir so it only runs on cache changes
|
||||
touch($this->check);
|
||||
|
||||
} else {
|
||||
$this->renderProblems();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -47,81 +51,48 @@ class ProblemsPlugin extends Plugin
|
||||
|
||||
}
|
||||
|
||||
public function onAfterGetPage()
|
||||
protected function renderProblems()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
$theme = 'antimatter';
|
||||
$baseUrlRelative = rtrim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), '/'); //make this dynamic
|
||||
$themeUrl = $baseUrlRelative .'/'. USER_PATH . basename(THEMES_DIR) .'/'. $theme;
|
||||
$problemsUrl = $baseUrlRelative . '/user/plugins/problems';
|
||||
|
||||
/** @var Grav $grav */
|
||||
$grav = Registry::get('Grav');
|
||||
$grav->page->content("# Issues Found\n##Please **Review** and **Resolve** before continuing...");
|
||||
$html = file_get_contents(__DIR__ . '/html/problems.html');
|
||||
|
||||
}
|
||||
$problems = '';
|
||||
foreach ($this->results as $key => $result) {
|
||||
|
||||
/**
|
||||
* Add current directory to twig lookup paths.
|
||||
*/
|
||||
public function onAfterTwigTemplatesPaths()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
Registry::get('Twig')->twig_paths[] = __DIR__ . '/templates';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function onFatalException($e)
|
||||
{
|
||||
// Run through potential issues
|
||||
if ($this->problemChecker()) {
|
||||
foreach ($this->results as $key => $result) {
|
||||
if ($key == 'files') {
|
||||
foreach ($result as $filename => $status) {
|
||||
if (key($status)) {
|
||||
continue;
|
||||
}
|
||||
$text = reset($status);
|
||||
echo "<div>{$filename} {$text}</div>";
|
||||
if ($key == 'files') {
|
||||
foreach ($result as $filename => $file_result) {
|
||||
foreach ($file_result as $status => $text) {
|
||||
$problems .= $this->getListRow($status, '<b>' . $filename . '</b> ' . $text);
|
||||
}
|
||||
} else {
|
||||
if (key($status)) {
|
||||
continue;
|
||||
}
|
||||
$text = reset($status);
|
||||
echo "<div>{$text}</div>";
|
||||
}
|
||||
} else {
|
||||
foreach ($result as $status => $text) {
|
||||
$problems .= $this->getListRow($status, $text);
|
||||
}
|
||||
}
|
||||
|
||||
// Create Required Folders if they don't exist
|
||||
if (!is_dir(LOG_DIR)) mkdir(LOG_DIR);
|
||||
if (!is_dir(CACHE_DIR)) mkdir(CACHE_DIR);
|
||||
if (!is_dir(IMAGES_DIR)) mkdir(IMAGES_DIR);
|
||||
if (!is_dir(DATA_DIR)) mkdir(DATA_DIR);
|
||||
|
||||
exit();
|
||||
}
|
||||
|
||||
$html = str_replace('%%BASE_URL%%', $baseUrlRelative, $html);
|
||||
$html = str_replace('%%THEME_URL%%', $themeUrl, $html);
|
||||
$html = str_replace('%%PROBLEMS_URL%%', $problemsUrl, $html);
|
||||
$html = str_replace('%%PROBLEMS%%', $problems, $html);
|
||||
|
||||
echo $html;
|
||||
|
||||
exit();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set needed variables to display the problems.
|
||||
*/
|
||||
public function onAfterSiteTwigVars()
|
||||
protected function getListRow($status, $text)
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
$twig = Registry::get('Twig');
|
||||
$twig->template = 'problems.html.twig';
|
||||
$twig->twig_vars['results'] = $this->results;
|
||||
|
||||
if ($this->config->get('plugins.problems.built_in_css')) {
|
||||
$twig->twig_vars['stylesheets'][] = 'user/plugins/problems/problems.css';
|
||||
}
|
||||
$output = "\n";
|
||||
$output .= '<li class="' . ($status ? 'success' : 'error') . ' clearfix"><i class="fa fa-' . ($status ? 'check' : 'times') . '"></i><p>'. $text . '</p></li>';
|
||||
return $output;
|
||||
}
|
||||
|
||||
protected function problemChecker()
|
||||
@@ -129,7 +100,6 @@ class ProblemsPlugin extends Plugin
|
||||
$min_php_version = '5.4.0';
|
||||
$problems_found = false;
|
||||
|
||||
|
||||
$essential_files = [
|
||||
'index.php' => false,
|
||||
'.htaccess' => false,
|
||||
@@ -169,6 +139,24 @@ class ProblemsPlugin extends Plugin
|
||||
}
|
||||
$this->results['gd'] = [$gd_status => 'PHP GD (Image Manipulation Library) is '. $gd_adjective . 'installed'];
|
||||
|
||||
|
||||
|
||||
// Check if Root is writeable and essential folders Exist
|
||||
if (is_writable(ROOT_DIR)) {
|
||||
// Create Required Folders if they don't exist
|
||||
if (!is_dir(LOG_DIR)) mkdir(LOG_DIR);
|
||||
if (!is_dir(CACHE_DIR)) mkdir(CACHE_DIR);
|
||||
if (!is_dir(IMAGES_DIR)) mkdir(IMAGES_DIR);
|
||||
if (!is_dir(DATA_DIR)) mkdir(DATA_DIR);
|
||||
$root_status = true;
|
||||
$root_adjective = '';
|
||||
} else {
|
||||
$problems_found = true;
|
||||
$root_status = false;
|
||||
$root_adjective = 'not ';
|
||||
}
|
||||
$this->results['root'] = [$root_status => '<b>' . ROOT_DIR . '</b> is '. $root_adjective . 'writable'];
|
||||
|
||||
// Check for essential files & perms
|
||||
$file_problems = [];
|
||||
foreach($essential_files as $file => $check_writable) {
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
{% extends 'partials/base.html.twig' %}
|
||||
|
||||
{% block content %}
|
||||
{{ page.content }}
|
||||
|
||||
<p class="center">
|
||||
<a href="{{ base_url_relative }}" class="button big"><i class="fa fa-refresh"></i> Reload Page</a>
|
||||
</p>
|
||||
|
||||
<ul class="problems">
|
||||
{% for key, result in results %}
|
||||
|
||||
{% if key == 'files' %}
|
||||
|
||||
{% for file, file_result in result %}
|
||||
|
||||
{% for status, description in file_result %}
|
||||
{% if status == 1 %}
|
||||
<li class="success clearfix">
|
||||
<i class="fa fa-check"></i>
|
||||
{% else %}
|
||||
<li class="error clearfix">
|
||||
<i class="fa fa-times"></i>
|
||||
{% endif %}
|
||||
<p>
|
||||
<b>{{ file }}</b> {{ description }}
|
||||
</p>
|
||||
</li>
|
||||
{% endfor %}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
{% else %}
|
||||
|
||||
{% for status, description in result %}
|
||||
{% if status == 1 %}
|
||||
<li class="success clearfix">
|
||||
<i class="fa fa-check"></i>
|
||||
{% else %}
|
||||
<li class="error clearfix">
|
||||
<i class="fa fa-times"></i>
|
||||
{% endif %}
|
||||
<p>
|
||||
{{ description }}
|
||||
</p>
|
||||
</li>
|
||||
{% endfor %}
|
||||
|
||||
{% endif %}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user