<commit_before><?php
namespace Grav\Plugin;

use \Grav\Common\Cache;
use \Grav\Common\Plugin;
use \Grav\Common\Registry;
use Tracy\Debugger;

class ProblemsPlugin extends Plugin
{
    /**
     * @var bool
     */
    protected $active = false;

    protected $results = array();

    public function onFatalException($e)
    {
        // Run through potential issues
        if ($this->problemChecker()) {
            $this->renderProblems();
        }
    }

    public function onAfterInitPlugins()
    {
        $cache = Registry::get('Cache');
        $validated_prefix = 'validated-';

        $this->check = CACHE_DIR . $validated_prefix .$cache->getKey();

        if(!file_exists($this->check)) {

            // If no issues remain, save a state file in the cache
            if (!$this->problemChecker()) {
                // delete any exising validated files
                foreach (glob(CACHE_DIR . $validated_prefix. '*') as $filename) {
                     unlink($filename);
                }

                // create a file in the cache dir so it only runs on cache changes
                touch($this->check);

            } else {
                $this->renderProblems();
            }

        }


    }

    protected function renderProblems()
    {
        $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';

        $html = file_get_contents(__DIR__ . '/html/problems.html');

        $problems = '';
        foreach ($this->results as $key => $result) {

            if ($key == 'files') {
                foreach ($result as $filename => $file_result) {
                    foreach ($file_result as $status => $text) {
                        $problems .= $this->getListRow($status, '<b>' . $filename . '</b> ' . $text);
                    }
                }
            } else {
                foreach ($result as $status => $text) {
                    $problems .= $this->getListRow($status, $text);
                }
            }
        }

        $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();


    }

    protected function getListRow($status, $text)
    {
        $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()
    {
        $min_php_version = '5.4.0';
        $problems_found = false;

        $essential_files = [
            'index.php' => false,
            '.htaccess' => false,
            'cache' => true,
            'logs' => true,
            'images' => true,
            'system' => false,
            'user/data' => true,
            'user/pages' => false,
            'user/config' => false,<commit_msg>Added new asset folder to list of folders to check - requirement for new asset management<commit_after><?php
namespace Grav\Plugin;

use \Grav\Common\Cache;
use \Grav\Common\Plugin;
use \Grav\Common\Registry;
use Tracy\Debugger;

class ProblemsPlugin extends Plugin
{
    /**
     * @var bool
     */
    protected $active = false;

    protected $results = array();

    public function onFatalException($e)
    {
        // Run through potential issues
        if ($this->problemChecker()) {
            $this->renderProblems();
        }
    }

    public function onAfterInitPlugins()
    {
        $cache = Registry::get('Cache');
        $validated_prefix = 'validated-';

        $this->check = CACHE_DIR . $validated_prefix .$cache->getKey();

        if(!file_exists($this->check)) {

            // If no issues remain, save a state file in the cache
            if (!$this->problemChecker()) {
                // delete any exising validated files
                foreach (glob(CACHE_DIR . $validated_prefix. '*') as $filename) {
                     unlink($filename);
                }

                // create a file in the cache dir so it only runs on cache changes
                touch($this->check);

            } else {
                $this->renderProblems();
            }

        }


    }

    protected function renderProblems()
    {
        $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';

        $html = file_get_contents(__DIR__ . '/html/problems.html');

        $problems = '';
        foreach ($this->results as $key => $result) {

            if ($key == 'files') {
                foreach ($result as $filename => $file_result) {
                    foreach ($file_result as $status => $text) {
                        $problems .= $this->getListRow($status, '<b>' . $filename . '</b> ' . $text);
                    }
                }
            } else {
                foreach ($result as $status => $text) {
                    $problems .= $this->getListRow($status, $text);
                }
            }
        }

        $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();


    }

    protected function getListRow($status, $text)
    {
        $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()
    {
        $min_php_version = '5.4.0';
        $problems_found = false;

        $essential_files = [
            'index.php' => false,
            '.htaccess' => false,
            'cache' => true,
            'logs' => true,
            'images' => true,
            'assets' => true,
            'system' => false,
            'user/data' => true,
            'user/pages' => false,
            'user/config' => false,
{'fraction_non_alphanumeric': 0.15568319226118502, 'fraction_numerical': 0.0009068923821039903, 'mean_word_length': 1.7547876769358868, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1914989', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1865, 'n_words': 435}