xref: /plugin/botmon/admin.php (revision e4ddd94610d8b35125580693f9a5729724a60f3b)
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 '<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">';
96
97		/* proces old logs */
98		if ($hasOldLogFiles) {
99
100			$helper = $this->loadHelper('botmon', true);
101
102			$helper->cleanup();
103		} else {
104			echo '<li>No files to process.</li>';
105		}
106
107		echo '</article></div><!-- End of BotMon Admin Tool -->';
108
109	}
110
111	/**
112	 * Check if there are old log files to be handled
113	 *
114	 * @return bool true if there are old log files, false otherwise
115	 */
116	private function hasOldLogFiles() {
117
118		$today = gmdate('Y-m-d');
119		$yesterday = gmdate('Y-m-d', time() - 86400);
120
121		// scan the log directory and delete all files except for today and yesterday:
122		$dir = scandir(getcwd() . '/lib/plugins/botmon/logs');
123		foreach($dir as $file) {
124			$fName = pathinfo($file, PATHINFO_BASENAME);
125			$bName = strtok($fName, '.');
126
127			if ($bName == '' || $bName == 'logfiles' || $bName == 'empty' || $fName == '.htaccess') {
128				// ignore
129			} else if ($bName == $today || $bName == $yesterday) {
130				// skip
131			} else {
132				return true;
133			}
134		}
135		return false;
136	}
137}