This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
phptsmadmin/includes/kohana/system/classes/Kohana/URL.php

214 lines
5.9 KiB
PHP
Raw Normal View History

2012-11-22 03:25:06 +00:00
<?php defined('SYSPATH') OR die('No direct script access.');
2011-01-13 14:49:56 +00:00
/**
* URL helper class.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
2012-11-22 03:25:06 +00:00
* @copyright (c) 2007-2012 Kohana Team
2011-01-13 14:49:56 +00:00
* @license http://kohanaframework.org/license
*/
class Kohana_URL {
/**
2011-05-16 12:47:16 +00:00
* Gets the base URL to the application.
* To specify a protocol, provide the protocol as a string or request object.
2011-01-13 14:49:56 +00:00
* If a protocol is used, a complete URL will be generated using the
* `$_SERVER['HTTP_HOST']` variable.
*
2011-05-16 12:47:16 +00:00
* // Absolute URL path with no host or protocol
2011-01-13 14:49:56 +00:00
* echo URL::base();
*
2011-05-16 12:47:16 +00:00
* // Absolute URL path with host, https protocol and index.php if set
* echo URL::base('https', TRUE);
2011-01-13 14:49:56 +00:00
*
2011-05-16 12:47:16 +00:00
* // Absolute URL path with host and protocol from $request
* echo URL::base($request);
2011-01-13 14:49:56 +00:00
*
2011-05-16 12:47:16 +00:00
* @param mixed $protocol Protocol string, [Request], or boolean
* @param boolean $index Add index file to URL?
2011-01-13 14:49:56 +00:00
* @return string
* @uses Kohana::$index_file
2011-05-16 12:47:16 +00:00
* @uses Request::protocol()
2011-01-13 14:49:56 +00:00
*/
2011-05-16 12:47:16 +00:00
public static function base($protocol = NULL, $index = FALSE)
2011-01-13 14:49:56 +00:00
{
// Start with the configured base URL
$base_url = Kohana::$base_url;
if ($protocol === TRUE)
2011-05-16 12:47:16 +00:00
{
// Use the initial request to get the protocol
$protocol = Request::$initial;
}
if ($protocol instanceof Request)
2011-01-13 14:49:56 +00:00
{
2012-11-22 03:25:06 +00:00
if ( ! $protocol->secure())
{
// Use the current protocol
list($protocol) = explode('/', strtolower($protocol->protocol()));
}
else
{
$protocol = 'https';
}
2011-01-13 14:49:56 +00:00
}
2011-05-16 12:47:16 +00:00
if ( ! $protocol)
2011-01-13 14:49:56 +00:00
{
// Use the configured default protocol
2011-05-16 12:47:16 +00:00
$protocol = parse_url($base_url, PHP_URL_SCHEME);
2011-01-13 14:49:56 +00:00
}
if ($index === TRUE AND ! empty(Kohana::$index_file))
{
// Add the index file to the URL
$base_url .= Kohana::$index_file.'/';
}
if (is_string($protocol))
{
if ($port = parse_url($base_url, PHP_URL_PORT))
{
// Found a port, make it usable for the URL
$port = ':'.$port;
}
if ($domain = parse_url($base_url, PHP_URL_HOST))
{
// Remove everything but the path from the URL
$base_url = parse_url($base_url, PHP_URL_PATH);
}
else
{
// Attempt to use HTTP_HOST and fallback to SERVER_NAME
$domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
}
// Add the protocol and domain to the base URL
$base_url = $protocol.'://'.$domain.$port.$base_url;
}
return $base_url;
}
/**
* Fetches an absolute site URL based on a URI segment.
*
* echo URL::site('foo/bar');
*
2011-05-16 12:47:16 +00:00
* @param string $uri Site URI to convert
* @param mixed $protocol Protocol string or [Request] class to use protocol from
* @param boolean $index Include the index_page in the URL
2011-01-13 14:49:56 +00:00
* @return string
* @uses URL::base
*/
2011-05-16 12:47:16 +00:00
public static function site($uri = '', $protocol = NULL, $index = TRUE)
2011-01-13 14:49:56 +00:00
{
// Chop off possible scheme, host, port, user and pass parts
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
if ( ! UTF8::is_ascii($path))
{
// Encode all non-ASCII characters, as per RFC 1738
2012-11-22 03:25:06 +00:00
$path = preg_replace_callback('~([^/]+)~', 'URL::_rawurlencode_callback', $path);
2011-01-13 14:49:56 +00:00
}
// Concat the URL
2011-05-16 12:47:16 +00:00
return URL::base($protocol, $index).$path;
2011-01-13 14:49:56 +00:00
}
2012-11-22 03:25:06 +00:00
/**
* Callback used for encoding all non-ASCII characters, as per RFC 1738
* Used by URL::site()
*
* @param array $matches Array of matches from preg_replace_callback()
* @return string Encoded string
*/
protected static function _rawurlencode_callback($matches)
{
return rawurlencode($matches[0]);
}
2011-01-13 14:49:56 +00:00
/**
* Merges the current GET parameters with an array of new or overloaded
* parameters and returns the resulting query string.
*
* // Returns "?sort=title&limit=10" combined with any existing GET values
* $query = URL::query(array('sort' => 'title', 'limit' => 10));
*
* Typically you would use this when you are sorting query results,
* or something similar.
*
* [!!] Parameters with a NULL value are left out.
*
2011-05-16 12:47:16 +00:00
* @param array $params Array of GET parameters
* @param boolean $use_get Include current request GET parameters
2011-01-13 14:49:56 +00:00
* @return string
*/
public static function query(array $params = NULL, $use_get = TRUE)
{
if ($use_get)
{
if ($params === NULL)
{
// Use only the current parameters
$params = $_GET;
}
else
{
// Merge the current and new parameters
2012-11-22 03:25:06 +00:00
$params = Arr::merge($_GET, $params);
2011-01-13 14:49:56 +00:00
}
}
if (empty($params))
{
// No query parameters
return '';
}
// Note: http_build_query returns an empty string for a params array with only NULL values
$query = http_build_query($params, '', '&');
// Don't prepend '?' to an empty string
return ($query === '') ? '' : ('?'.$query);
}
/**
* Convert a phrase to a URL-safe title.
*
* echo URL::title('My Blog Post'); // "my-blog-post"
*
2011-05-16 12:47:16 +00:00
* @param string $title Phrase to convert
* @param string $separator Word separator (any single character)
* @param boolean $ascii_only Transliterate to ASCII?
2011-01-13 14:49:56 +00:00
* @return string
* @uses UTF8::transliterate_to_ascii
*/
public static function title($title, $separator = '-', $ascii_only = FALSE)
{
if ($ascii_only === TRUE)
{
// Transliterate non-ASCII characters
$title = UTF8::transliterate_to_ascii($title);
// Remove all characters that are not the separator, a-z, 0-9, or whitespace
$title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
}
else
{
// Remove all characters that are not the separator, letters, numbers, or whitespace
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
}
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
// Trim separators from the beginning and end
return trim($title, $separator);
}
2012-11-22 03:25:06 +00:00
} // End url