xref: /plugin/botmon/helper.php (revision 20997f50c57e537871e9ba04fd9db047b967cb66)
1<?php
2/**
3 * BotMon Helper Plugin
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Sascha Leib <ad@hominem.info>
7 */
8
9use dokuwiki\Extension\Plugin;
10
11class helper_plugin_botmon extends Plugin {
12
13	/**
14	 * Constructor
15	 */
16	public function __construct() {
17		echo "<li>Processing logfiles …</li>\n";
18	}
19
20	/**
21	 * Cleanup function
22	 */
23	public function cleanup() {
24
25		// exclude the following two dates:
26		$today = gmdate('Y-m-d');
27		$yesterday = gmdate('Y-m-d', time() - 86400);
28
29		// scan the log directory and delete all files except for today and yesterday:
30		$dir = scandir(DOKU_PLUGIN . 'botmon/logs');
31		foreach($dir as $file) {
32			$fName = pathinfo($file, PATHINFO_BASENAME);
33			$bName = strtok($fName, '.');
34
35			if ($bName == '' || $bName == 'logfiles' || $bName == 'empty' || $fName == '.htaccess') {
36				// echo "File “{$fName}” ignored.\n";
37			} else if ($bName == $today || $bName == $yesterday) {
38				echo "<li class='skipped'>File “{$fName}” skipped.</li>\n";
39			} else {
40				if (unlink(DOKU_PLUGIN . 'botmon/logs/' . $file)) {
41					echo "<li class='success'>File “{$fName}” deleted.</li>\n";
42				} else {
43					echo "<li class='error'>File “{$fName}” could not be deleted!</li>\n";
44				}
45			}
46		}
47		echo "<li>Done.</li>\n";
48	}
49
50}