forked from AaronVanGeffen/AwstatsParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awparse.php
439 lines (386 loc) · 11.9 KB
/
awparse.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/*
AWSTATS DATA FILE PARSER / MERGER / GENERATOR ==============================
@version: 1.2
@date: 2012-08-30
@author: Aaron van Geffen
@website: http://aaronweb.net/
@license: BSD
VERSION HISTORY ============================================================
version 1.2 (2012-08-30)
* Merged patches by Dave Dykstra:
* Do a better job at merging FirstTime, LastTime, LastUpdate and TotalVisits.
* Fixed bug that caused some data to be merged incorrectly.
* Treat 3rd and 4th indexes as dates when merging VISITOR and EXTRA_1 sections.
version 1.1 (2012-08-29)
* Released on GitHub.
* Removed unnecessary call-time pass-by-references;
* Added newlines to error messages;
version 1.0 (2010-06-19)
* Initial release.
TODO =======================================================================
* Import mode: only add unique missing data, or merge all data
* Prefixes and suffixes for URLs on import, i.e. adding /projects as a prefix
PATCHES / PULL REQUESTS ====================================================
Are most welcome through Github. The repository is located at:
https://github.com/AaronVanGeffen/AwstatsParser
*/
// Not running PHP-CLI?
if (isset($_SERVER['HTTP_HOST']) || !isset($_SERVER['argv'], $_SERVER['argc']))
die("This script is intended for use with PHP-CLI only.\n");
// Klingon functions have arguments! (Note: argument 0 is the script's file name.)
if ($_SERVER['argc'] <= 1)
die("Not enough arguments. Expecting every argument to be an awstats file to parse. Pipe to save the script's output.\nExample usage: awparse.php awstats062010.txt awstats072010.txt > awstats06072010.txt\n");
// Instantiate a new merger class.
$stats = new AwstatsMerger();
// Assuming all arguments are files, try to add them.
for ($i = 1; $i < count($_SERVER['argv']); $i++)
{
if (!file_exists($_SERVER['argv'][$i]))
die('File not found: ' . $_SERVER['argv'][$i]);
$stats->add(new AwstatsFromFile($_SERVER['argv'][$i]));
}
// Print merged statistics file.
echo $stats->getFileContents();
/**
* Abstract class that allows generalization of Awstats files.
*/
abstract class AwstatsFile
{
// this should be protected but because of a bug in PHP 5.1
// it is set to public
public $data = array();
public function getFileContents()
{
$contents = "AWSTATS DATA FILE 6.9 (build 1.925)\n\n";
foreach ($this->data as $section_name => $rows)
{
$contents .= 'BEGIN_' . $section_name . ' ' . count($rows) . "\n";
foreach ($rows as $key => $row)
$contents .= $key . ' ' . implode(' ', $row) . "\n";
$contents .= 'END_' . $section_name . "\n\n";
}
return $contents;
}
public function writeFileContents($filename)
{
file_put_contents($filename, $this->getFileContents());
}
}
/**
* Class that interprets an existing Awstats statistics file into a generic AwstatsFile instance.
*/
class AwstatsFromFile extends AwstatsFile
{
/**
* Opens the designated file and parses all sections in it.
*/
public function __construct($filename)
{
if (!$fp = fopen($filename, 'r'))
die('File not found: ' . $filename);
while ($section_name = $this->read_to_section($fp))
{
// Skip the MAP section; have the Awstats script regenerate it later.
if ($section_name == 'MAP')
continue;
$this->data[$section_name] = $this->parse_section($fp);
}
}
/**
* Outputs the data in $data to the output buffer.
*/
public function print_data()
{
print_r($this->data);
}
/**
* Reads to the next section, ignoring everything in its way.
*/
private function read_to_section(&$fp)
{
do
$str = trim(fgets($fp));
while (!feof($fp) && strpos($str, "BEGIN_") === false);
return !feof($fp) ? substr($str, 6, strrpos($str, ' ') - 6) : false;
}
/**
* Parses a section until its end.
*/
private function parse_section(&$fp)
{
$stats = array();
do
{
if (feof($fp))
break;
$str = trim(fgets($fp));
if (strpos($str, 'END_') !== false)
break;
if ($str[0] == "#")
continue;
$row = explode(' ', $str);
$key = array_shift($row);
$stats[$key] = $row;
}
while (!feof($fp) && strpos($str, 'END_') === false);
return $stats;
}
}
/**
* Class that takes care of merging multiple instances of AwstatsFile into one AwstatsFile.
*/
class AwstatsMerger extends AwstatsFile
{
/**
* Adds (merges) an existing AwstatsFile instance to this instance.
* @param $file AwstatsFile instance
*/
public function add(AwstatsFile $file)
{
foreach ($file->data as $section_name => $rows)
{
// Is this the first time this type of statistic is passed?
if (empty($this->data[$section_name]))
{
$this->data[$section_name] = $rows;
continue;
}
// The hard way: merge stats.
else
{
// Does this section have its own merge function?
$merge_func = 'merge_' . strtolower($section_name);
if (method_exists($this, $merge_func))
$this->$merge_func($rows, $section_name);
// Use the generic merge function for basic addition.
else
$this->helper_sum_merge($rows, $section_name);
// Prevent unnecessary sorting.
if ($section_name == 'GENERAL')
continue;
// Few sections require us to sort ascending by their keys...
if (in_array($section_name, array('TIME', 'DAY')))
{
ksort($this->data[$section_name]);
continue;
}
// Other sections require us to order descending by their first value.
uasort($this->data[$section_name], 'AwstatsMerger::helper_sort_desc');
}
}
}
/**
* Merges general statistics.
* TODO: improve this? (How? Suggestions are welcome.)
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_general(&$rows, &$section_name)
{
foreach ($rows as $item => $row)
{
if (!isset($this->data['GENERAL'][$item]))
{
$this->data['GENERAL'][$item] = $row;
continue;
}
else
{
if ($item == 'FirstTime')
$this->data['GENERAL'][$item][0] = min($row[0], $this->data['GENERAL'][$item][0]);
elseif ($item == 'LastTime' || $item == 'LastUpdate')
$this->data['GENERAL'][$item][0] = max($row[0], $this->data['GENERAL'][$item][0]);
elseif ($item == 'TotalVisits')
$this->data['GENERAL'][$item][0] += $row[0];
}
}
}
/**
* Merges login statistics.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_login(&$rows, &$section_name)
{
foreach ($rows as $login => $row)
{
if (!isset($this->data['LOGIN'][$login]))
{
$this->data['LOGIN'][$login] = $row;
continue;
}
$this->data['LOGIN'][$login][0] += $row[0];
$this->data['LOGIN'][$login][1] += $row[1];
$this->data['LOGIN'][$login][2] += $row[2];
$this->data['LOGIN'][$login][3] = max($this->data['LOGIN'][$login][3], $row[3]);
}
}
/**
* Merges robot statistics.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_robot(&$rows, &$section_name)
{
foreach ($rows as $robot => $row)
{
if (!isset($this->data['ROBOT'][$robot]))
{
$this->data['ROBOT'][$robot] = $row;
continue;
}
$this->data['ROBOT'][$robot][0] += $row[0];
$this->data['ROBOT'][$robot][1] += $row[1];
$this->data['ROBOT'][$robot][2] = max($this->data['ROBOT'][$robot][2], $row[2]);
$this->data['ROBOT'][$robot][3] += $row[3];
}
}
/**
* Merges worm statistics.
* Note: handled the same way as emailsender and emailreceiver.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_worm(&$rows, &$section_name)
{
foreach ($rows as $worm => $row)
{
if (!isset($this->data[$section_name][$worm]))
{
$this->data[$section_name][$worm] = $row;
continue;
}
$this->data[$section_name][$worm][0] += $row[0];
$this->data[$section_name][$worm][1] += $row[1];
$this->data[$section_name][$worm][2] = max($this->data[$section_name][$worm][2], $row[2]);
}
}
/**
* Merges email sender statistics.
* Note: handled the same way as emailreceiver and worm.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_emailsender(&$rows, &$section_name)
{
return $this->merge_worm($rows, $section_name);
}
/**
* Merges email receiver statistics.
* Note: handled the same way as emailsender and worm.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_emailreceiver(&$rows, &$section_name)
{
return $this->merge_worm($rows, $section_name);
}
/**
* Merges unknown referer statistics.
* Note: handled the same way as unknownrefererbrowser.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_unknownreferer(&$rows, &$section_name)
{
foreach ($rows as $referer => $row)
{
if (!isset($this->data[$section_name][$referer]))
{
$this->data[$section_name][$referer] = $row;
continue;
}
$this->data[$section_name][$referer][0] = max($this->data[$section_name][$referer][0], $row[0]);
}
}
/**
* Merges unknown referer browser statistics.
* Note: handled the same way as unknownreferer.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_unknownrefererbrowser(&$rows, &$section_name)
{
return $this->merge_unknownreferer($rows, $section_name);
}
/**
* Merges sections that have dates in indexes 3 and 4 (if present)
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function helper_sum_with_dates(&$rows, &$section_name)
{
foreach ($rows as $key => $row)
{
if (!isset($this->data[$section_name][$key]))
{
$this->data[$section_name][$key] = $row;
continue;
}
// Merge rows, taking into account that not all entries have the same number of data items
// Indexes 3 and 4 are dates so take max instead of sum
// For indexes above 4 take whichever one comes first
foreach ($row as $num => $stats)
{
if ($num <= 2)
$this->data[$section_name][$key][$num] = isset($this->data[$section_name][$key][$num]) ? $this->data[$section_name][$key][$num] + $stats : $stats;
elseif ($num <= 4)
$this->data[$section_name][$key][$num] = isset($this->data[$section_name][$key][$num]) ? max($this->data[$section_name][$key][$num], $stats) : $stats;
elseif (!isset($this->data[$section_name][$key][$num]))
$this->data[$section_name][$key][$num] = $stats;
}
}
}
/**
* Merges visitor statistics.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_visitor(&$rows, &$section_name)
{
// Note that index 3 is a start date and the max will be taken when it
// should technically be min but it doesn't matter because the start date is never used.
return $this->helper_sum_with_dates($rows, $section_name);
}
/**
* Merges extra_1 statistics.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function merge_extra_1(&$rows, &$section_name)
{
return $this->helper_sum_with_dates($rows, $section_name);
}
/**
* Merges statistics by simply taking the sum of existing rows.
* @param $rows existing set of rows
* @param $section_name identifier of the current section
*/
private function helper_sum_merge(&$rows, &$section_name)
{
foreach ($rows as $key => $row)
{
if (!isset($this->data[$section_name][$key]))
{
$this->data[$section_name][$key] = $row;
continue;
}
foreach ($row as $num => $stats)
$this->data[$section_name][$key][$num] += $stats;
}
}
/**
* Helps sorting stats descending.
* @param $a first item to compare against
* @param $b section item to compare against
*/
public static function helper_sort_desc($a, $b)
{
if ($a[0] == $b[0])
return 0;
else
return $a[0] > $b[0] ? -1 : 1; // sorts descending
}
}
?>