xref: /dokuwiki/lib/plugins/logviewer/admin.php (revision 70cc2cbf41ee65a6048aab5aab40e124b337e295)
1*70cc2cbfSAndreas Gohr<?php
2*70cc2cbfSAndreas Gohr
3*70cc2cbfSAndreas Gohruse dokuwiki\Logger;
4*70cc2cbfSAndreas Gohr
5*70cc2cbfSAndreas Gohr/**
6*70cc2cbfSAndreas Gohr * DokuWiki Plugin logviewer (Admin Component)
7*70cc2cbfSAndreas Gohr *
8*70cc2cbfSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9*70cc2cbfSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
10*70cc2cbfSAndreas Gohr */
11*70cc2cbfSAndreas Gohrclass admin_plugin_logviewer extends DokuWiki_Admin_Plugin
12*70cc2cbfSAndreas Gohr{
13*70cc2cbfSAndreas Gohr
14*70cc2cbfSAndreas Gohr    protected $facilities;
15*70cc2cbfSAndreas Gohr    protected $facility;
16*70cc2cbfSAndreas Gohr    protected $date;
17*70cc2cbfSAndreas Gohr
18*70cc2cbfSAndreas Gohr    /** @inheritDoc */
19*70cc2cbfSAndreas Gohr    public function forAdminOnly()
20*70cc2cbfSAndreas Gohr    {
21*70cc2cbfSAndreas Gohr        return true;
22*70cc2cbfSAndreas Gohr    }
23*70cc2cbfSAndreas Gohr
24*70cc2cbfSAndreas Gohr    /** @inheritDoc */
25*70cc2cbfSAndreas Gohr    public function handle()
26*70cc2cbfSAndreas Gohr    {
27*70cc2cbfSAndreas Gohr        global $INPUT;
28*70cc2cbfSAndreas Gohr
29*70cc2cbfSAndreas Gohr        $this->facilities = $this->getFacilities();
30*70cc2cbfSAndreas Gohr        $this->facility = $INPUT->str('facility');
31*70cc2cbfSAndreas Gohr        if (!in_array($this->facility, $this->facilities)) {
32*70cc2cbfSAndreas Gohr            $this->facility = $this->facilities[0];
33*70cc2cbfSAndreas Gohr        }
34*70cc2cbfSAndreas Gohr
35*70cc2cbfSAndreas Gohr        $this->date = $INPUT->str('date');
36*70cc2cbfSAndreas Gohr        if (!preg_match('/^\d\d\d\d-\d\d-\d\d$/', $this->date)) {
37*70cc2cbfSAndreas Gohr            $this->date = gmdate('Y-m-d');
38*70cc2cbfSAndreas Gohr        }
39*70cc2cbfSAndreas Gohr    }
40*70cc2cbfSAndreas Gohr
41*70cc2cbfSAndreas Gohr    /** @inheritDoc */
42*70cc2cbfSAndreas Gohr    public function html()
43*70cc2cbfSAndreas Gohr    {
44*70cc2cbfSAndreas Gohr        echo '<div id="plugin__logviewer">';
45*70cc2cbfSAndreas Gohr        echo $this->locale_xhtml('intro');
46*70cc2cbfSAndreas Gohr        $this->displayTabs();
47*70cc2cbfSAndreas Gohr        $this->displayLog();
48*70cc2cbfSAndreas Gohr        echo '</div>';
49*70cc2cbfSAndreas Gohr    }
50*70cc2cbfSAndreas Gohr
51*70cc2cbfSAndreas Gohr    /**
52*70cc2cbfSAndreas Gohr     * Show the navigational tabs and date picker
53*70cc2cbfSAndreas Gohr     */
54*70cc2cbfSAndreas Gohr    protected function displayTabs()
55*70cc2cbfSAndreas Gohr    {
56*70cc2cbfSAndreas Gohr        global $ID;
57*70cc2cbfSAndreas Gohr
58*70cc2cbfSAndreas Gohr        $form = new dokuwiki\Form\Form(['method'=>'GET']);
59*70cc2cbfSAndreas Gohr        $form->setHiddenField('do', 'admin');
60*70cc2cbfSAndreas Gohr        $form->setHiddenField('page', 'logviewer');
61*70cc2cbfSAndreas Gohr        $form->setHiddenField('facility', $this->facility);
62*70cc2cbfSAndreas Gohr        $form->addTextInput('date','Date*')->attr('type','date')->val($this->date)->addClass('quickselect');
63*70cc2cbfSAndreas Gohr        $form->addButton('submit','>')->attr('type','submit');
64*70cc2cbfSAndreas Gohr        echo $form->toHTML();
65*70cc2cbfSAndreas Gohr
66*70cc2cbfSAndreas Gohr        echo '<ul class="tabs">';
67*70cc2cbfSAndreas Gohr        foreach ($this->facilities as $facility) {
68*70cc2cbfSAndreas Gohr            echo '<li>';
69*70cc2cbfSAndreas Gohr            if ($facility == $this->facility) {
70*70cc2cbfSAndreas Gohr                echo '<strong>' . hsc($facility) . '</strong>';
71*70cc2cbfSAndreas Gohr            } else {
72*70cc2cbfSAndreas Gohr                $link = wl($ID,
73*70cc2cbfSAndreas Gohr                    ['do' => 'admin', 'page' => 'logviewer', 'date' => $this->date, 'facility' => $facility]);
74*70cc2cbfSAndreas Gohr                echo '<a href="' . $link . '">' . hsc($facility) . '</a>';
75*70cc2cbfSAndreas Gohr            }
76*70cc2cbfSAndreas Gohr            echo '</li>';
77*70cc2cbfSAndreas Gohr        }
78*70cc2cbfSAndreas Gohr        echo '</ul>';
79*70cc2cbfSAndreas Gohr
80*70cc2cbfSAndreas Gohr    }
81*70cc2cbfSAndreas Gohr
82*70cc2cbfSAndreas Gohr    /**
83*70cc2cbfSAndreas Gohr     * Output the logfile contents
84*70cc2cbfSAndreas Gohr     */
85*70cc2cbfSAndreas Gohr    protected function displayLog()
86*70cc2cbfSAndreas Gohr    {
87*70cc2cbfSAndreas Gohr        $logfile = Logger::getInstance($this->facility)->getLogfile($this->date);
88*70cc2cbfSAndreas Gohr        if (!file_exists($logfile)) {
89*70cc2cbfSAndreas Gohr            echo $this->locale_xhtml('nolog');
90*70cc2cbfSAndreas Gohr            return;
91*70cc2cbfSAndreas Gohr        }
92*70cc2cbfSAndreas Gohr
93*70cc2cbfSAndreas Gohr        // loop through the file an print it
94*70cc2cbfSAndreas Gohr        echo '<dl>';
95*70cc2cbfSAndreas Gohr        $lines = file($logfile);
96*70cc2cbfSAndreas Gohr        $cnt = count($lines);
97*70cc2cbfSAndreas Gohr        for ($i = 0; $i < $cnt; $i++) {
98*70cc2cbfSAndreas Gohr            $line = $lines[$i];
99*70cc2cbfSAndreas Gohr
100*70cc2cbfSAndreas Gohr            if ($line[0] === ' ' && $line[1] === ' ') {
101*70cc2cbfSAndreas Gohr                // lines indented by two spaces are details, aggregate them
102*70cc2cbfSAndreas Gohr                echo '<dd>';
103*70cc2cbfSAndreas Gohr                while ($line[0] === ' ' && $line[1] === ' ') {
104*70cc2cbfSAndreas Gohr                    echo hsc(substr($line, 2)) . '<br />';
105*70cc2cbfSAndreas Gohr                    $line = $lines[$i++];
106*70cc2cbfSAndreas Gohr                }
107*70cc2cbfSAndreas Gohr                echo '</dd>';
108*70cc2cbfSAndreas Gohr                $i -= 2; // rewind the counter
109*70cc2cbfSAndreas Gohr            } else {
110*70cc2cbfSAndreas Gohr                // other lines are actual log lines in three parts
111*70cc2cbfSAndreas Gohr                list($dt, $file, $msg) = explode("\t", $line, 3);
112*70cc2cbfSAndreas Gohr                echo '<dt>';
113*70cc2cbfSAndreas Gohr                echo '<span class="datetime">' . hsc($dt) . '</span>';
114*70cc2cbfSAndreas Gohr                echo '<span class="log">';
115*70cc2cbfSAndreas Gohr                echo '<span class="msg">' . hsc($msg) . '</span>';
116*70cc2cbfSAndreas Gohr                echo '<span class="file">' . hsc($file) . '</span>';
117*70cc2cbfSAndreas Gohr                echo '</span>';
118*70cc2cbfSAndreas Gohr                echo '</dt>';
119*70cc2cbfSAndreas Gohr            }
120*70cc2cbfSAndreas Gohr        }
121*70cc2cbfSAndreas Gohr        echo '</dl>';
122*70cc2cbfSAndreas Gohr    }
123*70cc2cbfSAndreas Gohr
124*70cc2cbfSAndreas Gohr    /**
125*70cc2cbfSAndreas Gohr     * Get the available logging facilities
126*70cc2cbfSAndreas Gohr     *
127*70cc2cbfSAndreas Gohr     * @return array
128*70cc2cbfSAndreas Gohr     */
129*70cc2cbfSAndreas Gohr    protected function getFacilities()
130*70cc2cbfSAndreas Gohr    {
131*70cc2cbfSAndreas Gohr        global $conf;
132*70cc2cbfSAndreas Gohr        $conf['logdir'];
133*70cc2cbfSAndreas Gohr
134*70cc2cbfSAndreas Gohr        // default facilities first
135*70cc2cbfSAndreas Gohr        $facilities = [
136*70cc2cbfSAndreas Gohr            Logger::LOG_ERROR,
137*70cc2cbfSAndreas Gohr            Logger::LOG_DEPRECATED,
138*70cc2cbfSAndreas Gohr            Logger::LOG_DEBUG,
139*70cc2cbfSAndreas Gohr        ];
140*70cc2cbfSAndreas Gohr
141*70cc2cbfSAndreas Gohr        // add all other dirs
142*70cc2cbfSAndreas Gohr        $dirs = glob($conf['logdir'] . '/*', GLOB_ONLYDIR);
143*70cc2cbfSAndreas Gohr        foreach ($dirs as $dir) {
144*70cc2cbfSAndreas Gohr            $facilities[] = basename($dir);
145*70cc2cbfSAndreas Gohr        }
146*70cc2cbfSAndreas Gohr        $facilities = array_unique($facilities);
147*70cc2cbfSAndreas Gohr
148*70cc2cbfSAndreas Gohr        return $facilities;
149*70cc2cbfSAndreas Gohr    }
150*70cc2cbfSAndreas Gohr
151*70cc2cbfSAndreas Gohr}
152*70cc2cbfSAndreas Gohr
153