From 905fb0208b29160fa910f659659f9d79b36961c6 Mon Sep 17 00:00:00 2001 From: Cameron Date: Wed, 19 Apr 2017 13:20:59 -0700 Subject: [PATCH] e_url detection moved to application.php . Resolved conflicts between older URL scheme and new one when rootnamespace is in use. --- e107_handlers/application.php | 258 +++++++++++++++++++++++++++++++- e107_handlers/e107_class.php | 1 + e107_handlers/e_parse_class.php | 6 +- index.php | 158 ++----------------- 4 files changed, 269 insertions(+), 154 deletions(-) diff --git a/e107_handlers/application.php b/e107_handlers/application.php index cb7eb1b7b..c427012c0 100644 --- a/e107_handlers/application.php +++ b/e107_handlers/application.php @@ -10,11 +10,261 @@ /** - * e107 Single Entry Point handling - * - * Currently this file contains all classes required for single entry point functionallity - * They will be separated in different files in a proper way (soon) + * Class e_url + * New v2.1.6 */ +class e_url +{ + + private static $_instance; + + private $_request = null; + + private $_config = array(); + + private $_include = null; + + private $_rootnamespace = null; + + private $_legacy = array(); + + private $_legacyAliases = array(); + + + /** + * e_url constructor. + */ + function __construct() + { + $this->_request = (e_HTTP === '/') ? ltrim(e_REQUEST_URI,'/') : str_replace(e_HTTP,'', e_REQUEST_URI) ; + $this->_config = e107::getUrlConfig(); + $this->_rootnamespace = e107::getPref('url_main_module'); + $this->_legacy = e107::getPref('url_config'); + $this->_legacyAliases = e107::getPref('url_aliases'); + + $this->setRootNamespace(); + + } + + /** + * Detect older e_url system. + * @return bool + */ + private function isLegacy() + { + + $arr = (!empty($this->_legacyAliases[e_LAN])) ? array_merge($this->_legacy,$this->_legacyAliases[e_LAN]) : $this->_legacy; + + $list = array_keys($arr); + + foreach($list as $leg) + { + if(strpos($this->_request,$leg.'/') === 0) + { + return true; + } + + } + + return false; + } + + + /** + * @return string + */ + public function getInclude() + { + return $this->_include; + } + + + + private function setRootNamespace() + { + + $plugs = array_keys($this->_config); + + if(!empty($this->_rootnamespace) && in_array($this->_rootnamespace,$plugs)) // Move rootnamespace check to the end of the list. + { + $v = $this->_config[$this->_rootnamespace]; + unset($this->_config[$this->_rootnamespace]); + $this->_config[$this->_rootnamespace] = $v; + } + + } + + + public function run() + { + $pref = e107::getPref(); + $tp = e107::getParser(); + + if(empty($this->_config) || empty($this->_request) || $this->_request === 'index.php' || $this->isLegacy() === true) + { + return false; + } + + + /*if($cached = e107::getCache()->retrieve('Addon_url',5,true,true)) + { + $tmp = e107::unserialize($cached); + } + else*/ + { + // $tmp = e107::getAddonConfig('e_url'); + + // e107::getCache()->set('Addon_url',e107::serialize($tmp,'json'),true,true,true); + } + + + + // if(count($this->_config) && !empty($this->_request) && $this->_request !== 'index.php') + { + + + + /* + + $legacyConfig = array_keys(e107::getPref('url_config')); + foreach($legacyConfig as $leg) + { + if(strpos($this->req,$leg.'/') === 0) + { + var_dump("Found ".$leg); + + } + + + } + */ + + $replaceAlias = array('{alias}\/?','{alias}/?','{alias}\/','{alias}/',); + + foreach($this->_config as $plug=>$cfg) + { + if(empty($pref['e_url_list'][$plug])) // disabled. + { + e107::getDebug()->log('e_URL for '.$plug.' is disabled.'); + continue; + } + + foreach($cfg as $k=>$v) + { + + if(empty($v['regex'])) + { + // e107::getMessage()->addDebug("Skipping empty regex: ".$k.""); + continue; + } + + + if(!empty($v['alias'])) + { + $alias = (!empty($pref['e_url_alias'][e_LAN][$plug][$k])) ? $pref['e_url_alias'][e_LAN][$plug][$k] : $v['alias']; + // e107::getMessage()->addDebug("e_url alias found: ".$alias.""); + if(!empty($this->_rootnamespace) && $this->_rootnamespace === $plug) + { + $v['regex'] = str_replace($replaceAlias, '', $v['regex']); + } + else + { + + $v['regex'] = str_replace('{alias}', $alias, $v['regex']); + } + } + + + $regex = '#'.$v['regex'].'#'; + + if(empty($v['redirect'])) + { + continue; + } + + + $newLocation = preg_replace($regex, $v['redirect'], $this->_request); + + if($newLocation != $this->_request) + { + $redirect = e107::getParser()->replaceConstants($newLocation); + list($file,$query) = explode("?",$redirect,2); + + $get = array(); + if(!empty($query)) + { + parse_str($query,$get); + } + + + foreach($get as $gk=>$gv) + { + $_GET[$gk] = $gv; + } + + e107::getDebug()->log('e_URL in '.$plug.' with key: '.$k.' matched '.$v['regex'].' and included: '.$file.' with $_GET: '.print_a($_GET,true),1); + + if(file_exists($file)) + { + define('e_CURRENT_PLUGIN', $plug); + define('e_QUERY', $query); // do not add to e107_class.php + define('e_URL_LEGACY', $redirect); + + $this->_include= $file; + return true; + // exit; + } + elseif(getperms('0')) + { + + + echo "
"; + echo "

SEF Debug Info

"; + echo "File missing: ".$file; + echo "
Matched key: ".$k.""; + print_a($v); + echo "
"; + + } + + } + } + + } + + unset($tmp,$redirect,$regex); + } + + } + + + /** + * Singleton implementation + * @return e_url + */ + public static function instance() + { + if(null == self::$_instance) + { + self::$_instance = new self(); + } + return self::$_instance; + } + + + public function getConfig() + { + + + + } + + + +} + + + /** diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index 7673ce281..047c7e86b 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -212,6 +212,7 @@ class e107 'e_userperms' => '{e_HANDLER}user_handler.php', 'e_validator' => '{e_HANDLER}validator_class.php', 'e_vars' => '{e_HANDLER}model_class.php', + 'e_url' => '{e_HANDLER}application.php', 'ecache' => '{e_HANDLER}cache_handler.php', 'eController' => '{e_HANDLER}application.php', 'eDispatcher' => '{e_HANDLER}application.php', diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 4ba04ac6f..d172416dd 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -2610,6 +2610,8 @@ class e_parse extends e_parser */ function thumbSrcSet($src='', $width=null) { + $multiply = null; + if(is_array($width)) { $parm = $width; @@ -2648,8 +2650,8 @@ class e_parse extends e_parser return $this->thumbUrl($src, $parm)." ".$parm['h']."h ".$multiply; } - $width = (!empty($parm['w']) || !empty($parm['h'])) ? (intval($parm['w']) * $multiply) : ($this->thumbWidth * $multiply); - $height = (!empty($parm['h']) || !empty($parm['w'])) ? (intval($parm['h']) * $multiply) : ($this->thumbHeight * $multiply); + $width = (!empty($parm['w']) || !empty($parm['h'])) ? (intval($parm['w']) * $multiply) : (intval($this->thumbWidth) * $multiply); + $height = (!empty($parm['h']) || !empty($parm['w'])) ? (intval($parm['h']) * $multiply) : (intval($this->thumbHeight) * $multiply); } else diff --git a/index.php b/index.php index 073423873..e1653ad10 100644 --- a/index.php +++ b/index.php @@ -56,161 +56,21 @@ require_once("class2.php"); // ---------------------------- - -/** - * Simple URL ReWrite (experimental) //TODO test, discuss, benchmark, enhance and include in eFront etc. if all goes well. - * Why?: - * - To make simple url rewrites as quick and easy for plugin developers to implement as it currently is to do the same with .htaccess - * - To use the same familiar standard as used by e_cron, e_status, e_rss etc. etc. so even a novice developer can understand it. - * - * @example (.httaccess): - * RewriteRule ^ref-(.*)/?$ e107_plugins/myplugin/myplugin.php?ref=$1 [NC,L] - * - * @example (e_url.php file - in the 'myplugin' folder) - * - * class myplugin_url // plugin-folder + '_url' - { - function config() - { - $config = array(); - - $config[] = array( - 'regex' => '^ref-(.*)/?$', - 'redirect' => '{e_PLUGIN}myplugin/myplugin.php?ref=$1', - ); - - return $config; - } - - } - */ $sql->db_Mark_Time("Start Simple URL-ReWrite Routine"); - // XXX Cache didn't bring much benefit. + $eUrl = e_url::instance(); + $eUrl->run(); - /*if($cached = e107::getCache()->retrieve('Addon_url',5,true,true)) + if($file = $eUrl->getInclude()) { - $tmp = e107::unserialize($cached); - } - else*/ - { - // $tmp = e107::getAddonConfig('e_url'); - $tmp = e107::getUrlConfig(); - // e107::getCache()->set('Addon_url',e107::serialize($tmp,'json'),true,true,true); + include_once(HEADERF); + include_once($file); + include_once(FOOTERF); + exit; } - $req = (e_HTTP === '/') ? ltrim(e_REQUEST_URI,'/') : str_replace(e_HTTP,'', e_REQUEST_URI) ; - - - if(count($tmp) && !empty($req) && $req !== 'index.php') - { - $rootNamespace = e107::getPref('url_main_module'); - $replaceAlias = array('{alias}\/?','{alias}/?','{alias}\/','{alias}/',); - - $plugs = array_keys($tmp); - - if(in_array($rootNamespace,$plugs)) // Move rootnamespace check to the end of the list. - { - $v = $tmp[$rootNamespace]; - unset($tmp[$rootNamespace]); - $tmp[$rootNamespace] = $v; - } - - foreach($tmp as $plug=>$cfg) - { - if(empty($pref['e_url_list'][$plug])) // disabled. - { - e107::getDebug()->log('e_URL for '.$plug.' is disabled.'); - continue; - } - - foreach($cfg as $k=>$v) - { - - if(empty($v['regex'])) - { - // e107::getMessage()->addDebug("Skipping empty regex: ".$k.""); - continue; - } - - - if(!empty($v['alias'])) - { - $alias = (!empty($pref['e_url_alias'][e_LAN][$plug][$k])) ? $pref['e_url_alias'][e_LAN][$plug][$k] : $v['alias']; - // e107::getMessage()->addDebug("e_url alias found: ".$alias.""); - if(!empty($rootNamespace) && $rootNamespace === $plug) - { - $v['regex'] = str_replace($replaceAlias, '', $v['regex']); - } - else - { - - $v['regex'] = str_replace('{alias}', $alias, $v['regex']); - } - } - - - $regex = '#'.$v['regex'].'#'; - - if(empty($v['redirect'])) - { - continue; - } - - - $newLocation = preg_replace($regex, $v['redirect'], $req); - - if($newLocation != $req) - { - $redirect = e107::getParser()->replaceConstants($newLocation); - list($file,$query) = explode("?",$redirect,2); - - $get = array(); - if(!empty($query)) - { - parse_str($query,$get); - } - - - foreach($get as $gk=>$gv) - { - $_GET[$gk] = $gv; - } - - e107::getDebug()->log('e_URL in '.$plug.' with key: '.$k.' matched '.$v['regex'].' and included: '.$file.' with $_GET: '.print_a($_GET,true),1); - - if(file_exists($file)) - { - define('e_CURRENT_PLUGIN', $plug); - define('e_QUERY', $query); // do not add to e107_class.php - define('e_URL_LEGACY', $redirect); - include_once($file); - exit; - } - elseif(getperms('0')) - { - require_once(HEADERF); - echo "
"; - echo "

SEF Debug Info

"; - echo "File missing: ".$file; - echo "
Matched key: ".$k.""; - - print_a($v); - echo "
"; - require_once(FOOTERF); - exit; - } - - } - } - - } - - unset($tmp,$redirect,$regex); - } - // ----------------------------------------- @@ -226,7 +86,9 @@ // If not already done - define legacy constants $request->setLegacyQstring(); - $request->setLegacyPage(); + $request->setLegacyPage(); + + $inc = $front->isLegacy(); if($inc)