1#!/usr/bin/env php
2<?php
3
4// This is the base directory of the SimpleSAMLphp installation
5$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
6
7// Add library autoloader.
8require_once($baseDir . '/lib/_autoload.php');
9
10// Initialize the configuration.
11$configdir = SimpleSAML\Utils\Config::getConfigDir();
12SimpleSAML_Configuration::setConfigDir($configdir);
13
14$progName = array_shift($argv);
15$debug = false;
16$dryrun = false;
17$output = '/tmp/simplesamlphp-new.log';
18$infile = null;
19
20foreach ($argv as $a) {
21    if (strlen($a) === 0) {
22        continue;
23    }
24    if (strpos($a, '=') !== false) {
25        $p = strpos($a, '=');
26        $v = substr($a, $p + 1);
27        $a = substr($a, 0, $p);
28    } else {
29        $v = null;
30    }
31
32    // Map short options to long options.
33    $shortOptMap = array('-d' => '--debug');
34    if (array_key_exists($a, $shortOptMap)) {
35        $a = $shortOptMap[$a];
36    }
37
38    switch ($a) {
39        case '--help':
40            printHelp();
41            exit(0);
42        case '--debug':
43            $debug = true;
44            break;
45        case '--dry-run':
46            $dryrun = true;
47            break;
48        case '--infile':
49            $infile = $v;
50            break;
51        case '--outfile':
52            $output = $v;
53            break;
54        default:
55            echo('Unknown option: ' . $a . "\n");
56            echo('Please run `' . $progName . ' --help` for usage information.' . "\n");
57            exit(1);
58    }
59}
60
61$cleaner = new sspmod_statistics_LogCleaner($infile);
62$cleaner->dumpConfig();
63$todelete = $cleaner->clean($debug);
64
65echo "Cleaning these trackIDs: " . join(', ', $todelete) . "\n";
66
67if (!$dryrun) {
68    $cleaner->store($todelete, $output);
69}
70
71/**
72 * This function prints the help output.
73 */
74
75function printHelp() {
76    global $progName;
77
78    echo <<<END
79Usage: $progName [options]
80
81This program cleans logs. This script is experimental. Do not run it unless you have talked to Andreas about it.
82The script deletes log lines related to sessions that produce more than 200 lines.
83
84Options:
85	-d, --debug			Used when configuring the log file syntax. See doc.
86	--dry-run			Aggregate but do not store the results.
87	--infile			File input.
88	--outfile			File to output the results.
89
90END;
91}
92
93