Index: branches/5.0.x/core/kernel/db/dblist.php
===================================================================
diff -u -r12264 -r12294
--- branches/5.0.x/core/kernel/db/dblist.php (.../dblist.php) (revision 12264)
+++ branches/5.0.x/core/kernel/db/dblist.php (.../dblist.php) (revision 12294)
@@ -1,6 +1,6 @@
Application->getUnitOption($this->Prefix, 'ParentPrefix');
- if($parent_prefix)
- {
+ if ($parent_prefix) {
$parent_table_key = $this->Application->getUnitOption($this->Prefix, 'ParentTableKey');
if (is_array($parent_table_key)) $parent_table_key = getArrayValue($parent_table_key, $parent_prefix);
$foreign_key_field = $this->Application->getUnitOption($this->Prefix, 'ForeignKey');
Index: branches/5.0.x/core/kernel/constants.php
===================================================================
diff -u -r12244 -r12294
--- branches/5.0.x/core/kernel/constants.php (.../constants.php) (revision 12244)
+++ branches/5.0.x/core/kernel/constants.php (.../constants.php) (revision 12294)
@@ -1,6 +1,6 @@
+* This class is a Facade for any other class which needs to deal with Kernel4 framework.
* The class incapsulates the main run-cycle of the script, provide access to all other objects in the framework.
*
-* The class is a singleton, which means that there could be only one instance of KernelApplication in the script.
-* This could be guranteed by NOT calling the class constuctor directly, but rather calling KernelApplication::Instance() method,
+* The class is a singleton, which means that there could be only one instance of kApplication in the script.
+* This could be guranteed by NOT calling the class constuctor directly, but rather calling kApplication::Instance() method,
* which returns an instance of the application. The method gurantees that it will return exactly the same instance for any call.
* See singleton pattern by GOF.
* @package kernel4
@@ -98,6 +98,13 @@
var $ReplacementTemplates = Array ();
/**
+ * Mod-Rewrite listeners used during url building and parsing
+ *
+ * @var Array
+ */
+ var $RewriteListeners = Array ();
+
+ /**
* Reference to debugger
*
* @var Debugger
@@ -1466,15 +1473,28 @@
// $params = array_merge_recursive2($this->getPassThroughVariables($params), $params);
$params = array_merge($this->getPassThroughVariables($params), $params);
- if ($force_rewrite || ($this->RewriteURLs($ssl) && !$no_rewrite))
- {
+ if ($force_rewrite || ($this->RewriteURLs($ssl) && !$no_rewrite)) {
+ static $rewrite_listeners_done = false;
+
+ if (!$rewrite_listeners_done) {
+ $mod_rewrite_helper =& $this->recallObject('ModRewriteHelper');
+ /* @var $mod_rewrite_helper kModRewriteHelper */
+
+ $mod_rewrite_helper->initRewriteListeners();
+
+ $rewrite_listeners_done = true;
+ }
+
$session =& $this->recallObject('Session');
- if( $session->NeedQueryString() && !$force_no_sid ) $params['sid'] = $this->GetSID();
+
+ if ($session->NeedQueryString() && !$force_no_sid) {
+ $params['sid'] = $this->GetSID();
+ }
+
$url = $this->BuildEnv_NEW($t, $params, $pass, $pass_events);
$ret = $this->BaseURL($prefix, $ssl).$url.$map_link;
}
- else
- {
+ else {
unset($params['pass_category']); // we don't need to pass it when mod_rewrite is off
$env = $this->BuildEnv($t, $params, $pass, $pass_events);
$ret = $this->BaseURL($prefix, $ssl).$index_file.'?'.$env.$map_link;
@@ -1547,7 +1567,7 @@
foreach ($pass_info as $prefix) {
list($prefix_only) = explode('.', $prefix, 1);
- $sorted[$prefix] = $this->getUnitOption($prefix_only, 'PassPriority', 0);
+ $sorted[$prefix] = $this->getUnitOption($prefix_only, 'RewritePriority', 0);
}
arsort($sorted);
@@ -1573,8 +1593,8 @@
$env = '';
$encode = false;
- if (isset($params['__URLENCODE__']))
- {
+
+ if (isset($params['__URLENCODE__'])) {
$encode = $params['__URLENCODE__'];
unset($params['__URLENCODE__']);
}
@@ -1583,59 +1603,104 @@
unset($params['__SSL__']);
}
- $m_only = true;
+ $catalog_item_found = false;
$pass_info = $this->getPassInfo($pass);
+
if ($pass_info) {
- if ($pass_info[0] == 'm') array_shift($pass_info);
- $params['t'] = $t;
- foreach($pass_info as $pass_index => $pass_element)
- {
- list($prefix) = explode('.', $pass_element);
- $require_rewrite = $this->findModule('Var', $prefix) && $this->getUnitOption($prefix, 'CatalogItem');
- if ($require_rewrite) {
+ if ($pass_info[0] == 'm') {
+ array_shift($pass_info);
+ }
+
+ $inject_parts = Array (); // url parts for beginning of url
+ $params['t'] = $t; // make template available for rewrite listeners
+ $params['pass_template'] = true; // by default we keep given template in resulting url
+
+ if (!array_key_exists('pass_category', $params)) {
+ $params['pass_category'] = false; // by default we don't keep categories in url
+ }
+
+ foreach ($pass_info as $pass_index => $pass_element) {
+ list ($prefix) = explode('.', $pass_element);
+ $catalog_item = $this->findModule('Var', $prefix) && $this->getUnitOption($prefix, 'CatalogItem');
+
+ if (array_key_exists($prefix, $this->RewriteListeners)) {
// if next prefix is same as current, but with special => exclude current prefix from url
- $next_prefix = getArrayValue($pass_info, $pass_index + 1);
+ $next_prefix = array_key_exists($pass_index + 1, $pass_info) ? $pass_info[$pass_index + 1] : false;
if ($next_prefix) {
$next_prefix = substr($next_prefix, 0, strlen($prefix) + 1);
- if ($prefix.'.' == $next_prefix) continue;
+ if ($prefix . '.' == $next_prefix) {
+ continue;
+ }
}
- $a = $this->BuildModuleEnv_NEW($pass_element, $params, $pass_events);
- if ($a) {
- $ret .= '/'.$a;
- $m_only = false;
+
+ // rewrited url part
+ $url_part = $this->BuildModuleEnv_NEW($pass_element, $params, $pass_events);
+
+ if (is_string($url_part) && $url_part) {
+ $ret .= $url_part . '/';
+
+ if ($catalog_item) {
+ // pass category later only for catalog items
+ $catalog_item_found = true;
+ }
}
+ elseif (is_array($url_part)) {
+ // rewrite listener want to insert something at the beginning of url too
+ if ($url_part[0]) {
+ $inject_parts[] = $url_part[0];
+ }
+
+ if ($url_part[1]) {
+ $ret .= $url_part[1] . '/';
+ }
+
+ if ($catalog_item) {
+ // pass category later only for catalog items
+ $catalog_item_found = true;
+ }
+ } elseif ($url_part === false) {
+ // rewrite listener decided not to rewrite given $pass_element
+ $env .= ':' . $this->BuildModuleEnv($pass_element, $params, $pass_events);
+ }
}
- else
- {
- $env .= ':'.$this->BuildModuleEnv($pass_element, $params, $pass_events);
+ else {
+ $env .= ':' . $this->BuildModuleEnv($pass_element, $params, $pass_events);
}
}
- if (!$m_only || preg_match('/c\.[-\d]*/', implode(',', $pass_info))) {
- $params['pass_category'] = 1;
+ if ($catalog_item_found || preg_match('/c\.[-\d]*/', implode(',', $pass_info))) {
+ // "c" prefix is present -> keep category
+ $params['pass_category'] = true;
}
- $ret = $this->BuildModuleEnv_NEW('m', $params, $pass_events).$ret;
- $cat_processed = isset($params['category_processed']) && $params['category_processed'];
- if ($cat_processed) {
- unset($params['category_processed']);
- }
- if (!$m_only || !$cat_processed || !defined('EXP_DIR_URLS')) {
- $ret = trim($ret, '/').'.html';
+ $params['inject_parts'] = $inject_parts;
+
+ $ret = $this->BuildModuleEnv_NEW('m', $params, $pass_events) . '/' . $ret;
+ $cat_processed = array_key_exists('category_processed', $params) && $params['category_processed'];
+
+ // remove tempporary parameters used by listeners
+ unset($params['t'], $params['inject_parts'], $params['pass_template'], $params['pass_category'], $params['category_processed']);
+
+ if ($catalog_item_found || !$cat_processed || !defined('EXP_DIR_URLS')) {
+ // this catalog item detail page OR there is no category given
+ $ret = trim($ret, '/') . '.html';
}
else {
- $ret .= '/';
+ // url ends with "/" and not with ".html"
+ $ret = trim($ret, '/') . '/';
}
-// $ret = trim($ret, '/').'/';
- if($env) $params[ENV_VAR_NAME] = ltrim($env, ':');
+ if ($env) {
+ $params[ENV_VAR_NAME] = ltrim($env, ':');
+ }
}
unset($params['pass'], $params['opener'], $params['m_event']);
- if ($force_admin) $params['admin'] = 1;
+ if ($force_admin) {
+ $params['admin'] = 1;
+ }
- if( getArrayValue($params,'escape') )
- {
+ if (array_key_exists('escape', $params) && $params['escape']) {
$ret = addslashes($ret);
unset($params['escape']);
}
@@ -1644,30 +1709,31 @@
$params_str = '';
$join_string = $encode ? '&' : '&';
- foreach ($params as $param => $value)
- {
- $params_str .= $join_string.$param.'='.$value;
+
+ foreach ($params as $param => $value) {
+ $params_str .= $join_string . $param . '=' . $value;
}
- $ret .= preg_replace('/^'.$join_string.'(.*)/', '?\\1', $params_str);
+ if ($params_str) {
+ $ret .= '?' . substr($params_str, strlen($join_string));
+ }
+
if ($encode) {
$ret = str_replace('\\', '%5C', $ret);
}
+
return $ret;
}
-
- function BuildModuleEnv_NEW($prefix_special, &$params, $pass_events = false)
+ function BuildModuleEnv_NEW($prefix_special, &$params, $keep_events = false)
{
- $event_params = Array('pass_events' => $pass_events, 'url_params' => $params);
- $event = new kEvent($prefix_special.':BuildEnv', $event_params);
- $this->HandleEvent($event);
- $params = $event->getEventParam('url_params'); // save back unprocessed parameters
+ list ($prefix) = explode('.', $prefix_special);
- $ret = '';
- if ($event->getEventParam('env_string')) {
- $ret = trim( $event->getEventParam('env_string'), '/');
- }
+ $url_parts = Array ();
+ $listener = $this->RewriteListeners[$prefix];
+
+ $ret = $listener[0]->$listener[1](REWRITE_MODE_BUILD, $prefix_special, $params, $url_parts, $keep_events);
+
return $ret;
}
Index: branches/5.0.x/core/units/general/helpers/mod_rewrite_helper.php
===================================================================
diff -u -r12151 -r12294
--- branches/5.0.x/core/units/general/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 12151)
+++ branches/5.0.x/core/units/general/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 12294)
@@ -1,6 +1,6 @@
HTTPQuery =& $this->Application->recallObject('HTTPQuery');
}
- function SetDefaultValues(&$vars)
+ function processRewriteURL()
{
- $defaults = Array ('m_cat_id' => 0, 'm_cat_page' => 1, 'm_opener' => 's', 't' => 'index');
- foreach ($defaults as $default_key => $default_value) {
- // bug: null is never returned
- if ($this->HTTPQuery->Get($default_key) == null) {
- $vars[$default_key] = $default_value;
+ $passed = Array ();
+ $url = $this->HTTPQuery->Get('_mod_rw_url_');
+ if (substr($url, -5) == '.html') {
+ $url = substr($url, 0, strlen($url) - 5);
+ }
+
+ $restored = false;
+ $sql = 'SELECT Data, Cached
+ FROM ' . TABLE_PREFIX . 'Cache
+ WHERE VarName = "mod_rw_' . md5($url) . '"';
+ $cache = $this->Conn->GetRow($sql);
+
+ if (false && $cache && $cache['Cached'] > 0) {
+ // not used for now
+ $cache = unserialize($cache['Data']);
+ $vars = $cache['vars'];
+ $passed = $cache['passed'];
+ $restored = true;
+ }
+ else {
+ $vars = $this->parseRewriteURL($url);
+ $passed = $vars['pass']; // also used in bottom of this method
+ unset($vars['pass']);
+
+ $cache = Array ('vars' => $vars, 'passed' => $passed);
+
+ $fields_hash = Array (
+ 'VarName' => 'mod_rw_' . md5($url),
+ 'Data' => serialize($cache),
+ 'Cached' => adodb_mktime(),
+ );
+ $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Cache', 'REPLACE');
+
+ if (array_key_exists('t', $this->HTTPQuery->Post) && $this->HTTPQuery->Post['t']) {
+ // template from POST overrides template from URL.
+ $vars['t'] = $this->HTTPQuery->Post['t'];
+ if (isset($vars['is_virtual']) && $vars['is_virtual']) {
+ $vars['m_cat_id'] = 0; // this is virtual template category (for Proj-CMS)
+ }
}
+
+ unset($vars['is_virtual']);
}
+
+ foreach ($vars as $name => $value) {
+ $this->HTTPQuery->Set($name,$value);
+ }
+
+// if ($restored) {
+ $this->InitAll();
+// }
+
+ $this->HTTPQuery->finalizeParsing($passed);
}
+ function parseRewriteURL($url)
+ {
+ $sql = 'SELECT Data, Cached
+ FROM ' . TABLE_PREFIX . 'Cache
+ WHERE VarName = "mod_rw_' . md5($url) . '"';
+ $vars = $this->Conn->GetRow($sql);
+
+ if (false && $vars && $vars['Cached'] > 0) {
+ // not used for now
+ $vars = unserialize($menu['Data']);
+ return $vars;
+ }
+
+ $vars = Array ('pass' => Array ('m'));
+ $url_parts = $url ? explode('/', trim($url, '/')) : Array ();
+
+ if (($this->HTTPQuery->Get('rewrite') == 'on') || !$url_parts) {
+ $this->_setDefaultValues($vars);
+ }
+
+ if (!$url_parts) {
+ $this->InitAll();
+ $vars['t'] = $this->HTTPQuery->getDefaultTemplate('');
+
+ return $vars;
+ }
+ else {
+ $vars['t'] = '';
+ }
+
+ $this->_parseLanguage($url_parts, $vars);
+ $this->_parseTheme($url_parts, $vars);
+ $this->_setDefaultPages($vars);
+
+ if ($this->_parsePage($url_parts, $vars)) {
+ $this->_partsFound[] = 'parsePage';
+ }
+
+ // http://site-url///[_]//
+ // http://site-url///[_]/ (category-based section template)
+ // http://site-url///[_]//
+ // http://site-url///[_]/ (category-based detail template)
+ // http://site-url////[_]/ (customized url)
+
+ if ( $this->processRewriteListeners($url_parts, $vars) ) {
+ return $vars;
+ }
+
+ if ($this->_parsePhisycalTemplate($url_parts, $vars)) {
+ $this->_partsFound[] = 'parsePhisycalTemplate';
+ }
+
+ if (($this->_modulePrefix === false) && in_array('parseCategory', $this->_partsFound)) {
+ // no item found, but category found -> module index page
+ foreach ($this->Application->ModuleInfo as $module_name => $info) {
+ // no idea what module we are talking about, so pass info form all modules
+ $vars['pass'][] = $info['Var'];
+ }
+
+ return $vars;
+ }
+
+ if (!$this->_partsFound) {
+ $not_found = $this->Application->ConfigValue('ErrorTemplate');
+ $vars['t'] = $not_found ? $not_found : 'error_notfound';
+
+ $themes_helper =& $this->Application->recallObject('ThemesHelper');
+ /* @var $themes_helper kThemesHelper */
+
+ $vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t']);
+
+ header('HTTP/1.0 404 Not Found');
+ }
+
+ return $vars;
+ }
+
+ function InitAll()
+ {
+ $this->Application->VerifyLanguageId();
+ $this->Application->Phrases->Init('phrases');
+ $this->Application->VerifyThemeId();
+ }
+
/**
+ * Processes url using rewrite listeners
+ *
+ * @param Array $url_parts
+ * @param Array $vars
+ * @return bool
+ */
+ function processRewriteListeners(&$url_parts, &$vars)
+ {
+ $this->initRewriteListeners();
+
+ foreach ($this->Application->RewriteListeners as $prefix => $listener) {
+ $listener_result = $listener[0]->$listener[1](REWRITE_MODE_PARSE, $prefix, $vars, $url_parts);
+ if ($listener_result === false) {
+ // will not proceed to other methods
+ return true;
+ }
+ }
+
+ // will proceed to other methods
+ return false;
+ }
+
+ /**
+ * Parses real template name from url
+ *
+ * @param Array $url_parts
+ * @param Array $vars
+ * @return bool
+ */
+ function _parsePhisycalTemplate($url_parts, &$vars)
+ {
+ if (!$url_parts) {
+ return false;
+ }
+
+ do {
+ $template_path = implode('/', $url_parts);
+
+ $t_parts['path'] = dirname($template_path) == '.' ? '' : '/' . dirname($template_path);
+ $t_parts['file'] = basename($template_path);
+
+ $sql = 'SELECT FileId
+ FROM ' . TABLE_PREFIX . 'ThemeFiles
+ WHERE (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')';
+ $template_found = $this->Conn->GetOne($sql);
+
+ if (!$template_found) {
+ array_shift($url_parts);
+ }
+ } while (!$template_found && $url_parts);
+
+ if ($template_found) {
+ $vars['t'] = $template_path;
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Parses category part of url, build main part of url
+ *
+ * @param int $rewrite_mode Mode in what rewrite listener was called. Possbile two modes: REWRITE_MODE_BUILD, REWRITE_MODE_PARSE.
+ * @param string $prefix Prefix, that listener uses for system integration
+ * @param Array $params Params, that are used for url building or created during url parsing.
+ * @param Array $url_parts Url parts to parse (only for parsing).
+ * @param bool $keep_events Keep event names in resulting url (only for building).
+ * @return bool|string|Array Return true to continue to next listener; return false (when building) not to rewrite given prefix; return false (when parsing) to stop processing at this listener.
+ */
+ function MainRewriteListener($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts, $keep_events = false)
+ {
+ if ($rewrite_mode == REWRITE_MODE_BUILD) {
+ return $this->_buildMainUrl($prefix, $params, $keep_events);
+ }
+
+ if ( $this->_parseFriendlyUrl($url_parts, $params) ) {
+ // friendly urls work like exact match only!
+ return false;
+ }
+
+ if ($this->_parseCategory($url_parts, $params)) {
+ $this->_partsFound[] = 'parseCategory';
+ }
+
+ return true;
+ }
+
+ /**
+ * Build main part of every url
+ *
+ * @param string $prefix_special
+ * @param Array $params
+ * @param bool $keep_events
+ * @return string
+ */
+ function _buildMainUrl($prefix_special, &$params, $keep_events)
+ {
+ $ret = '';
+ list ($prefix) = explode('.', $prefix_special);
+
+ $processed_params = $this->getProcessedParams($prefix_special, $params, $keep_events);
+ if ($processed_params === false) {
+ return '';
+ }
+
+ // add language
+ $default_language_id = $this->Application->GetDefaultLanguageId();
+ if ($processed_params['m_lang'] && ($processed_params['m_lang'] != $default_language_id)) {
+ $language_name = $this->Application->getCache('language_names', $processed_params['m_lang']);
+ if ($language_name === false) {
+ $sql = 'SELECT PackName
+ FROM ' . TABLE_PREFIX . 'Language
+ WHERE LanguageId = ' . $processed_params['m_lang'];
+ $language_name = $this->Conn->GetOne($sql);
+
+ $this->Application->setCache('language_names', $processed_params['m_lang'], $language_name);
+ }
+
+ $ret .= $language_name . '/';
+ }
+
+ // add theme
+ $default_theme_id = $this->Application->GetDefaultThemeId();
+ if ($processed_params['m_theme'] && ($processed_params['m_theme'] != $default_theme_id)) {
+ $theme_name = $this->Application->getCache('theme_names', $processed_params['m_theme']);
+ if ($theme_name === false) {
+ $sql = 'SELECT Name
+ FROM ' . TABLE_PREFIX . 'Theme
+ WHERE ThemeId = ' . $processed_params['m_theme'];
+ $theme_name = $this->Conn->GetOne($sql);
+
+ $this->Application->setCache('theme_names', $processed_params['m_theme'], $theme_name);
+
+ }
+
+ $ret .= $theme_name . '/';
+ }
+
+ // inject custom url parts made by other rewrite listeners just after language/theme url parts
+ $ret .= implode('/', $params['inject_parts']) . '/';
+
+ // add category
+ if ($processed_params['m_cat_id'] > 0 && $params['pass_category']) {
+ $category_filename = $this->Application->getFilename('c', $processed_params['m_cat_id']);
+
+ preg_match('/^Content\/(.*)/i', $category_filename, $regs);
+
+ if ($regs) {
+ $template = array_key_exists('t', $params) ? $params['t'] : false;
+
+ if (strtolower($regs[1]) == strtolower($template)) {
+ // we could have category path like "Content/" in this case remove template
+ $params['pass_template'] = false;
+ }
+
+ $ret .= $regs[1] . '/';
+ }
+
+ $params['category_processed'] = true;
+ }
+
+ // reset category page
+ $force_page_adding = false;
+ if (array_key_exists('reset', $params) && $params['reset']) {
+ unset($params['reset']);
+
+ if ($processed_params['m_cat_id']) {
+ $processed_params['m_cat_page'] = 1;
+ $force_page_adding = true;
+ }
+ }
+
+ if ((array_key_exists('category_processed', $params) && $params['category_processed'] && ($processed_params['m_cat_page'] > 1)) || $force_page_adding) {
+ // category name was added before AND category page number found
+ $ret = rtrim($ret, '/') . '_' . $processed_params['m_cat_page'] . '/';
+ }
+
+ $template = array_key_exists('t', $params) ? $params['t'] : false;
+ $category_template = $processed_params['m_cat_id'] ? $this->Application->getCache('category_designs', $processed_params['m_cat_id']) : '';
+
+ if ((strtolower($template) == '__default__') && ($processed_params['m_cat_id'] == 0)) {
+ // for "Home" category set template to index when not set
+ $template = 'index';
+ }
+
+ // remove template from url if it is category index cached template
+ if (($template == $category_template) || (mb_strtolower($template) == '__default__')) {
+ // given template is also default template for this category or '__default__' given
+ $params['pass_template'] = false;
+ }
+
+ if ($template && $params['pass_template']) {
+ $ret .= $template . '/';
+ }
+
+ return mb_strtolower( rtrim($ret, '/') );
+ }
+
+ /**
* Gets language part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
- function ProcessLanguage(&$url_parts, &$vars)
+ function _parseLanguage(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
- $res = false;
$url_part = array_shift($url_parts);
$sql = 'SELECT LanguageId
@@ -62,15 +410,12 @@
$this->Application->Phrases = new PhrasesCache();
if ($language_id) {
$vars['m_lang'] = $language_id;
- $res = true;
+ return true;
}
-// $this->Application->VerifyLanguageId();
- if (!$res) {
- array_unshift($url_parts, $url_part);
- }
+ array_unshift($url_parts, $url_part);
- return $res;
+ return false;
}
/**
@@ -80,13 +425,12 @@
* @param Array $vars
* @return bool
*/
- function ProcessTheme(&$url_parts, &$vars)
+ function _parseTheme(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
- $res = false;
$url_part = array_shift($url_parts);
$sql = 'SELECT ThemeId
@@ -95,25 +439,90 @@
$theme_id = $this->Conn->GetOne($sql);
if ($theme_id) {
$vars['m_theme'] = $theme_id;
- $res = true;
+ return true;
}
-// $this->Application->VerifyThemeId(); // verify anyway - will set default if not found!!!
- if (!$res) {
- array_unshift($url_parts, $url_part);
+ array_unshift($url_parts, $url_part);
+
+ return false;
+ }
+
+ /**
+ * Checks if whole url_parts matches a whole In-CMS page
+ *
+ * @param array $url_parts
+ * @return boolean
+ */
+ function _parseFriendlyUrl($url_parts, &$vars)
+ {
+ if (!$url_parts) {
+ return false;
}
- return $res;
+ $sql = 'SELECT CategoryId, NamedParentPath
+ FROM ' . TABLE_PREFIX . 'Category
+ WHERE FriendlyURL = ' . $this->Conn->qstr(implode('/', $url_parts));
+
+ $friendly = $this->Conn->GetRow($sql);
+ if ($friendly) {
+ $vars['m_cat_id'] = $friendly['CategoryId'];
+ $vars['t'] = preg_replace('/^Content\//i', '', $friendly['NamedParentPath']);
+ return true;
+ }
+
+ return false;
}
/**
+ * Set's 1st page for all modules (will be used when not passed in url)
+ *
+ * @param Array $vars
+ */
+ function _setDefaultPages(&$vars)
+ {
+ // set 1st page for all rewrite listeners, since we don't know which of them will need it
+ foreach ($this->Application->RewriteListeners as $prefix => $listener) {
+ $vars[$prefix . '_Page'] = 1;
+ }
+ }
+
+ /**
+ * Set's page (when found) to all modules
+ *
+ * @param Array $url_parts
+ * @param Array $vars
+ * @return string
+ */
+ function _parsePage(&$url_parts, &$vars)
+ {
+ if (!$url_parts) {
+ return false;
+ }
+
+ $page_number = end($url_parts);
+ if (!is_numeric($page_number)) {
+ return false;
+ }
+
+ // set module pages for all modules, since we don't know which module will need it
+ foreach ($this->Application->RewriteListeners as $prefix => $listener) {
+ $vars[$prefix . '_id'] = 0;
+ $vars[$prefix . '_Page'] = $page_number;
+ }
+
+ array_pop($url_parts);
+
+ return true;
+ }
+
+ /**
* Extracts category part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
- function ProcessCategory($url_parts, &$vars)
+ function _parseCategory($url_parts, &$vars)
{
if (!$url_parts) {
return false;
@@ -171,47 +580,105 @@
$vars['m_cat_id'] = 0;
}
- /*if ($url_part) {
- array_unshift($url_parts, $url_part);
- }*/
-
return $res;
}
/**
- * Set's 1st page for all modules (will be used when not passed in url)
+ * Builds/parses category item part of url
*
- * @param Array $vars
- * @todo this is temporary fix, until mod-rewrite helper will become category-independend
+ * @param int $rewrite_mode Mode in what rewrite listener was called. Possbile two modes: REWRITE_MODE_BUILD, REWRITE_MODE_PARSE.
+ * @param string $prefix Prefix, that listener uses for system integration
+ * @param Array $params Params, that are used for url building or created during url parsing.
+ * @param Array $url_parts Url parts to parse (only for parsing).
+ * @param bool $keep_events Keep event names in resulting url (only for building).
+ * @return bool Return true to continue to next listener; return false (when building) not to rewrite given prefix; return false (when parsing) to stop processing at this listener.
*/
- function SetDefaultPages(&$vars)
+ function CategoryItemRewriteListener($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts, $keep_events = false)
{
- // set module pages for all modules, since we don't know which module will need it
- foreach ($this->Application->ModuleInfo as $module_name => $module_data) {
- $vars[$module_data['Var'] . '_Page'] = 1;
+ static $parsed = false;
+
+ if ($rewrite_mode == REWRITE_MODE_BUILD) {
+ return $this->_buildCategoryItemUrl($prefix, $params, $keep_events);
}
+
+ if (!$parsed) {
+ $this->_modulePrefix = $this->_parseCategoryItemUrl($url_parts, $params);
+ if ($this->_modulePrefix !== false) {
+ $params['pass'][] = $this->_modulePrefix;
+ $this->_partsFound[] = 'parseCategoryItemUrl';
+ }
+
+ $parsed = true;
+ }
+
+ return true;
}
- function _processTopic(&$url_parts, &$vars, $set_template = true)
+ /**
+ * Build category teim part of url
+ *
+ * @param string $prefix_special
+ * @param Array $params
+ * @param bool $keep_events
+ * @return string
+ */
+ function _buildCategoryItemUrl($prefix_special, &$params, $keep_events)
{
- $sql = 'SELECT c.ParentPath, c.CategoryId
- FROM ' . TABLE_PREFIX . 'Category AS c
- WHERE c.CategoryId = ' . $vars['m_cat_id'];
- $cat_item = $this->Conn->GetRow($sql);
+ $ret = '';
+ list ($prefix) = explode('.', $prefix_special);
+ $processed_params = $this->getProcessedParams($prefix_special, $params, $keep_events);
- if ($set_template) {
- $item_template = $this->GetItemTemplate($cat_item, 'bb');
- $vars['t'] = $item_template;
+ if ($processed_params === false) {
+ return '';
}
- $this->Application->HandleEvent($bb_event, 'bb:ParseEnv', Array ('url_parts' => $url_parts, 'vars' => $vars));
- $url_parts = $bb_event->getEventParam('url_parts');
- $vars = $bb_event->getEventParam('vars');
+ if ($processed_params[$prefix_special . '_id']) {
+ // this allows to fill 3 cache records with one query (see this method for details)
+ $category_id = array_key_exists('m_cat_id', $params) ? $params['m_cat_id'] : $this->Application->GetVar('m_cat_id');
+ $category_filename = $this->Application->getFilename('c', $category_id);
- return 'bb';
+ // if template is also item template of category, then remove template
+ $template = array_key_exists('t', $params) ? $params['t'] : false;
+
+ $item_template = $this->GetItemTemplate($category_id, $prefix);
+
+ if ($template == $item_template || strtolower($template) == '__default__') {
+ // given template is also default template for this category item or '__default__' given
+ $params['pass_template'] = false;
+ }
+
+ // get item's filename
+ if ($prefix == 'bb') {
+ $ret .= 'bb_' . $processed_params[$prefix_special . '_id'] . '/';
+ }
+ else {
+ $filename = $this->Application->getFilename($prefix, $processed_params[$prefix_special . '_id'], $category_id);
+ if ($filename !== false) {
+ $ret .= $filename . '/';
+ }
+ }
+ } else {
+ if ($processed_params[$prefix_special . '_Page'] == 1) {
+ // when printing category items and we are on the 1st page -> there is no information about
+ // category item prefix and $params['pass_category'] will not be added automatically
+ $params['pass_category'] = true;
+ }
+ else {
+ $ret .= $processed_params[$prefix_special . '_Page'] . '/';
+ }
+ }
+
+ return mb_strtolower( rtrim($ret, '/') );
}
- function ProcessModuleItem(&$url_parts, &$vars, $set_template = true)
+ /**
+ * Sets template and id, corresponding to category item given in url
+ *
+ * @param Array $url_parts
+ * @param Array $vars
+ * @return bool|string
+ */
+ function _parseCategoryItemUrl(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
@@ -223,9 +690,11 @@
return false;
}
- if (preg_match('/^bb_[\d]+/', $item_filename)) {
+ if (preg_match('/^bb_([\d]+)/', $item_filename, $regs)) {
// process topics separatly, because they don't use item filenames
- return $this->_processTopic($url_parts, $vars, $set_template);
+ array_pop($url_parts);
+
+ return $this->_parseTopicUrl($regs[1], $vars);
}
// locating the item in CategoryItems by filename to detect its ItemPrefix and its category ParentPath
@@ -250,11 +719,13 @@
array_pop($url_parts);
- if ((!$set_template || $item_template) && $item_id) {
- if ($set_template) {
+ if ($item_id) {
+ if ($item_template) {
+ // when template is found in category -> set it
$vars['t'] = $item_template;
}
+ // we have category item id
$vars[$module_prefix . '_id'] = $item_id;
return $module_prefix;
@@ -265,9 +736,82 @@
}
/**
+ * Set's template and topic id corresponding to topic given in url
+ *
+ * @param int $topic_id
+ * @param Array $vars
+ * @return string
+ */
+ function _parseTopicUrl($topic_id, &$vars)
+ {
+ $sql = 'SELECT c.ParentPath, c.CategoryId
+ FROM ' . TABLE_PREFIX . 'Category AS c
+ WHERE c.CategoryId = ' . $vars['m_cat_id'];
+ $cat_item = $this->Conn->GetRow($sql);
+
+ $item_template = $this->GetItemTemplate($cat_item, 'bb');
+
+ if ($item_template) {
+ $vars['t'] = $item_template;
+ }
+
+ $vars['bb_id'] = $topic_id;
+
+ return 'bb';
+ }
+
+ /**
+ * Returns enviroment variable values for given prefix (uses directly given params, when available)
+ *
+ * @param string $prefix_special
+ * @param Array $params
+ * @param bool $keep_events
+ * @return Array
+ */
+ function getProcessedParams($prefix_special, &$params, $keep_events)
+ {
+ list ($prefix) = explode('.', $prefix_special);
+
+ $query_vars = $this->Application->getUnitOption($prefix, 'QueryString');
+ if (!$query_vars) {
+ // given prefix doesn't use "env" variable to pass it's data
+ return false;
+ }
+
+ $event_key = array_search('event', $query_vars);
+ if ($event_key) {
+ // pass through event of this prefix
+ unset($query_vars[$event_key]);
+ }
+
+ if (array_key_exists($prefix_special . '_event', $params) && !$params[$prefix_special . '_event']) {
+ // if empty event, then remove it from url
+ unset( $params[$prefix_special . '_event'] );
+ }
+
+ // if pass events is off and event is not implicity passed
+ if (!$keep_events && !array_key_exists($prefix_special . '_event', $params)) {
+ unset($params[$prefix_special . '_event']); // remove event from url if requested
+ //otherwise it will use value from get_var
+ }
+
+ $processed_params = Array ();
+ foreach ($query_vars as $index => $var_name) {
+ // if value passed in params use it, otherwise use current from application
+ $var_name = $prefix_special . '_' . $var_name;
+ $processed_params[$var_name] = array_key_exists($var_name, $params) ? $params[$var_name] : $this->Application->GetVar($var_name);
+ if (array_key_exists($var_name, $params)) {
+ unset($params[$var_name]);
+ }
+ }
+
+ return $processed_params;
+ }
+
+ /**
* Returns module item details template specified in given category custom field for given module prefix
*
- * @param mixed $category
+ * @param int|Array $category
* @param string $module_prefix
* @return string
*/
@@ -311,6 +855,28 @@
}
/**
+ * Loads all registered rewrite listeners, so they could be quickly accessed later
+ *
+ */
+ function initRewriteListeners()
+ {
+ static $init_done = false;
+
+ if ($init_done) {
+ return ;
+ }
+
+ foreach ($this->Application->RewriteListeners as $prefix => $listener_data) {
+ list ($listener_prefix, $listener_method) = explode(':', $listener_data['listener']);
+ $listener =& $this->Application->recallObject($listener_prefix);
+
+ $this->Application->RewriteListeners[$prefix] = Array (&$listener, $listener_method);
+ }
+
+ $init_done = true;
+ }
+
+ /**
* Returns category custom field id, where given module prefix item template name is stored
*
* @param string $module_prefix
@@ -333,256 +899,21 @@
return $item_template_field_id;
}
- function ProcessPhisycalTemplate($url_parts, &$vars)
- {
- if (!$url_parts) {
- return false;
- }
-
- do {
- $template_path = implode('/', $url_parts);
-
- $t_parts['path'] = dirname($template_path) == '.' ? '' : '/' . dirname($template_path);
- $t_parts['file'] = basename($template_path);
-
- $sql = 'SELECT FileId
- FROM ' . TABLE_PREFIX . 'ThemeFiles
- WHERE (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')';
- $template_found = $this->Conn->GetOne($sql);
-
- if (!$template_found) {
- array_shift($url_parts);
- }
- } while (!$template_found && $url_parts);
-
- if ($template_found) {
- $vars['t'] = $template_path;
- return true;
- }
-
- return false;
- }
-
/**
- * Set's page (when found) to all modules
+ * Sets default parsed values before actual url parsing
*
- * @param Array $url_parts
* @param Array $vars
- * @return string
*/
- function ProcessPage(&$url_parts, &$vars)
+ function _setDefaultValues(&$vars)
{
- if (!$url_parts) {
- return false;
- }
+ $defaults = Array ('m_cat_id' => 0, 'm_cat_page' => 1, 'm_opener' => 's', 't' => 'index');
- $page_number = end($url_parts);
- if (!is_numeric($page_number)) {
- return false;
- }
-
- // set module pages for all modules, since we don't know which module will need it
- foreach ($this->Application->ModuleInfo as $module_name => $module_data) {
- $vars[ $module_data['Var'] . '_id'] = 0;
- $vars[ $module_data['Var'] . '_Page'] = $page_number;
- }
-
- array_pop($url_parts);
-
- return true;
- }
-
- /**
- * Checks if whole url_parts matches a whole In-CMS page
- *
- * @param array $url_parts
- * @return boolean
- */
- function ProcessFriendlyUrl($url_parts, &$vars)
- {
- if (!$url_parts) {
- return false;
- }
-
- $sql = 'SELECT CategoryId, NamedParentPath
- FROM ' . TABLE_PREFIX . 'Category
- WHERE FriendlyURL = ' . $this->Conn->qstr(implode('/', $url_parts));
-
- $friendly = $this->Conn->GetRow($sql);
- if ($friendly) {
- $vars['m_cat_id'] = $friendly['CategoryId'];
- $vars['t'] = preg_replace('/^Content\//i', '', $friendly['NamedParentPath']);
- return true;
- }
-
- return false;
- }
-
- function processRewriteURL()
- {
- $passed = Array ();
- $url = $this->HTTPQuery->Get('_mod_rw_url_');
- if (substr($url, -5) == '.html') {
- $url = substr($url, 0, strlen($url) - 5);
- }
-
- $restored = false;
- $sql = 'SELECT Data, Cached
- FROM ' . TABLE_PREFIX . 'Cache
- WHERE VarName = "mod_rw_' . md5($url) . '"';
- $cache = $this->Conn->GetRow($sql);
-
- if (false && $cache && $cache['Cached'] > 0) {
- // not used for now
- $cache = unserialize($cache['Data']);
- $vars = $cache['vars'];
- $passed = $cache['passed'];
- $restored = true;
- }
- else {
- $passed = Array ();
- $vars = $this->parseRewriteURL($url, $passed);
- $cache = Array ('vars' => $vars, 'passed' => $passed);
-
- $fields_hash = Array (
- 'VarName' => 'mod_rw_' . md5($url),
- 'Data' => serialize($cache),
- 'Cached' => adodb_mktime(),
- );
- $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Cache', 'REPLACE');
-
- if (array_key_exists('t', $this->HTTPQuery->Post) && $this->HTTPQuery->Post['t']) {
- // template from POST overrides template from URL.
- $vars['t'] = $this->HTTPQuery->Post['t'];
- if (isset($vars['is_virtual']) && $vars['is_virtual']) {
- $vars['m_cat_id'] = 0; // this is virtual template category (for Proj-CMS)
- }
+ foreach ($defaults as $default_key => $default_value) {
+ // bug: null is never returned
+ if ($this->HTTPQuery->Get($default_key) == null) {
+ $vars[$default_key] = $default_value;
}
-
- unset($vars['is_virtual']);
}
-
- foreach ($vars as $name => $value) {
- $this->HTTPQuery->Set($name,$value);
- }
-
-// if ($restored) {
- $this->InitAll();
-// }
-
- $this->HTTPQuery->finalizeParsing($passed);
}
-
- function InitAll()
- {
-// $this->Application->Phrases = new PhrasesCache();
- $this->Application->VerifyLanguageId();
- $this->Application->Phrases->Init('phrases');
- $this->Application->VerifyThemeId();
- }
-
- function parseRewriteURL($url, &$passed)
- {
- $sql = 'SELECT Data, Cached
- FROM ' . TABLE_PREFIX . 'Cache
- WHERE VarName = "mod_rw_' . md5($url) . '"';
- $vars = $this->Conn->GetRow($sql);
-
- if (false && $vars && $vars['Cached'] > 0) {
- // not used for now
- $vars = unserialize($menu['Data']);
- return $vars;
- }
-
- $vars = Array ();
- $url_parts = $url ? explode('/', trim($url, '/')) : Array ();
-
- $process_module = true;
- if ($this->HTTPQuery->Get('rewrite') == 'on' || !$url_parts) {
- $this->SetDefaultValues($vars);
- }
-
- if (!$url_parts) {
- $this->InitAll();
-
- $vars['t'] = $this->HTTPQuery->getDefaultTemplate('');
- $passed[] = 'm';
-
- return $vars;
- }
- else {
- $vars['t'] = '';
- }
-
- $passed = Array ('m');
- $this->ProcessLanguage($url_parts, $vars);
- $this->ProcessTheme($url_parts, $vars);
-
- if ( $this->ProcessFriendlyUrl($url_parts, $vars) ) {
- // friendly urls work like exact match only!
- return $vars;
- }
-
- // http://site-url///[_]//
- // http://site-url///[_]/ (category-based section template)
- // http://site-url///[_]//
- // http://site-url///[_]/ (category-based detail template)
-
- $found = Array ();
-
- $category_found = $this->ProcessCategory($url_parts, $vars);
- if ($category_found) {
- $found[] = 'ProcessCategory';
- }
-
- $this->SetDefaultPages($vars);
-
- $page_found = $this->ProcessPage($url_parts, $vars);
- if ($page_found) {
- $found[] = 'ProcessPage';
- }
-
- $module_prefix = $this->ProcessModuleItem($url_parts, $vars);
- if ($module_prefix !== false) {
- $passed[] = $module_prefix;
- $found[] = 'ProcessModuleItem';
- }
-
- $template_found = $this->ProcessPhisycalTemplate($url_parts, $vars);
- if ($template_found) {
- $found[] = 'ProcessPhisycalTemplate';
- }
-
- if (($module_prefix === false) && in_array('ProcessCategory', $found)) {
- // no item found, but category found -> module index page
- /*if (!$vars['t']) {
- // no template found before -> assume it's index
- $vars['t'] = 'index';
- }*/
-
- foreach ($this->Application->ModuleInfo as $module_name => $info) {
- // no idea what module we are talking about, so pass info form all modules
- $passed[] = $info['Var'];
- }
-
- return $vars;
- }
-
- if (!$found) {
- $not_found = $this->Application->ConfigValue('ErrorTemplate');
- $vars['t'] = $not_found ? $not_found : 'error_notfound';
-
- $themes_helper =& $this->Application->recallObject('ThemesHelper');
- /* @var $themes_helper kThemesHelper */
-
- $vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t']);
-
- header('HTTP/1.0 404 Not Found');
- }
-
- return $vars;
-
-// $this->HTTPQuery->finalizeParsing($passed, $module_params);
- }
}
?>
\ No newline at end of file
Index: branches/5.0.x/core/units/general/general_config.php
===================================================================
diff -u -r12117 -r12294
--- branches/5.0.x/core/units/general/general_config.php (.../general_config.php) (revision 12117)
+++ branches/5.0.x/core/units/general/general_config.php (.../general_config.php) (revision 12294)
@@ -1,6 +1,6 @@
true,
+ 'RewritePriority' => 100,
+ 'RewriteListener' => 'ModRewriteHelper:MainRewriteListener',
+
'PermTabText' => 'In-Portal',
'PermSection' => Array('search' => 'in-portal:configuration_search', 'custom' => 'in-portal:configuration_custom'),
Index: branches/5.0.x/core/kernel/db/dbitem.php
===================================================================
diff -u -r12264 -r12294
--- branches/5.0.x/core/kernel/db/dbitem.php (.../dbitem.php) (revision 12264)
+++ branches/5.0.x/core/kernel/db/dbitem.php (.../dbitem.php) (revision 12294)
@@ -1,6 +1,6 @@
setID($new_id);
$this->Loaded = false;
$this->FieldValues = Array();
$this->OriginalFieldValues = Array ();
- $this->SetDefaultValues();
+ $this->SetDefaultValues(); // will wear off kDBItem::setID effect, so set it later
$this->FieldErrors = Array();
+
+ $this->setID($new_id);
+
return $this->Loaded;
}
Index: branches/5.0.x/core/kernel/utility/unit_config_reader.php
===================================================================
diff -u -r12277 -r12294
--- branches/5.0.x/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 12277)
+++ branches/5.0.x/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 12294)
@@ -1,6 +1,6 @@
$this->Application->ConfigCacheIds,
'Application.ConfigHash' => $this->Application->ConfigHash,
'Application.ReplacementTemplates' => $this->Application->ReplacementTemplates,
+ 'Application.RewriteListeners' => $this->Application->RewriteListeners,
'Application.ModuleInfo' => $this->Application->ModuleInfo,
);
@@ -153,6 +154,7 @@
$this->Application->ConfigCacheIds = $cache['Application.ConfigCacheIds'];
$this->Application->ReplacementTemplates = $cache['Application.ReplacementTemplates'];
+ $this->Application->RewriteListeners = $cache['Application.RewriteListeners'];
$this->Application->ModuleInfo = $cache['Application.ModuleInfo'];
@@ -336,6 +338,7 @@
}
if ($store_cache) {
+ $this->_sortRewriteListeners();
$this->CacheParsedData();
if ($this->Application->isDebugMode(false) && constOn('DBG_VALIDATE_CONFIGS')) {
@@ -352,6 +355,34 @@
}
/**
+ * Sort rewrite listeners according to RewritePriority (non-prioritized listeners goes first)
+ *
+ */
+ function _sortRewriteListeners()
+ {
+ $listeners = Array ();
+ $prioritized_listeners = Array ();
+
+ // process non-prioritized listeners
+ foreach ($this->Application->RewriteListeners as $prefix => $listener_data) {
+ if ($listener_data['priority'] === false) {
+ $listeners[$prefix] = $listener_data;
+ }
+ else {
+ $prioritized_listeners[$prefix] = $listener_data['priority'];
+ }
+ }
+
+ // process prioritized listeners
+ asort($prioritized_listeners);
+ foreach ($prioritized_listeners as $prefix => $priority) {
+ $listeners[$prefix] = $this->Application->RewriteListeners[$prefix];
+ }
+
+ $this->Application->RewriteListeners = $listeners;
+ }
+
+ /**
* Re-reads all configs
*
*/
@@ -514,10 +545,22 @@
}
}
- if (isset($config['ReplacementTemplates']) && $config['ReplacementTemplates']) {
+ if (array_key_exists('ReplacementTemplates', $config) && $config['ReplacementTemplates']) {
// replacement templates defined in this config
- $this->Application->ReplacementTemplates = array_merge_recursive2($this->Application->ReplacementTemplates, $config['ReplacementTemplates']);
+ $this->Application->ReplacementTemplates = array_merge($this->Application->ReplacementTemplates, $config['ReplacementTemplates']);
}
+
+ if (array_key_exists('RewriteListener', $config) && $config['RewriteListener']) {
+ // replacement templates defined in this config
+ $rewrite_listener = $config['RewriteListener'];
+ if (strpos($rewrite_listener, ':') === false) {
+ $rewrite_listener = $prefix . '_EventHandler:' . $rewrite_listener;
+ }
+
+ $rewrite_priority = array_key_exists('RewritePriority', $config) ? $config['RewritePriority'] : false;
+
+ $this->Application->RewriteListeners[$prefix] = Array ('listener' => $rewrite_listener, 'priority' => $rewrite_priority);
+ }
}
function ValidateConfig($prefix)
Index: branches/5.0.x/core/kernel/event_handler.php
===================================================================
diff -u -r12117 -r12294
--- branches/5.0.x/core/kernel/event_handler.php (.../event_handler.php) (revision 12117)
+++ branches/5.0.x/core/kernel/event_handler.php (.../event_handler.php) (revision 12294)
@@ -1,6 +1,6 @@
mapEvents();
$this->mapPermissions();
}
@@ -103,16 +103,17 @@
*/
function processEvent(&$event)
{
- $event_name=$event->Name;
- if( isset($this->eventMethods[$event_name]) ) $event_name=$this->eventMethods[$event_name];
+ $event_name = $event->Name;
- if( method_exists($this,$event_name) )
- {
+ if ( array_key_exists($event_name, $this->eventMethods) ) {
+ $event_name = $this->eventMethods[$event_name];
+ }
+
+ if (method_exists($this, $event_name)) {
$this->$event_name($event);
}
- else
- {
- trigger_error('event '.$event->Name.' not implemented in class '.get_class($this).'', E_USER_ERROR);
+ else {
+ trigger_error('event ' . $event->Name . ' not implemented in class ' . get_class($this) . '', E_USER_ERROR);
}
}
@@ -137,7 +138,7 @@
{
$url = $this->Application->RecallVar('export_finish_url');
if ($url) {
- $this->Application->Redirect('external:'.$url);
+ $this->Application->Redirect('external:' . $url);
}
$event->SetRedirectParam('opener', 'u');
@@ -164,168 +165,27 @@
* @param kEvent $event
* @access protected
*/
- function &inheritEvent(&$event, $name=null)
+ function &inheritEvent(&$event, $name = null)
{
$child_event = new kEvent();
$child_event->MasterEvent =& $event;
- $child_event->Prefix=$event->Prefix;
- $child_event->Special=$event->Special;
- $child_event->Prefix_Special=$event->Prefix_Special;
+ $child_event->Prefix = $event->Prefix;
+ $child_event->Special = $event->Special;
+ $child_event->Prefix_Special = $event->Prefix_Special;
$child_event->Name = $name;
+
return $child_event;
}
/**
- * Created url part for this module
- *
- * @param kEvent $event
- */
- function BuildEnv(&$event)
- {
- $prefix_special = $event->getPrefixSpecial();
- $url_params = $event->getEventParam('url_params');
- $pass_events = $event->getEventParam('pass_events');
-
- $query_vars = $this->Application->getUnitOption($event->Prefix, 'QueryString');
-
- $event_key = array_search('event', $query_vars);
- if ($event_key) {
- // pass through event of this prefix
- unset($query_vars[$event_key]);
- }
-
- if (!getArrayValue($url_params, $prefix_special.'_event')) {
- // if empty event, then remove it from url
- unset( $url_params[$prefix_special.'_event'] );
- }
-
- //if pass events is off and event is not implicity passed
- if ( !$pass_events && !isset($url_params[$prefix_special.'_event']) ) {
- unset($url_params[$prefix_special.'_event']); // remove event from url if requested
- //otherwise it will use value from get_var
- }
-
- if(!$query_vars) return true;
-
- $processed_params = Array();
- foreach($query_vars as $index => $var_name) {
- //if value passed in params use it, otherwise use current from application
- $var_name = $prefix_special.'_'.$var_name;
- $processed_params[$var_name] = isset( $url_params[$var_name] ) ? $url_params[$var_name] : $this->Application->GetVar($var_name);
- if ( isset($url_params[$var_name]) ) unset( $url_params[$var_name] );
- }
-
- $ret = '';
-
- if (!$processed_params[$prefix_special.'_id']) {
- if ($processed_params[$prefix_special.'_Page'] == 1) {
- // when printing "items" from category, there is 1st page -> nothing from "item prefix" in url
- // and auto-guess pass_category will not be added to url
- $url_params['pass_category'] = 1;
- }
- else {
- $ret .= $processed_params[$prefix_special.'_Page'].'/';
- }
- }
-
- if ($processed_params[$prefix_special.'_id']) {
- // this allows to fill 3 cache records with one query (see this method for details)
- $category_id = isset($url_params['m_cat_id']) ? $url_params['m_cat_id'] : $this->Application->GetVar('m_cat_id');
- $category_filename = $this->Application->getFilename('c', $category_id);
-
- // if template is also item template of category, then remove template
- $template = getArrayValue($url_params, 't');
-
- $mod_rw_helper =& $this->Application->recallObject('ModRewriteHelper');
- /* @var $mod_rw_helper kModRewriteHelper */
-
- $item_template = $mod_rw_helper->GetItemTemplate($category_id, $event->Prefix);
-
- if ($template == $item_template || strtolower($template) == '__default__') {
- unset($url_params['t']);
- }
-
- // get item's filename
- $filename = $this->Application->getFilename($event->Prefix, $processed_params[$prefix_special.'_id'], $category_id );
- if($filename !== false) $ret .= $filename.'/';
- }
-
- $event->setEventParam('url_params', $url_params);
- $event->setEventParam('env_string', mb_strtolower($ret) );
- }
-
- /**
- * Process mod_rewrite url part left after previous parser
- *
- * @param kEvent $event
- */
- function ParseEnv(&$event)
- {
- // /
-
- $url_parts = $event->getEventParam('url_parts');
- $vars = $event->getEventParam('vars');
-
- $defaults = Array('id' => 0, 'Page' => 1);
- foreach ($defaults as $var_name => $var_value) {
- $this->Application->SetVar($event->getPrefixSpecial().'_'.$var_name, $var_value);
- $vars[$event->getPrefixSpecial().'_'.$var_name] = $var_value;
- }
-
- if (!$url_parts) {
-// $event->status = erFAIL;
- return false;
- }
-
- $ret = '';
- $url_part = array_shift($url_parts);
-
- // match module page
- if ( is_numeric($url_part) ) {
- $this->Application->SetVar( $event->getPrefixSpecial().'_Page', $url_part);
- $vars[$event->getPrefixSpecial().'_Page'] = $url_part;
- $url_part = $url_parts ? array_shift($url_parts) : '';
- }
-
- if (!$url_part) {
- $event->setEventParam('url_parts', $url_parts);
-// $event->status = erFAIL;
- return true;
- }
-
- // match item's filename
- $db =& $this->Application->GetADODBConnection();
- $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
- $table = $this->Application->getUnitOption($event->Prefix, 'TableName');
-
- $sql = 'SELECT item_table.'.$id_field.'
- FROM '.$table.' item_table
- LEFT JOIN '.TABLE_PREFIX.'CategoryItems cat_items ON item_table.ResourceId = cat_items.ItemResourceId
- WHERE (item_table.Filename = '.$db->qstr($url_part).') AND (cat_items.CategoryId = '.$this->Application->GetVar('m_cat_id').')';
-
- $item_id = $db->GetOne($sql);
- if ($item_id !== false) {
- $this->Application->SetVar($event->getPrefixSpecial().'_id', $item_id);
- $vars[$event->getPrefixSpecial().'_id'] = $item_id;
- }
- elseif ($url_part !== 'index') {
- // otherwise category/index.html is parsed as /index.tpl
- array_unshift($url_parts, $url_part);
- $event->status = erFAIL;
- }
-
- $event->setEventParam('url_parts', $url_parts);
- $event->setEventParam('vars', $vars);
- }
-
- /**
* Checks permissions of user
*
* @param kEvent $event
*/
function CheckPermission(&$event)
{
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
+
return $perm_helper->CheckEventPermission($event, $this->permMapping);
}
Index: branches/5.0.x/core/units/general/main_event_handler.php
===================================================================
diff -u -r12117 -r12294
--- branches/5.0.x/core/units/general/main_event_handler.php (.../main_event_handler.php) (revision 12117)
+++ branches/5.0.x/core/units/general/main_event_handler.php (.../main_event_handler.php) (revision 12294)
@@ -1,6 +1,6 @@
Conn =& $this->Application->GetADODBConnection();
}
-
- /**
- * Created url part for this module
- *
- * @param kEvent $event
- */
- function BuildEnv(&$event)
- {
- $prefix_special = $event->getPrefixSpecial();
- $url_params = $event->getEventParam('url_params');
-
- $query_vars = $this->Application->getUnitOption($event->Prefix, 'QueryString');
-
- //if pass events is off and event is not implicity passed
- if ( !$event->getEventParam('pass_events') && !isset($url_params[$prefix_special.'_event']) )
- {
- $url_params[$prefix_special.'_event'] = ''; // remove event from url if requested
- //otherwise it will use value from get_var
- }
-
- if(!$query_vars) return true;
-
- $processed_params = Array();
- foreach($query_vars as $index => $var_name)
- {
- //if value passed in params use it, otherwise use current from application
- $var_name = $prefix_special.'_'.$var_name;
- $processed_params[$var_name] = isset( $url_params[$var_name] ) ? $url_params[$var_name] : $this->Application->GetVar($var_name);
- if ( isset($url_params[$var_name]) ) unset( $url_params[$var_name] );
- }
-
- $ret = '';
- $default_language_id = $this->Application->GetDefaultLanguageId();
- if( $processed_params['m_lang'] && ($processed_params['m_lang'] != $default_language_id) )
- {
- $language_name = $this->Application->getCache('language_names', $processed_params['m_lang']);
- if ($language_name === false) {
- $sql = 'SELECT PackName
- FROM '.TABLE_PREFIX.'Language
- WHERE LanguageId = '.$processed_params['m_lang'];
- $language_name = $this->Conn->GetOne($sql);
- $this->Application->setCache('language_names', $processed_params['m_lang'], $language_name);
- }
- $ret .= $language_name.'/';
- }
-
- $default_theme_id = $this->Application->GetDefaultThemeId();
- if( $processed_params['m_theme'] && ($processed_params['m_theme'] != $default_theme_id) )
- {
- $theme_name = $this->Application->getCache('theme_names', $processed_params['m_theme']);
- if ($theme_name === false) {
- $sql = 'SELECT Name
- FROM '.TABLE_PREFIX.'Theme
- WHERE ThemeId = '.$processed_params['m_theme'];
- $theme_name = $this->Conn->GetOne($sql);
- $this->Application->setCache('theme_names', $processed_params['m_theme'], $theme_name);
-
- }
- $ret .= $theme_name.'/';
- }
-
- if ($processed_params['m_cat_id'] > 0 && isset($url_params['pass_category']) && $url_params['pass_category']) {
- $category_filename = $this->Application->getFilename('c', $processed_params['m_cat_id']);
- $t = isset($url_params['t']) && $url_params['t'] ? $url_params['t'] : false;
-
- // we could have category path like "Content/" in this case remove template (proj-cms trick)
- preg_match('/^Content\/(.*)/i', $category_filename, $regs);
-
- if ($regs) {
- if (strtolower($regs[1]) == strtolower($t)) {
- $url_params['t'] = '';
- }
-
- $ret .= $regs[1] . '/';
- }
-
- $url_params['category_processed'] = true;
- }
- else {
-// $url_params['no_category'] = 1; // for debugging
- }
- unset($url_params['pass_category']); // unset it just in case
-
- $force_page_adding = false;
- if (getArrayValue($url_params, 'reset'))
- {
- unset($url_params['reset']);
- if ($processed_params['m_cat_id'])
- {
- $processed_params['m_cat_page'] = 1;
- $force_page_adding = true;
- }
- }
-
- if( (array_key_exists('category_processed', $url_params) && $url_params['category_processed'] && ($processed_params['m_cat_page'] > 1)) || $force_page_adding ) {
- // category page number found & category name was added before
- $ret = preg_replace('/(.*)\//', '\\1', $ret).'_'.$processed_params['m_cat_page'].'/';
- }
-
- $template = getArrayValue($url_params, 't');
- $category_template = $processed_params['m_cat_id'] ? $this->Application->getCache('category_designs', $processed_params['m_cat_id']) : '';
-
- if ((strtolower($template) == '__default__') && ($processed_params['m_cat_id'] == 0)) {
- // for "Home" category set template to index when not set
- $template = 'index';
- }
-
- // remove template from url if it is category index cached template
- if ($template == $category_template || mb_strtolower($template) == '__default__') {
- $template = '';
- }
-
- if ($template) {
- $ret .= $template.'/';
- }
- unset($url_params['t']);
-
- $event->setEventParam('url_params', $url_params);
- $event->setEventParam('env_string', mb_strtolower($ret) );
- }
}
?>
\ No newline at end of file
Index: branches/5.0.x/core/kernel/event_manager.php
===================================================================
diff -u -r12167 -r12294
--- branches/5.0.x/core/kernel/event_manager.php (.../event_manager.php) (revision 12167)
+++ branches/5.0.x/core/kernel/event_manager.php (.../event_manager.php) (revision 12294)
@@ -1,6 +1,6 @@
status == erSUCCESS || $event->status == erPERM_FAIL) && ($event->redirect === true || strlen($event->redirect) > 0)) {
// we need to pass category if the action was submitted to self-template, with the category passed
// and it has not explicly set redirect template or pass_cateogry param
- if ($event->redirect === true && !isset($event->redirect_params['pass_category']) && $this->Application->GetVar('m_cat_id')) {
+ if ($event->redirect === true && !array_key_exists('pass_category', $event->redirect_params) && $this->Application->GetVar('m_cat_id')) {
$event->redirect_params['pass_category'] = 1;
}