Index: branches/5.1.x/core/kernel/application.php =================================================================== diff -u -r12657 -r13086 --- branches/5.1.x/core/kernel/application.php (.../application.php) (revision 12657) +++ branches/5.1.x/core/kernel/application.php (.../application.php) (revision 13086) @@ -1,6 +1,6 @@ Application =& $instance; } + return $instance; } @@ -277,8 +299,12 @@ */ function Init() { - if($this->InitDone) return false; + if($this->InitDone) { + return false; + } + $this->isAdmin = constOn('ADMIN'); + $this->InitMemcached(); if (!constOn('SKIP_OUT_COMPRESSION')) { @@ -324,10 +350,7 @@ $this->Debugger->appendTimestamp('After UnitConfigReader'); } - $rewrite_on = $this->ConfigValue('UseModRewrite'); - // admin=1 - when front is browsed using admin session - $admin_on = getArrayValue($_REQUEST, 'admin') || $this->IsAdmin(); - define('MOD_REWRITE', $rewrite_on && !$admin_on ? 1 : 0); + define('MOD_REWRITE', $this->ConfigValue('UseModRewrite') && !$this->isAdmin ? 1 : 0); $this->HttpQuery =& $this->recallObject('HTTPQuery'); @@ -351,6 +374,7 @@ $this->StoreVar('UserGroups', $user_groups, true); // true for optional } + $this->LoadStructureTemplateMapping(); $this->HttpQuery->AfterInit(); $this->Session->ValidateExpired(); @@ -368,6 +392,8 @@ $this->Debugger->appendTimestamp('Loaded cache and phrases'); } + $this->ValidateLogin(); + $this->UnitConfigReader->AfterConfigRead(); if (defined('DEBUG_MODE') && $this->isDebugMode()) { @@ -394,8 +420,6 @@ mb_internal_encoding('UTF-8'); } - $this->ValidateLogin(); - if (defined('DEBUG_MODE') && $this->isDebugMode()) { $this->Debugger->profileFinish('kernel4_startup'); } @@ -465,18 +489,26 @@ function VerifyLanguageId() { $language_id = $this->GetVar('m_lang'); + if (!$language_id) { $language_id = 'default'; } + $this->SetVar('lang.current_id', $language_id ); $this->SetVar('m_lang', $language_id ); $lang_mode = $this->GetVar('lang_mode'); $this->SetVar('lang_mode', ''); + $lang =& $this->recallObject('lang.current'); - if ( !$lang->IsLoaded() || (!$this->Application->IsAdmin() && !$lang->GetDBField('Enabled')) ) { - if (!defined('IS_INSTALL')) $this->ApplicationDie('Unknown or disabled language'); + /* @var $lang kDBItem */ + + if (!$lang->isLoaded() || (!$this->isAdmin && !$lang->GetDBField('Enabled'))) { + if (!defined('IS_INSTALL')) { + $this->ApplicationDie('Unknown or disabled language'); + } } + $this->SetVar('lang_mode',$lang_mode); } @@ -486,7 +518,7 @@ */ function VerifyThemeId() { - if ($this->Application->IsAdmin()) { + if ($this->isAdmin) { safeDefine('THEMES_PATH', '/core/admin_templates'); return; } @@ -533,22 +565,30 @@ return $path; } - function GetDefaultLanguageId() + function GetDefaultLanguageId($init = false) { - static $language_id = 0; + static $language_info = null; - if ($language_id > 0) { - return $language_id; + if (!isset($language_info)) { + // cache primary language info first + $table = $this->getUnitOption('lang', 'TableName'); + $id_field = $this->getUnitOption('lang', 'IDField'); + + $sql = 'SELECT ' . $id_field . ', IF(AdminInterfaceLang, "Admin", "Front") AS LanguageKey + FROM ' . $table . ' + WHERE (AdminInterfaceLang = 1 OR PrimaryLang = 1) AND (Enabled = 1)'; + $language_info = $this->Conn->GetCol($sql, 'LanguageKey'); } - $table = $this->getUnitOption('lang', 'TableName'); - $id_field = $this->getUnitOption('lang', 'IDField'); + $language_key = ($this->isAdmin && $init) || count($language_info) == 1 ? 'Admin' : 'Front'; - $sql = 'SELECT '.$id_field.' - FROM '.$table.' - WHERE (PrimaryLang = 1) AND (Enabled = 1)'; - $language_id = $this->Conn->GetOne($sql); + if (array_key_exists($language_key, $language_info) && $language_info[$language_key] > 0) { + // get from cache + return $language_info[$language_key]; + } + $language_id = $language_info && array_key_exists($language_key, $language_info) ? $language_info[$language_key] : false; + if (!$language_id && defined('IS_INSTALL') && IS_INSTALL) { $language_id = 1; } @@ -567,7 +607,7 @@ if (constOn('DBG_FORCE_THEME')) { $theme_id = DBG_FORCE_THEME; } - elseif (!$force_front && $this->IsAdmin()) { + elseif (!$force_front && $this->isAdmin) { $theme_id = 999; } else { @@ -804,7 +844,7 @@ $this->Debugger->appendMemoryUsage('Application before Run:'); } - if ($this->IsAdmin()) { + if ($this->isAdminUser) { // for permission checking in events & templates $this->LinkVar('module'); // for common configuration templates $this->LinkVar('module_key'); // for common search templates @@ -813,8 +853,6 @@ if ($this->GetVar('m_opener') == 'p') { $this->LinkVar('main_prefix'); // window prefix, that opened selector $this->LinkVar('dst_field'); // field to set value choosed in selector -// $this->LinkVar('return_template'); // template to go, when something was coosen from popup (from finalizePopup) -// $this->LinkVar('return_m'); // main env part to restore after popup will be closed (from finalizePopup) } if ($this->GetVar('ajax') == 'yes' && !$this->GetVar('debug_ajax')) { @@ -829,7 +867,7 @@ $perm_helper =& $this->recallObject('PermissionsHelper'); /* @var $perm_helper kPermissionsHelper */ - if ($perm_helper->CheckUserPermission($user, 'CATEGORY.MODIFY', 0, 0)) { + if ($perm_helper->CheckUserPermission($user, 'CATEGORY.MODIFY', 0, $this->ModuleInfo['Core']['RootCat'])) { // user can edit cms blocks $editing_mode = $this->GetVar('editing_mode'); define('EDITING_MODE', $editing_mode ? $editing_mode : EDITING_MODE_BROWSE); @@ -844,7 +882,7 @@ $this->InitParser(); $t = $this->GetVar('t'); - if (!$this->TemplatesCache->TemplateExists($t) && !$this->IsAdmin()) { + if (!$this->TemplatesCache->TemplateExists($t) && !$this->isAdmin) { $cms_handler =& $this->recallObject('st_EventHandler'); /* @var $cms_handler CategoriesEventHandler */ @@ -927,7 +965,7 @@ $this->Session->SaveData(); } - if (defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !$this->IsAdmin()) { + if (defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !$this->isAdmin) { $this->_storeStatistics(); } } @@ -1270,7 +1308,7 @@ */ function IncludeTemplate($params) { - return $this->Parser->IncludeTemplate($params, isset($block_params['is_silent']) ? 1 : 0); + return $this->Parser->IncludeTemplate($params, isset($params['is_silent']) ? 1 : 0); } /** @@ -1308,28 +1346,51 @@ * @param string $t Template path * @var string $prefix index.php prefix - could be blank, 'admin' */ - function HREF($t, $prefix='', $params=null, $index_file=null) + function HREF($t, $prefix = '', $params = null, $index_file = null) { - if(!$t) $t = $this->GetVar('t'); // moved from kMainTagProcessor->T() + static $theme_id = null; + if (!isset($theme_id)) { + $theme_id = $this->GetVar('m_theme'); + } + + if (!$t) { + // when template not specified, use current + $t = $this->GetVar('t'); + } + $t = preg_replace('/^Content\//i', '', $t); + if (substr($t, -4) == '.tpl') { + // cut template extension (deprecated link format) + $t = substr($t, 0, strlen($t) - 4); + } - /*if ($this->GetVar('skip_last_template')) { - $params['opener'] = 'p'; - $this->SetVar('m_opener', 'p'); + if (substr($t, 0, 3) == 'id:') { + // link to structure page using it's id + $params['m_cat_id'] = substr($t, 3); + $t = $this->structureTemplateMapping[$t]; } - if ($t == 'incs/close_popup') { - // because this template closes the popup and we don't need popup mark here anymore - $params['m_opener'] = 's'; - }*/ + if (array_key_exists('use_section', $params)) { + $use_section = $params['use_section']; + unset($params['use_section']); + } - if( substr($t, -4) == '.tpl' ) $t = substr($t, 0, strlen($t) - 4 ); + if (isset($use_section) && $use_section && array_key_exists($t . ':' . $theme_id, $this->structureTemplateMapping)) { + // structure template corresponding to given physical template + $t = $this->structureTemplateMapping[$t . ':' . $theme_id]; + unset($params['use_section']); + } - if ( $this->IsAdmin() && $prefix == '') $prefix = ADMIN_DIRECTORY; - if ( $this->IsAdmin() && $prefix == '_FRONT_END_') $prefix = ''; + if (preg_match('/external:(.*)/', $t, $rets)) { + // external url + return $rets[1]; + } + if ($this->isAdmin && $prefix == '') $prefix = ADMIN_DIRECTORY; + if ($this->isAdmin && $prefix == '_FRONT_END_') $prefix = ''; + $index_file = $this->getIndexFile($prefix, $index_file, $params); if (isset($params['_auto_prefix_'])) { @@ -1857,29 +1918,31 @@ function Redirect($t='', $params=null, $prefix='', $index_file=null) { $js_redirect = getArrayValue($params, 'js_redirect'); - if (preg_match("/external:(.*)/", $t, $rets)) { - $location = $rets[1]; + + if ($t == '' || $t === true) { + $t = $this->GetVar('t'); } - else { - if ($t == '' || $t === true) $t = $this->GetVar('t'); - // pass prefixes and special from previous url - if( isset($params['js_redirect']) ) unset($params['js_redirect']); + // pass prefixes and special from previous url + if (array_key_exists('js_redirect', $params)) { + unset($params['js_redirect']); + } - if (!isset($params['pass'])) $params['pass'] = 'all'; - if ($this->GetVar('ajax') == 'yes' && $t == $this->GetVar('t')) { - // redirects to the same template as current - $params['ajax'] = 'yes'; - } - $params['__URLENCODE__'] = 1; - $location = $this->HREF($t, $prefix, $params, $index_file); - //echo " location : $location
"; + if (!array_key_exists('pass', $params)) { + $params['pass'] = 'all'; } + if ($this->GetVar('ajax') == 'yes' && $t == $this->GetVar('t')) { + // redirects to the same template as current + $params['ajax'] = 'yes'; + } + + $params['__URLENCODE__'] = 1; + $location = $this->HREF($t, $prefix, $params, $index_file); + $a_location = $location; $location = "Location: $location"; - if ($this->isDebugMode() && constOn('DBG_REDIRECT')) { $this->Debugger->appendTrace(); echo "Debug output above!!! Proceed to redirect: $a_location
"; @@ -1920,9 +1983,9 @@ exit; } - function Phrase($label, $allow_editing = true) + function Phrase($label, $allow_editing = true, $use_admin = false) { - return $this->Phrases->GetPhrase($label, $allow_editing); + return $this->Phrases->GetPhrase($label, $allow_editing, $use_admin); } /** @@ -1952,16 +2015,22 @@ { $session =& $this->recallObject('Session'); $user_id = $session->GetField('PortalUserId'); - if (!$user_id && $user_id != -1) $user_id = -2; + + if (!$user_id && $user_id != -1) { + $user_id = -2; + } + $this->SetVar('u.current_id', $user_id); - if (!$this->IsAdmin()) { + if (!$this->isAdmin) { // needed for "profile edit", "registration" forms ON FRONT ONLY $this->SetVar('u_id', $user_id); } $this->StoreVar('user_id', $user_id, $user_id == -2); // storing Guest user_id (-2) is optional + $this->isAdminUser = $this->isAdmin && $this->LoggedIn(); + if ($this->GetVar('expired') == 1) { // this parameter is set only from admin $user =& $this->recallObject('u.current'); @@ -1989,7 +2058,7 @@ } function LoadCache() { - $cache_key = $this->GetVar('t').$this->GetVar('m_theme').$this->GetVar('m_lang').$this->IsAdmin(); + $cache_key = $this->GetVar('t').$this->GetVar('m_theme').$this->GetVar('m_lang').$this->isAdmin; $query = sprintf("SELECT PhraseList, ConfigVariables FROM %s WHERE Template = %s", TABLE_PREFIX.'PhraseCache', $this->Conn->qstr(md5($cache_key))); @@ -2010,14 +2079,28 @@ $this->ConfigCacheIds = $config_ids; } + function LoadStructureTemplateMapping() + { + // get template mapping + $sql = 'SELECT Data + FROM ' . TABLE_PREFIX . 'Cache + WHERE VarName = "template_mapping"'; + $template_mapping = $this->Conn->GetOne($sql); + + if (!$this->isAdmin && $template_mapping) { + // template mappings only for Front-End + $this->structureTemplateMapping = unserialize($template_mapping); + } + } + function UpdateCache() { $update = false; //something changed $update = $update || $this->Phrases->NeedsCacheUpdate(); $update = $update || (count($this->ConfigCacheIds) && $this->ConfigCacheIds != $this->Caches['ConfigVariables']); if ($update) { - $cache_key = $this->GetVar('t').$this->GetVar('m_theme').$this->GetVar('m_lang').$this->IsAdmin(); + $cache_key = $this->GetVar('t').$this->GetVar('m_theme').$this->GetVar('m_lang').$this->isAdmin; $query = sprintf("REPLACE %s (PhraseList, CacheDate, Template, ConfigVariables) VALUES (%s, %s, %s, %s)", TABLE_PREFIX.'PhraseCache', @@ -2263,17 +2346,6 @@ } /** - * Checks if it is admin - * - * @return bool - * @author Alex - */ - function IsAdmin() - { - return constOn('ADMIN'); - } - - /** * Apply url rewriting used by mod_rewrite or not * * @param bool $ssl Force ssl link to be build @@ -2628,7 +2700,7 @@ */ function setVisitField($field, $value) { - if ($this->IsAdmin() || !$this->ConfigValue('UseVisitorTracking')) { + if ($this->isAdmin || !$this->ConfigValue('UseVisitorTracking')) { // admin logins are not registred in visits list return ; }