*/
class admin_plugin_logviewer extends DokuWiki_Admin_Plugin
{
    protected $facilities;
    protected $facility;
    protected $date;
    /** @inheritDoc */
    public function forAdminOnly()
    {
        return true;
    }
    /** @inheritDoc */
    public function handle()
    {
        global $INPUT;
        $this->facilities = $this->getFacilities();
        $this->facility = $INPUT->str('facility');
        if (!in_array($this->facility, $this->facilities)) {
            $this->facility = $this->facilities[0];
        }
        $this->date = $INPUT->str('date');
        if (!preg_match('/^\d\d\d\d-\d\d-\d\d$/', $this->date)) {
            $this->date = gmdate('Y-m-d');
        }
    }
    /** @inheritDoc */
    public function html()
    {
        echo '
';
        echo $this->locale_xhtml('intro');
        $this->displayTabs();
        $this->displayLog();
        echo '
';
    }
    /**
     * Show the navigational tabs and date picker
     */
    protected function displayTabs()
    {
        global $ID;
        $form = new dokuwiki\Form\Form(['method' => 'GET']);
        $form->setHiddenField('do', 'admin');
        $form->setHiddenField('page', 'logviewer');
        $form->setHiddenField('facility', $this->facility);
        $form->addTextInput('date', $this->getLang('date'))
            ->attr('type', 'date')->val($this->date)->addClass('quickselect');
        $form->addButton('submit', '>')->attr('type', 'submit');
        echo $form->toHTML();
        echo '';
        foreach ($this->facilities as $facility) {
            echo '- ';
            if ($facility == $this->facility) {
                echo '' . hsc($facility) . '';
            } else {
                $link = wl($ID,
                    ['do' => 'admin', 'page' => 'logviewer', 'date' => $this->date, 'facility' => $facility]);
                echo '' . hsc($facility) . '';
            }
            echo '';
        }
        echo '
';
    }
    /**
     * Output the logfile contents
     */
    protected function displayLog()
    {
        $logfile = Logger::getInstance($this->facility)->getLogfile($this->date);
        if (!file_exists($logfile)) {
            echo $this->locale_xhtml('nolog');
            return;
        }
        // loop through the file an print it
        echo '';
        $lines = file($logfile);
        $cnt = count($lines);
        for ($i = 0; $i < $cnt; $i++) {
            $line = $lines[$i];
            if ($line[0] === ' ' && $line[1] === ' ') {
                // lines indented by two spaces are details, aggregate them
                echo '- ';
                while (isset($line[0]) && $line[0] === ' ' && isset($line[1]) && $line[1] === ' ') {
                    echo hsc(substr($line, 2)) . '
 ';
                    $i++;
                    $line = $lines[$i] ?? '';
                }
                echo '
';
                $i -= 1; // rewind the counter
            } else {
                // other lines are actual log lines in three parts
                list($dt, $file, $msg) = sexplode("\t", $line, 3, '');
                echo '- ';
                echo '' . hsc($dt) . '';
                echo '';
                echo '' . hsc($msg) . '';
                echo '' . hsc($file) . '';
                echo '';
                echo '';
            }
        }
        echo '
';
    }
    /**
     * Get the available logging facilities
     *
     * @return array
     */
    protected function getFacilities()
    {
        global $conf;
        $conf['logdir'];
        // default facilities first
        $facilities = [
            Logger::LOG_ERROR,
            Logger::LOG_DEPRECATED,
            Logger::LOG_DEBUG,
        ];
        // add all other dirs
        $dirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR);
        foreach ($dirs as $dir) {
            $facilities[] = basename($dir);
        }
        $facilities = array_unique($facilities);
        return $facilities;
    }
}