1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\ChangeLog\PageChangeLog;
5
6/**
7 * All DokuWiki plugins to extend the admin function
8 * need to inherit from this class
9 */
10class admin_plugin_revert extends AdminPlugin
11{
12    protected $cmd;
13    // some vars which might need tuning later
14    protected $max_lines = 800; // lines to read from changelog
15    protected $max_revs  = 20;  // numer of old revisions to check
16
17
18    /**
19     * Constructor
20     */
21    public function __construct()
22    {
23        $this->setupLocale();
24    }
25
26    /**
27     * access for managers
28     */
29    public function forAdminOnly()
30    {
31        return false;
32    }
33
34    /**
35     * return sort order for position in admin menu
36     */
37    public function getMenuSort()
38    {
39        return 40;
40    }
41
42    /**
43     * handle user request
44     */
45    public function handle()
46    {
47    }
48
49    /**
50     * output appropriate html
51     */
52    public function html()
53    {
54        global $INPUT;
55
56        echo $this->locale_xhtml('intro');
57
58        $this->printSearchForm();
59
60        if (is_array($INPUT->param('revert')) && checkSecurityToken()) {
61            $this->revertEdits($INPUT->arr('revert'), $INPUT->str('filter'));
62        } elseif ($INPUT->has('filter')) {
63            $this->listEdits($INPUT->str('filter'));
64        }
65    }
66
67    /**
68     * Display the form for searching spam pages
69     */
70    protected function printSearchForm()
71    {
72        global $lang, $INPUT;
73        echo '<form action="" method="post"><div class="no">';
74        echo '<label>' . $this->getLang('filter') . ': </label>';
75        echo '<input type="text" name="filter" class="edit" value="' . hsc($INPUT->str('filter')) . '" /> ';
76        echo '<button type="submit">' . $lang['btn_search'] . '</button> ';
77        echo '<span>' . $this->getLang('note1') . '</span>';
78        echo '</div></form><br /><br />';
79    }
80
81    /**
82     * Start the reversion process
83     */
84    protected function revertEdits($revert, $filter)
85    {
86        echo '<hr /><br />';
87        echo '<p>' . $this->getLang('revstart') . '</p>';
88
89        echo '<ul>';
90        foreach ($revert as $id) {
91            global $REV;
92
93            // find the last non-spammy revision
94            $data = '';
95            $pagelog = new PageChangeLog($id);
96            $old  = $pagelog->getRevisions(0, $this->max_revs);
97            if ($old !== []) {
98                foreach ($old as $REV) {
99                    $data = rawWiki($id, $REV);
100                    if (strpos($data, (string) $filter) === false) break;
101                }
102            }
103
104            if ($data) {
105                saveWikiText($id, $data, 'old revision restored', false);
106                printf('<li><div class="li">' . $this->getLang('reverted') . '</div></li>', $id, $REV);
107            } else {
108                saveWikiText($id, '', '', false);
109                printf('<li><div class="li">' . $this->getLang('removed') . '</div></li>', $id);
110            }
111            @set_time_limit(10);
112            flush();
113        }
114        echo '</ul>';
115
116        echo '<p>' . $this->getLang('revstop') . '</p>';
117    }
118
119    /**
120     * List recent edits matching the given filter
121     */
122    protected function listEdits($filter)
123    {
124        global $conf;
125        global $lang;
126        echo '<hr /><br />';
127        echo '<form action="" method="post"><div class="no">';
128        echo '<input type="hidden" name="filter" value="' . hsc($filter) . '" />';
129        formSecurityToken();
130
131        $recents = getRecents(0, $this->max_lines);
132        echo '<ul>';
133
134        $cnt = 0;
135        foreach ($recents as $recent) {
136            if ($filter) {
137                if (strpos(rawWiki($recent['id']), (string) $filter) === false) continue;
138            }
139
140            $cnt++;
141            $date = dformat($recent['date']);
142
143            echo ($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
144            echo '<div class="li">';
145            echo '<input type="checkbox" name="revert[]" value="' . hsc($recent['id']) .
146                '" checked="checked" id="revert__' . $cnt . '" />';
147            echo ' <label for="revert__' . $cnt . '">' . $date . '</label> ';
148
149            echo '<a href="' . wl($recent['id'], "do=diff") . '">';
150            $p = [];
151            $p['src']    = DOKU_BASE . 'lib/images/diff.png';
152            $p['width']  = 15;
153            $p['height'] = 11;
154            $p['title']  = $lang['diff'];
155            $p['alt']    = $lang['diff'];
156            $att = buildAttributes($p);
157            echo "<img $att />";
158            echo '</a> ';
159
160            echo '<a href="' . wl($recent['id'], "do=revisions") . '">';
161            $p = [];
162            $p['src']    = DOKU_BASE . 'lib/images/history.png';
163            $p['width']  = 12;
164            $p['height'] = 14;
165            $p['title']  = $lang['btn_revs'];
166            $p['alt']    = $lang['btn_revs'];
167            $att = buildAttributes($p);
168            echo "<img $att />";
169            echo '</a> ';
170
171            echo html_wikilink(':' . $recent['id'], (useHeading('navigation')) ? null : $recent['id']);
172            echo ' – ' . htmlspecialchars($recent['sum']);
173
174            echo ' <span class="user">';
175                echo $recent['user'] . ' ' . $recent['ip'];
176            echo '</span>';
177
178            echo '</div>';
179            echo '</li>';
180
181            @set_time_limit(10);
182            flush();
183        }
184        echo '</ul>';
185
186        echo '<p>';
187        echo '<button type="submit">' . $this->getLang('revert') . '</button> ';
188        printf($this->getLang('note2'), hsc($filter));
189        echo '</p>';
190
191        echo '</div></form>';
192    }
193}
194//Setup VIM: ex: et ts=4 :
195