xref: /plugin/botmon/admin.php (revision 6b6cd387da613838df43ec1dc8194b7d3a057341)
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
38		$hasOldLogFiles = $this->hasOldLogFiles();
39
40		// spinner animation as SVG image:
41		$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>';
42
43		$pluginPath = $conf['basedir'] . 'lib/plugins/' . $this->getPluginName();
44
45		/* Plugin Headline */
46		echo NL . '<div id="botmon__admin">
47	<h1>Bot Monitoring Plugin</h1>
48	<nav id="botmon__tabs">
49		<ul class="tabs" role="tablist">
50			<li role="presentation"><a role="tab" href="#botmon__latest" aria-controls="botmon__latest" id="botmon__tab1" aria-selected="true">Latest</a></li>
51			<li role="presentation"><a role="tab" href="#botmon__log" aria-controls="botmon__log" id="botmon__tab3" aria-selected="false">Log</a></li>
52		</ul>
53	</nav>
54	<article role="tabpanel" id="botmon__latest">
55		<h2 class="a11y">Latest data</h2>
56		<header id="botmon__today__title">Loading&nbsp;&hellip;</header>
57		<div id="botmon__today__content">
58			<details id="botmon__today__overview" open>
59				<summary>Overview</summary>
60				<div class="botmon_bots_grid" data-geoip="' . $geoIPconf  . '">
61					<dl id="botmon__today__botsvshumans"></dl>
62					<dl id="botmon__botslist"></dl>
63					<dl id="botmon__botips"></dl>
64					<dl id="botmon__botcountries"></dl>
65				</div>
66			</details>
67			<details id="botmon__today__webmetrics">
68				<summary>Humans overview</summary>
69				<div class="botmon_webmetrics_grid" data-geoip="' . $geoIPconf  . '">
70					<dl id="botmon__today__wm_overview"></dl>
71					<dl id="botmon__today__wm_clients"></dl>
72					<dl id="botmon__today__wm_platforms"></dl>
73					<dl id="botmon__today__wm_countries"></dl>
74				</div>
75			</details>
76			<details id="botmon__today__traffic">
77				<summary>Web traffic (humans only)</summary>
78				<div class="botmon_traffic_grid">
79					<dl id="botmon__today__wm_pages"></dl>
80					<dl id="botmon__today__wm_referers"></dl>
81				</div>
82			</details>
83			<details id="botmon__today__visitors">
84				<summary>Visitor logs</summary>
85				<div id="botmon__today__visitorlists"></div>
86			</details>
87		</div>
88		<footer aria-live="polite">
89			<span>' . $svg . '</span>
90			<span id="botmon__today__status">Initialising&nbsp;&hellip;</span>
91		</footer>
92	</article>
93	<article role="tabpanel" id="botmon__log" hidden>
94		<h2 class="a11y">Process log</h2>
95		<ul id="botmon__loglist">' . NL;
96
97		/* proces old logs */
98		if ($hasOldLogFiles) {
99
100			$helper = $this->loadHelper('botmon', true);
101
102			$helper->cleanup();
103		} else {
104			echo DOKU_TAB . DOKU_TAB . DOKU_TAB . '<li>No files to process.</li>' . NL;
105		}
106
107		echo DOKU_TAB . DOKU_TAB . '</ul>' . NL . DOKU_TAB . '</article>' . NL;
108		echo DOKU_TAB . '<script>
109		const BMSettings = {
110			showday: ' . json_encode($this->getConf('showday')) . ',
111			combineNets: ' . json_encode($this->getConf('combineNets')) . ',
112			useCaptcha: ' . json_encode($this->getConf('useCaptcha') !== 'disabled') . '
113		};
114	</script>' . NL;
115		echo '</div><!-- End of BotMon Admin Tool -->';
116
117	}
118
119	/**
120	 * Check if there are old log files to be handled
121	 *
122	 * @return bool true if there are old log files, false otherwise
123	 */
124	private function hasOldLogFiles() {
125
126		$today = gmdate('Y-m-d');
127		$yesterday = gmdate('Y-m-d', time() - 86400);
128
129		// scan the log directory and delete all files except for today and yesterday:
130		$dir = scandir(getcwd() . '/lib/plugins/botmon/logs');
131		foreach($dir as $file) {
132			$fName = pathinfo($file, PATHINFO_BASENAME);
133			$bName = strtok($fName, '.');
134
135			if ($bName == '' || $bName == 'logfiles' || $bName == 'empty' || $fName == '.htaccess') {
136				// ignore
137			} else if ($bName == $today || $bName == $yesterday) {
138				// skip
139			} else {
140				return true;
141			}
142		}
143		return false;
144	}
145}