1<?php
2/**
3 * DokuWiki Plugin log404 (Admin Component)
4 *
5 * @license GPL 3 http://www.gnu.org/licenses/gpl-3.0.html
6 * @author  Sam Wilson <sam@samwilson.id.au>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12/**
13 * The admin class handles the display and user-manipulation of the 404 log.
14 */
15class admin_plugin_log404 extends DokuWiki_Admin_Plugin {
16
17    public function handle() {
18        global $ID;
19        if (isset($_GET['delete'])) {
20            $log = $this->loadHelper('log404');
21            $log->deleteRecord($_GET['delete']);
22            msg(sprintf($this->getLang('deleted'), $_GET['delete']));
23            send_redirect(wl($ID, array('do'=>'admin', 'page'=>$this->getPluginName()), true, '&'));
24        }
25    }
26
27    public function html() {
28        ptln('<h1>'.$this->getLang('menu').'</h1>');
29        $log = $this->loadHelper('log404');
30        $log->load();
31        ptln('<ol class='.$this->getPluginName().'>');
32        foreach ($log->getRecords() as $id => $detail) {
33            ptln($this->getHtml($id, $detail));
34        }
35        ptln('</ol>');
36    }
37
38    protected function getHtml($id, $data) {
39        global $ID;
40        $delUrl = wl($ID, array('do'=>'admin', 'page'=>$this->getPluginName(), 'delete'=>$id));
41        $ignoreUrl = wl($ID, array('do'=>'admin', 'page'=>$this->getPluginName(), 'ignore'=>$id));
42        $title = '<strong class="title">'.$data['count'].' <code>'.$id.'</code></strong> '
43               . ' <a href="'.wl($id).'">'.$this->getLang('go-to-page').'</a>'
44               . ' <a href="'.$delUrl.'">'.sprintf($this->getLang('delete'), $data['count']).'</a>'
45               . ' <a href="'.$ignoreUrl.'">'.$this->getLang('ignore').'</a>'
46               . '</span>';
47        $out = $title.'<ol>';
48        foreach ($data['hits'] as $hit) {
49            $line = $hit['date'];
50            if (!empty($hit['ip'])) {
51                $line .= ' <em>'.$this->getLang('ip').'</em> '.$hit['ip'];
52            }
53            if (!empty($hit['referer'])) {
54                $line .= ' <em>'.$this->getLang('referer').'</em> <a href="'.$hit['referer'].'">'.$hit['referer'].'</a>';
55            }
56            if (!empty($hit['user_agent'])) {
57                $line .= ' <em>'.$this->getLang('user-agent').'</em> '.$hit['user_agent'];
58            }
59            // The line should never actually be empty, but still...
60            if (!empty($line)) {
61                $out .= "<li>$line</li>";
62            }
63        }
64        $out .= '</ol>';
65        return "<li>$out</li>";
66    }
67
68}
69