From patchwork Tue Sep 22 18:25:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 3487 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4BwqW41jQKz3wbW for ; Tue, 22 Sep 2020 18:25:52 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4BwqW02BB5z13K; Tue, 22 Sep 2020 18:25:48 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4BwqVz4dQsz30Xh; Tue, 22 Sep 2020 18:25:47 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4BwqVy4xtPz2xg0 for ; Tue, 22 Sep 2020 18:25:46 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) by mail01.ipfire.org (Postfix) with ESMTPSA id 4BwqVx3fHlzJ8; Tue, 22 Sep 2020 18:25:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1600799145; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l5+4gqjj7JUL/mB0x3WszAa7NbYnpYTgXP8dZUx4184=; b=c8SrzfvCHop4Biz3EtNRSf3nkR7rLDCtwuKWj6cB7NC1cXf6PS6HfmSN/R5IJdDZXL2z+K b70ZHgog1J2hmCaU2rLA6ZRXBGcGSv93We6oYsHi0ag0lBJCPGxAe3Z5lrfem8XziBKx7B gtmxj5wCuWpc/5eXPgpUWGVu9qi9yO1rY4Va7tyxlPvUwUv575cmYz9kx2U242rB7R6xb4 /j/uMXC3M8oYJtIbHh8LGcMCK8wFwt66CBAjeoCTbaSI0LQdUtiJHGMFWycnO2orTyffa7 xF8mWGEfgnD7DpaCABJObjWHLZNI4gVvkNu4EjhKnC4XVaircx6qQ/bGmWFAUQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1600799145; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l5+4gqjj7JUL/mB0x3WszAa7NbYnpYTgXP8dZUx4184=; b=11ypHK5F66pieyOSCTAp/byZBTvfSYwUaSVPanX26BYcUaOrbW1ER6XzYTyTqtJp8CbIah suYn/Is46q9h9QBQ== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 10/11] ipinfo.cgi: Allow to display multiple flags. Date: Tue, 22 Sep 2020 20:25:08 +0200 Message-Id: <20200922182509.18643-10-stefan.schantl@ipfire.org> In-Reply-To: <20200922182509.18643-1-stefan.schantl@ipfire.org> References: <20200922182509.18643-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Authentication-Results: mail01.ipfire.org; auth=pass smtp.mailfrom=stefan.schantl@ipfire.org X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" Signed-off-by: Stefan Schantl --- config/cfgroot/location-functions.pl | 17 +++++++--- html/cgi-bin/ipinfo.cgi | 48 ++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/config/cfgroot/location-functions.pl b/config/cfgroot/location-functions.pl index b0b8cd086..2cfe7f908 100644 --- a/config/cfgroot/location-functions.pl +++ b/config/cfgroot/location-functions.pl @@ -190,10 +190,13 @@ sub get_locations() { return @sorted_locations; } -# Function to check if a given address has a special flag. -sub address_has_flag($) { +# Function to check if a given address has one ore more special flags. +sub address_has_flags($) { my ($address) = @_; + # Array to store the flags of the address. + my @flags; + # Init libloc database handle. my $db_handle = &init(); @@ -206,10 +209,16 @@ sub address_has_flag($) { # Grab the mapped location code for this flag. $mapped_code = $network_flags{$flag}; - # Return the code. - return $mapped_code; + # Add the mapped code to the array of flags. + push(@flags, $mapped_code); } } + + # Sort the array of flags. + @flags = sort(@flags); + + # Return the array of flags. + return @flags; } 1; diff --git a/html/cgi-bin/ipinfo.cgi b/html/cgi-bin/ipinfo.cgi index cce6097ff..d8cb6c6b7 100644 --- a/html/cgi-bin/ipinfo.cgi +++ b/html/cgi-bin/ipinfo.cgi @@ -64,7 +64,7 @@ if (&General::validip($addr)) { # enumerate location information for IP address... my $db_handle = &Location::Functions::init(); my $ccode = &Location::Functions::lookup_country_code($db_handle, $addr); - my $network_flag = &Location::Functions::address_has_flag($addr); + my @network_flags = &Location::Functions::address_has_flags($addr); # Try to get the continent of the country code. my $continent = &Location::get_continent_code($db_handle, $ccode); @@ -111,12 +111,48 @@ if (&General::validip($addr)) { &Header::openbox('100%', 'left', $addr . " $ccode (" . $hostname . ') : '.$whois_server); # Check if the address has a flag. - if ($network_flag) { - # Get - my $network_flag_name = &Location::Functions::get_full_country_name($network_flag); + if (@network_flags) { + # Get amount of flags for this network. + my $flags_amount = @network_flags; + my $processed_flags; + + # The message string which will be displayed. + my $message_string = "This address is marked as"; + + # Loop through the array of network_flags. + foreach my $network_flag (@network_flags) { + # Increment value of processed flags. + $processed_flags++; + + # Get the network flag name. + my $network_flag_name = &Location::Functions::get_full_country_name($network_flag); + + # Add the flag name to the message string. + $message_string = "$message_string" . " $network_flag_name"; + + # Check if multiple flags are set for this network. + if ($flags_amount gt "1") { + # Check if the the current flag is the next-to-last one. + if ($processed_flags eq $flags_amount - 1) { + $message_string = "$message_string" . " and "; + + # Check if the current flag it the last one. + } elsif ($processed_flags eq $flags_amount) { + # The message is finished add a dot for ending the sentence. + $message_string = "$message_string" . "."; + + # Otherwise add a simple comma to the message string. + } else { + $message_string = "$message_string" . ", "; + } + } else { + # Nothing special to do, simple add a dot to finish the sentence. + $message_string = "$message_string" . "."; + } + } - # Display notice. - print "

This address is marked as $network_flag_name.

\n"; + # Display the generated notice. + print "

$message_string

\n"; print "
\n"; }