Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 652da20

Browse files
author
Ivan Dudarev
committed
Fixed issue #6
1 parent 92f5d99 commit 652da20

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/Converter.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,12 @@ protected function createTmpDb()
551551
$sql = 'CREATE '.'TABLE `_ips` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `ip` INTEGER,`action` TEXT, `parameter` TEXT, `value` TEXT, `offset` TEXT); CREATE INDEX `ip` ON `_ips` (`ip`); CREATE INDEX `parameter` ON `_ips` (`parameter`); CREATE INDEX `value` ON `_ips` (`value`);';
552552
$this->pdo->exec($sql);
553553
$prepare['insert']['ips'] = $this->pdo->prepare('INSERT '.'INTO `_ips` (`ip`,`action`,`parameter`,`value`) VALUES (:ip,:action,:parameter,:value);');
554+
$prepare['insert']['ips']->execute(array(
555+
'ip' => 0,
556+
'action' => 'add',
557+
'parameter' => NULL,
558+
'value' => NULL,
559+
));
554560
foreach ($this->networks as $network) {
555561
$file = $this->csv[$network['csv']];
556562
$csv = fopen($file['file'], 'r');
@@ -709,7 +715,7 @@ protected function createTmpRegisters()
709715
*/
710716
protected function createTmpNetworks()
711717
{
712-
$ip = 0;
718+
$ip = -1;
713719
$fields = array();
714720
$values = array();
715721
$format = array();
@@ -725,24 +731,28 @@ protected function createTmpNetworks()
725731
$values[$register][] = 0;
726732
}
727733
$pack = implode('',$format['pack']);
728-
$empty = self::packArray($pack,$fields);
729-
$bin = pack('N',$ip).$empty;
734+
$binaryPrevData = self::packArray($pack,$fields);
735+
$empty = pack('N',0).$binaryPrevData;
730736
$this->meta['networks']['pack'] = implode('/',$format['unpack']);
731737
$offset = 0;
732-
$this->meta['networks']['len'] = strlen($bin);
738+
$this->meta['networks']['len'] = strlen($empty);
733739
$this->meta['index'][0] = 0;
734740
$file = $this->temporaryDir.DIRECTORY_SEPARATOR.'networks.'.uniqid().'.tmp';
735741
$tmpFile = fopen($file,'w');
736742
$ipinfo = $this->pdo->query('SELECT * '.'FROM `_ips` ORDER BY `ip` ASC, `action` DESC, `id` ASC;');
737743
while ($row = $ipinfo->fetch()) {
738744
if ($row['ip'] !== $ip) {
739745
foreach ($values as $param=>$v) {
740-
$fields[$param] = array_pop($v);
746+
if (!empty($param)) $fields[$param] = array_pop($v);
747+
}
748+
$binaryData = self::packArray($pack,$fields);
749+
if ($binaryData !== $binaryPrevData || empty($ip)) {
750+
fwrite($tmpFile, pack('N', $ip) . $binaryData);
751+
$octet = (int)long2ip($ip);
752+
if (!isset($this->meta['index'][$octet])) $this->meta['index'][$octet] = $offset;
753+
$offset++;
754+
$binaryPrevData = $binaryData;
741755
}
742-
fwrite($tmpFile,pack('N',$ip).self::packArray($pack,$fields));
743-
$octet = (int)long2ip($ip);
744-
if (!isset($this->meta['index'][$octet])) $this->meta['index'][$octet] = $offset;
745-
$offset++;
746756
$ip = $row['ip'];
747757
}
748758
if ($row['action'] == 'remove') {
@@ -756,12 +766,15 @@ protected function createTmpNetworks()
756766
}
757767
if ($ip < ip2long('255.255.255.255')) {
758768
foreach ($values as $param => $v) {
759-
$fields[$param] = array_pop($v);
769+
if (!empty($param)) $fields[$param] = array_pop($v);
770+
}
771+
$binaryData = self::packArray($pack, $fields);
772+
if ($binaryData !== $binaryPrevData) {
773+
$octet = (int)long2ip($ip);
774+
if (!isset($this->meta['index'][$octet])) $this->meta['index'][$octet] = $offset;
775+
$offset++;
776+
fwrite($tmpFile, pack('N', $ip) . $binaryData);
760777
}
761-
$octet = (int)long2ip($ip);
762-
if (!isset($this->meta['index'][$octet])) $this->meta['index'][$octet] = $offset;
763-
$offset++;
764-
fwrite($tmpFile, pack('N', $ip) . self::packArray($pack, $fields));
765778
}
766779
$this->meta['networks']['items'] = $offset;
767780
for($i=1;$i<=255;$i++) {

src/Iptool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function about()
211211
'author' => '',
212212
'license' => '',
213213
'networks' => array(
214-
'count' => $this->meta['networks']['items']-1,
214+
'count' => $this->meta['networks']['items'],
215215
'data' => array(),
216216
),
217217
);

0 commit comments

Comments
 (0)