This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
import.php
executable file
·155 lines (152 loc) · 4.68 KB
/
import.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
<?php
/**
* This script is only runnable form command line
*/
if (substr(php_sapi_name(), 0, 3) != 'cli'):
die('This script can only be run from command line!');
endif;
include dirname(__FILE__).'/includes/functions.php';
/**
* Convert seconds to minutes, hours..
* NOTICE : Approximate, assumes months have 30 days.
*/
function seconds2human($ss)
{
$s = $ss%60;
$mins = floor(($ss%3600)/60);
$hours = floor(($ss%86400)/3600);
$days = floor(($ss%2592000)/86400);
$months = floor($ss/2592000);
$text = '';
if ($months > 0) {
$text .= $months." months,";
}
if ($days > 0) {
$text .= $days." days,";
}
if ($hours > 0) {
$text .= $hours." hours,";
}
if ($mins > 0) {
$text .= $mins." minutes,";
}
if ($s > 0) {
$text .= $s." seconds";
}
return $text;
}
if (!extension_loaded('simplexml')) {
echo "This script require php-xml package.".PHP_EOL;
echo "Install the package, restart webserver and run script again.".PHP_EOL;
exit;
}
/**
* Magic starts here
*/
$start_ts = time();
require dirname(__FILE__).'/config.php';
if (!isset($argv[1])):
die('Please provide a file name to import!'."\n");
endif;
$tmp = pathinfo($argv[1]);
if ($tmp['dirname'] == "."):
$filepath = dirname(__FILE__).'/'.$argv[1];
else:
$filepath = $argv[1];
endif;
if (!is_file($filepath)):
echo "File:".$filepath;
echo PHP_EOL;
echo 'File does not exist!';
echo PHP_EOL;
exit;
endif;
do {
echo PHP_EOL;
echo "Do you want to clear the database before importing (yes/no)?: ";
$handle = fopen ("php://stdin","r");
$input = fgets($handle);
} while (!in_array(trim($input), array('yes', 'no')));
$db = getPdo();
if (trim(strtolower($input)) == 'yes'):
echo PHP_EOL;
echo "Clearing the db";
echo PHP_EOL;
$db->exec("TRUNCATE TABLE data");
endif;
echo "Reading file";
echo PHP_EOL;
$content = utf8_encode(file_get_contents($filepath));
echo "Parsing file";
echo PHP_EOL;
$xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_PARSEHUGE);
if ($xml === false):
die('There is a problem with this xml file?!'.PHP_EOL);
endif;
$total = 0;
$inserted = 0;
echo "Processing data (This may take some time depending on file size)";
echo PHP_EOL;
$q = "INSERT INTO data (ip, port_id, scanned_ts, protocol, state, reason, reason_ttl, service, banner, title) VALUES (:ip, :port, :scanned_ts, :protocol, :state, :reason, :reason_ttl, :service, :banner, :title)";
$stmt = $db->prepare($q);
$stmt->bindParam(':ip', $ip, PDO::PARAM_INT);
$stmt->bindParam(':port', $port, PDO::PARAM_INT);
$stmt->bindParam(':scanned_ts', $scanned_ts);
$stmt->bindParam(':protocol', $protocol, PDO::PARAM_STR);
$stmt->bindParam(':state', $state, PDO::PARAM_STR);
$stmt->bindParam(':reason', $reason, PDO::PARAM_STR);
$stmt->bindParam(':reason_ttl', $reason_ttl, PDO::PARAM_INT);
$stmt->bindParam(':service', $service, PDO::PARAM_STR);
$stmt->bindParam(':banner', $banner, PDO::PARAM_STR);
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
foreach ($xml->host as $host):
foreach ($host->ports as $p):
$ip = sprintf('%u', ip2long($host->address['addr']));
$ts = (int) $host['endtime'];
$scanned_ts = date("Y-m-d H:i:s", $ts);
$port = (int) $p->port['portid'];
$protocol = (string) $p->port['protocol'];
if (isset($p->port->service)):
$service = (string) $p->port->service['name'];
if ($service == 'title'):
if (isset($p->port->service['banner'])):
$title = $p->port->service['banner'];
else:
$title = '';
endif;
$banner = '';
else:
if (isset($p->port->service['banner'])):
$banner = $p->port->service['banner'];
else:
$banner = '';
endif;
$title = '';
endif;
else:
$service = '';
$banner = '';
$title = '';
endif;
$state = (string) $p->port->state['state'];
$reason = (string) $p->port->state['reason'];
$reason_ttl = (int) $p->port->state['reason_ttl'];
$total++;
if ($stmt->execute()):
$inserted++;
endif;
endforeach;
endforeach;
if (DB_DRIVER == 'pgsql') {
$q = "UPDATE data SET searchtext = to_tsvector('english', title || '' || banner || '' || service || '' || protocol || '' || port_id)";
$db->exec($q);
}
$end_ts = time();
echo PHP_EOL;
echo "Summary:";
echo PHP_EOL;
echo "Total records:".$total."\n";
echo "Inserted records:".$inserted."\n";
$secs = $end_ts - $start_ts;
echo "Took about:".seconds2human($secs);
echo PHP_EOL;