1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-15 12:06:19 +02:00

Better routing/URL assembling, inline documentation, large number of fixes, news area improvements - should work flowless with every URL configuration. Awaiting testing results for more stability fixes.

This commit is contained in:
secretr
2011-11-30 15:14:02 +00:00
parent 076f03b696
commit 078816befe
12 changed files with 299 additions and 94 deletions

View File

@ -89,11 +89,13 @@ class eurl_admin_ui extends e_admin_controller_ui
{ {
$labels = array(); $labels = array();
$obj = eDispatcher::getConfigObject($module, $location); $obj = eDispatcher::getConfigObject($module, $location);
if($obj) if(!$obj) continue;
{ $config = $obj->config();
if(!$config || !vartrue($config['config']['allowMain'])) continue;
$admin = $obj->admin(); $admin = $obj->admin();
$labels = vartrue($admin['labels'], array()); $labels = vartrue($admin['labels'], array());
}
$this->prefs['url_main_module']['writeParms'][$module] = vartrue($section['name'], eHelper::labelize($module)); $this->prefs['url_main_module']['writeParms'][$module] = vartrue($section['name'], eHelper::labelize($module));
} }

View File

@ -134,7 +134,16 @@ function nextprev_shortcode($parm = '')
if($total_pages <= 1) { return ''; } if($total_pages <= 1) { return ''; }
// urldecoded once by parse_str() // urldecoded once by parse_str()
$url = str_replace(array('--FROM--', '--AMP--'), array('[FROM]', '&amp;'), $parm['url']); if(substr($parm['url'], 0, 5) == 'url::')
{
// New - use URL assembling engine
// Format is: url::route::params::options
// Example: url::news/list/category::id=xxx&name=yyy&page=--PAGE--::full=1
// WARNING - url parameter string have to be rawurlencode-ed BEFORE passed to the shortcode, or it'll break everything
$urlParms = explode('::', $parm['url']);
$url = str_replace(array('--FROM--', '--AMP--'), array('[FROM]', '&amp;'), $e107->url->create($urlParms[1], $urlParms[2], varset($urlParms[3])));
}
else $url = str_replace(array('--FROM--', '--AMP--'), array('[FROM]', '&amp;'), $parm['url']);
// Simple parser vars // Simple parser vars
$e_vars = new e_vars(array( $e_vars = new e_vars(array(

View File

@ -1,7 +1,12 @@
<?php <?php
/** /**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Default config - create ONLY - old legacy URLs * Default config - create ONLY - old legacy URLs
* All possible config options added here - to be used as a reference.
* A good programming practice is to remove all non-used options.
*/ */
class core_news_url extends eUrlConfig class core_news_url extends eUrlConfig
{ {
@ -9,17 +14,47 @@ class core_news_url extends eUrlConfig
{ {
return array( return array(
'config' => array( 'config' => array(
'allowMain' => false, // [optional] default false; disallow this module (while using this config) to be set as site main URL namespace
'noSingleEntry' => true, // [optional] default false; disallow this module to be shown via single entry point when this config is used 'noSingleEntry' => true, // [optional] default false; disallow this module to be shown via single entry point when this config is used
'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script 'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script
'format' => 'get', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored 'format' => 'get', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'selfParse' => true, // [optional] default false; use only this->parse() method, no core routine URL parsing 'selfParse' => true, // [optional] default false; use only this->parse() method, no core routine URL parsing
'selfCreate' => true, // [optional] default false; use only this->create() method, no core routine URL creating 'selfCreate' => true, // [optional] default false; use only this->create() method, no core routine URL creating
'defaultRoute' => '', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/ 'defaultRoute' => 'list/new',// [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'errorRoute' => '', // [optional] default empty; route (no leading module) used when module is found but no inner route is matched, leave empty to force error 404 page 'errorRoute' => '', // [optional] default empty; route (no leading module) used when module is found but no inner route is matched, leave empty to force error 404 page
'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html) 'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html), not used when format is 'get' or legacy non-empty
), ),
'rules' => array() // rule set array 'rules' => array(), // rule set array - can't be used with format 'get' and noSingleEntry true
### [optional] vars mapping (create URL routine), override per rule is allowed
### Keys of this array will be used as a map for finding values from the provided parameters array.
### Those values will be assigned to new keys - corresponding values of mapVars array
### It gives extremely flexibility when used with allowVars. For example we pass $news item array as
### it's retrieved from the DB, with no modifications. This gives us the freedom to create any variations of news
### URLs using the DB data with a single line URL rule. Another aspect of this feature is the simplified code
### for URL assembling - we just do eRouter::create($theRoute, $newsDbArray)
### Not used when in selfCreate mod (create url)
'mapVars' => array(
//'news_id' => 'id',
//'news_sef' => 'name',
),
### [optional] allowed vars definition (create URL routine), override per rule is allowed
### This numerical array serves as a filter for passed vars when creating URLs
### Everything outside this scope is ignored while assembling URLs. Exception are route variables.
### For example: when <id:[\d]+> is present in the route string, there is no need to extra allow 'id'
### To disallow everything but route variables, set allowVars to false
### When format is get, false value will disallow everything (no params) and default preserved variables
### will be extracted from mapVars (if available)
### Default value is empty array
### Not used when in selfCreate mod (create url)
'allowVars' => array(/*'page', 'name'*/),
### Those are regex templates, allowing us to avoid the repeating regex patterns writing in your rules.
### varTemplates are merged with the core predefined templates. Full list with core regex templates and examples can be found
### in rewrite_extended news URL config
'varTemplates' => array(/*'testIt' => '[\d]+'*/),
); );
} }
@ -87,21 +122,10 @@ class core_news_url extends eUrlConfig
case 'day': case 'day':
case 'month': case 'month':
case 'year': case 'year':
$url .= $route[1].'-'.$params['id']; if($page) $page = '.'.$page;
$url .= $route[1].'.'.$params['id'].$page;
break; break;
case 'nextprev':
$route = $params['route'];
unset($params['route']);
if($route != 'list/nextprev')
{
$params['page'] = '[FROM]';
$url = $this->create($route, $params);
unset($tmp);
}
break;
default: default:
$url = 'news.php'; $url = 'news.php';
break; break;

View File

@ -1,7 +1,10 @@
<?php <?php
/** /**
* Mod rewrite & SEF URLs support, manually (rules-less) created/parsed urls * Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Mod rewrite & SEF URLs support, example of manually (rules-less) created/parsed urls
*/ */
class core_news_rewrite_url extends eUrlConfig class core_news_rewrite_url extends eUrlConfig
{ {
@ -9,17 +12,15 @@ class core_news_rewrite_url extends eUrlConfig
{ {
return array( return array(
'config' => array( 'config' => array(
'allowMain' => true,
'noSingleEntry' => false, // [optional] default false; disallow this module to be shown via single entry point when this config is used 'noSingleEntry' => false, // [optional] default false; disallow this module to be shown via single entry point when this config is used
'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script 'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script
'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored 'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'selfParse' => true, // [optional] default false; use only this->parse() method, no core routine URL parsing 'selfParse' => true, // [optional] default false; use only this->parse() method, no core routine URL parsing
'selfCreate' => true, // [optional] default false; use only this->create() method, no core routine URL creating 'selfCreate' => true, // [optional] default false; use only this->create() method, no core routine URL creating
'defaultRoute' => 'list/items', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/ 'defaultRoute' => 'list/items', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'errorRoute' => '', // [optional] default empty; route (no leading module) used when module is found but no inner route is matched, leave empty to force error 404 page 'urlSuffix' => '.html', // [optional] default empty; string to append to the URL (e.g. .html)
'urlSuffix' => '', // [optional] default empty; string to append to the URL (e.g. .html)
), ),
'rules' => array() // rule set array
); );
} }
@ -42,7 +43,7 @@ class core_news_rewrite_url extends eUrlConfig
if('--FROM--' != $params['page']) $page = $params['page'] ? intval($params['page']) : '0'; if('--FROM--' != $params['page']) $page = $params['page'] ? intval($params['page']) : '0';
else $page = '--FROM--'; else $page = '--FROM--';
if(!$route) $route = 'item/default'; if(!$route) $route = 'list/items';
if(is_string($route)) $route = explode('/', $route, 2); if(is_string($route)) $route = explode('/', $route, 2);
$r = array(); $r = array();
@ -88,7 +89,7 @@ class core_news_rewrite_url extends eUrlConfig
// news/Category/Category-Name?page=xxx // news/Category/Category-Name?page=xxx
// news/Short/Category-Name?page=xxx // news/Short/Category-Name?page=xxx
$r[0] = $route[1] == 'category' ? 'Short' : 'Category'; $r[0] = $route[1] == 'category' ? 'Short' : 'Category';
$r[1] = $params['id']; $r[1] = $params['name'] ? $params['name'] : $params['id'];
if($page) $parm = array('page' => $page); if($page) $parm = array('page' => $page);
} }
break; break;
@ -96,7 +97,8 @@ class core_news_rewrite_url extends eUrlConfig
case 'day': case 'day':
case 'month': case 'month':
case 'year': case 'year':
$r[0] = $route[1].'-'.$params['id']; $r = array($route[1], intval($params['id']));
if($page) $parm = array('page' => $page);
break; break;
default: default:
@ -179,7 +181,8 @@ class core_news_rewrite_url extends eUrlConfig
if(!vartrue($parts[1])) $id = 0; if(!vartrue($parts[1])) $id = 0;
else $id = intval($parts[1]); else $id = intval($parts[1]);
$this->legacyQueryString = 'day-'.$id; $this->legacyQueryString = 'day.'.$id.'.'.$page;
return 'list/day';
break; break;
# could be pref or LAN constant # could be pref or LAN constant
@ -187,15 +190,17 @@ class core_news_rewrite_url extends eUrlConfig
if(!vartrue($parts[1])) $id = 0; if(!vartrue($parts[1])) $id = 0;
else $id = intval($parts[1]); else $id = intval($parts[1]);
$this->legacyQueryString = 'month-'.$id; $this->legacyQueryString = 'month.'.$id.'.'.$page;
return 'list/month';
break; break;
# could be pref or LAN constant # could be pref or LAN constant - not supported yet
case 'year': case 'year':
if(!vartrue($parts[1])) $id = 0; if(!vartrue($parts[1])) $id = 0;
else $id = intval($parts[1]); else $id = intval($parts[1]);
$this->legacyQueryString = 'year-'.$id; $this->legacyQueryString = 'year.'.$id.'.'.$page;
//return 'list/year';
break; break;
# force not found # force not found

View File

@ -1,7 +1,13 @@
<?php <?php
/** /**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
*
* $Id$
*
* Mod rewrite & SEF URLs support, managed entirely by the core router (rules) * Mod rewrite & SEF URLs support, managed entirely by the core router (rules)
* It contains a lot of examples (mostly complex), use them to play around and learn things :/
* Generally, things are much more simpler...
*
*/ */
class core_news_rewrite_extended_url extends eUrlConfig class core_news_rewrite_extended_url extends eUrlConfig
{ {
@ -12,7 +18,8 @@ class core_news_rewrite_extended_url extends eUrlConfig
'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script; override per rule is allowed 'legacy' => '{e_BASE}news.php', // [optional] default empty; if it's a legacy module (no single entry point support) - URL to the entry point script; override per rule is allowed
'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored 'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored
'defaultRoute' => 'list/items', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/ 'defaultRoute' => 'list/items', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/
'legacyQuery' => '', // [optional] default null; default legacy query string template, parsed (simpleParse) with requestParams values (request object) and GET vars part of allowVars array (rule); override per rule is allowed 'urlSuffix' => '',
'allowMain' => true,
### default vars mapping (create URL), override per rule is allowed ### default vars mapping (create URL), override per rule is allowed
'mapVars' => array( 'mapVars' => array(
@ -24,6 +31,28 @@ class core_news_rewrite_extended_url extends eUrlConfig
### false means - disallow all vars beside those required by the rules ### false means - disallow all vars beside those required by the rules
### Override per rule is allowed ### Override per rule is allowed
'allowVars' => false, 'allowVars' => false,
### Best news - you don't need to write one and the same
### regex over and over again. Even better news - you might avoid
### writing regex at all! Just use the core regex templates, they
### should fit almost every case.
### Here is a test custom regex template:
'varTemplates' => array('testIt' => '[\d]+'),
/* Predefined Core regex templates, see usage below
'az' => '[A-Za-z]+', // NOTE - it won't match non-latin word characters!
'alphanum' => '[\w\pL]+',
'sefsecure' => '[\w\pL.\-\s!,]+',
'secure' => '[^\/\'"\\<%]+',
'number' => '[\d]+',
'username' => '[\w\pL.\-\s!,]+',
'azOptional' => '[A-Za-z]{0,}',
'alphanumOptional' => '[\w\pL]{0,}',
'sefsecureOptional' => '[\w\pL.\-\s!,]{0,}',
'secureOptional' => '[^\/\'"\\<%]{0,}',
'numberOptional' => '[\d]{0,}',
'usernameOptional' => '[\w\pL.\-\s!,]{0,}',
*/
), ),
'rules' => array( 'rules' => array(
@ -31,36 +60,41 @@ class core_news_rewrite_extended_url extends eUrlConfig
'' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ), '' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ),
'Category' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ), 'Category' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'default.0.{page}', ),
## URL with ID and Title - no DB call, balanced performance! ## URL with ID and Title - no DB call, balanced performance, name optional
'Category/<id:[\d]+>/<name:[\w\pL.\-\s]+>' => array('list/items', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_title' => 'name'), 'legacyQuery' => 'list.{id}.{page}'), ## Demonstrating the usage of custom user defined regex template defined above - 'testIt'
'Category/<id:{testIt}>/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_name' => 'name'), 'legacyQuery' => 'list.{id}.{page}'),
## URL with ID only - best performance! ## URL with Title only - prettiest and slowest! Example with direct regex - no templates
// 'Category/<id:[\d]+>' => array('list/items', 'allowVars' => array('page'), 'legacyQuery' => 'list.{id}.{page}', 'mapVars' => array('category_id' => 'id')), //'Category/<name:{sefsecure}>' => array('list/category', 'allowVars' => array('page'), 'mapVars' => array('category_name' => 'name'), 'legacyQuery' => 'list.{name}.{page}', 'parseCallback' => 'categoryIdByTitle'),
## URL with Title only - prettiest and slowest!
##'Category/<name:[\w\pL.\-\s]+>' => array('list/items', 'allowVars' => array('page'), 'mapVars' => array('category_title' => 'name'), 'legacyQuery' => 'list.{id}.{page}', 'parseCallback' => 'categoryIdByTitle'), ## URL with ID only - best performance, fallback when no sef name provided
'Category/<id:{number}>' => array('list/category', 'allowVars' => array('page'), 'legacyQuery' => 'list.{id}.{page}', 'mapVars' => array('category_id' => 'id')),
### View item requested by id or string, if you remove the catch ALL example, uncomment at least on row from this block ### View item requested by id or string, if you remove the catch ALL example, uncomment at least on row from this block
### leading category name example - could be enabled together with the next example to handle creating of URLs without knowing the category title ### leading category name example - could be enabled together with the next example to handle creating of URLs without knowing the category title
// 'View/<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'), // 'View/<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// to be noted here - value 'name' is replaced by item id within the callback method; TODO replace news_title with news_sef field // to be noted here - value 'name' is replaced by item id within the callback method; TODO replace news_title with news_sef field
// 'View/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'), // 'View/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'news_id' => 'id'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// 'View/<id:[\d]+>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'), // 'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
## URL with ID and Title - no DB call, balanced performance! ## URL with ID and Title - no DB call, balanced performance!
'Short/<id:[\d]+>/<name:[\w\pL.\-\s]+>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_title' => 'name'), 'legacyQuery' => 'list.{id}.{page}'), 'Short/<id:{number}>/<name:{sefsecure}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id', 'category_name' => 'name'), 'legacyQuery' => 'cat.{id}.{page}'),
## fallback when name is not provided
'Short/<id:{number}>' => array('list/short', 'allowVars' => array('page'), 'mapVars' => array('category_id' => 'id'), 'legacyQuery' => 'cat.{id}.{page}'),
// less used after // less used after
'Brief/<id:[\d]+>' => array('list/short', 'allowVars' => array('page'), 'legacyQuery' => 'cat.{id}.{page}', 'mapVars' => array('news_id' => 'id')), //'Brief/<id:[\d]+>' => array('list/short', 'allowVars' => array('page'), 'legacyQuery' => 'cat.{id}.{page}', 'mapVars' => array('category_id' => 'id')),
'Day/<id:[\d]+>' => array('list/day', 'legacyQuery' => 'day-{id}'), 'Day/<id:{number}>' => array('list/day', 'allowVars' => array('page'), 'legacyQuery' => 'day.{id}.{page}'),
'Month/<id:[\d]+>' => array('list/month', 'legacyQuery' => 'month-{id}'), 'Month/<id:{number}>' => array('list/month', 'allowVars' => array('page'), 'legacyQuery' => 'month.{id}.{page}'),
'Year/<id:[\d]+>' => array('list/year', 'legacyQuery' => 'year-{id}'), //'Year/<id:[\d]+>' => array('list/year', 'allowVars' => array('page'), 'legacyQuery' => 'year.{id}.{page}'), not supported yet
### View news item - kinda catch all - very bad performance when News is chosen as default namespace - two additional DB queries on every site call! ### View news item - kinda catch all - very bad performance when News is chosen as default namespace - two additional DB queries on every site call!
// Leading category name - uncomment to enable ## Leading category name - uncomment to enable
'<category:[\w\pL.\-\s]+>/<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'), '<category:{sefsecure}>/<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name', 'mapVars' => array('category_name' => 'category', 'news_title' => 'name', 'news_id' => 'id'), 'category_name' => 'category'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// Base location as item view - uncomment to enable // Base location as item view - fallback if category sef is missing
// '<name:[\w\pL.\-\s]+>' => array('view/item', 'mapVars' => array('news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'), '<name:{sefsecure}>' => array('view/item', 'mapVars' => array('news_title' => 'name'), 'mapVars' => array('news_id' => 'id', 'news_title' => 'name'), 'legacyQuery' => 'extend.{name}', 'parseCallback' => 'itemIdByTitle'),
// fallback if news sef is missing
'View/<id:{number}>' => array('view/item', 'mapVars' => array('news_id' => 'id'), 'legacyQuery' => 'extend.{id}'),
) )
); );
@ -109,15 +143,25 @@ class core_news_rewrite_extended_url extends eUrlConfig
public function itemIdByTitle(eRequest $request) public function itemIdByTitle(eRequest $request)
{ {
$name = $request->getRequestParam('name'); $name = $request->getRequestParam('name');
if(!$name || is_numeric($name)) return; if(($id = $request->getRequestParam('id')))
{
$request->setRequestParam('name', $id);
return;
}
elseif(!$name) return;
elseif(is_numeric($name))
{
return;
}
$sql = e107::getDb('url'); $sql = e107::getDb('url');
$name = e107::getParser()->toDB($name); $name = e107::getParser()->toDB($name);
if($sql->db_Select('news', 'news_id', "news_title='{$name}'")) // TODO - it'll be news_url (new) field if($sql->db_Select('news', 'news_id', "news_title='{$name}'")) // TODO - it'll be news_sef (new) field
{ {
$name = $sql->db_Fetch(); $name = $sql->db_Fetch();
$request->setRequestParam('name', $name['news_id']); $request->setRequestParam('name', $name['news_id']);
} }
else $request->setRequestParam('name', 0);
} }
/** /**
@ -127,14 +171,24 @@ class core_news_rewrite_extended_url extends eUrlConfig
public function categoryIdByTitle(eRequest $request) public function categoryIdByTitle(eRequest $request)
{ {
$name = $request->getRequestParam('name'); $name = $request->getRequestParam('name');
if(!$name || is_numeric($name)) return; if(($id = $request->getRequestParam('id')))
{
$request->setRequestParam('name', $id);
return;
}
elseif(!$name) return;
elseif(is_numeric($name))
{
return;
}
$sql = e107::getDb('url'); $sql = e107::getDb('url');
$id = e107::getParser()->toDB($name); $id = e107::getParser()->toDB($name);
if($sql->db_Select('news_category', 'category_id', "category_name='{$name}'")) // TODO - it'll be category_url (new) field if($sql->db_Select('news_category', 'category_id', "category_name='{$name}'")) // TODO - it'll be category_sef (new) field
{ {
$name = $sql->db_Fetch(); $name = $sql->db_Fetch();
$request->setRequestParam('name', $name['category_id']); $request->setRequestParam('name', $name['category_id']);
} }
else $request->setRequestParam('name', 0);
} }
} }

View File

@ -1,5 +1,10 @@
<?php <?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* Search routing config
*/
class core_search_url extends eUrlConfig class core_search_url extends eUrlConfig
{ {
public function config() public function config()

View File

@ -1,5 +1,10 @@
<?php <?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* Search routing config
*/
class core_search_rewrite_url extends eUrlConfig class core_search_rewrite_url extends eUrlConfig
{ {
public function config() public function config()

View File

@ -1,5 +1,10 @@
<?php <?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* System routing config
*/
class core_system_rewrite_url extends eUrlConfig class core_system_rewrite_url extends eUrlConfig
{ {
public function config() public function config()
@ -7,14 +12,17 @@ class core_system_rewrite_url extends eUrlConfig
return array( return array(
'config' => array( 'config' => array(
'format' => 'path', // get|path - notify core for the current URL format, if set to 'get' rules will be ignored 'allowMain' => true,
'defaultRoute' => 'error/notfound', // [optional] default empty; route (no leading module) used when module is found with no additional controller/action information e.g. /news/ 'format' => 'path',
'defaultRoute' => 'error/notfound',
'errorRoute' => 'error/notfound',
), ),
// rule set array // rule set array
'rules' => array( 'rules' => array(
'error404' => 'error/notfound', 'error404' => 'error/notfound',
'hello' => 'error/hello-world',
) )
); );
} }

View File

@ -1,5 +1,10 @@
<?php <?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* User routing config
*/
class core_user_url extends eUrlConfig class core_user_url extends eUrlConfig
{ {
public function config() public function config()

View File

@ -1,5 +1,10 @@
<?php <?php
/**
* Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* $Id$
*
* User routing config
*/
class core_user_rewrite_url extends eUrlConfig class core_user_rewrite_url extends eUrlConfig
{ {
public function config() public function config()

View File

@ -724,9 +724,10 @@ class eRouter
protected function _init() protected function _init()
{ {
// Gather all rules, add-on info, cache, module for main namespace etc // Gather all rules, add-on info, cache, module for main namespace etc
$this->setMainModule(e107::getPref('url_main_module', ''));
$this->_loadConfig() $this->_loadConfig()
->setAliases(); ->setAliases();
// we need config first as setter does some checks if module can be set as main
$this->setMainModule(e107::getPref('url_main_module', ''));
} }
/** /**
@ -736,14 +737,14 @@ class eRouter
*/ */
public function setMainModule($module) public function setMainModule($module)
{ {
if(!$module) return $this; if(!$module || !$this->isModule($module) || !$this->getConfigValue($module, 'allowMain')) return $this;
$this->_mainNsModule = $module; $this->_mainNsModule = $module;
return $this; return $this;
} }
/** /**
* Get main url namespace module * Get main url namespace module
* @return string
*/ */
public function getMainModule() public function getMainModule()
{ {
@ -752,6 +753,8 @@ class eRouter
/** /**
* Check if given module is the main module * Check if given module is the main module
* @param string $module
* @return boolean
*/ */
public function isMainModule($module) public function isMainModule($module)
{ {
@ -1169,6 +1172,16 @@ class eRouter
return isset($this->_globalConfig[$module]) ? $this->_globalConfig[$module] : array(); return isset($this->_globalConfig[$module]) ? $this->_globalConfig[$module] : array();
} }
/**
* Retrieve single value from a module global configuration array
* @param string $module system module
* @return array configuration
*/
public function getConfigValue($module, $key, $default = null)
{
return isset($this->_globalConfig[$module]) && isset($this->_globalConfig[$module][$key]) ? $this->_globalConfig[$module][$key] : $default;
}
/** /**
* Get system name of a module by its alias * Get system name of a module by its alias
* Returns null if $alias is not an existing alias * Returns null if $alias is not an existing alias
@ -1462,7 +1475,7 @@ class eRouter
if(vartrue($config['selfParse'])) if(vartrue($config['selfParse']))
{ {
// controller/action[/additional/parms] // controller/action[/additional/parms]
if(vartrue($config['urlSuffix'])) $rawPathInfo = $this->removeUrlSuffix($rawPathInfo, $config['urlSuffix']);
$route = $this->configCallback($module, 'parse', array($rawPathInfo, $_GET, $request, $this, $config), $config['location']); $route = $this->configCallback($module, 'parse', array($rawPathInfo, $_GET, $request, $this, $config), $config['location']);
} }
// default module route // default module route
@ -1495,12 +1508,12 @@ class eRouter
$vars->action = $request->getAction(); $vars->action = $request->getAction();
if($rule->allowVars) if($rule->allowVars)
{ {
foreach ($rule->allowVars as $key => $value) foreach ($rule->allowVars as $key)
{ {
if(isset($_GET[$key]) && !$request->isRequestParam($key)) if(isset($_GET[$key]) && !$request->isRequestParam($key))
{ {
// sanitize // sanitize
$vars->$key = preg_replace('/[^\d\w]/', '', $_GET[$key]); $vars->$key = preg_replace('/[^\d\w\-]/', '', $_GET[$key]);
} }
} }
} }
@ -1722,9 +1735,15 @@ class eRouter
$format = isset($config['format']) && $config['format'] ? $config['format'] : self::FORMAT_GET; $format = isset($config['format']) && $config['format'] ? $config['format'] : self::FORMAT_GET;
$urlSuffix = '';
// Fix base url for legacy links // Fix base url for legacy links
if($config['noSingleEntry']) $base = $options['full'] ? SITEURL : e_HTTP; if($config['noSingleEntry']) $base = $options['full'] ? SITEURL : e_HTTP;
elseif(self::FORMAT_GET !== $config['format'])
{
$urlSuffix = $this->urlSuffix;
if(isset($config['urlSuffix'])) $urlSuffix = $config['urlSuffix'];
}
// TODO - main module - don't include it in the return URL // TODO - main module - don't include it in the return URL
// Create by config callback // Create by config callback
@ -1756,12 +1775,11 @@ class eRouter
} }
if($params) if($params)
{ {
$params = $this->createPathInfo($params, $options); $params = $this->createPathInfo($params, $options);
return $base.implode('/', $route).'?'.$params.$anc; return $base.implode('/', $route).$urlSuffix.'?'.$params.$anc;
} }
if(!$route) $urlSuffix = '';
return $base.implode('/', $route).$anc; return $base.implode('/', $route).$urlSuffix.$anc;
} }
@ -1798,19 +1816,25 @@ class eRouter
} }
} }
if($config['allowVars']) // false means - no vars are allowed, nothing to preserve here
if($config['allowVars'] === false) $params = array();
// default empty array value - try to guess what's allowed - mapVars is the best possible candidate
elseif(empty($config['allowVars']) && !empty($config['mapVars'])) $params = array_unique(array_values($config['mapVars']));
// disallow everything but valid URL parameters
if(!empty($config['allowVars']))
{ {
$copy = $params; $copy = $params;
$params = array(); $params = array();
foreach ($config['allowVars'] as $key) foreach ($config['allowVars'] as $key)
{ {
$params[$key] = $copy[$key]; if(isset($copy[$key])) $params[$key] = $copy[$key];
} }
unset($copy); unset($copy);
} }
if($format === self::FORMAT_GET) if($format === self::FORMAT_GET)
{ {
$urlSuffix = '';
$copy = $params; $copy = $params;
$params = array(); $params = array();
$params[$this->routeVar] = implode('/', $route); $params[$this->routeVar] = implode('/', $route);
@ -1822,10 +1846,11 @@ class eRouter
$route = array(); $route = array();
} }
$params = $this->createPathInfo($params, $options); $params = $this->createPathInfo($params, $options);
return $base.implode('/', $route).'?'.$params.$anc; if(!$route) $urlSuffix = '';
return $base.implode('/', $route).$urlSuffix.'?'.$params.$anc;
} }
if(!$route) $urlSuffix = '';
return $format === self::FORMAT_GET ? $base.'?'.$this->routeVar.'='.implode('/', $route).$anc : $base.implode('/', $route).$anc; return $format === self::FORMAT_GET ? $base.'?'.$this->routeVar.'='.implode('/', $route).$anc : $base.implode('/', $route).$urlSuffix.$anc;
} }
/** /**
@ -1873,7 +1898,7 @@ class eRouter
/** /**
* Parses a path info into URL segments * Parses a path info into URL segments
* Be sure to not use non-unique chars for equal and ampersand signs, or you'll break your URLs * Be sure to not use non-unique chars for equal and ampersand signs, or you'll break your URLs
* * XXX - maybe we can switch to http_build_query(), should be able to do everything we need in a much better way
* @param eRequest $request * @param eRequest $request
* @param string $pathInfo path info * @param string $pathInfo path info
* @param string $equal * @param string $equal
@ -2022,6 +2047,39 @@ class eUrlRule
*/ */
public $legacyQuery; public $legacyQuery;
/**
* Core regex templates
* Example usage - route <var:{number}> will result in
* @var array
*/
public $regexTemplates = array(
'az' => '[A-Za-z]+', // NOTE - it won't match non-latin word characters!
'alphanum' => '[\w\pL]+',
'sefsecure' => '[\w\pL.\-\s!,]+',
'secure' => '[^\/\'"\\<%]+',
'number' => '[\d]+',
'username' => '[\w\pL.\-\s!,]+', // TODO - should equal to username pattern, sync it
'azOptional' => '[A-Za-z]{0,}',
'alphanumOptional' => '[\w\pL]{0,}',
'sefsecureOptional' => '[\w\pL.\-\s!,]{0,}',
'secureOptional' => '[^\/\'"\\<%]{0,}',
'numberOptional' => '[\d]{0,}',
'usernameOptional' => '[\w\pL.\-\s!,]{0,}', // TODO - should equal to username pattern, sync it
);
/**
* User defined regex templates
* @var array
*/
public $varTemplates = array();
/**
* All regex templates
* @var e_var
*/
protected $_regexTemplates;
/** /**
* Constructor. * Constructor.
* @param string $route the route of the URL (controller/action) * @param string $route the route of the URL (controller/action)
@ -2034,6 +2092,7 @@ class eUrlRule
if ($fromCache && !$pattern) if ($fromCache && !$pattern)
{ {
$this->setData($route); $this->setData($route);
$this->_regexTemplates = new e_vars($this->regexTemplates);
return; return;
} }
@ -2053,12 +2112,25 @@ class eUrlRule
foreach ($matches2[1] as $name) $this->references[$name] = "<$name>"; foreach ($matches2[1] as $name) $this->references[$name] = "<$name>";
} }
if($this->varTemplates)
{
// don't override core regex templates
$this->regexTemplates = array_merge($this->varTemplates, $this->regexTemplates);
$this->varTemplates = array();
}
$this->_regexTemplates = new e_vars($this->regexTemplates);
if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) if (preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches))
{ {
$tokens = array_combine($matches[1], $matches[2]); $tokens = array_combine($matches[1], $matches[2]);
$tp = e107::getParser();
foreach ($tokens as $name => $value) foreach ($tokens as $name => $value)
{ {
if ($value === '') $value = '[^\/]+'; if ($value === '') $value = '[^\/]+';
elseif($value[0] == '{')
{
$value = $tp->simpleParse($value, $this->_regexTemplates, '[^\/]+');
}
$tr["<$name>"] = "(?P<$name>$value)"; $tr["<$name>"] = "(?P<$name>$value)";
if (isset($this->references[$name])) $tr2["<$name>"] = $tr["<$name>"]; if (isset($this->references[$name])) $tr2["<$name>"] = $tr["<$name>"];
else $this->params[$name] = $value; else $this->params[$name] = $value;
@ -2127,6 +2199,7 @@ class eUrlRule
else return false; else return false;
} }
// map vars first
foreach ($this->mapVars as $srcKey => $dstKey) foreach ($this->mapVars as $srcKey => $dstKey)
{ {
if (isset($params[$srcKey])/* && !isset($params[$dstKey])*/) if (isset($params[$srcKey])/* && !isset($params[$dstKey])*/)
@ -2136,17 +2209,19 @@ class eUrlRule
} }
} }
// disallow everything but valid URL parameters // false means - no vars are allowed, preserve only route vars
if($this->allowVars === false) $this->allowVars = array_keys($this->params); if($this->allowVars === false) $this->allowVars = array_keys($this->params);
// empty array (default) - everything is allowed
if($this->allowVars) // disallow everything but valid URL parameters
if(!empty($this->allowVars))
{ {
$copy = $params; $copy = $params;
$params = array(); $params = array();
$this->allowVars = array_merge($this->allowVars, array_keys($this->params)); $this->allowVars = array_unique(array_merge($this->allowVars, array_keys($this->params)));
foreach ($this->allowVars as $key) foreach ($this->allowVars as $key)
{ {
$params[$key] = $copy[$key]; if(isset($copy[$key])) $params[$key] = $copy[$key];
} }
unset($copy); unset($copy);
} }
@ -2225,7 +2300,6 @@ class eUrlRule
{ {
$manager->parsePathInfo($request, ltrim(substr($pathInfo, strlen($matches[0])), '/')); $manager->parsePathInfo($request, ltrim(substr($pathInfo, strlen($matches[0])), '/'));
} }
return (null !== $this->routePattern ? strtr($this->route, $tr) : $this->route); return (null !== $this->routePattern ? strtr($this->route, $tr) : $this->route);
} }
else return false; else return false;

View File

@ -105,6 +105,12 @@ $nobody_regexp = "'(^|,)(".str_replace(",", "|", e_UC_NOBODY).")(,|$)'";
$newsRoute = 'list/short'; $newsRoute = 'list/short';
break; break;
case 'day':
case 'month':
$newsUrlparms['id'] = $sub_action;
$newsRoute = 'list/'.$action;
break;
default: default:
$newsRoute = 'list/items'; $newsRoute = 'list/items';
break; break;
@ -680,9 +686,10 @@ else
$param['current_action'] = $action; $param['current_action'] = $action;
// NEW - news category title when in list // NEW - news category title when in list
if($sub_action && vartrue($newsAr[1]['category_name'])) if($sub_action && 'list' == $action && vartrue($newsAr[1]['category_name']))
{ {
$category_name = $newsAr[1]['category_name']; // we know category name - pass it to the nexprev url
$category_name = $newsUrlparms['name'] = $newsAr[1]['category_name'];
if(!isset($NEWSLISTCATTITLE)) if(!isset($NEWSLISTCATTITLE))
{ {
$NEWSLISTCATTITLE = "<h1 class='newscatlist-title'>".$tp->toHTML($category_name,FALSE,'TITLE')."</h1>"; $NEWSLISTCATTITLE = "<h1 class='newscatlist-title'>".$tp->toHTML($category_name,FALSE,'TITLE')."</h1>";
@ -717,6 +724,8 @@ else
$amount = ITEMVIEW; $amount = ITEMVIEW;
$nitems = defined('NEWS_NEXTPREV_NAVCOUNT') ? '&navcount='.NEWS_NEXTPREV_NAVCOUNT : '' ; $nitems = defined('NEWS_NEXTPREV_NAVCOUNT') ? '&navcount='.NEWS_NEXTPREV_NAVCOUNT : '' ;
$url = rawurlencode(e107::getUrl()->create($newsRoute, $newsUrlparms)); $url = rawurlencode(e107::getUrl()->create($newsRoute, $newsUrlparms));
// Example of passing route data instead building the URL outside the shortcode - for a reference only
// $url = rawurlencode('url::'.$newsRoute.'::'.http_build_query($newsUrlparms, null, '&'));
$parms = 'tmpl_prefix='.deftrue('NEWS_NEXTPREV_TMPL', 'default').'&total='.$news_total.'&amount='.$amount.'&current='.$newsfrom.$nitems.'&url='.$url; $parms = 'tmpl_prefix='.deftrue('NEWS_NEXTPREV_TMPL', 'default').'&total='.$news_total.'&amount='.$amount.'&current='.$newsfrom.$nitems.'&url='.$url;
echo $tp->parseTemplate("{NEXTPREV={$parms}}"); echo $tp->parseTemplate("{NEXTPREV={$parms}}");