Commit c2a5f03c authored by Ad Schellevis's avatar Ad Schellevis

(legacy) curly braces and spaces in pfsense-utils.inc

parent 1e884f54
<?php
/*
* Copyright (C) 2004-2007 Scott Ullrich <sullrich@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
Copyright (C) 2004-2007 Scott Ullrich <sullrich@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/****f* legacy/is_private_ip
* NAME
......@@ -35,7 +34,8 @@
* RESULT
* returns true if an ip address is in a private range
******/
function is_private_ip($iptocheck) {
function is_private_ip($iptocheck)
{
$isprivate = false;
$ip_private_list=array(
"10.0.0.0/8",
......@@ -44,15 +44,16 @@ function is_private_ip($iptocheck) {
"192.168.0.0/16",
);
foreach($ip_private_list as $private) {
if(ip_in_subnet($iptocheck,$private)==true)
if (ip_in_subnet($iptocheck,$private)==true) {
$isprivate = true;
}
}
return $isprivate;
}
/****f* legacy/get_dns_servers
* NAME
* get_dns_servres - get system dns servers
* get_dns_servers - get system dns servers
* INPUTS
* $dns_servers - an array of the dns servers
* RESULT
......@@ -63,9 +64,10 @@ function get_dns_servers() {
$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($dns_s as $dns) {
$matches = "";
if (preg_match("/nameserver (.*)/", $dns, $matches))
if (preg_match("/nameserver (.*)/", $dns, $matches)) {
$dns_servers[] = $matches[1];
}
}
return array_unique($dns_servers);
}
......@@ -134,7 +136,6 @@ function setup_polling()
* This function only supports the fxp driver's loadable microcode.
******/
function setup_microcode() {
/* if list */
$ifs = legacy_interface_listget();
......@@ -154,13 +155,14 @@ function get_carp_interface_status($carpinterface) {
$interface = get_real_interface($interface);
exec("/sbin/ifconfig $interface | /usr/bin/grep -v grep | /usr/bin/grep carp: | /usr/bin/grep 'vhid {$vhid}'", $carp_query);
foreach($carp_query as $int) {
if(stristr($int, "MASTER"))
if (stristr($int, "MASTER")) {
return gettext("MASTER");
if(stristr($int, "BACKUP"))
} elseif (stristr($int, "BACKUP")) {
return gettext("BACKUP");
if(stristr($int, "INIT"))
} elseif(stristr($int, "INIT")) {
return gettext("INIT");
}
}
return;
}
......@@ -187,7 +189,6 @@ function backup_config_section($section_name) {
function restore_config_section($section_name, $new_contents)
{
global $config;
$tmpxml = '/tmp/tmpxml';
$fout = fopen($tmpxml, 'w');
......@@ -353,13 +354,14 @@ function setup_serial_port($sync = true)
}
foreach($ttys_split as $tty) {
if (stristr($tty, "ttyv0"))
if (stristr($tty, "ttyv0")) {
fwrite($fd, "ttyv0 \"/usr/libexec/getty {$console_type}\" cons25 on secure\n");
else if (stristr($tty, "ttyu0"))
} elseif (stristr($tty, "ttyu0")) {
fwrite($fd, "ttyu0 \"/usr/libexec/getty {$serial_type}\" cons25 {$on_off} secure\n");
else
} else {
fwrite($fd, $tty . "\n");
}
}
unset($on_off, $console_type, $serial_type);
fclose($fd);
......@@ -377,23 +379,25 @@ function reload_ttys()
/* Any PPPoE servers enabled? */
function is_pppoe_server_enabled() {
function is_pppoe_server_enabled()
{
global $config;
$pppoeenable = false;
if (!isset($config['pppoes']['pppoe']) || !is_array($config['pppoes']['pppoe']))
if (!isset($config['pppoes']['pppoe']) || !is_array($config['pppoes']['pppoe'])) {
return false;
}
foreach ($config['pppoes']['pppoe'] as $pppoes)
if ($pppoes['mode'] == 'server')
foreach ($config['pppoes']['pppoe'] as $pppoes) {
if ($pppoes['mode'] == 'server') {
$pppoeenable = true;
}
}
return $pppoeenable;
}
function add_hostname_to_watch($hostname) {
if(!is_dir("/var/db/dnscache")) {
if (!is_dir("/var/db/dnscache")) {
mkdir("/var/db/dnscache");
}
$result = array();
......@@ -427,16 +431,16 @@ function add_hostname_to_watch($hostname) {
function is_fqdn($fqdn) {
$hostname = false;
if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
if (preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
$hostname = true;
}
if(preg_match("/\.\./", $fqdn)) {
if (preg_match("/\.\./", $fqdn)) {
$hostname = false;
}
if(preg_match("/^\./i", $fqdn)) {
if (preg_match("/^\./i", $fqdn)) {
$hostname = false;
}
if(preg_match("/\//i", $fqdn)) {
if (preg_match("/\//i", $fqdn)) {
$hostname = false;
}
return($hostname);
......@@ -486,8 +490,9 @@ function download_file($url, $destination, $verify_ssl = false, $connect_timeout
$fp = fopen($destination, "wb");
if (!$fp)
if (!$fp) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
......@@ -501,8 +506,9 @@ function download_file($url, $destination, $verify_ssl = false, $connect_timeout
if (!empty($config['system']['proxyurl'])) {
curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
if (!empty($config['system']['proxyport']))
if (!empty($config['system']['proxyport'])) {
curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
}
if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
......@@ -517,7 +523,7 @@ function download_file($url, $destination, $verify_ssl = false, $connect_timeout
}
/* Split() is being DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. */
if(!function_exists("split")) {
if (!function_exists("split")) {
function split($separator, $haystack, $limit = null) {
log_error("deprecated split() call with separator '{$separator}'");
return preg_split($separator, $haystack, $limit);
......@@ -526,33 +532,33 @@ if(!function_exists("split")) {
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname)
{
global $config, $pconfig;
global $config;
if (!$origname) {
return;
}
$sectionref = &$config;
foreach($section as $sectionname) {
if(is_array($sectionref) && isset($sectionref[$sectionname]))
if (is_array($sectionref) && isset($sectionref[$sectionname])) {
$sectionref = &$sectionref[$sectionname];
else
} else {
return;
}
}
if(is_array($sectionref)) {
if (is_array($sectionref)) {
foreach($sectionref as $itemkey => $item) {
$fieldfound = true;
$fieldref = &$sectionref[$itemkey];
foreach($field as $fieldname) {
if(is_array($fieldref) && isset($fieldref[$fieldname]))
if (is_array($fieldref) && isset($fieldref[$fieldname])) {
$fieldref = &$fieldref[$fieldname];
else {
} else {
$fieldfound = false;
break;
}
}
if($fieldfound && $fieldref == $origname) {
if ($fieldfound && $fieldref == $origname) {
$fieldref = $new_alias_name;
}
}
......@@ -561,7 +567,7 @@ function update_alias_names_upon_change($section, $field, $new_alias_name, $orig
function process_alias_unzip($temp_filename) {
if(!file_exists("/usr/local/bin/unzip")) {
if (!file_exists("/usr/local/bin/unzip")) {
log_error(gettext("Alias archive is a .zip file which cannot be decompressed because utility is missing!"));
return false;
}
......@@ -581,8 +587,9 @@ function process_alias_unzip($temp_filename) {
log_error(sprintf(gettext('The following file could not be read %s from %s'), $f2p, $temp_filename));
continue;
}
while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE)
while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE) {
fwrite($fd, $tmpbuf);
}
fclose($tmpfd);
unlink($f2p);
}
......@@ -593,7 +600,7 @@ function process_alias_unzip($temp_filename) {
}
function process_alias_tgz($temp_filename) {
if(!file_exists('/usr/bin/tar')) {
if (!file_exists('/usr/bin/tar')) {
log_error(gettext("Alias archive is a .tar/tgz file which cannot be decompressed because utility is missing!"));
return false;
}
......@@ -613,8 +620,9 @@ function process_alias_tgz($temp_filename) {
log_error(sprintf(gettext('The following file could not be read %s from %s'), $f2p, $temp_filename));
continue;
}
while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE)
while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE) {
fwrite($fd, $tmpbuf);
}
fclose($tmpfd);
unlink($f2p);
}
......@@ -639,10 +647,7 @@ function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
}
// If the file doesn't exist or is older than update_freq days, fetch a new copy.
if (!file_exists($urltable_filename)
|| ((time() - filemtime($urltable_filename)) > ($freq * 86400 - 90))
|| $forceupdate) {
if (!file_exists($urltable_filename) || ((time() - filemtime($urltable_filename)) > ($freq * 86400 - 90)) || $forceupdate) {
// Try to fetch the URL supplied
@unlink("{$urltable_filename}.tmp");
$verify_ssl = isset($config['system']['checkaliasesurlcert']);
......@@ -683,64 +688,54 @@ function xml2array($contents, $get_attributes = 1, $priority = 'tag')
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values)
if (!$xml_values) {
return; //Hmm...
}
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data)
{
foreach ($xml_values as $data) {
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag')
if ($priority == 'tag') {
$result = $value;
else
} else {
$result['value'] = $value;
}
if (isset ($attributes) and $get_attributes)
{
foreach ($attributes as $attr => $val)
{
if ($priority == 'tag')
}
if (isset ($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if ($priority == 'tag') {
$attributes_data[$attr] = $val;
else
} else {
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
if ($type == "open")
{
}
if ($type == "open") {
$parent[$level -1] = & $current;
if (!is_array($current) or (!in_array($tag, array_keys($current))))
{
if (!is_array($current) || (!in_array($tag, array_keys($current)))) {
$current[$tag] = $result;
if ($attributes_data)
if ($attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
}
else
{
if (isset ($current[$tag][0]))
{
} else {
if (isset ($current[$tag][0])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
} else {
$current[$tag] = array ($current[$tag], $result );
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr']))
{
if (isset ($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
......@@ -748,52 +743,36 @@ function xml2array($contents, $get_attributes = 1, $priority = 'tag')
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
}
elseif ($type == "complete")
{
if (!isset ($current[$tag]))
{
} elseif ($type == "complete") {
if (!isset ($current[$tag])) {
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data)
if ($priority == 'tag' and $attributes_data) {
$current[$tag . '_attr'] = $attributes_data;
}
else
{
if (isset ($current[$tag][0]) and is_array($current[$tag]))
{
} else {
if (isset ($current[$tag][0]) and is_array($current[$tag])) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data)
{
if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
} else {
$current[$tag] = array ($current[$tag], $result );
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes)
{
if (isset ($current[$tag . '_attr']))
{
if ($priority == 'tag' and $get_attributes) {
if (isset ($current[$tag . '_attr'])) {
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data)
{
if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
}
elseif ($type == 'close')
{
} elseif ($type == 'close') {
$current = & $parent[$level -1];
}
}
......@@ -806,26 +785,27 @@ function filter_rules_sort() {
global $config;
/* mark each rule with the sequence number (to retain the order while sorting) */
for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
$config['filter']['rule'][$i]['seq'] = $i;
}
usort($config['filter']['rule'], "filter_rules_compare");
/* strip the sequence numbers again */
for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
unset($config['filter']['rule'][$i]['seq']);
}
}
function filter_rules_compare($a, $b) {
if (isset($a['floating']) && isset($b['floating']))
if (isset($a['floating']) && isset($b['floating'])) {
return $a['seq'] - $b['seq'];
else if (isset($a['floating']))
} elseif (isset($a['floating'])) {
return -1;
else if (isset($b['floating']))
} elseif (isset($b['floating'])) {
return 1;
else if ($a['interface'] == $b['interface'])
} elseif ($a['interface'] == $b['interface']) {
return $a['seq'] - $b['seq'];
else
} else {
return compare_interface_friendly_names($a['interface'], $b['interface']);
}
}
......@@ -840,8 +820,9 @@ function filter_rules_compare($a, $b) {
function load_mac_manufacturer_table() {
/* load MAC-Manufacture data from the file */
$macs = false;
if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes"))
if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes")) {
$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
}
if ($macs){
foreach ($macs as $line){
if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)){
......@@ -850,8 +831,9 @@ function load_mac_manufacturer_table() {
}
}
return $mac_man;
} else
} else {
return -1;
}
}
......@@ -868,12 +850,12 @@ function is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false,
global $config;
$isipv6 = is_ipaddrv6($ipaddr);
if ($check_subnets) {
$iflist = get_configured_interface_list();
foreach ($iflist as $if => $ifname) {
if ($ignore_if == $if)
if ($ignore_if == $if) {
continue;
}
if ($isipv6 === true) {
$bitmask = get_interface_subnetv6($if);
......@@ -883,39 +865,46 @@ function is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false,
$subnet = gen_subnet(get_interface_ip($if), $bitmask);
}
if (ip_in_subnet($ipaddr, $subnet . '/' . $bitmask))
if (ip_in_subnet($ipaddr, $subnet . '/' . $bitmask)) {
return true;
}
}
} else {
if ($isipv6 === true)
if ($isipv6 === true) {
$interface_list_ips = get_configured_ipv6_addresses();
else
} else {
$interface_list_ips = get_configured_ip_addresses();
}
foreach($interface_list_ips as $if => $ilips) {
/* Also ignore CARP interfaces, it'll be checked below */
if ($ignore_if == $if || strstr($ignore_if, "_vip"))
if ($ignore_if == $if || strstr($ignore_if, "_vip")) {
continue;
if (strcasecmp($ipaddr, $ilips) == 0)
}
if (strcasecmp($ipaddr, $ilips) == 0) {
return true;
}
}
}
$interface_list_vips = get_configured_vips_list(true);
foreach ($interface_list_vips as $id => $vip) {
if ($ignore_if == $vip['if'])
if ($ignore_if == $vip['if']) {
continue;
if (strcasecmp($ipaddr, $vip['ipaddr']) == 0)
}
if (strcasecmp($ipaddr, $vip['ipaddr']) == 0) {
return true;
}
}
if ($check_localip) {
if (is_array($config['pptpd']) && !empty($config['pptpd']['localip']) && (strcasecmp($ipaddr, $config['pptpd']['localip']) == 0))
if (is_array($config['pptpd']) && !empty($config['pptpd']['localip']) && (strcasecmp($ipaddr, $config['pptpd']['localip']) == 0)) {
return true;
}
if (!is_array($config['l2tp']) && !empty($config['l2tp']['localip']) && (strcasecmp($ipaddr, $config['l2tp']['localip']) == 0))
if (!is_array($config['l2tp']) && !empty($config['l2tp']['localip']) && (strcasecmp($ipaddr, $config['l2tp']['localip']) == 0)) {
return true;
}
}
return false;
}
......@@ -932,7 +921,7 @@ function calculate_ipv6_delegation_length($if) {
if(!isset($config['interfaces'][$if]) || !is_array($config['interfaces'][$if])) {
return false;
} elseif (!isset($config['interfaces'][$if]['ipaddrv6'])) {
return (0);
return 0;
}
switch($config['interfaces'][$if]['ipaddrv6']) {
......@@ -952,5 +941,5 @@ function calculate_ipv6_delegation_length($if) {
$pdlen = 0;
break;
}
return($pdlen);
return $pdlen;
}
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