From 47043de5f79a3c5c519415c9f8187825ec4bcd72 Mon Sep 17 00:00:00 2001 From: doktornotor Date: Sun, 18 Oct 2015 12:46:51 +0200 Subject: Code style cleanups --- config/lightsquid/sqstat.php | 611 +++++++++++++++++++++---------------------- 1 file changed, 299 insertions(+), 312 deletions(-) (limited to 'config/lightsquid') diff --git a/config/lightsquid/sqstat.php b/config/lightsquid/sqstat.php index 7b12b970..18fa2d25 100644 --- a/config/lightsquid/sqstat.php +++ b/config/lightsquid/sqstat.php @@ -1,410 +1,397 @@ + Copyright (C) 2011 Sergey Dvoriancev + Copyright (C) 2015 ESF, LLC + 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. */ - -/* -*** sqstat - Squid Proxy Server realtime stat *** -(c) Alex Samorukov, samm@os2.kiev.ua -*/ - +/* Squid Proxy Server realtime stats */ require_once('guiconfig.inc'); require_once('sqstat.class.php'); -# init +// init $squidclass = new squidstat(); -# ------------------------------------------------------------------------------ -# Requests -# ------------------------------------------------------------------------------ - -# AJAX responce -if ($_REQUEST['getactivity']) -{ - header("Content-type: text/javascript"); - echo sqstat_AJAX_response( $_REQUEST ); - exit; +/* + * Requests + */ + +/* AJAX response */ +if ($_REQUEST['getactivity']) { + header("Content-type: text/javascript"); + echo sqstat_AJAX_response( $_REQUEST ); + exit; } -# ------------------------------------------------------------------------------ -# HTML Page -# ------------------------------------------------------------------------------ +/* + * HTML Page + */ -$pgtitle = "Proxy Squid: Realtime stat (sqstat)"; +$pgtitle = "Squid Proxy Server: Realtime Stats (SQStat)"; require_once("head.inc"); -$csrf_token= csrf_get_tokens(); +$csrf_token = csrf_get_tokens(); ?> - + - + - - - + query_exec(); - } - - if ($squidclass->errno == 0) { - $data = sqstat_resultHTML($data); - } - else { - # error - $data = sqstat_errorHTML(); - } + // Prepare page data + $data = ''; + sqstat_loadconfig(); + if (sqstat_loadconfig() == 0) { + $data = $squidclass->query_exec(); + } + + if ($squidclass->errno == 0) { + $data = sqstat_resultHTML($data); + } else { + // error + $data = sqstat_errorHTML(); + } ?> - -
- + +
+
- +
- +
- + + query_exec(); + // Actions + $data = $squidclass->query_exec(); - $ver = sqstat_serverInfoHTML(); - $res .= "el('sqstat_serverver').innerHTML = '$ver';"; + $ver = sqstat_serverInfoHTML(); + $res .= "el('sqstat_serverver').innerHTML = '$ver';"; - $time = date("h:i:s d/m/Y"); - $res .= "el('sqstat_updtime').innerHTML = '$time';"; + $time = date("h:i:s d/m/Y"); + $res .= "el('sqstat_updtime').innerHTML = '$time';"; - $data = sqstat_resultHTML( $data ); - if ($squidclass->errno == 0) { - $data = sqstat_AJAX_prep($data); - $res .= "el('sqstat_result').innerHTML = '$data';"; - } - else { - # error - $res .= sqstat_AJAX_error(sqstat_errorHTML()); - } + $data = sqstat_resultHTML( $data ); + if ($squidclass->errno == 0) { + $data = sqstat_AJAX_prep($data); + $res .= "el('sqstat_result').innerHTML = '$data';"; + } else { + // error + $res .= sqstat_AJAX_error(sqstat_errorHTML()); + } - return $res; + return $res; } -function sqstat_AJAX_prep($text) -{ - $text = str_replace("'", "\'", $text); - $text = str_replace("\n", "\\r\\n", $text); - return $text; +function sqstat_AJAX_prep($text) { + $text = str_replace("'", "\'", $text); + $text = str_replace("\n", "\\r\\n", $text); + return $text; } -function sqstat_AJAX_error($err) -{ - $err = sqstat_AJAX_prep($err); - $t .= "el('sqstat_result').innerHTML = '$err';"; - return $t; +function sqstat_AJAX_error($err) { + $err = sqstat_AJAX_prep($err); + $t .= "el('sqstat_result').innerHTML = '$err';"; + return $t; } -# ------------------------------------------------------------------------------ -# Reports -# ------------------------------------------------------------------------------ +/* + *Reports + */ -function sqstat_headerHTML() -{ - global $squidclass; +function sqstat_headerHTML() { + global $squidclass; - $date = date("h:i:s d/m/Y"); - $squidinfo = sqstat_serverInfoHTML(); + $date = date("h:i:s d/m/Y"); + $squidinfo = sqstat_serverInfoHTML(); - if (empty($squidclass->autorefresh)) $squidclass->autorefresh = 0; + if (empty($squidclass->autorefresh)) { + $squidclass->autorefresh = 0; + } - return -<< - + Squid RealTime stat {$squidclass->sqstat_version} for the {$servers} proxy server {$squidinfo}.
Auto refresh: sec. - - Created at: {$date}
+ + Created at: {$date}
EOD; } -function sqstat_serverInfoHTML() -{ - global $squidclass; - return $squidclass->server_version . " ({$squidclass->squidhost}:{$squidclass->squidport})"; +function sqstat_serverInfoHTML() { + global $squidclass; + return $squidclass->server_version . " ({$squidclass->squidhost}:{$squidclass->squidport})"; } -function sqstat_resultHTML($data) -{ - global $squidclass; - - $group_by_name = $squidclass->group_by_name; - $use_js = true; - - $t = array(); - - # table header - $t[] = ""; - $t[] = ""; - $t[] = ""; - if ($squidclass->use_sessions) - $t[] = ""; - $t[] = ""; - $t[] = ""; - - # table body - if (is_array($data['users'])) { - $tbl = array(); - - $con_color = 0; - foreach($data['users'] as $key => $v) { - # skeep total info - if ($key == 'total') continue; - # group row - $tbl[] = ""; - $tbl[] = ""; - $tbl[] = ""; - $tbl[] = ""; - - # connections row - foreach ($v['con'] as $con) { - if ($use_js) - $js = "onMouseout='hideddrivetip()' onMouseover='ddrivetip(\"" . $squidclass->implode_with_keys($con,"
") . "\")'"; - else $js=''; - - # begin new row - $class = (++$con_color % 2 == 0) ? " class='odd'" : ""; - $tbl[] = ""; - - # URL - $uri = "{$con['uritext']}"; - $tbl[] = ""; - $tbl[] = ""; - - # speed - if ($squidclass->use_sessions) { - $cur_s = round($con['cur_speed'], 2) > 0 ? sprintf("%01.2f KB/s", $con['cur_speed']) : ''; - $avg_s = round($con['avg_speed'], 2) > 0 ? sprintf("%01.2f KB/s", $con['avg_speed']) : ''; - $tbl[] = ""; - $tbl[] = ""; - } - - # file size - $filesize = $squidclass->filesize_format($con["bytes"]); - $duration = $squidclass->duration($con["seconds"], "short"); - $tbl[] = ""; - $tbl[] = ""; - - # end row - $tbl[] = ""; - } - - # total user speed - if ($squidclass->use_sessions) { - $user_curr = sprintf("%01.2f KB/s", $v['user_curr']); - $user_avg = sprintf("%01.2f KB/s", $v['user_avg']); - $tbl[] =""; - $tbl[] =""; - $tbl[] =""; - $tbl[] =""; - $tbl[] =""; - } - } - - - # status row - $stat = array(); - $ausers = sprintf("%d", $data['ausers']); - $acon = sprintf("%d", $data['acon']); - $stat[] = ""; - if ($squidclass->use_sessions) { - $total_curr = sprintf("%01.2f", $data['total_curr']); - $total_avg = sprintf("%01.2f", $data['total_avg']); - $stat[] = ""; - } - else { - $stat[] = ""; - } - $t[] = ""; - } - - if ($ausers == 0) { - $t[] = ""; - } - else { - $stat = implode("\n", $stat); - $tbl = implode("\n", $tbl); - $t[] = $stat . $tbl . $stat; - } - - $t[] = "
{$group_by_name}URICurr. SpeedAvg. SpeedSizeTime
" . (is_int($key) ? long2ip($key) : $key) . " 
{$uri}{$cur_s}{$avg_s}{$filesize}{$duration}
{$user_curr}{$user_avg}
Total:{$ausers} users and {$acon} connections @ {$total_curr}/{$total_avg} KB/s (CURR/AVG){$ausers} users and {$acon} connections
No active connections
"; - $t[] = "

Report based on SQStat © Alex Samorukov, 2006

"; - - return implode("\n", $t); +function sqstat_resultHTML($data) { + global $squidclass; + + $group_by_name = $squidclass->group_by_name; + $use_js = true; + + $t = array(); + + // table header + $t[] = ""; + $t[] = ""; + $t[] = ""; + if ($squidclass->use_sessions) { + $t[] = ""; + } + $t[] = ""; + $t[] = ""; + + // table body + if (is_array($data['users'])) { + $tbl = array(); + + $con_color = 0; + foreach($data['users'] as $key => $v) { + // skip total info + if ($key == 'total') { + continue; + } + // group row + $tbl[] = ""; + $tbl[] = ""; + $tbl[] = ""; + $tbl[] = ""; + + // connections row + foreach ($v['con'] as $con) { + if ($use_js) { + $js = "onMouseout='hideddrivetip()' onMouseover='ddrivetip(\"" . $squidclass->implode_with_keys($con,"
") . "\")'"; + } else { + $js=''; + } + + // begin new row + $class = (++$con_color % 2 == 0) ? " class='odd'" : ""; + $tbl[] = ""; + + // URL + $uri = "{$con['uritext']}"; + $tbl[] = ""; + $tbl[] = ""; + + // speed + if ($squidclass->use_sessions) { + $cur_s = round($con['cur_speed'], 2) > 0 ? sprintf("%01.2f KB/s", $con['cur_speed']) : ''; + $avg_s = round($con['avg_speed'], 2) > 0 ? sprintf("%01.2f KB/s", $con['avg_speed']) : ''; + $tbl[] = ""; + $tbl[] = ""; + } + + // file size + $filesize = $squidclass->filesize_format($con["bytes"]); + $duration = $squidclass->duration($con["seconds"], "short"); + $tbl[] = ""; + $tbl[] = ""; + + // end row + $tbl[] = ""; + } + + // total user speed + if ($squidclass->use_sessions) { + $user_curr = sprintf("%01.2f KB/s", $v['user_curr']); + $user_avg = sprintf("%01.2f KB/s", $v['user_avg']); + $tbl[] =""; + $tbl[] =""; + $tbl[] =""; + $tbl[] =""; + $tbl[] =""; + } + } + + + // status row + $stat = array(); + $ausers = sprintf("%d", $data['ausers']); + $acon = sprintf("%d", $data['acon']); + $stat[] = ""; + if ($squidclass->use_sessions) { + $total_curr = sprintf("%01.2f", $data['total_curr']); + $total_avg = sprintf("%01.2f", $data['total_avg']); + $stat[] = ""; + } else { + $stat[] = ""; + } + $t[] = ""; + } // ENDIF (is_array($data['users'])) + + if ($ausers == 0) { + $t[] = ""; + } else { + $stat = implode("\n", $stat); + $tbl = implode("\n", $tbl); + $t[] = $stat . $tbl . $stat; + } + + $t[] = "
{$group_by_name}URICurr. SpeedAvg. SpeedSizeTime
" . (is_int($key) ? long2ip($key) : $key) . " 
{$uri}{$cur_s}{$avg_s}{$filesize}{$duration}
{$user_curr}{$user_avg}
Total:{$ausers} users and {$acon} connections @ {$total_curr}/{$total_avg} KB/s (CURR/AVG){$ausers} users and {$acon} connections
No active connections
"; + $t[] = "

Report based on SQStat © Alex Samorukov, 2006

"; + + return implode("\n", $t); } -function sqstat_errorHTML() -{ - global $squidclass; - $t = array(); +function sqstat_errorHTML() { + global $squidclass; + $t = array(); - # table header - $t[] = ""; - $t[] = ""; - $t[] = ""; - $t[] = "
SqStat error
"; - $t[] = '

Error (' . $squidclass->errno . '): ' . $squidclass->errstr . '

'; - $t[] = "
"; + // table header + $t[] = ""; + $t[] = ""; + $t[] = ""; + $t[] = "
SqStat error
"; + $t[] = '

Error (' . $squidclass->errno . '): ' . $squidclass->errstr . '

'; + $t[] = "
"; - return implode ("\n", $t); + return implode ("\n", $t); } -function sqstat_loadconfig() -{ - global $squidclass, $config; - - $squidclass->errno = 0; - $squidclass->errstr = ''; - - $squidclass->sqstat_version = SQSTAT_VERSION; - - # === load config from pfSense === - $iface = '127.0.0.1'; - $iport = 3128; - $squid_settings = $config['installedpackages']['squid']['config'][0]; - if (!empty($squid_settings)) { - # squid interface IP & port - $realif = array(); - $iface = ($squid_settings['active_interface'] ? $squid_settings['active_interface'] : 'lan'); - $iface = explode(",", $iface); - foreach ($iface as $i => $if) { - $realif[] = sqstat_get_real_interface_address($if); - $iface = $realif[$i][0] ? $realif[$i][0] : '127.0.0.1'; - } - $iport = $squid_settings['proxy_port'] ? $squid_settings['proxy_port'] : 3128; - } - $squidclass->squidhost = $iface; - $squidclass->squidport = $iport; - - $squidclass->group_by = "host"; - $squidclass->resolveip = true; - $squidclass->hosts_file = ''; # hosts file not used - $squidclass->autorefresh = 3; # refresh 3 sec by default - $squidclass->cachemgr_passwd = ''; - - # load hosts file, if defined - if (!empty($squidclass->hosts_file)) { - $squidclass->load_hosts(); - } - - return $squidclass->errno; +function sqstat_loadconfig() { + global $squidclass, $config; + + $squidclass->errno = 0; + $squidclass->errstr = ''; + + $squidclass->sqstat_version = SQSTAT_VERSION; + + /* Load config from pfSense */ + $iface = '127.0.0.1'; + $iport = 3128; + if (is_array($config['installedpackages']['squid']['config'][0])) { + $squid_settings = $config['installedpackages']['squid']['config'][0]; + } else { + $squid_settings = array(); + } + + // Squid interface IP & port + $realif = array(); + $iface = ($squid_settings['active_interface'] ? $squid_settings['active_interface'] : 'lan'); + $iface = explode(",", $iface); + foreach ($iface as $i => $if) { + $realif[] = sqstat_get_real_interface_address($if); + $iface = $realif[$i][0] ? $realif[$i][0] : '127.0.0.1'; + } + $iport = $squid_settings['proxy_port'] ? $squid_settings['proxy_port'] : 3128; + + $squidclass->squidhost = $iface; + $squidclass->squidport = $iport; + + $squidclass->group_by = "host"; + $squidclass->resolveip = true; + $squidclass->hosts_file = ''; // hosts file not used + $squidclass->autorefresh = 3; // refresh 3 secs by default + $squidclass->cachemgr_passwd = ''; + + // Load hosts file if defined + if (!empty($squidclass->hosts_file)) { + $squidclass->load_hosts(); + } + + return $squidclass->errno; } -function sqstat_get_real_interface_address($iface) -{ +function sqstat_get_real_interface_address($iface) { global $config; $iface = convert_friendly_interface_to_real_interface_name($iface); -- cgit v1.2.3