From 8c4d6c99822527d34abae6ab60d2f616b988f04b Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Wed, 6 Aug 2014 12:46:49 -0600 Subject: [PATCH] Updates for problem checker plugin --- index.php | 4 + user/plugins/problems/html/problems.html | 34 +++++ user/plugins/problems/problems.css | 4 + user/plugins/problems/problems.php | 132 ++++++++---------- .../problems/templates/problems.html.twig | 55 -------- 5 files changed, 102 insertions(+), 127 deletions(-) create mode 100644 user/plugins/problems/html/problems.html delete mode 100644 user/plugins/problems/templates/problems.html.twig diff --git a/index.php b/index.php index 5c046cefe..df77bbadc 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,10 @@ PHP %s to run.', $ver, $req)); +} + use Tracy\Debugger; // Register system libraries to the auto-loader. diff --git a/user/plugins/problems/html/problems.html b/user/plugins/problems/html/problems.html new file mode 100644 index 000000000..9c90474fe --- /dev/null +++ b/user/plugins/problems/html/problems.html @@ -0,0 +1,34 @@ + + + + + Grav Problems + + + + + + + + + +
+
+

Issues Found

+

Please Review and Resolve before continuing...

+ +

+ Reload Page +

+ +
    + %%PROBLEMS%% +
+
+
+ + diff --git a/user/plugins/problems/problems.css b/user/plugins/problems/problems.css index 96bd66437..dc4b655f9 100644 --- a/user/plugins/problems/problems.css +++ b/user/plugins/problems/problems.css @@ -1,3 +1,7 @@ +section#body { + padding-top: 3rem; +} + ul.problems { list-style: none; padding: 0; diff --git a/user/plugins/problems/problems.php b/user/plugins/problems/problems.php index 5dc6b8018..2964e4180 100644 --- a/user/plugins/problems/problems.php +++ b/user/plugins/problems/problems.php @@ -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 "
{$filename} {$text}
"; + if ($key == 'files') { + foreach ($result as $filename => $file_result) { + foreach ($file_result as $status => $text) { + $problems .= $this->getListRow($status, '' . $filename . ' ' . $text); } - } else { - if (key($status)) { - continue; - } - $text = reset($status); - echo "
{$text}
"; + } + } 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 .= '
  • '. $text . '

  • '; + 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 => '' . ROOT_DIR . ' is '. $root_adjective . 'writable']; + // Check for essential files & perms $file_problems = []; foreach($essential_files as $file => $check_writable) { diff --git a/user/plugins/problems/templates/problems.html.twig b/user/plugins/problems/templates/problems.html.twig deleted file mode 100644 index 3d16dc24b..000000000 --- a/user/plugins/problems/templates/problems.html.twig +++ /dev/null @@ -1,55 +0,0 @@ -{% extends 'partials/base.html.twig' %} - -{% block content %} - {{ page.content }} - -

    - Reload Page -

    - -