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

125 lines
3.9 KiB
PHP

<?php
// File name: parseurl.inc.php
// Description: PHP subroutine to parse URL according to RFC 1808
// Date: 2004-12-05
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2004-2010 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/errhndl.inc.php";
// parse_url_rfc1808: Our own parse_url(), in order to confirm to RFC 1808
function parse_url_rfc1808($url)
{
// Try PHP parse_url() first
$GLOBALS["php_errormsg"] = null;
set_error_handler("null_error_handler");
$url_php = parse_url($url);
restore_error_handler();
// Error parsing the URL
if (!is_null($GLOBALS["php_errormsg"])) {
return null;
}
// Compose the network location/login (net_loc)
if (!is_array($url_php)) {
$url_php = array();
}
if (array_key_exists("host", $url_php)) {
$net_loc = $url_php["host"];
// Add the user and password if they exists
if (array_key_exists("user", $url_php)) {
if (array_key_exists("pass", $url_php)) {
$net_loc = $url_php["user"] . ":" . $url_php["pass"] . "@" . $net_loc;
} else {
$net_loc = $url_php["user"] . "@" . $net_loc;
}
}
// Add the port
if (array_key_exists("port", $url_php)) {
$need_port = true;
// Skip the default port
if (array_key_exists("scheme", $url_php)) {
switch (strtolower($url_php["scheme"])) {
case "http":
if ($url_php["port"] == 80) {
$need_port = false;
}
break;
case "https":
if ($url_php["port"] == 443) {
$need_port = false;
}
break;
case "ftp":
if ($url_php["port"] == 21) {
$need_port = false;
}
break;
case "ftps":
if ($url_php["port"] == 990) {
$need_port = false;
}
break;
case "gopher":
if ($url_php["port"] == 70) {
$need_port = false;
}
break;
case "prospero":
if ($url_php["port"] == 191) {
$need_port = false;
}
break;
case "wais":
if ($url_php["port"] == 210) {
$need_port = false;
}
break;
}
}
// We need to specify the port
if ($need_port) {
$net_loc .= ":" . $url_php["port"];
}
}
}
// Seperate the params from the path
if (array_key_exists("path", $url_php)) {
$pos = strpos($url_php["path"], ";");
if ($pos !== false) {
$url_php["params"] = substr($url_php["path"], $pos+1);
$url_php["path"] = substr($url_php["path"], 0, $pos);
}
}
// Compose the URL parts by RFC 1808
$url = array();
if (array_key_exists("scheme", $url_php)) {
$url["scheme"] = $url_php["scheme"];
}
if (isset($net_loc)) {
$url["net_loc"] = $net_loc;
}
if (array_key_exists("path", $url_php)) {
$url["path"] = $url_php["path"];
}
if (array_key_exists("params", $url_php)) {
$url["params"] = $url_php["params"];
}
if (array_key_exists("query", $url_php)) {
$url["query"] = $url_php["query"];
}
if (array_key_exists("fragment", $url_php)) {
$url["fragment"] = $url_php["fragment"];
}
return $url;
}
?>