xref: /plugin/botmon/admin.php (revision eba0eb773833fa5cc1bcef21e83484b7a3ec1ebe)
1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4
5/**
6 * Bot Monitoring Plugin
7*
8* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9* @author     Sascha Leib <ad@hominem.info>
10*/
11
12/**
13 * All DokuWiki plugins to extend the admin function
14 * need to inherit from this class
15**/
16class admin_plugin_botmon extends AdminPlugin {
17
18	/**
19	 * Return the path to the icon being displayed in the main admin menu.
20	 *
21	 * @return string full path to the icon file
22	**/
23	public function getMenuIcon() {
24		$plugin = $this->getPluginName();
25		return DOKU_PLUGIN . $plugin . '/img/admin.svg';
26	}
27
28	/**
29	 * output appropriate html
30	*/
31	public function html() {
32
33		global $conf;
34
35		// display GeoIP data?
36		$geoIPconf = $this->getConf('geoiplib');
37		$useCaptchaConf = ($this->getConf('useCaptcha') !== 'disabled');
38
39		$hasOldLogFiles = $this->hasOldLogFiles();
40
41		// spinner animation as SVG image:
42		$svg = '<svg width="12" height="12" viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg" id="botmon__today__busy"><defs><linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="100%"><stop offset="0%" stop-color="#666"></stop><stop offset="100%" stop-color="#666"></stop></linearGradient></defs><circle cx="25" cy="25" r="20" fill="none" stroke="url(#gradient)" stroke-width="8" stroke-dasharray="31.4 31.4"><animateTransform attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="1s" repeatCount="indefinite"></animateTransform></circle></svg>';
43
44		$pluginPath = $conf['basedir'] . 'lib/plugins/' . $this->getPluginName();
45
46		/* Plugin Headline */
47		echo NL . '<div id="botmon__admin">
48	<h1>Bot Monitoring Plugin</h1>
49	<nav id="botmon__tabs">
50		<ul class="tabs" role="tablist">
51			<li role="presentation"><a role="tab" href="#botmon__latest" aria-controls="botmon__latest" id="botmon__tab1" aria-selected="true">Latest</a></li>
52			<li role="presentation"><a role="tab" href="#botmon__log" aria-controls="botmon__log" id="botmon__tab3" aria-selected="false">Log</a></li>
53		</ul>
54	</nav>
55	<article role="tabpanel" id="botmon__latest">
56		<h2 class="a11y">Latest data</h2>
57		<header id="botmon__today__title">Loading&nbsp;&hellip;</header>
58		<div id="botmon__today__content">
59			<details id="botmon__today__overview" open>
60				<summary>Bots overview</summary>
61				<div class="botmon_bots_grid" data-geoip="' . $geoIPconf  . '">
62					<dl id="botmon__today__botsvshumans"></dl>
63					<dl id="botmon__botslist"></dl>
64					<dl id="botmon__botips"></dl>
65					<dl id="botmon__botcountries"></dl>
66				</div>
67			</details>
68			<details id="botmon__today__webmetrics">
69				<summary>Humans overview</summary>
70				<div class="botmon_webmetrics_grid" data-geoip="' . $geoIPconf  . '">
71					<dl id="botmon__today__wm_overview"></dl>
72					<dl id="botmon__today__wm_clients"></dl>
73					<dl id="botmon__today__wm_platforms"></dl>
74					<dl id="botmon__today__wm_countries"></dl>
75				</div>
76			</details>
77			<details id="botmon__today__traffic">
78				<summary>Web traffic (humans only)</summary>
79				<div class="botmon_traffic_grid">
80					<dl id="botmon__today__wm_pages"></dl>
81					<dl id="botmon__today__wm_referers"></dl>
82				</div>
83			</details>' . NL;
84		if ($useCaptchaConf) {
85			echo '			<details id="botmon__today__captcha">
86				<summary>Captcha statistics</summary>
87				<div class="botmon_captcha_grid">
88					<dl id="botmon__today__cp_humans"></dl>
89					<dl id="botmon__today__cp_sus"></dl>
90					<dl id="botmon__today__cp_bots"></dl>
91				</div>
92			</details>' . NL;
93		}
94		echo '			<details id="botmon__today__visitors">
95				<summary>Visitor logs</summary>
96				<div id="botmon__today__visitorlists"></div>
97			</details>
98		</div>
99		<footer aria-live="polite">
100			<span>' . $svg . '</span>
101			<span id="botmon__today__status">Initialising&nbsp;&hellip;</span>
102		</footer>
103	</article>
104	<article role="tabpanel" id="botmon__log" hidden>
105		<h2 class="a11y">Process log</h2>
106		<ul id="botmon__loglist">' . NL;
107
108		/* proces old logs */
109		if ($hasOldLogFiles) {
110
111			$helper = $this->loadHelper('botmon', true);
112
113			$helper->cleanup();
114		} else {
115			echo DOKU_TAB . DOKU_TAB . DOKU_TAB . '<li>No files to process.</li>' . NL;
116		}
117
118		echo DOKU_TAB . DOKU_TAB . '</ul>' . NL . DOKU_TAB . '</article>' . NL;
119		echo DOKU_TAB . '<script>
120		const BMSettings = {
121			showday: ' . json_encode($this->getConf('showday')) . ',
122			combineNets: ' . json_encode($this->getConf('combineNets')) . ',
123			useCaptcha: ' . json_encode($this->getConf('useCaptcha') !== 'disabled') . '
124		};
125	</script>' . NL;
126		echo '</div><!-- End of BotMon Admin Tool -->';
127
128	}
129
130	/**
131	 * Check if there are old log files to be handled
132	 *
133	 * @return bool true if there are old log files, false otherwise
134	 */
135	private function hasOldLogFiles() {
136
137		$today = gmdate('Y-m-d');
138		$yesterday = gmdate('Y-m-d', time() - 86400);
139
140		// scan the log directory and delete all files except for today and yesterday:
141		$dir = scandir(getcwd() . '/lib/plugins/botmon/logs');
142		foreach($dir as $file) {
143			$fName = pathinfo($file, PATHINFO_BASENAME);
144			$bName = strtok($fName, '.');
145
146			if ($bName == '' || $bName == 'logfiles' || $bName == 'empty' || $fName == '.htaccess') {
147				// ignore
148			} else if ($bName == $today || $bName == $yesterday) {
149				// skip
150			} else {
151				return true;
152			}
153		}
154		return false;
155	}
156}