diff --git a/class2.php b/class2.php index bba6f3c44..cfba4b392 100755 --- a/class2.php +++ b/class2.php @@ -1748,6 +1748,19 @@ if($fpUrl === $fpPref) } unset($fpUrl, $fpPref); +$dbg->logTime('Legacy Route detection'); +// Reverse lookup of current URI against legacy e_url entry to determine route. +if(!deftrue('e_SINGLE_ENTRY') && deftrue('e_CURRENT_PLUGIN')) +{ + if($route = e107::detectRoute(e_CURRENT_PLUGIN, e_REQUEST_URI)) + { + e107::route($route); + } + + unset($route); +} + + diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index 90e8bdda6..22618466b 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -4032,6 +4032,65 @@ class e107 } } + /** + * Reverse lookup of current URI against legacy e_url entry for the specified plugin. + * Useful for when SEF (e_SINGLE_ENTRY) is not in use. + * @param string $route eg. forum/index (must match SEF route ) + */ + public static function detectRoute($plugin=null, $uri=null) + { + if(empty($plugin) || empty($uri)) + { + return null; + } + + if(!$addon = e107::getAddon($plugin,'e_url')) + { + trigger_error("Couldn't load e_url for ".$plugin); + return null; + } + + if(!$result = e107::callMethod($addon, 'config')) + { + trigger_error($plugin.' - e_url::config() method returned nothing'); + return null; + } + + foreach($result as $key=>$var) + { + if(empty($var['legacy'])) + { + continue; + } + + $legacy = self::getParser()->replaceConstants($var['legacy'], 'abs'); // remove {e_PLUGIN} + $legacy = preg_replace('/\{[\w]*\}/', '__XXX__', $legacy); // replace {fields} with placeholder. + $legacy = preg_quote($legacy,'/'); // add slashes + + $pattern = '/'. str_replace('__XXX__', '([\w]*)', $legacy) .'/'; // replace placeholder with regexp + + if(preg_match($pattern, $uri)) + { + return $plugin.'/'.$key; + } + + } + + return null; + } + + + public static function route($route) + { + if(defined('e_ROUTE')) + { + return null; + } + + define('e_ROUTE', $route); + } + + /** * Generate a plugin's search engine-friendly URL with HTML special characters escaped * diff --git a/e107_plugins/forum/e_url.php b/e107_plugins/forum/e_url.php index 5413f0629..f684e7f11 100644 --- a/e107_plugins/forum/e_url.php +++ b/e107_plugins/forum/e_url.php @@ -29,18 +29,21 @@ class forum_url // plugin-folder + '_url' 'regex' => '^forum/rules/?', 'sef' => 'forum/rules', 'redirect' => '{e_PLUGIN}forum/forum.php?f=rules', + 'legacy' => '{e_PLUGIN}forum/forum.php?f=rules', ); $config['stats'] = array( 'regex' => '^forum/stats/?', 'sef' => 'forum/stats', 'redirect' => '{e_PLUGIN}forum/forum_stats.php', + 'legacy' => '{e_PLUGIN}forum/forum_stats.php', ); $config['track'] = array( 'regex' => '^forum/track/?', 'sef' => 'forum/track', 'redirect' => '{e_PLUGIN}forum/forum.php?f=track', + 'legacy' => '{e_PLUGIN}forum/forum.php?f=track', ); $config['markread'] = array( @@ -53,13 +56,15 @@ class forum_url // plugin-folder + '_url' $config['new'] = array( 'regex' => '^forum/new$/?', 'sef' => 'forum/new', - 'redirect' => '{e_PLUGIN}forum/forum.php?new' + 'redirect' => '{e_PLUGIN}forum/forum.php?new', + 'legacy' => '{e_PLUGIN}forum/forum.php?new' ); $config['post'] = array( 'regex' => '^forum/post/?', 'sef' => 'forum/post/', 'redirect' => '{e_PLUGIN}forum/forum_post.php', + 'legacy' => '{e_PLUGIN}forum/forum_post.php', ); // only create url - parsed above. @@ -94,7 +99,7 @@ class forum_url // plugin-folder + '_url' 'regex' => '^forum\/?$', // matched against url, and if true, redirected to 'redirect' below. 'sef' => 'forum', // used by e107::url(); to create a url from the db table. 'redirect' => '{e_PLUGIN}forum/forum.php', // file-path of what to load when the regex returns true. - + 'legacy' => '{e_PLUGIN}forum/forum.php', ); @@ -106,8 +111,6 @@ class forum_url // plugin-folder + '_url' ); - - return $config; } diff --git a/e107_plugins/forum/forum.php b/e107_plugins/forum/forum.php index c90123751..f6a930bb4 100644 --- a/e107_plugins/forum/forum.php +++ b/e107_plugins/forum/forum.php @@ -97,7 +97,6 @@ class forum_front case 'rules': include_once(HEADERF); - $this->forum_rules('show'); include_once(FOOTERF); exit; diff --git a/e107_tests/tests/unit/e107Test.php b/e107_tests/tests/unit/e107Test.php index 1d9b3656b..1d978994e 100644 --- a/e107_tests/tests/unit/e107Test.php +++ b/e107_tests/tests/unit/e107Test.php @@ -1314,13 +1314,51 @@ class e107Test extends \Codeception\Test\Unit } + function testDetectRoute() + { + e107::getPlugin()->install('forum'); + $tests = array( + 0 => array( + 'plugin' => 'forum', + 'uri' => '/e107_plugins/forum/forum.php?f=rules', + 'expected' => 'forum/rules', + ), + 1 => array( + 'plugin' => 'forum', + 'uri' => '/e107_plugins/forum/forum_viewforum.php?id=543123', + 'expected' => 'forum/forum', + ), + + ); + + foreach($tests as $index => $var) + { + $result = e107::detectRoute($var['plugin'], $var['uri']); + if(empty($var['expected'])) + { + echo $result."\n"; + continue; + } + + $this->assertSame($var['expected'], $result); + } + + + + + + e107::getPlugin()->uninstall('forum'); + + } + +/* public function testThemeLan() { $result = e107::themeLan(null, 'basic-light'); var_dump($result); - } + }*/ /* public function testLan() {