summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjim-p <jimp@pfsense.org>2017-03-08 12:23:33 -0500
committerjim-p <jimp@pfsense.org>2017-03-08 12:28:23 -0500
commitdd5b7657a39537baa4c3a31de6269d6bcb44827a (patch)
treea523ba3414673922b3d2269fd7141364fb4c6825
parent7bc6d4cebf8ce1b0c73bd648699215e450068c7c (diff)
downloadFreeBSD-ports-dd5b7657a39537baa4c3a31de6269d6bcb44827a.zip
FreeBSD-ports-dd5b7657a39537baa4c3a31de6269d6bcb44827a.tar.gz
Rewrite/reformat sgerror.php for better style compliance.
-rw-r--r--www/pfSense-pkg-squidGuard/files/usr/local/www/sgerror.php440
1 files changed, 232 insertions, 208 deletions
diff --git a/www/pfSense-pkg-squidGuard/files/usr/local/www/sgerror.php b/www/pfSense-pkg-squidGuard/files/usr/local/www/sgerror.php
index 4730df1..823c699 100644
--- a/www/pfSense-pkg-squidGuard/files/usr/local/www/sgerror.php
+++ b/www/pfSense-pkg-squidGuard/files/usr/local/www/sgerror.php
@@ -2,63 +2,84 @@
include "globals.inc";
include "config.inc";
$page_info = <<<EOD
-# ----------------------------------------------------------------------------------------------------------------------
-# SquidGuard error page generator
-# (C)2006-2007 Serg Dvoriancev
-# ----------------------------------------------------------------------------------------------------------------------
-# This programm processed redirection to specified URL or generated error page for standart HTTP error code.
-# Redirection supported http and https protocols.
-# ----------------------------------------------------------------------------------------------------------------------
-# Format:
-# sgerror.php?url=[http://myurl]or[https://myurl]or[error_code[space_code]output-message][incoming SquidGuard variables]
-# Incoming SquidGuard variables:
-# a=client_address
-# n=client_name
-# i=client_user
-# s=client_group
-# t=target_group
-# u=client_url
-# Example:
-# sgerror.php?url=http://myurl.com&a=..&n=..&i=..&s=..&t=..&u=..
-# sgerror.php?url=https://myurl.com&a=..&n=..&i=..&s=..&t=..&u=..
-# sgerror.php?url=404%20output-message&a=..&n=..&i=..&s=..&t=..&u=..
-# ----------------------------------------------------------------------------------------------------------------------
-# Tags:
-# myurl and output messages can include Tags
-# [a] - client address
-# [n] - client name
-# [i] - client user
-# [s] - client group
-# [t] - target group
-# [u] - client url
-# Example:
-# sgerror.php?url=401 Unauthorized access to URL [u] for client [n]
-# sgerror.php?url=http://my_error_page.php?cladr=%5Ba%5D&clname=%5Bn%5D // %5b=[ %d=]
-# ----------------------------------------------------------------------------------------------------------------------
-# Special Tags:
-# blank - get blank page
-# blank_img - get one-pixel transparent image (for replace banners and etc.)
-# Example:
-# sgerror.php?url=blank
-# sgerror.php?url=blank_img
-# ----------------------------------------------------------------------------------------------------------------------
+/*
+ * sgerror.php
+ *
+ * part of pfSense (https://www.pfsense.org)
+ * Copyright (c) 2017 Rubicon Communications, LLC (Netgate)
+ * Copyright (c) 2006-2011 Serg Dvoriancev
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+----------------------------------------------------------------------------------------------------------------------
+SquidGuard error page generator
+----------------------------------------------------------------------------------------------------------------------
+This program processes redirection requests to specified URL or generated error page for a standard HTTP error code.
+Redirection supports HTTP and HTTPS protocols.
+----------------------------------------------------------------------------------------------------------------------
+Format:
+ sgerror.php?url=[http://myurl]or[https://myurl]or[error_code[space_code]output-message][incoming SquidGuard variables]
+Incoming SquidGuard variables:
+ a=client_address
+ n=client_name
+ i=client_user
+ s=client_group
+ t=target_group
+ u=client_url
+Example:
+ sgerror.php?url=http://myurl.com&a=..&n=..&i=..&s=..&t=..&u=..
+ sgerror.php?url=https://myurl.com&a=..&n=..&i=..&s=..&t=..&u=..
+ sgerror.php?url=404%20output-message&a=..&n=..&i=..&s=..&t=..&u=..
+----------------------------------------------------------------------------------------------------------------------
+Tags:
+ myurl and output messages can include Tags
+ [a] - client address
+ [n] - client name
+ [i] - client user
+ [s] - client group
+ [t] - target group
+ [u] - client url
+Example:
+ sgerror.php?url=401 Unauthorized access to URL [u] for client [n]
+ sgerror.php?url=http://my_error_page.php?cladr=%5Ba%5D&clname=%5Bn%5D // %5b=[ %d=]
+----------------------------------------------------------------------------------------------------------------------
+Special Tags:
+ blank - get blank page
+ blank_img - get one-pixel transparent image (to replace images such as banners, ads, etc.)
+Example:
+ sgerror.php?url=blank
+ sgerror.php?url=blank_img
+----------------------------------------------------------------------------------------------------------------------
EOD;
define('ACTION_URL', 'url');
define('ACTION_RES', 'res');
define('ACTION_MSG', 'msg');
-define('TAG_BLANK', 'blank');
+define('TAG_BLANK', 'blank');
define('TAG_BLANK_IMG', 'blank_img');
-# ----------------------------------------------------------------------------------------------------------------------
-# ?url=EMPTY_IMG
-# Use this options for replace baners/ads to transparent picture. Thisbetter for viewing.
-# ----------------------------------------------------------------------------------------------------------------------
-# NULL GIF file
-# HEX: 47 49 46 38 39 61 - - -
-# SYM: G I F 8 9 a 01 00 | 01 00 80 00 00 FF FF FF | 00 00 00 2C 00 00 00 00 | 01 00 01 00 00 02 02 44 | 01 00 3B
-# ----------------------------------------------------------------------------------------------------------------------
+/* ----------------------------------------------------------------------------------------------------------------------
+ * ?url=EMPTY_IMG
+ * Use this option to replace banners/ads with a transparent picture. This is better for web page rendering.
+ * ----------------------------------------------------------------------------------------------------------------------
+ * NULL GIF file
+ * HEX: 47 49 46 38 39 61 - - -
+ * SYM: G I F 8 9 a 01 00 | 01 00 80 00 00 FF FF FF | 00 00 00 2C 00 00 00 00 | 01 00 01 00 00 02 02 44 | 01 00 3B
+ * ----------------------------------------------------------------------------------------------------------------------
+ */
define(GIF_BODY, "GIF89a\x01\x00\x01\x00\x80\x00\x00\xFF\xFF\xFF\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B");
$url = '';
@@ -97,185 +118,188 @@ $err_code[503] = "503 Service Unavailable";
$err_code[504] = "504 Gateway Time-out";
$err_code[505] = "505 HTTP Version not supported";
-# ----------------------------------------------------------------------------------------------------------------------
-# check arg's
-# ----------------------------------------------------------------------------------------------------------------------
-
-if (count($_REQUEST)) {
- $url = trim($_REQUEST['url']);
- $msg = $_REQUEST['msg'];
- $cl['a'] = $_REQUEST['a'];
- $cl['n'] = $_REQUEST['n'];
- $cl['i'] = $_REQUEST['i'];
- $cl['s'] = $_REQUEST['s'];
- $cl['t'] = $_REQUEST['t'];
- $cl['u'] = $_REQUEST['u'];
-} else {
- # Show 'About page'
- echo get_page(get_about());
- exit();
+/* ----------------------------------------------------------------------------------------------------------------------
+ * Functions
+ * ----------------------------------------------------------------------------------------------------------------------
+ */
+function get_page($body) { ?>
+<html>
+ <head>
+ <title>squidGuard Error page</title>
+ </head>
+ <body>
+<?=$body?>
+ </body>
+</html>
+<?php
}
-# ----------------------------------------------------------------------------------------------------------------------
-# url's
-# ----------------------------------------------------------------------------------------------------------------------
-if ($url) {
- $err_id = 0;
+/*
+ * Generate an error page for the user
+ */
+function get_error_page($er_code_id, $err_msg='') {
+ global $g, $config, $err_code, $cl;
+ header("HTTP/1.1 " . $err_code[$er_code_id]);
- // check error code
- foreach ($err_code as $key => $val) {
- if (strpos(strtolower($url), strval($key)) === 0) {
- $err_id = $key;
- break;
- }
- }
+?>
+<html>
+ <head>
+ <title>squidGuard Error page</title>
+ </head>
+ <body>
+ <?php if ($config['installedpackages']['squidguarddefault']['config'][0]['deniedmessage']): ?>
+ <h3><?= $config['installedpackages']['squidguarddefault']['config'][0]['deniedmessage'] ?>: <?= htmlspecialchars($err_code[$er_code_id]) ?></h3>;
+ <?php else: ?>
+ <h3>Request denied by <?= $g['product_name'] ?> proxy: <?= htmlspecialchars($err_code[$er_code_id]) ?></h3>
+ <?php endif; ?>
- # blank page
- if ($url === TAG_BLANK) {
- echo get_page('');
- }
- # blank image
- elseif ($url === TAG_BLANK_IMG) {
- $msg = trim($msg);
- if(strpos($msg, "maxlen_") !== false) {
- $maxlen = intval(trim(str_replace("maxlen_", "", $url)));
- filter_by_image_size($cl['u'], $maxlen);
- exit();
- }
- else {
- # --------------------------------------------------------------
- # return blank image
- # --------------------------------------------------------------
- header("Content-Type: image/gif;"); // charset=windows-1251");
- echo GIF_BODY;
- }
- }
- # error code
- elseif ($err_id !== 0) {
- $er_msg = strstr($_GET['url'], ' ');
- echo get_error_page($err_id, $er_msg);
- }
- # redirect url
- elseif ((strpos(strtolower($url), "http://") === 0) or (strpos(strtolower($url), "https://") === 0)) {
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # redirect to specified url
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- header("HTTP/1.0");
- header("Location: $url", '', 302);
- }
- // error arguments
- else {
- echo get_page("sgerror: error arguments $url");
- }
-}
-else {
- echo get_page($_SERVER['QUERY_STRING']); //$url . implode(" ", $_GET));
-# echo get_error_page(500);
-}
+ <?php if ($err_msg): ?>
+ <b>Reason:</b> <?= $err_msg ?>
+ <?php endif; ?>
-# ~~~~~~~~~~
-# Exit
-# ~~~~~~~~~~
-exit();
+ <hr size="1" noshade>
+ <?php if ($cl['a']): ?>
+ <b> Client address: </b> <?= htmlspecialchars($cl['a']) ?><br/>
+ <?php endif; ?>
-# ----------------------------------------------------------------------------------------------------------------------
-# functions
-# ----------------------------------------------------------------------------------------------------------------------
-function get_page($body) {
- $str = Array();
- $str[] = '<html>';
- $str[] = "<body>\n$body\n</body>";
- $str[] = '</html>';
- return implode("\n", $str);
-}
+ <?php if ($cl['n']): ?>
+ <b> Client name: </b> <?= htmlspecialchars($cl['n']) ?><br/>
+ <?php endif; ?>
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-# IE displayed self-page, if them size > 1024
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-function get_error_page($er_code_id, $err_msg='') {
- global $err_code;
- global $cl;
- global $g;
- global $config;
- $str = Array();
+ <?php if ($cl['i']): ?>
+ <b> Client user: </b> <?= htmlspecialchars($cl['i']) ?><br/>
+ <?php endif; ?>
- header("HTTP/1.1 " . $err_code[$er_code_id]);
+ <?php if ($cl['s']): ?>
+ <b> Client group: </b> <?= htmlspecialchars($cl['s']) ?><br/>
+ <?php endif; ?>
- $str[] = '<html>';
- $str[] = '<body>';
- if ($config['installedpackages']['squidguarddefault']['config'][0]['deniedmessage']) {
- $str[] = "<h3>{$config['installedpackages']['squidguarddefault']['config'][0]['deniedmessage']}: " . htmlspecialchars($err_code[$er_code_id]) . "</h3>";
- } else {
- $str[] = "<h3>Request denied by {$g['product_name']} proxy: " . htmlspecialchars($err_code[$er_code_id]) . "</h3>";
- }
- if ($err_msg) $str[] = "<b> Reason: </b> $err_msg";
- $str[] = '<hr size="1" noshade>';
- if ($cl['a']) $str[] = "<b> Client address: </b> " . htmlspecialchars($cl['a']) . "<br>";
- if ($cl['n']) $str[] = "<b> Client name: </b> " . htmlspecialchars($cl['n']) . "<br>";
- if ($cl['i']) $str[] = "<b> Client user: </b> " . htmlspecialchars($cl['i']) . "<br>";
- if ($cl['s']) $str[] = "<b> Client group: </b> " . htmlspecialchars($cl['s']) . "<br>";
- if ($cl['t']) $str[] = "<b> Target group: </b> " . htmlspecialchars($cl['t']) . "<br>";
- if ($cl['u']) $str[] = "<b> URL: </b> " . htmlspecialchars($cl['u']) . "<br>";
- $str[] = '<hr size="1" noshade>';
- $str[] = "</body>";
- $str[] = "</html>";
+ <?php if ($cl['t']): ?>
+ <b> Target group: </b> <?= htmlspecialchars($cl['t']) ?><br/>
+ <?php endif; ?>
+
+ <?php if ($cl['u']): ?>
+ <b> URL: </b> <?= htmlspecialchars($cl['u']) ?><br/>
+ <?php endif; ?>
- return implode("\n", $str);
+ <hr size="1" noshade>
+ </body>
+</html>
+<?php
}
function get_about() {
- global $err_code;
- global $page_info;
- $str = Array();
+ global $err_code, $page_info; ?>
+<?= str_replace("\n", "<br/>", $page_info); ?>
+<br/>
+<table>
+ <tr><th><b>HTTP error codes (ERROR_CODE):</b></th></tr>
+ <?php foreach ($err_code as $val): ?>
+ <tr><td><?= htmlspecialchars($val) ?></td></tr>
+ <?php endforeach; ?>
+</table>
+<?php
+}
- // about info
- $s = str_replace("\n", "<br>", $page_info);
- $str[] = $s;
- $str[] = "<br>";
+function filter_by_image_size($url, $val_size) {
+ // Load URL header
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HEADER, 1);
+ curl_setopt($ch, CURLOPT_NOBODY, 1);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ $hd = curl_exec($ch);
+ curl_close($ch);
- $str[] = '<table>';
- $str[] = ' <b>HTTP error codes (ERROR_CODE):</th></tr>';
- foreach($err_code as $val) {
- $str []= "<tr><td>" . htmlspecialchars($val);
- }
- $str[] = '</table>';
+ $size = 0;
+ $SKEY = "content-length:";
+ $s_tmp = strtolower($hd);
+ $s_tmp = str_replace("\n", " ", $s_tmp); // replace all "\n"
+ if (strpos($s_tmp, $SKEY) !== false) {
+ $s_tmp = trim(substr($s_tmp, strpos($s_tmp, $SKEY) + strlen($SKEY)));
+ $s_tmp = trim(substr($s_tmp, 0, strpos($s_tmp, " ")));
+ if (is_numeric($s_tmp))
+ $size = intval($s_tmp);
+ else $size = 0;
+ }
- return implode("\n", $str);
+ /*
+ * check url type and content size
+ * redirect to specified url
+ */
+ if (($size !== 0) && ($size < $val_size)) {
+ header("HTTP/1.0");
+ header("Location: $url", '', 302);
+ } else {
+ // Returna blank image
+ header("Content-Type: image/gif;");
+ echo GIF_BODY;
+ }
}
-function filter_by_image_size($url, $val_size) {
+/* ----------------------------------------------------------------------------------------------------------------------
+ * Check arguments
+ * ----------------------------------------------------------------------------------------------------------------------
+ */
+if (count($_REQUEST)) {
+ $url = trim($_REQUEST['url']);
+ $msg = $_REQUEST['msg'];
+ $cl['a'] = $_REQUEST['a'];
+ $cl['n'] = $_REQUEST['n'];
+ $cl['i'] = $_REQUEST['i'];
+ $cl['s'] = $_REQUEST['s'];
+ $cl['t'] = $_REQUEST['t'];
+ $cl['u'] = $_REQUEST['u'];
+} else {
+ // Show 'About page'
+ echo get_page(get_about());
+ exit();
+}
- # load url header
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 1);
- curl_setopt($ch, CURLOPT_NOBODY, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $hd = curl_exec($ch);
- curl_close($ch);
+/* ----------------------------------------------------------------------------------------------------------------------
+ * Process URLs
+ * ----------------------------------------------------------------------------------------------------------------------
+ */
+if ($url) {
+ $err_id = 0;
- $size = 0;
- $SKEY = "content-length:";
- $s_tmp = strtolower($hd);
- $s_tmp = str_replace("\n", " ", $s_tmp); # replace all "\n"
- if (strpos($s_tmp, $SKEY) !== false) {
- $s_tmp = trim(substr($s_tmp, strpos($s_tmp, $SKEY) + strlen($SKEY)));
- $s_tmp = trim(substr($s_tmp, 0, strpos($s_tmp, " ")));
- if (is_numeric($s_tmp))
- $size = intval($s_tmp);
- else $size = 0;
- }
+ // Check error code
+ foreach ($err_code as $key => $val) {
+ if (strpos(strtolower($url), strval($key)) === 0) {
+ $err_id = $key;
+ break;
+ }
+ }
- # === check url type and content size ===
- # redirect to specified url
- if (($size !== 0) && ($size < $val_size)) {
- header("HTTP/1.0");
- header("Location: $url", '', 302);
- }
- # return blank image
- else {
- header("Content-Type: image/gif;");
- echo GIF_BODY;
- }
+ if ($url === TAG_BLANK) {
+ // Output a blank page
+ echo get_page('');
+ } elseif ($url === TAG_BLANK_IMG) {
+ // Output a blank image
+ $msg = trim($msg);
+ if (strpos($msg, "maxlen_") !== false) {
+ $maxlen = intval(trim(str_replace("maxlen_", "", $url)));
+ filter_by_image_size($cl['u'], $maxlen);
+ exit();
+ } else {
+ // Return a blank image
+ header("Content-Type: image/gif;"); // charset=windows-1251");
+ echo GIF_BODY;
+ }
+ } elseif ($err_id !== 0) {
+ // Output an error code
+ $er_msg = strstr($_GET['url'], ' ');
+ echo get_error_page($err_id, $er_msg);
+ } elseif ((strpos(strtolower($url), "http://") === 0) or (strpos(strtolower($url), "https://") === 0)) {
+ // Redirect to the specified url
+ header("HTTP/1.0");
+ header("Location: $url", '', 302);
+ } else {
+ // Output an error
+ echo get_page("sgerror: error arguments $url");
+ }
+} else {
+ echo get_page($_SERVER['QUERY_STRING']); //$url . implode(" ", $_GET));
+ // echo get_error_page(500);
}
?> \ No newline at end of file
OpenPOWER on IntegriCloud