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