1<?php
2/**
3 * Access Counter and Popularity Plugin -- Data Manager
4 * Easy way to view and manage the log
5 *
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author  HokkaidoPerson <dosankomali@yahoo.co.jp>
8 */
9
10
11// must be run within Dokuwiki
12if(!defined('DOKU_INC')) die();
13
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15require_once(DOKU_PLUGIN.'admin.php');
16
17/**
18 * All DokuWiki plugins to extend the admin function
19 * need to inherit from this class
20 */
21class admin_plugin_accscounter extends DokuWiki_Admin_Plugin {
22
23    /**
24     * access for managers
25     */
26    function forAdminOnly(){
27        return false;
28    }
29
30    /**
31     * return sort order for position in admin menu
32     */
33    function getMenuSort() {
34        return 100;
35    }
36
37    /**
38     * handle user request
39     */
40    function handle() {
41    }
42
43    /**
44     * output appropriate html
45     */
46    function html() {
47        global $lang;
48        global $conf;
49        $achelper = plugin_load('helper','accscounter');
50
51        echo $this->locale_xhtml('heading');
52
53        if ($_REQUEST['function'] == 'sfscheck' and !plugin_isdisabled('stopforumspam2')) {
54            if ($helper = plugin_load('helper','stopforumspam2')) {
55                foreach ($_REQUEST['select'] as $page) {
56                    if (file_exists($achelper->counterFN($page, '.ip'))) {
57                        $ipdata = @file($achelper->counterFN($page, '.ip'));
58                        if ($ipdata != FALSE) {
59                            $newcontents = array();
60                            foreach ($ipdata as $dataline) {
61                                $element = explode('|', $dataline);
62                                $element[0] = trim($element[0]);
63                                $element[1] = trim($element[1]);
64                                if ($helper->quickipcheck($element[0], $this->getConf('sfsExFreq'), $this->getConf('sfsExConf'))) {
65                                    $counterarray = @file($achelper->counterFN($page, '.number'), FILE_IGNORE_NEW_LINES);
66                                    $counterarray[0] = $counterarray[0] - $element[1];
67                                    $counterarray[2] = $counterarray[2] - $element[1];
68                                    if ($counterarray[0] < 0) $counterarray[0] = 0;
69                                    if ($counterarray[2] < 0) $counterarray[2] = 0;
70                                    $writing = '';
71                                    foreach ($counterarray as $part) {
72                                        $writing .= $part . "\n";
73                                    }
74                                    io_saveFile($achelper->counterFN($page, '.number'), $writing);
75                                    msg(sprintf($this->getLang('sfstried'), $page, $element[0], $element[1]));
76                                } else $newcontents[] = $element[0] . '|' . $element[1];
77                            }
78                            $writing = '';
79                            foreach ($newcontents as $part) {
80                                $writing .= $part . "\n";
81                            }
82                            io_saveFile($achelper->counterFN($page, '.ip'), $writing);
83                            msg($this->getLang('sfsfinish'), 1);
84                        }
85                    }
86                }
87            }
88        }
89
90        if ($_REQUEST['function'] == 'delete') {
91            foreach ($_REQUEST['select'] as $page) {
92                if (file_exists($achelper->counterFN($page, '.number')) and @unlink($achelper->counterFN($page, '.number')) == FALSE) msg(sprintf($this->getLang('mngfaileddel'), $page), -1);
93                if (file_exists($achelper->counterFN($page, '.ip')) and @unlink($achelper->counterFN($page, '.ip')) == FALSE) msg(sprintf($this->getLang('mngfaileddel'), $page), -1);
94            }
95            msg($this->getLang('mngdelfinish'), 1);
96        }
97
98        if ($_REQUEST['action'] == 'mng'){
99            $found = array();
100            $metadir = $this->getFiles($conf['metadir'] . '/_accscounter');
101            $length = utf8_strlen($conf['metadir'] . '/_accscounter/');
102            foreach ($metadir as $filename) {
103                $islogfile = utf8_substr($filename, $length);
104                $islogfile = utf8_decodeFN($islogfile);
105                $islogfile = str_replace('/', ':', $islogfile);
106                if (utf8_substr($islogfile, mb_strrpos($islogfile, '.') + 1) != 'number') continue;
107                $page = utf8_substr($islogfile, 0, utf8_strlen($islogfile) - utf8_strlen('.number'));
108                $specified = FALSE;
109                if ($_REQUEST['mode'] == 'existing' and page_exists($page)) $specified = TRUE;
110                if ($_REQUEST['mode'] == 'deleted' and !page_exists($page)) $specified = TRUE;
111                if ($_REQUEST['mode'] == 'all') $specified = TRUE;
112                if ($_REQUEST['mode'] == 'search' and strpos($page, cleanID($_REQUEST['keyword'])) !== FALSE) $specified = TRUE;
113                if ($specified) $found[$page] = $filename;
114            }
115
116            if ($found == array()) {
117                echo $this->locale_xhtml('noitem');
118                echo $this->locale_xhtml('mngintro');
119                echo '<form method="get" action="">';
120                echo '<input type="hidden" name="do" value="admin">';
121                echo '<input type="hidden" name="page" value="accscounter">';
122                echo '<input type="hidden" name="action" value="mng">';
123                echo '<input type="hidden" name="mode" value="search">';
124                echo '<input type="text" name="keyword" size="40"> ';
125                echo '<input type="submit" value="' . $lang['btn_search'] . '"class="button" />';
126                echo '</form>';
127                return;
128            }
129            echo $this->locale_xhtml('viewer');
130            echo '<form name="form" method="post" action="">';
131            echo '<table class="table">';
132            echo '<tr><th><label><input type="checkbox" name="all" onClick="AllChecked();" />' . $this->getLang('selectall') . '</label></th><th>' . $this->getLang('pagename') . '</th><th>' . $this->getLang('sofar') . '</th><th>' . $this->getLang('lastdate') . '</th><th>' . $this->getLang('today') . '</th><th>' . $this->getLang('yest') . '</th><th>' . $this->getLang('ipadd') . '</th></tr>';
133            foreach ($found as $page => $filename) {
134                $array = @file($filename);
135                if ($array === FALSE) continue;
136                echo '<tr><td><input type="checkbox" name="select[]" value="' . $page . '" onClick="DisChecked();" /></td>';
137                echo '<td>' . p_render('xhtml', p_get_instructions('[[:' . $page . ']]'), $info) . '</td>';
138                echo '<td>' . $array[0] . '</td>';
139                echo '<td>' . $array[1] . '</td>';
140                echo '<td>' . $array[2] . '</td>';
141                echo '<td>' . $array[3] . '</td>';
142                echo '<td>' . $array[4] . '</td>';
143                echo '</tr>';
144            }
145            echo '</table>';
146            echo '<select name="function">';
147            echo '<option value="none" selected>' . $this->getLang('pleasechoose') . '</option>';
148            echo '<option value="sfscheck">' . $this->getLang('sfscheck') . '</option>';
149            echo '<option value="delete">' . $this->getLang('delete') . '</option>';
150            echo '</select><br><input type="submit" value="' . $this->getLang('run') . '"class="button" />';
151            echo '</form>';
152            echo <<<EOT
153<script language="JavaScript" type="text/javascript">
154<!--
155  function AllChecked(){
156    var all = document.form.all.checked;
157    for (var i=0; i<document.form.elements['select[]'].length; i++){
158      document.form.elements['select[]'][i].checked = all;
159    }
160  }
161
162  function DisChecked(){
163    var checks = document.form.elements['select[]'];
164    var checksCount = 0;
165    for (var i=0; i<checks.length; i++){
166      if(checks[i].checked == false){
167        document.form.all.checked = false;
168      }else{
169        checksCount += 1;
170        if(checksCount == checks.length){
171          document.form.all.checked = true;
172        }
173      }
174    }
175  }
176// -->
177</script>
178EOT;
179            return;
180        }
181
182        echo $this->locale_xhtml('mngintro');
183        echo '<form method="get" action="">';
184        echo '<input type="hidden" name="do" value="admin">';
185        echo '<input type="hidden" name="page" value="accscounter">';
186        echo '<input type="hidden" name="action" value="mng">';
187        echo '<input type="hidden" name="mode" value="search">';
188        echo '<input type="text" name="keyword" size="40"> ';
189        echo '<input type="submit" value="' . $lang['btn_search'] . '"class="button" />';
190        echo '</form>';
191
192    }
193
194    protected function getFiles($path) {
195        $result = array();
196
197        foreach(glob($path . "/*") as $file) {
198            if (is_dir($file)) {
199                $result = array_merge($result, $this->getFiles($file));
200            }
201
202            $result[] = $file;
203        }
204
205        return $result;
206    }
207
208}
209