Files
ipreg/import.php

134 lines
3.9 KiB
PHP

<?php
/*****************************************************************************
IP Reg, a PHP/MySQL IPAM tool
Copyright (C) 2007-2009 Wietse Warendorff (up to v0.5)
Copyright (C) 2011-2023 Thomas Hooge
SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
include("includes.php");
// ========== ACTIONS START ===================================================
switch ($submit = form_get_action()) {
case NULL: break;
case 'edit': $action = ACT_EDIT; break;
case 'pfsense':
if ($_FILES['xmlfile']['size'] == 0) {
$action = ACT_DEFAULT;
break;
}
$action = ACT_EDIT;
break;
case 'exec-pfsense':
$g_message->Add(print_r($_SESSION['pfimport'], true));
unset($_SESSION['pfimport']);
$action = ACT_DEFAULT;
break;
default:
$g_error->Add(submit_error($submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
$smarty->assign("scripts", 'checkbox.js');
include("header.php");
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
$smarty->display("import.tpl");
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit display options ===================================
$smarty->assign("xmlfile",$_FILES['xmlfile']['name']);
$smarty->assign("xmlfiletype", $_FILES['xmlfile']['type']);
$smarty->assign("xmltmp", $_FILES['xmlfile']['tmp_name']);
$uploadfile = '/var/www/html/ipreg/import/'. basename($_FILES['xmlfile']['tmp_name']);
if (! move_uploaded_file($_FILES['xmlfile']['tmp_name'], $uploadfile)) {
$smarty->display("import.tpl");
$smarty->display('footer.tpl');
exit;
}
$xml = simplexml_load_file($uploadfile);
$conf_alias = $xml->xpath('/pfsense/aliases/alias');
$alias = array();
foreach ($conf_alias as $a) {
if ($a->type == 'host') {
$host = explode(' ', $a->address);
if (count($host) == 1) {
$alias[(string) $a->name] = $host[0];
}
}
}
$wanip = (string) $xml->xpath('/pfsense/interfaces/wan/ipaddr')[0];
$nat = $xml->xpath('/pfsense/nat/rule');
$res = array();
$ruleid = 0;
// for database checks
$sql = "SELECT COUNT(*)
FROM nat
JOIN node AS n1 ON nat.nat_ext=n1.node_id
JOIN node AS n2 ON nat.nat_int=n2.node_id
WHERE n1.node_ip=? AND n2.node_ip=?";
$sth = $dbh->prepare($sql);
foreach ($nat as $n) {
if (! $n->destination->address) {
if ($n->destination->network == 'wanip') {
$nat_ext = ip2long($wanip);
} else {
$nat_ext = ip2long((string) $n->destination->network);
}
} else {
$nat_ext = ip2long((string) $n->destination->address);
};
$nat_ext_port = (int) $n->destination->port;
$tgt = (string) $n->target;
if (array_key_exists($tgt, $alias)) {
$tgt = $alias[$tgt];
}
$nat_int = ip2long($tgt);
$nat_int_port = (int) $n->{'local-port'};
$description = (string) $n->descr;
// check if data already exists
$sth->execute([long2ip($nat_ext), long2ip($nat_int)]);
$dupe = $sth->fetchColumn() == 0;
$ruleid += 1;
$res[] = ['id' => $ruleid,
'ext' => $nat_ext,
'ext_port' => $nat_ext_port,
'int' => $nat_int,
'int_port' => $nat_int_port,
'description' => $description,
'dupe' => $dupe];
}
// store importdata for next selection step
$_SESSION['pfimport'] = $res;
$smarty->assign("natlist", $res);
$smarty->display("importpfsense.tpl");
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo "<p>Unknown function call: Please report to system development!</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
$smarty->display('footer.tpl');