xref: /dokuwiki/lib/plugins/logviewer/admin.php (revision 428df3066fac74a7f2359dc27c889c65e87e4db3)
170cc2cbfSAndreas Gohr<?php
270cc2cbfSAndreas Gohr
370cc2cbfSAndreas Gohruse dokuwiki\Logger;
470cc2cbfSAndreas Gohr
570cc2cbfSAndreas Gohr/**
670cc2cbfSAndreas Gohr * DokuWiki Plugin logviewer (Admin Component)
770cc2cbfSAndreas Gohr *
870cc2cbfSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
970cc2cbfSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
1070cc2cbfSAndreas Gohr */
1170cc2cbfSAndreas Gohrclass admin_plugin_logviewer extends DokuWiki_Admin_Plugin
1270cc2cbfSAndreas Gohr{
1370cc2cbfSAndreas Gohr
1470cc2cbfSAndreas Gohr    protected $facilities;
1570cc2cbfSAndreas Gohr    protected $facility;
1670cc2cbfSAndreas Gohr    protected $date;
1770cc2cbfSAndreas Gohr
1870cc2cbfSAndreas Gohr    /** @inheritDoc */
1970cc2cbfSAndreas Gohr    public function forAdminOnly()
2070cc2cbfSAndreas Gohr    {
2170cc2cbfSAndreas Gohr        return true;
2270cc2cbfSAndreas Gohr    }
2370cc2cbfSAndreas Gohr
2470cc2cbfSAndreas Gohr    /** @inheritDoc */
2570cc2cbfSAndreas Gohr    public function handle()
2670cc2cbfSAndreas Gohr    {
2770cc2cbfSAndreas Gohr        global $INPUT;
2870cc2cbfSAndreas Gohr
2970cc2cbfSAndreas Gohr        $this->facilities = $this->getFacilities();
3070cc2cbfSAndreas Gohr        $this->facility = $INPUT->str('facility');
3170cc2cbfSAndreas Gohr        if (!in_array($this->facility, $this->facilities)) {
3270cc2cbfSAndreas Gohr            $this->facility = $this->facilities[0];
3370cc2cbfSAndreas Gohr        }
3470cc2cbfSAndreas Gohr
3570cc2cbfSAndreas Gohr        $this->date = $INPUT->str('date');
3670cc2cbfSAndreas Gohr        if (!preg_match('/^\d\d\d\d-\d\d-\d\d$/', $this->date)) {
3770cc2cbfSAndreas Gohr            $this->date = gmdate('Y-m-d');
3870cc2cbfSAndreas Gohr        }
3970cc2cbfSAndreas Gohr    }
4070cc2cbfSAndreas Gohr
4170cc2cbfSAndreas Gohr    /** @inheritDoc */
4270cc2cbfSAndreas Gohr    public function html()
4370cc2cbfSAndreas Gohr    {
4470cc2cbfSAndreas Gohr        echo '<div id="plugin__logviewer">';
4570cc2cbfSAndreas Gohr        echo $this->locale_xhtml('intro');
4670cc2cbfSAndreas Gohr        $this->displayTabs();
4770cc2cbfSAndreas Gohr        $this->displayLog();
4870cc2cbfSAndreas Gohr        echo '</div>';
4970cc2cbfSAndreas Gohr    }
5070cc2cbfSAndreas Gohr
5170cc2cbfSAndreas Gohr    /**
5270cc2cbfSAndreas Gohr     * Show the navigational tabs and date picker
5370cc2cbfSAndreas Gohr     */
5470cc2cbfSAndreas Gohr    protected function displayTabs()
5570cc2cbfSAndreas Gohr    {
5670cc2cbfSAndreas Gohr        global $ID;
5770cc2cbfSAndreas Gohr
5870cc2cbfSAndreas Gohr        $form = new dokuwiki\Form\Form(['method' => 'GET']);
5970cc2cbfSAndreas Gohr        $form->setHiddenField('do', 'admin');
6070cc2cbfSAndreas Gohr        $form->setHiddenField('page', 'logviewer');
6170cc2cbfSAndreas Gohr        $form->setHiddenField('facility', $this->facility);
62bc45a28eSAndreas Gohr        $form->addTextInput('date', $this->getLang('date'))
63bc45a28eSAndreas Gohr            ->attr('type', 'date')->val($this->date)->addClass('quickselect');
6470cc2cbfSAndreas Gohr        $form->addButton('submit', '>')->attr('type', 'submit');
6570cc2cbfSAndreas Gohr        echo $form->toHTML();
6670cc2cbfSAndreas Gohr
6770cc2cbfSAndreas Gohr        echo '<ul class="tabs">';
6870cc2cbfSAndreas Gohr        foreach ($this->facilities as $facility) {
6970cc2cbfSAndreas Gohr            echo '<li>';
7070cc2cbfSAndreas Gohr            if ($facility == $this->facility) {
7170cc2cbfSAndreas Gohr                echo '<strong>' . hsc($facility) . '</strong>';
7270cc2cbfSAndreas Gohr            } else {
7370cc2cbfSAndreas Gohr                $link = wl($ID,
7470cc2cbfSAndreas Gohr                    ['do' => 'admin', 'page' => 'logviewer', 'date' => $this->date, 'facility' => $facility]);
7570cc2cbfSAndreas Gohr                echo '<a href="' . $link . '">' . hsc($facility) . '</a>';
7670cc2cbfSAndreas Gohr            }
7770cc2cbfSAndreas Gohr            echo '</li>';
7870cc2cbfSAndreas Gohr        }
7970cc2cbfSAndreas Gohr        echo '</ul>';
8070cc2cbfSAndreas Gohr
8170cc2cbfSAndreas Gohr    }
8270cc2cbfSAndreas Gohr
8370cc2cbfSAndreas Gohr    /**
8470cc2cbfSAndreas Gohr     * Output the logfile contents
8570cc2cbfSAndreas Gohr     */
8670cc2cbfSAndreas Gohr    protected function displayLog()
8770cc2cbfSAndreas Gohr    {
8870cc2cbfSAndreas Gohr        $logfile = Logger::getInstance($this->facility)->getLogfile($this->date);
8970cc2cbfSAndreas Gohr        if (!file_exists($logfile)) {
9070cc2cbfSAndreas Gohr            echo $this->locale_xhtml('nolog');
9170cc2cbfSAndreas Gohr            return;
9270cc2cbfSAndreas Gohr        }
9370cc2cbfSAndreas Gohr
9470cc2cbfSAndreas Gohr        // loop through the file an print it
9570cc2cbfSAndreas Gohr        echo '<dl>';
9670cc2cbfSAndreas Gohr        $lines = file($logfile);
9770cc2cbfSAndreas Gohr        $cnt = count($lines);
9870cc2cbfSAndreas Gohr        for ($i = 0; $i < $cnt; $i++) {
9970cc2cbfSAndreas Gohr            $line = $lines[$i];
10070cc2cbfSAndreas Gohr
101*428df306Sfiwswe            if (substr($line, 0, 2) === '  ') {
10270cc2cbfSAndreas Gohr                // lines indented by two spaces are details, aggregate them
10370cc2cbfSAndreas Gohr                echo '<dd>';
104*428df306Sfiwswe                while (substr($line, 0, 2) === '  ') {
10570cc2cbfSAndreas Gohr                    echo hsc(substr($line, 2)) . '<br />';
106586feb6eSAndreas Gohr                    $i++;
107586feb6eSAndreas Gohr                    $line = $lines[$i] ?? '';
10870cc2cbfSAndreas Gohr                }
10970cc2cbfSAndreas Gohr                echo '</dd>';
110b9a4556dSAndreas Gohr                $i -= 1; // rewind the counter
11170cc2cbfSAndreas Gohr            } else {
11270cc2cbfSAndreas Gohr                // other lines are actual log lines in three parts
113ec34bb30SAndreas Gohr                list($dt, $file, $msg) = sexplode("\t", $line, 3, '');
11470cc2cbfSAndreas Gohr                echo '<dt>';
11570cc2cbfSAndreas Gohr                echo '<span class="datetime">' . hsc($dt) . '</span>';
11670cc2cbfSAndreas Gohr                echo '<span class="log">';
11770cc2cbfSAndreas Gohr                echo '<span class="msg">' . hsc($msg) . '</span>';
11870cc2cbfSAndreas Gohr                echo '<span class="file">' . hsc($file) . '</span>';
11970cc2cbfSAndreas Gohr                echo '</span>';
12070cc2cbfSAndreas Gohr                echo '</dt>';
12170cc2cbfSAndreas Gohr            }
12270cc2cbfSAndreas Gohr        }
12370cc2cbfSAndreas Gohr        echo '</dl>';
12470cc2cbfSAndreas Gohr    }
12570cc2cbfSAndreas Gohr
12670cc2cbfSAndreas Gohr    /**
12770cc2cbfSAndreas Gohr     * Get the available logging facilities
12870cc2cbfSAndreas Gohr     *
12970cc2cbfSAndreas Gohr     * @return array
13070cc2cbfSAndreas Gohr     */
13170cc2cbfSAndreas Gohr    protected function getFacilities()
13270cc2cbfSAndreas Gohr    {
13370cc2cbfSAndreas Gohr        global $conf;
13470cc2cbfSAndreas Gohr        $conf['logdir'];
13570cc2cbfSAndreas Gohr
13670cc2cbfSAndreas Gohr        // default facilities first
13770cc2cbfSAndreas Gohr        $facilities = [
13870cc2cbfSAndreas Gohr            Logger::LOG_ERROR,
13970cc2cbfSAndreas Gohr            Logger::LOG_DEPRECATED,
14070cc2cbfSAndreas Gohr            Logger::LOG_DEBUG,
14170cc2cbfSAndreas Gohr        ];
14270cc2cbfSAndreas Gohr
14370cc2cbfSAndreas Gohr        // add all other dirs
14470cc2cbfSAndreas Gohr        $dirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR);
14570cc2cbfSAndreas Gohr        foreach ($dirs as $dir) {
14670cc2cbfSAndreas Gohr            $facilities[] = basename($dir);
14770cc2cbfSAndreas Gohr        }
14870cc2cbfSAndreas Gohr        $facilities = array_unique($facilities);
14970cc2cbfSAndreas Gohr
15070cc2cbfSAndreas Gohr        return $facilities;
15170cc2cbfSAndreas Gohr    }
15270cc2cbfSAndreas Gohr
15370cc2cbfSAndreas Gohr}
15470cc2cbfSAndreas Gohr
155