Files
selima-perl/lib/php/monica/http.inc.php
2026-03-10 21:31:43 +08:00

1131 lines
38 KiB
PHP

<?php
// File name: http.inc.php
// Description: PHP subroutines to display custom HTTP status messages
// Date: 2001-02-13
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2001-2008 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/a2html.inc.php";
require_once "monica/addget.inc.php";
require_once "monica/cgiemu.inc.php";
require_once "monica/chkpriv.inc.php";
require_once "monica/curtime.inc.php";
require_once "monica/altlang.inc.php";
require_once "monica/getlang.inc.php";
require_once "monica/gettext.inc.php";
require_once "monica/geoip.inc.php";
require_once "monica/hires.inc.php";
require_once "monica/htmlchar.inc.php";
require_once "monica/lninfo.inc.php";
require_once "monica/login.inc.php";
require_once "monica/mail.inc.php";
require_once "monica/page2rel.inc.php";
require_once "monica/pagefunc.inc.php";
require_once "monica/rel2abs.inc.php";
require_once "monica/requri.inc.php";
require_once "monica/unicode.inc.php";
// Settings
$_HTTP_ERROR_TYPES = array (
E_ERROR => "Error",
E_WARNING => "Warning",
E_PARSE => "Parsing Error",
E_NOTICE => "Notice",
E_CORE_ERROR => "Core Error",
E_CORE_WARNING => "Core Warning",
E_COMPILE_ERROR => "Compile Error",
E_COMPILE_WARNING => "Compile Warning",
E_USER_ERROR => "User Error",
E_USER_WARNING => "User Warning",
E_USER_NOTICE => "User Notice",
E_STRICT => "Runtime Notice",
E_RECOVERABLE_ERROR => "Catchable Fatal Error",
);
// http_204: HTTP 204 No Content
function http_204()
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Output the status message
header("HTTP/1.1 204 No Content");
header("Content-Length: 0");
// Client cache
if (array_key_exists("LAST_MODIFIED", $GLOBALS)) {
if (!is_null(get_login_sn())) {
header("Cache-Control: private");
} else {
header("Cache-Control: public");
}
header("Last-Modified: " . date("r", $GLOBALS["LAST_MODIFIED"]));
} else {
header("Cache-Control: no-cache");
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_301: HTTP 301 Moved Permanently
function http_301($url)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
$url = rel2abs($url);
$url = _http_uri_add_sid($url);
// Obtain the status message
$html = _http_get_custom_status_message(301);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>301 Moved Permanently</title>
</head>
<body>
<h1>301 Moved Permanently</h1>
<p>You should refer to <a href="$url">the new location</a>.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("\$url", h($url), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_303: HTTP 303 See Others
function http_303($url)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
$url = rel2abs($url);
$url = _http_uri_add_sid($url);
// Obtain the status message
$html = _http_get_custom_status_message(303);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>303 See Others</title>
</head>
<body>
<h1>303 See Others</h1>
<p>Please follow <a href="$url">this location</a>.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("\$url", h($url), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
if (_http_need_302()) {
header("HTTP/1.1 302 Found");
} else {
header("HTTP/1.1 303 See Others");
}
header("Location: $url");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_304: HTTP 304 Not Modified
function http_304()
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Output the status message
// See HTTP/1.1 sec 10.3.5 for appropriate headers to send
header("HTTP/1.1 304 Not Modified");
// We must send them
if (count($GLOBALS["ALL_LINGUAS"]) > 1) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_307: HTTP 307 Temporary Redirect
function http_307($url)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
$url = rel2abs($url);
$url = _http_uri_add_sid($url);
// Obtain the status message
$html = _http_get_custom_status_message(307);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>307 Temporary Redirect</title>
</head>
<body>
<h1>307 Temporary Redirect</h1>
<p>Please refer to <a href="$url">the following location</a>.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("\$url", h($url), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
if (_http_need_302()) {
header("HTTP/1.1 302 Found");
} else {
header("HTTP/1.1 307 Temporary Redirect");
}
header("Location: $url");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_400: HTTP 400 Bad Request
// False to disable error page output
function http_400($errmsg = null)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// No error page output
if ($errmsg === false) {
$html = "";
} else {
// Obtain the status message
$html = _http_get_custom_status_message(400);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>400 Bad Request</title>
</head>
<body>
<h1>400 Bad Request</h1>
<!-- errmsg -->
<p>Sorry, there is some error in your request.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("<!-- errmsg -->", is_null($errmsg)? "": h(C_($errmsg)), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
}
// Output the status message
header("HTTP/1.1 400 Bad Request");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log the error message to the server error log
if (is_null($errmsg)) {
error_log("Bad Request: " . REQUEST_URI);
} elseif ($errmsg === false) {
$request = $_SERVER["REQUEST_METHOD"] . " "
. $_SERVER["REQUEST_URI"] . " " . $_SERVER["SERVER_PROTOCOL"];
error_log("Invalid URI in request $request");
} else {
error_log("Bad Request: " . REQUEST_URI . ": " . $errmsg);
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_403: HTTP 403 Forbidden
function http_403($errmsg = null)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// No error page output
if ($errmsg === false) {
$html = "";
} else {
// Obtain the status message
$html = _http_get_custom_status_message(403);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>403 Forbidden</title>
</head>
<body>
<h1>403 Forbidden</h1>
<!-- errmsg -->
<p>You are not allowed to enter here.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("<!-- errmsg -->", is_null($errmsg)? "": h(C_($errmsg)), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
}
// Output the status message
header("HTTP/1.1 403 Forbidden");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log the error message to the server error log
if (is_null($errmsg)) {
error_log("Forbidden: " . REQUEST_URI);
} else {
error_log("Forbidden: " . REQUEST_URI . ": " . $errmsg);
}
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_404: HTTP 404 Not Found
function http_404()
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Obtain the status message
$html = _http_get_custom_status_message(404);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
<p>The document you requested was not found.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 404 Not Found");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log the error message to the server error log
error_log("File does not exist: " . REQUEST_URI);
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_405: HTTP 405 Method Not Allowed
function http_405($allowed)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
$allowed = implode(", ", $allowed);
// Obtain the status message
$html = _http_get_custom_status_message(405);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>405 Method Not Allowed</title>
</head>
<body>
<h1>405 Method Not Allowed</h1>
<p>You should use the following methods: $allowed.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("\$allowed", h($allowed), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 405 Method Not Allowed");
header("Allow: $allowed");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log the error message to the server error log
error_log("Method not allowed (" . $_SERVER["REQUEST_METHOD"] . "): " . REQUEST_URI);
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_410: HTTP 410 Gone
function http_410()
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Obtain the status message
$html = _http_get_custom_status_message(410);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>410 Gone</title>
</head>
<body>
<h1>410 Gone</h1>
<p>Sorry, this page is removed permanently, and there is no forwarding address available. Please remove your bookmarks, and stop referring to this page any more. Thanks.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 410 Gone");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log error message into server error log
error_log("Gone: " . REQUEST_URI);
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_413: HTTP 413 Request Entity Too Large
function http_413()
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Obtain the status message
$html = _http_get_custom_status_message(413);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>413 Request Entity Too Large</title>
</head>
<body>
<h1>413 Request Entity Too Large</h1>
<p>The server refuses to process your large submitted data.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 413 Request Entity Too Large");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log error message into server error log
error_log("Request Entity Too Large: " . REQUEST_URI);
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_500: HTTP 500 Internal Server Error
function http_500($errmsg)
{
// Do not call the error handler recursively
if (defined("_HTTP_HTTP_500_IN_PROGRESS")) {
return;
}
define("_HTTP_HTTP_500_IN_PROGRESS", true);
// Find out our context
$trace = debug_backtrace();
// Obtain the caller information
for ($i = 0, $callers = array(); $i < count($trace); $i++) {
if (!array_key_exists("file", $trace[$i]) || !array_key_exists("line", $trace[$i])) {
continue;
}
$callers[] = $trace[$i];
}
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Get the environment infomation needed for error reporting
// Get the request information
$request = array();
foreach (array_keys($_SERVER) as $name) {
if (substr($name, 0, 5) == "HTTP_") {
$name1 = substr($name, 5);
$name1 = strtolower($name1);
$name1 = str_replace("_", " ", $name1);
$name1 = ucwords($name1);
$name1 = str_replace(" ", "-", $name1);
$request[$name1] = $_SERVER[$name];
}
}
if (!array_key_exists("Referer", $request)) {
$request["Referer"] = "(not set)";
}
if (!array_key_exists("Accept-Language", $request)) {
$request["Accept-Language"] = "(not set)";
}
if (!array_key_exists("User-Agent", $request)) {
$request["User-Agent"] = "(not set)";
}
// Mail the error message to the webmaster
if (!defined("MAIL_ERROR") || MAIL_ERROR) {
// Get the webmaster address to notify to
if (defined("WEBMASTER")) {
$webmaster = WEBMASTER;
} elseif (array_key_exists("SERVER_ADMIN", $_SERVER)) {
$webmaster = $_SERVER["SERVER_ADMIN"];
} else {
$webmaster = "webmaster@" . $_SERVER["SERVER_NAME"];
}
// Get the site name
$site = (defined("SITENAME_ABBR")? SITENAME_ABBR:
(defined("PACKAGE")? PACKAGE:
REQUEST_HOST));
$body = "";
// Basic information
ob_start();
?>[%s] HTTP 500 Server Error Report %s
==============================
* Time: %s
* URI: %s
* Request: %s %s %s
* Error Message:
%s
%s.
<?php
$part = ob_get_contents();
ob_end_clean();
for ($i = 0, $trace = array(); $i < count($callers); $i++) {
$trace[] = " at " . $callers[$i]["file"] . " line " . $callers[$i]["line"];
}
$body .= sprintf($part, $site, TODAY,
date("Y-m-d H:i:s", NOW), REQUEST_FULLURI,
$_SERVER["REQUEST_METHOD"], REQUEST_URI,
$_SERVER["SERVER_PROTOCOL"], $errmsg, implode("\n", $trace));
// Client headers
$body .= "Client Headers:\n";
$keys = array_keys($request);
sort($keys);
foreach ($keys as $key) {
$body .= "* " . $key . ": " . $request[$key] . "\n";
}
$body .= "\n";
// POSTed form
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$body .= "POST form:\n";
if (array_key_exists("USER_INPUT", $GLOBALS)) {
$FORM =& $GLOBALS["USER_INPUT"]["POST_RAW"];
} else {
$FORM =& $_POST;
}
$cols = array_keys($FORM);
sort($cols);
foreach ($cols as $col) {
$body .= "* " . $col . ": " . $FORM[$col] . "\n";
}
$body .= "\n";
}
// CGI metavariables - refer to CGI 1.1
$cgivars = array("AUTH_TYPE", "CONTENT_LENGTH", "CONTENT_TYPE",
"GATEWAY_INTERFACE", "PATH_INFO", "PATH_TRANSLATED",
"QUERY_STRING", "REMOTE_ADDR", "REMOTE_HOST", "REMOTE_IDENT",
"REMOTE_USER", "REQUEST_METHOD", "SCRIPT_NAME", "SERVER_NAME",
"SERVER_PORT", "SERVER_PROTOCOL", "SERVER_SOFTWARE");
$body .= "CGI Metavariables:\n";
foreach ($cgivars as $var) {
if (array_key_exists($var, $_SERVER)) {
$body .= "* " . $var . ": " . $_SERVER[$var] . "\n";
}
}
$body .= "\n";
// Other information
$body .= "Other information:\n";
if (!is_null(get_login_sn())) {
$body .= "* User ID.: " . get_login_id() . "\n";
$body .= "* User name: " . get_login_name() . "\n";
$body .= "* User S/N: " . get_login_sn() . "\n";
$body .= "* User groups: " . implode(" ", get_login_groups()) . "\n";
} else {
$body .= "* User ID.: (not logged in)\n";
}
$body .= "* Client country: " . geoiplookup() . "\n";
$body .= "\n";
// Compose and send the mail
$mail = new Mail();
$mail->from("monica-http500@" . $_SERVER["SERVER_NAME"], "$site Website");
$mail->to($webmaster, "$site Webmaster");
$mail->subject(sprintf("[%s] HTTP 500 Server Error Report %s", $site, TODAY));
$mail->body($body);
$mail->send();
}
// Obtain the status message
$html = _http_get_custom_status_message(500);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>500 Internal Server Error</title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<!-- errmsg -->
</div>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
if (ini_get("display_errors") || is_admin()) {
for ($i = 0, $trace = array(); $i < count($callers); $i++) {
$trace[] = "at <samp>" . h($callers[$i]["file"]) . "</samp> line " . h($callers[$i]["line"]);
}
$html = str_replace("<!-- errmsg -->", "<div class=\"errmsg\">\n<samp>" . a2html($errmsg) . "</samp><br />\n" . implode("<br />\n", $trace) . ".\n</div>", $html);
} else {
$html = str_replace("<!-- errmsg -->", "", $html);
}
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 500 Internal Server Error");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log the error message to the server error log
for ($i = 0, $trace = array(); $i < count($callers); $i++) {
$trace[] = "at " . $callers[$i]["file"] . " line " . $callers[$i]["line"];
}
error_log("$errmsg " . implode(" ", $trace) . ".");
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http_503: HTTP 503 Service Unavailable
function http_503($errmsg = null)
{
// Stop and clean the previous output buffering
if (!defined("LAST_OB_LEVEL")) {
define("LAST_OB_LEVEL", (ini_get("output_buffering") == "")? 0: 1);
}
while (ob_get_level() > LAST_OB_LEVEL) {
ob_end_clean();
}
// Obtain the status message
$html = _http_get_custom_status_message(503);
if (is_null($html)) {
ob_start();
?><?xml version="1.0" encoding="US-ASCII" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=US-ASCII" />
<title>503 Service Unavailable</title>
</head>
<body>
<h1>503 Service Unavailable</h1>
<!-- errmsg -->
<p>Sorry, our service is currently not available. Please come back later.</p>
</body>
</html><?php
$html = ob_get_contents();
ob_end_clean();
}
// Replace the page content
$html = str_replace("<!-- errmsg -->", is_null($errmsg)? "": h($errmsg), $html);
// Convert the URLs to relative
$html = page2rel($html, REQUEST_PATH);
// Encode the e-mail at-signs (@)
$html = str_replace("@", "&#64;", $html);
// Decode the e-mail at-signs (@) of spamtrap
$html = str_replace("spamtrap&#64;", "spamtrap@", $html);
// Convert to the desired character set
$html = page_encode($html);
// Output the status message
header("HTTP/1.1 503 Service Unavailable");
header("Content-Language: " . getlang());
header("Content-Length: " . strlen($html));
// Content negotiation, see HTTP/1.1 section 13.6
if ( count($GLOBALS["ALL_LINGUAS"]) > 1
&& $_SERVER["REQUEST_METHOD"] != "POST"
&& $_SERVER["REQUEST_METHOD"] != "PUT"
&& !array_key_exists("lang", $_GET)) {
header("Content-Location: " . rel2abs(altlang(getlang(), page_param())));
header("Vary: accept-language,cookie");
}
if ($_SERVER["REQUEST_METHOD"] != "HEAD") {
echo $html;
}
// Log the error message to the server error log
// No logging due to maintainance
// Print the processing time for debugging purpose
if (defined("LOGTIME") && LOGTIME) {
error_log("Process time: " . (time_hires() - T_START) . " sec.");
}
// No need to return
exit;
}
// http500_error_handler: Standard error handler for Monica
function http500_error_handler($no, $message, $file, $line, $context)
{
$prefix = $no;
if (array_key_exists($no, $GLOBALS["_HTTP_ERROR_TYPES"])) {
$prefix .= " " . $GLOBALS["_HTTP_ERROR_TYPES"][$no];
}
http_500("$prefix: $message");
return;
}
// _http_need_302: Is this browser capable of HTTP 303 and HTTP 307?
// Currently only older Netscape without Gecko (version <= 4.xx)
// is known of this problem.
// Return:
// false: Standard behavior to send HTTP 303 or 307.
// true: Browser lacks the capability of redirecting
// HTTP 303 and 307. Send HTTP 302 instead.
function _http_need_302()
{
// Unable to identify User-Agent, default to the standard behavior.
if (!array_key_exists("HTTP_USER_AGENT", $_SERVER)) {
return false;
}
$ua = $_SERVER["HTTP_USER_AGENT"];
// Mozilla/Netscape series
if (preg_match("/^Mozilla\//", $ua)) {
// True Mozilla/Netscape
if (stristr($ua, "compatible") === false) {
// Netscape without Gecko (version <= 4.xx)
if (stristr($ua, "Gecko") === false) {
return true;
}
// Mozilla/Netscape with Gecko
return false;
}
// Netscape compatible
return false;
}
// Others
return false;
}
// _http_uri_add_sid: Add session ID into an URI
function _http_uri_add_sid($url)
{
// No session
if (!isset($_SESSION)) {
return $url;
}
// Session ID already set in the cookies
if (array_key_exists(session_name(), $_COOKIE)) {
return $url;
}
// Add the session ID to the GET argument list
return add_get_arg($url, session_name(), session_id());
}
// _http_get_custom_status_message: Obtain the custom status message
function _http_get_custom_status_message($status)
{
$langfile = getlang(LN_FILENAME);
unset($file);
if (is_file(DOC_ROOT . "/$langfile/errors/$status.html.xhtml")) {
$file = "/%s/errors/$status.html.xhtml";
} elseif (is_file(DOC_ROOT . "/$langfile/errors/$status.html")) {
$file = "/%s/errors/$status.html";
} elseif (is_file(DOC_ROOT . "/errors/$status.html.xhtml")) {
$file = "/errors/$status.html.xhtml";
} elseif (is_file(DOC_ROOT . "/errors/$status.html")) {
$file = "/errors/$status.html";
}
// Not found
if (!isset($file)) {
return null;
}
$html = readpage($file);
if (is_null($html)) {
return null;
}
$charset = h(getlang(LN_CHARSET));
$html = preg_replace("/(?<=\bencoding=\")$charset(?=\")/", "<!--monica:charset-->", $html);
$html = preg_replace("/(?<=\bcontent=\"text\/html; charset=)$charset(?=\")/", "<!--monica:charset-->", $html);
$html = preg_replace("/(?<=\bcontent=\"application\/xhtml\+xml; charset=)$charset(?=\")/", "<!--monica:charset-->", $html);
$html = preg_replace("/(?<=\btype=\"hidden\" name=\"charset\" value=\")$charset(?=\" \/>)/", "<!--monica:charset-->", $html);
$html = preg_replace("/(?<=\baccept-charset=\")$charset(?=\")/", "<!--monica:charset-->", $html);
return $html;
}
?>