-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.php
More file actions
55 lines (49 loc) · 1.4 KB
/
Copy pathimport.php
File metadata and controls
55 lines (49 loc) · 1.4 KB
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
<?php
// Import Access Log
// Autoload Classes
spl_autoload_register(function ($class_name) {
require_once '/path/to/class/folder' . $class_name . '.class.php';
});
// Required Variables
$accessLog = new AccessLog();
$errorMsg = '';
//echo "Import Access Log\n";
//echo "Importing data (v1.2)\n";
//echo 'Started at: ' . date('g:i:s A', time()) . "\n";
$i = 1;
// Loop through file
$handle = @fopen("/path/to/access/log/access.log", "r");
if($handle){
while(($log = fgets($handle, 4096)) !== false) {
if(!empty($log)){
// ----- Regular Expression Matching -----
// http://stackoverflow.com/questions/7603017/parse-apache-log-in-php-using-preg-match
$regex = '/^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] \"(\S+) (.*?) (\S+)\" (\S+) (\S+) "([^"]*)" "([^"]*)"$/';
preg_match($regex ,$log, $matches);
// Add to Database
if(!empty($matches)){
$accessLog->addEntry($matches);
if($accessLog->error){
$errorMsg = $accessLog->errorMsg;
break;
}else{
$i++;
}
}else{
echo "[Access Log Import Error] Unable to match: $log\n";
}
}
}
if(!feof($handle)){
echo "[Access Log Import Error] unexpected fgets() fail\n";
}
fclose($handle);
}
// Close
if(!empty($errorMsg)){
echo "[Access Log Import Error] $errorMsg\n";
}
$imported = number_format($i);
//echo 'Ended at: ' . date('g:i:s A', time()) . "\n";
//echo "Import complete.\n$imported entries imported.\n";
?>