Files
dokuwiki-plugin-botmon/admin.php

143 lines
4.5 KiB
PHP
Raw Normal View History

2025-08-29 23:14:10 +03:00
<?php
use dokuwiki\Extension\AdminPlugin;
/**
2025-09-01 16:25:25 +02:00
* Bot Monitoring Plugin
2025-08-29 23:14:10 +03:00
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sascha Leib <ad@hominem.info>
*/
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
**/
2025-09-01 16:25:25 +02:00
class admin_plugin_botmon extends AdminPlugin {
2025-08-29 23:14:10 +03:00
/**
* Return the path to the icon being displayed in the main admin menu.
*
* @return string full path to the icon file
**/
public function getMenuIcon() {
$plugin = $this->getPluginName();
return DOKU_PLUGIN . $plugin . '/img/admin.svg';
}
/**
* output appropriate html
*/
public function html() {
2025-09-01 18:55:56 +02:00
global $conf;
2025-10-13 20:00:12 +02:00
// display GeoIP data?
$geoIPconf = $this->getConf('geoiplib');
2025-10-15 20:12:42 +02:00
$hasOldLogFiles = $this->hasOldLogFiles();
// spinner animation as SVG image:
$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>';
2025-09-01 18:55:56 +02:00
$pluginPath = $conf['basedir'] . 'lib/plugins/' . $this->getPluginName();
2025-08-29 23:14:10 +03:00
/* Plugin Headline */
2025-09-10 00:02:42 +02:00
echo '<div id="botmon__admin">
<h1>Bot Monitoring Plugin</h1>
<nav id="botmon__tabs">
<ul class="tabs" role="tablist">
2025-10-15 22:28:37 +02:00
<li role="presentation"><a role="tab" href="#botmon__latest" aria-controls="botmon__latest" id="botmon__tab1" aria-selected="true">Latest</a></li>
<li role="presentation"><a role="tab" href="#botmon__log" aria-controls="botmon__log" id="botmon__tab3" aria-selected="false">Log</a></li>
2025-09-10 00:02:42 +02:00
</ul>
2025-10-15 20:12:42 +02:00
</nav>
<article role="tabpanel" id="botmon__latest">
2025-10-03 21:30:29 +02:00
<h2 class="a11y">Latest data</h2>
2025-09-10 00:02:42 +02:00
<header id="botmon__today__title">Loading&nbsp;&hellip;</header>
<div id="botmon__today__content">
<details id="botmon__today__overview" open>
2025-10-14 20:58:33 +02:00
<summary>Overview</summary>
2025-10-13 20:00:12 +02:00
<div class="botmon_bots_grid" data-geoip="' . $geoIPconf . '">
2025-09-10 00:02:42 +02:00
<dl id="botmon__today__botsvshumans"></dl>
<dl id="botmon__botslist"></dl>
2025-10-14 20:58:33 +02:00
<dl id="botmon__botips"></dl>
2025-10-13 20:00:12 +02:00
<dl id="botmon__botcountries"></dl>
2025-09-10 00:02:42 +02:00
</div>
</details>
<details id="botmon__today__webmetrics">
<summary>Humans overview</summary>
2025-10-13 20:00:12 +02:00
<div class="botmon_webmetrics_grid" data-geoip="' . $geoIPconf . '">
2025-09-10 00:02:42 +02:00
<dl id="botmon__today__wm_overview"></dl>
2025-09-10 23:07:51 +02:00
<dl id="botmon__today__wm_clients"></dl>
<dl id="botmon__today__wm_platforms"></dl>
2025-10-13 20:00:12 +02:00
<dl id="botmon__today__wm_countries"></dl>
2025-10-03 21:30:29 +02:00
</div>
</details>
<details id="botmon__today__traffic">
<summary>Web traffic (humans only)</summary>
<div class="botmon_traffic_grid">
<dl id="botmon__today__wm_pages"></dl>
2025-09-15 22:58:08 +02:00
<dl id="botmon__today__wm_referers"></dl>
2025-09-10 00:02:42 +02:00
</div>
</details>
<details id="botmon__today__visitors">
<summary>Visitor logs</summary>
<div id="botmon__today__visitorlists"></div>
</details>
</div>
<footer aria-live="polite">
<span>' . $svg . '</span>
2025-09-10 00:02:42 +02:00
<span id="botmon__today__status">Initialising&nbsp;&hellip;</span>
</footer>
</article>
2025-10-15 22:28:37 +02:00
<article role="tabpanel" id="botmon__log" hidden>
<h2 class="a11y">Process log</h2>
2025-10-15 20:12:42 +02:00
<ul id="botmon__loglist">';
/* proces old logs */
if ($hasOldLogFiles) {
$helper = $this->loadHelper('botmon', true);
$helper->cleanup();
} else {
echo '<li>No files to process.</li>';
}
2025-10-25 21:14:35 +02:00
echo '</article>' . NL;
echo '<script>
BMSettings = {
showDay: ' . json_encode($this->getConf('showDay')) . '
};
</script>';
echo '</div><!-- End of BotMon Admin Tool -->';
2025-08-29 23:14:10 +03:00
2025-09-10 00:02:42 +02:00
}
/**
2025-10-15 20:12:42 +02:00
* Check if there are old log files to be handled
2025-09-10 00:02:42 +02:00
*
* @return bool true if there are old log files, false otherwise
*/
private function hasOldLogFiles() {
$today = gmdate('Y-m-d');
$yesterday = gmdate('Y-m-d', time() - 86400);
2025-09-01 18:55:56 +02:00
2025-09-10 00:02:42 +02:00
// scan the log directory and delete all files except for today and yesterday:
$dir = scandir(getcwd() . '/lib/plugins/botmon/logs');
foreach($dir as $file) {
$fName = pathinfo($file, PATHINFO_BASENAME);
$bName = strtok($fName, '.');
2025-08-29 23:14:10 +03:00
2025-09-19 16:06:34 +02:00
if ($bName == '' || $bName == 'logfiles' || $bName == 'empty' || $fName == '.htaccess') {
2025-09-10 00:02:42 +02:00
// ignore
} else if ($bName == $today || $bName == $yesterday) {
// skip
} else {
return true;
}
}
return false;
2025-08-29 23:14:10 +03:00
}
}