Commit b4a0c4bc authored by Franco Fichtner's avatar Franco Fichtner

rc.initial: apply PSR2 style

parent e1eca26e
......@@ -48,13 +48,13 @@ if (empty($iflist)) {
return;
}
foreach($iflist as $ifname => $friendly) {
foreach ($iflist as $ifname => $friendly) {
/* point to this interface's config */
$ifconf = $config['interfaces'][$ifname];
/* look for 'special cases' */
$class = "";
if (isset($ifconf['ipaddr'])) {
switch($ifconf['ipaddr']) {
switch ($ifconf['ipaddr']) {
case "dhcp":
$class = "/DHCP4";
break;
......@@ -71,7 +71,7 @@ foreach($iflist as $ifname => $friendly) {
}
$class6 = null;
if (isset($ifconf['ipaddrv6'])) {
switch($ifconf['ipaddrv6']) {
switch ($ifconf['ipaddrv6']) {
case "dhcp6":
$class6 = "/DHCP6";
break;
......@@ -100,7 +100,8 @@ foreach($iflist as $ifname => $friendly) {
$v6first = false;
if (!empty($ipaddr) && !empty($subnet)) {
printf("v4%s: %s/%s",
printf(
"v4%s: %s/%s",
$class,
$ipaddr,
$subnet
......@@ -112,7 +113,8 @@ foreach($iflist as $ifname => $friendly) {
if (!$v6first) {
printf("\n%s", str_repeat(" ", 20));
}
printf("v6%s: %s/%s",
printf(
"v6%s: %s/%s",
$class6,
$ipaddr6,
$subnet6
......
......@@ -56,8 +56,9 @@ if (strcasecmp(chop(fgets($fp)), 'y') == 0) {
echo "Failed to locate the admin user account! Attempting to restore access.\n";
$admin_user = array();
$admin_user['uid'] = 0;
if (!isset($config['system']['user']))
if (!isset($config['system']['user'])) {
$config['system']['user'] = array();
}
$config['system']['user'][] = $admin_user;
}
......
......@@ -55,7 +55,8 @@ if ($dry_run) {
}
function console_prompt_for_yn ($prompt_text) {
function console_prompt_for_yn($prompt_text)
{
global $fp;
$good_answer = false;
......@@ -92,12 +93,14 @@ function console_get_interface_from_ppp($realif)
return '';
}
function prompt_for_enable_dhcp_server($version = 4) {
function prompt_for_enable_dhcp_server($version = 4)
{
global $config, $fp, $interface;
if($interface == "wan") {
if($config['interfaces']['lan'])
if ($interface == "wan") {
if ($config['interfaces']['lan']) {
return false;
}
}
/* only allow DHCP server to be enabled when static IP is
configured on this interface */
if ($version === 6) {
......@@ -111,13 +114,16 @@ function prompt_for_enable_dhcp_server($version = 4) {
$label_DHCP = ($version === 6) ? "DHCP6" : "DHCP";
$upperifname = strtoupper($interface);
return console_prompt_for_yn (sprintf(gettext("Do you want to enable the %s server on %s?"), $label_DHCP, $upperifname));
return console_prompt_for_yn(sprintf(gettext("Do you want to enable the %s server on %s?"), $label_DHCP, $upperifname));
}
function get_interface_config_description($iface) {
function get_interface_config_description($iface)
{
global $config;
$c = $config['interfaces'][$iface];
if (!$c) { return null; }
if (!$c) {
return null;
}
$if = $c['if'];
$result = $if;
$result2 = array();
......@@ -125,12 +131,12 @@ function get_interface_config_description($iface) {
$ipaddrv6 = $c['ipaddrv6'];
if (is_ipaddr($ipaddr)) {
$result2[] = "static";
} else if ($ipaddr == "dhcp") {
} elseif ($ipaddr == "dhcp") {
$result2[] = "dhcp";
}
if (is_ipaddr($ipaddrv6)) {
$result2[] = "staticv6";
} else if ($ipaddrv6 == "dhcp6") {
} elseif ($ipaddrv6 == "dhcp6") {
$result2[] = "dhcp6";
}
if (count($result2)) {
......@@ -150,7 +156,7 @@ $count = count($ifdescrs);
if ($count > 1) {
echo "Available interfaces:\n\n";
$x=1;
foreach($ifdescrs as $iface => $ifdescr) {
foreach ($ifdescrs as $iface => $ifdescr) {
$config_descr = get_interface_config_description($iface);
echo "{$x} - {$ifdescr} ({$config_descr})\n";
$x++;
......@@ -161,10 +167,12 @@ if ($count > 1) {
$intnum = $count;
}
if($intnum < 1)
if ($intnum < 1) {
exit;
if($intnum > $count)
}
if ($intnum > $count) {
exit;
}
$index = 1;
foreach ($ifdescrs as $ifname => $ifdesc) {
......@@ -175,18 +183,21 @@ foreach ($ifdescrs as $ifname => $ifdesc) {
$index++;
}
}
if(!$interface) {
if (!$interface) {
echo "Invalid interface!\n";
exit;
}
$ifaceassigned = "";
function next_unused_gateway_name($interface) {
function next_unused_gateway_name($interface)
{
global $g, $config;
$new_name = "GW_" . strtoupper($interface);
if (!is_array($config['gateways']['gateway_item'])) { return $new_name; }
if (!is_array($config['gateways']['gateway_item'])) {
return $new_name;
}
$count = 1;
do {
$existing = false;
......@@ -204,7 +215,8 @@ function next_unused_gateway_name($interface) {
return $new_name;
}
function add_gateway_to_config($interface, $gatewayip, $inet_type) {
function add_gateway_to_config($interface, $gatewayip, $inet_type)
{
global $g, $config, $dry_run;
if (!is_array($config['gateways']['gateway_item'])) {
$config['gateways']['gateway_item'] = array();
......@@ -217,12 +229,14 @@ function add_gateway_to_config($interface, $gatewayip, $inet_type) {
$is_default = true;
foreach ($a_gateways as $item) {
if ($item['ipprotocol'] === $inet_type) {
if (isset($item['defaultgw']))
if (isset($item['defaultgw'])) {
$is_default = false;
if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip))
}
if (($item['interface'] === $interface) && ($item['gateway'] === $gatewayip)) {
$new_name = $item['name'];
}
}
}
if ($new_name == '') {
$new_name = next_unused_gateway_name($interface);
$item = array(
......@@ -244,7 +258,8 @@ function add_gateway_to_config($interface, $gatewayip, $inet_type) {
return $new_name;
}
function console_configure_ip_address($version) {
function console_configure_ip_address($version)
{
global $g, $config, $interface, $restart_dhcpd, $ifaceassigned, $fp;
$label_IPvX = ($version === 6) ? "IPv6" : "IPv4";
......@@ -253,11 +268,12 @@ function console_configure_ip_address($version) {
$upperifname = strtoupper($interface);
if($interface == "wan") {
if (console_prompt_for_yn (sprintf(gettext("Configure %s address %s interface via %s?"), $label_IPvX, $upperifname, $label_DHCP))) {
if ($interface == "wan") {
if (console_prompt_for_yn(sprintf(gettext("Configure %s address %s interface via %s?"), $label_IPvX, $upperifname, $label_DHCP))) {
$ifppp = console_get_interface_from_ppp(get_real_interface("wan"));
if (!empty($ifppp))
if (!empty($ifppp)) {
$ifaceassigned = $ifppp;
}
$intip = ($version === 6) ? "dhcp6" : "dhcp";
$intbits = "";
$isintdhcp = true;
......@@ -265,22 +281,28 @@ function console_configure_ip_address($version) {
}
}
if($isintdhcp == false or $interface <> "wan") {
while(true) {
if ($isintdhcp == false or $interface <> "wan") {
while (true) {
do {
echo "\n" . sprintf(gettext("Enter the new %s %s address. Press <ENTER> for none:"),
$upperifname, $label_IPvX) . "\n> ";
echo "\n" . sprintf(
gettext("Enter the new %s %s address. Press <ENTER> for none:"),
$upperifname,
$label_IPvX
) . "\n> ";
$intip = chop(fgets($fp));
$is_ipaddr = ($version === 6) ? is_ipaddrv6($intip) : is_ipaddrv4($intip);
if ($is_ipaddr && is_ipaddr_configured($intip, $interface, true)) {
$ip_conflict = true;
echo gettext("This IP address conflicts with another interface or a VIP") . "\n";
} else
} else {
$ip_conflict = false;
}
} while (($ip_conflict === true) || !($is_ipaddr || $intip == ''));
if ($intip != '') {
echo "\n" . sprintf(gettext("Subnet masks are entered as bit counts (as in CIDR notation) in %s."),
$g['product_name']) . "\n";
echo "\n" . sprintf(
gettext("Subnet masks are entered as bit counts (as in CIDR notation) in %s."),
$g['product_name']
) . "\n";
if ($version === 6) {
echo "e.g. ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00 = 120\n";
echo " ffff:ffff:ffff:ffff:ffff:ffff:ffff:0 = 112\n";
......@@ -294,8 +316,12 @@ function console_configure_ip_address($version) {
}
do {
$upperifname = strtoupper($interface);
echo "\n" . sprintf(gettext("Enter the new %s %s subnet bit count (1 to %s):"),
$upperifname, $label_IPvX, $maxbits) . "\n> ";
echo "\n" . sprintf(
gettext("Enter the new %s %s subnet bit count (1 to %s):"),
$upperifname,
$label_IPvX,
$maxbits
) . "\n> ";
$intbits = chop(fgets($fp));
$intbits_ok = is_numeric($intbits) && (($intbits >= 1) && ($intbits <= $maxbits));
$restart_dhcpd = true;
......@@ -305,7 +331,7 @@ function console_configure_ip_address($version) {
echo gettext("You cannot set network address to an interface");
continue 2;
$intbits_ok = false;
} else if ($intip == gen_subnet_max($intip, $intbits)) {
} elseif ($intip == gen_subnet_max($intip, $intbits)) {
echo gettext("You cannot set broadcast address to an interface");
continue 2;
$intbits_ok = false;
......@@ -327,7 +353,7 @@ function console_configure_ip_address($version) {
if ($gwip != '') {
if (!$is_ipaddr) {
echo sprintf(gettext("not an %s IP address!"), $label_IPvX) . "\n";
} else if (!$is_in_subnet) {
} elseif (!$is_in_subnet) {
echo gettext("not in subnet!") . "\n";
}
}
......@@ -339,8 +365,9 @@ function console_configure_ip_address($version) {
}
}
$ifppp = console_get_interface_from_ppp(get_real_interface($interface));
if (!empty($ifppp))
if (!empty($ifppp)) {
$ifaceassigned = $ifppp;
}
break;
}
}
......@@ -351,8 +378,9 @@ function console_configure_ip_address($version) {
list($intip, $intbits, $gwname) = console_configure_ip_address(4);
list($intip6, $intbits6, $gwname6) = console_configure_ip_address(6);
if (!empty($ifaceassigned))
if (!empty($ifaceassigned)) {
$config['interfaces'][$interface]['if'] = $ifaceassigned;
}
$config['interfaces'][$interface]['ipaddr'] = $intip;
$config['interfaces'][$interface]['subnet'] = $intbits;
$config['interfaces'][$interface]['gateway'] = $gwname;
......@@ -361,7 +389,8 @@ $config['interfaces'][$interface]['subnetv6'] = $intbits6;
$config['interfaces'][$interface]['gatewayv6'] = $gwname6;
$config['interfaces'][$interface]['enable'] = true;
function console_configure_dhcpd($version = 4) {
function console_configure_dhcpd($version = 4)
{
global $g, $config, $restart_dhcpd, $fp, $interface, $dry_run, $intip, $intbits, $intip6, $intbits6;
$label_IPvX = ($version === 6) ? "IPv6" : "IPv4";
......@@ -380,8 +409,9 @@ function console_configure_dhcpd($version = 4) {
}
$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpstartip) : is_ipaddrv4($dhcpstartip);
$is_inrange = is_inrange($dhcpstartip, $subnet_start, $subnet_end);
if (!$is_inrange)
if (!$is_inrange) {
echo gettext("This IP address must be in the interface's subnet") . "\n";
}
} while (!$is_ipaddr || !$is_inrange);
do {
......@@ -393,8 +423,9 @@ function console_configure_dhcpd($version = 4) {
}
$is_ipaddr = ($version === 6) ? is_ipaddrv6($dhcpendip) : is_ipaddrv4($dhcpendip);
$is_inrange = is_inrange($dhcpendip, $subnet_start, $subnet_end);
if (!$is_inrange)
if (!$is_inrange) {
echo gettext("This IP address must be in the interface's subnet") . "\n";
}
$not_inorder = ($version === 6) ? (inet_pton($dhcpendip) < inet_pton($dhcpstartip)) : ip_less_than($dhcpendip, $dhcpstartip);
if ($not_inorder) {
echo gettext("The end address of the DHCP range must be >= the start address") . "\n";
......@@ -406,7 +437,7 @@ function console_configure_dhcpd($version = 4) {
$config[$dhcpd][$interface]['range']['from'] = $dhcpstartip;
$config[$dhcpd][$interface]['range']['to'] = $dhcpendip;
} else {
if(isset($config[$dhcpd][$interface]['enable'])) {
if (isset($config[$dhcpd][$interface]['enable'])) {
unset($config[$dhcpd][$interface]['enable']);
printf(gettext("Disabling %s DHCPD..."), $label_IPvX);
$restart_dhcpd = true;
......@@ -420,8 +451,7 @@ console_configure_dhcpd(6);
//*****************************************************************************
if ($config['system']['webgui']['protocol'] == "https") {
if (console_prompt_for_yn (gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
if (console_prompt_for_yn(gettext("Do you want to revert to HTTP as the webConfigurator protocol?"))) {
$config['system']['webgui']['protocol'] = "http";
$restart_webgui = true;
}
......@@ -432,21 +462,27 @@ if (isset($config['system']['webgui']['noantilockout'])) {
unset($config['system']['webgui']['noantilockout']);
}
if($config['interfaces']['lan']) {
if($config['dhcpd'])
if($config['dhcpd']['wan'])
if ($config['interfaces']['lan']) {
if ($config['dhcpd']) {
if ($config['dhcpd']['wan']) {
unset($config['dhcpd']['wan']);
if($config['dhcpdv6'])
if($config['dhcpdv6']['wan'])
}
}
if ($config['dhcpdv6']) {
if ($config['dhcpdv6']['wan']) {
unset($config['dhcpdv6']['wan']);
}
}
}
if(!$config['interfaces']['lan']) {
if (!$config['interfaces']['lan']) {
unset($config['interfaces']['lan']);
if($config['dhcpd']['lan'])
if ($config['dhcpd']['lan']) {
unset($config['dhcpd']['lan']);
if($config['dhcpdv6']['lan'])
}
if ($config['dhcpdv6']['lan']) {
unset($config['dhcpdv6']['lan']);
}
unset($config['nat']);
if (!$dry_run) {
system("rm /var/dhcpd/var/db/* >/dev/null 2>/dev/null");
......@@ -461,7 +497,7 @@ if (!$dry_run) {
interface_reconfigure(strtolower($upperifname));
echo "\n Reloading filter...";
filter_configure_sync();
if($restart_dhcpd) {
if ($restart_dhcpd) {
echo "\n DHCPD...";
services_dhcpd_configure();
}
......@@ -472,27 +508,39 @@ if (!$dry_run) {
if ($intip != '') {
if (is_ipaddr($intip)) {
echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
$upperifname, "{$intip}/{$intbits}") . "\n";
echo "\n\n" . sprintf(
gettext("The IPv4 %s address has been set to %s"),
$upperifname,
"{$intip}/{$intbits}"
) . "\n";
} else {
echo "\n\n" . sprintf(gettext("The IPv4 %s address has been set to %s"),
$upperifname, $intip) . "\n";
echo "\n\n" . sprintf(
gettext("The IPv4 %s address has been set to %s"),
$upperifname,
$intip
) . "\n";
}
}
if ($intip6 != '') {
if (is_ipaddr($intip6)) {
echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
$upperifname, "${intip6}/${intbits6}") . "\n";
echo "\n\n" . sprintf(
gettext("The IPv6 %s address has been set to %s"),
$upperifname,
"${intip6}/${intbits6}"
) . "\n";
} else {
echo "\n\n" . sprintf(gettext("The IPv6 %s address has been set to %s"),
$upperifname, $intip6) . "\n";
echo "\n\n" . sprintf(
gettext("The IPv6 %s address has been set to %s"),
$upperifname,
$intip6
) . "\n";
}
}
if ($intip != '' || $intip6 != '') {
if (count($ifdescrs) == "1" or $interface == "lan") {
echo gettext('You can now access the webConfigurator by opening the following URL in your web browser:') . "\n";
if(!empty($config['system']['webgui']['port'])) {
if (!empty($config['system']['webgui']['port'])) {
$webuiport = $config['system']['webgui']['port'];
if ($intip != '') {
echo " {$config['system']['webgui']['protocol']}://{$intip}:{$webuiport}/\n";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment