xref: /dokuwiki/lib/plugins/logviewer/admin.php (revision bc45a28e1f07fa57fb3a4a1daaf0010a433b226a)
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);
62*bc45a28eSAndreas Gohr        $form->addTextInput('date',$this->getLang('date'))
63*bc45a28eSAndreas 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
10170cc2cbfSAndreas Gohr            if ($line[0] === ' ' && $line[1] === ' ') {
10270cc2cbfSAndreas Gohr                // lines indented by two spaces are details, aggregate them
10370cc2cbfSAndreas Gohr                echo '<dd>';
10470cc2cbfSAndreas Gohr                while ($line[0] === ' ' && $line[1] === ' ') {
10570cc2cbfSAndreas Gohr                    echo hsc(substr($line, 2)) . '<br />';
10670cc2cbfSAndreas Gohr                    $line = $lines[$i++];
10770cc2cbfSAndreas Gohr                }
10870cc2cbfSAndreas Gohr                echo '</dd>';
10970cc2cbfSAndreas Gohr                $i -= 2; // rewind the counter
11070cc2cbfSAndreas Gohr            } else {
11170cc2cbfSAndreas Gohr                // other lines are actual log lines in three parts
11270cc2cbfSAndreas Gohr                list($dt, $file, $msg) = explode("\t", $line, 3);
11370cc2cbfSAndreas Gohr                echo '<dt>';
11470cc2cbfSAndreas Gohr                echo '<span class="datetime">' . hsc($dt) . '</span>';
11570cc2cbfSAndreas Gohr                echo '<span class="log">';
11670cc2cbfSAndreas Gohr                echo '<span class="msg">' . hsc($msg) . '</span>';
11770cc2cbfSAndreas Gohr                echo '<span class="file">' . hsc($file) . '</span>';
11870cc2cbfSAndreas Gohr                echo '</span>';
11970cc2cbfSAndreas Gohr                echo '</dt>';
12070cc2cbfSAndreas Gohr            }
12170cc2cbfSAndreas Gohr        }
12270cc2cbfSAndreas Gohr        echo '</dl>';
12370cc2cbfSAndreas Gohr    }
12470cc2cbfSAndreas Gohr
12570cc2cbfSAndreas Gohr    /**
12670cc2cbfSAndreas Gohr     * Get the available logging facilities
12770cc2cbfSAndreas Gohr     *
12870cc2cbfSAndreas Gohr     * @return array
12970cc2cbfSAndreas Gohr     */
13070cc2cbfSAndreas Gohr    protected function getFacilities()
13170cc2cbfSAndreas Gohr    {
13270cc2cbfSAndreas Gohr        global $conf;
13370cc2cbfSAndreas Gohr        $conf['logdir'];
13470cc2cbfSAndreas Gohr
13570cc2cbfSAndreas Gohr        // default facilities first
13670cc2cbfSAndreas Gohr        $facilities = [
13770cc2cbfSAndreas Gohr            Logger::LOG_ERROR,
13870cc2cbfSAndreas Gohr            Logger::LOG_DEPRECATED,
13970cc2cbfSAndreas Gohr            Logger::LOG_DEBUG,
14070cc2cbfSAndreas Gohr        ];
14170cc2cbfSAndreas Gohr
14270cc2cbfSAndreas Gohr        // add all other dirs
14370cc2cbfSAndreas Gohr        $dirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR);
14470cc2cbfSAndreas Gohr        foreach ($dirs as $dir) {
14570cc2cbfSAndreas Gohr            $facilities[] = basename($dir);
14670cc2cbfSAndreas Gohr        }
14770cc2cbfSAndreas Gohr        $facilities = array_unique($facilities);
14870cc2cbfSAndreas Gohr
14970cc2cbfSAndreas Gohr        return $facilities;
15070cc2cbfSAndreas Gohr    }
15170cc2cbfSAndreas Gohr
15270cc2cbfSAndreas Gohr}
15370cc2cbfSAndreas Gohr
154