Index: branches/RC/core/units/general/helpers/mod_rewrite_helper.php =================================================================== diff -u -N -r11838 -r11843 --- branches/RC/core/units/general/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 11838) +++ branches/RC/core/units/general/helpers/mod_rewrite_helper.php (.../mod_rewrite_helper.php) (revision 11843) @@ -17,7 +17,7 @@ function SetDefaultValues(&$vars) { - $defaults = Array('m_cat_id' => 0, 'm_cat_page' => 1, 'm_opener' => 's'); + $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) { @@ -26,9 +26,16 @@ } } + /** + * Gets language part from url + * + * @param Array $url_parts + * @param Array $vars + * @return bool + */ function ProcessLanguage(&$url_parts, &$vars) { - if (!isset($url_parts[0])) { + if (!$url_parts) { return false; } @@ -39,6 +46,7 @@ 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) { $vars['m_lang'] = $language_id; @@ -53,9 +61,16 @@ return $res; } + /** + * Gets theme part from url + * + * @param Array $url_parts + * @param Array $vars + * @return bool + */ function ProcessTheme(&$url_parts, &$vars) { - if (!isset($url_parts[0])) { + if (!$url_parts) { return false; } @@ -79,9 +94,16 @@ return $res; } - function ProcessCategory(&$url_parts, &$vars) + /** + * Extracts category part from url + * + * @param Array $url_parts + * @param Array $vars + * @return bool + */ + function ProcessCategory($url_parts, &$vars) { - if (!isset($url_parts[0])) { + if (!$url_parts) { return false; } @@ -94,7 +116,7 @@ do { $category_path = trim($category_path . '/' . $url_part, '/'); - // bb__ -> forums/bb__2 OR forums/bb_2_6 + // bb_ -> forums/bb_2 if ( !preg_match('/^bb_[\d]+$/', $url_part) && preg_match('/(.*)_([\d]+)$/', $category_path, $rets) ) { $category_path = $rets[1]; $vars['m_cat_page'] = $rets[2]; @@ -131,33 +153,19 @@ $vars['t'] = strtolower( preg_replace('/^Content\//i', '', $last_category_info['NamedParentPath']) ); $vars['m_cat_id'] = $last_category_info['CategoryId']; - $vars['is_virtual'] = true; // for template from POST + $vars['is_virtual'] = true; // for template from POST, strange code there! } else { $vars['m_cat_id'] = 0; } - if ($url_part) { + /*if ($url_part) { array_unshift($url_parts, $url_part); - } + }*/ return $res; } - function ProcessPage(&$url_parts, &$vars) - { - if (!is_numeric($url_parts[0]) || count($url_parts) > 1) 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'] = $url_parts[0]; - $vars[ $module_data['Var'] . '_Reviews_Page'] = 0; - } - return true; - } - /** * Set's 1st page for all modules (will be used when not passed in url) * @@ -168,99 +176,88 @@ { // 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; + $vars[$module_data['Var'] . '_Page'] = 1; } } - function ProcessModuleIndex(&$url_parts, &$vars) + function _processTopic($url_parts, &$vars, $set_template = true) { - if ( count($url_parts) > 1) return false; // if no more parts or only 1 part left - if ( $url_parts && $url_parts[0] == 'index') { // treat index same as no parts - array_shift($url_parts); - } + $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); - 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) { - $url_parts[0] = $topic_page; - } - } - - if (!is_numeric($url_parts[0])) return false; - $this->ProcessPage($url_parts, $vars); + if ($set_template) { + $item_template = $this->GetItemTemplate($cat_item, 'bb'); + $vars['t'] = $item_template; } - /*$sql = 'SELECT CachedTemplate - FROM ' . TABLE_PREFIX . 'Category - WHERE CategoryId = ' . $vars['m_cat_id']; - $vars['t'] = $this->Conn->GetOne($sql);*/ + $this->Application->HandleEvent($bb_event, 'bb:ParseEnv', Array ('url_parts' => $url_parts, 'vars' => $vars)); + $vars = $bb_event->getEventParam('vars'); - if (!$vars['t']) { - $vars['t'] = 'index'; - } - return true; + return 'bb'; } - function ProcessModuleItem(&$url_parts, &$vars, $set_t = true) + function ProcessModuleItem(&$url_parts, &$vars, $set_template = true) { - 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']; - $cat_item = $this->Conn->GetRow($sql); - - if ($set_t) { - $item_template = $this->GetItemTemplate($cat_item, 'bb'); - $vars['t'] = $item_template; - } - - $this->Application->HandleEvent($bb_event, 'bb:ParseEnv', Array('url_parts' => $url_parts, 'vars' => $vars)); - $vars = $bb_event->getEventParam('vars'); - - return 'bb'; + if (!$url_parts) { + return false; } - if (!isset($url_parts[0]) || (count($url_parts) != 1)) { + $item_filename = end($url_parts); + if (is_numeric($item_filename)) { + // this page, don't process here return false; } - $url_part = array_shift($url_parts); + if (preg_match('/^bb_[\d]+/', $item_filename)) { + // process topics separatly, because they don't use item filenames + return $this->_processTopic($url_parts, $vars, $set_template); + } // 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 - ON c.CategoryId = ci.CategoryId - WHERE - ci.CategoryId = ' . $vars['m_cat_id'] . ' - AND - ci.Filename = ' . $this->Conn->qstr($url_part)); + $sql = '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'] . ') AND (ci.Filename = ' . $this->Conn->qstr($item_filename) . ')'; + $cat_item = $this->Conn->GetRow($sql); - if ($cat_item !== false) { // item found + if ($cat_item !== false) { + // item found $module_prefix = $cat_item['ItemPrefix']; $item_template = $this->GetItemTemplate($cat_item, $module_prefix); // 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'] - ); - if ((!$set_t || $item_template) && $item_id) { - if ($set_t) { + $sql = 'SELECT ' . $module_config['IDField'] . ' + FROM ' . $module_config['TableName'] . ' + WHERE ResourceId = ' . $cat_item['ItemResourceId']; + $item_id = $this->Conn->GetOne($sql); + + array_pop($url_parts); + + if ((!$set_template || $item_template) && $item_id) { + if ($set_template) { $vars['t'] = $item_template; } - $vars[ $module_prefix . '_id' ] = $item_id; + $vars[$module_prefix . '_id'] = $item_id; + return $module_prefix; } } - array_unshift($url_parts, $url_part); + return false; } + /** + * Returns module item details template specified in given category custom field for given module prefix + * + * @param mixed $category + * @param string $module_prefix + * @return string + */ function GetItemTemplate($category, $module_prefix) { $cache_key = serialize($category) . '_' . $module_prefix; @@ -285,21 +282,27 @@ $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 . ', + $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'; + 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'; $item_template = $this->Conn->GetOne($query); $this->Application->setCache(__CLASS__ . __FUNCTION__, $cache_key, $item_template); return $item_template; } + /** + * Returns category custom field id, where given module prefix item template name is stored + * + * @param string $module_prefix + * @return int + */ function _getItemTemplateCustomField($module_prefix) { $cached_value = $this->Application->getCache(__CLASS__ . __FUNCTION__, $module_prefix); @@ -317,13 +320,13 @@ return $item_template_field_id; } - function ProcessPhisycalTemplate(&$url_parts, &$vars) + function ProcessPhisycalTemplate($url_parts, &$vars) { - if (!isset($url_parts[0])) return false; + if (!$url_parts) { + return false; + } - $remaining = array(); - do - { + do { $template_path = implode('/', $url_parts); $t_parts['path'] = dirname($template_path) == '.' ? '' : '/' . dirname($template_path); @@ -332,33 +335,61 @@ $sql = 'SELECT FileId 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'); $template_found = $this->Conn->GetOne($sql); - if (!$template_found) - { - array_unshift($remaining, array_pop($url_parts)); + + if (!$template_found) { + array_shift($url_parts); } } while (!$template_found && $url_parts); - $url_parts = $remaining; - if ($template_found) { $vars['t'] = $template_path; return true; } + return false; } /** + * Set's page (when found) to all modules + * + * @param Array $url_parts + * @param Array $vars + * @return string + */ + function ProcessPage(&$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->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) + function ProcessFriendlyUrl($url_parts, &$vars) { - if (!isset($url_parts[0])) return false; + if (!$url_parts) { + return false; + } $sql = 'SELECT CategoryId, NamedParentPath FROM ' . TABLE_PREFIX . 'Category @@ -376,40 +407,56 @@ function processRewriteURL() { - $passed = array(); + $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) . '"'); + $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); - $this->Conn->Query('REPLACE ' . TABLE_PREFIX . 'Cache (VarName, Data, Cached) VALUES ("mod_rw_' . md5($url) . '", ' . $this->Conn->qstr(serialize($cache)) . ', ' . adodb_mktime() . ')'); + $passed = Array (); + $vars = $this->parseRewriteURL($url, $passed); + $cache = Array ('vars' => $vars, 'passed' => $passed); - if (isset($this->HTTPQuery->Post['t']) && $this->HTTPQuery->Post['t']) { + $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) - { + + foreach ($vars as $name => $value) { $this->HTTPQuery->Set($name,$value); } + // if ($restored) { $this->InitAll(); // } + $this->HTTPQuery->finalizeParsing($passed); } @@ -423,35 +470,38 @@ function parseRewriteURL($url, &$passed) { - $vars = $this->Conn->GetRow('SELECT Data, Cached FROM ' . TABLE_PREFIX . 'Cache WHERE VarName = "mod_rw_' . md5($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(); - $url_parts = $url ? explode('/', trim($url, '/')) : Array(); + $vars = Array (); + $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(); $vars['t'] = $this->HTTPQuery->getDefaultTemplate(''); $passed[] = 'm'; + return $vars; } - else - { + else { $vars['t'] = ''; } - $passed = Array('m'); + $passed = Array ('m'); $this->ProcessLanguage($url_parts, $vars); $this->ProcessTheme($url_parts, $vars); @@ -460,19 +510,55 @@ return $vars; } - $this->ProcessCategory($url_parts, $vars); + // http://site-url///[_]/