Support XE-compatible rewrite rules

This commit is contained in:
Kijin Sung 2020-06-12 14:16:36 +09:00
parent 1cd3cfc7d9
commit baddbd3cba
5 changed files with 81 additions and 90 deletions

View file

@ -13,32 +13,7 @@ RewriteRule ^(.+)/(addons|files|layouts|m\.layouts|modules|widgets|widgetstyles)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)\.min\.(css|js)$ ./$1.$2 [L]
# rss, blogAPI
RewriteRule ^(rss|atom)$ ./index.php?module=rss&act=$1 [L]
RewriteRule ^([a-zA-Z0-9_]+)/(rss|atom|api)$ ./index.php?mid=$1&act=$2 [L]
# trackback
RewriteRule ^([0-9]+)/(.+)/trackback$ ./index.php?document_srl=$1&key=$2&act=trackback [L]
RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ ./index.php?mid=$1&document_srl=$2&key=$3&act=trackback [L]
# document category
RewriteRule ^([a-zA-Z0-9_]+)/category/([0-9]+)$ ./index.php?mid=$1&category=$2 [L,QSA]
# document permanent link
RewriteRule ^([0-9]+)$ ./index.php?document_srl=$1 [L,QSA]
# admin module link
RewriteRule ^admin/?$ ./index.php?module=admin [L,QSA]
# mid link
# all other short URLs
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_]+)/?$ ./index.php?mid=$1 [L,QSA]
# mid + document link
RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)$ ./index.php?mid=$1&document_srl=$2 [L,QSA]
# mid + entry title
RewriteRule ^([a-zA-Z0-9_]+)/entry/(.+)$ ./index.php?mid=$1&entry=$2 [L,QSA]
# file download
RewriteRule ^files/download/([0-9]+)/([a-zA-Z0-9_-]+)/(.+)$ ./index.php?act=procFileOutput&file_srl=$1&file_key=$2&filename=$3 [L]
RewriteRule . index.php [L]

View file

@ -249,7 +249,7 @@ class Context
// Set information about the current request.
self::_checkGlobalVars();
self::setRequestMethod();
self::setRequestArguments();
self::setRequestArguments(Rhymix\Framework\Router::getRequestArguments());
self::setUploadInfo();
// Load system configuration.
@ -1185,9 +1185,9 @@ class Context
*
* @return void
*/
public static function setRequestArguments()
public static function setRequestArguments($router_args = [])
{
foreach($_REQUEST as $key => $val)
foreach($router_args ?: $_REQUEST as $key => $val)
{
if($val === '' || isset(self::$_reserved_keys[$key]) || self::get($key))
{
@ -1211,7 +1211,7 @@ class Context
}
// Set deprecated request parameters.
if(!$_POST && !empty($GLOBALS['HTTP_RAW_POST_DATA']))
if($_SERVER['REQUEST_METHOD'] === 'POST' && !$_POST && !empty($GLOBALS['HTTP_RAW_POST_DATA']))
{
if(self::getRequestMethod() === 'XMLRPC')
{
@ -1231,6 +1231,10 @@ class Context
foreach((array)$params as $key => $val)
{
if (isset($router_args[$key]))
{
continue;
}
$key = escape($key);
$val = self::_filterXmlVars($key, $val);
self::set($key, $val, true);
@ -1242,6 +1246,10 @@ class Context
parse_str($GLOBALS['HTTP_RAW_POST_DATA'], $params);
foreach($params as $key => $val)
{
if (isset($router_args[$key]))
{
continue;
}
$key = escape($key);
$val = self::_filterRequestVar($key, $val);
self::set($key, $val, true);

View file

@ -7,6 +7,21 @@ namespace Rhymix\Framework;
*/
class Router
{
/**
* List of XE-compatible rewrite rules.
*/
protected static $_xe_compatible_rules = array(
'admin' => ['module' => 'admin'],
'(?<act>rss|atom)' => [],
'(?<document_srl>[0-9]+)' => [],
'(?<mid>[a-zA-Z0-9_-]+)/?' => [],
'(?<mid>[a-zA-Z0-9_-]+)/(?<document_srl>[0-9]+)' => [],
'(?<mid>[a-zA-Z0-9_-]+)/category/(?<category_srl>[0-9]+)' => [],
'(?<mid>[a-zA-Z0-9_-]+)/entry/(?<entry>[^/]+)' => [],
'(?<mid>[a-zA-Z0-9_-]+)/(?<act>rss|atom|api)' => [],
'files/download/(?<file_srl>[0-9]+)/(?<file_key>[a-zA-Z0-9_-]+)/(?<filename>[^/]+)' => ['act' => 'procFileOutput'],
);
/**
* Return the currently configured rewrite level.
*
@ -25,4 +40,49 @@ class Router
}
return intval($level);
}
/**
* Extract request arguments from the current URL.
*
* @return array
*/
public static function getRequestArguments(): array
{
// Get the local part of the current URL.
$url = $_SERVER['REQUEST_URI'];
if (starts_with(\RX_BASEURL, $url))
{
$url = substr($url, strlen(\RX_BASEURL));
}
// Separate additional arguments from the URL.
$args = array();
$argstart = strpos($url, '?');
if ($argstart !== false)
{
@parse_str(substr($url, $argstart + 1), $args);
$url = substr($url, 0, $argstart);
}
// Decode the URL into plain UTF-8.
$url = urldecode($url);
if ($url === '' || (function_exists('mb_check_encoding') && !mb_check_encoding($url, 'UTF-8')))
{
return array();
}
// Try XE-compatible rules.
foreach (self::$_xe_compatible_rules as $regexp => $additional_args)
{
if (preg_match('#^' . $regexp . '$#', $url, $matches))
{
$matches = array_filter($matches, 'is_string', \ARRAY_FILTER_USE_KEY);
$allargs = array_merge($additional_args ?: [], $matches, $args ?: []);
return $allargs;
}
}
// If no pattern matches, return an empty array.
return array();
}
}

View file

@ -25,33 +25,7 @@ location ~ ^/rhymix/(.+)\.min\.(css|js)$ {
try_files $uri $uri/ /rhymix/$1.$2;
}
# rss, blogAPI
rewrite ^/rhymix/(rss|atom)$ /rhymix/index.php?module=rss&act=$1 last;
rewrite ^/rhymix/([a-zA-Z0-9_]+)/(rss|atom|api)$ /rhymix/index.php?mid=$1&act=$2 last;
# trackback
rewrite ^/rhymix/([0-9]+)/(.+)/trackback$ /rhymix/index.php?document_srl=$1&key=$2&act=trackback last;
rewrite ^/rhymix/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ /rhymix/index.php?mid=$1&document_srl=$2&key=$3&act=trackback last;
# administrator page
rewrite ^/rhymix/admin/?$ /rhymix/index.php?module=admin last;
# document category
rewrite ^/rhymix/([a-zA-Z0-9_]+)/category/([0-9]+)$ /rhymix/index.php?mid=$1&category=$2 last;
# document permanent link
rewrite ^/rhymix/([0-9]+)$ /rhymix/index.php?document_srl=$1 last;
# mid link
location ~ ^/rhymix/([a-zA-Z0-9_]+)/?$ {
try_files $uri $uri/ /rhymix/index.php?mid=$1;
# all other short URLs
location /rhymix/ {
try_files $uri $uri/ /rhymix/index.php$is_args$args;
}
# mid + document link
rewrite ^/rhymix/([a-zA-Z0-9_]+)/([0-9]+)$ /rhymix/index.php?mid=$1&document_srl=$2 last;
# mid + entry title
rewrite ^/rhymix/([a-zA-Z0-9_]+)/entry/(.+)$ /rhymix/index.php?mid=$1&entry=$2 last;
# file download
rewrite ^/rhymix/files/download/([0-9]+)/([a-zA-Z0-9_-]+)/(.+)$ /rhymix/index.php?act=procFileOutput&file_srl=$1&file_key=$2&filename=$3 last;

View file

@ -25,33 +25,7 @@ location ~ ^/(.+)\.min\.(css|js)$ {
try_files $uri $uri/ /$1.$2;
}
# rss, blogAPI
rewrite ^/(rss|atom)$ /index.php?module=rss&act=$1 last;
rewrite ^/([a-zA-Z0-9_]+)/(rss|atom|api)$ /index.php?mid=$1&act=$2 last;
# trackback
rewrite ^/([0-9]+)/(.+)/trackback$ /index.php?document_srl=$1&key=$2&act=trackback last;
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ /index.php?mid=$1&document_srl=$2&key=$3&act=trackback last;
# administrator page
rewrite ^/admin/?$ /index.php?module=admin last;
# document category
rewrite ^/([a-zA-Z0-9_]+)/category/([0-9]+)$ /index.php?mid=$1&category=$2 last;
# document permanent link
rewrite ^/([0-9]+)$ /index.php?document_srl=$1 last;
# mid link
location ~ ^/([a-zA-Z0-9_]+)/?$ {
try_files $uri $uri/ /index.php?mid=$1;
# all other short URLs
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# mid + document link
rewrite ^/([a-zA-Z0-9_]+)/([0-9]+)$ /index.php?mid=$1&document_srl=$2 last;
# mid + entry title
rewrite ^/([a-zA-Z0-9_]+)/entry/(.+)$ /index.php?mid=$1&entry=$2 last;
# file download
rewrite ^/files/download/([0-9]+)/([a-zA-Z0-9_-]+)/(.+)$ /index.php?act=procFileOutput&file_srl=$1&file_key=$2&filename=$3 last;