1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4
5/**
6 * Redirect2 - DokuWiki Redirect Manager
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @author     Satoshi Sahara <sahara.satoshi@gmail.com>
10 */
11class admin_plugin_redirect2 extends AdminPlugin
12{
13    protected $LogFile;
14
15    function __construct()
16    {
17        global $conf;
18        $this->LogFile = $conf['cachedir'].'/redirection.log';
19    }
20
21    /**
22     * Access for managers allowed
23     */
24    function forAdminOnly(){ return false; }
25
26    /**
27     * return sort order for position in admin menu
28     */
29    function getMenuSort() { return 140; }
30
31    /**
32     * return prompt for admin menu
33     */
34    function getMenuText($language) { return $this->getLang('name'); }
35
36    /**
37     * handle user request
38     */
39    public function handle()
40    {
41        global $INPUT;
42        $map = plugin_load('helper', $this->getPluginName());
43
44        if ($INPUT->post->str('redirdata') && checkSecurityToken()) {
45            if (io_saveFile($map->ConfFile, cleanText($INPUT->str('redirdata')))) {
46                msg($this->getLang('saved'), 1);
47            }
48        }
49    }
50
51    /**
52     * output appropriate html
53     */
54    public function html()
55    {
56        global $lang;
57        $map = plugin_load('helper', $this->getPluginName());
58
59        echo $this->locale_xhtml('intro');
60        echo '<form action="" method="post" >';
61        echo '<input type="hidden" name="do" value="admin" />';
62        echo '<input type="hidden" name="page" value="'.$this->getPluginName().'" />';
63        echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
64        echo '<textarea class="edit" rows="15" cols="80" style="height: 300px" name="redirdata">';
65        echo formtext(io_readFile($map->ConfFile));
66        echo '</textarea><br />';
67        echo '<input type="submit" value="'.$lang['btn_save'].'" class="button" />';
68        echo '<br />';
69        echo '</form>';
70
71        if (!$this->getConf('logging')) return;
72        $logData = $this->getLogData();
73
74        echo '<br />';
75        echo $this->locale_xhtml('loginfo');
76        echo '<table class="'.$this->getPluginName().'">';
77        echo '<thead><tr>';
78        echo '<th>Count</th>';
79        echo '<th>Status</th>';
80        echo '<th>Page/Media</th>';
81        echo '<th>Redirect to | Referer</th>';
82        echo '<th>Recent redirection</th>';
83        echo '</tr></thead>';
84        echo '<tbody>';
85        foreach ($logData as $id => $data) {
86            echo '<tr>';
87            echo '<td>'.$data['count'].'</td>';
88            echo '<td>'.$data['status'].'</td>';
89            echo '<td>'.$this->html_atag($data['caller'], $id).'</td>';
90            echo '<td>'.$this->html_atag($data['caller'], $data['dest']).'</td>';
91            echo '<td>'.$data['last'].'</td>';
92            echo '</tr>';
93        }
94        echo '</tbody>';
95        echo '</table>';
96
97    }
98
99
100    /**
101     * Load redierction data from log file
102     */
103    protected function getLogData()
104    {
105        $logData = array();
106        if (!file_exists($this->LogFile)) return $logData;
107
108        $logfile = new SplFileObject($this->LogFile);
109        $logfile->setFlags(SplFileObject::READ_CSV);
110        $logfile->setCsvControl("\t"); // tsv
111
112        foreach ($logfile as $line) {
113            if ($line[0] == NULL) continue;
114            list($datetime, $caller, $status, $orig, $dest) = $line;
115            if (!isset($logData[$orig])) {
116                $logData[$orig] = array(
117                        'count'  => 1,
118                        'caller' => $caller,
119                        'status' => $status,
120                        'dest'   => $dest,
121                        'last'   => $datetime,
122                );
123            } else {
124                $logData[$orig]['count'] ++;
125                if ($datetime > $logData[$orig]['last']) {
126                    $logData[$orig]['last'] = $datetime;
127                }
128            }
129        }
130        unset($logfile);
131        uasort($logData, array($this, 'compareCounts'));
132        return $logData;
133    }
134
135    protected function compareCounts($a, $b)
136    {
137        return $b['count'] - $a['count'];
138    }
139
140    private function html_atag($caller, $id)
141    {
142        if (preg_match('@^(https?://|/)@', $id)) {
143            $linkType = 'external';
144        } elseif ($caller == 'redirectMedia') {
145            $linkType = 'media';
146        } else {
147            $linkType = 'page';
148        }
149        $format = 'xhtml';
150        switch ($linkType) {
151            case 'media':
152                $link = '{{:'.$id.'?linkonly|'.$id.'}}';
153                $html = strip_tags(p_render($format, p_get_instructions($link), $info), '<a>');
154                break;
155            case 'page':
156                $link = '[[:'.$id.'|'.$id.']]';
157                $html = strip_tags(p_render($format, p_get_instructions($link), $info), '<a>');
158                break;
159            default:
160                $html = hsc($id);
161        }
162        return $html;
163    }
164
165}
166//Setup VIM: ex: et ts=4 enc=utf-8 :