Index: branches/RC/core/units/general/helpers/mod_rewrite_helper.php =================================================================== diff -u -N -r11682 -r11838 --- branches/RC/core/units/general/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 11682) +++ branches/RC/core/units/general/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 11838) @@ -18,8 +18,8 @@ function SetDefaultValues(&$vars) { $defaults = Array('m_cat_id' => 0, 'm_cat_page' => 1, 'm_opener' => 's'); - foreach ($defaults as $default_key => $default_value) - { + foreach ($defaults as $default_key => $default_value) { + // bug: null is never returned if ($this->HTTPQuery->Get($default_key) == null) { $vars[$default_key] = $default_value; } @@ -28,15 +28,19 @@ function ProcessLanguage(&$url_parts, &$vars) { - if (!isset($url_parts[0])) return false; + if (!isset($url_parts[0])) { + return false; + } + $res = false; $url_part = array_shift($url_parts); - $sql = 'SELECT LanguageId FROM '.TABLE_PREFIX.'Language WHERE LOWER(PackName) = '.$this->Conn->qstr($url_part).' AND Enabled = 1'; + $sql = 'SELECT LanguageId + FROM ' . TABLE_PREFIX . 'Language + WHERE LOWER(PackName) = ' . $this->Conn->qstr($url_part) . ' AND Enabled = 1'; $language_id = $this->Conn->GetOne($sql); $this->Application->Phrases = new PhrasesCache(); - if($language_id) - { + if ($language_id) { $vars['m_lang'] = $language_id; $res = true; } @@ -45,26 +49,33 @@ if (!$res) { array_unshift($url_parts, $url_part); } + return $res; } function ProcessTheme(&$url_parts, &$vars) { - if (!isset($url_parts[0])) return false; + if (!isset($url_parts[0])) { + return false; + } + $res = false; $url_part = array_shift($url_parts); - $sql = 'SELECT ThemeId FROM '.TABLE_PREFIX.'Theme WHERE LOWER(Name) = '.$this->Conn->qstr($url_part).' AND Enabled = 1'; + $sql = 'SELECT ThemeId + FROM ' . TABLE_PREFIX . 'Theme + WHERE LOWER(Name) = ' . $this->Conn->qstr($url_part) . ' AND Enabled = 1'; $theme_id = $this->Conn->GetOne($sql); - if($theme_id) - { + if ($theme_id) { $vars['m_theme'] = $theme_id; $res = true; } + // $this->Application->VerifyThemeId(); // verify anyway - will set default if not found!!! if (!$res) { array_unshift($url_parts, $url_part); } + return $res; } @@ -84,7 +95,7 @@ do { $category_path = trim($category_path . '/' . $url_part, '/'); // bb__ -> forums/bb__2 OR forums/bb_2_6 - if( !preg_match('/^bb_([\d]+)*_[\d]+/', $url_part) && preg_match('/(.*)_([\d]+)$/', $category_path, $rets) ) { + if ( !preg_match('/^bb_[\d]+$/', $url_part) && preg_match('/(.*)_([\d]+)$/', $category_path, $rets) ) { $category_path = $rets[1]; $vars['m_cat_page'] = $rets[2]; } @@ -140,9 +151,9 @@ // 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'] = $url_parts[0]; - $vars[ $module_data['Var'].'_Reviews_Page'] = 0; + $vars[ $module_data['Var'] . '_id'] = 0; + $vars[ $module_data['Var'] . '_Page'] = $url_parts[0]; + $vars[ $module_data['Var'] . '_Reviews_Page'] = 0; } return true; } @@ -156,9 +167,8 @@ function SetDefaultPages(&$vars) { // 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; + foreach ($this->Application->ModuleInfo as $module_name => $module_data) { + $vars[ $module_data['Var'] . '_Page'] = 1; } } @@ -169,7 +179,7 @@ array_shift($url_parts); } - if( $url_parts ) { // if parts are left, it can only be module page + if ( $url_parts ) { // if parts are left, it can only be module page if (preg_match('/^bb_(.*)/', $url_parts[0], $regs)) { list ($topic_id, $topic_page) = explode('_', $regs[1], 2); if (!$topic_id) { @@ -192,12 +202,12 @@ return true; } - function ProcessModuleItem(&$url_parts, &$vars, $set_t=true) + function ProcessModuleItem(&$url_parts, &$vars, $set_t = true) { - if (preg_match('/^bb_.*/', $url_parts[0])) { + if (preg_match('/^bb_[\d]+/', $url_parts[0])) { $sql = 'SELECT c.ParentPath, c.CategoryId - FROM '.TABLE_PREFIX.'Category AS c - WHERE c.CategoryId = '.$vars['m_cat_id']; + FROM ' . TABLE_PREFIX . 'Category AS c + WHERE c.CategoryId = ' . $vars['m_cat_id']; $cat_item = $this->Conn->GetRow($sql); if ($set_t) { @@ -207,30 +217,25 @@ $this->Application->HandleEvent($bb_event, 'bb:ParseEnv', Array('url_parts' => $url_parts, 'vars' => $vars)); $vars = $bb_event->getEventParam('vars'); + return 'bb'; } - if (!isset($url_parts[0])) return false; - if ( count($url_parts) != 1 ) return false; - $url_part = array_shift($url_parts); - - // match module reviews page - $page = 1; - if( preg_match('/(.*)_([\d]+)$/', $url_part, $rets) ) - { - $url_part = $rets[1]; - $page = $rets[2]; + if (!isset($url_parts[0]) || (count($url_parts) != 1)) { + return false; } + $url_part = array_shift($url_parts); + // locating the item in CategoryItems by filename to detect its ItemPrefix and its category ParentPath $cat_item = $this->Conn->GetRow(' - SELECT ci.ItemResourceId, ci.ItemPrefix, c.ParentPath, ci.CategoryId FROM '.TABLE_PREFIX.'CategoryItems AS ci - LEFT JOIN '.TABLE_PREFIX.'Category AS c + SELECT ci.ItemResourceId, ci.ItemPrefix, c.ParentPath, ci.CategoryId FROM ' . TABLE_PREFIX . 'CategoryItems AS ci + LEFT JOIN ' . TABLE_PREFIX . 'Category AS c ON c.CategoryId = ci.CategoryId WHERE - ci.CategoryId = '.$vars['m_cat_id'].' + ci.CategoryId = ' . $vars['m_cat_id'] . ' AND - ci.Filename = '.$this->Conn->qstr($url_part)); + ci.Filename = ' . $this->Conn->qstr($url_part)); if ($cat_item !== false) { // item found $module_prefix = $cat_item['ItemPrefix']; @@ -239,16 +244,16 @@ // converting ResourceId to correpsonding Item id $module_config = $this->Application->getUnitOptions($module_prefix); $item_id = $this->Conn->GetOne( - 'SELECT '.$module_config['IDField'].' FROM '.$module_config['TableName'].' - WHERE ResourceId = '.$cat_item['ItemResourceId'] + 'SELECT ' . $module_config['IDField'] . ' FROM ' . $module_config['TableName'] . ' + WHERE ResourceId = ' . $cat_item['ItemResourceId'] ); if ((!$set_t || $item_template) && $item_id) { if ($set_t) { $vars['t'] = $item_template; } - $vars[ $module_prefix.'_id' ] = $item_id; - $vars[ $module_prefix.'_Reviews_Page' ] = $page; + + $vars[ $module_prefix . '_id' ] = $item_id; return $module_prefix; } } @@ -270,8 +275,8 @@ $category = $this->Application->findModule('Var', $module_prefix, 'RootCat'); } $sql = 'SELECT c.ParentPath, c.CategoryId - FROM '.TABLE_PREFIX.'Category AS c - WHERE c.CategoryId = '.$category; + FROM ' . TABLE_PREFIX . 'Category AS c + WHERE c.CategoryId = ' . $category; $category = $this->Conn->GetRow($sql); } $parent_path = implode(',',explode('|', substr($category['ParentPath'], 1, -1))); @@ -280,14 +285,14 @@ $item_template_field_id = $this->_getItemTemplateCustomField($module_prefix); // looking for item template through cats hierarchy sorted by parent path - $query = 'SELECT ccd.l1_cust_'.$item_template_field_id.', - FIND_IN_SET(c.CategoryId, '.$this->Conn->qstr($parent_path).') AS Ord1, - c.CategoryId, c.Name, ccd.l1_cust_'.$item_template_field_id.' - FROM '.TABLE_PREFIX.'Category AS c - LEFT JOIN '.TABLE_PREFIX.'CategoryCustomData AS ccd + $query = 'SELECT ccd.l1_cust_' . $item_template_field_id . ', + FIND_IN_SET(c.CategoryId, ' . $this->Conn->qstr($parent_path) . ') AS Ord1, + c.CategoryId, c.Name, ccd.l1_cust_' . $item_template_field_id . ' + FROM ' . TABLE_PREFIX . 'Category AS c + LEFT JOIN ' . TABLE_PREFIX . 'CategoryCustomData AS ccd ON ccd.ResourceId = c.ResourceId - WHERE c.CategoryId IN ('.$parent_path.') AND ccd.l1_cust_'.$item_template_field_id.' != \'\' - ORDER BY FIND_IN_SET(c.CategoryId, '.$this->Conn->qstr($parent_path).') DESC'; + WHERE c.CategoryId IN (' . $parent_path . ') AND ccd.l1_cust_' . $item_template_field_id . ' != \'\' + ORDER BY FIND_IN_SET(c.CategoryId, ' . $this->Conn->qstr($parent_path) . ') DESC'; $item_template = $this->Conn->GetOne($query); $this->Application->setCache(__CLASS__ . __FUNCTION__, $cache_key, $item_template); @@ -303,8 +308,8 @@ } $sql = 'SELECT CustomFieldId - FROM '.TABLE_PREFIX.'CustomField - WHERE FieldName = '.$this->Conn->qstr($module_prefix.'_ItemTemplate'); + FROM ' . TABLE_PREFIX . 'CustomField + WHERE FieldName = ' . $this->Conn->qstr($module_prefix . '_ItemTemplate'); $item_template_field_id = $this->Conn->GetOne($sql); $this->Application->setCache(__CLASS__ . __FUNCTION__, $module_prefix, $item_template_field_id); @@ -321,16 +326,16 @@ { $template_path = implode('/', $url_parts); - $t_parts['path'] = dirname($template_path) == '.' ? '' : '/'.dirname($template_path); + $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').')'; + FROM ' . TABLE_PREFIX . 'ThemeFiles + WHERE (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')'; -// $sql = 'SELECT FileId FROM '.TABLE_PREFIX.'ThemeFiles WHERE CONCAT(FilePath, "/", FileName) = '.$this->Conn->qstr('/'.$template_path.'.tpl'); +// $sql = 'SELECT FileId FROM ' . TABLE_PREFIX . 'ThemeFiles WHERE CONCAT(FilePath, "/", FileName) = ' . $this->Conn->qstr('/' . $template_path . '.tpl'); $template_found = $this->Conn->GetOne($sql); - if(!$template_found) + if (!$template_found) { array_unshift($remaining, array_pop($url_parts)); } @@ -373,10 +378,10 @@ { $passed = array(); $url = $this->HTTPQuery->Get('_mod_rw_url_'); - if( substr($url, -5) == '.html' ) $url = substr($url, 0, strlen($url) - 5 ); + if ( substr($url, -5) == '.html' ) $url = substr($url, 0, strlen($url) - 5 ); $restored = false; - $cache = $this->Conn->GetRow('SELECT Data, Cached FROM '.TABLE_PREFIX.'Cache WHERE VarName = "mod_rw_'.md5($url).'"'); + $cache = $this->Conn->GetRow('SELECT Data, Cached FROM ' . TABLE_PREFIX . 'Cache WHERE VarName = "mod_rw_' . md5($url) . '"'); if (false && $cache && $cache['Cached'] > 0) { $cache = unserialize($cache['Data']); $vars = $cache['vars']; @@ -387,7 +392,7 @@ $passed = array(); $vars = $this->parseRewriteURL($url,$passed); $cache = array('vars'=>$vars,'passed'=>$passed); - $this->Conn->Query('REPLACE '.TABLE_PREFIX.'Cache (VarName, Data, Cached) VALUES ("mod_rw_'.md5($url).'", '.$this->Conn->qstr(serialize($cache)).', '.adodb_mktime().')'); + $this->Conn->Query('REPLACE ' . TABLE_PREFIX . 'Cache (VarName, Data, Cached) VALUES ("mod_rw_' . md5($url) . '", ' . $this->Conn->qstr(serialize($cache)) . ', ' . adodb_mktime() . ')'); if (isset($this->HTTPQuery->Post['t']) && $this->HTTPQuery->Post['t']) { // template from POST overrides template from URL. @@ -418,7 +423,7 @@ function parseRewriteURL($url, &$passed) { - $vars = $this->Conn->GetRow('SELECT Data, Cached FROM '.TABLE_PREFIX.'Cache WHERE VarName = "mod_rw_'.md5($url).'"'); + $vars = $this->Conn->GetRow('SELECT Data, Cached FROM ' . TABLE_PREFIX . 'Cache WHERE VarName = "mod_rw_' . md5($url) . '"'); if (false && $vars && $vars['Cached'] > 0) { $vars = unserialize($menu['Data']); return $vars; @@ -428,12 +433,12 @@ $url_parts = $url ? explode('/', trim($url, '/')) : Array(); $process_module = true; - if($this->HTTPQuery->Get('rewrite') == 'on' || !$url_parts) + if ($this->HTTPQuery->Get('rewrite') == 'on' || !$url_parts) { $this->SetDefaultValues($vars); } - if(!$url_parts) + if (!$url_parts) { $this->InitAll(); @@ -450,14 +455,14 @@ $this->ProcessLanguage($url_parts, $vars); $this->ProcessTheme($url_parts, $vars); - // bug #1: cancells all other processing in case, when cms page is found - // bug #2: doesn't match template partially, e.g. "test/case/other" will not match to page "test/case" with "other" left for future processing if ( $this->ProcessFriendlyUrl($url_parts, $vars) ) { + // friendly urls work like exact match only! return $vars; } $this->ProcessCategory($url_parts, $vars); $this->SetDefaultPages($vars); + if ( $this->ProcessModuleIndex($url_parts, $vars) ) { foreach ($this->Application->ModuleInfo as $module_name => $info) { $passed[] = $info['Var']; @@ -471,9 +476,9 @@ /*// match module $next_template = $this->HTTPQuery->Get('next_template'); - if($url_part || $next_template) + if ($url_part || $next_template) { - if($next_template) + if ($next_template) { $next_template_parts = explode('/', $next_template); $module_folder = array_shift($next_template_parts); @@ -485,7 +490,7 @@ foreach ($this->Application->ModuleInfo as $module_name => $module_data) { - if( trim($module_data['TemplatePath'], '/') == $module_folder ) + if ( trim($module_data['TemplatePath'], '/') == $module_folder ) { $module_prefix = $module_data['Var']; break; @@ -505,7 +510,7 @@ foreach ($this->Application->ModuleInfo as $module_name => $module_data) { if (!$module_data['TemplatePath']) continue; - if( preg_match('/^'.preg_quote($module_data['TemplatePath'], '/').'/i', $vars['t']) ) + if ( preg_match('/^' . preg_quote($module_data['TemplatePath'], '/') . '/i', $vars['t']) ) { $module_prefix = $module_data['Var']; break; @@ -552,12 +557,12 @@ $module_params = Array(); if ( isset($module_prefix) ) { $passed[] = $module_prefix; - $module_event = new kEvent($module_prefix.':ParseEnv', Array('url_parts' => $url_parts) ); + $module_event = new kEvent($module_prefix . ':ParseEnv', Array('url_parts' => $url_parts) ); if ($process_module) { $this->Application->HandleEvent($module_event); } - $item_id = $this->HTTPQuery->Get($module_prefix.'_id'); - $module_params = Array($module_prefix.'_id' => $item_id ? $item_id : '0' ); + $item_id = $this->HTTPQuery->Get($module_prefix . '_id'); + $module_params = Array($module_prefix . '_id' => $item_id ? $item_id : '0' ); if ($module_event->status == erFAIL) { $not_found = $this->Application->ConfigValue('ErrorTemplate');