-
Notifications
You must be signed in to change notification settings - Fork 1
/
libNsdata.php
52 lines (38 loc) · 1.11 KB
/
libNsdata.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
<?php
/**
* Read nsdata file and return raw string
* @author Johnson Liu
* @version v1.0
*/
function readNsdata() {
require( __DIR__ . '/nsdata.php');
return $nsdata;
}
/**
* Replace all labels in nsdata file
* @author Johnson Liu
* @version v1.0
*/
function translateNsdata( $ip_pool, $nsdata, $serial = false ) {
$output_data = '';
if ($serial) $ip_pool['serial'] = $serial;
else $ip_pool['serial'] = date('YmdH');
$nsdata_lines = explode(PHP_EOL, $nsdata);
// process by line
foreach ( $nsdata_lines as $line ) {
$start = strpos($line, '\\');
$end = strrpos($line, '\\');
// need label
if ( $start && $end ) {
$label = substr($line, $start+1, $end-$start-1);
// label exists
if ( $ip_pool[$label] )
$output_data = $output_data . substr($line, 0, $start) . $ip_pool[$label] . substr($line, $end+1, strlen($line)-$end) . PHP_EOL;
// no label needed
} else {
$output_data = $output_data . $line . PHP_EOL;
}
}
return $output_data;
}
?>