3 # This file is a library file for the Net-Traffic Addon.
5 # Copyright (C) 2006 Achim Weber <dotzball@users.sourceforge.net>
7 # $Id: net-traffic-admin.pl,v 1.13 2006/12/10 13:46:00 dotzball Exp $
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
32 $|=1; # line buffering
34 my $updateUrl = "http://blockouttraffic.de/version/Net-Traffic.latest";
35 my $latestVersionFile = "${General::swroot}/net-traffic/latestVersion";
40 $NETTRAFF::settingsfile
= "${General::swroot}/net-traffic/settings";
41 $NETTRAFF::versionfile
= "${General::swroot}/net-traffic/version";
42 $NETTRAFF::logfile
= "/var/log/net-traffic.log";
43 $NETTRAFF::colorOk
= '#00FF00';
44 $NETTRAFF::colorWarn
= '#FFFF00';
45 $NETTRAFF::colorMax
= '#FF0000';
48 #~ $NETTRAFF::settingsCGI = '/cgi-bin/fwrulesadm.cgi';
49 #~ $NETTRAFF::configCGI = '/cgi-bin/fwrules.cgi';
50 #~ $NETTRAFF::advConfCGI = '/cgi-bin/fwadvconf.cgi';
53 @NETTRAFF::longmonths
= ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
54 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
55 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
56 $Lang::tr
{'december'} );
58 @NETTRAFF::months
= ( 0,1,2,3,4,5,6,7,8,9,10,11 );
60 @NETTRAFF::years
=("2001","2002","2003","2004","2005","2006","2007","2008","2009");
62 #workaround to suppress a warning when a variable is used only once
63 my @dummy = ( $General::version
);
68 $NETTRAFF::settings
{'MONTHLY_VOLUME_ON'} = 'off';
69 $NETTRAFF::settings
{'MONTHLY_VOLUME'} = '1';
70 $NETTRAFF::settings
{'STARTDAY'} = '1';
71 $NETTRAFF::settings
{'WARN_ON'} = 'off';
72 $NETTRAFF::settings
{'WARN'} = '80';
73 $NETTRAFF::settings
{'CALC_INTERVAL'} = '60';
74 $NETTRAFF::settings
{'SHOW_AT_HOME'} = 'on';
75 $NETTRAFF::settings
{'SEND_EMAIL_ON'} = 'off';
76 $NETTRAFF::settings
{'EMAIL_TO'} = '';
77 $NETTRAFF::settings
{'EMAIL_FROM'} = '';
78 $NETTRAFF::settings
{'EMAIL_USR'} = '';
79 $NETTRAFF::settings
{'EMAIL_PW'} = '';
80 $NETTRAFF::settings
{'EMAIL_SERVER'} = '';
81 $NETTRAFF::settings
{'VERSION_CHECK_ON'} = 'off';
83 &NETTRAFF
::readSettings
();
88 &General
::readhash
($NETTRAFF::settingsfile
, \
%NETTRAFF::settings
);
92 sub showNetTrafficVersion
94 my %versionSettings = ();
96 &General
::readhash
($NETTRAFF::versionfile
, \
%versionSettings);
99 <a href="http://$versionSettings{'URL'}" target="_blank">
100 <b>Net-Traffic $versionSettings{'VERSION_INSTALLED'}
103 print "Build $versionSettings{'BUILD_INSTALLED'}";
105 if ($versionSettings{'IS_TESTVERSION'} == 1) {
106 print " - Testversion $versionSettings{'TESTVERSION'}";
108 print "</b></a><br /><br />\n";
110 # check for new version
111 &checkForNewVersion
();
120 if ($percent =~ m/^(\d+)%$/ )
123 <table width='100%' border='1' cellspacing='0' cellpadding='0' style='border-width:1px;border-style:solid;border-color:$fg;width:100%;height:10px;'>
127 if ($percent eq "100%" || $1 > 100)
129 $fg = $NETTRAFF::colorMax
;
130 print "<td width='100%' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'>"
132 elsif ($percent eq "0%")
134 print "<td width='100%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>"
138 if($NETTRAFF::settings
{'WARN_ON'} eq 'on'
139 && $1 >= $NETTRAFF::settings
{'WARN'})
141 $fg = $NETTRAFF::colorWarn
;
144 print "<td width='$percent' bgcolor='$fg' style='background-color:$fg;border-style:solid;border-width:1px;border-color:$bg'></td><td width='" . (100-$1) . "%' bgcolor='$bg' style='background-color:$bg;border-style:solid;border-width:1px;border-color:$bg'>"
147 <img src='/images/null.gif' width='1' height='1' alt='' />
157 sub checkForNewVersion
159 if ($NETTRAFF::settings
{'VERSION_CHECK_ON'} ne 'on')
164 # download latest version
165 &downloadLatestVersionInfo
();
167 if(-e
$latestVersionFile)
169 my %versionSettings = ();
170 &General
::readhash
($NETTRAFF::versionfile
, \
%versionSettings);
172 my %latestVersion = ();
173 &General
::readhash
($latestVersionFile, \
%latestVersion);
175 if( $versionSettings{'VERSION_INSTALLED'} lt $latestVersion{'VERSION_AVAILABLE'}
176 || ( $versionSettings{'VERSION_INSTALLED'} le $latestVersion{'VERSION_AVAILABLE'}
177 && $versionSettings{'BUILD_INSTALLED'} lt $latestVersion{'BUILD_AVAILABLE'} ) )
179 &Header
::openbox
('100%', 'left', $Lang::tr
{'info'});
184 $Lang::tr{'net traffic newversion'}
185 <a href="$latestVersion{'URL_UPDATE'}" target="_blank">
186 <b>$latestVersion{'URL_UPDATE'}</b>
192 <b>v$latestVersion{'VERSION_AVAILABLE'} - Build $latestVersion{'BUILD_AVAILABLE'}
201 sub downloadLatestVersionInfo
203 # only check if we are online
204 if (! -e
'/var/ipfire/red/active')
209 # download latest version file if it is not existing or outdated (i.e. 5 days old)
210 if((! -e
$latestVersionFile) || (int(-M
$latestVersionFile) > 5))
212 my %versionSettings = ();
213 &General
::readhash
($NETTRAFF::versionfile
, \
%versionSettings);
215 my $ua = LWP
::UserAgent
->new;
217 $ua->agent("Mozilla/4.0 (compatible; IPFire $General::version; $versionSettings{'VERSION_INSTALLED'})");
218 my $content = $ua->get($updateUrl);
220 if ( $content->is_success )
222 #~ open(FILE, ">$latestVersionFile") or die "Could not write file: $latestVersionFile";
224 #~ print FILE "$content->content\n";
227 my %latestVersion = ();
229 # latest versions, format is: MOD_VERSION="1.3.0"
230 $content->content =~ /MOD_VERSION="(.+?)"/;
231 $latestVersion{'VERSION_AVAILABLE'} = $1;
233 # latest build, format is: MOD_BUILD="0"
234 $content->content =~ /MOD_BUILD="(.+?)"/;
235 $latestVersion{'BUILD_AVAILABLE'} = $1;
237 # URL format is: MOD_URL="http://blockouttraffic.de/nt_index.php"
238 $content->content =~ /MOD_URL="(.+?)"/;
239 $latestVersion{'URL_UPDATE'} = $1;
241 &General
::writehash
($latestVersionFile, \
%latestVersion);
250 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
252 return sprintf("%04d-%02d-%02d, %02d:%02d", 1900+$year, $mon+1, $mday, $hour, $min);;