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 …</header> 56 <div id="botmon__today__content"> 57 <details id="botmon__today__overview" open> 58 <summary>Bot overview (page views)</summary> 59 <div class="botmon_overview_grid"> 60 <dl id="botmon__today__botsvshumans"></dl> 61 <dl id="botmon__botslist"></dl> 62 <dl id="botmon__today__botips"></dl> 63 <dl id="botmon__today__countries"></dl> 64 </div> 65 </details> 66 <details id="botmon__today__webmetrics"> 67 <summary>Web metrics</summary> 68 <div class="botmon_overview_grid"> 69 <dl id="botmon__today__wm_overview"></dl> 70 <dl id="botmon__today__wm_clients"></dl> 71 <dl id="botmon__today__wm_platforms"></dl> 72 <dl></dl> 73 </div> 74 </details> 75 <details id="botmon__today__visitors"> 76 <summary>Visitor logs</summary> 77 <div id="botmon__today__visitorlists"></div> 78 </details> 79 </div> 80 <footer aria-live="polite"> 81 <span>' . $svg . '</span> 82 <span id="botmon__today__status">Initialising …</span> 83 </footer> 84 </article> 85</div><!-- End of BotMon Admin Tool -->'; 86 87 } 88 89 /** 90 * Check if there are old log files that can be deleted. 91 * 92 * @return bool true if there are old log files, false otherwise 93 */ 94 private function hasOldLogFiles() { 95 96 $today = gmdate('Y-m-d'); 97 $yesterday = gmdate('Y-m-d', time() - 86400); 98 99 // scan the log directory and delete all files except for today and yesterday: 100 $dir = scandir(getcwd() . '/lib/plugins/botmon/logs'); 101 foreach($dir as $file) { 102 $fName = pathinfo($file, PATHINFO_BASENAME); 103 $bName = strtok($fName, '.'); 104 105 if ($bName == '' || $bName == 'logfiles') { 106 // ignore 107 } else if ($bName == $today || $bName == $yesterday) { 108 // skip 109 } else { 110 return true; 111 } 112 } 113 return false; 114 } 115}