mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-30 15:52:17 +09:00
Merge pull request #378 from kijin/pr/url-conversion
경로↔URL 변환 및 정리 함수 추가
This commit is contained in:
commit
c23a1949cc
14 changed files with 284 additions and 88 deletions
|
|
@ -46,4 +46,43 @@ class FilenameFilter
|
|||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean a path to remove ./, ../, trailing slashes, etc.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function cleanPath($path)
|
||||
{
|
||||
// Convert relative paths to absolute paths.
|
||||
if (!preg_match('@^(?:/|[a-z]:[\\\\/]|\\\\|https?:)@i', $path))
|
||||
{
|
||||
$path = \RX_BASEDIR . $path;
|
||||
}
|
||||
|
||||
// Convert backslashes to forward slashes.
|
||||
$path = str_replace('\\', '/', $path);
|
||||
|
||||
// Remove querystrings and URL fragments.
|
||||
if (($querystring = strpbrk($path, '?#')) !== false)
|
||||
{
|
||||
$path = substr($path, 0, -1 * strlen($querystring));
|
||||
}
|
||||
|
||||
// Remove single dots, three or more dots, and duplicate slashes.
|
||||
$path = preg_replace(array(
|
||||
'@(?<!^|^http:|^https:)/{2,}@',
|
||||
'@/(?:(?:\.|\.{3,})/)+@',
|
||||
), '/', $path);
|
||||
|
||||
// Remove double dots and the preceding directory.
|
||||
while (preg_match('@/(?!\.\.)[^/]+/\.\.(?:/|$)@', $path, $matches))
|
||||
{
|
||||
$path = str_replace($matches[0], '/', $path);
|
||||
}
|
||||
|
||||
// Trim trailing slashes.
|
||||
return rtrim($path, '/');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
common/framework/storage.php
Normal file
11
common/framework/storage.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
/**
|
||||
* The storage class.
|
||||
*/
|
||||
class Storage
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -18,10 +18,7 @@ class URL
|
|||
*/
|
||||
public static function getCurrentURL(array $changes = array())
|
||||
{
|
||||
$proto = \RX_SSL ? 'https://' : 'http://';
|
||||
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
|
||||
$local = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
|
||||
$url = $proto . $host . $local;
|
||||
$url = self::getCurrentDomainURL(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/');
|
||||
if (count($changes))
|
||||
{
|
||||
return self::modifyURL($url, $changes);
|
||||
|
|
@ -32,6 +29,19 @@ class URL
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current domain.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function getCurrentDomainURL($path = '/')
|
||||
{
|
||||
$proto = \RX_SSL ? 'https://' : 'http://';
|
||||
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
|
||||
return $proto . $host . '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a URL to its canonical format.
|
||||
*
|
||||
|
|
@ -42,9 +52,7 @@ class URL
|
|||
{
|
||||
if (preg_match('#^\.?/([^/]|$)#', $url) || !preg_match('#^(https?:|/)#', $url))
|
||||
{
|
||||
$proto = \RX_SSL ? 'https://' : 'http://';
|
||||
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
|
||||
$url = $proto . $host . \RX_BASEURL . ltrim($url, './');
|
||||
$url = self::getCurrentDomainURL(\RX_BASEURL . ltrim($url, './'));
|
||||
}
|
||||
return preg_replace_callback('#^(https?:|)//([^/]+)#i', function($matches) {
|
||||
if ($matches[1] === '') $matches[1] = \RX_SSL ? 'https:' : 'http:';
|
||||
|
|
@ -125,6 +133,53 @@ class URL
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a server-side path to a URL.
|
||||
*
|
||||
* This method returns false if the path cannot be converted to a URL,
|
||||
* e.g. if the path is outside of the document root.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string|false
|
||||
*/
|
||||
public static function fromServerPath($path)
|
||||
{
|
||||
$cleanpath = Filters\FilenameFilter::cleanPath($path);
|
||||
if (substr($path, -1) === '/')
|
||||
{
|
||||
$cleanpath .= '/';
|
||||
}
|
||||
$root = Filters\FilenameFilter::cleanPath($_SERVER['DOCUMENT_ROOT']);
|
||||
if ($cleanpath === $root)
|
||||
{
|
||||
return self::getCurrentDomainURL('/');
|
||||
}
|
||||
if (starts_with($root . '/', $cleanpath))
|
||||
{
|
||||
return self::getCurrentDomainURL(substr($cleanpath, strlen($root)));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a URL to a server-side path.
|
||||
*
|
||||
* This method returns false if the URL cannot be converted to a server-side path,
|
||||
* e.g. if the URL belongs to an external domain.
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public static function toServerPath($url)
|
||||
{
|
||||
$url = self::getCanonicalURL($url);
|
||||
if (!self::isInternalURL($url))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Filters\FilenameFilter::cleanPath($_SERVER['DOCUMENT_ROOT'] . parse_url($url, \PHP_URL_PATH));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode UTF-8 domain into IDNA (punycode)
|
||||
*
|
||||
|
|
|
|||
|
|
@ -133,6 +133,19 @@ function class_basename($class)
|
|||
return basename(str_replace('\\', '/', is_object($class) ? get_class($class) : $class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean a path to remove ./, ../, trailing slashes, etc.
|
||||
*
|
||||
* This function is an alias to Rhymix\Framework\Filters\FilenameFilter::cleanPath().
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
function clean_path($path)
|
||||
{
|
||||
return Rhymix\Framework\Filters\FilenameFilter::cleanPath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is a shortcut to htmlspecialchars().
|
||||
*
|
||||
|
|
@ -337,6 +350,34 @@ function base64_decode_urlsafe($str)
|
|||
return @base64_decode(str_pad(strtr($str, '-_', '+/'), ceil(strlen($str) / 4) * 4, '=', STR_PAD_RIGHT));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a server-side path to a URL.
|
||||
*
|
||||
* This function is an alias to Rhymix\Framework\URL::fromServerPath().
|
||||
* It returns false if the path cannot be converted.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string|false
|
||||
*/
|
||||
function path2url($path)
|
||||
{
|
||||
return Rhymix\Framework\URL::fromServerPath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a URL to a server-side path.
|
||||
*
|
||||
* This function is an alias to Rhymix\Framework\URL::toServerPath().
|
||||
* It returns false if the URL cannot be converted.
|
||||
*
|
||||
* @param string $url
|
||||
* @return string|false
|
||||
*/
|
||||
function url2path($url)
|
||||
{
|
||||
return Rhymix\Framework\URL::toServerPath($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert hexadecimal color codes to an array of R, G, B values.
|
||||
* This function can handle both 6-digit and 3-digit notations, optionally prefixed with '#'.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue