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