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

217 lines
7.5 KiB
PHP

<?php
// File name: requri.inc.php
// Description: PHP subroutines to set the Request-URI variables
// Date: 2002-04-12
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2002-2008 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/cgiemu.inc.php";
require_once "monica/https.inc.php";
require_once "monica/parseurl.inc.php";
require_once "monica/server.inc.php";
// Initialize it
if (!defined("REQUEST_URI")) {
_requri_init();
}
// _requri_init: Obtain the REQUEST_URI
function _requri_init()
{
// Get the name of the script
// The last calling stack
$callers = debug_backtrace();
define("THIS_FILE", basename($callers[count($callers)-1]["file"]));
// Build the document root
// config.inc.php should always be put at /admin/lib/php/config.inc.php
if (defined("CONFIG_INC")) {
if (substr(CONFIG_INC, -29) == "/admin/lib/php/config.inc.php") {
define("DOC_ROOT", substr(CONFIG_INC, 0, -29));
}
}
if (!defined("DOC_ROOT")) {
define("DOC_ROOT", $_SERVER["DOCUMENT_ROOT"]);
}
// Find the root difference
// Apache's implementation
if (is_apache()) {
// Both mod_php5 and CGI use SCRIPT_FILENAME now
$file = $_SERVER["SCRIPT_FILENAME"];
// Microsoft IIS' implementation
} elseif (is_iis()) {
$file = $_SERVER["PATH_TRANSLATED"];
}
if (isset($file)) {
$len = strlen(DOC_ROOT);
if (substr($file, 0, $len) == DOC_ROOT) {
$script = substr($file, $len);
}
}
if (isset($script)) {
$len = strlen($script);
// Both mod_php5 and CGI use SCRIPT_NAME now
$script_name = is_apache()? $_SERVER["SCRIPT_NAME"]:
$_SERVER["PATH_INFO"];
if (substr($script_name, -$len) == $script) {
define("ROOT_DIFF", substr($script_name, 0, -$len));
} elseif (substr($script_name, -($len-4)) . ".php" == $script) {
define("ROOT_DIFF", substr($script_name, 0, -($len-4)));
}
}
// Assume no root difference if that is not available
if (!defined("ROOT_DIFF")) {
define("ROOT_DIFF", "");
}
// Build the REQUEST_PATH first
// Apache REQUEST_URI exists. Use it for simplicity and accuracy.
if (array_key_exists("REQUEST_URI", $_SERVER)) {
define("REQUEST_URI", $_SERVER["REQUEST_URI"]);
// If $_SERVER["REQUEST_URI"] contains a ":" somewhere (in query,
// for example), PHP's parse_url() will treat it as a port seperator
// and make a wrong parse result. We prevent this by adding a dummy
// scheme and net_loc to avoid this kinds of mistakes.
if (substr(REQUEST_URI, 0, 1) == "/") {
$url = parse_url_rfc1808("http://somewhere" . REQUEST_URI);
unset($url["scheme"]);
unset($url["net_loc"]);
} else {
$url = parse_url_rfc1808(REQUEST_URI);
}
$path = array_key_exists("path", $url)? $url["path"]: "/";
// Construct the REQUEST_PATH from scratches
// Avoid it whenever possible, since its result is not always right,
// especially for directory indices, like index.php.
} else {
$path = $_SERVER["SCRIPT_NAME"];
if ( !_requri_path_info_is_broken()
&& array_key_exists("PATH_INFO", $_SERVER)
&& $_SERVER["PATH_INFO"] != "") {
$path .= $_SERVER["PATH_INFO"];
}
$url = array();
}
$len = strlen(ROOT_DIFF);
define("REQUEST_PATH", $path == ""? "/":
(substr($path, 0, $len) == ROOT_DIFF?
substr($path, $len): $path));
// Set the REQUEST_FILE from REQUEST_PATH
$file = preg_replace("/^.*\//", "", REQUEST_PATH);
// Set to "." for directories, since we do not know what
// exact file name it should be
define("REQUEST_FILE", ($file == "")? ".": $file);
// Construct the REQUEST_URI
// REQUEST_URI is raw. All arguments are kept.
if (!defined("REQUEST_URI")) {
define("REQUEST_URI", REQUEST_PATH
. ((array_key_exists("QUERY_STRING", $_SERVER)
&& $_SERVER["QUERY_STRING"] != "")?
"?" . $_SERVER["QUERY_STRING"]: ""));
}
// Strip the unwanted arguments from the query string
$query = "";
if ( array_key_exists("QUERY_STRING", $_SERVER)
&& $_SERVER["QUERY_STRING"] != "") {
$query = _requri_clean_qs($_SERVER["QUERY_STRING"]);
}
// Construct the REQUEST_URI
define("REQUEST_FILEQS", REQUEST_FILE . $query);
// Construct the REQUEST_FULLURI, with the scheme and the host name
define("REQUEST_SCHEME", array_key_exists("scheme", $url)? $url["scheme"]:
(is_https()? "https": "http"));
if ( array_key_exists("net_loc", $url)) {
$net_loc = $url["net_loc"];
define("REQUEST_HOST", preg_replace("/:\d+$/", "", $net_loc));
} else {
define("REQUEST_HOST", array_key_exists("HTTP_HOST", $_SERVER)?
preg_replace("/:\d+$/", "", $_SERVER["HTTP_HOST"]):
$_SERVER["SERVER_NAME"]);
$net_loc = REQUEST_HOST;
if (is_https()) {
if ($_SERVER["SERVER_PORT"] != 443) {
$net_loc .= ":" . $_SERVER["SERVER_PORT"];
}
} else {
if ($_SERVER["SERVER_PORT"] != 80) {
$net_loc .= ":" . $_SERVER["SERVER_PORT"];
}
}
}
define("REQUEST_HOSTPORT", REQUEST_SCHEME . "://$net_loc" . ROOT_DIFF);
define("REQUEST_HOSTPATH", REQUEST_HOSTPORT . REQUEST_PATH);
if (substr(REQUEST_URI, 0, 1) != "/") {
define("REQUEST_FULLURI", REQUEST_URI);
} else {
define("REQUEST_FULLURI", REQUEST_SCHEME . "://$net_loc" . REQUEST_URI);
}
// Construct the cononical server name
if (!defined("CANONICAL_SERVER_NAME")) {
$host = (is_https()? "https": "http") . "://"
. preg_replace("/:\d+$/", "", $_SERVER["SERVER_NAME"]);
if (is_https()) {
if ($_SERVER["SERVER_PORT"] != 443) {
$host .= ":" . $_SERVER["SERVER_PORT"];
}
} else {
if ($_SERVER["SERVER_PORT"] != 80) {
$host .= ":" . $_SERVER["SERVER_PORT"];
}
}
define("CANONICAL_SERVER_NAME", $host);
}
return;
}
// _requri_clean_qs: Remove "lang" and session_name() from the query string
function _requri_clean_qs($qs)
{
$args = explode("&", $qs);
for ($i = 0; $i < count($args); $i++) {
// Only check well-formed query strings
if (preg_match("/^([^=]*)=(.*)$/", $args[$i], $m)) {
$name = urldecode($m[1]);
$val = urldecode($m[2]);
switch ($name) {
case "lang":
case session_name():
$args = array_merge(
array_slice($args, 0, $i),
array_slice($args, $i + 1)
);
$i--;
break;
default:
$args[$i] = rawurlencode($name) . "=" . rawurlencode($val);
break;
}
} else {
$args[$i] = rawurlencode(urldecode($args[$i]));
}
}
if (count($args) == 0) {
return "";
}
return "?" . implode("&", $args);
}
// _requri_path_info_is_broken: If PATH_INFO is broken
function _requri_path_info_is_broken()
{
// PATH_INFO is broken in Microsoft-IIS
return is_iis();
}
?>