From 880e13538ee1d89892bfb0324350079e93e64811 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Mon, 22 Nov 2021 09:09:55 +0100 Subject: [PATCH] www/nginx: give this code a style update --- LICENSE | 1 + .../NgxUniqueDefaultServerConstraint.php | 199 ++++++++---------- .../OPNsense/Nginx/Migrations/M1_24_0.php | 69 +++--- 3 files changed, 126 insertions(+), 143 deletions(-) diff --git a/LICENSE b/LICENSE index 4e612f590..761fe6976 100644 --- a/LICENSE +++ b/LICENSE @@ -27,6 +27,7 @@ Copyright (c) 2021 Manuel Hofmann Copyright (c) 2003-2004 Manuel Kasper Copyright (c) 2020 Marc Leuser Copyright (c) 2021 Marcel Koepfli +Copyright (c) 2021 Markus Peter Copyright (c) 2020 Martin Wasley Copyright (c) 2017-2021 Michael Muenz Copyright (c) 2021 Nim G diff --git a/www/nginx/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/NgxUniqueDefaultServerConstraint.php b/www/nginx/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/NgxUniqueDefaultServerConstraint.php index 1b5423465..953425125 100644 --- a/www/nginx/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/NgxUniqueDefaultServerConstraint.php +++ b/www/nginx/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/NgxUniqueDefaultServerConstraint.php @@ -1,30 +1,30 @@ + * 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) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ namespace OPNsense\Base\Constraints; @@ -43,92 +43,79 @@ class NgxUniqueDefaultServerConstraint extends BaseConstraint { $node = $this->getOption('node'); if ($node) { - $httpServerNode = $node->getParentNode(); - $defaultServerNode = $httpServerNode->getChild("default_server"); - if (!$this->isEmpty($defaultServerNode)) - { - $myUUID = $httpServerNode->getAttribute("uuid"); - $myListenHTTPAddress = $httpServerNode->getChild("listen_http_address"); - - $httpServersNode = $httpServerNode->getParentNode(); - - $httpServers = $httpServersNode->getChildren(); + $httpServerNode = $node->getParentNode(); + $defaultServerNode = $httpServerNode->getChild("default_server"); + if (!$this->isEmpty($defaultServerNode)) { + $myUUID = $httpServerNode->getAttribute("uuid"); + $myListenHTTPAddress = $httpServerNode->getChild("listen_http_address"); - $msg = ""; - foreach ($httpServers as $httpServer) - { - $uuid = $httpServer->getAttribute("uuid"); - if ($uuid != $myUUID) - { - $defaultServerNode = $httpServer->getChild("default_server"); - if (!$this->isEmpty($defaultServerNode)) - { - $listenHTTPAddressNode = $httpServer->getChild("listen_http_address"); - if ($this->compareListenAddresses($myListenHTTPAddress, $listenHTTPAddressNode, $msg)) - { - $validator->appendMessage(new Message( - sprintf(gettext("There can only be one Default Server on each listening address: %s conflict."), $msg), - $attribute - )); - } - } - } - } - } + $httpServersNode = $httpServerNode->getParentNode(); + + $httpServers = $httpServersNode->getChildren(); + + $msg = ""; + foreach ($httpServers as $httpServer) { + $uuid = $httpServer->getAttribute("uuid"); + if ($uuid != $myUUID) { + $defaultServerNode = $httpServer->getChild("default_server"); + if (!$this->isEmpty($defaultServerNode)) { + $listenHTTPAddressNode = $httpServer->getChild("listen_http_address"); + if ($this->compareListenAddresses($myListenHTTPAddress, $listenHTTPAddressNode, $msg)) { + $validator->appendMessage(new Message( + sprintf(gettext("There can only be one Default Server on each listening address: %s conflict."), $msg), + $attribute + )); + } + } + } + } + } } return true; } - + private function compareListenAddresses($as, $bs, &$msg): bool { - foreach (explode(",", $as) as $a) - { - list($a_af, $a_ip, $a_port) = $this->extractAFIPPort($a); - foreach (explode(",", $bs) as $b) - { - list($b_af, $b_ip, $b_port) = $this->extractAFIPPort($b); - if ($a_af == $b_af && $a_port == $b_port) - { - if ($a_ip == null || $a_ip == "::" || $b_ip == null || $b_ip == "::" || $a_ip == $b_ip) - { - $msg = "IPv" . $a_af . ": [" . $a_ip . "]:" . $a_port . " and IPv" . $b_af . ": [" . $b_ip . "]:" . $b_port; - return true; - } - } - } - } - return false; - } - private function extractAFIPPort($in): array - { - $af = null; - $ip = null; - $port = null; - if (!strpos($in, ":")) - { - //if only number, then ipv4 port only - $af = 4; - $port = $in; - } - else - { - //extract ip and port - if (preg_match("/(?:([0-9.]+)|\[([0-9a-fA-F:]+)\]):(\d+)/", $in, $parts)); - { - if (strpos($in, "[") === 0) - { - $af = 6; - $ip = inet_ntop(inet_pton($parts[2])); - $port = $parts[3]; - } - else - { - $af = 4; - $ip = long2ip(ip2long($parts[1])); - $port = $parts[3]; - } - } - } - return [$af, $ip, $port]; - } + foreach (explode(",", $as) as $a) { + list($a_af, $a_ip, $a_port) = $this->extractAFIPPort($a); + foreach (explode(",", $bs) as $b) { + list($b_af, $b_ip, $b_port) = $this->extractAFIPPort($b); + if ($a_af == $b_af && $a_port == $b_port) { + if ($a_ip == null || $a_ip == "::" || $b_ip == null || $b_ip == "::" || $a_ip == $b_ip) { + $msg = "IPv" . $a_af . ": [" . $a_ip . "]:" . $a_port . " and IPv" . $b_af . ": [" . $b_ip . "]:" . $b_port; + return true; + } + } + } + } + return false; + } + + private function extractAFIPPort($in): array + { + $af = null; + $ip = null; + $port = null; + if (!strpos($in, ":")) { + //if only number, then ipv4 port only + $af = 4; + $port = $in; + } else { + //extract ip and port + if (preg_match("/(?:([0-9.]+)|\[([0-9a-fA-F:]+)\]):(\d+)/", $in, $parts)) { + } + { + if (strpos($in, "[") === 0) { + $af = 6; + $ip = inet_ntop(inet_pton($parts[2])); + $port = $parts[3]; + } else { + $af = 4; + $ip = long2ip(ip2long($parts[1])); + $port = $parts[3]; + } + } + } + return [$af, $ip, $port]; + } } diff --git a/www/nginx/src/opnsense/mvc/app/models/OPNsense/Nginx/Migrations/M1_24_0.php b/www/nginx/src/opnsense/mvc/app/models/OPNsense/Nginx/Migrations/M1_24_0.php index cac938df9..981fbd99a 100644 --- a/www/nginx/src/opnsense/mvc/app/models/OPNsense/Nginx/Migrations/M1_24_0.php +++ b/www/nginx/src/opnsense/mvc/app/models/OPNsense/Nginx/Migrations/M1_24_0.php @@ -1,31 +1,29 @@ + * All rights reserved. * - * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * - * 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. * - * 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) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * 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) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ namespace OPNsense\Nginx\Migrations; @@ -37,23 +35,20 @@ class M1_24_0 extends BaseModelMigration public function run($model) { foreach ($model->getNodeByReference('http_server')->iterateItems() as $http_server) { - if ($http_server->listen_http_port != '') - { - $http_server->listen_http_address = $http_server->listen_http_port . ',[::]:' . $http_server->listen_http_port; - $http_server->listen_http_port = null; - } - if ($http_server->listen_https_port != '') - { - $http_server->listen_https_address = $http_server->listen_https_port . ',[::]:' . $http_server->listen_https_port; - $http_server->listen_https_port = null; - } + if ($http_server->listen_http_port != '') { + $http_server->listen_http_address = $http_server->listen_http_port . ',[::]:' . $http_server->listen_http_port; + $http_server->listen_http_port = null; + } + if ($http_server->listen_https_port != '') { + $http_server->listen_https_address = $http_server->listen_https_port . ',[::]:' . $http_server->listen_https_port; + $http_server->listen_https_port = null; + } } foreach ($model->getNodeByReference('stream_server')->iterateItems() as $server) { - if ($server->listen_port != '') - { - $server->listen_address = $server->listen_port . ',[::]:' . $server->listen_port; - $server->listen_port = null; - } + if ($server->listen_port != '') { + $server->listen_address = $server->listen_port . ',[::]:' . $server->listen_port; + $server->listen_port = null; + } } } }