Index: trunk/core/kernel/application.php =================================================================== diff -u -r5505 -r6093 --- trunk/core/kernel/application.php (.../application.php) (revision 5505) +++ trunk/core/kernel/application.php (.../application.php) (revision 6093) @@ -136,8 +136,20 @@ */ var $EventManager = null; + /** + * Ref to itself, needed because everybody used to write $this->Application, even inside kApplication + * + * @var kApplication + */ var $Application = null; + /** + * Ref for TemplatesChache + * + * @var TemplatesCache + */ + var $TemplatesCache = null; + var $CompilationCache = array(); //used when compiling templates var $CachedProcessors = array(); //used when running compiled templates @@ -189,6 +201,8 @@ { if($this->InitDone) return false; + ob_start(); // collect any output from method (other then tags) into buffer + if(defined('DEBUG_MODE') && $this->isDebugMode() && constOn('DBG_PROFILE_MEMORY')) { $this->Debugger->appendMemoryUsage('Application before Init:'); } @@ -227,20 +241,21 @@ $this->HttpQuery->AfterInit(); - $this->UnitConfigReader->AfterConfigRead(); - $this->LoadCache(); $this->InitConfig(); - // Module items are recalled during url parsing & PhrasesCache is needed already there, + $this->Phrases->Init('phrases'); + + $this->UnitConfigReader->AfterConfigRead(); + + + /*// Module items are recalled during url parsing & PhrasesCache is needed already there, // because it's used in their build events. That's why phrases cache initialization is // called from kHTTPQuery in case when mod_rewrite is used if (!$this->RewriteURLs()) { $this->Phrases = new PhrasesCache(); - } + }*/ - $this->Phrases->Init('phrases'); - if(!$this->RecallVar('UserGroups')) { $session =& $this->recallObject('Session'); $user_groups = trim($session->GetField('GroupList'), ','); @@ -313,15 +328,7 @@ { $language_id = $this->GetVar('m_lang'); if (!$language_id) { - $language_id = $this->GetDefaultLanguageId(); - if (!$language_id) { - if (!$this->Application->IsAdmin()) { - die ('No Primary Language Selected'); - } - else { - $language_id = 1; - } - } + $language_id = 'default'; } $this->SetVar('lang.current_id', $language_id ); $this->SetVar('m_lang', $language_id ); @@ -330,7 +337,7 @@ $this->SetVar('lang_mode', ''); $lang =& $this->recallObject('lang.current'); if ( !$lang->IsLoaded() || (!$this->Application->IsAdmin() && !$lang->GetDBField('Enabled')) ) { - if (!defined('IS_INSTALL')) die ('Unknown or disabled language'); + if (!defined('IS_INSTALL')) $this->ApplicationDie('Unknown or disabled language'); } $this->SetVar('lang_mode',$lang_mode); } @@ -350,14 +357,14 @@ if (!$theme_id) { $theme_id = $this->GetDefaultThemeId(); if (!$theme_id) { - die('No Primary Theme Selected'); + $this->ApplicationDie('No Primary Theme Selected'); } } $this->SetVar('m_theme', $theme_id); $this->SetVar('theme.current_id', $theme_id ); // KOSTJA: this is to fool theme' getPassedId $theme =& $this->recallObject('theme.current'); if (!$theme->IsLoaded() || !$theme->GetDBField('Enabled')) { - if (!defined('IS_INSTALL')) die('Unknown or disabled theme'); + if (!defined('IS_INSTALL')) $this->ApplicationDie('Unknown or disabled theme'); } safeDefine('THEMES_PATH', '/themes/'.$theme->GetDBField('Name')); } @@ -375,6 +382,10 @@ WHERE (PrimaryLang = 1) AND (Enabled = 1)'; $language_id = $this->Conn->GetOne($sql); + if (!$language_id && defined('IS_INSTALL') && IS_INSTALL) { + $language_id = 1; + } + return $language_id; } @@ -501,20 +512,25 @@ } // this allows to save 2 sql queries for each category - $sql = 'SELECT NamedParentPath, CachedCategoryTemplate, CachedItemTemplate + $sql = 'SELECT NamedParentPath, CachedCategoryTemplate FROM '.$table.' WHERE '.$id_field.' = '.$this->Conn->qstr($id); $category_data = $this->Conn->GetRow($sql); $filename = $category_data['NamedParentPath']; $this->setCache('category_templates', $id, $category_data['CachedCategoryTemplate']); - $this->setCache('item_templates', $id, $category_data['CachedItemTemplate']); +// $this->setCache('item_templates', $id, $category_data['CachedItemTemplate']); } else { $resource_id = $this->Conn->GetOne('SELECT ResourceId FROM '.$table.' WHERE '.$id_field.' = '.$this->Conn->qstr($id)); if (is_null($category_id)) $category_id = $this->GetVar('m_cat_id'); $sql = 'SELECT Filename FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$resource_id.' AND CategoryId = '.$category_id; $filename = $this->Conn->GetOne($sql); + /*if (!$filename) { + $sql = 'SELECT Filename FROM '.TABLE_PREFIX.'CategoryItems WHERE ItemResourceId = '.$resource_id.' AND PrimaryCat = 1'; + $filename = $this->Conn->GetOne($sql); + }*/ + /*$sql = 'SELECT Filename FROM '.$table.' WHERE '.$id_field.' = '.$this->Conn->qstr($id); @@ -628,12 +644,11 @@ if (!$this->RequestProcessed) $this->ProcessRequest(); $this->InitParser(); - $template_cache =& $this->recallObject('TemplatesCache'); $t = $this->GetVar('t'); if ($this->isModuleEnabled('In-CMS')) { $cms_handler =& $this->recallObject('cms_EventHandler'); - if (!$template_cache->TemplateExists($t) && !$this->IsAdmin()) { + if (!$this->TemplatesCache->TemplateExists($t) && !$this->IsAdmin()) { $t = $cms_handler->GetDesignTemplate(); } /*else { @@ -645,7 +660,7 @@ $this->Debugger->appendMemoryUsage('Application before Parsing:'); } - $this->HTML = $this->Parser->Parse( $template_cache->GetTemplateBody($t), $t ); + $this->HTML = $this->Parser->ParseTemplate( $t ); if ($this->isDebugMode() && constOn('DBG_PROFILE_MEMORY')) { $this->Debugger->appendMemoryUsage('Application after Parsing:'); @@ -654,7 +669,10 @@ function InitParser() { - if( !is_object($this->Parser) ) $this->Parser =& $this->recallObject('TemplateParser'); + if( !is_object($this->Parser) ) { + $this->Parser =& $this->recallObject('TemplateParser'); + $this->TemplatesCache =& $this->recallObject('TemplatesCache'); + } } /** @@ -677,7 +695,26 @@ //eval("?".">".$this->HTML); - echo $this->HTML; + if ($this->isDebugMode()) { + $this->EventManager->RunRegularEvents(reAFTER); + $this->Session->SaveData(); + + $this->HTML = ob_get_clean() . $this->HTML . $this->Debugger->printReport(true); + } + else { + $this->HTML = ob_get_clean().$this->HTML; + } + + if ($this->UseOutputCompression()) { + header('Content-Encoding: gzip'); + $compression_level = $this->ConfigValue('OutputCompressionLevel'); + if ($compression_level < 0 || $compression_level > 9) $compression_level = 7; + echo gzencode($this->HTML, $compression_level); + } + else { + echo $this->HTML; + } + $this->UpdateCache(); flush(); @@ -687,21 +724,19 @@ $cache->printStatistics(); } - $event_manager =& $this->recallObject('EventManager'); - $event_manager->RunRegularEvents(reAFTER); - - $session =& $this->recallObject('Session'); - $session->SaveData(); - //$this->SaveBlocksCache(); + $this->EventManager->RunRegularEvents(reAFTER); + $this->Session->SaveData(); } - function SaveBlocksCache() + /** + * Checks if output compression options is available + * + * @return string + */ + function UseOutputCompression() { - /*if (constOn('EXPERIMENTAL_PRE_PARSE')) { - $data = serialize($this->PreParsedCache); - - $this->Conn->Query('REPLACE '.TABLE_PREFIX.'Cache (VarName, Data, Cached) VALUES ("blocks_cache", '.$this->Conn->qstr($data).', '.adodb_mktime().')'); - }*/ + if (constOn('IS_INSTALL') || constOn('DBG_ZEND_PRESENT') || constOn('SKIP_OUT_COMPRESSION')) return false; + return $this->ConfigValue('UseOutputCompression') && function_exists('gzencode') && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'); } // Facade @@ -884,11 +919,13 @@ $this->cache[$name] = $tpl; } + /* Seems to be not used anywhere... /Kostja + function SetTemplateBody($title,$body) { $templates_cache =& $this->recallObject('TemplatesCache'); $templates_cache->SetTemplateBody($title,$body); - } + }*/ function ProcessTag($tag_data) { @@ -979,10 +1016,14 @@ $index_file = $this->getIndexFile($prefix, $index_file, $params); + if (isset($params['_auto_prefix_'])) { + unset($params['_auto_prefix_']); // this is parser-related param, do not need to pass it here + } + $ssl = isset($params['__SSL__']) ? $params['__SSL__'] : null; if ($ssl !== null) { $session =& $this->recallObject('Session'); - $cookie_url = $session->CookieDomain.$session->CookiePath; + $cookie_url = trim($session->CookieDomain.$session->CookiePath, '/.'); if ($ssl) { $target_url = $this->ConfigValue('SSL_URL'); } @@ -1039,10 +1080,27 @@ unset($params['__NO_REWRITE__']); } - if ($this->RewriteURLs($ssl) && !$no_rewrite) + $force_rewrite = false; + if( isset($params['__MOD_REWRITE__']) ) { + $force_rewrite = true; + unset($params['__MOD_REWRITE__']); + } + + $force_no_sid = false; + if( isset($params['__NO_SID__']) ) + { + $force_no_sid = true; + unset($params['__NO_SID__']); + } + + // append pass through variables to each link to be build + $params = array_merge_recursive2($this->getPassThroughVariables($params), $params); + + if ($force_rewrite || ($this->RewriteURLs($ssl) && !$no_rewrite)) + { $session =& $this->recallObject('Session'); - if( $session->NeedQueryString() ) $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; } @@ -1055,7 +1113,42 @@ return $ret; } + /** + * Returns variables with values that should be passed throught with this link + variable list + * + * @param Array $params + * @return Array + */ + function getPassThroughVariables(&$params) + { + static $cached_pass_through = null; + if (isset($params['no_pass_through']) && $params['no_pass_through']) { + unset($params['no_pass_through']); + return Array(); + } + + // because pass through is not changed during script run, then we can cache it + if (is_null($cached_pass_through)) { + + $cached_pass_through = Array(); + $pass_through = $this->Application->GetVar('pass_through'); + + if ($pass_through) { + // names of variables to pass to each link + $cached_pass_through['pass_through'] = $pass_through; + $pass_through = explode(',', $pass_through); + foreach ($pass_through as $pass_through_var) { + $cached_pass_through[$pass_through_var] = $this->Application->GetVar($pass_through_var); + } + } + + } + + return $cached_pass_through; + } + + /** * Returns sorted array of passed prefixes (to build url from) * @@ -1103,32 +1196,46 @@ unset($params['__SSL__']); } + $m_only = true; $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); + $require_rewrite = $this->findModule('Var', $prefix) && $this->getUnitOption($prefix, 'CatalogItem'); if ($require_rewrite) { // if next prefix is same as current, but with special => exclude current prefix from url $next_prefix = getArrayValue($pass_info, $pass_index + 1); if ($next_prefix) { $next_prefix = substr($next_prefix, 0, strlen($prefix) + 1); if ($prefix.'.' == $next_prefix) continue; } - - $ret .= '/'.$this->BuildModuleEnv_NEW($pass_element, $params, $pass_events); + $a = $this->BuildModuleEnv_NEW($pass_element, $params, $pass_events); + if ($a) { + $ret .= '/'.$a; + $m_only = false; + } } else { $env .= ':'.$this->BuildModuleEnv($pass_element, $params, $pass_events); } } $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']); + } - $ret = trim($ret, '/').'.html'; + if (!$m_only || !$cat_processed || !defined('EXP_DIR_URLS')) { + $ret = trim($ret, '/').'.html'; + } + else { + $ret .= '/'; + } + // $ret = trim($ret, '/').'/'; if($env) $params[ENV_VAR_NAME] = ltrim($env, ':'); } @@ -1260,7 +1367,9 @@ // to put category & item templates into cache $filename = $this->getFilename('c', $category_id); if ($item_id) { - $t = $this->getCache('item_templates', $category_id); + $mod_rw_helper =& $this->Application->recallObject('ModRewriteHelper'); + $t = $mod_rw_helper->GetItemTemplate($category_id, $pass_element); // $pass_element should be the last processed element +// $t = $this->getCache('item_templates', $category_id); } elseif ($category_id) { $t = $this->getCache('category_templates', $category_id); @@ -1317,24 +1426,28 @@ function Redirect($t='', $params=null, $prefix='', $index_file=null) { - if ($t == '' || $t === true) $t = $this->GetVar('t'); - - // pass prefixes and special from previous url $js_redirect = getArrayValue($params, 'js_redirect'); - if( isset($params['js_redirect']) ) 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'; + if (preg_match("/external:(.*)/", $t, $rets)) { + $location = $rets[1]; } + else { + if ($t == '' || $t === true) $t = $this->GetVar('t'); - $params['__URLENCODE__'] = 1; - $location = $this->HREF($t, $prefix, $params, $index_file); + // pass prefixes and special from previous url + if( isset($params['js_redirect']) ) 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
"; + } + $a_location = $location; $location = "Location: $location"; - //echo " location : $location
"; if ($this->isDebugMode() && constOn('DBG_REDIRECT')) { @@ -1359,14 +1472,15 @@ } else { // no output before -> redirect using HTTP header + +// header('HTTP/1.1 302 Found'); header("$location"); } } } - $session =& $this->recallObject('Session'); - $session->SaveData(); - $this->SaveBlocksCache(); + ob_end_flush(); + $this->Session->SaveData(); exit; } @@ -1945,8 +2059,9 @@ function LoggedIn() { - $user =& $this->recallObject('u'); - $user_id = $user->GetID(); + $user_id = $this->Application->RecallVar('user_id'); +// $user =& $this->recallObject('u'); +// $user_id = $user->GetID(); $ret = $user_id > 0; if ($this->IsAdmin() && ($user_id == -1)) { @@ -2066,6 +2181,17 @@ return 3600 * ($target_zone - $this->ConfigValue('Config_Server_Time')); } + function ApplicationDie($message = '') + { + $message = ob_get_clean().$message; + if ($this->isDebugMode()) { + $message .= $this->Debugger->printReport(true); + } + + echo $this->UseOutputCompression() ? gzencode($message, DBG_COMPRESSION_LEVEL) : $message; + exit; + } + } ?> \ No newline at end of file