-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
166 lines (138 loc) · 4.38 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
date_default_timezone_set("UTC");
//
$default_mode = 'weekly';
$default_template = 'posse';
//$default_mode = 'regulars';
//TODO check for autoload, and give decent message
require_once 'vendor/autoload.php';
require_once 'Profile.php';
if (file_exists('config.php')) {
require_once 'config.php';
if (!isset($config)) {
exit("Error: log_glob_pattern not defined in config.php");
}
} else {
// Demo config
$config = [
'demo' => true,
'members' => Profile::studio(),
'appIcon' => "Sparkle.png",
'ringName' => [
"Sparkle",
" the",
"Updater"
],
];
}
$m = new Mustache_Engine;
$mode = isset($_GET["mode"]) ? $_GET["mode"] : $default_mode;
$template = isset($_GET["template"]) ? $_GET["template"] : $default_template;
if (!isset($config['members'])) {
// Sanity check glob pattern
exec("ls " . $config['log_glob_pattern'] . " 2>&1", $lsoutput, $lsexitcode);
if ($lsexitcode != 0) {
echo("log_glob_pattern sanity check:<BR>" . implode('<BR>', $lsoutput) . "<BR>");
exit("Error: file matching problem in log pattern: " . $config['log_glob_pattern']);
}
// Extract the profiles -- zgrep does all the heavy lifting
exec("zgrep --no-filename 'osVersion=.*Sparkle/[0-9].*' " . $config['log_glob_pattern'], $output, $exitcode);
if ($exitcode > 1) exit("Error: zgrep-ing failed with log pattern: " . $config['log_glob_pattern']);
//print_r($output);
$profiles = [];
foreach ($output as &$line) {
preg_match('/^([\d\.]+).*\[(.*)\].*xml\?([^ ]+)/', $line, $match);
parse_str($match[3], $p);
$p["ip"] = $match[1];
$p["time"] = strtotime($match[2]);
$profiles[] = new Profile($p);
}
//print_r($profiles);
switch ($mode) {
case 'regulars':
// filter to regulars
$observed = array();
foreach ($profiles as &$p) {
$pi = clone $p;
unset($pi->time);
$pi = implode(":", (array)$pi);
if (!array_key_exists($pi, $observed)) {
$observed[$pi] = $p;
$observed[$pi]->count = 1;
} else if ($p->time > $observed[$pi]->time + 7*24*60*60) {
$observed[$pi]->count += 1;
}
// Always update the time
$observed[$pi]->time = $p->time;
}
//print_r($observed);
function multicount($o) { return $o->count > 1; }
$profiles = array_filter($observed, "multicount");
//print_r($profiles);
break;
case 'weekly':
$now = time();
function weekling($p) {
global $now;
return $p->time > $now - 7 * 24 * 60 * 60;
}
function dayling($p) {
global $now;
return $p->time > $now - 24 * 60 * 60;
}
$profiles = array_filter($profiles, "weekling");
break;
}
function rtime($a, $b) {
return $a->time == $b->time ? 0 : ($a->time > $b->time ? -1 : 1);
}
// Sort with most recent first
usort($profiles, "rtime");
//print_r(count($profiles));
$config['members'] = $profiles;
}
// When GeoIP is configured, lookup info on server
if (isset($config['geoip_city_db'])) {
// TODO check for GeoIP2 package and suggest installing
$g = new GeoIp2\Database\Reader($config['geoip_city_db']);
foreach ($config['members'] as &$p) {
try {
$r = $g->city($p->ip);
$p->geo = [
"city" => $r->city->name,
"region" => $r->mostSpecificSubdivision->isoCode,
"regionName" => $r->mostSpecificSubdivision->name,
"country" => $r->country->name,
"countryCode" => $r->country->isoCode,
"zip" => $r->postal->code,
"continent" => $r->continent->name,
"lat" => $r->location->latitude,
"lon" => $r->location->longitude,
"timezone" => $r->location->timeZone,
];
} catch (Exception $e) {
$p->geo = ["city" => "", "region" => "", "regionName" => "",
"country" => "", "countryCode" => "", "zip" => "",
"continent" => "", "lat" => "", "lon" => "", "timezone" => "",];
}
// Generate a short summary
$d = array();
if ($p->geo['city']) $d[] = $p->geo['city'];
if ($p->geo['countryCode'] == 'US' && $p->geo['region']) $d[] = $p->geo['region'];
$d[] = count($d) ? $p->geo['countryCode'] : $p->geo['country'];
$summary = implode(", ", $d);
if (! $summary) $summary = "Earth";
$p->geo['summary'] = $summary;
}
}
// If needed, set appName based on first member appName
if (!isset($config['appName'])) {
$firstp = reset($config['members']);
$config['appName'] = $firstp ? $firstp->appName : "No detected";
}
if (!isset($config['ringName'])) {
$config['ringName'] = [$config['appName'], "", ""];
}
echo $m->render(file_get_contents($template . ".html"), $config);