xref: /plugin/botmon/admin.php (revision e56d7b71abb5d1a6e19cb8d0b2e4e35a95ea2380)
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		// spinner animation as SVG image:
39		$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>';
40
41		$pluginPath = $conf['basedir'] . 'lib/plugins/' . $this->getPluginName();
42
43		/* Plugin Headline */
44		echo '<div id="botmon__admin">
45	<h1>Bot Monitoring Plugin</h1>
46	<nav id="botmon__tabs">
47		<ul class="tabs" role="tablist">
48			<li role="presentation" class="active"><a role="tab" href="#botmon__panel1" aria-controls="botmon__panel1" id="botmon__tab1" aria-selected="true">Latest</a></li>
49		</ul>
50	</nav>';
51
52	if ($this->hasOldLogFiles()) {
53		echo '<div class="info"><strong>Note:</strong> There are old log files that can be deleted. <a href="' . $pluginPath . '/cleanup.php" target="_blank">Click here</a> to run a delete script, or use <em>cron</em> to automatically delete them.</div>';
54	}
55
56	echo '<article role="tabpanel" id="botmon__today"">
57		<h2 class="a11y">Latest data</h2>
58		<header id="botmon__today__title">Loading&nbsp;&hellip;</header>
59		<div id="botmon__today__content">
60			<details id="botmon__today__overview" open>
61				<summary>Overview</summary>
62				<div class="botmon_bots_grid" data-geoip="' . $geoIPconf  . '">
63					<dl id="botmon__today__botsvshumans"></dl>
64					<dl id="botmon__botslist"></dl>
65					<dl id="botmon__botips"></dl>
66					<dl id="botmon__botcountries"></dl>
67				</div>
68			</details>
69			<details id="botmon__today__webmetrics">
70				<summary>Humans overview</summary>
71				<div class="botmon_webmetrics_grid" data-geoip="' . $geoIPconf  . '">
72					<dl id="botmon__today__wm_overview"></dl>
73					<dl id="botmon__today__wm_clients"></dl>
74					<dl id="botmon__today__wm_platforms"></dl>
75					<dl id="botmon__today__wm_countries"></dl>
76				</div>
77			</details>
78			<details id="botmon__today__traffic">
79				<summary>Web traffic (humans only)</summary>
80				<div class="botmon_traffic_grid">
81					<dl id="botmon__today__wm_pages"></dl>
82					<dl id="botmon__today__wm_referers"></dl>
83				</div>
84			</details>
85			<details id="botmon__today__visitors">
86				<summary>Visitor logs</summary>
87				<div id="botmon__today__visitorlists"></div>
88			</details>
89		</div>
90		<footer aria-live="polite">
91			<span>' . $svg . '</span>
92			<span id="botmon__today__status">Initialising&nbsp;&hellip;</span>
93		</footer>
94	</article>
95</div><!-- End of BotMon Admin Tool -->';
96
97	}
98
99	/**
100	 * Check if there are old log files that can be deleted.
101	 *
102	 * @return bool true if there are old log files, false otherwise
103	 */
104	private function hasOldLogFiles() {
105
106		$today = gmdate('Y-m-d');
107		$yesterday = gmdate('Y-m-d', time() - 86400);
108
109		// scan the log directory and delete all files except for today and yesterday:
110		$dir = scandir(getcwd() . '/lib/plugins/botmon/logs');
111		foreach($dir as $file) {
112			$fName = pathinfo($file, PATHINFO_BASENAME);
113			$bName = strtok($fName, '.');
114
115			if ($bName == '' || $bName == 'logfiles' || $bName == 'empty' || $fName == '.htaccess') {
116				// ignore
117			} else if ($bName == $today || $bName == $yesterday) {
118				// skip
119			} else {
120				return true;
121			}
122		}
123		return false;
124	}
125}